* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / whobbs.c
1 /*
2  * $Id$
3  * 
4  * Command-line "who is online?" utility
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <errno.h>
14 #include "citadel.h"
15 #include "citadel_ipc.h"
16 #include "tools.h"
17
18 void logoff(int code)
19 {
20         exit(code);
21         }
22
23 static void escapize(char *buf, size_t n) {
24         char hold[512];
25         int i;
26
27         strcpy(hold, buf);
28         strcpy(buf, "");
29
30         for (i=0; i<strlen(hold); ++i) {
31                 size_t tmp = strlen(buf);
32
33                 if (hold[i]=='<')
34                         snprintf(&buf[tmp], n - tmp, "&lt;");
35                 else if (hold[i]=='>')
36                         snprintf(&buf[tmp], n - tmp, "&gt;");
37                 else if (hold[i]==34)
38                         snprintf(&buf[tmp], n - tmp, "&quot;");
39                 else
40                         snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
41         }
42 }
43
44
45
46
47 int main(int argc, char **argv)
48 {
49         char buf[512];
50         char nodetitle[SIZ];
51         int www = 0;
52         int s_pid = 0;
53         int my_pid = 0;
54         char hostbuf[SIZ];
55         char portbuf[SIZ];
56         char s_user[SIZ];
57         char s_room[SIZ];
58         char s_host[SIZ];
59         char s_client[SIZ];
60         int r;                  /* IPC response code */
61         time_t timenow;
62         char *listing = NULL;
63         CtdlIPC *ipc = NULL;
64
65         /* If this environment variable is set, we assume that the program
66          * is being called as a cgi-bin from a webserver and will output
67          * everything as HTML.
68          */     
69         if (getenv("REQUEST_METHOD") != NULL) www = 1;
70
71         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
72         if (!ipc) {
73                 fprintf(stderr, "Server not available: %s\n", strerror(errno));
74                 logoff(errno);
75         }
76         CtdlIPC_chat_recv(ipc, buf);
77         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
78                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
79                 logoff(atoi(buf));
80                 }
81         strcpy(nodetitle, "this Citadel site");
82         r = CtdlIPCServerInfo(ipc, buf);
83         if (r / 100 == 1) {
84                 my_pid = ipc->ServInfo.pid;
85                 strcpy(nodetitle, ipc->ServInfo.humannode);
86         }
87         
88         if (www) {
89                 printf( "Content-type: text/html\n"
90                         "\n"
91                         "<HTML><HEAD>"
92                         "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
93                         "<TITLE>");
94                 printf("%s: who is online", nodetitle);
95                 printf( "</TITLE></HEAD><BODY><H1>");
96         } else {
97                 printf("            ");
98         }
99
100         if (www) {
101                 printf("<CENTER><H1>");
102         }
103
104         printf("Users currently logged on to %s\n", nodetitle);
105
106         if (www) {
107                 printf("</H1>\n");
108         }
109
110         r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
111         if (r / 100 != 1) {
112                 fprintf(stderr,"%s: %s\n",argv[0], buf);
113                 logoff(atoi(buf));
114         }
115
116         if (www) {
117                 printf( "<TABLE BORDER=1 WIDTH=100%%>"
118                         "<TR><TH>Session</TH><TH>User name</TH>"
119                         "<TH>Room</TH><TH>From host</TH>"
120                         "<TH>Client software</TH></TR>\n");
121         } else {
122
123                 printf( "Session         User name               "
124                         "Room                  From host\n");
125                 printf( "------- ------------------------- "
126                         "------------------- ------------------------\n");
127         }
128
129
130         while (strlen(listing) > 0) {
131                 extract_token(buf, listing, 0, '\n', sizeof buf);
132                 remove_token(listing, 0, '\n');
133
134                 /* Escape some stuff if we're using www mode */
135                 if (www) escapize(buf, sizeof buf);
136
137                 s_pid = extract_int(buf,0);
138                 extract_token(s_user, buf, 1, '|', sizeof s_user);
139                 extract_token(s_room, buf, 2, '|', sizeof s_room);
140                 extract_token(s_host, buf, 3, '|', sizeof s_host);
141                 extract_token(s_client, buf, 4, '|', sizeof s_client);
142                 if (s_pid != my_pid) {
143
144                         if (www) printf("<TR><TD>");
145                         printf("%-7d", s_pid);
146                         printf("%c", 
147                                 ((s_pid == my_pid) ? '*' : ' '));
148                         if (www) printf("</TD><TD>");
149                         printf("%-26s", s_user);
150                         if (www) printf("</TD><TD>");
151                         printf("%-19s ", s_room);
152                         if (www) printf("</TD><TD>");
153                         printf("%-24s\n", s_host);
154                         if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
155                         }
156                 }
157         free(listing);
158
159         if (www) printf("</TABLE></CENTER>\n"
160                         "<FONT SIZE=-1>"
161                         "(This display will automatically refresh "
162                         "once per minute)</FONT>\n"
163                         "</BODY></HTML>\n");
164
165         r = CtdlIPCQuit(ipc);
166         return (r / 100 == 2) ? 0 : r;
167 }
168
169
170 #ifndef HAVE_STRERROR
171 /*
172  * replacement strerror() for systems that don't have it
173  */
174 char *strerror(int e)
175 {
176         static char buf[32];
177
178         snprintf(buf, sizeof buf, "errno = %d",e);
179         return(buf);
180         }
181 #endif