]> code.citadel.org Git - citadel.git/blobdiff - citadel/whobbs.c
* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / whobbs.c
index b59543bc26991c22e936af1787d0dad11968da95..82d9f0d26716c33ecb0b001972172cb1965da003 100644 (file)
@@ -1,10 +1,18 @@
-/* $Id$ */
+/*
+ * $Id$
+ * 
+ * Command-line "who is online?" utility
+ *
+ */
+
+#include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 #include "citadel.h"
-#include "ipc.h"
+#include "citadel_ipc.h"
 #include "tools.h"
 
 void logoff(int code)
@@ -12,7 +20,7 @@ void logoff(int code)
        exit(code);
        }
 
-void escapize(char buf[]) {
+static void escapize(char *buf, size_t n) {
        char hold[512];
        int i;
 
@@ -20,14 +28,16 @@ void escapize(char buf[]) {
        strcpy(buf, "");
 
        for (i=0; i<strlen(hold); ++i) {
+               size_t tmp = strlen(buf);
+
                if (hold[i]=='<')
-                       sprintf(&buf[strlen(buf)], "&lt;");
+                       snprintf(&buf[tmp], n - tmp, "&lt;");
                else if (hold[i]=='>')
-                       sprintf(&buf[strlen(buf)], "&gt;");
+                       snprintf(&buf[tmp], n - tmp, "&gt;");
                else if (hold[i]==34)
-                       sprintf(&buf[strlen(buf)], "&quot;");
+                       snprintf(&buf[tmp], n - tmp, "&quot;");
                else
-                       sprintf(&buf[strlen(buf)], "%c", hold[i]);
+                       snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
        }
 }
 
@@ -37,17 +47,20 @@ void escapize(char buf[]) {
 int main(int argc, char **argv)
 {
        char buf[512];
-       char nodetitle[256];
-       int a;
+       char nodetitle[SIZ];
        int www = 0;
        int s_pid = 0;
        int my_pid = 0;
-       char hostbuf[256];
-       char portbuf[256];
-       char s_user[256];
-       char s_room[256];
-       char s_host[256];
-       char s_client[256];
+       char hostbuf[SIZ];
+       char portbuf[SIZ];
+       char s_user[SIZ];
+       char s_room[SIZ];
+       char s_host[SIZ];
+       char s_client[SIZ];
+       int r;                  /* IPC response code */
+       time_t timenow;
+       char *listing = NULL;
+       CtdlIPC *ipc = NULL;
 
        /* If this environment variable is set, we assume that the program
         * is being called as a cgi-bin from a webserver and will output
@@ -55,23 +68,22 @@ int main(int argc, char **argv)
         */     
        if (getenv("REQUEST_METHOD") != NULL) www = 1;
 
-       attach_to_server(argc, argv, hostbuf, portbuf);
-       serv_gets(buf);
+       ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
+       if (!ipc) {
+               fprintf(stderr, "Server not available: %s\n", strerror(errno));
+               logoff(errno);
+       }
+       CtdlIPC_chat_recv(ipc, buf);
        if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
                fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
                logoff(atoi(buf));
                }
-       strcpy(nodetitle, "this BBS");
-       serv_puts("INFO");
-       serv_gets(buf);
-       if (buf[0]=='1') {
-               a = 0;
-               while (serv_gets(buf), strcmp(buf,"000")) {
-                       if (a==0) my_pid = atoi(buf);
-                       if (a==2) strcpy(nodetitle, buf);
-                       ++a;
-                       }
-               }
+       strcpy(nodetitle, "this Citadel site");
+       r = CtdlIPCServerInfo(ipc, buf);
+       if (r / 100 == 1) {
+               my_pid = ipc->ServInfo.pid;
+               strcpy(nodetitle, ipc->ServInfo.humannode);
+       }
        
        if (www) {
                printf( "Content-type: text/html\n"
@@ -95,10 +107,9 @@ int main(int argc, char **argv)
                printf("</H1>\n");
        }
 
-       serv_puts("RWHO");
-       serv_gets(buf);
-       if (buf[0]!='1') {
-               fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
+       r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
+       if (r / 100 != 1) {
+               fprintf(stderr,"%s: %s\n",argv[0], buf);
                logoff(atoi(buf));
        }
 
@@ -112,20 +123,22 @@ int main(int argc, char **argv)
                printf( "Session         User name               "
                        "Room                  From host\n");
                printf( "------- ------------------------- "
-                       "-------------------- ------------------------\n");
+                       "------------------- ------------------------\n");
        }
 
 
-       while (serv_gets(buf), strcmp(buf,"000")) {
+       while (strlen(listing) > 0) {
+               extract_token(buf, listing, 0, '\n', sizeof buf);
+               remove_token(listing, 0, '\n');
 
                /* Escape some stuff if we're using www mode */
-               if (www) escapize(buf);
+               if (www) escapize(buf, sizeof buf);
 
                s_pid = extract_int(buf,0);
-               extract(s_user,buf,1);
-               extract(s_room,buf,2);
-               extract(s_host,buf,3);
-               extract(s_client,buf,4);
+               extract_token(s_user, buf, 1, '|', sizeof s_user);
+               extract_token(s_room, buf, 2, '|', sizeof s_room);
+               extract_token(s_host, buf, 3, '|', sizeof s_host);
+               extract_token(s_client, buf, 4, '|', sizeof s_client);
                if (s_pid != my_pid) {
 
                        if (www) printf("<TR><TD>");
@@ -133,14 +146,15 @@ int main(int argc, char **argv)
                        printf("%c", 
                                ((s_pid == my_pid) ? '*' : ' '));
                        if (www) printf("</TD><TD>");
-                       printf("%-25s", s_user);
+                       printf("%-26s", s_user);
                        if (www) printf("</TD><TD>");
-                       printf("%-20s ", s_room);
+                       printf("%-19s ", s_room);
                        if (www) printf("</TD><TD>");
                        printf("%-24s\n", s_host);
                        if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
                        }
                }
+       free(listing);
 
        if (www) printf("</TABLE></CENTER>\n"
                        "<FONT SIZE=-1>"
@@ -148,10 +162,9 @@ int main(int argc, char **argv)
                        "once per minute)</FONT>\n"
                        "</BODY></HTML>\n");
 
-       serv_puts("QUIT");
-       serv_gets(buf);
-       return 0;
-       }
+       r = CtdlIPCQuit(ipc);
+       return (r / 100 == 2) ? 0 : r;
+}
 
 
 #ifndef HAVE_STRERROR
@@ -162,7 +175,7 @@ char *strerror(int e)
 {
        static char buf[32];
 
-       sprintf(buf,"errno = %d",e);
+       snprintf(buf, sizeof buf, "errno = %d",e);
        return(buf);
        }
 #endif