f13ba59d396a84c64a560e0ad2d5cef19766b973
[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                         if (BufLen <= 0)
64                             continue;
65                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
66                         User->Session = extract_int(buf, 0);
67
68                         User->UserNameLen = extract_token(user, buf, 1, '|', sizeof user);
69                         User->UserName = malloc(User->UserNameLen + 1);
70                         memcpy(User->UserName, user, User->UserNameLen + 1);
71
72                         User->RoomLen = extract_token(room, buf, 2, '|', sizeof room);
73                         User->Room = malloc(User->RoomLen + 1);
74                         memcpy(User->Room, room, User->RoomLen + 1);
75
76                         User->HostLen = extract_token(host, buf, 3, '|', sizeof host);
77                         User->Host = malloc(User->HostLen + 1);
78                         memcpy(User->Host, host, User->HostLen + 1);
79
80                         User->RealRoomLen = extract_token(realroom, buf, 9, '|', sizeof realroom);
81                         User->RealRoom = malloc(User->RealRoomLen + 1);
82                         memcpy(User->RealRoom, realroom, User->RealRoomLen + 1);
83
84                         User->RealHostLen = extract_token(realhost, buf, 10, '|', sizeof realhost);
85                         User->RealHost = malloc(User->RealHostLen + 1);
86                         memcpy(User->RealHost, realhost, User->RealHostLen + 1);
87                         
88                         User->LastActive = extract_long(buf, 5);
89                         User->Idle = (now - User->LastActive) > 900L;
90                         User->SessionCount = 1;
91
92                         if (GetHash(List, User->UserName, User->UserNameLen, (void**)&OldUser)) {
93                                 OldUser->SessionCount++;
94                                 if (!User->Idle) {
95                                         if (User->Session == WCC->ctdl_pid) 
96                                                 OldUser->Session = User->Session;
97
98                                         OldUser->Idle = User->Idle;
99                                         OldUser->LastActive = User->LastActive;
100                                 }
101                                 DestroyUserStruct(User);
102                         }
103                         else
104                                 Put(List, User->UserName, User->UserNameLen, User, DestroyUserStruct);
105                 }
106                 SortByPayload(List, CompareUserStruct);
107                 return 1;
108         }
109         else
110                 return 0;
111 }
112
113 /**
114  * \brief Display inner div of Wholist
115  */
116 void who_inner_div(void) {
117         UserStateStruct *User;
118         char buf[SIZ];
119         struct wcsession *WCC = WC;     /* This is done to make it run faster; WC is a function */
120         HashList *List;
121         HashPos  *it;
122         char *UserName;
123         long len;
124         time_t now;
125         int bg = 0;
126
127         wprintf("<table class=\"altern\">"
128                 "<tr>\n");
129         wprintf("<th class=\"edit_col\"> </th>\n");
130         wprintf("<th colspan=\"2\"> </th>\n");
131         wprintf("<th>%s</th>\n", _("User name"));
132         wprintf("<th>%s</th>", _("Room"));
133         wprintf("<th class=\"host_col\">%s</th>\n</tr>\n", _("From host"));
134
135         serv_puts("TIME");
136         serv_getln(buf, sizeof buf);
137
138         if (buf[0] == '2') {
139                 now = extract_long(&buf[4], 0);
140         }
141         else {
142                 now = time(NULL);
143         }
144
145         List = NewHash();
146
147         if (GetWholistSection(List, now)) {
148                 it = GetNewHashPos();
149                 while (GetNextHashPos(List, it, &len, &UserName, (void**)&User)) {
150
151                         bg = 1 - bg;
152                         wprintf("<tr class=\"%s\">",
153                                 (bg ? "even" : "odd")
154                         );
155
156
157                         wprintf("<td class=\"edit_col\">");
158                         if ((WCC->is_aide) &&
159                             (User->Session != WCC->ctdl_pid)) {
160                                 wprintf(" <a href=\"terminate_session?which_session=%d", User->Session);
161                                 wprintf("\" onClick=\"return ConfirmKill();\">%s</a>", _("(kill)"));
162                         }
163                         if (User->Session == WCC->ctdl_pid) {
164                                 wprintf(" <a href=\"edit_me\">%s</a>", _("(edit)"));
165                         }
166                         wprintf("</td>");
167
168                         /** (link to page this user) */
169                         wprintf("<td width=\"5%\"><a href=\"display_page?recp=");
170                         urlescputs(User->UserName);
171                         wprintf("\">"
172                                 "<img align=\"middle\" "
173                                 "src=\"static/citadelchat_24x.gif\" "
174                                 "alt=\"(p)\""
175                                 " border=\"0\" /></a> ");
176                         wprintf("</td>");
177
178                         /** (idle flag) */
179                         wprintf("<td width=\"5%\">");
180                         if (User->Idle) {
181                                 wprintf(" "
182                                         "<img align=\"middle\" "
183                                         "src=\"static/inactiveuser_24x.gif\" "
184                                         "alt=\"(%s %d %s)\" border=\"0\" />",
185                                         _("idle since"),
186                                         (now - User->LastActive) / 60,
187                                         _("Minutes")
188                                         );
189                                 
190                         }
191                         else {
192                                 wprintf(" "
193                                         "<img align=\"middle\" "
194                                         "src=\"static/activeuser_24x.gif\" "
195                                         "alt=\"(active)\" border=\"0\" />");
196                         }
197                         wprintf("</td>\n<td>");
198
199                         /** username (link to user bio/photo page) */
200                         wprintf("<a href=\"showuser?who=");
201                         urlescputs(User->UserName);
202                         wprintf("\">");
203                         escputs(User->UserName);
204                         if (User->SessionCount > 1)
205                                 wprintf(" [%d] ", User->SessionCount);
206                         wprintf("</a>");
207
208                         /** room */
209                         wprintf("</td>\n\t<td>");
210                         escputs(User->Room);
211                         if (!IsEmptyStr(User->RealRoom) ) {
212                                 wprintf("<br /><i>");
213                                 escputs(User->RealRoom);
214                                 wprintf("</i>");
215                         }
216                         wprintf("</td>\n\t<td class=\"host_col\">");
217
218                         /** hostname */
219                         escputs(User->Host);
220                         if (!IsEmptyStr(User->RealHost)) {
221                                 wprintf("<br /><i>");
222                                 escputs(User->RealHost);
223                                 wprintf("</i>");
224                         }
225                         wprintf("</td>\n</tr>");
226                 }
227                 DeleteHashPos(&it);
228         }
229         wprintf("</table>");
230         DeleteHash(&List);
231
232 }
233
234
235 /**
236  * \brief who is on?
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
251         wprintf("<div id=\"banner\">\n");
252         wprintf("<div class=\"room_banner\">");
253         wprintf("<img src=\"static/usermanag_48x.gif\">");
254         wprintf("<h1>");
255         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
256         escputs(title);
257         wprintf("</h1></div>");
258         wprintf("<ul class=\"room_actions\">\n");
259         wprintf("<li class=\"start_page\">");
260         offer_start_page();
261         wprintf("</li></ul>");
262         wprintf("</div>");
263
264         wprintf("<div id=\"content\" class=\"fix_scrollbar_bug who_is_online\">\n");
265         wprintf("<div class=\"box\">");
266         wprintf("<div class=\"boxlabel\">");    
267         snprintf(title, sizeof title, _("Users currently on %s"), serv_info.serv_humannode);
268         escputs(title);
269         wprintf("</div>");      
270         wprintf("<div class=\"boxcontent\">");
271         wprintf("<div id=\"who_inner\" >");
272         who_inner_div();
273         wprintf("</div>");
274
275         wprintf("<div class=\"instructions\">");
276         wprintf(_("Click on a name to read user info.  Click on %s "
277                 "to send an instant message to that user."),
278                 "<img align=\"middle\" src=\"static/citadelchat_16x.gif\" alt=\"(p)\" border=\"0\">"
279         );
280         wprintf("</div></div>\n");
281
282         /**
283          * JavaScript to make the ajax refresh happen:
284          * See http://www.sergiopereira.com/articles/prototype.js.html for info on Ajax.PeriodicalUpdater
285          * It wants: 1. The div being updated
286          *           2. The URL of the update source
287          *           3. Other flags (such as the HTTP method and the refresh frequency)
288          */
289         wprintf(
290                 "<script type=\"text/javascript\">                                      "
291                 " new Ajax.PeriodicalUpdater('who_inner', 'who_inner_html',     "
292                 "                            { method: 'get', frequency: 30 }  );       "
293                 "</script>                                                              \n"
294         );
295         wDumpContent(1);
296 }
297
298 /**
299  * \brief end session \todo what??? does this belong here? 
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 }
309
310
311 /**
312  * \brief Change your session info (fake roomname and hostname)
313  */
314 void edit_me(void)
315 {
316         char buf[SIZ];
317
318         if (!IsEmptyStr(bstr("change_room_name_button"))) {
319                 serv_printf("RCHG %s", bstr("fake_roomname"));
320                 serv_getln(buf, sizeof buf);
321                 http_redirect("who");
322         } else if (!IsEmptyStr(bstr("change_host_name_button"))) {
323                 serv_printf("HCHG %s", bstr("fake_hostname"));
324                 serv_getln(buf, sizeof buf);
325                 http_redirect("who");
326         } else if (!IsEmptyStr(bstr("change_user_name_button"))) {
327                 serv_printf("UCHG %s", bstr("fake_username"));
328                 serv_getln(buf, sizeof buf);
329                 http_redirect("who");
330         } else if (!IsEmptyStr(bstr("cancel_button"))) {
331                 http_redirect("who");
332         } else {
333                 output_headers(1, 1, 0, 0, 0, 0);
334
335                 wprintf("<div id=\"banner\">\n");
336                 wprintf("<table class=\"who_banner\"><tr><td>");
337                 wprintf("<span class=\"titlebar\">");
338                 wprintf(_("Edit your session display"));
339                 wprintf("</span></td></tr></table>\n");
340                 wprintf("</div>\n<div id=\"content\">\n");
341
342                 wprintf(_("This screen allows you to change the way your "
343                         "session appears in the 'Who is online' listing. "
344                         "To turn off any 'fake' name you've previously "
345                         "set, simply click the appropriate 'change' button "
346                         "without typing anything in the corresponding box. "));
347                 wprintf("<br />\n");
348
349                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
350                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
351
352                 wprintf("<table border=0 width=100%%>\n");
353
354                 wprintf("<tr><td><b>");
355                 wprintf(_("Room name:"));
356                 wprintf("</b></td>\n<td>");
357                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
358                 wprintf("</td>\n<td align=center>");
359                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
360                         _("Change room name"));
361                 wprintf("</td>\n</tr>\n");
362
363                 wprintf("<tr><td><b>");
364                 wprintf(_("Host name:"));
365                 wprintf("</b></td><td>");
366                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
367                 wprintf("</td>\n<td align=center>");
368                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
369                         _("Change host name"));
370                 wprintf("</td>\n</tr>\n");
371
372                 if (WC->is_aide) {
373                         wprintf("<tr><td><b>");
374                         wprintf(_("User name:"));
375                         wprintf("</b></td><td>");
376                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
377                         wprintf("</td>\n<td align=center>");
378                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
379                                 _("Change user name"));
380                         wprintf("</td>\n</tr>\n");
381                 }
382                 wprintf("<tr><td> </td><td> </td><td align=center>");
383                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
384                         _("Cancel"));
385                 wprintf("</td></tr></table>\n");
386                 wprintf("</form></center>\n");
387                 wDumpContent(1);
388         }
389 }
390
391 /**
392  * \brief Wholist section
393  */
394 void wholist_section(void) {
395         UserStateStruct *User;
396         HashList *List;
397         HashPos  *it;
398         char *UserName;
399         long len;
400         char buf[SIZ];
401         time_t now;
402
403         serv_puts("TIME");
404         serv_getln(buf, sizeof buf);
405         if (buf[0] == '2') {
406                 now = extract_long(&buf[4], 0);
407         }
408         else {
409                 now = time(NULL);
410         }
411
412         List = NewHash();
413
414         if (GetWholistSection(List, now)) {
415                 SortByPayload(List, CompareUserStruct);
416                 it = GetNewHashPos();
417                 while (GetNextHashPos(List, it, &len, &UserName, (void**)&User)) {
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
439
440 /*@}*/