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