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