* do linebuffered reading from server
[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;
46         wcsession *WCC = WC;
47         UserStateStruct *User, *OldUser;
48         void *VOldUser;
49         size_t BufLen;
50         char buf[SIZ];
51         const char *Pos;
52
53         serv_puts("RWHO");
54         serv_getln(buf, sizeof buf);
55         if (buf[0] == '1') {
56                 Buf = NewStrBuf();
57                 while (BufLen = StrBuf_ServGetlnBuffered(Buf), strcmp(ChrPtr(Buf), "000")) {
58                         if (BufLen <= 0)
59                             continue;
60                         Pos = NULL;
61                         User = (UserStateStruct*) malloc(sizeof(UserStateStruct));
62                         User->Session = StrBufExtractNext_int(Buf, &Pos, '|');
63
64                         User->UserName = NewStrBufPlain(NULL, BufLen);
65                         StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
66                         
67                         User->Room = NewStrBufPlain(NULL, BufLen);
68                         StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
69
70                         User->Host = NewStrBufPlain(NULL, BufLen);
71                         StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
72
73                         StrBufSkip_NTokenS(Buf, &Pos, '|', 1);
74
75                         User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
76                         StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
77
78                         User->RealRoom = NewStrBufPlain(NULL, BufLen);
79                         StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
80
81                         User->RealHost = NewStrBufPlain(NULL, BufLen);
82                         StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
83                         
84                         User->Idle = (now - User->LastActive) > 900L;
85                         User->IdleSince = (now - User->LastActive) / 60;
86                         User->SessionCount = 1;
87
88                         if (GetHash(List, 
89                                     ChrPtr(User->UserName), 
90                                     StrLength(User->UserName), 
91                                     &VOldUser)) {
92                                 OldUser = VOldUser;
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, 
105                                     ChrPtr(User->UserName), 
106                                     StrLength(User->UserName), 
107                                     User, DestroyUserStruct);
108                 }
109                 SortByPayload(List, CompareUserStruct);
110
111                 FreeStrBuf(&Buf);
112                 return 1;
113         }
114         else
115                 return 0;
116 }
117
118 /*
119  * end session
120  */
121 void terminate_session(void)
122 {
123         char buf[SIZ];
124
125         serv_printf("TERM %s", bstr("which_session"));
126         serv_getln(buf, sizeof buf);
127         url_do_template();
128 }
129
130
131 /*
132  * Change your session info (fake roomname and hostname)
133  */
134 void edit_me(void)
135 {
136         char buf[SIZ];
137
138         if (havebstr("change_room_name_button")) {
139                 serv_printf("RCHG %s", bstr("fake_roomname"));
140                 serv_getln(buf, sizeof buf);
141                 http_redirect("who");
142         } else if (havebstr("change_host_name_button")) {
143                 serv_printf("HCHG %s", bstr("fake_hostname"));
144                 serv_getln(buf, sizeof buf);
145                 http_redirect("who");
146         } else if (havebstr("change_user_name_button")) {
147                 serv_printf("UCHG %s", bstr("fake_username"));
148                 serv_getln(buf, sizeof buf);
149                 http_redirect("who");
150         } else if (havebstr("cancel_button")) {
151                 http_redirect("who");
152         } else {
153                 output_headers(1, 1, 0, 0, 0, 0);
154
155                 wprintf("<div id=\"banner\">\n");
156                 wprintf("<table class=\"who_banner\"><tr><td>");
157                 wprintf("<span class=\"titlebar\">");
158                 wprintf(_("Edit your session display"));
159                 wprintf("</span></td></tr></table>\n");
160                 wprintf("</div>\n<div id=\"content\">\n");
161
162                 wprintf(_("This screen allows you to change the way your "
163                         "session appears in the 'Who is online' listing. "
164                         "To turn off any 'fake' name you've previously "
165                         "set, simply click the appropriate 'change' button "
166                         "without typing anything in the corresponding box. "));
167                 wprintf("<br />\n");
168
169                 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
170                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
171
172                 wprintf("<table border=0 width=100%%>\n");
173
174                 wprintf("<tr><td><b>");
175                 wprintf(_("Room name:"));
176                 wprintf("</b></td>\n<td>");
177                 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
178                 wprintf("</td>\n<td align=center>");
179                 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
180                         _("Change room name"));
181                 wprintf("</td>\n</tr>\n");
182
183                 wprintf("<tr><td><b>");
184                 wprintf(_("Host name:"));
185                 wprintf("</b></td><td>");
186                 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
187                 wprintf("</td>\n<td align=center>");
188                 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
189                         _("Change host name"));
190                 wprintf("</td>\n</tr>\n");
191
192                 if (WC->is_aide) {
193                         wprintf("<tr><td><b>");
194                         wprintf(_("User name:"));
195                         wprintf("</b></td><td>");
196                         wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
197                         wprintf("</td>\n<td align=center>");
198                         wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
199                                 _("Change user name"));
200                         wprintf("</td>\n</tr>\n");
201                 }
202                 wprintf("<tr><td> </td><td> </td><td align=center>");
203                 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
204                         _("Cancel"));
205                 wprintf("</td></tr></table>\n");
206                 wprintf("</form></center>\n");
207                 wDumpContent(1);
208         }
209 }
210
211 void _terminate_session(void) {
212         slrp_highest();
213         terminate_session();
214 }
215
216 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
217
218 {
219         HashList *List;
220         char buf[SIZ];
221         time_t now;
222
223         serv_puts("TIME");
224         serv_getln(buf, sizeof buf);
225         if (buf[0] == '2') {
226                 now = extract_long(&buf[4], 0);
227         }
228         else {
229                 now = time(NULL);
230         }
231
232         List = NewHash(1, NULL);
233         GetWholistSection(List, now);
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 }