9f0e3d3d63c76ccef3aaa61a7147aca17978d513
[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 IdleSince;
17         int SessionCount;
18 } UserStateStruct;
19
20 void DestroyUserStruct(void *vUser)
21 {
22         UserStateStruct *User = (UserStateStruct*) vUser;
23         FreeStrBuf(&User->UserName);
24         FreeStrBuf(&User->Room);
25         FreeStrBuf(&User->Host);
26         FreeStrBuf(&User->RealRoom);
27         FreeStrBuf(&User->RealHost);
28         free(User);
29 }
30
31 int CompareUserStruct(const void *VUser1, const void *VUser2)
32 {
33         const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
34         const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
35         
36         if (User1->Idle != User2->Idle)
37                 return User1->Idle > User2->Idle;
38         return strcasecmp(ChrPtr(User1->UserName), 
39                           ChrPtr(User2->UserName));
40 }
41
42
43 int GetWholistSection(HashList *List, time_t now, StrBuf *Buf)
44 {
45         wcsession *WCC = WC;
46         UserStateStruct *User, *OldUser;
47         void *VOldUser;
48         size_t BufLen;
49         const char *Pos;
50
51         serv_puts("RWHO");
52         StrBuf_ServGetlnBuffered(Buf);
53         if (GetServerStatus(Buf, NULL) == 1) {
54                 while (BufLen = StrBuf_ServGetlnBuffered(Buf), strcmp(ChrPtr(Buf), "000")) {
55                         if (BufLen <= 0)
56                             continue;
57                         Pos = NULL;
58                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
59                         User->Session = StrBufExtractNext_int(Buf, &Pos, '|');
60
61                         User->UserName = NewStrBufPlain(NULL, BufLen);
62                         StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
63                         
64                         User->Room = NewStrBufPlain(NULL, BufLen);
65                         StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
66
67                         User->Host = NewStrBufPlain(NULL, BufLen);
68                         StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
69
70                         StrBufSkip_NTokenS(Buf, &Pos, '|', 1);
71
72                         User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
73                         StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
74
75                         User->RealRoom = NewStrBufPlain(NULL, BufLen);
76                         StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
77
78                         User->RealHost = NewStrBufPlain(NULL, BufLen);
79                         StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
80                         
81                         User->Idle = (now - User->LastActive) > 900L;
82                         User->IdleSince = (now - User->LastActive) / 60;
83                         User->SessionCount = 1;
84
85                         if (GetHash(List, 
86                                     ChrPtr(User->UserName), 
87                                     StrLength(User->UserName), 
88                                     &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, 
102                                     ChrPtr(User->UserName), 
103                                     StrLength(User->UserName), 
104                                     User, DestroyUserStruct);
105                 }
106                 SortByPayload(List, CompareUserStruct);
107                 return 1;
108         }
109         else {
110                 return 0;
111         }
112 }
113
114 /*
115  * end session
116  */
117 void terminate_session(void)
118 {
119         char buf[SIZ];
120
121         serv_printf("TERM %s", bstr("which_session"));
122         serv_getln(buf, sizeof buf);
123         url_do_template();
124 }
125
126
127 /*
128  * Change your session info (fake roomname and hostname)
129  */
130 void edit_me(void)
131 {
132         char buf[SIZ];
133
134         if (havebstr("change_room_name_button")) {
135                 serv_printf("RCHG %s", bstr("fake_roomname"));
136                 serv_getln(buf, sizeof buf);
137                 http_redirect("who");
138         } else if (havebstr("change_host_name_button")) {
139                 serv_printf("HCHG %s", bstr("fake_hostname"));
140                 serv_getln(buf, sizeof buf);
141                 http_redirect("who");
142         } else if (havebstr("change_user_name_button")) {
143                 serv_printf("UCHG %s", bstr("fake_username"));
144                 serv_getln(buf, sizeof buf);
145                 http_redirect("who");
146         } else if (havebstr("cancel_button")) {
147                 http_redirect("who");
148         } else {
149                 output_headers(1, 1, 0, 0, 0, 0);
150
151                 wprintf("<div id=\"banner\">\n");
152                 wprintf("<table class=\"who_banner\"><tr><td>");
153                 wprintf("<span class=\"titlebar\">");
154                 wprintf(_("Edit your session display"));
155                 wprintf("</span></td></tr></table>\n");
156                 wprintf("</div>\n<div id=\"content\">\n");
157
158                 wprintf(_("This screen allows you to change the way your "
159                         "session appears in the 'Who is online' listing. "
160                         "To turn off any 'fake' name you've previously "
161                         "set, simply click the appropriate 'change' button "
162                         "without typing anything in the corresponding box. "));
163                 wprintf("<br />\n");
164
165                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
166                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
167
168                 wprintf("<table border=0 width=100%%>\n");
169
170                 wprintf("<tr><td><b>");
171                 wprintf(_("Room name:"));
172                 wprintf("</b></td>\n<td>");
173                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
174                 wprintf("</td>\n<td align=center>");
175                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
176                         _("Change room name"));
177                 wprintf("</td>\n</tr>\n");
178
179                 wprintf("<tr><td><b>");
180                 wprintf(_("Host name:"));
181                 wprintf("</b></td><td>");
182                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
183                 wprintf("</td>\n<td align=center>");
184                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
185                         _("Change host name"));
186                 wprintf("</td>\n</tr>\n");
187
188                 if (WC->is_aide) {
189                         wprintf("<tr><td><b>");
190                         wprintf(_("User name:"));
191                         wprintf("</b></td><td>");
192                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
193                         wprintf("</td>\n<td align=center>");
194                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
195                                 _("Change user name"));
196                         wprintf("</td>\n</tr>\n");
197                 }
198                 wprintf("<tr><td> </td><td> </td><td align=center>");
199                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
200                         _("Cancel"));
201                 wprintf("</td></tr></table>\n");
202                 wprintf("</form></center>\n");
203                 wDumpContent(1);
204         }
205 }
206
207 void _terminate_session(void) {
208         slrp_highest();
209         terminate_session();
210 }
211
212 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
213
214 {
215         StrBuf *Buf;
216         HashList *List;
217         time_t now;
218
219         Buf = NewStrBuf();
220
221         serv_puts("TIME");
222         StrBuf_ServGetlnBuffered(Buf);
223         if (GetServerStatus(Buf, NULL)  == 2) {
224                 const char *pos = ChrPtr(Buf) + 4;
225                 now = StrBufExtractNext_long(Buf, &pos, '|');
226         }
227         else {
228                 now = time(NULL);
229         }
230
231         List = NewHash(1, NULL);
232         GetWholistSection(List, now, Buf);
233         FreeStrBuf(&Buf);
234         return List;
235 }
236
237
238 void DeleteWholistHash(HashList **KillMe)
239 {
240         DeleteHash(KillMe);
241 }
242
243 void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
244 {
245         UserStateStruct *User = (UserStateStruct*) CTX;
246         StrBufAppendTemplate(Target, TP, User->UserName, 0);
247 }
248
249 void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
250 {
251         UserStateStruct *User = (UserStateStruct*) CTX;
252         StrBufAppendTemplate(Target, TP, User->Room, 0);
253 }
254
255 void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
256 {
257         UserStateStruct *User = (UserStateStruct*) CTX;
258         StrBufAppendTemplate(Target, TP, User->Host, 0);
259 }
260
261 void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
262 {
263         UserStateStruct *User = (UserStateStruct*) CTX;
264         StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
265 }
266 int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
267 {
268         UserStateStruct *User = (UserStateStruct*) CTX;
269         return StrLength(User->RealRoom) > 0;
270 }
271
272 void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
273 {
274         UserStateStruct *User = (UserStateStruct*) CTX;
275         StrBufAppendTemplate(Target, TP, User->RealHost, 0);
276 }
277
278 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
279 {
280         UserStateStruct *User = (UserStateStruct*) CTX;
281         StrBufAppendPrintf(Target, "%d", User->LastActive);
282 }
283
284 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
285 {
286         UserStateStruct *User = (UserStateStruct*) CTX;
287         StrBufAppendPrintf(Target, "%d", User->IdleSince);
288 }
289
290 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
291 {
292         UserStateStruct *User = (UserStateStruct*) CTX;
293         StrBufAppendPrintf(Target, "%d", User->Session);
294 }
295
296 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
297 {
298         UserStateStruct *User = (UserStateStruct*) CTX;
299         return User->Idle;
300 }
301
302 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
303 {
304         UserStateStruct *User = (UserStateStruct*) CTX;
305         return User->SessionCount;
306 }
307
308 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
309 {
310         UserStateStruct *User = (UserStateStruct*) CTX;
311         StrBufAppendPrintf(Target, "%d", User->SessionCount);
312 }
313
314 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
315 {
316         UserStateStruct *User = (UserStateStruct*) CTX;
317         return (User->Session == WC->ctdl_pid);
318 }
319
320 void 
321 InitModule_WHO
322 (void)
323 {
324         WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
325         WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
326
327         RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
328
329         RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, CTX_WHO);
330         RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, CTX_WHO);
331         RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, CTX_WHO);
332         RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, CTX_WHO);
333         RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, CTX_WHO);
334         RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, CTX_WHO);
335         RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, CTX_WHO);
336         RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, CTX_WHO);
337         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
338         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
339
340         RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
341         RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
342         RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
343         RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
344 }