]> code.citadel.org Git - citadel.git/blobdiff - citadel/whobbs.c
* The size constant "256" which shows up everywhere as a buffer size has now
[citadel.git] / citadel / whobbs.c
index 189773f3ee0f7aceb1351464f0c61b8933b05e91..f22c8f0310a10458781659852b542cdbdc27c814 100644 (file)
@@ -1,83 +1,67 @@
+/*
+ * $Id$
+ * 
+ * Command-line "who is online?" utility
+ *
+ */
+
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <string.h>
 #include "citadel.h"
 #include "ipc.h"
+#include "tools.h"
 
-/*
- * num_parms()  -  discover number of parameters...
- */
-int num_parms(char *source)
+void logoff(int code)
 {
-       int a;
-       int count = 1;
-
-       for (a=0; a<strlen(source); ++a) 
-               if (source[a]=='|') ++count;
-       return(count);
+       exit(code);
        }
 
-
-/*
- * extract()  -  extract a parameter from a series of "|" separated...
- */
-void extract(char *dest, char *source, int parmnum)
-{
-       char buf[256];
-       int count = 0;
-       int n;
-
-       n = num_parms(source);
-
-       if (parmnum >= n) {
-               strcpy(dest,"");
-               return;
-               }
-       strcpy(buf,source);
-       if ( (parmnum == 0) && (n == 1) ) {
-               strcpy(dest,buf);
-               return;
-               }
-
-       while (count++ < parmnum) do {
-               strcpy(buf,&buf[1]);
-               } while( (strlen(buf)>0) && (buf[0]!='|') );
-       if (buf[0]=='|') strcpy(buf,&buf[1]);
-       for (count = 0; count<strlen(buf); ++count)
-               if (buf[count] == '|') buf[count] = 0;
-       strcpy(dest,buf);
+void escapize(char buf[]) {
+       char hold[512];
+       int i;
+
+       strcpy(hold, buf);
+       strcpy(buf, "");
+
+       for (i=0; i<strlen(hold); ++i) {
+               if (hold[i]=='<')
+                       sprintf(&buf[strlen(buf)], "&lt;");
+               else if (hold[i]=='>')
+                       sprintf(&buf[strlen(buf)], "&gt;");
+               else if (hold[i]==34)
+                       sprintf(&buf[strlen(buf)], "&quot;");
+               else
+                       sprintf(&buf[strlen(buf)], "%c", hold[i]);
        }
+}
 
-/*
- * extract_int()  -  extract an int parm w/o supplying a buffer
- */
-int extract_int(char *source, int parmnum)
-{
-       char buf[256];
-       
-       extract(buf,source,parmnum);
-       return(atoi(buf));
-       }
 
 
-void logoff(int code)
-{
-       exit(code);
-       }
 
-void main(int argc, char **argv)
+int main(int argc, char **argv)
 {
-       char buf[256];
-       char nodetitle[256];
+       char buf[512];
+       char nodetitle[SIZ];
        int a;
+       int www = 0;
        int s_pid = 0;
        int my_pid = 0;
-       char s_user[256];
-       char s_room[256];
-       char s_host[256];
-
-       attach_to_server(argc,argv);
+       char hostbuf[SIZ];
+       char portbuf[SIZ];
+       char s_user[SIZ];
+       char s_room[SIZ];
+       char s_host[SIZ];
+       char s_client[SIZ];
+
+       /* If this environment variable is set, we assume that the program
+        * is being called as a cgi-bin from a webserver and will output
+        * everything as HTML.
+        */     
+       if (getenv("REQUEST_METHOD") != NULL) www = 1;
+
+       attach_to_server(argc, argv, hostbuf, portbuf);
        serv_gets(buf);
        if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
                fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
@@ -94,36 +78,89 @@ void main(int argc, char **argv)
                        ++a;
                        }
                }
-       printf("            Users currently logged on to %s\n", nodetitle);
+       
+       if (www) {
+               printf( "Content-type: text/html\n"
+                       "\n"
+                       "<HTML><HEAD>"
+                       "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
+                       "<TITLE>");
+               printf("%s: who is online", nodetitle);
+               printf( "</TITLE></HEAD><BODY><H1>");
+       } else {
+               printf("            ");
+       }
+
+       if (www) {
+               printf("<CENTER><H1>");
+       }
+
+       printf("Users currently logged on to %s\n", nodetitle);
+
+       if (www) {
+               printf("</H1>\n");
+       }
+
        serv_puts("RWHO");
        serv_gets(buf);
        if (buf[0]!='1') {
                fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
                logoff(atoi(buf));
-               }
+       }
+
+       if (www) {
+               printf( "<TABLE BORDER=1 WIDTH=100%%>"
+                       "<TR><TH>Session</TH><TH>User name</TH>"
+                       "<TH>Room</TH><TH>From host</TH>"
+                       "<TH>Client software</TH></TR>\n");
+       } else {
+
+               printf( "Session         User name               "
+                       "Room                  From host\n");
+               printf( "------- ------------------------- "
+                       "-------------------- ------------------------\n");
+       }
+
 
-       printf("Session         User name               Room                  From host\n");
-       printf("------- ------------------------- -------------------- ------------------------\n");
        while (serv_gets(buf), strcmp(buf,"000")) {
+
+               /* Escape some stuff if we're using www mode */
+               if (www) escapize(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);
                if (s_pid != my_pid) {
-                       printf("%-7d%c%-25s %-20s %-24s\n",
-                               s_pid,
-                               ((s_pid == my_pid) ? '*' : ' '),
-                               s_user,s_room,s_host);
+
+                       if (www) printf("<TR><TD>");
+                       printf("%-7d", s_pid);
+                       printf("%c", 
+                               ((s_pid == my_pid) ? '*' : ' '));
+                       if (www) printf("</TD><TD>");
+                       printf("%-25s", s_user);
+                       if (www) printf("</TD><TD>");
+                       printf("%-20s ", s_room);
+                       if (www) printf("</TD><TD>");
+                       printf("%-24s\n", s_host);
+                       if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
                        }
                }
 
+       if (www) printf("</TABLE></CENTER>\n"
+                       "<FONT SIZE=-1>"
+                       "(This display will automatically refresh "
+                       "once per minute)</FONT>\n"
+                       "</BODY></HTML>\n");
+
        serv_puts("QUIT");
        serv_gets(buf);
-       exit(0);
+       return 0;
        }
 
 
-#ifdef NO_STRERROR
+#ifndef HAVE_STRERROR
 /*
  * replacement strerror() for systems that don't have it
  */