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