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) {
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;
79 calc_dirs_n_files(relh, home, relhome, ctdldir);
81 /* If this environment variable is set, we assume that the program
82 * is being called as a cgi-bin from a webserver and will output
85 if (getenv("REQUEST_METHOD") != NULL) www = 1;
87 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
89 fprintf(stderr, "Server not available: %s\n", strerror(errno));
92 CtdlIPC_chat_recv(ipc, buf);
93 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
94 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
97 strcpy(nodetitle, "this Citadel site");
98 r = CtdlIPCServerInfo(ipc, buf);
100 my_pid = ipc->ServInfo.pid;
101 strcpy(nodetitle, ipc->ServInfo.humannode);
105 printf( "Content-type: text/html\n"
108 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
110 printf("%s: who is online", nodetitle);
111 printf( "</TITLE></HEAD><BODY><H1>");
117 printf("<CENTER><H1>");
120 printf("Users currently logged on to %s\n", nodetitle);
126 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
128 fprintf(stderr,"%s: %s\n",argv[0], buf);
133 printf( "<TABLE BORDER=1 WIDTH=100%%>"
134 "<TR><TH>Session</TH><TH>User name</TH>"
135 "<TH>Room</TH><TH>From host</TH>"
136 "<TH>Client software</TH></TR>\n");
139 printf( "Session User name "
141 printf( "------- ------------------------- "
142 "------------------- ------------------------\n");
146 while (!IsEmptyStr(listing)) {
147 extract_token(buf, listing, 0, '\n', sizeof buf);
148 remove_token(listing, 0, '\n');
150 /* Escape some stuff if we're using www mode */
151 if (www) escapize(buf, sizeof buf);
153 s_pid = extract_int(buf,0);
154 extract_token(s_user, buf, 1, '|', sizeof s_user);
155 extract_token(s_room, buf, 2, '|', sizeof s_room);
156 extract_token(s_host, buf, 3, '|', sizeof s_host);
157 extract_token(s_client, buf, 4, '|', sizeof s_client);
158 if (s_pid != my_pid) {
160 if (www) printf("<TR><TD>");
161 printf("%-7d", s_pid);
163 ((s_pid == my_pid) ? '*' : ' '));
164 if (www) printf("</TD><TD>");
165 printf("%-26s", s_user);
166 if (www) printf("</TD><TD>");
167 printf("%-19s ", s_room);
168 if (www) printf("</TD><TD>");
169 printf("%-24s\n", s_host);
170 if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
175 if (www) printf("</TABLE></CENTER>\n"
177 "(This display will automatically refresh "
178 "once per minute)</FONT>\n"
181 r = CtdlIPCQuit(ipc);
182 return (r / 100 == 2) ? 0 : r;
186 #ifndef HAVE_STRERROR
188 * replacement strerror() for systems that don't have it
190 char *strerror(int e)
194 snprintf(buf, sizeof buf, "errno = %d",e);