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