* prefer the actual session id
[citadel.git] / webcit / who.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup DislpayWho Display a list of all users currently logged on to the Citadel server.
6  * \ingroup WebcitDisplayItems
7  */
8 /*@{*/
9 #include "webcit.h"
10
11
12 typedef struct UserStateStruct {
13         char *UserName;
14         long UserNameLen;
15         char *Room;
16         long RoomLen;
17         char *Host;
18         long HostLen;
19         char *RealRoom;
20         long RealRoomLen;
21         char *RealHost;
22         long RealHostLen;
23         long LastActive;
24         int Session;
25         int Idle;
26         int SessionCount;
27 } UserStateStruct;
28
29 void DestroyUserStruct(void *vUser)
30 {
31         UserStateStruct *User = (UserStateStruct*) vUser;
32         free(User->UserName);
33         free(User->Room);
34         free(User->Host);
35         free(User->RealRoom);
36         free(User->RealHost);
37         free(User);
38 }
39
40 int CompareUserStruct(const void *VUser1, const void *VUser2)
41 {
42         const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
43         const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
44         
45         if (User1->Idle != User2->Idle)
46                 return User1->Idle > User2->Idle;
47         return strcasecmp(User1->UserName, User2->UserName);
48 }
49
50
51 int GetWholistSection(HashList *List, time_t now)
52 {
53         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
54         UserStateStruct *User, *OldUser;
55         char buf[SIZ], user[SIZ], room[SIZ], host[SIZ],
56                 realroom[SIZ], realhost[SIZ];
57         size_t BufLen;
58
59         serv_puts("RWHO");
60         serv_getln(buf, sizeof buf);
61         if (buf[0] == '1') {
62                 while (BufLen = serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
63                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
64                         User->Session = extract_int(buf, 0);
65
66                         User->UserNameLen = extract_token(user, buf, 1, '|', sizeof user);
67                         User->UserName = malloc(User->UserNameLen + 1);
68                         memcpy(User->UserName, user, User->UserNameLen + 1);
69
70                         User->RoomLen = extract_token(room, buf, 2, '|', sizeof room);
71                         User->Room = malloc(User->RoomLen + 1);
72                         memcpy(User->Room, room, User->RoomLen + 1);
73
74                         User->HostLen = extract_token(host, buf, 3, '|', sizeof host);
75                         User->Host = malloc(User->HostLen + 1);
76                         memcpy(User->Host, host, User->HostLen + 1);
77
78                         User->RealRoomLen = extract_token(realroom, buf, 9, '|', sizeof realroom);
79                         User->RealRoom = malloc(User->RealRoomLen + 1);
80                         memcpy(User->RealRoom, realroom, User->RealRoomLen + 1);
81
82                         User->RealHostLen = extract_token(realhost, buf, 10, '|', sizeof realhost);
83                         User->RealHost = malloc(User->RealHostLen + 1);
84                         memcpy(User->RealHost, realhost, User->RealHostLen + 1);
85                         
86                         User->LastActive = extract_long(buf, 5);
87                         User->Idle = (now - User->LastActive) > 900L;
88                         User->SessionCount = 1;
89
90                         if (GetHash(List, User->UserName, User->UserNameLen, (void**)&OldUser)) {
91                                 OldUser->SessionCount++;
92                                 if (!User->Idle) {
93                                         if (User->Session == WCC->ctdl_pid) 
94                                                 OldUser->Session = User->Session;
95
96                                         OldUser->Idle = User->Idle;
97                                         OldUser->LastActive = User->LastActive;
98                                 }
99                                 DestroyUserStruct(User);
100                         }
101                         else
102                                 Put(List, User->UserName, User->UserNameLen, User, DestroyUserStruct);
103                 }
104                 SortByPayload(List, CompareUserStruct);
105                 return 1;
106         }
107         else
108                 return 0;
109 }
110
111 /**
112  * \brief Display inner div of Wholist
113  */
114 void who_inner_div(void) {
115         UserStateStruct *User;
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();
144
145         if (GetWholistSection(List, now)) {
146                 it = GetNewHashPos();
147                 while (GetNextHashPos(List, it, &len, &UserName, (void**)&User)) {
148
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 %d %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  * \brief who is on?
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  * \brief end session \todo what??? does this belong here? 
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  * \brief Change your session info (fake roomname and hostname)
311  */
312 void edit_me(void)
313 {
314         char buf[SIZ];
315
316         if (!IsEmptyStr(bstr("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 (!IsEmptyStr(bstr("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 (!IsEmptyStr(bstr("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 (!IsEmptyStr(bstr("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=\"%ld\">\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  * \brief Wholist section
391  */
392 void wholist_section(void) {
393         UserStateStruct *User;
394         HashList *List;
395         HashPos  *it;
396         char *UserName;
397         long len;
398         char buf[SIZ];
399         time_t now;
400
401         serv_puts("TIME");
402         serv_getln(buf, sizeof buf);
403         if (buf[0] == '2') {
404                 now = extract_long(&buf[4], 0);
405         }
406         else {
407                 now = time(NULL);
408         }
409
410         List = NewHash();
411
412         if (GetWholistSection(List, now)) {
413                 SortByPayload(List, CompareUserStruct);
414                 it = GetNewHashPos();
415                 while (GetNextHashPos(List, it, &len, &UserName, (void**)&User)) {
416                         if (strcmp(User->UserName, NLI)) {
417                                 wprintf("<li class=\"");
418                                 if (User->Idle) {
419                                         wprintf("inactiveuser");
420                                 }
421                                 else {
422                                         wprintf("activeuser");
423                                 }
424                                 wprintf("\"><a href=\"showuser?who=");
425                                 urlescputs(User->UserName);
426                                 wprintf("\">");
427                                 escputs(User->UserName);
428                                 wprintf("</a></li>");
429                         }
430                 }
431                 DeleteHashPos(&it);
432         }
433         DeleteHash(&List);
434 }
435
436
437
438 /*@}*/