]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/tcpclient.cpp
Made config.cpp output errors now that it's a true daemon!
[irc/gameservirc.git] / gameserv / tcpclient.cpp
index 64053d287c098c56c540efd710eb0c8f8e3b6682..12e86cc430c0f954f22abdfd9e30b595e26b5385 100644 (file)
 #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.8";
 
 int sock;
 long timestamp;
@@ -37,6 +46,12 @@ List<aClient> clients;
 void save_timestamp();
 void load_timestamp();
 
+// Make this a daemon
+int daemon(int nochdir, int noclose);
+
+// Close all file descriptors from >= fd
+void closeall(int fd);
+
 int main()
 {
   char buffer[1024], buf[1024];
@@ -47,6 +62,13 @@ int main()
 
   load_config_file();          // default = gameserv.conf
 
+    // 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);
   if (sock == -1) {
@@ -69,6 +91,13 @@ int main()
         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);
+#elif defined(HYBRID)
+       raw("PASS %s :TS", remotepass);
+       raw("SERVER %s 1 :%s", servername, servername);
+       raw("NICK %S 1 %d +o %s %s %s :GameServ", time(NULL), gsident, gshost,
+               servername);
+       // Sending a timestamp of 1 to force ops.
+       raw(":%s SJOIN 1 %s +ntm :@%S", servername, c_Forest);
 #elif defined(P10)
        // Server numeric is: []  <-- must be unique
        raw("PASS :%s", remotepass);
@@ -80,8 +109,10 @@ int main()
 #if defined(P10)
        raw("%s T %s :%s", gsnum, c_Forest, c_ForestTopic);
        raw("[] EB");  // End burst
-#else   
+#else 
+#ifndef HYBRID
        raw(":%S MODE %s +o %S", c_Forest);
+#endif
        raw(":%S TOPIC %s :%s", c_Forest, c_ForestTopic);
 #endif
 
@@ -163,7 +194,7 @@ int main()
            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] == ':')
@@ -182,9 +213,10 @@ int main()
       #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] == '+')
@@ -209,9 +241,19 @@ int main()
 
                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;
@@ -276,17 +318,29 @@ int main()
 
        #if defined(BAHAMUT)
        } else if (stricmp(cmd, "SJOIN") == 0) {
-           char *channel, *nick;
+           char *channel, *nick, *tmp, *rest;
            strtok(NULL, " "); // Ignore the TS
+#ifndef HYBRID
            strtok(NULL, " "); // Ignore the TS
+#endif
            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
-
+           rest = strtok(NULL, "");
+           tmp = strchr(rest, ':');
+           tmp++;
+           nick = strtok(tmp, " ");
+           while (nick != NULL)
+           {
+               if (*nick == '@')
+                   nick++;
+               if (*nick == '+')
+                   nick++; // Assume for users set op and voice, they
+                            // are never passed as +@nick
+               if (stricmp(channel, c_Forest) == 0 && is_playing(nick))
+                   raw(":%S MODE %s +v %s", channel, nick);
+
+               nick = strtok(NULL, " ");
+           }
+#endif
        } else {
            #ifdef DEBUGMODE
                log("Unrecognized Message: cmd = %s   source = %s", cmd, source);
@@ -321,6 +375,34 @@ aClient *find(const char *nick)
        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)
 {
@@ -333,7 +415,6 @@ aClient *findbynick(char *nick)
     {
        client = newPtr->getData();
        #ifdef P10
-           log("Comparing %s with %s", client->getNick(), nick);
            if (strcmp(client->getNick(), nick) == 0)
        #else
            if (stricmp(client->getNick(), nick) == 0)
@@ -448,3 +529,68 @@ long int midnight(long int offset)
 {
     return (time(NULL) - (time(NULL) % 86400)) + (offset * 3600);
 }
+
+/* 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("gameserv.pid");
+               if (outfile.fail())
+                   cerr << "Unable to open gameserv.pid" << 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++);
+}
+
+