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