#include <string.h>
#include <fstream>
#include <stdlib.h>
+#include <fcntl.h>
+#include <signal.h>
+//#include <sys/types.h>
+//#include <sys/wait.h>
+//#include <errno.h>
+
+
using std::ofstream;
using std::ifstream;
+using std::cerr;
+using std::endl;
char *PACKAGE = "GameServ";
-char *VERSION = "1.1.7";
+char *VERSION = "1.1.9";
int sock;
-long timestamp;
+int day;
List<aClient> clients;
-void save_timestamp();
-void load_timestamp();
+void save_day();
+void load_day();
+void prettyIntro();
+
+// Make this a daemon
+int daemon(int nochdir, int noclose);
+
+// Close all file descriptors from >= fd
+void closeall(int fd);
-int main()
+int main(int argc, char *argv[])
{
char buffer[1024], buf[1024];
int connected = 1;
- char *cmd, *source = NULL;
+ char *cmd, *source = NULL, *conf = "gameserv.conf";
srand(time(NULL));
-
- load_config_file(); // default = gameserv.conf
+ if (argc > 1)
+ {
+ if ( argc > 2 || stricmp(argv[1], "--help") == 0)
+ {
+ cout << "Usage: gameserv [options] [configfile]" << endl;
+ cout << "Options:" << endl;
+ cout << "--help Displays this help dialogue" << endl;
+ return 1;
+ }
+ conf = new char[strlen(argv[1])];
+ strcpy(conf, argv[1]);
+ }
+
+ prettyIntro();
+
+ if (load_config_file(conf))
+ {
+ cout << "Config file loaded ok...\n"
+ << "Turning into a daemon" << endl;
+ }
+ else
+ exit(2);
+
+ // Turn into a daemon
+ if (daemon(1,0) < 0)
+ {
+ perror("Could not turn into a daemon");
+ exit(2);
+ }
ignore_pipe();
sock = make_connection(remoteport, SOCK_STREAM, remoteserver);
init_masters();
load_gs_dbase();
- load_timestamp();
+ load_day();
long int loadtime = time(NULL);
long int currentTime;
long int oldTime = loadtime;
char *server;
server = strtok(NULL, " ");
server++;
- raw(":%s 351 %s %s %s. %s", servername, source+1, PACKAGE, VERSION, servername);
+ raw(":%s 351 %s %s_%s. %s", servername, source+1, PACKAGE, VERSION, servername);
#if !defined(P10)
} else if (strncmp(cmd, "NICK", 4) == 0) {
if (buffer[0] == ':')
#else
} else if (stricmp(cmd, "N") == 0 && strlen(source) == 2) {
{
- char *nick;
+ char *nick, *realnick;
+ realnick = strtok(NULL, " ");
- for (int x = 0; x < 6; x++)
+ for (int x = 0; x < 5; x++)
nick = strtok(NULL, " ");
if (nick[0] == '+')
nick = strtok(NULL, " ");
- newuser = new aClient(nick);
+ #ifdef P10
+ newuser = new aClient(nick, realnick);
+ #else
+ newuser = new aClient(nick);
+ #endif
+
+
if (loaded)
+ #ifdef P10
+ notice(s_GameServ, nick, welcomemsg, realnick);
+ #else
notice(s_GameServ, nick, welcomemsg, nick);
+ #endif
clients.insertAtBack(newuser);
delete newuser;
end:
save_gs_dbase();
- save_timestamp();
+ save_day();
delete_monsters();
delete_masters();
return findbynick(nick);
}
+#ifdef P10
+
+aClient *findbyrealnick(char *realnick)
+{
+ ListNode <aClient> *newPtr;
+ newPtr = clients.First();
+
+ aClient *client = NULL;
+
+ while (newPtr)
+ {
+ client = newPtr->getData();
+ if (stricmp(client->getRealNick(), realnick) == 0)
+ return client;
+ client = NULL;
+ newPtr = newPtr->Next();
+ }
+ return client;
+}
+
+#else
+
+aClient *findbyrealnick(char *realnick)
+{
+ return findbynick(realnick);
+}
+
+#endif
aClient *findbynick(char *nick)
{
return client;
}
-void load_timestamp()
+void load_day()
{
ifstream infile;
- infile.open(".gstimestamp");
+ infile.open(".gsday");
if (infile.fail())
{
#ifdef DEBUGMODE
- log("Error opening .gstimestamp");
+ log("Error opening .gsday");
#endif
generate:
#ifdef DEBUGMODE
- log("Generating new timestamp");
+ log("Generating new day");
#endif
- timestamp = midnight();
- save_timestamp();
+ struct tm *tm;
+ time_t ti;
+ time(&ti);
+ tm = localtime(&ti);
+
+ day = tm->tm_mday;
+
+ save_day();
return;
}
- infile >> timestamp;
+ infile >> day;
infile.close();
- if (timestamp < 1000000)
+ if (day < 1 || day > 31)
goto generate;
}
-void save_timestamp()
+void save_day()
{
ofstream outfile;
- outfile.open(".gstimestamp");
+ outfile.open(".gsday");
if (outfile.fail())
{
- log("Error creating new file .gstimestamp");
+ log("Error creating new file .gsday");
return;
}
- outfile << timestamp << endl;
+ outfile << day << endl;
outfile.close();
}
-long int midnight(long int offset)
+/* daemon() - detach process from user and disappear into the background
+ * returns -1 on failure, but you can't do much except exit in that case
+ * since we may already have forked. This is based on the BSD version,
+ * so the caller is responsible for things like the umask, etc.
+ */
+
+/* believed to work on all Posix systems */
+
+int daemon(int nochdir, int noclose)
+{
+ pid_t pid;
+ switch (pid = fork())
+ {
+ case 0: break;
+ case -1: return -1;
+ default: _exit(0); /* exit the original process */
+ }
+
+ if (setsid() < 0) /* shoudn't fail */
+ return -1;
+
+ /* dyke out this switch if you want to acquire a control tty in */
+ /* the future -- not normally advisable for daemons */
+
+ switch (pid = fork())
+ {
+ case 0: break;
+ case -1: return -1;
+ default:
+ ofstream outfile;
+ outfile.open(pidfile);
+ if (outfile.fail())
+ cerr << "Unable to open " << pidfile << endl;
+ outfile << pid << endl;
+ outfile.close();
+
+ _exit(0);
+ }
+
+ if (!nochdir)
+ chdir("/");
+
+ if (!noclose)
+ {
+ closeall(0);
+ open("/dev/null",O_RDWR);
+ dup(0); dup(0);
+ }
+
+ return 0;
+}
+
+
+/* closeall() -- close all FDs >= a specified value */
+
+void closeall(int fd)
+{
+ int fdlimit = sysconf(_SC_OPEN_MAX);
+
+ while (fd < fdlimit)
+ close(fd++);
+}
+
+void prettyIntro()
{
- return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
+cout << endl;
+cout << " GGGG AAA MM MM EEEEEEE SSSSS EEEEEEE RRRRRR VV VV " << endl;
+cout << " GG GG AAAAA MMM MMM EE SS EE RR RR VV VV " << endl;
+cout << "GG AA AA MM MM MM EEEEE SSSSS EEEEE RRRRRR VV VV " << endl;
+cout << "GG GGG AAAAAAA MM MM EE SS EE RR RR VV VV " << endl;
+cout << "G G AA AA MM MM EEEEEEE SSSSS EEEEEEE RR RR VVV" << endl;
+cout << " GGGGG V\n\n" << endl;
+cout << "Version: " << VERSION << endl;
}