* typedef wcsession, so we don't always need to say gcc again its a struct.
[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         ///who();
124         url_do_template();
125 }
126
127
128 /*
129  * Change your session info (fake roomname and hostname)
130  */
131 void edit_me(void)
132 {
133         char buf[SIZ];
134
135         if (havebstr("change_room_name_button")) {
136                 serv_printf("RCHG %s", bstr("fake_roomname"));
137                 serv_getln(buf, sizeof buf);
138                 http_redirect("who");
139         } else if (havebstr("change_host_name_button")) {
140                 serv_printf("HCHG %s", bstr("fake_hostname"));
141                 serv_getln(buf, sizeof buf);
142                 http_redirect("who");
143         } else if (havebstr("change_user_name_button")) {
144                 serv_printf("UCHG %s", bstr("fake_username"));
145                 serv_getln(buf, sizeof buf);
146                 http_redirect("who");
147         } else if (havebstr("cancel_button")) {
148                 http_redirect("who");
149         } else {
150                 output_headers(1, 1, 0, 0, 0, 0);
151
152                 wprintf("<div id=\"banner\">\n");
153                 wprintf("<table class=\"who_banner\"><tr><td>");
154                 wprintf("<span class=\"titlebar\">");
155                 wprintf(_("Edit your session display"));
156                 wprintf("</span></td></tr></table>\n");
157                 wprintf("</div>\n<div id=\"content\">\n");
158
159                 wprintf(_("This screen allows you to change the way your "
160                         "session appears in the 'Who is online' listing. "
161                         "To turn off any 'fake' name you've previously "
162                         "set, simply click the appropriate 'change' button "
163                         "without typing anything in the corresponding box. "));
164                 wprintf("<br />\n");
165
166                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
167                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
168
169                 wprintf("<table border=0 width=100%%>\n");
170
171                 wprintf("<tr><td><b>");
172                 wprintf(_("Room name:"));
173                 wprintf("</b></td>\n<td>");
174                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
175                 wprintf("</td>\n<td align=center>");
176                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
177                         _("Change room name"));
178                 wprintf("</td>\n</tr>\n");
179
180                 wprintf("<tr><td><b>");
181                 wprintf(_("Host name:"));
182                 wprintf("</b></td><td>");
183                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
184                 wprintf("</td>\n<td align=center>");
185                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
186                         _("Change host name"));
187                 wprintf("</td>\n</tr>\n");
188
189                 if (WC->is_aide) {
190                         wprintf("<tr><td><b>");
191                         wprintf(_("User name:"));
192                         wprintf("</b></td><td>");
193                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
194                         wprintf("</td>\n<td align=center>");
195                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
196                                 _("Change user name"));
197                         wprintf("</td>\n</tr>\n");
198                 }
199                 wprintf("<tr><td> </td><td> </td><td align=center>");
200                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
201                         _("Cancel"));
202                 wprintf("</td></tr></table>\n");
203                 wprintf("</form></center>\n");
204                 wDumpContent(1);
205         }
206 }
207
208 void _terminate_session(void) {
209         slrp_highest();
210         terminate_session();
211 }
212
213 HashList *GetWholistHash(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
214 {
215         HashList *List;
216         char buf[SIZ];
217         time_t now;
218
219         serv_puts("TIME");
220         serv_getln(buf, sizeof buf);
221         if (buf[0] == '2') {
222                 now = extract_long(&buf[4], 0);
223         }
224         else {
225                 now = time(NULL);
226         }
227
228         List = NewHash(1, NULL);
229         GetWholistSection(List, now);
230         return List;
231 }
232
233
234 void DeleteWholistHash(HashList **KillMe)
235 {
236         DeleteHash(KillMe);
237 }
238
239 void tmplput_who_username(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
240 {
241         UserStateStruct *User = (UserStateStruct*) vContext;
242         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->UserName, 0);
243 }
244
245 void tmplput_who_room(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
246 {
247         UserStateStruct *User = (UserStateStruct*) vContext;
248         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->Room, 0);
249 }
250
251 void tmplput_who_host(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
252 {
253         UserStateStruct *User = (UserStateStruct*) vContext;
254         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->Host, 0);
255 }
256
257 void tmplput_who_realroom(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
258 {
259         UserStateStruct *User = (UserStateStruct*) vContext;
260         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->RealRoom, 0);
261 }
262 int conditional_who_realroom(WCTemplateToken *Tokens, void *vContext, int ContextType)
263 {
264         UserStateStruct *User = (UserStateStruct*) vContext;
265         return StrLength(User->RealRoom) > 0;
266 }
267
268 void tmplput_who_realhost(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
269 {
270         UserStateStruct *User = (UserStateStruct*) vContext;
271         StrBufAppendTemplate(Target, nArgs, Tokens, vContext, ContextType, User->RealHost, 0);
272 }
273
274 void tmplput_who_lastactive(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
275 {
276         UserStateStruct *User = (UserStateStruct*) vContext;
277         StrBufAppendPrintf(Target, "%d", User->LastActive);
278 }
279
280 void tmplput_who_idlesince(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
281 {
282         UserStateStruct *User = (UserStateStruct*) vContext;
283         StrBufAppendPrintf(Target, "%d", User->IdleSince);
284 }
285
286 void tmplput_who_session(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
287 {
288         UserStateStruct *User = (UserStateStruct*) vContext;
289         StrBufAppendPrintf(Target, "%d", User->Session);
290 }
291
292 int conditional_who_idle(WCTemplateToken *Tokens, void *vContext, int ContextType)
293 {
294         UserStateStruct *User = (UserStateStruct*) vContext;
295         return User->Idle;
296 }
297
298 int conditional_who_nsessions(WCTemplateToken *Tokens, void *vContext, int ContextType)
299 {
300         UserStateStruct *User = (UserStateStruct*) vContext;
301         return User->SessionCount;
302 }
303
304 void tmplput_who_nsessions(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *vContext, int ContextType)
305 {
306         UserStateStruct *User = (UserStateStruct*) vContext;
307         StrBufAppendPrintf(Target, "%d", User->SessionCount);
308 }
309
310 int conditional_who_isme(WCTemplateToken *Tokens, void *vContext, int ContextType)
311 {
312         UserStateStruct *User = (UserStateStruct*) vContext;
313         return (User->Session == WC->ctdl_pid);
314 }
315
316 void 
317 InitModule_WHO
318 (void)
319 {
320         WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
321         WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
322
323         RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE);
324
325         RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, CTX_WHO);
326         RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, CTX_WHO);
327         RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, CTX_WHO);
328         RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, CTX_WHO);
329         RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, CTX_WHO);
330         RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, CTX_WHO);
331         RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, CTX_WHO);
332         RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, CTX_WHO);
333         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
334         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, CTX_WHO);
335
336         RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
337         RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
338         RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
339         RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
340 }