d37105c2b252c525a701980cd3d466952682e80e
[citadel.git] / webcit / who.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6
7 typedef struct UserStateStruct {
8         char *UserName;
9         long UserNameLen;
10         char *Room;
11         long RoomLen;
12         char *Host;
13         long HostLen;
14         char *RealRoom;
15         long RealRoomLen;
16         char *RealHost;
17         long RealHostLen;
18         long LastActive;
19         int Session;
20         int Idle;
21         int SessionCount;
22 } UserStateStruct;
23
24 void DestroyUserStruct(void *vUser)
25 {
26         UserStateStruct *User = (UserStateStruct*) vUser;
27         free(User->UserName);
28         free(User->Room);
29         free(User->Host);
30         free(User->RealRoom);
31         free(User->RealHost);
32         free(User);
33 }
34
35 int CompareUserStruct(const void *VUser1, const void *VUser2)
36 {
37         const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
38         const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
39         
40         if (User1->Idle != User2->Idle)
41                 return User1->Idle > User2->Idle;
42         return strcasecmp(User1->UserName, User2->UserName);
43 }
44
45
46 int GetWholistSection(HashList *List, time_t now)
47 {
48         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
49         UserStateStruct *User, *OldUser;
50         void *VOldUser;
51         char buf[SIZ], user[SIZ], room[SIZ], host[SIZ],
52                 realroom[SIZ], realhost[SIZ];
53         size_t BufLen;
54
55         serv_puts("RWHO");
56         serv_getln(buf, sizeof buf);
57         if (buf[0] == '1') {
58                 while (BufLen = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
59                         if (BufLen <= 0)
60                             continue;
61                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
62                         User->Session = extract_int(buf, 0);
63
64                         User->UserNameLen = extract_token(user, buf, 1, '|', sizeof user);
65                         User->UserName = malloc(User->UserNameLen + 1);
66                         memcpy(User->UserName, user, User->UserNameLen + 1);
67
68                         User->RoomLen = extract_token(room, buf, 2, '|', sizeof room);
69                         User->Room = malloc(User->RoomLen + 1);
70                         memcpy(User->Room, room, User->RoomLen + 1);
71
72                         User->HostLen = extract_token(host, buf, 3, '|', sizeof host);
73                         User->Host = malloc(User->HostLen + 1);
74                         memcpy(User->Host, host, User->HostLen + 1);
75
76                         User->RealRoomLen = extract_token(realroom, buf, 9, '|', sizeof realroom);
77                         User->RealRoom = malloc(User->RealRoomLen + 1);
78                         memcpy(User->RealRoom, realroom, User->RealRoomLen + 1);
79
80                         User->RealHostLen = extract_token(realhost, buf, 10, '|', sizeof realhost);
81                         User->RealHost = malloc(User->RealHostLen + 1);
82                         memcpy(User->RealHost, realhost, User->RealHostLen + 1);
83                         
84                         User->LastActive = extract_long(buf, 5);
85                         User->Idle = (now - User->LastActive) > 900L;
86                         User->SessionCount = 1;
87
88                         if (GetHash(List, User->UserName, User->UserNameLen, &VOldUser)) {
89                                 OldUser = VOldUser;
90                                 OldUser->SessionCount++;
91                                 if (!User->Idle) {
92                                         if (User->Session == WCC->ctdl_pid) 
93                                                 OldUser->Session = User->Session;
94
95                                         OldUser->Idle = User->Idle;
96                                         OldUser->LastActive = User->LastActive;
97                                 }
98                                 DestroyUserStruct(User);
99                         }
100                         else
101                                 Put(List, User->UserName, User->UserNameLen, User, DestroyUserStruct);
102                 }
103                 SortByPayload(List, CompareUserStruct);
104                 return 1;
105         }
106         else
107                 return 0;
108 }
109
110 /*
111  * Display inner div of Wholist
112  */
113 void who_inner_div(void) {
114         UserStateStruct *User;
115         void *VUser;
116         char buf[SIZ];
117         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
118         HashList *List;
119         HashPos  *it;
120         char *UserName;
121         long len;
122         time_t now;
123         int bg = 0;
124
125         wprintf("<table class=\"altern\">"
126                 "<tr>\n");
127         wprintf("<th class=\"edit_col\"> </th>\n");
128         wprintf("<th colspan=\"2\"> </th>\n");
129         wprintf("<th>%s</th>\n", _("User name"));
130         wprintf("<th>%s</th>", _("Room"));
131         wprintf("<th class=\"host_col\">%s</th>\n</tr>\n", _("From host"));
132
133         serv_puts("TIME");
134         serv_getln(buf, sizeof buf);
135
136         if (buf[0] == '2') {
137                 now = extract_long(&buf[4], 0);
138         }
139         else {
140                 now = time(NULL);
141         }
142
143         List = NewHash(1, NULL);
144
145         if (GetWholistSection(List, now)) {
146                 it = GetNewHashPos();
147                 while (GetNextHashPos(List, it, &len, &UserName, &VUser)) {
148                         User = VUser;
149                         bg = 1 - bg;
150                         wprintf("<tr class=\"%s\">",
151                                 (bg ? "even" : "odd")
152                         );
153
154
155                         wprintf("<td class=\"edit_col\">");
156                         if ((WCC->is_aide) &&
157                             (User->Session != WCC->ctdl_pid)) {
158                                 wprintf(" <a href=\"terminate_session?which_session=%d", User->Session);
159                                 wprintf("\" onClick=\"return ConfirmKill();\">%s</a>", _("(kill)"));
160                         }
161                         if (User->Session == WCC->ctdl_pid) {
162                                 wprintf(" <a href=\"edit_me\">%s</a>", _("(edit)"));
163                         }
164                         wprintf("</td>");
165
166                         /* (link to page this user) */
167                         wprintf("<td width=\"5%%\"><a href=\"display_page?recp=");
168                         urlescputs(User->UserName);
169                         wprintf("\">"
170                                 "<img align=\"middle\" "
171                                 "src=\"static/citadelchat_24x.gif\" "
172                                 "alt=\"(p)\""
173                                 " border=\"0\" /></a> ");
174                         wprintf("</td>");
175
176                         /* (idle flag) */
177                         wprintf("<td width=\"5%%\">");
178                         if (User->Idle) {
179                                 wprintf(" "
180                                         "<img align=\"middle\" "
181                                         "src=\"static/inactiveuser_24x.gif\" "
182                                         "alt=\"(%s %ld %s)\" border=\"0\" />",
183                                         _("idle since"),
184                                         (now - User->LastActive) / 60,
185                                         _("Minutes")
186                                         );
187                                 
188                         }
189                         else {
190                                 wprintf(" "
191                                         "<img align=\"middle\" "
192                                         "src=\"static/activeuser_24x.gif\" "
193                                         "alt=\"(active)\" border=\"0\" />");
194                         }
195                         wprintf("</td>\n<td>");
196
197                         /* username (link to user bio/photo page) */
198                         wprintf("<a href=\"showuser?who=");
199                         urlescputs(User->UserName);
200                         wprintf("\">");
201                         escputs(User->UserName);
202                         if (User->SessionCount > 1)
203                                 wprintf(" [%d] ", User->SessionCount);
204                         wprintf("</a>");
205
206                         /* room */
207                         wprintf("</td>\n\t<td>");
208                         escputs(User->Room);
209                         if (!IsEmptyStr(User->RealRoom) ) {
210                                 wprintf("<br /><i>");
211                                 escputs(User->RealRoom);
212                                 wprintf("</i>");
213                         }
214                         wprintf("</td>\n\t<td class=\"host_col\">");
215
216                         /* hostname */
217                         escputs(User->Host);
218                         if (!IsEmptyStr(User->RealHost)) {
219                                 wprintf("<br /><i>");
220                                 escputs(User->RealHost);
221                                 wprintf("</i>");
222                         }
223                         wprintf("</td>\n</tr>");
224                 }
225                 DeleteHashPos(&it);
226         }
227         wprintf("</table>");
228         DeleteHash(&List);
229
230 }
231
232
233 /*
234  * Display a list of users currently logged in to the system
235  */
236 void who(void)
237 {
238         char title[256];
239
240         output_headers(1, 1, 2, 0, 0, 0);
241
242         wprintf("<script type=\"text/javascript\">\n"
243                 "function ConfirmKill() { \n"
244                 "return confirm('%s');\n"
245                 "}\n"
246                 "</script>\n", _("Do you really want to kill this session?")
247         );
248
249         wprintf("<div id=\"banner\">\n");
250         wprintf("<div class=\"room_banner\">");
251         wprintf("<img src=\"static/usermanag_48x.gif\">");
252         wprintf("<h1>");
253         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
254         escputs(title);
255         wprintf("</h1></div>");
256         wprintf("<ul class=\"room_actions\">\n");
257         wprintf("<li class=\"start_page\">");
258         offer_start_page();
259         wprintf("</li></ul>");
260         wprintf("</div>");
261
262         wprintf("<div id=\"content\" class=\"fix_scrollbar_bug who_is_online\">\n");
263         wprintf("<div class=\"box\">");
264         wprintf("<div class=\"boxlabel\">");    
265         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
266         escputs(title);
267         wprintf("</div>");      
268         wprintf("<div class=\"boxcontent\">");
269         wprintf("<div id=\"who_inner\" >");
270         who_inner_div();
271         wprintf("</div>");
272
273         wprintf("<div class=\"instructions\">");
274         wprintf(_("Click on a name to read user info.  Click on %s "
275                 "to send an instant message to that user."),
276                 "<img align=\"middle\" src=\"static/citadelchat_16x.gif\" alt=\"(p)\" border=\"0\">"
277         );
278         wprintf("</div></div>\n");
279
280         /*
281          * JavaScript to make the ajax refresh happen:
282          * See http://www.sergiopereira.com/articles/prototype.js.html for info on Ajax.PeriodicalUpdater
283          * It wants: 1. The div being updated
284          *           2. The URL of the update source
285          *           3. Other flags (such as the HTTP method and the refresh frequency)
286          */
287         wprintf(
288                 "<script type=\"text/javascript\">                                      "
289                 " new Ajax.PeriodicalUpdater('who_inner', 'who_inner_html',     "
290                 "                            { method: 'get', frequency: 30 }  );       "
291                 "</script>                                                              \n"
292         );
293         wDumpContent(1);
294 }
295
296 /*
297  * end session
298  */
299 void terminate_session(void)
300 {
301         char buf[SIZ];
302
303         serv_printf("TERM %s", bstr("which_session"));
304         serv_getln(buf, sizeof buf);
305         who();
306 }
307
308
309 /*
310  * Change your session info (fake roomname and hostname)
311  */
312 void edit_me(void)
313 {
314         char buf[SIZ];
315
316         if (havebstr("change_room_name_button")) {
317                 serv_printf("RCHG %s", bstr("fake_roomname"));
318                 serv_getln(buf, sizeof buf);
319                 http_redirect("who");
320         } else if (havebstr("change_host_name_button")) {
321                 serv_printf("HCHG %s", bstr("fake_hostname"));
322                 serv_getln(buf, sizeof buf);
323                 http_redirect("who");
324         } else if (havebstr("change_user_name_button")) {
325                 serv_printf("UCHG %s", bstr("fake_username"));
326                 serv_getln(buf, sizeof buf);
327                 http_redirect("who");
328         } else if (havebstr("cancel_button")) {
329                 http_redirect("who");
330         } else {
331                 output_headers(1, 1, 0, 0, 0, 0);
332
333                 wprintf("<div id=\"banner\">\n");
334                 wprintf("<table class=\"who_banner\"><tr><td>");
335                 wprintf("<span class=\"titlebar\">");
336                 wprintf(_("Edit your session display"));
337                 wprintf("</span></td></tr></table>\n");
338                 wprintf("</div>\n<div id=\"content\">\n");
339
340                 wprintf(_("This screen allows you to change the way your "
341                         "session appears in the 'Who is online' listing. "
342                         "To turn off any 'fake' name you've previously "
343                         "set, simply click the appropriate 'change' button "
344                         "without typing anything in the corresponding box. "));
345                 wprintf("<br />\n");
346
347                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
348                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
349
350                 wprintf("<table border=0 width=100%%>\n");
351
352                 wprintf("<tr><td><b>");
353                 wprintf(_("Room name:"));
354                 wprintf("</b></td>\n<td>");
355                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
356                 wprintf("</td>\n<td align=center>");
357                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
358                         _("Change room name"));
359                 wprintf("</td>\n</tr>\n");
360
361                 wprintf("<tr><td><b>");
362                 wprintf(_("Host name:"));
363                 wprintf("</b></td><td>");
364                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
365                 wprintf("</td>\n<td align=center>");
366                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
367                         _("Change host name"));
368                 wprintf("</td>\n</tr>\n");
369
370                 if (WC->is_aide) {
371                         wprintf("<tr><td><b>");
372                         wprintf(_("User name:"));
373                         wprintf("</b></td><td>");
374                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
375                         wprintf("</td>\n<td align=center>");
376                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
377                                 _("Change user name"));
378                         wprintf("</td>\n</tr>\n");
379                 }
380                 wprintf("<tr><td> </td><td> </td><td align=center>");
381                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
382                         _("Cancel"));
383                 wprintf("</td></tr></table>\n");
384                 wprintf("</form></center>\n");
385                 wDumpContent(1);
386         }
387 }
388
389 /*
390  * Wholist section
391  */
392 void wholist_section(void) {
393         UserStateStruct *User;
394         void *VUser;
395         HashList *List;
396         HashPos  *it;
397         char *UserName;
398         long len;
399         char buf[SIZ];
400         time_t now;
401
402         serv_puts("TIME");
403         serv_getln(buf, sizeof buf);
404         if (buf[0] == '2') {
405                 now = extract_long(&buf[4], 0);
406         }
407         else {
408                 now = time(NULL);
409         }
410
411         List = NewHash(1, NULL);
412
413         if (GetWholistSection(List, now)) {
414                 SortByPayload(List, CompareUserStruct);
415                 it = GetNewHashPos();
416                 while (GetNextHashPos(List, it, &len, &UserName, &VUser)) {
417                         User = VUser;
418                         if (strcmp(User->UserName, NLI)) {
419                                 wprintf("<li class=\"");
420                                 if (User->Idle) {
421                                         wprintf("inactiveuser");
422                                 }
423                                 else {
424                                         wprintf("activeuser");
425                                 }
426                                 wprintf("\"><a href=\"showuser?who=");
427                                 urlescputs(User->UserName);
428                                 wprintf("\">");
429                                 escputs(User->UserName);
430                                 wprintf("</a></li>");
431                         }
432                 }
433                 DeleteHashPos(&it);
434         }
435         DeleteHash(&List);
436 }
437
438 void _terminate_session(void) {
439         slrp_highest();
440         terminate_session();
441 }
442
443 void 
444 InitModule_WHO
445 (void)
446 {
447         WebcitAddUrlHandler(HKEY("who"), who, 0);
448         WebcitAddUrlHandler(HKEY("who_inner_html"), who_inner_div, AJAX);
449         WebcitAddUrlHandler(HKEY("wholist_section"), wholist_section, AJAX);
450         WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
451         WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
452 }