4 * Command-line "who is online?" utility
15 #include "citadel_ipc.h"
23 static void escapize(char *buf, size_t n) {
30 for (i=0; i<strlen(hold); ++i) {
31 size_t tmp = strlen(buf);
34 snprintf(&buf[tmp], n - tmp, "<");
35 else if (hold[i]=='>')
36 snprintf(&buf[tmp], n - tmp, ">");
38 snprintf(&buf[tmp], n - tmp, """);
40 snprintf(&buf[tmp], n - tmp, "%c", hold[i]);
47 int main(int argc, char **argv)
60 int r; /* IPC response code */
63 struct CtdlServInfo serv_info;
66 /* If this environment variable is set, we assume that the program
67 * is being called as a cgi-bin from a webserver and will output
70 if (getenv("REQUEST_METHOD") != NULL) www = 1;
72 ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf);
74 fprintf(stderr, "Server not available: %s\n", strerror(errno));
77 CtdlIPC_getline(ipc, buf);
78 if ((buf[0]!='2')&&(strncmp(buf,"551",3))) {
79 fprintf(stderr,"%s: %s\n",argv[0],&buf[4]);
82 strcpy(nodetitle, "this BBS");
83 r = CtdlIPCServerInfo(ipc, &serv_info, buf);
85 my_pid = serv_info.serv_pid;
86 strcpy(nodetitle, serv_info.serv_humannode);
90 printf( "Content-type: text/html\n"
93 "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"60\">\n"
95 printf("%s: who is online", nodetitle);
96 printf( "</TITLE></HEAD><BODY><H1>");
102 printf("<CENTER><H1>");
105 printf("Users currently logged on to %s\n", nodetitle);
111 r = CtdlIPCOnlineUsers(ipc, &listing, &timenow, buf);
113 fprintf(stderr,"%s: %s\n",argv[0], buf);
118 printf( "<TABLE BORDER=1 WIDTH=100%%>"
119 "<TR><TH>Session</TH><TH>User name</TH>"
120 "<TH>Room</TH><TH>From host</TH>"
121 "<TH>Client software</TH></TR>\n");
124 printf( "Session User name "
126 printf( "------- ------------------------- "
127 "------------------- ------------------------\n");
131 while (strlen(listing) > 0) {
132 extract_token(buf, listing, 0, '\n');
133 remove_token(listing, 0, '\n');
135 /* Escape some stuff if we're using www mode */
136 if (www) escapize(buf, sizeof buf);
138 s_pid = extract_int(buf,0);
139 extract(s_user,buf,1);
140 extract(s_room,buf,2);
141 extract(s_host,buf,3);
142 extract(s_client,buf,4);
143 if (s_pid != my_pid) {
145 if (www) printf("<TR><TD>");
146 printf("%-7d", s_pid);
148 ((s_pid == my_pid) ? '*' : ' '));
149 if (www) printf("</TD><TD>");
150 printf("%-26s", s_user);
151 if (www) printf("</TD><TD>");
152 printf("%-19s ", s_room);
153 if (www) printf("</TD><TD>");
154 printf("%-24s\n", s_host);
155 if (www) printf("</TD><TD>%s</TD></TR>\n", s_client);
160 if (www) printf("</TABLE></CENTER>\n"
162 "(This display will automatically refresh "
163 "once per minute)</FONT>\n"
166 r = CtdlIPCQuit(ipc);
167 return (r / 100 == 2) ? 0 : r;
171 #ifndef HAVE_STRERROR
173 * replacement strerror() for systems that don't have it
175 char *strerror(int e)
179 snprintf(buf, sizeof buf, "errno = %d",e);