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;
78 CtdlInitBase64Table();
80 calc_dirs_n_files(relh, home, relhome, ctdldir);
82 /* If this environment variable is set, we assume that the program
83 * is being called as a cgi-bin from a webserver and will output
86 if (getenv("REQUEST_METHOD") != NULL) www = 1;
88 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
90 fprintf(stderr, "Server not available: %s\n", strerror(errno));
93 CtdlIPC_chat_recv(ipc, buf);
94 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
95 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
98 strcpy(nodetitle, "this Citadel site");
99 r = CtdlIPCServerInfo(ipc, buf);
101 my_pid = ipc->ServInfo.pid;
102 strcpy(nodetitle, ipc->ServInfo.humannode);
106 printf( "Content-type: text/html\n"
109 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
111 printf("%s: who is online", nodetitle);
112 printf( "</TITLE></HEAD><BODY><H1>");
118 printf("<CENTER><H1>");
121 printf("Users currently logged on to %s\n", nodetitle);
127 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
129 fprintf(stderr,"%s: %s\n",argv[0], buf);
134 printf( "<TABLE BORDER=1 WIDTH=100%%>"
135 "<TR><TH>Session</TH><TH>User name</TH>"
136 "<TH>Room</TH><TH>From host</TH>"
137 "<TH>Client software</TH></TR>\n");
140 printf( "Session User name "
142 printf( "------- ------------------------- "
143 "------------------- ------------------------\n");
147 while (!IsEmptyStr(listing)) {
148 extract_token(buf, listing, 0, '\n', sizeof buf);
149 remove_token(listing, 0, '\n');
151 /* Escape some stuff if we're using www mode */
152 if (www) escapize(buf, sizeof buf);
154 s_pid = extract_int(buf,0);
155 extract_token(s_user, buf, 1, '|', sizeof s_user);
156 extract_token(s_room, buf, 2, '|', sizeof s_room);
157 extract_token(s_host, buf, 3, '|', sizeof s_host);
158 extract_token(s_client, buf, 4, '|', sizeof s_client);
159 if (s_pid != my_pid) {
161 if (www) printf("<TR><TD>");
162 printf("%-7d", s_pid);
164 ((s_pid == my_pid) ? '*' : ' '));
165 if (www) printf("</TD><TD>");
166 printf("%-26s", s_user);
167 if (www) printf("</TD><TD>");
168 printf("%-19s ", s_room);
169 if (www) printf("</TD><TD>");
170 printf("%-24s\n", s_host);
171 if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
176 if (www) printf("</TABLE></CENTER>\n"
178 "(This display will automatically refresh "
179 "once per minute)</FONT>\n"
182 r = CtdlIPCQuit(ipc);
183 return (r / 100 == 2) ? 0 : r;
187 #ifndef HAVE_STRERROR
189 * replacement strerror() for systems that don't have it
191 char *strerror(int e)
195 snprintf(buf, sizeof buf, "errno = %d",e);