* locate_user_vcard_in_this_room() lost the ability to return the msgnum of the vcard...
[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
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;
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                 return Hash;
294         }
295         FreeStrBuf(&Buf);
296         return NULL;
297 }
298
299
300 void tmplput_USERLIST_UserName(StrBuf *Target, WCTemplputParams *TP)
301 {
302         UserListEntry *ul = (UserListEntry*) CTX;
303         StrBufAppendTemplate(Target, TP, ul->UserName, 0);
304 }
305
306 void tmplput_USERLIST_Password(StrBuf *Target, WCTemplputParams *TP)
307 {
308         UserListEntry *ul = (UserListEntry*) CTX;
309         StrBufAppendTemplate(Target, TP, ul->Passvoid, 0);
310 }
311
312 void tmplput_USERLIST_AccessLevelNo(StrBuf *Target, WCTemplputParams *TP)
313 {
314         UserListEntry *ul = (UserListEntry*) CTX;
315
316         StrBufAppendPrintf(Target, "%d", ul->AccessLevel, 0);
317 }
318
319 void tmplput_USERLIST_AccessLevelStr(StrBuf *Target, WCTemplputParams *TP)
320 {
321         UserListEntry *ul = (UserListEntry*) CTX;
322         
323         StrBufAppendBufPlain(Target, _(axdefs[ul->AccessLevel]), -1, 0);
324 }
325
326 void tmplput_USERLIST_UID(StrBuf *Target, WCTemplputParams *TP)
327 {
328         UserListEntry *ul = (UserListEntry*) CTX;
329
330         StrBufAppendPrintf(Target, "%d", ul->UID, 0);
331 }
332
333 void tmplput_USERLIST_LastLogonNo(StrBuf *Target, WCTemplputParams *TP)
334 {
335         UserListEntry *ul = (UserListEntry*) CTX;
336
337         StrBufAppendPrintf(Target,"%ld", ul->LastLogonT, 0);
338 }
339 void tmplput_USERLIST_LastLogonStr(StrBuf *Target, WCTemplputParams *TP)
340 {
341         UserListEntry *ul = (UserListEntry*) CTX;
342         StrEscAppend(Target, NULL, asctime(localtime(&ul->LastLogonT)), 0, 0);
343 }
344
345 void tmplput_USERLIST_nLogons(StrBuf *Target, WCTemplputParams *TP)
346 {
347         UserListEntry *ul = (UserListEntry*) CTX;
348
349         StrBufAppendPrintf(Target, "%d", ul->nLogons, 0);
350 }
351
352 void tmplput_USERLIST_nPosts(StrBuf *Target, WCTemplputParams *TP)
353 {
354         UserListEntry *ul = (UserListEntry*) CTX;
355
356         StrBufAppendPrintf(Target, "%d", ul->nPosts, 0);
357 }
358
359 void tmplput_USERLIST_Flags(StrBuf *Target, WCTemplputParams *TP)
360 {
361         UserListEntry *ul = (UserListEntry*) CTX;
362
363         StrBufAppendPrintf(Target, "%d", ul->Flags, 0);
364 }
365
366 void tmplput_USERLIST_DaysTillPurge(StrBuf *Target, WCTemplputParams *TP)
367 {
368         UserListEntry *ul = (UserListEntry*) CTX;
369
370         StrBufAppendPrintf(Target, "%d", ul->DaysTillPurge, 0);
371 }
372
373 int ConditionalUser(StrBuf *Target, WCTemplputParams *TP)
374 {
375         UserListEntry *ul = (UserListEntry*) CTX;
376         if (havebstr("usernum")) {
377                 return ibstr("usernum") == ul->UID;
378         }
379         else if (havebstr("username")) {
380                 return strcmp(bstr("username"), ChrPtr(ul->UserName)) == 0;
381         }
382         else 
383                 return 0;
384 }
385
386 int ConditionalFlagINetEmail(StrBuf *Target, WCTemplputParams *TP)
387 {
388         UserListEntry *ul = (UserListEntry*) CTX;
389         return (ul->Flags & US_INTERNET) != 0;
390 }
391
392 int ConditionalUserAccess(StrBuf *Target, WCTemplputParams *TP)
393 {
394         UserListEntry *ul = (UserListEntry*) CTX;
395
396         if (TP->Tokens->Params[3]->Type == TYPE_LONG)
397                 return (TP->Tokens->Params[3]->lvalue == ul->AccessLevel);
398         else
399                 return 0;
400 }
401
402 /*
403  *  Locate the message number of a user's vCard in the current room
404  *  Returns the message id of his vcard
405  */
406 long locate_user_vcard_in_this_room(message_summary **VCMsg, wc_mime_attachment **VCAtt)
407 {
408         wcsession *WCC = WC;
409         HashPos *at;
410         HashPos *att;
411         const char *HashKey;
412         long HKLen;
413         void *vMsg;
414         message_summary *Msg;
415         wc_mime_attachment *Att;
416         int Done;
417         StrBuf *Buf;
418         long vcard_msgnum = (-1L);
419         int already_tried_creating_one = 0;
420         StrBuf *FoundCharset = NewStrBuf();
421         StrBuf *Error = NULL;
422
423         Buf = NewStrBuf();
424 TRYAGAIN:
425         Done = 0;
426         /* Search for the user's vCard */
427         if (load_msg_ptrs("MSGS ALL||||1", 1) > 0) {
428                 at = GetNewHashPos(WCC->summ, 0);
429                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
430                         Msg = (message_summary*) vMsg;          
431                         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
432                         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
433                         Msg->MsgBody->msgnum = Msg->msgnum;
434
435                         load_message(Msg, FoundCharset, &Error);
436                         
437                         if (Msg->AllAttach != NULL) {
438                                 att = GetNewHashPos(Msg->AllAttach, 0);
439                                 while (GetNextHashPos(Msg->AllAttach, att, &HKLen, &HashKey, &vMsg)) {
440                                         Att = (wc_mime_attachment*) vMsg;
441                                         if (
442                                                 (strcasecmp(ChrPtr(Att->ContentType), "text/x-vcard") == 0)
443                                                 || (strcasecmp(ChrPtr(Att->ContentType), "text/vcard") == 0)
444                                         ) {
445                                                 *VCAtt = Att;
446                                                 *VCMsg = Msg;
447                                                 if (Att->Data == NULL) {
448                                                         MimeLoadData(Att);
449                                                         vcard_msgnum = Msg->msgnum;
450                                                 }
451                                         }
452                                 }
453                         }
454                         FreeStrBuf(&Error);     /* don't care... */
455                         
456                 }
457                 DeleteHashPos(&at);             
458         }
459
460         /* If there's no vcard, create one */
461         if ((*VCMsg == NULL) && (already_tried_creating_one == 0)) {
462                 already_tried_creating_one = 1;
463                 serv_puts("ENT0 1|||4");
464                 StrBuf_ServGetln(Buf);
465                 if (GetServerStatus(Buf, NULL) != 4) {
466                         serv_puts("Content-type: text/x-vcard");
467                         serv_puts("");
468                         serv_puts("begin:vcard");
469                         serv_puts("end:vcard");
470                         serv_puts("000");
471                 }
472                 goto TRYAGAIN;
473         }
474         FreeStrBuf(&Buf);
475         FreeStrBuf(&FoundCharset);
476
477         return(vcard_msgnum);
478 }
479
480
481 /*
482  *  Display the form for editing a user's address book entry
483  *  username the name of the user
484  *  usernum the citadel-uid of the user
485  */
486 void display_edit_address_book_entry(const char *username, long usernum) {
487         wcsession *WCC = WC;
488         message_summary *VCMsg = NULL;
489         wc_mime_attachment *VCAtt = NULL;
490         StrBuf *roomname;
491         StrBuf *Buf;
492         long vcard_msgnum = (-1L);
493
494         /* Locate the user's config room, creating it if necessary */
495         Buf = NewStrBuf();
496         roomname = NewStrBuf();
497         StrBufPrintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
498         serv_printf("GOTO %s||1", ChrPtr(roomname));
499         StrBuf_ServGetln(Buf);
500         if (GetServerStatus(Buf, NULL) != 2) {
501                 serv_printf("CRE8 1|%s|5|||1|", ChrPtr(roomname));
502                 StrBuf_ServGetln(Buf);
503                 GetServerStatus(Buf, NULL);
504                 serv_printf("GOTO %s||1", ChrPtr(roomname));
505                 StrBuf_ServGetln(Buf);
506                 if (GetServerStatus(Buf, NULL) != 2) {
507                         FlushStrBuf(WCC->ImportantMsg);
508                         StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
509                         select_user_to_edit(username);
510                         FreeStrBuf(&Buf);
511                         FreeStrBuf(&roomname);
512                         return;
513                 }
514         }
515         FreeStrBuf(&Buf);
516
517         locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
518
519         if (VCMsg == NULL) {
520                 StrBufPlain(WCC->ImportantMsg, 
521                             _("An error occurred while trying to create or edit this address book entry."), 
522                             0);
523                 select_user_to_edit(username);
524                 FreeStrBuf(&roomname);
525                 return;
526         }
527
528         do_edit_vcard(vcard_msgnum, "1", 
529                       VCMsg,
530                       VCAtt,
531                       "select_user_to_edit", 
532                       ChrPtr(roomname));
533         FreeStrBuf(&roomname);
534 }
535
536
537 void display_edituser(const char *supplied_username, int is_new) {
538         const char *Pos;
539         wcsession *WCC = WC;
540         UserListEntry* UL;
541         StrBuf *Buf;
542         char username[256];
543
544         if (supplied_username != NULL) {
545                 safestrncpy(username, supplied_username, sizeof username);
546         }
547         else {
548                 safestrncpy(username, bstr("username"), sizeof username);
549         }
550
551         Buf = NewStrBuf();
552         serv_printf("AGUP %s", username);
553         StrBuf_ServGetln(Buf);
554         if (GetServerStatus(Buf, NULL) != 2) {
555                 FlushStrBuf(WCC->ImportantMsg);
556                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
557                 select_user_to_edit(username);
558                 FreeStrBuf(&Buf);
559                 return;
560         }
561         else {
562                 Pos = ChrPtr(Buf) + 4;
563                 UL = NewUserListOneEntry(Buf, Pos);
564                 if ((UL != NULL) && havebstr("edit_abe_button")) {
565                         display_edit_address_book_entry(username, UL->UID);
566                 }
567                 else if ((UL != NULL) && havebstr("delete_button")) {
568                         delete_user(username);
569                 }
570                 else if (UL != NULL) {
571                         WCTemplputParams SubTP;
572                         memset(&SubTP, 0, sizeof(WCTemplputParams));
573                         SubTP.Filter.ContextType = CTX_USERLIST;
574                         SubTP.Context = UL;
575                         output_headers(1, 0, 0, 0, 1, 0);
576                         DoTemplate(HKEY("userlist_detailview"), NULL, &SubTP);
577                         end_burst();
578                 }
579                 DeleteUserListEntry(UL);
580                 
581         }
582         FreeStrBuf(&Buf);
583 }
584
585 /*
586  *  do the backend operation of the user edit on the server
587  */
588 void edituser(void) {
589         wcsession *WCC = WC;
590         int is_new = 0;
591         unsigned int flags = 0;
592         const char *username;
593
594         is_new = ibstr("is_new");
595         username = bstr("username");
596
597         if (!havebstr("ok_button")) {
598                 StrBufPlain(WCC->ImportantMsg, _("Changes were not saved."), -1);
599         }       
600         else {
601                 StrBuf *Buf = NewStrBuf();
602
603                 flags = ibstr("flags");
604                 if (yesbstr("inetmail")) {
605                         flags |= US_INTERNET;
606                 }
607                 else {
608                         flags &= ~US_INTERNET ;
609                 }
610
611                 if ((havebstr("newname")) && (strcasecmp(bstr("username"), bstr("newname")))) {
612                         serv_printf("RENU %s|%s", bstr("username"), bstr("newname"));
613                         StrBuf_ServGetln(Buf);
614                         if (GetServerStatus(Buf, NULL) == 2) {
615                                 FlushStrBuf(WCC->ImportantMsg);
616                                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);                             
617                         }
618                         else {
619                                 username = bstr("newname");
620                         }
621                 }
622
623                 serv_printf("ASUP %s|%s|%d|%s|%s|%s|%s|%s|%s|",
624                         username,
625                         bstr("password"),
626                         flags,
627                         bstr("timescalled"),
628                         bstr("msgsposted"),
629                         bstr("axlevel"),
630                         bstr("usernum"),
631                         bstr("lastcall"),
632                         bstr("purgedays")
633                 );
634                 StrBuf_ServGetln(Buf);
635                 if (GetServerStatus(Buf, NULL) == 2) {
636                         StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
637                 }
638                 FreeStrBuf(&Buf);
639         }
640
641         /*
642          * If we are in the middle of creating a new user, move on to
643          * the vCard edit screen.
644          */
645         if (is_new) {
646                 display_edit_address_book_entry(username, lbstr("usernum") );
647         }
648         else {
649                 select_user_to_edit(username);
650         }
651 }
652
653 /*
654  *  burge a user 
655  *  username the name of the user to remove
656  */
657 void delete_user(char *username) {
658         wcsession *WCC = WC;
659         StrBuf *Buf;
660         
661         Buf = NewStrBuf();
662         serv_printf("ASUP %s|0|0|0|0|0|", username);
663         StrBuf_ServGetln(Buf);
664         if (GetServerStatus(Buf, NULL) != 2) 
665                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
666
667         select_user_to_edit( bstr("username"));
668         FreeStrBuf(&Buf);
669 }
670                 
671
672
673 /*
674  *  create a new user
675  * take the web environment username and create it on the citadel server
676  */
677 void create_user(void) {
678         wcsession *WCC = WC;
679         long FullState;
680         StrBuf *Buf;
681         const char *username;
682
683         Buf = NewStrBuf();
684         username = bstr("username");
685         serv_printf("CREU %s", username);
686         StrBuf_ServGetln(Buf);
687         if (GetServerStatus(Buf, &FullState) == 2) {
688                 sprintf(WC->ImportantMessage, _("A new user has been created."));
689                 display_edituser(username, 1);
690         }
691         else if (FullState == 570) {
692                 StrBufPlain(WCC->ImportantMsg, 
693                             _("You are attempting to create a new user from within Citadel "
694                               "while running in host based authentication mode.  In this mode, "
695                               "you must create new users on the host system, not within Citadel."), 
696                             0);
697                 select_user_to_edit(NULL);
698         }
699         else {
700                 StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
701                 select_user_to_edit(NULL);
702         }
703         FreeStrBuf(&Buf);
704 }
705
706
707 void _select_user_to_edit(void) {
708         select_user_to_edit(NULL);
709 }
710
711
712 void _display_edituser(void) {
713         display_edituser(NULL, 0);
714 }
715
716
717 void 
718 InitModule_USEREDIT
719 (void)
720 {
721         WebcitAddUrlHandler(HKEY("select_user_to_edit"), _select_user_to_edit, 0);
722         WebcitAddUrlHandler(HKEY("display_edituser"), _display_edituser, 0);
723         WebcitAddUrlHandler(HKEY("edituser"), edituser, 0);
724         WebcitAddUrlHandler(HKEY("create_user"), create_user, 0);
725
726         RegisterNamespace("USERLIST:USERNAME",      0, 1, tmplput_USERLIST_UserName, CTX_USERLIST);
727         RegisterNamespace("USERLIST:PASSWD",        0, 1, tmplput_USERLIST_Password, CTX_USERLIST);
728         RegisterNamespace("USERLIST:ACCLVLNO",      0, 0, tmplput_USERLIST_AccessLevelNo, CTX_USERLIST);
729         RegisterNamespace("USERLIST:ACCLVLSTR",     0, 0, tmplput_USERLIST_AccessLevelStr, CTX_USERLIST);
730         RegisterNamespace("USERLIST:UID",           0, 0, tmplput_USERLIST_UID, CTX_USERLIST);
731         RegisterNamespace("USERLIST:LASTLOGON:STR", 0, 0, tmplput_USERLIST_LastLogonStr, CTX_USERLIST);
732         RegisterNamespace("USERLIST:LASTLOGON:NO",  0, 0, tmplput_USERLIST_LastLogonNo, CTX_USERLIST);
733         RegisterNamespace("USERLIST:NLOGONS",       0, 0, tmplput_USERLIST_nLogons, CTX_USERLIST);
734         RegisterNamespace("USERLIST:NPOSTS",        0, 0, tmplput_USERLIST_nPosts, CTX_USERLIST);
735                                                     
736         RegisterNamespace("USERLIST:FLAGS",         0, 0, tmplput_USERLIST_Flags, CTX_USERLIST);
737         RegisterNamespace("USERLIST:DAYSTILLPURGE", 0, 0, tmplput_USERLIST_DaysTillPurge, CTX_USERLIST);
738
739         RegisterConditional(HKEY("COND:USERNAME"),  0,    ConditionalUser, CTX_USERLIST);
740         RegisterConditional(HKEY("COND:USERACCESS"), 0,   ConditionalUserAccess, CTX_USERLIST);
741         RegisterConditional(HKEY("COND:USERLIST:FLAG:USE_INTERNET"), 0, ConditionalFlagINetEmail, CTX_USERLIST);
742
743         RegisterIterator("USERLIST", 0, NULL, iterate_load_userlist, NULL, DeleteHash, CTX_USERLIST, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
744         
745
746
747         RegisterSortFunc(HKEY("user:name"),
748                          HKEY("userlist"),
749                          CompareUserListName,
750                          CompareUserListNameRev,
751                          GroupchangeUserListName,
752                          CTX_USERLIST);
753         RegisterSortFunc(HKEY("user:accslvl"),
754                          HKEY("userlist"),
755                          CompareAccessLevel,
756                          CompareAccessLevelRev,
757                          GroupchangeAccessLevel,
758                          CTX_USERLIST);
759
760         RegisterSortFunc(HKEY("user:nlogons"),
761                          HKEY("userlist"),
762                          ComparenLogons,
763                          ComparenLogonsRev,
764                          GroupchangenLogons,
765                          CTX_USERLIST);
766
767         RegisterSortFunc(HKEY("user:uid"),
768                          HKEY("userlist"),
769                          CompareUID,
770                          CompareUIDRev,
771                          GroupchangeUID,
772                          CTX_USERLIST);
773
774         RegisterSortFunc(HKEY("user:lastlogon"),
775                          HKEY("userlist"),
776                          CompareLastLogon,
777                          CompareLastLogonRev,
778                          GroupchangeLastLogon,
779                          CTX_USERLIST);
780
781         RegisterSortFunc(HKEY("user:nmsgposts"),
782                          HKEY("userlist"),
783                          ComparenPosts,
784                          ComparenPostsRev,
785                          GroupchangenPosts,
786                          CTX_USERLIST);
787
788 }