* upgrade hash retriever function, it now takes the usual cloud of parameters as...
[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 #if 0
235 /*
236  * Display a list of users currently logged in to the system
237  * /
238 void who(void)
239 {
240         char title[256];
241
242         output_headers(1, 1, 2, 0, 0, 0);
243
244         wprintf("<script type=\"text/javascript\">\n"
245                 "function ConfirmKill() { \n"
246                 "return confirm('%s');\n"
247                 "}\n"
248                 "</script>\n", _("Do you really want to kill this session?")
249         );
250 #endif
251
252         wprintf("<div id=\"banner\">\n");
253         wprintf("<div class=\"room_banner\">");
254         wprintf("<img src=\"static/usermanag_48x.gif\">");
255         wprintf("<h1>");
256         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
257         escputs(title);
258         wprintf("</h1></div>");
259         wprintf("<ul class=\"room_actions\">\n");
260         wprintf("<li class=\"start_page\">");
261         offer_start_page();
262         wprintf("</li></ul>");
263         wprintf("</div>");
264
265         wprintf("<div id=\"content\" class=\"fix_scrollbar_bug who_is_online\">\n");
266         wprintf("<div class=\"box\">");
267         wprintf("<div class=\"boxlabel\">");    
268         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
269         escputs(title);
270         wprintf("</div>");      
271         wprintf("<div class=\"boxcontent\">");
272         wprintf("<div id=\"who_inner\" >");
273         who_inner_div();
274         wprintf("</div>");
275
276         wprintf("<div class=\"instructions\">");
277         wprintf(_("Click on a name to read user info.  Click on %s "
278                 "to send an instant message to that user."),
279                 "<img align=\"middle\" src=\"static/citadelchat_16x.gif\" alt=\"(p)\" border=\"0\">"
280         );
281         wprintf("</div></div>\n");
282
283         / *
284          * JavaScript to make the ajax refresh happen:
285          * See http://www.sergiopereira.com/articles/prototype.js.html for info on Ajax.PeriodicalUpdater
286          * It wants: 1. The div being updated
287          *           2. The URL of the update source
288          *           3. Other flags (such as the HTTP method and the refresh frequency)
289          * /
290         wprintf(
291                 "<script type=\"text/javascript\">                                      "
292                 " new Ajax.PeriodicalUpdater('who_inner', 'who_inner_html',     "
293                 "                            { method: 'get', frequency: 30 }  );       "
294                 "</script>                                                              \n"
295         );
296         wDumpContent(1);
297 }
298 */
299 #endif
300
301 /*
302  * end session
303  */
304 void terminate_session(void)
305 {
306         char buf[SIZ];
307
308         serv_printf("TERM %s", bstr("which_session"));
309         serv_getln(buf, sizeof buf);
310         ///who();
311         url_do_template();
312 }
313
314
315 /*
316  * Change your session info (fake roomname and hostname)
317  */
318 void edit_me(void)
319 {
320         char buf[SIZ];
321
322         if (havebstr("change_room_name_button")) {
323                 serv_printf("RCHG %s", bstr("fake_roomname"));
324                 serv_getln(buf, sizeof buf);
325                 http_redirect("who");
326         } else if (havebstr("change_host_name_button")) {
327                 serv_printf("HCHG %s", bstr("fake_hostname"));
328                 serv_getln(buf, sizeof buf);
329                 http_redirect("who");
330         } else if (havebstr("change_user_name_button")) {
331                 serv_printf("UCHG %s", bstr("fake_username"));
332                 serv_getln(buf, sizeof buf);
333                 http_redirect("who");
334         } else if (havebstr("cancel_button")) {
335                 http_redirect("who");
336         } else {
337                 output_headers(1, 1, 0, 0, 0, 0);
338
339                 wprintf("<div id=\"banner\">\n");
340                 wprintf("<table class=\"who_banner\"><tr><td>");
341                 wprintf("<span class=\"titlebar\">");
342                 wprintf(_("Edit your session display"));
343                 wprintf("</span></td></tr></table>\n");
344                 wprintf("</div>\n<div id=\"content\">\n");
345
346                 wprintf(_("This screen allows you to change the way your "
347                         "session appears in the 'Who is online' listing. "
348                         "To turn off any 'fake' name you've previously "
349                         "set, simply click the appropriate 'change' button "
350                         "without typing anything in the corresponding box. "));
351                 wprintf("<br />\n");
352
353                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
354                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
355
356                 wprintf("<table border=0 width=100%%>\n");
357
358                 wprintf("<tr><td><b>");
359                 wprintf(_("Room name:"));
360                 wprintf("</b></td>\n<td>");
361                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
362                 wprintf("</td>\n<td align=center>");
363                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
364                         _("Change room name"));
365                 wprintf("</td>\n</tr>\n");
366
367                 wprintf("<tr><td><b>");
368                 wprintf(_("Host name:"));
369                 wprintf("</b></td><td>");
370                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
371                 wprintf("</td>\n<td align=center>");
372                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
373                         _("Change host name"));
374                 wprintf("</td>\n</tr>\n");
375
376                 if (WC->is_aide) {
377                         wprintf("<tr><td><b>");
378                         wprintf(_("User name:"));
379                         wprintf("</b></td><td>");
380                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
381                         wprintf("</td>\n<td align=center>");
382                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
383                                 _("Change user name"));
384                         wprintf("</td>\n</tr>\n");
385                 }
386                 wprintf("<tr><td> </td><td> </td><td align=center>");
387                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
388                         _("Cancel"));
389                 wprintf("</td></tr></table>\n");
390                 wprintf("</form></center>\n");
391                 wDumpContent(1);
392         }
393 }
394
395 /*
396  * Wholist section
397  * /
398 void wholist_section(void) {
399         UserStateStruct *User;
400         void *VUser;
401         HashList *List;
402         HashPos  *it;
403         const char *UserName;
404         long len;
405         char buf[SIZ];
406         time_t now;
407
408         serv_puts("TIME");
409         serv_getln(buf, sizeof buf);
410         if (buf[0] == '2') {
411                 now = extract_long(&buf[4], 0);
412         }
413         else {
414                 now = time(NULL);
415         }
416
417         List = NewHash(1, NULL);
418
419         if (GetWholistSection(List, now)) {
420                 SortByPayload(List, CompareUserStruct);
421                 it = GetNewHashPos();
422                 while (GetNextHashPos(List, it, &len, &UserName, &VUser)) {
423                         User = VUser;
424                         if (strcmp(ChrPtr(User->UserName), NLI)) {
425                                 wprintf("<li class=\"");
426                                 if (User->Idle) {
427                                         wprintf("inactiveuser");
428                                 }
429                                 else {
430                                         wprintf("activeuser");
431                                 }
432                                 wprintf("\"><a href=\"showuser?who=");
433                                 UrlescPutStrBuf(User->UserName);
434                                 wprintf("\">");
435                                 StrEscPuts(User->UserName);
436                                 wprintf("</a></li>");
437                         }
438                 }
439                 DeleteHashPos(&it);
440         }
441         DeleteHash(&List);
442 }
443 */
444
445 void _terminate_session(void) {
446         slrp_highest();
447         terminate_session();
448 }
449
450 HashList *GetWholistHash(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
451 {
452         HashList *List;
453         char buf[SIZ];
454         time_t now;
455
456         serv_puts("TIME");
457         serv_getln(buf, sizeof buf);
458         if (buf[0] == '2') {
459                 now = extract_long(&buf[4], 0);
460         }
461         else {
462                 now = time(NULL);
463         }
464
465         List = NewHash(1, NULL);
466         GetWholistSection(List, now);
467         return List;
468 }
469
470 void WholistSubst(StrBuf *TemplBuffer, void *vContext, WCTemplateToken *Token)
471 {
472         UserStateStruct *User = (UserStateStruct*) vContext;
473
474         SVPutBuf("WHO:NAME", User->UserName, 1);
475         SVPutBuf("WHO:ROOM", User->Room, 1);
476         SVPutBuf("WHO:HOST", User->Host, 1);
477         SVPutBuf("WHO:REALROOM", User->RealRoom, 1);
478         SVPutBuf("WHO:REALHOST", User->RealHost, 1);
479         svputlong("WHO:LASTACTIVE", User->LastActive);
480         ///svputlong("WHO:IDLESINCE",(now - User->LastActive) / 60);//// todo
481         svputlong("WHO:SESSION", User->Session);
482         svputlong("WHO:IDLE", User->Idle);
483         svputlong("WHO:NSESSIONS", User->SessionCount);
484         svputlong("WHO:ISME", (User->Session == WC->ctdl_pid));
485 }
486
487 void DeleteWholistHash(HashList **KillMe)
488 {
489         DeleteHash(KillMe);
490 }
491
492 void 
493 InitModule_WHO
494 (void)
495 {
496 ///     WebcitAddUrlHandler(HKEY("who"), who, 0);
497 //      WebcitAddUrlHandler(HKEY("who_inner_html"), who_inner_div, AJAX);
498 //      WebcitAddUrlHandler(HKEY("wholist_section"), wholist_section, AJAX);
499         WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
500         WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
501
502         RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, WholistSubst, DeleteWholistHash, CTX_WHO);
503 }