*** empty log message ***
[citadel.git] / webcit / who.c
1 /*
2  * $Id$
3  *
4  * Display a list of all users currently logged on to the Citadel server.
5  */
6
7 #include <ctype.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <sys/socket.h>
16 #include <sys/time.h>
17 #include <limits.h>
18 #include <netinet/in.h>
19 #include <netdb.h>
20 #include <string.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include "webcit.h"
27
28
29
30 /*
31  * Display inner div of Wholist
32  */
33 void who_inner_div(void) {
34         char buf[SIZ], user[SIZ], room[SIZ], host[SIZ],
35                 realroom[SIZ], realhost[SIZ];
36         int sess;
37         time_t last_activity;
38         time_t now;
39         int bg = 0;
40
41         wprintf("<table border=\"0\" cellspacing=\"0\" width=\"100%%\" bgcolor=\"#FFFFFF\">"
42                 "<tr>\n");
43         wprintf("<th colspan=\"3\"> </th>\n");
44         wprintf("<th>User Name</th>\n");
45         wprintf("<th>Room</th>");
46         wprintf("<th>From host</th>\n</tr>\n");
47
48         serv_puts("TIME");
49         serv_getln(buf, sizeof buf);
50         if (buf[0] == '2') {
51                 now = extract_long(&buf[4], 0);
52         }
53         else {
54                 now = time(NULL);
55         }
56
57         serv_puts("RWHO");
58         serv_getln(buf, sizeof buf);
59         if (buf[0] == '1') {
60                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
61                         sess = extract_int(buf, 0);
62                         extract_token(user, buf, 1, '|', sizeof user);
63                         extract_token(room, buf, 2, '|', sizeof room);
64                         extract_token(host, buf, 3, '|', sizeof host);
65                         extract_token(realroom, buf, 9, '|', sizeof realroom);
66                         extract_token(realhost, buf, 10, '|', sizeof realhost);
67                         last_activity = extract_long(buf, 5);
68
69                         bg = 1 - bg;
70                         wprintf("<tr bgcolor=\"#%s\">",
71                                 (bg ? "DDDDDD" : "FFFFFF")
72                         );
73
74
75                         wprintf("<td>");
76                         if ((WC->is_aide) &&
77                             (sess != WC->ctdl_pid)) {
78                                 wprintf(" <a href=\"/terminate_session?which_session=%d", sess);
79                                 wprintf("\" onClick=\"return ConfirmKill();\" "
80                                 ">[kill]</a>");
81                         }
82                         if (sess == WC->ctdl_pid) {
83                                 wprintf(" <a href=\"/edit_me\" "
84                                         ">[edit]</a>");
85                         }
86                         wprintf("</td>");
87
88                         /* (link to page this user) */
89                         wprintf("<td><a href=\"/display_page?recp=");
90                         urlescputs(user);
91                         wprintf("\">"
92                                 "<img align=\"middle\" "
93                                 "src=\"/static/citadelchat_24x.gif\" "
94                                 "alt=\"(p)\""
95                                 " border=\"0\" /></a> ");
96                         wprintf("</td>");
97
98                         /* (idle flag) */
99                         wprintf("<td>");
100                         if ((now - last_activity) > 900L) {
101                                 wprintf(" "
102                                         "<img align=\"middle\" "
103                                         "src=\"/static/inactiveuser_24x.gif\" "
104                                         "alt=\"[idle]\" border=\"0\" />");
105                         }
106                         else {
107                                 wprintf(" "
108                                         "<img align=\"middle\" "
109                                         "src=\"/static/activeuser_24x.gif\" "
110                                         "alt=\"[active]\" border=\"0\" />");
111                         }
112                         wprintf("</td>\n<td>");
113
114
115
116                         /* username (link to user bio/photo page) */
117                         wprintf("<a href=\"/showuser?who=");
118                         urlescputs(user);
119                         wprintf("\">");
120                         escputs(user);
121                         wprintf("</a>");
122
123                         /* room */
124                         wprintf("</td>\n\t<td>");
125                         escputs(room);
126                         if (strlen(realroom) > 0) {
127                                 wprintf("<br /><i>");
128                                 escputs(realroom);
129                                 wprintf("</i>");
130                         }
131                         wprintf("</td>\n\t<td>");
132
133                         /* hostname */
134                         escputs(host);
135                         if (strlen(realhost) > 0) {
136                                 wprintf("<br /><i>");
137                                 escputs(realhost);
138                                 wprintf("</i>");
139                         }
140                         wprintf("</td>\n</tr>");
141                 }
142         }
143         wprintf("</table>");
144 }
145
146
147 /*
148  * XML-encapsulated version of wholist inner html
149  */
150 void who_inner_html(void) {
151         output_headers(0, 0, 0, 0, 0, 0, 0);
152
153         wprintf("Content-type: text/xml;charset=UTF-8\r\n"
154                 "Server: %s\r\n"
155                 "Connection: close\r\n"
156                 "Pragma: no-cache\r\n"
157                 "Cache-Control: no-store\r\n",
158                 SERVER);
159         begin_burst();
160
161         wprintf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
162                 "<ajax-response>\r\n"
163                 "<response type=\"element\" id=\"fix_scrollbar_bug\">\r\n"
164         );
165
166         who_inner_div();
167
168         wprintf("</response>\r\n"
169                 "</ajax-response>\r\n"
170                 "\r\n"
171         );
172
173         wDumpContent(0);
174 }
175
176
177 /*
178  * who is on?
179  */
180 void who(void)
181 {
182         /*
183         output_headers(1, 1, 2, 0, 1, 0, 0); old refresh30 version
184         */
185         output_headers(1, 1, 2, 0, 0, 0, 0);
186
187         wprintf("<script type=\"text/javascript\">\n"
188                 "function ConfirmKill() { \n"
189                 "return confirm('Do you really want to kill this session?');\n"
190                 "}\n"
191                 "</script>\n"
192         );
193
194         wprintf("<div id=\"banner\">\n");
195         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
196         wprintf("<IMG SRC=\"/static/usermanag_48x.gif\" ALT=\" \" "
197                 "ALIGN=MIDDLE "
198                 ">");
199                 /* "onLoad=\"javascript:bodyOnLoad()\" " */
200         wprintf("<SPAN CLASS=\"titlebar\"> Users currently on ");
201         escputs(serv_info.serv_humannode);
202         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
203         offer_start_page();
204         wprintf("</TD></TR></TABLE>\n");
205         wprintf("</div>\n");
206
207         wprintf("<div id=\"content\">\n");
208
209         wprintf("<div style=\"display:inline\" id=\"fix_scrollbar_bug\">");
210         who_inner_div();        /* Actual data handled by another function */
211         wprintf("</div>\n");
212
213         wprintf("<div id=\"instructions\" align=center>"
214                 "Click on a name to read user info.  Click on "
215                 "<IMG ALIGN=MIDDLE SRC=\"/static/citadelchat_16x.gif\" "
216                 "ALT=\"(p)\" BORDER=0>"
217                 " to send an instant message to that user.</div>\n");
218
219         /* JavaScript to make the ajax refresh happen:
220          * 1. Register the request 'getWholist' which calls the WebCit action 'who_inner_html'
221          * 2. Register the 'fix_scrollbar_bug' div as one we're interested in ajaxifying
222          * 3. setInterval to make the ajax refresh happen every 30 seconds.  The random number
223          *    in the request is there to prevent IE from caching the XML even though it's been
224          *    told not to.  Die, Microsoft, Die.
225          */
226         wprintf(
227 "                                                                       \n"
228 " <script type=\"text/javascript\">                                     \n"
229 "       ajaxEngine.registerRequest('getWholist', 'who_inner_html');\n"
230 "       ajaxEngine.registerAjaxElement('fix_scrollbar_bug');    \n"
231 "       setInterval(\"ajaxEngine.sendRequest('getWholist', 'junk='+Math.random());\", 30000);   \n"
232 "</script>\n"
233         );
234
235
236         wDumpContent(1);
237 }
238
239
240 void terminate_session(void)
241 {
242         char buf[SIZ];
243
244         serv_printf("TERM %s", bstr("which_session"));
245         serv_getln(buf, sizeof buf);
246         who();
247 }
248
249
250 /*
251  * Change your session info (fake roomname and hostname)
252  */
253 void edit_me(void)
254 {
255         char buf[SIZ];
256
257         if (!strcasecmp(bstr("sc"), "Change room name")) {
258                 serv_printf("RCHG %s", bstr("fake_roomname"));
259                 serv_getln(buf, sizeof buf);
260                 http_redirect("/who");
261         } else if (!strcasecmp(bstr("sc"), "Change host name")) {
262                 serv_printf("HCHG %s", bstr("fake_hostname"));
263                 serv_getln(buf, sizeof buf);
264                 http_redirect("/who");
265         } else if (!strcasecmp(bstr("sc"), "Change user name")) {
266                 serv_printf("UCHG %s", bstr("fake_username"));
267                 serv_getln(buf, sizeof buf);
268                 http_redirect("/who");
269         } else if (!strcasecmp(bstr("sc"), "Cancel")) {
270                 http_redirect("/who");
271         } else {
272
273                 output_headers(1, 1, 0, 0, 0, 0, 0);
274
275                 wprintf("<div id=\"banner\">\n");
276                 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
277                 wprintf("<SPAN CLASS=\"titlebar\">");
278                 wprintf("Edit your session display");
279                 wprintf("</SPAN></TD></TR></TABLE>\n");
280                 wprintf("</div>\n<div id=\"content\">\n");
281
282                 wprintf("This screen allows you to change the way your\n");
283                 wprintf("session appears in the 'Who is online' listing.\n");
284                 wprintf("To turn off any 'fake' name you've previously\n");
285                 wprintf("set, simply click the appropriate 'change' button\n");
286                 wprintf("without typing anything in the corresponding box.\n");
287                 wprintf("<br />\n");
288
289                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/edit_me\">\n");
290
291                 wprintf("<TABLE border=0 width=100%%>\n");
292
293                 wprintf("<TR><TD><B>Room name:</B></TD>\n<TD>");
294                 wprintf("<INPUT TYPE=\"text\" NAME=\"fake_roomname\" MAXLENGTH=\"64\">\n");
295                 wprintf("</TD>\n<TD ALIGN=center>");
296                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change room name\">");
297                 wprintf("</TD>\n</TR>\n");
298
299                 wprintf("<TR><TD><B>Host name:</B></TD><TD>");
300                 wprintf("<INPUT TYPE=\"text\" NAME=\"fake_hostname\" MAXLENGTH=\"64\">\n");
301                 wprintf("</TD>\n<TD ALIGN=center>");
302                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change host name\">");
303                 wprintf("</TD>\n</TR>\n");
304
305                 if (WC->is_aide) {
306                         wprintf("<TR><TD><B>User name:</B></TD><TD>");
307                         wprintf("<INPUT TYPE=\"text\" NAME=\"fake_username\" MAXLENGTH=\"64\">\n");
308                         wprintf("</TD>\n<TD ALIGN=center>");
309                         wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Change user name\">");
310                         wprintf("</TD>\n</TR>\n");
311                 }
312                 wprintf("<TR><TD> </TD><TD> </TD><TD ALIGN=center>");
313                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
314                 wprintf("</TD></TR></TABLE>\n");
315                 wprintf("</FORM></CENTER>\n");
316                 wDumpContent(1);
317         }
318 }