more sprintf bashing. now the only ones left are in mime_parser
[citadel.git] / citadel / whobbs.c
1 /*
2  * $Id$
3  * 
4  * Command-line "who is online?" utility
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include "citadel.h"
13 #include "ipc.h"
14 #include "tools.h"
15
16 void logoff(int code)
17 {
18         exit(code);
19         }
20
21 static void escapize(char *buf, size_t n) {
22         char hold[512];
23         int i;
24
25         strcpy(hold, buf);
26         strcpy(buf, "");
27
28         for (i=0; i<strlen(hold); ++i) {
29                 size_t tmp = strlen(buf);
30
31                 if (hold[i]=='<')
32                         snprintf(&buf[tmp], n - tmp, "&lt;");
33                 else if (hold[i]=='>')
34                         snprintf(&buf[tmp], n - tmp, "&gt;");
35                 else if (hold[i]==34)
36                         snprintf(&buf[tmp], n - tmp, "&quot;");
37                 else
38                         snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
39         }
40 }
41
42
43
44
45 int main(int argc, char **argv)
46 {
47         char buf[512];
48         char nodetitle[SIZ];
49         int a;
50         int www = 0;
51         int s_pid = 0;
52         int my_pid = 0;
53         char hostbuf[SIZ];
54         char portbuf[SIZ];
55         char s_user[SIZ];
56         char s_room[SIZ];
57         char s_host[SIZ];
58         char s_client[SIZ];
59
60         /* If this environment variable is set, we assume that the program
61          * is being called as a cgi-bin from a webserver and will output
62          * everything as HTML.
63          */     
64         if (getenv("REQUEST_METHOD") != NULL) www = 1;
65
66         attach_to_server(argc, argv, hostbuf, portbuf);
67         serv_gets(buf);
68         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
69                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
70                 logoff(atoi(buf));
71                 }
72         strcpy(nodetitle, "this BBS");
73         serv_puts("INFO");
74         serv_gets(buf);
75         if (buf[0]=='1') {
76                 a = 0;
77                 while (serv_gets(buf), strcmp(buf,"000")) {
78                         if (a==0) my_pid = atoi(buf);
79                         if (a==2) strcpy(nodetitle, buf);
80                         ++a;
81                         }
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         serv_puts("RWHO");
107         serv_gets(buf);
108         if (buf[0]!='1') {
109                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
110                 logoff(atoi(buf));
111         }
112
113         if (www) {
114                 printf( "<TABLE BORDER=1 WIDTH=100%%>"
115                         "<TR><TH>Session</TH><TH>User name</TH>"
116                         "<TH>Room</TH><TH>From host</TH>"
117                         "<TH>Client software</TH></TR>\n");
118         } else {
119
120                 printf( "Session         User name               "
121                         "Room                  From host\n");
122                 printf( "------- ------------------------- "
123                         "-------------------- ------------------------\n");
124         }
125
126
127         while (serv_gets(buf), strcmp(buf,"000")) {
128
129                 /* Escape some stuff if we're using www mode */
130                 if (www) escapize(buf, sizeof buf);
131
132                 s_pid = extract_int(buf,0);
133                 extract(s_user,buf,1);
134                 extract(s_room,buf,2);
135                 extract(s_host,buf,3);
136                 extract(s_client,buf,4);
137                 if (s_pid != my_pid) {
138
139                         if (www) printf("<TR><TD>");
140                         printf("%-7d", s_pid);
141                         printf("%c", 
142                                 ((s_pid == my_pid) ? '*' : ' '));
143                         if (www) printf("</TD><TD>");
144                         printf("%-25s", s_user);
145                         if (www) printf("</TD><TD>");
146                         printf("%-20s ", s_room);
147                         if (www) printf("</TD><TD>");
148                         printf("%-24s\n", s_host);
149                         if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
150                         }
151                 }
152
153         if (www) printf("</TABLE></CENTER>\n"
154                         "<FONT SIZE=-1>"
155                         "(This display will automatically refresh "
156                         "once per minute)</FONT>\n"
157                         "</BODY></HTML>\n");
158
159         serv_puts("QUIT");
160         serv_gets(buf);
161         return 0;
162         }
163
164
165 #ifndef HAVE_STRERROR
166 /*
167  * replacement strerror() for systems that don't have it
168  */
169 char *strerror(int e)
170 {
171         static char buf[32];
172
173         snprintf(buf, sizeof buf, "errno = %d",e);
174         return(buf);
175         }
176 #endif