4 * Command-line "who is online?" utility
14 #include <libcitadel.h>
16 #include "citadel_ipc.h"
17 #include "citadel_dirs.h"
24 static void escapize(char *buf, size_t n) {
33 for (i=0; i<len; ++i) {
35 snprintf(&buf[tmp], n - tmp, "<");
38 else if (hold[i]=='>'){
39 snprintf(&buf[tmp], n - tmp, ">");
42 else if (hold[i]==34){
43 snprintf(&buf[tmp], n - tmp, """);
47 snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
56 int main(int argc, char **argv)
69 int r; /* IPC response code */
75 char relhome[PATH_MAX]="";
76 char ctdldir[PATH_MAX]=CTDLDIR;
78 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
80 /* If this environment variable is set, we assume that the program
81 * is being called as a cgi-bin from a webserver and will output
84 if (getenv("REQUEST_METHOD") != NULL) www = 1;
86 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
88 fprintf(stderr, "Server not available: %s\n", strerror(errno));
91 CtdlIPC_chat_recv(ipc, buf);
92 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
93 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
96 strcpy(nodetitle, "this Citadel site");
97 r = CtdlIPCServerInfo(ipc, buf);
99 my_pid = ipc->ServInfo.pid;
100 strcpy(nodetitle, ipc->ServInfo.humannode);
104 printf( "Content-type: text/html\n"
107 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
109 printf("%s: who is online", nodetitle);
110 printf( "</TITLE></HEAD><BODY><H1>");
116 printf("<CENTER><H1>");
119 printf("Users currently logged on to %s\n", nodetitle);
125 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
127 fprintf(stderr,"%s: %s\n",argv[0], buf);
132 printf( "<TABLE BORDER=1 WIDTH=100%%>"
133 "<TR><TH>Session</TH><TH>User name</TH>"
134 "<TH>Room</TH><TH>From host</TH>"
135 "<TH>Client software</TH></TR>\n");
138 printf( "Session User name "
140 printf( "------- ------------------------- "
141 "------------------- ------------------------\n");
145 while (!IsEmptyStr(listing)) {
146 extract_token(buf, listing, 0, '\n', sizeof buf);
147 remove_token(listing, 0, '\n');
149 /* Escape some stuff if we're using www mode */
150 if (www) escapize(buf, sizeof buf);
152 s_pid = extract_int(buf,0);
153 extract_token(s_user, buf, 1, '|', sizeof s_user);
154 extract_token(s_room, buf, 2, '|', sizeof s_room);
155 extract_token(s_host, buf, 3, '|', sizeof s_host);
156 extract_token(s_client, buf, 4, '|', sizeof s_client);
157 if (s_pid != my_pid) {
159 if (www) printf("<TR><TD>");
160 printf("%-7d", s_pid);
162 ((s_pid == my_pid) ? '*' : ' '));
163 if (www) printf("</TD><TD>");
164 printf("%-26s", s_user);
165 if (www) printf("</TD><TD>");
166 printf("%-19s ", s_room);
167 if (www) printf("</TD><TD>");
168 printf("%-24s\n", s_host);
169 if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
174 if (www) printf("</TABLE></CENTER>\n"
176 "(This display will automatically refresh "
177 "once per minute)</FONT>\n"
180 r = CtdlIPCQuit(ipc);
181 return (r / 100 == 2) ? 0 : r;
185 #ifndef HAVE_STRERROR
187 * replacement strerror() for systems that don't have it
189 char *strerror(int e)
193 snprintf(buf, sizeof buf, "errno = %d",e);