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