4 * Command-line "who is online?" utility
15 #include "citadel_ipc.h"
16 #include "citadel_dirs.h"
24 static void escapize(char *buf, size_t n) {
31 for (i=0; i<strlen(hold); ++i) {
32 size_t tmp = strlen(buf);
35 snprintf(&buf[tmp], n - tmp, "<");
36 else if (hold[i]=='>')
37 snprintf(&buf[tmp], n - tmp, ">");
39 snprintf(&buf[tmp], n - tmp, """);
41 snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
48 int main(int argc, char **argv)
61 int r; /* IPC response code */
67 char relhome[PATH_MAX]="";
68 char ctdldir[PATH_MAX]=CTDLDIR;
71 calc_dirs_n_files(relh, home, relhome, ctdldir);
73 /* If this environment variable is set, we assume that the program
74 * is being called as a cgi-bin from a webserver and will output
77 if (getenv("REQUEST_METHOD") != NULL) www = 1;
79 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
81 fprintf(stderr, "Server not available: %s\n", strerror(errno));
84 CtdlIPC_chat_recv(ipc, buf);
85 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
86 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
89 strcpy(nodetitle, "this Citadel site");
90 r = CtdlIPCServerInfo(ipc, buf);
92 my_pid = ipc->ServInfo.pid;
93 strcpy(nodetitle, ipc->ServInfo.humannode);
97 printf( "Content-type: text/html\n"
100 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
102 printf("%s: who is online", nodetitle);
103 printf( "</TITLE></HEAD><BODY><H1>");
109 printf("<CENTER><H1>");
112 printf("Users currently logged on to %s\n", nodetitle);
118 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
120 fprintf(stderr,"%s: %s\n",argv[0], buf);
125 printf( "<TABLE BORDER=1 WIDTH=100%%>"
126 "<TR><TH>Session</TH><TH>User name</TH>"
127 "<TH>Room</TH><TH>From host</TH>"
128 "<TH>Client software</TH></TR>\n");
131 printf( "Session User name "
133 printf( "------- ------------------------- "
134 "------------------- ------------------------\n");
138 while (strlen(listing) > 0) {
139 extract_token(buf, listing, 0, '\n', sizeof buf);
140 remove_token(listing, 0, '\n');
142 /* Escape some stuff if we're using www mode */
143 if (www) escapize(buf, sizeof buf);
145 s_pid = extract_int(buf,0);
146 extract_token(s_user, buf, 1, '|', sizeof s_user);
147 extract_token(s_room, buf, 2, '|', sizeof s_room);
148 extract_token(s_host, buf, 3, '|', sizeof s_host);
149 extract_token(s_client, buf, 4, '|', sizeof s_client);
150 if (s_pid != my_pid) {
152 if (www) printf("<TR><TD>");
153 printf("%-7d", s_pid);
155 ((s_pid == my_pid) ? '*' : ' '));
156 if (www) printf("</TD><TD>");
157 printf("%-26s", s_user);
158 if (www) printf("</TD><TD>");
159 printf("%-19s ", s_room);
160 if (www) printf("</TD><TD>");
161 printf("%-24s\n", s_host);
162 if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
167 if (www) printf("</TABLE></CENTER>\n"
169 "(This display will automatically refresh "
170 "once per minute)</FONT>\n"
173 r = CtdlIPCQuit(ipc);
174 return (r / 100 == 2) ? 0 : r;
178 #ifndef HAVE_STRERROR
180 * replacement strerror() for systems that don't have it
182 char *strerror(int e)
186 snprintf(buf, sizeof buf, "errno = %d",e);