* Changed the comments at the beginning of each file to a consistent format
[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 void escapize(char buf[]) {
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                 if (hold[i]=='<')
30                         sprintf(&buf[strlen(buf)], "&lt;");
31                 else if (hold[i]=='>')
32                         sprintf(&buf[strlen(buf)], "&gt;");
33                 else if (hold[i]==34)
34                         sprintf(&buf[strlen(buf)], "&quot;");
35                 else
36                         sprintf(&buf[strlen(buf)], "%c", hold[i]);
37         }
38 }
39
40
41
42
43 int main(int argc, char **argv)
44 {
45         char buf[512];
46         char nodetitle[256];
47         int a;
48         int www = 0;
49         int s_pid = 0;
50         int my_pid = 0;
51         char hostbuf[256];
52         char portbuf[256];
53         char s_user[256];
54         char s_room[256];
55         char s_host[256];
56         char s_client[256];
57
58         /* If this environment variable is set, we assume that the program
59          * is being called as a cgi-bin from a webserver and will output
60          * everything as HTML.
61          */     
62         if (getenv("REQUEST_METHOD") != NULL) www = 1;
63
64         attach_to_server(argc, argv, hostbuf, portbuf);
65         serv_gets(buf);
66         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
67                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
68                 logoff(atoi(buf));
69                 }
70         strcpy(nodetitle, "this BBS");
71         serv_puts("INFO");
72         serv_gets(buf);
73         if (buf[0]=='1') {
74                 a = 0;
75                 while (serv_gets(buf), strcmp(buf,"000")) {
76                         if (a==0) my_pid = atoi(buf);
77                         if (a==2) strcpy(nodetitle, buf);
78                         ++a;
79                         }
80                 }
81         
82         if (www) {
83                 printf( "Content-type: text/html\n"
84                         "\n"
85                         "<HTML><HEAD>"
86                         "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
87                         "<TITLE>");
88                 printf("%s: who is online", nodetitle);
89                 printf( "</TITLE></HEAD><BODY><H1>");
90         } else {
91                 printf("            ");
92         }
93
94         if (www) {
95                 printf("<CENTER><H1>");
96         }
97
98         printf("Users currently logged on to %s\n", nodetitle);
99
100         if (www) {
101                 printf("</H1>\n");
102         }
103
104         serv_puts("RWHO");
105         serv_gets(buf);
106         if (buf[0]!='1') {
107                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
108                 logoff(atoi(buf));
109         }
110
111         if (www) {
112                 printf( "<TABLE BORDER=1 WIDTH=100%%>"
113                         "<TR><TH>Session</TH><TH>User name</TH>"
114                         "<TH>Room</TH><TH>From host</TH>"
115                         "<TH>Client software</TH></TR>\n");
116         } else {
117
118                 printf( "Session         User name               "
119                         "Room                  From host\n");
120                 printf( "------- ------------------------- "
121                         "-------------------- ------------------------\n");
122         }
123
124
125         while (serv_gets(buf), strcmp(buf,"000")) {
126
127                 /* Escape some stuff if we're using www mode */
128                 if (www) escapize(buf);
129
130                 s_pid = extract_int(buf,0);
131                 extract(s_user,buf,1);
132                 extract(s_room,buf,2);
133                 extract(s_host,buf,3);
134                 extract(s_client,buf,4);
135                 if (s_pid != my_pid) {
136
137                         if (www) printf("<TR><TD>");
138                         printf("%-7d", s_pid);
139                         printf("%c", 
140                                 ((s_pid == my_pid) ? '*' : ' '));
141                         if (www) printf("</TD><TD>");
142                         printf("%-25s", s_user);
143                         if (www) printf("</TD><TD>");
144                         printf("%-20s ", s_room);
145                         if (www) printf("</TD><TD>");
146                         printf("%-24s\n", s_host);
147                         if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
148                         }
149                 }
150
151         if (www) printf("</TABLE></CENTER>\n"
152                         "<FONT SIZE=-1>"
153                         "(This display will automatically refresh "
154                         "once per minute)</FONT>\n"
155                         "</BODY></HTML>\n");
156
157         serv_puts("QUIT");
158         serv_gets(buf);
159         return 0;
160         }
161
162
163 #ifndef HAVE_STRERROR
164 /*
165  * replacement strerror() for systems that don't have it
166  */
167 char *strerror(int e)
168 {
169         static char buf[32];
170
171         sprintf(buf,"errno = %d",e);
172         return(buf);
173         }
174 #endif