* Attempting to add a smart host in webcit was instead adding it as an RBL host....
[citadel.git] / webcit / who.c
blob320ef01538701f61f988376210b9bc24c089bc45
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;
46 wcsession *WCC = WC;
47 UserStateStruct *User, *OldUser;
48 void *VOldUser;
49 size_t BufLen;
50 char buf[SIZ];
51 const char *Pos;
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, '|');
64 User->UserName = NewStrBufPlain(NULL, BufLen);
65 StrBufExtract_NextToken(User->UserName, Buf, &Pos, '|');
67 User->Room = NewStrBufPlain(NULL, BufLen);
68 StrBufExtract_NextToken(User->Room, Buf, &Pos, '|');
70 User->Host = NewStrBufPlain(NULL, BufLen);
71 StrBufExtract_NextToken(User->Host, Buf, &Pos, '|');
73 StrBufSkip_NTokenS(Buf, &Pos, '|', 1);
75 User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
76 StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
78 User->RealRoom = NewStrBufPlain(NULL, BufLen);
79 StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
81 User->RealHost = NewStrBufPlain(NULL, BufLen);
82 StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
84 User->Idle = (now - User->LastActive) > 900L;
85 User->IdleSince = (now - User->LastActive) / 60;
86 User->SessionCount = 1;
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;
98 OldUser->Idle = User->Idle;
99 OldUser->LastActive = User->LastActive;
101 DestroyUserStruct(User);
103 else
104 Put(List,
105 ChrPtr(User->UserName),
106 StrLength(User->UserName),
107 User, DestroyUserStruct);
109 SortByPayload(List, CompareUserStruct);
111 FreeStrBuf(&Buf);
112 return 1;
114 else
115 return 0;
119 * end session
121 void terminate_session(void)
123 char buf[SIZ];
125 serv_printf("TERM %s", bstr("which_session"));
126 serv_getln(buf, sizeof buf);
127 url_do_template();
132 * Change your session info (fake roomname and hostname)
134 void edit_me(void)
136 char buf[SIZ];
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);
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");
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");
169 wprintf("<form method=\"POST\" action=\"edit_me\">\n");
170 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
172 wprintf("<table border=0 width=100%%>\n");
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");
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");
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");
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);
211 void _terminate_session(void) {
212 slrp_highest();
213 terminate_session();
216 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
219 HashList *List;
220 char buf[SIZ];
221 time_t now;
223 serv_puts("TIME");
224 serv_getln(buf, sizeof buf);
225 if (buf[0] == '2') {
226 now = extract_long(&buf[4], 0);
228 else {
229 now = time(NULL);
232 List = NewHash(1, NULL);
233 GetWholistSection(List, now);
234 return List;
238 void DeleteWholistHash(HashList **KillMe)
240 DeleteHash(KillMe);
243 void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
245 UserStateStruct *User = (UserStateStruct*) CTX;
246 StrBufAppendTemplate(Target, TP, User->UserName, 0);
249 void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
251 UserStateStruct *User = (UserStateStruct*) CTX;
252 StrBufAppendTemplate(Target, TP, User->Room, 0);
255 void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
257 UserStateStruct *User = (UserStateStruct*) CTX;
258 StrBufAppendTemplate(Target, TP, User->Host, 0);
261 void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
263 UserStateStruct *User = (UserStateStruct*) CTX;
264 StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
266 int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
268 UserStateStruct *User = (UserStateStruct*) CTX;
269 return StrLength(User->RealRoom) > 0;
272 void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
274 UserStateStruct *User = (UserStateStruct*) CTX;
275 StrBufAppendTemplate(Target, TP, User->RealHost, 0);
278 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
280 UserStateStruct *User = (UserStateStruct*) CTX;
281 StrBufAppendPrintf(Target, "%d", User->LastActive);
284 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
286 UserStateStruct *User = (UserStateStruct*) CTX;
287 StrBufAppendPrintf(Target, "%d", User->IdleSince);
290 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
292 UserStateStruct *User = (UserStateStruct*) CTX;
293 StrBufAppendPrintf(Target, "%d", User->Session);
296 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
298 UserStateStruct *User = (UserStateStruct*) CTX;
299 return User->Idle;
302 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
304 UserStateStruct *User = (UserStateStruct*) CTX;
305 return User->SessionCount;
308 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
310 UserStateStruct *User = (UserStateStruct*) CTX;
311 StrBufAppendPrintf(Target, "%d", User->SessionCount);
314 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
316 UserStateStruct *User = (UserStateStruct*) CTX;
317 return (User->Session == WC->ctdl_pid);
320 void
321 InitModule_WHO
322 (void)
324 WebcitAddUrlHandler(HKEY("terminate_session"), _terminate_session, 0);
325 WebcitAddUrlHandler(HKEY("edit_me"), edit_me, 0);
327 RegisterIterator("WHOLIST", 0, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
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);
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);