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