*/
#include "sockhelp.h"
+#include "options.h"
#include "list.h"
#include "aClient.h"
#include "extern.h"
#include <unistd.h>
#include <string.h>
#include <iostream.h>
+#include <fstream.h>
#include <iomanip.h>
-#include <time.h>
#include <stdlib.h>
+char *PACKAGE = "GameServ";
+char *VERSION = "1.1.7";
int sock;
+long timestamp;
+
List<aClient> clients;
+void save_timestamp();
+void load_timestamp();
+
int main(int argc, char *argv[])
{
- char buffer[1024], buf[1024], input[1024], uplink[80], kb[1024];
+ char buffer[1024], buf[1024];
int connected = 1;
char *cmd, *source = NULL;
srand(time(NULL));
-
- load_config_file();
+
+ load_config_file();
if (argc == 1) {
argc = 3;
return -1;
}
+#ifdef UNREAL
raw("PROTOCTL NICKv2 VHP");
raw("PASS :%s", remotepass);
- raw("SERVER %s 1 :Testing Server", servername);
- raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :GameServ", time(NULL), gshost,
- servername, time(NULL), gshost);
+ raw("SERVER %s 1 :%s", servername, servername);
+ raw("NICK %S 1 %d %S %s %s %d +owghraAxNt %s :%s v%s", time(NULL), gshost,
+ servername, time(NULL), gshost, PACKAGE, VERSION);
raw(":%S JOIN %s", c_Forest);
+ raw(":%S MODE %s +mtn", c_Forest);
+#elif defined(BAHAMUT)
+ raw("PASS %s :TS", remotepass);
+ raw("SERVER %s 1 :%s", servername, servername);
+ raw("NICK %S 1 %d +o %s %s %s 0 :GameServ", time(NULL), gsident, gshost,
+ servername);
+ raw(":%s SJOIN %d %d %s +mnt :@%S", servername, time(NULL), time(NULL), c_Forest);
+#endif
raw(":%S MODE %s +o %S", c_Forest);
- raw(":%S MODE %s +ntm", c_Forest);
+ raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
sock_gets(sock,buffer,sizeof(buffer)-1); /* -1 added thanks to
David Duchene <dave@ltd.com> for pointing out the possible
printf("Server: %s\n",buffer);
- init_monsters();
- //load_gs_dbase();
+ init_masters();
+ load_gs_dbase();
+ load_timestamp();
+ long int loadtime = time(NULL);
+ long int currentTime;
+ long int oldTime = loadtime;
+ bool loaded = false;
+
+ if (load_monsters() == false)
+ goto end;
+
while (connected) {
if (sock_gets(sock,buffer,sizeof(buffer)) == -1) {
connected = 0;
cmd = strtok(buf, " ");
cout << "Server: " << buffer << endl << flush;
+
+ // Wait N seconds then we're loaded.
+ if (!loaded)
+ {
+ if (time(NULL) >= welcomedelay + loadtime)
+ loaded = true;
+ }
+
+ // Save the player data every updateperiod seconds
+ currentTime = time(NULL);
+ if (currentTime - oldTime >= updateperiod)
+ {
+ oldTime = currentTime;
+ save_gs_dbase();
+ }
+
if (stricmp(cmd, "PING") == 0) {
char *timestamp;
timestamp = strtok(NULL, "");
raw("PONG %s", timestamp);
+ } else if (stricmp(cmd, "VERSION") == 0) {
+ char *server;
+ server = strtok(NULL, " ");
+ server++;
+ raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername);
} else if (strncmp(cmd, "NICK", 4) == 0) {
if (buffer[0] == ':')
{
aClient *tempPtr;
- if (tempPtr = find((source + 1)))
+ if ((tempPtr = find((source + 1))))
{
char *nick;
nick = strtok(NULL, " ");
aClient *newuser;
nick = strtok(NULL, " ");
newuser = new aClient(nick);
+ if (loaded)
+ notice(s_GameServ, nick, welcomemsg, nick);
+
clients.insertAtBack(newuser);
delete newuser;
}
} else if (stricmp(cmd, "QUIT") == 0) {
aClient *quitter;
- if (quitter = find(source + 1))
+ if ((quitter = find(source + 1)))
clients.remove(quitter);
- if (quitter = findplayer(source + 1))
- players.remove(quitter);
+ if ((quitter = findIRCplayer(source + 1)))
+ {
+ quitter->setNick("!NULL!");
+ quitter->stats->user = NULL; // Unidentify them
+ }
} else if (stricmp(cmd, "PRIVMSG") == 0) {
char *rest, *dest;
dest = strtok(NULL, " ");
rest = strtok(NULL, "");
- if (stricmp(dest, s_GameServ) == 0)
+ if (strnicmp(dest, s_GameServ, strlen(s_GameServ)) == 0)
gameserv(source, rest);
else if (stricmp(dest, c_Forest) == 0)
forest(source, rest);
channel = strtok(NULL, " ");
if (stricmp(channel, c_Forest) == 0 && is_playing(source + 1))
raw(":%S MODE %s +v %s", c_Forest, (source + 1));
+
+ #if defined(BAHAMUT)
+ } else if (stricmp(cmd, "SJOIN") == 0) {
+ char *channel, *nick;
+ strtok(NULL, " "); // Ignore the TS
+ strtok(NULL, " "); // Ignore the TS
+ channel = strtok(NULL, " ");
+ strtok(NULL, " ");
+ nick = strtok(NULL, " ");
+ nick++; // Get rid of the :
+ if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
+ raw(":%S MODE %s +v %s", channel, nick);
+ #endif
+
} else {
// cout << "Unrecognized Message: cmd = " << cmd << setw(30) << "source = " <<
// source << endl;
}
}
+
+ end:
+
+ save_gs_dbase();
+ save_timestamp();
+
+ delete_monsters();
+ delete_masters();
+
printf("<CLOSED>\n");
close(sock);
unload_config_file();
return client;
}
+aClient *findIRCplayer(const char *nick)
+{
+ ListNode <aClient> *newPtr;
+ aClient *p = NULL;
+
+ for (newPtr = players.First(); newPtr; newPtr = newPtr->Next())
+ {
+ p = newPtr->getData();
+ if (stricmp(p->getNick(), nick) == 0)
+ return p;
+ p = NULL;
+ }
+ return NULL;
+}
aClient *findplayer(const char *name)
{
ListNode <aClient> *newPtr;
return client;
}
+void load_timestamp()
+{
+ ifstream infile;
+
+ infile.open(".gstimestamp");
+
+ if (infile.fail())
+ {
+ cout << "Error opening .gstimestamp" << endl;
+ cout << "Generating new timestamp" << endl;
+ generate:
+ timestamp = midnight();
+ save_timestamp();
+ return;
+ }
+
+ infile >> timestamp;
+ infile.close();
+ if (timestamp < 1000000)
+ goto generate;
+}
+
+void save_timestamp()
+{
+ ofstream outfile;
+
+ outfile.open(".gstimestamp");
+
+ if (outfile.fail())
+ {
+ cout << "Error creating new file." << endl;
+ return;
+ }
+
+ outfile << timestamp << endl;
+
+ outfile.close();
+}
+
+long int midnight(long int offset)
+{
+ return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
+}