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