2 * Command-line "who is online?" utility
12 #include <libcitadel.h>
14 #include "citadel_ipc.h"
15 #include "citadel_dirs.h"
22 static void escapize(char *buf, size_t n) {
31 for (i=0; i<len; ++i) {
33 snprintf(&buf[tmp], n - tmp, "<");
36 else if (hold[i]=='>'){
37 snprintf(&buf[tmp], n - tmp, ">");
40 else if (hold[i]==34){
41 snprintf(&buf[tmp], n - tmp, """);
45 snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
54 int main(int argc, char **argv)
67 int r; /* IPC response code */
73 char relhome[PATH_MAX]="";
74 char ctdldir[PATH_MAX]=CTDLDIR;
76 calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
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
82 if (getenv("REQUEST_METHOD") != NULL) www = 1;
84 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
86 fprintf(stderr, "Server not available: %s\n", strerror(errno));
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]);
94 strcpy(nodetitle, "this Citadel site");
95 r = CtdlIPCServerInfo(ipc, buf);
97 my_pid = ipc->ServInfo.pid;
98 strcpy(nodetitle, ipc->ServInfo.humannode);
102 printf( "Content-type: text/html\n"
105 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
107 printf("%s: who is online", nodetitle);
108 printf( "</TITLE></HEAD><BODY><H1>");
114 printf("<CENTER><H1>");
117 printf("Users currently logged on to %s\n", nodetitle);
123 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
125 fprintf(stderr,"%s: %s\n",argv[0], buf);
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");
136 printf( "Session User name "
138 printf( "------- ------------------------- "
139 "------------------- ------------------------\n");
143 while (!IsEmptyStr(listing)) {
144 extract_token(buf, listing, 0, '\n', sizeof buf);
145 remove_token(listing, 0, '\n');
147 /* Escape some stuff if we're using www mode */
148 if (www) escapize(buf, sizeof buf);
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) {
157 if (www) printf("<TR><TD>");
158 printf("%-7d", s_pid);
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);
172 if (www) printf("</TABLE></CENTER>\n"
174 "(This display will automatically refresh "
175 "once per minute)</FONT>\n"
178 r = CtdlIPCQuit(ipc);
179 return (r / 100 == 2) ? 0 : r;
186 void stty_ctdl(int cmd) {
190 #ifndef HAVE_STRERROR
192 * replacement strerror() for systems that don't have it
194 char *strerror(int e)
198 snprintf(buf, sizeof buf, "errno = %d",e);