* When saving a Task, if the status is COMPLETED then also set PERCENT-COMPLETE:100...
[citadel.git] / webcit / who.c
blob9e1e3c90e797f39573186947d651f83e3ca7109a
1 /*
2 * $Id$
3 */
5 #include "webcit.h"
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;
20 void DestroyUserStruct(void *vUser)
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);
31 int CompareUserStruct(const void *VUser1, const void *VUser2)
33 const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
34 const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
36 if (User1->Idle != User2->Idle)
37 return User1->Idle > User2->Idle;
38 return strcasecmp(ChrPtr(User1->UserName),
39 ChrPtr(User2->UserName));
43 int GetWholistSection(HashList *List, time_t now)
45 StrBuf *Buf, *XBuf;
46 wcsession *WCC = WC;
47 UserStateStruct *User, *OldUser;
48 void *VOldUser;
49 size_t BufLen;
50 char buf[SIZ];
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, '|');
63 StrBufExtract_token(XBuf, Buf, 1, '|');
64 User->UserName = NewStrBufDup(XBuf);
66 StrBufExtract_token(XBuf, Buf, 2, '|');
67 User->Room = NewStrBufDup(XBuf);
69 StrBufExtract_token(XBuf, Buf, 3, '|');
70 User->Host = NewStrBufDup(XBuf);
72 StrBufExtract_token(XBuf, Buf, 9, '|');
73 User->RealRoom = NewStrBufDup(XBuf);
75 StrBufExtract_token(XBuf, Buf, 10, '|');
76 User->RealHost = NewStrBufDup(XBuf);
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;
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;
93 OldUser->Idle = User->Idle;
94 OldUser->LastActive = User->LastActive;
96 DestroyUserStruct(User);
98 else
99 Put(List,
100 ChrPtr(User->UserName),
101 StrLength(User->UserName),
102 User, DestroyUserStruct);
104 SortByPayload(List, CompareUserStruct);
106 FreeStrBuf(&XBuf);
107 FreeStrBuf(&Buf);
108 return 1;
110 else
111 return 0;
115 * end session
117 void terminate_session(void)
119 char buf[SIZ];
121 serv_printf("TERM %s", bstr("which_session"));
122 serv_getln(buf, sizeof buf);
123 url_do_template();
128 * Change your session info (fake roomname and hostname)
130 void edit_me(void)
132 char buf[SIZ];
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);
151 wprintf("<div id=\"banner\">\n");
152 wprintf("<table class=\"who_banner\"><tr><td>");
153 wprintf("<span class=\"titlebar\">");
154 wprintf(_("Edit your session display"));
155 wprintf("</span></td></tr></table>\n");
156 wprintf("</div>\n<div id=\"content\">\n");
158 wprintf(_("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 wprintf("<br />\n");
165 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
166 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
168 wprintf("<table border=0 width=100%%>\n");
170 wprintf("<tr><td><b>");
171 wprintf(_("Room name:"));
172 wprintf("</b></td>\n<td>");
173 wprintf("<input type=\"text\" name=\"fake_roomname\" maxlength=\"64\">\n");
174 wprintf("</td>\n<td align=center>");
175 wprintf("<input type=\"submit\" name=\"change_room_name_button\" value=\"%s\">",
176 _("Change room name"));
177 wprintf("</td>\n</tr>\n");
179 wprintf("<tr><td><b>");
180 wprintf(_("Host name:"));
181 wprintf("</b></td><td>");
182 wprintf("<input type=\"text\" name=\"fake_hostname\" maxlength=\"64\">\n");
183 wprintf("</td>\n<td align=center>");
184 wprintf("<input type=\"submit\" name=\"change_host_name_button\" value=\"%s\">",
185 _("Change host name"));
186 wprintf("</td>\n</tr>\n");
188 if (WC->is_aide) {
189 wprintf("<tr><td><b>");
190 wprintf(_("User name:"));
191 wprintf("</b></td><td>");
192 wprintf("<input type=\"text\" name=\"fake_username\" maxlength=\"64\">\n");
193 wprintf("</td>\n<td align=center>");
194 wprintf("<input type=\"submit\" name \"change_user_name_button\" value=\"%s\">",
195 _("Change user name"));
196 wprintf("</td>\n</tr>\n");
198 wprintf("<tr><td> </td><td> </td><td align=center>");
199 wprintf("<input type=\"submit\" name=\"cancel_button\" value=\"%s\">",
200 _("Cancel"));
201 wprintf("</td></tr></table>\n");
202 wprintf("</form></center>\n");
203 wDumpContent(1);
207 void _terminate_session(void) {
208 slrp_highest();
209 terminate_session();
212 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
215 HashList *List;
216 char buf[SIZ];
217 time_t now;
219 serv_puts("TIME");
220 serv_getln(buf, sizeof buf);
221 if (buf[0] == '2') {
222 now = extract_long(&buf[4], 0);
224 else {
225 now = time(NULL);
228 List = NewHash(1, NULL);
229 GetWholistSection(List, now);
230 return List;
234 void DeleteWholistHash(HashList **KillMe)
236 DeleteHash(KillMe);
239 void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
241 UserStateStruct *User = (UserStateStruct*) CTX;
242 StrBufAppendTemplate(Target, TP, User->UserName, 0);
245 void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
247 UserStateStruct *User = (UserStateStruct*) CTX;
248 StrBufAppendTemplate(Target, TP, User->Room, 0);
251 void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
253 UserStateStruct *User = (UserStateStruct*) CTX;
254 StrBufAppendTemplate(Target, TP, User->Host, 0);
257 void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
259 UserStateStruct *User = (UserStateStruct*) CTX;
260 StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
262 int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
264 UserStateStruct *User = (UserStateStruct*) CTX;
265 return StrLength(User->RealRoom) > 0;
268 void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
270 UserStateStruct *User = (UserStateStruct*) CTX;
271 StrBufAppendTemplate(Target, TP, User->RealHost, 0);
274 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
276 UserStateStruct *User = (UserStateStruct*) CTX;
277 StrBufAppendPrintf(Target, "%d", User->LastActive);
280 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
282 UserStateStruct *User = (UserStateStruct*) CTX;
283 StrBufAppendPrintf(Target, "%d", User->IdleSince);
286 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
288 UserStateStruct *User = (UserStateStruct*) CTX;
289 StrBufAppendPrintf(Target, "%d", User->Session);
292 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
294 UserStateStruct *User = (UserStateStruct*) CTX;
295 return User->Idle;
298 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
300 UserStateStruct *User = (UserStateStruct*) CTX;
301 return User->SessionCount;
304 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
306 UserStateStruct *User = (UserStateStruct*) CTX;
307 StrBufAppendPrintf(Target, "%d", User->SessionCount);
310 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
312 UserStateStruct *User = (UserStateStruct*) CTX;
313 return (User->Session == WC->ctdl_pid);
316 void
317 InitModule_WHO
318 (void)
320 WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
321 WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
323 RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
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);
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);