removed a bunch of blank comment lines
[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), 
52                        ((BufLen >= 0) && 
53                         ((BufLen != 3) || strcmp(ChrPtr(Buf), "000"))))
54                 {
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                 return 1;
108         }
109         else {
110                 return 0;
111         }
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                 wc_printf("<div id=\"banner\">\n");
152                 wc_printf("<table class=\"who_banner\"><tr><td>");
153                 wc_printf("<span class=\"titlebar\">");
154                 wc_printf(_("Edit your session display"));
155                 wc_printf("</span></td></tr></table>\n");
156                 wc_printf("</div>\n<div id=\"content\">\n");
157
158                 wc_printf(_("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                 wc_printf("<br>\n");
164
165                 wc_printf("<form method=\"POST\" action=\"edit_me\">\n");
166                 wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
167
168                 wc_printf("<table border=0 width=100%%>\n");
169
170                 wc_printf("<tr><td><b>");
171                 wc_printf(_("Room name:"));
172                 wc_printf("</b></td>\n<td>");
173                 wc_printf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
174                 wc_printf("</td>\n<td align=center>");
175                 wc_printf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
176                         _("Change room name"));
177                 wc_printf("</td>\n</tr>\n");
178
179                 wc_printf("<tr><td><b>");
180                 wc_printf(_("Host name:"));
181                 wc_printf("</b></td><td>");
182                 wc_printf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
183                 wc_printf("</td>\n<td align=center>");
184                 wc_printf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
185                         _("Change host name"));
186                 wc_printf("</td>\n</tr>\n");
187
188                 if (WC->is_aide) {
189                         wc_printf("<tr><td><b>");
190                         wc_printf(_("User name:"));
191                         wc_printf("</b></td><td>");
192                         wc_printf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
193                         wc_printf("</td>\n<td align=center>");
194                         wc_printf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
195                                 _("Change user name"));
196                         wc_printf("</td>\n</tr>\n");
197                 }
198                 wc_printf("<tr><td> </td><td> </td><td align=center>");
199                 wc_printf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
200                         _("Cancel"));
201                 wc_printf("</td></tr></table>\n");
202                 wc_printf("</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, WCTemplputParams *TP)
213
214 {
215         StrBuf *Buf;
216         HashList *List;
217         time_t now;
218
219         Buf = NewStrBuf();
220
221         serv_puts("TIME");
222         StrBuf_ServGetln(Buf);
223         if (GetServerStatus(Buf, NULL)  == 2) {
224                 const char *pos = ChrPtr(Buf) + 4;
225                 now = StrBufExtractNext_long(Buf, &pos, '|');
226         }
227         else {
228                 now = time(NULL);
229         }
230
231         List = NewHash(1, NULL);
232         GetWholistSection(List, now, Buf);
233         FreeStrBuf(&Buf);
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 int conditional_who_realhost(StrBuf *Target, WCTemplputParams *TP)
278 {
279         UserStateStruct *User = (UserStateStruct*) CTX;
280         return StrLength(User->RealHost) > 0;
281 }
282
283 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
284 {
285         UserStateStruct *User = (UserStateStruct*) CTX;
286         StrBufAppendPrintf(Target, "%d", User->LastActive);
287 }
288
289 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
290 {
291         UserStateStruct *User = (UserStateStruct*) CTX;
292         StrBufAppendPrintf(Target, "%d", User->IdleSince);
293 }
294
295 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
296 {
297         UserStateStruct *User = (UserStateStruct*) CTX;
298         StrBufAppendPrintf(Target, "%d", User->Session);
299 }
300
301 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
302 {
303         UserStateStruct *User = (UserStateStruct*) CTX;
304         return User->Idle;
305 }
306
307 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
308 {
309         UserStateStruct *User = (UserStateStruct*) CTX;
310         return User->SessionCount;
311 }
312
313 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
314 {
315         UserStateStruct *User = (UserStateStruct*) CTX;
316         StrBufAppendPrintf(Target, "%d", User->SessionCount);
317 }
318
319 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
320 {
321         UserStateStruct *User = (UserStateStruct*) CTX;
322         return (User->Session == WC->ctdl_pid);
323 }
324
325 void 
326 InitModule_WHO
327 (void)
328 {
329         
330
331         WebcitAddUrlHandler(HKEY("terminate_session"), "", 0, _terminate_session, 0);
332         WebcitAddUrlHandler(HKEY("edit_me"), "", 0, edit_me, 0);
333
334         RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
335
336         RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, NULL, CTX_WHO);
337         RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, NULL, CTX_WHO);
338         RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, NULL, CTX_WHO);
339         RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, NULL, CTX_WHO);
340         RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, NULL, CTX_WHO);
341         RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, NULL, CTX_WHO);
342         RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, NULL, CTX_WHO);
343         RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, NULL, CTX_WHO);
344         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
345         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
346
347         RegisterConditional(HKEY("WHO:IDLE"),      1, conditional_who_idle, CTX_WHO);
348         RegisterConditional(HKEY("WHO:NSESSIONS"), 1, conditional_who_nsessions, CTX_WHO);
349         RegisterConditional(HKEY("WHO:ISME"),      1, conditional_who_isme, CTX_WHO);
350         RegisterConditional(HKEY("WHO:REALROOM"),  1, conditional_who_realroom, CTX_WHO);
351         RegisterConditional(HKEY("WHO:REALHOST"),  1, conditional_who_realhost, CTX_WHO);
352 }