be5dbe05eaca1714563a87f17b8f47bf5ef65afb
[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 "citadel_dirs.h"
17 #include "tools.h"
18
19 void logoff(int code)
20 {
21         exit(code);
22         }
23
24 static void escapize(char *buf, size_t n) {
25         char hold[512];
26         int i, len;
27         size_t tmp;
28
29         strcpy(hold, buf);
30         strcpy(buf, "");
31         tmp = 0;
32         len = strlen(hold);
33         for (i=0; i<len; ++i) {
34                 if (hold[i]=='<') {
35                         snprintf(&buf[tmp], n - tmp, "&lt;");
36                         tmp += 4;
37                 }
38                 else if (hold[i]=='>'){
39                         snprintf(&buf[tmp], n - tmp, "&gt;");
40                         tmp += 4;
41                 }
42                 else if (hold[i]==34){
43                         snprintf(&buf[tmp], n - tmp, "&quot;");
44                         tmp += 6;
45                 }
46                 else{
47                         snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
48                         tmp ++;
49                 }
50         }
51 }
52
53
54
55
56 int main(int argc, char **argv)
57 {
58         char buf[512];
59         char nodetitle[SIZ];
60         int www = 0;
61         int s_pid = 0;
62         int my_pid = 0;
63         char hostbuf[SIZ];
64         char portbuf[SIZ];
65         char s_user[SIZ];
66         char s_room[SIZ];
67         char s_host[SIZ];
68         char s_client[SIZ];
69         int r;                  /* IPC response code */
70         time_t timenow;
71         char *listing = NULL;
72         CtdlIPC *ipc = NULL;
73         int relh=0;
74         int home=0;
75         char relhome[PATH_MAX]="";
76         char ctdldir[PATH_MAX]=CTDLDIR;
77
78         CtdlInitBase64Table();
79
80         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
81
82         /* If this environment variable is set, we assume that the program
83          * is being called as a cgi-bin from a webserver and will output
84          * everything as HTML.
85          */     
86         if (getenv("REQUEST_METHOD") != NULL) www = 1;
87
88         ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
89         if (!ipc) {
90                 fprintf(stderr, "Server not available: %s\n", strerror(errno));
91                 logoff(errno);
92         }
93         CtdlIPC_chat_recv(ipc, buf);
94         if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
95                 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
96                 logoff(atoi(buf));
97                 }
98         strcpy(nodetitle, "this Citadel site");
99         r = CtdlIPCServerInfo(ipc, buf);
100         if (r / 100 == 1) {
101                 my_pid = ipc->ServInfo.pid;
102                 strcpy(nodetitle, ipc->ServInfo.humannode);
103         }
104         
105         if (www) {
106                 printf( "Content-type: text/html\n"
107                         "\n"
108                         "<HTML><HEAD>"
109                         "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
110                         "<TITLE>");
111                 printf("%s: who is online", nodetitle);
112                 printf( "</TITLE></HEAD><BODY><H1>");
113         } else {
114                 printf("            ");
115         }
116
117         if (www) {
118                 printf("<CENTER><H1>");
119         }
120
121         printf("Users currently logged on to %s\n", nodetitle);
122
123         if (www) {
124                 printf("</H1>\n");
125         }
126
127         r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
128         if (r / 100 != 1) {
129                 fprintf(stderr,"%s: %s\n",argv[0], buf);
130                 logoff(atoi(buf));
131         }
132
133         if (www) {
134                 printf( "<TABLE BORDER=1 WIDTH=100%%>"
135                         "<TR><TH>Session</TH><TH>User name</TH>"
136                         "<TH>Room</TH><TH>From host</TH>"
137                         "<TH>Client software</TH></TR>\n");
138         } else {
139
140                 printf( "Session         User name               "
141                         "Room                  From host\n");
142                 printf( "------- ------------------------- "
143                         "------------------- ------------------------\n");
144         }
145
146
147         while (!IsEmptyStr(listing)) {
148                 extract_token(buf, listing, 0, '\n', sizeof buf);
149                 remove_token(listing, 0, '\n');
150
151                 /* Escape some stuff if we're using www mode */
152                 if (www) escapize(buf, sizeof buf);
153
154                 s_pid = extract_int(buf,0);
155                 extract_token(s_user, buf, 1, '|', sizeof s_user);
156                 extract_token(s_room, buf, 2, '|', sizeof s_room);
157                 extract_token(s_host, buf, 3, '|', sizeof s_host);
158                 extract_token(s_client, buf, 4, '|', sizeof s_client);
159                 if (s_pid != my_pid) {
160
161                         if (www) printf("<TR><TD>");
162                         printf("%-7d", s_pid);
163                         printf("%c", 
164                                 ((s_pid == my_pid) ? '*' : ' '));
165                         if (www) printf("</TD><TD>");
166                         printf("%-26s", s_user);
167                         if (www) printf("</TD><TD>");
168                         printf("%-19s ", s_room);
169                         if (www) printf("</TD><TD>");
170                         printf("%-24s\n", s_host);
171                         if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
172                         }
173                 }
174         free(listing);
175
176         if (www) printf("</TABLE></CENTER>\n"
177                         "<FONT SIZE=-1>"
178                         "(This display will automatically refresh "
179                         "once per minute)</FONT>\n"
180                         "</BODY></HTML>\n");
181
182         r = CtdlIPCQuit(ipc);
183         return (r / 100 == 2) ? 0 : r;
184 }
185
186
187 #ifndef HAVE_STRERROR
188 /*
189  * replacement strerror() for systems that don't have it
190  */
191 char *strerror(int e)
192 {
193         static char buf[32];
194
195         snprintf(buf, sizeof buf, "errno = %d",e);
196         return(buf);
197         }
198 #endif