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