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