2edf9c64a1bb2d77897dbee4821d005f2688383e
[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)
44 {
45         StrBuf *Buf, *XBuf;
46         wcsession *WCC = WC;
47         UserStateStruct *User, *OldUser;
48         void *VOldUser;
49         size_t BufLen;
50         char buf[SIZ];
51
52         serv_puts("RWHO");
53         serv_getln(buf, sizeof buf);
54         if (buf[0] == '1') {
55                 Buf = NewStrBuf();
56                 XBuf = NewStrBuf();
57                 while (BufLen = StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
58                         if (BufLen <= 0)
59                             continue;
60                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
61                         User->Session = StrBufExtract_int(Buf, 0, '|');
62
63                         StrBufExtract_token(XBuf, Buf, 1, '|');
64                         User->UserName = NewStrBufDup(XBuf);
65
66                         StrBufExtract_token(XBuf, Buf, 2, '|');
67                         User->Room = NewStrBufDup(XBuf);
68
69                         StrBufExtract_token(XBuf, Buf, 3, '|');
70                         User->Host = NewStrBufDup(XBuf);
71
72                         StrBufExtract_token(XBuf, Buf, 9, '|');
73                         User->RealRoom = NewStrBufDup(XBuf);
74
75                         StrBufExtract_token(XBuf, Buf, 10, '|');
76                         User->RealHost = NewStrBufDup(XBuf);
77                         
78                         User->LastActive = StrBufExtract_long(Buf, 5, '|');
79                         User->Idle = (now - User->LastActive) > 900L;
80                         User->IdleSince = (now - User->LastActive) / 60;
81                         User->SessionCount = 1;
82
83                         if (GetHash(List, 
84                                     ChrPtr(User->UserName), 
85                                     StrLength(User->UserName), 
86                                     &VOldUser)) {
87                                 OldUser = VOldUser;
88                                 OldUser->SessionCount++;
89                                 if (!User->Idle) {
90                                         if (User->Session == WCC->ctdl_pid) 
91                                                 OldUser->Session = User->Session;
92
93                                         OldUser->Idle = User->Idle;
94                                         OldUser->LastActive = User->LastActive;
95                                 }
96                                 DestroyUserStruct(User);
97                         }
98                         else
99                                 Put(List, 
100                                     ChrPtr(User->UserName), 
101                                     StrLength(User->UserName), 
102                                     User, DestroyUserStruct);
103                 }
104                 SortByPayload(List, CompareUserStruct);
105
106                 FreeStrBuf(&XBuf);
107                 FreeStrBuf(&Buf);
108                 return 1;
109         }
110         else
111                 return 0;
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, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
213 {
214         HashList *List;
215         char buf[SIZ];
216         time_t now;
217
218         serv_puts("TIME");
219         serv_getln(buf, sizeof buf);
220         if (buf[0] == '2') {
221                 now = extract_long(&buf[4], 0);
222         }
223         else {
224                 now = time(NULL);
225         }
226
227         List = NewHash(1, NULL);
228         GetWholistSection(List, now);
229         return List;
230 }
231
232
233 void DeleteWholistHash(HashList **KillMe)
234 {
235         DeleteHash(KillMe);
236 }
237
238 void tmplput_who_username(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
239 {
240         UserStateStruct *User = (UserStateStruct*) vContext;
241         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->UserName, 0);
242 }
243
244 void tmplput_who_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
245 {
246         UserStateStruct *User = (UserStateStruct*) vContext;
247         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->Room, 0);
248 }
249
250 void tmplput_who_host(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
251 {
252         UserStateStruct *User = (UserStateStruct*) vContext;
253         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->Host, 0);
254 }
255
256 void tmplput_who_realroom(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
257 {
258         UserStateStruct *User = (UserStateStruct*) vContext;
259         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->RealRoom, 0);
260 }
261 int conditional_who_realroom(WCTemplateToken *Tokens, void *vContext, int ContextType)
262 {
263         UserStateStruct *User = (UserStateStruct*) vContext;
264         return StrLength(User->RealRoom) > 0;
265 }
266
267 void tmplput_who_realhost(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
268 {
269         UserStateStruct *User = (UserStateStruct*) vContext;
270         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->RealHost, 0);
271 }
272
273 void tmplput_who_lastactive(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
274 {
275         UserStateStruct *User = (UserStateStruct*) vContext;
276         StrBufAppendPrintf(Target, "%d", User->LastActive);
277 }
278
279 void tmplput_who_idlesince(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
280 {
281         UserStateStruct *User = (UserStateStruct*) vContext;
282         StrBufAppendPrintf(Target, "%d", User->IdleSince);
283 }
284
285 void tmplput_who_session(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
286 {
287         UserStateStruct *User = (UserStateStruct*) vContext;
288         StrBufAppendPrintf(Target, "%d", User->Session);
289 }
290
291 int conditional_who_idle(WCTemplateToken *Tokens, void *vContext, int ContextType)
292 {
293         UserStateStruct *User = (UserStateStruct*) vContext;
294         return User->Idle;
295 }
296
297 int conditional_who_nsessions(WCTemplateToken *Tokens, void *vContext, int ContextType)
298 {
299         UserStateStruct *User = (UserStateStruct*) vContext;
300         return User->SessionCount;
301 }
302
303 void tmplput_who_nsessions(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
304 {
305         UserStateStruct *User = (UserStateStruct*) vContext;
306         StrBufAppendPrintf(Target, "%d", User->SessionCount);
307 }
308
309 int conditional_who_isme(WCTemplateToken *Tokens, void *vContext, int ContextType)
310 {
311         UserStateStruct *User = (UserStateStruct*) vContext;
312         return (User->Session == WC->ctdl_pid);
313 }
314
315 void 
316 InitModule_WHO
317 (void)
318 {
319         WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
320         WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
321
322         RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
323
324         RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, CTX_WHO);
325         RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, CTX_WHO);
326         RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, CTX_WHO);
327         RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, CTX_WHO);
328         RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, CTX_WHO);
329         RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, CTX_WHO);
330         RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, CTX_WHO);
331         RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, CTX_WHO);
332         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
333         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
334
335         RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
336         RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
337         RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
338         RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
339 }