* fix memleak
[citadel.git] / webcit / useredit.c
1 /*
2  * $Id$
3  */
4
5 #include "webcit.h"
6 #include "webserver.h"
7
8
9 /*
10  *  show a list of available users to edit them
11  *  message the header message???
12  *  preselect = which user should be selected in the browser
13  */
14 void select_user_to_edit(const char *preselect)
15 {
16         output_headers(1, 0, 0, 0, 1, 0);
17         do_template("aide_edituser_select", NULL);
18         end_burst();
19 }
20
21
22 typedef struct _UserListEntry {
23         int UID;
24         int AccessLevel;
25         int nLogons;
26         int nPosts;
27
28         StrBuf *UserName;
29         StrBuf *Passvoid;
30         time_t LastLogonT;
31         /* Just available for Single users to view: */
32         unsigned int Flags;
33         int DaysTillPurge;
34 } UserListEntry;
35
36
37 UserListEntry* NewUserListOneEntry(StrBuf *SerializedUser, const char *Pos)
38 {
39         UserListEntry *ul;
40
41         if (StrLength(SerializedUser) < 8) 
42                 return NULL;
43
44         ul = (UserListEntry*) malloc(sizeof(UserListEntry));
45         ul->UserName = NewStrBuf();
46         ul->Passvoid = NewStrBuf();
47
48         StrBufExtract_NextToken(ul->UserName,               SerializedUser, &Pos, '|');
49         StrBufExtract_NextToken(ul->Passvoid,               SerializedUser, &Pos, '|');
50         ul->Flags         = StrBufExtractNext_unsigned_long(SerializedUser, &Pos, '|');
51         ul->nLogons       = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
52         ul->nPosts        = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
53         ul->AccessLevel   = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
54         ul->UID           = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
55         ul->LastLogonT    = StrBufExtractNext_long(         SerializedUser, &Pos, '|');
56         ul->DaysTillPurge = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
57         return ul;
58 }
59
60 void DeleteUserListEntry(void *vUserList)
61 {
62         UserListEntry *ul = (UserListEntry*) vUserList;
63         if (!ul) return;
64         FreeStrBuf(&ul->UserName);
65         FreeStrBuf(&ul->Passvoid);
66         free(ul);
67 }
68
69 UserListEntry* NewUserListEntry(StrBuf *SerializedUserList)
70 {
71         const char *Pos = NULL;
72         UserListEntry *ul;
73
74         if (StrLength(SerializedUserList) < 8) 
75                 return NULL;
76
77         ul = (UserListEntry*) malloc(sizeof(UserListEntry));
78         ul->UserName = NewStrBuf();
79         ul->Passvoid = NewStrBuf();
80
81         StrBufExtract_NextToken(ul->UserName,    SerializedUserList, &Pos, '|');
82         ul->AccessLevel = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
83         ul->UID         = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
84         ul->LastLogonT  = StrBufExtractNext_long(SerializedUserList, &Pos, '|');
85         ul->nLogons     = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
86         ul->nPosts      = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
87         StrBufExtract_NextToken(ul->Passvoid,    SerializedUserList, &Pos, '|');
88         ul->Flags = 0;
89         ul->DaysTillPurge = -1;
90         return ul;
91 }
92
93 /*
94  * Sort by Username
95  */
96 int CompareUserListName(const void *vUser1, const void *vUser2)
97 {
98         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
99         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
100
101         return strcmp(ChrPtr(u1->UserName), ChrPtr(u2->UserName));
102 }
103
104 int CompareUserListNameRev(const void *vUser1, const void *vUser2)
105 {
106         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
107         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
108         return strcmp(ChrPtr(u2->UserName), ChrPtr(u1->UserName));
109 }
110
111 int GroupchangeUserListName(const void *vUser1, const void *vUser2)
112 {
113         UserListEntry *u1 = (UserListEntry*) vUser1;
114         UserListEntry *u2 = (UserListEntry*) vUser2;
115         return ChrPtr(u2->UserName)[0] != ChrPtr(u1->UserName)[0];
116 }
117
118 /*
119  * Sort by access level
120  */
121 int CompareAccessLevel(const void *vUser1, const void *vUser2)
122 {
123         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
124         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
125
126         return (u1->AccessLevel > u2->AccessLevel);
127 }
128
129 int CompareAccessLevelRev(const void *vUser1, const void *vUser2)
130 {
131         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
132         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
133
134         return (u2->AccessLevel > u1->AccessLevel);
135 }
136
137 int GroupchangeAccessLevel(const void *vUser1, const void *vUser2)
138 {
139         UserListEntry *u1 = (UserListEntry*) vUser1;
140         UserListEntry *u2 = (UserListEntry*) vUser2;
141
142         return u2->AccessLevel != u1->AccessLevel;
143 }
144
145 /*
146  * Sort by UID
147  */
148 int CompareUID(const void *vUser1, const void *vUser2)
149 {
150         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
151         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
152
153         return (u1->UID > u2->UID);
154 }
155
156 int CompareUIDRev(const void *vUser1, const void *vUser2)
157 {
158         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
159         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
160
161         return (u2->UID > u1->UID);
162 }
163
164 int GroupchangeUID(const void *vUser1, const void *vUser2)
165 {
166         UserListEntry *u1 = (UserListEntry*) vUser1;
167         UserListEntry *u2 = (UserListEntry*) vUser2;
168
169         return (u2->UID / 10) != (u1->UID / 10);
170 }
171
172 /*
173  * Sort By Date /// TODO!
174  */
175 int CompareLastLogon(const void *vUser1, const void *vUser2)
176 {
177         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
178         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
179
180         return (u1->LastLogonT > u2->LastLogonT);
181 }
182
183 int CompareLastLogonRev(const void *vUser1, const void *vUser2)
184 {
185         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
186         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
187
188         return (u2->LastLogonT > u1->LastLogonT);
189 }
190
191 int GroupchangeLastLogon(const void *vUser1, const void *vUser2)
192 {
193         UserListEntry *u1 = (UserListEntry*) vUser1;
194         UserListEntry *u2 = (UserListEntry*) vUser2;
195
196         return (u2->LastLogonT != u1->LastLogonT);
197 }
198
199 /*
200  * Sort By Number of Logons
201  */
202 int ComparenLogons(const void *vUser1, const void *vUser2)
203 {
204         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
205         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
206
207         return (u1->nLogons > u2->nLogons);
208 }
209
210 int ComparenLogonsRev(const void *vUser1, const void *vUser2)
211 {
212         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
213         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
214
215         return (u2->nLogons > u1->nLogons);
216 }
217
218 int GroupchangenLogons(const void *vUser1, const void *vUser2)
219 {
220         UserListEntry *u1 = (UserListEntry*) vUser1;
221         UserListEntry *u2 = (UserListEntry*) vUser2;
222
223         return (u2->nLogons / 100) != (u1->nLogons / 100);
224 }
225
226 /*
227  * Sort By Number of Posts
228  */
229 int ComparenPosts(const void *vUser1, const void *vUser2)
230 {
231         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
232         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
233
234         return (u1->nPosts > u2->nPosts);
235 }
236
237 int ComparenPostsRev(const void *vUser1, const void *vUser2)
238 {
239         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
240         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
241
242         return (u2->nPosts > u1->nPosts);
243 }
244
245 int GroupchangenPosts(const void *vUser1, const void *vUser2)
246 {
247         UserListEntry *u1 = (UserListEntry*) vUser1;
248         UserListEntry *u2 = (UserListEntry*) vUser2;
249
250         return (u2->nPosts / 100) != (u1->nPosts / 100);
251 }
252
253
254 HashList *iterate_load_userlist(StrBuf *Target, WCTemplputParams *TP)
255 {
256         int Done = 0;
257         CompareFunc SortIt;
258         HashList *Hash = NULL;
259         StrBuf *Buf;
260         UserListEntry* ul;
261         char nnn[64];
262         int nUsed;
263         int len;
264         WCTemplputParams SubTP;
265
266         memset(&SubTP, 0, sizeof(WCTemplputParams));    
267         serv_puts("LIST");
268         Buf = NewStrBuf();
269         StrBuf_ServGetln(Buf);
270         if (GetServerStatus(Buf, NULL) == 1) {
271                 Hash = NewHash(1, NULL);
272
273                 while (!Done) {
274                         len = StrBuf_ServGetln(Buf);
275                         if ((len == 3) &&
276                             (strcmp(ChrPtr(Buf), "000")==0)) {
277                                 Done = 1;
278                                 break;
279                         }
280                         ul = NewUserListEntry(Buf);
281                         if (ul == NULL)
282                                 continue;
283                         nUsed = GetCount(Hash);
284                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
285                         Put(Hash, nnn, nUsed, ul, DeleteUserListEntry); 
286                 }
287                 SubTP.Filter.ContextType = CTX_USERLIST;
288                 SortIt = RetrieveSort(&SubTP, HKEY("USER"), HKEY("user:uid"), 0);
289                 if (SortIt != NULL)
290                         SortByPayload(Hash, SortIt);
291                 else 
292                         SortByPayload(Hash, CompareUID);
293         }
294         FreeStrBuf(&Buf);
295         return Hash;
296 }
297
298
299 void tmplput_USERLIST_UserName(StrBuf *Target, WCTemplputParams *TP)
300 {
301         UserListEntry *ul = (UserListEntry*) CTX;
302         StrBufAppendTemplate(Target, TP, ul->UserName, 0);
303 }
304
305 void tmplput_USERLIST_Password(StrBuf *Target, WCTemplputParams *TP)
306 {
307         UserListEntry *ul = (UserListEntry*) CTX;
308         StrBufAppendTemplate(Target, TP, ul->Passvoid, 0);
309 }
310
311 void tmplput_USERLIST_AccessLevelNo(StrBuf *Target, WCTemplputParams *TP)
312 {
313         UserListEntry *ul = (UserListEntry*) CTX;
314
315         StrBufAppendPrintf(Target, "%d", ul->AccessLevel, 0);
316 }
317
318 void tmplput_USERLIST_AccessLevelStr(StrBuf *Target, WCTemplputParams *TP)
319 {
320         UserListEntry *ul = (UserListEntry*) CTX;
321         
322         StrBufAppendBufPlain(Target, _(axdefs[ul->AccessLevel]), -1, 0);
323 }
324
325 void tmplput_USERLIST_UID(StrBuf *Target, WCTemplputParams *TP)
326 {
327         UserListEntry *ul = (UserListEntry*) CTX;
328
329         StrBufAppendPrintf(Target, "%d", ul->UID, 0);
330 }
331
332 void tmplput_USERLIST_LastLogonNo(StrBuf *Target, WCTemplputParams *TP)
333 {
334         UserListEntry *ul = (UserListEntry*) CTX;
335
336         StrBufAppendPrintf(Target,"%ld", ul->LastLogonT, 0);
337 }
338 void tmplput_USERLIST_LastLogonStr(StrBuf *Target, WCTemplputParams *TP)
339 {
340         UserListEntry *ul = (UserListEntry*) CTX;
341         StrEscAppend(Target, NULL, asctime(localtime(&ul->LastLogonT)), 0, 0);
342 }
343
344 void tmplput_USERLIST_nLogons(StrBuf *Target, WCTemplputParams *TP)
345 {
346         UserListEntry *ul = (UserListEntry*) CTX;
347
348         StrBufAppendPrintf(Target, "%d", ul->nLogons, 0);
349 }
350
351 void tmplput_USERLIST_nPosts(StrBuf *Target, WCTemplputParams *TP)
352 {
353         UserListEntry *ul = (UserListEntry*) CTX;
354
355         StrBufAppendPrintf(Target, "%d", ul->nPosts, 0);
356 }
357
358 void tmplput_USERLIST_Flags(StrBuf *Target, WCTemplputParams *TP)
359 {
360         UserListEntry *ul = (UserListEntry*) CTX;
361
362         StrBufAppendPrintf(Target, "%d", ul->Flags, 0);
363 }
364
365 void tmplput_USERLIST_DaysTillPurge(StrBuf *Target, WCTemplputParams *TP)
366 {
367         UserListEntry *ul = (UserListEntry*) CTX;
368
369         StrBufAppendPrintf(Target, "%d", ul->DaysTillPurge, 0);
370 }
371
372 int ConditionalUser(StrBuf *Target, WCTemplputParams *TP)
373 {
374         UserListEntry *ul = (UserListEntry*) CTX;
375         if (havebstr("usernum")) {
376                 return ibstr("usernum") == ul->UID;
377         }
378         else if (havebstr("username")) {
379                 return strcmp(bstr("username"), ChrPtr(ul->UserName)) == 0;
380         }
381         else 
382                 return 0;
383 }
384
385 int ConditionalFlagINetEmail(StrBuf *Target, WCTemplputParams *TP)
386 {
387         UserListEntry *ul = (UserListEntry*) CTX;
388         return (ul->Flags & US_INTERNET) != 0;
389 }
390
391 int ConditionalUserAccess(StrBuf *Target, WCTemplputParams *TP)
392 {
393         UserListEntry *ul = (UserListEntry*) CTX;
394
395         if (TP->Tokens->Params[3]->Type == TYPE_LONG)
396                 return (TP->Tokens->Params[3]->lvalue == ul->AccessLevel);
397         else
398                 return 0;
399 }
400
401 /*
402  *  Locate the message number of a user's vCard in the current room
403  *  Returns the message id of his vcard
404  */
405 long locate_user_vcard_in_this_room(message_summary **VCMsg, wc_mime_attachment **VCAtt)
406 {
407         wcsession *WCC = WC;
408         HashPos *at;
409         HashPos *att;
410         const char *HashKey;
411         long HKLen;
412         void *vMsg;
413         message_summary *Msg;
414         wc_mime_attachment *Att;
415         int Done;
416         StrBuf *Buf;
417         long vcard_msgnum = (-1L);
418         int already_tried_creating_one = 0;
419         StrBuf *FoundCharset = NewStrBuf();
420         StrBuf *Error = NULL;
421         SharedMessageStatus Stat;
422
423
424         Buf = NewStrBuf();
425 TRYAGAIN:
426         memset(&Stat, 0, sizeof(SharedMessageStatus));
427         Stat.maxload = 10000;
428         Stat.lowest_found = (-1);
429         Stat.highest_found = (-1);
430         Done = 0;
431         /* Search for the user's vCard */
432         if (load_msg_ptrs("MSGS ALL||||1", &Stat) > 0) {
433                 at = GetNewHashPos(WCC->summ, 0);
434                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
435                         Msg = (message_summary*) vMsg;          
436                         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
437                         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
438                         Msg->MsgBody->msgnum = Msg->msgnum;
439
440                         load_message(Msg, FoundCharset, &Error);
441                         
442                         if (Msg->AllAttach != NULL) {
443                                 att = GetNewHashPos(Msg->AllAttach, 0);
444                                 while (GetNextHashPos(Msg->AllAttach, att, &HKLen, &HashKey, &vMsg) && 
445                                        (vcard_msgnum == -1)) {
446                                         Att = (wc_mime_attachment*) vMsg;
447                                         if (
448                                                 (strcasecmp(ChrPtr(Att->ContentType), "text/x-vcard") == 0)
449                                                 || (strcasecmp(ChrPtr(Att->ContentType), "text/vcard") == 0)
450                                         ) {
451                                                 *VCAtt = Att;
452                                                 *VCMsg = Msg;
453                                                 vcard_msgnum = Msg->msgnum;
454                                                 if (Att->Data == NULL) {
455                                                         MimeLoadData(Att);
456                                                 }
457                                         }
458                                 }
459                                 DeleteHashPos(&att);
460                         }
461                         FreeStrBuf(&Error);     /* don't care... */
462                         
463                 }
464                 DeleteHashPos(&at);             
465         }
466
467         /* If there's no vcard, create one */
468         if ((*VCMsg == NULL) && (already_tried_creating_one == 0)) {
469                 FlushStrBuf(Buf);
470                 already_tried_creating_one = 1;
471                 serv_puts("ENT0 1|||4");
472                 StrBuf_ServGetln(Buf);
473                 if (GetServerStatus(Buf, NULL) == 4) {
474                         serv_puts("Content-type: text/x-vcard");
475                         serv_puts("");
476                         serv_puts("begin:vcard");
477                         serv_puts("end:vcard");
478                         serv_puts("000");
479                 }
480                 else 
481                         lprintf(1, "Error while creating user vcard: %s\n", ChrPtr(Buf));
482                 goto TRYAGAIN;
483         }
484         FreeStrBuf(&Buf);
485         FreeStrBuf(&FoundCharset);
486
487         return(vcard_msgnum);
488 }
489
490
491 /*
492  *  Display the form for editing a user's address book entry
493  *  username the name of the user
494  *  usernum the citadel-uid of the user
495  */
496 void display_edit_address_book_entry(const char *username, long usernum) {
497         wcsession *WCC = WC;
498         message_summary *VCMsg = NULL;
499         wc_mime_attachment *VCAtt = NULL;
500         StrBuf *roomname;
501         StrBuf *Buf;
502         long vcard_msgnum = (-1L);
503
504         /* Locate the user's config room, creating it if necessary */
505         Buf = NewStrBuf();
506         roomname = NewStrBuf();
507         StrBufPrintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
508         serv_printf("GOTO %s||1", ChrPtr(roomname));
509         StrBuf_ServGetln(Buf);
510         if (GetServerStatus(Buf, NULL) != 2) {
511                 serv_printf("CRE8 1|%s|5|||1|", ChrPtr(roomname));
512                 StrBuf_ServGetln(Buf);
513                 GetServerStatus(Buf, NULL);
514                 serv_printf("GOTO %s||1", ChrPtr(roomname));
515                 StrBuf_ServGetln(Buf);
516                 if (GetServerStatus(Buf, NULL) != 2) {
517                         FlushStrBuf(WCC->ImportantMsg);
518                         StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
519                         select_user_to_edit(username);
520                         FreeStrBuf(&Buf);
521                         FreeStrBuf(&roomname);
522                         return;
523                 }
524         }
525         FreeStrBuf(&Buf);
526
527         locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
528
529         if (VCMsg == NULL) {
530                 StrBufPlain(WCC->ImportantMsg, 
531                             _("An error occurred while trying to create or edit this address book entry."), 
532                             0);
533                 select_user_to_edit(username);
534                 FreeStrBuf(&roomname);
535                 return;
536         }
537
538         do_edit_vcard(vcard_msgnum, "1", 
539                       VCMsg,
540                       VCAtt,
541                       "select_user_to_edit", 
542                       ChrPtr(roomname));
543         FreeStrBuf(&roomname);
544 }
545
546
547 void display_edituser(const char *supplied_username, int is_new) {
548         const char *Pos;
549         wcsession *WCC = WC;
550         UserListEntry* UL;
551         StrBuf *Buf;
552         char username[256];
553
554         if (supplied_username != NULL) {
555                 safestrncpy(username, supplied_username, sizeof username);
556         }
557         else {
558                 safestrncpy(username, bstr("username"), sizeof username);
559         }
560
561         Buf = NewStrBuf();
562         serv_printf("AGUP %s", username);
563         StrBuf_ServGetln(Buf);
564         if (GetServerStatus(Buf, NULL) != 2) {
565                 FlushStrBuf(WCC->ImportantMsg);
566                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
567                 select_user_to_edit(username);
568                 FreeStrBuf(&Buf);
569                 return;
570         }
571         else {
572                 Pos = ChrPtr(Buf) + 4;
573                 UL = NewUserListOneEntry(Buf, Pos);
574                 if ((UL != NULL) && havebstr("edit_abe_button")) {
575                         display_edit_address_book_entry(username, UL->UID);
576                 }
577                 else if ((UL != NULL) && havebstr("delete_button")) {
578                         delete_user(username);
579                 }
580                 else if (UL != NULL) {
581                         WCTemplputParams SubTP;
582                         memset(&SubTP, 0, sizeof(WCTemplputParams));
583                         SubTP.Filter.ContextType = CTX_USERLIST;
584                         SubTP.Context = UL;
585                         output_headers(1, 0, 0, 0, 1, 0);
586                         DoTemplate(HKEY("aide_edituser_detailview"), NULL, &SubTP);
587                         end_burst();
588                 }
589                 DeleteUserListEntry(UL);
590                 
591         }
592         FreeStrBuf(&Buf);
593 }
594
595 /*
596  *  do the backend operation of the user edit on the server
597  */
598 void edituser(void) {
599         wcsession *WCC = WC;
600         int is_new = 0;
601         unsigned int flags = 0;
602         const char *username;
603
604         is_new = ibstr("is_new");
605         username = bstr("username");
606
607         if (!havebstr("ok_button")) {
608                 StrBufPlain(WCC->ImportantMsg, _("Changes were not saved."), -1);
609         }       
610         else {
611                 StrBuf *Buf = NewStrBuf();
612
613                 flags = ibstr("flags");
614                 if (yesbstr("inetmail")) {
615                         flags |= US_INTERNET;
616                 }
617                 else {
618                         flags &= ~US_INTERNET ;
619                 }
620
621                 if ((havebstr("newname")) && (strcasecmp(bstr("username"), bstr("newname")))) {
622                         serv_printf("RENU %s|%s", bstr("username"), bstr("newname"));
623                         StrBuf_ServGetln(Buf);
624                         if (GetServerStatus(Buf, NULL) == 2) {
625                                 FlushStrBuf(WCC->ImportantMsg);
626                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);                             
627                         }
628                         else {
629                                 username = bstr("newname");
630                         }
631                 }
632
633                 serv_printf("ASUP %s|%s|%d|%s|%s|%s|%s|%s|%s|",
634                         username,
635                         bstr("password"),
636                         flags,
637                         bstr("timescalled"),
638                         bstr("msgsposted"),
639                         bstr("axlevel"),
640                         bstr("usernum"),
641                         bstr("lastcall"),
642                         bstr("purgedays")
643                 );
644                 StrBuf_ServGetln(Buf);
645                 if (GetServerStatus(Buf, NULL) == 2) {
646                         StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
647                 }
648                 FreeStrBuf(&Buf);
649         }
650
651         /*
652          * If we are in the middle of creating a new user, move on to
653          * the vCard edit screen.
654          */
655         if (is_new) {
656                 display_edit_address_book_entry(username, lbstr("usernum") );
657         }
658         else {
659                 select_user_to_edit(username);
660         }
661 }
662
663 /*
664  *  burge a user 
665  *  username the name of the user to remove
666  */
667 void delete_user(char *username) {
668         wcsession *WCC = WC;
669         StrBuf *Buf;
670         
671         Buf = NewStrBuf();
672         serv_printf("ASUP %s|0|0|0|0|0|", username);
673         StrBuf_ServGetln(Buf);
674         if (GetServerStatus(Buf, NULL) != 2) 
675                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
676
677         select_user_to_edit( bstr("username"));
678         FreeStrBuf(&Buf);
679 }
680                 
681
682
683 /*
684  *  create a new user
685  * take the web environment username and create it on the citadel server
686  */
687 void create_user(void) {
688         wcsession *WCC = WC;
689         long FullState;
690         StrBuf *Buf;
691         const char *username;
692
693         Buf = NewStrBuf();
694         username = bstr("username");
695         serv_printf("CREU %s", username);
696         StrBuf_ServGetln(Buf);
697         if (GetServerStatus(Buf, &FullState) == 2) {
698                 sprintf(WC->ImportantMessage, _("A new user has been created."));
699                 display_edituser(username, 1);
700         }
701         else if (FullState == 570) {
702                 StrBufPlain(WCC->ImportantMsg, 
703                             _("You are attempting to create a new user from within Citadel "
704                               "while running in host based authentication mode.  In this mode, "
705                               "you must create new users on the host system, not within Citadel."), 
706                             0);
707                 select_user_to_edit(NULL);
708         }
709         else {
710                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
711                 select_user_to_edit(NULL);
712         }
713         FreeStrBuf(&Buf);
714 }
715
716
717 void _select_user_to_edit(void) {
718         select_user_to_edit(NULL);
719 }
720
721
722 void _display_edituser(void) {
723         display_edituser(NULL, 0);
724 }
725
726
727 void 
728 InitModule_USEREDIT
729 (void)
730 {
731         WebcitAddUrlHandler(HKEY("select_user_to_edit"), "", 0, _select_user_to_edit, 0);
732         WebcitAddUrlHandler(HKEY("display_edituser"), "", 0, _display_edituser, 0);
733         WebcitAddUrlHandler(HKEY("edituser"), "", 0, edituser, 0);
734         WebcitAddUrlHandler(HKEY("create_user"), "", 0, create_user, 0);
735
736         RegisterNamespace("USERLIST:USERNAME",      0, 1, tmplput_USERLIST_UserName, NULL, CTX_USERLIST);
737         RegisterNamespace("USERLIST:PASSWD",        0, 1, tmplput_USERLIST_Password, NULL, CTX_USERLIST);
738         RegisterNamespace("USERLIST:ACCLVLNO",      0, 0, tmplput_USERLIST_AccessLevelNo, NULL, CTX_USERLIST);
739         RegisterNamespace("USERLIST:ACCLVLSTR",     0, 0, tmplput_USERLIST_AccessLevelStr, NULL, CTX_USERLIST);
740         RegisterNamespace("USERLIST:UID",           0, 0, tmplput_USERLIST_UID, NULL, CTX_USERLIST);
741         RegisterNamespace("USERLIST:LASTLOGON:STR", 0, 0, tmplput_USERLIST_LastLogonStr, NULL, CTX_USERLIST);
742         RegisterNamespace("USERLIST:LASTLOGON:NO",  0, 0, tmplput_USERLIST_LastLogonNo, NULL, CTX_USERLIST);
743         RegisterNamespace("USERLIST:NLOGONS",       0, 0, tmplput_USERLIST_nLogons, NULL, CTX_USERLIST);
744         RegisterNamespace("USERLIST:NPOSTS",        0, 0, tmplput_USERLIST_nPosts, NULL, CTX_USERLIST);
745                                                     
746         RegisterNamespace("USERLIST:FLAGS",         0, 0, tmplput_USERLIST_Flags, NULL, CTX_USERLIST);
747         RegisterNamespace("USERLIST:DAYSTILLPURGE", 0, 0, tmplput_USERLIST_DaysTillPurge, NULL, CTX_USERLIST);
748
749         RegisterConditional(HKEY("COND:USERNAME"),  0,    ConditionalUser, CTX_USERLIST);
750         RegisterConditional(HKEY("COND:USERACCESS"), 0,   ConditionalUserAccess, CTX_USERLIST);
751         RegisterConditional(HKEY("COND:USERLIST:FLAG:USE_INTERNET"), 0, ConditionalFlagINetEmail, CTX_USERLIST);
752
753         RegisterIterator("USERLIST", 0, NULL, iterate_load_userlist, NULL, DeleteHash, CTX_USERLIST, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
754         
755
756
757         RegisterSortFunc(HKEY("user:name"),
758                          HKEY("userlist"),
759                          CompareUserListName,
760                          CompareUserListNameRev,
761                          GroupchangeUserListName,
762                          CTX_USERLIST);
763         RegisterSortFunc(HKEY("user:accslvl"),
764                          HKEY("userlist"),
765                          CompareAccessLevel,
766                          CompareAccessLevelRev,
767                          GroupchangeAccessLevel,
768                          CTX_USERLIST);
769
770         RegisterSortFunc(HKEY("user:nlogons"),
771                          HKEY("userlist"),
772                          ComparenLogons,
773                          ComparenLogonsRev,
774                          GroupchangenLogons,
775                          CTX_USERLIST);
776
777         RegisterSortFunc(HKEY("user:uid"),
778                          HKEY("userlist"),
779                          CompareUID,
780                          CompareUIDRev,
781                          GroupchangeUID,
782                          CTX_USERLIST);
783
784         RegisterSortFunc(HKEY("user:lastlogon"),
785                          HKEY("userlist"),
786                          CompareLastLogon,
787                          CompareLastLogonRev,
788                          GroupchangeLastLogon,
789                          CTX_USERLIST);
790
791         RegisterSortFunc(HKEY("user:nmsgposts"),
792                          HKEY("userlist"),
793                          ComparenPosts,
794                          ComparenPostsRev,
795                          GroupchangenPosts,
796                          CTX_USERLIST);
797
798 }