* fixed some memory leaks
[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 "citadel.h"
14 #include "citadel_ipc.h"
15 #include "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         struct CtdlServInfo *serv_info = 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         attach_to_server(argc, argv, hostbuf, portbuf);
72         serv_gets(buf);
73         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
74                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
75                 logoff(atoi(buf));
76                 }
77         strcpy(nodetitle, "this BBS");
78         r = CtdlIPCServerInfo(serv_info, buf);
79         if (r / 100 == 1) {
80                 my_pid = serv_info->serv_pid;
81                 strcpy(nodetitle, serv_info->serv_humannode);
82         }
83         
84         if (www) {
85                 printf( "Content-type: text/html\n"
86                         "\n"
87                         "<HTML><HEAD>"
88                         "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
89                         "<TITLE>");
90                 printf("%s: who is online", nodetitle);
91                 printf( "</TITLE></HEAD><BODY><H1>");
92         } else {
93                 printf("            ");
94         }
95
96         if (www) {
97                 printf("<CENTER><H1>");
98         }
99
100         printf("Users currently logged on to %s\n", nodetitle);
101
102         if (www) {
103                 printf("</H1>\n");
104         }
105
106         r = CtdlIPCOnlineUsers(&listing, &timenow, buf);
107         if (r / 100 != 1) {
108                 fprintf(stderr,"%s: %s\n",argv[0], buf);
109                 logoff(atoi(buf));
110         }
111
112         if (www) {
113                 printf( "<TABLE BORDER=1 WIDTH=100%%>"
114                         "<TR><TH>Session</TH><TH>User name</TH>"
115                         "<TH>Room</TH><TH>From host</TH>"
116                         "<TH>Client software</TH></TR>\n");
117         } else {
118
119                 printf( "Session         User name               "
120                         "Room                  From host\n");
121                 printf( "------- ------------------------- "
122                         "------------------- ------------------------\n");
123         }
124
125
126         while (strlen(listing) > 0) {
127                 extract_token(buf, listing, 0, '\n');
128                 remove_token(listing, 0, '\n');
129
130                 /* Escape some stuff if we're using www mode */
131                 if (www) escapize(buf, sizeof buf);
132
133                 s_pid = extract_int(buf,0);
134                 extract(s_user,buf,1);
135                 extract(s_room,buf,2);
136                 extract(s_host,buf,3);
137                 extract(s_client,buf,4);
138                 if (s_pid != my_pid) {
139
140                         if (www) printf("<TR><TD>");
141                         printf("%-7d", s_pid);
142                         printf("%c", 
143                                 ((s_pid == my_pid) ? '*' : ' '));
144                         if (www) printf("</TD><TD>");
145                         printf("%-25s", s_user);
146                         if (www) printf("</TD><TD>");
147                         printf("%-20s ", s_room);
148                         if (www) printf("</TD><TD>");
149                         printf("%-24s\n", s_host);
150                         if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
151                         }
152                 }
153         free(listing);
154
155         if (www) printf("</TABLE></CENTER>\n"
156                         "<FONT SIZE=-1>"
157                         "(This display will automatically refresh "
158                         "once per minute)</FONT>\n"
159                         "</BODY></HTML>\n");
160
161         r = CtdlIPCQuit();
162         return (r / 100 == 2) ? 0 : r;
163 }
164
165
166 #ifndef HAVE_STRERROR
167 /*
168  * replacement strerror() for systems that don't have it
169  */
170 char *strerror(int e)
171 {
172         static char buf[32];
173
174         snprintf(buf, sizeof buf, "errno = %d",e);
175         return(buf);
176         }
177 #endif