Integrated the DKIM signer into serv_smtpclient, but disabled it
[citadel.git] / webcit / who.c
1
2 #include "webcit.h"
3
4 CtxType CTX_WHO = CTX_NONE;
5
6 typedef struct UserStateStruct {
7         StrBuf *UserName;
8         StrBuf *Room;
9         StrBuf *Host;
10         StrBuf *UserAgent;
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         FreeStrBuf(&User->UserAgent);
29         free(User);
30 }
31
32 int CompareUserStruct(const void *VUser1, const void *VUser2)
33 {
34         const UserStateStruct *User1 = (UserStateStruct*) GetSearchPayload(VUser1);
35         const UserStateStruct *User2 = (UserStateStruct*) GetSearchPayload(VUser2);
36         
37         if (User1->Idle != User2->Idle)
38                 return User1->Idle > User2->Idle;
39         return strcasecmp(ChrPtr(User1->UserName), 
40                           ChrPtr(User2->UserName));
41 }
42
43
44 int GetWholistSection(HashList *List, time_t now, StrBuf *Buf, const char *FilterName, long FNLen)
45 {
46         UserStateStruct *User, *OldUser;
47         void *VOldUser;
48         size_t BufLen;
49         const char *Pos;
50
51         serv_puts("RWHO");
52         StrBuf_ServGetln(Buf);
53         if (GetServerStatus(Buf, NULL) == 1) {
54                 while (BufLen = StrBuf_ServGetln(Buf), 
55                        ((BufLen >= 0) && 
56                         ((BufLen != 3) || strcmp(ChrPtr(Buf), "000"))))
57                 {
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                         User->UserAgent = NewStrBufPlain(NULL, BufLen);
74                         StrBufExtract_NextToken(User->UserAgent, Buf, &Pos, '|');
75
76                         User->LastActive = StrBufExtractNext_long(Buf, &Pos, '|');
77                         StrBufSkip_NTokenS(Buf, &Pos, '|', 3);
78
79                         User->RealRoom = NewStrBufPlain(NULL, BufLen);
80                         StrBufExtract_NextToken(User->RealRoom, Buf, &Pos, '|');
81
82                         User->RealHost = NewStrBufPlain(NULL, BufLen);
83                         StrBufExtract_NextToken(User->RealHost, Buf, &Pos, '|');
84                         
85                         User->Idle = (now - User->LastActive) > 900L;
86                         User->IdleSince = (now - User->LastActive) / 60;
87                         User->SessionCount = 1;
88
89                         if (FilterName == NULL) {
90                                 if (GetHash(List, 
91                                             SKEY(User->UserName), 
92                                             &VOldUser)) {
93                                         OldUser = VOldUser;
94                                         OldUser->SessionCount++;
95                                         if (!User->Idle) {
96                                                 if (User->Session == WC->ctdl_pid) 
97                                                         OldUser->Session = User->Session;
98                                                 
99                                                 OldUser->Idle = User->Idle;
100                                                 OldUser->LastActive = User->LastActive;
101                                         }
102                                         DestroyUserStruct(User);
103                                 }
104                                 else
105                                         Put(List, 
106                                             SKEY(User->UserName), 
107                                             User, DestroyUserStruct);
108                         }
109                         else {
110                                 if (strcmp(FilterName, ChrPtr(User->UserName)) == 0)
111                                 {
112                                         Put(List, 
113                                             SKEY(User->UserName), 
114                                             User, DestroyUserStruct);
115                                 }
116                                 else 
117                                 {
118                                         DestroyUserStruct(User);
119                                 }
120                         }
121                 }
122                 if (FilterName == NULL)
123                         SortByPayload(List, CompareUserStruct);
124                 return 1;
125         }
126         else {
127                 return 0;
128         }
129 }
130
131 /*
132  * end session
133  */
134 void terminate_session(void)
135 {
136         char buf[SIZ];
137
138         serv_printf("TERM %s", bstr("which_session"));
139         serv_getln(buf, sizeof buf);
140         url_do_template();
141 }
142
143
144 void _terminate_session(void) {
145         slrp_highest();
146         terminate_session();
147 }
148
149 HashList *GetWholistHash(StrBuf *Target, WCTemplputParams *TP)
150
151 {
152         const char *ch = NULL;
153         int HashUniq = 1;
154         long len;
155         StrBuf *FilterNameStr = NULL;
156         StrBuf *Buf;
157         HashList *List;
158         time_t now;
159
160         Buf = NewStrBuf();
161
162         serv_puts("TIME");
163         StrBuf_ServGetln(Buf);
164         if (GetServerStatus(Buf, NULL)  == 2) {
165                 const char *pos = ChrPtr(Buf) + 4;
166                 now = StrBufExtractNext_long(Buf, &pos, '|');
167         }
168         else {
169                 now = time(NULL);
170         }
171         if (HaveTemplateTokenString(NULL, TP, 2, &ch, &len))
172         {
173                 FilterNameStr = NewStrBuf();
174                 GetTemplateTokenString(FilterNameStr, TP, 2, &ch, &len);
175                 HashUniq = 0;
176         }
177
178         List = NewHash(HashUniq, NULL);
179         GetWholistSection(List, now, Buf, ch, len);
180         FreeStrBuf(&Buf);
181         FreeStrBuf(&FilterNameStr);
182         return List;
183 }
184
185
186 void DeleteWholistHash(HashList **KillMe)
187 {
188         DeleteHash(KillMe);
189 }
190
191 void tmplput_who_username(StrBuf *Target, WCTemplputParams *TP)
192 {
193         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
194         StrBufAppendTemplate(Target, TP, User->UserName, 0);
195 }
196
197 void tmplput_who_UserAgent(StrBuf *Target, WCTemplputParams *TP)
198 {
199         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
200         StrBufAppendTemplate(Target, TP, User->UserAgent, 0);
201 }
202
203 void tmplput_who_room(StrBuf *Target, WCTemplputParams *TP)
204 {
205         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
206         StrBufAppendTemplate(Target, TP, User->Room, 0);
207 }
208
209 void tmplput_who_host(StrBuf *Target, WCTemplputParams *TP)
210 {
211         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
212         StrBufAppendTemplate(Target, TP, User->Host, 0);
213 }
214
215 void tmplput_who_realroom(StrBuf *Target, WCTemplputParams *TP)
216 {
217         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
218         StrBufAppendTemplate(Target, TP, User->RealRoom, 0);
219 }
220 int conditional_who_realroom(StrBuf *Target, WCTemplputParams *TP)
221 {
222         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
223         return StrLength(User->RealRoom) > 0;
224 }
225
226 void tmplput_who_realhost(StrBuf *Target, WCTemplputParams *TP)
227 {
228         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
229         StrBufAppendTemplate(Target, TP, User->RealHost, 0);
230 }
231 int conditional_who_realhost(StrBuf *Target, WCTemplputParams *TP)
232 {
233         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
234         return StrLength(User->RealHost) > 0;
235 }
236
237 void tmplput_who_lastactive(StrBuf *Target, WCTemplputParams *TP)
238 {
239         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
240         StrBufAppendPrintf(Target, "%d", User->LastActive);
241 }
242
243 void tmplput_who_idlesince(StrBuf *Target, WCTemplputParams *TP)
244 {
245         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
246         StrBufAppendPrintf(Target, "%d", User->IdleSince);
247 }
248
249 void tmplput_who_session(StrBuf *Target, WCTemplputParams *TP)
250 {
251         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
252         StrBufAppendPrintf(Target, "%d", User->Session);
253 }
254
255 int conditional_who_idle(StrBuf *Target, WCTemplputParams *TP)
256 {
257         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
258         return User->Idle;
259 }
260
261 int conditional_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
262 {
263         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
264         return User->SessionCount;
265 }
266
267 void tmplput_who_nsessions(StrBuf *Target, WCTemplputParams *TP)
268 {
269         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
270         StrBufAppendPrintf(Target, "%d", User->SessionCount);
271 }
272
273 int conditional_who_isme(StrBuf *Target, WCTemplputParams *TP)
274 {
275         UserStateStruct *User = (UserStateStruct*) CTX(CTX_WHO);
276         return (User->Session == WC->ctdl_pid);
277 }
278
279 void 
280 InitModule_WHO
281 (void)
282 {
283         RegisterCTX(CTX_WHO);
284
285         WebcitAddUrlHandler(HKEY("terminate_session"), "", 0, _terminate_session, 0);
286
287         RegisterIterator("WHOLIST", 1, NULL, GetWholistHash, NULL, DeleteWholistHash, CTX_WHO, CTX_NONE, IT_NOFLAG);
288
289         RegisterNamespace("WHO:NAME",        0, 1, tmplput_who_username, NULL, CTX_WHO);
290         RegisterNamespace("WHO:USERAGENT",   0, 1, tmplput_who_UserAgent, NULL, CTX_WHO);
291         RegisterNamespace("WHO:ROOM",        0, 1, tmplput_who_room, NULL, CTX_WHO);
292         RegisterNamespace("WHO:HOST",        0, 1, tmplput_who_host, NULL, CTX_WHO);
293         RegisterNamespace("WHO:REALROOM",    0, 1, tmplput_who_realroom, NULL, CTX_WHO);
294         RegisterNamespace("WHO:REALHOST",    0, 1, tmplput_who_realhost, NULL, CTX_WHO);
295         RegisterNamespace("WHO:LASTACTIVE",  0, 1, tmplput_who_lastactive, NULL, CTX_WHO);
296         RegisterNamespace("WHO:IDLESINCE",   0, 1, tmplput_who_idlesince, NULL, CTX_WHO);
297         RegisterNamespace("WHO:SESSION",     0, 1, tmplput_who_session, NULL, CTX_WHO);
298         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
299         RegisterNamespace("WHO:NSESSIONS",   0, 1, tmplput_who_nsessions, NULL, CTX_WHO);
300
301         RegisterConditional("WHO:IDLE",      1, conditional_who_idle, CTX_WHO);
302         RegisterConditional("WHO:NSESSIONS", 1, conditional_who_nsessions, CTX_WHO);
303         RegisterConditional("WHO:ISME",      1, conditional_who_isme, CTX_WHO);
304         RegisterConditional("WHO:REALROOM",  1, conditional_who_realroom, CTX_WHO);
305         RegisterConditional("WHO:REALHOST",  1, conditional_who_realhost, CTX_WHO);
306 }