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