* undo linebuffering, its going away under that name
[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         return(vcard_msgnum);
466 }
467
468
469 /**
470  *  Display the form for editing a user's address book entry
471  *  username the name of the user
472  *  usernum the citadel-uid of the user
473  */
474 void display_edit_address_book_entry(const char *username, long usernum) {
475         wcsession *WCC = WC;
476         message_summary *VCMsg = NULL;
477         wc_mime_attachment *VCAtt = NULL;
478         StrBuf *roomname;
479         StrBuf *Buf;
480         long vcard_msgnum = (-1L);
481
482         /** Locate the user's config room, creating it if necessary */
483         Buf = NewStrBuf();
484         roomname = NewStrBuf();
485         StrBufPrintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
486         serv_printf("GOTO %s||1", ChrPtr(roomname));
487         StrBuf_ServGetln(Buf);
488         if (GetServerStatus(Buf, NULL) != 2) {
489                 serv_printf("CRE8 1|%s|5|||1|", ChrPtr(roomname));
490                 StrBuf_ServGetln(Buf);
491                 GetServerStatus(Buf, NULL);
492                 serv_printf("GOTO %s||1", ChrPtr(roomname));
493                 StrBuf_ServGetln(Buf);
494                 if (GetServerStatus(Buf, NULL) != 2) {
495                         FlushStrBuf(WCC->ImportantMsg);
496                         StrBufAppendBuf(WCC->ImportantMsg, Buf, 4);
497                         select_user_to_edit(username);
498                         FreeStrBuf(&Buf);
499                         FreeStrBuf(&roomname);
500                         return;
501                 }
502         }
503         FreeStrBuf(&Buf);
504
505         locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
506
507         if (VCMsg == NULL) {
508                 StrBufPlain(WCC->ImportantMsg, 
509                             _("An error occurred while trying to create or edit this address book entry."), 
510                             0);
511                 select_user_to_edit(username);
512                 FreeStrBuf(&roomname);
513                 return;
514         }
515
516         do_edit_vcard(vcard_msgnum, "1", 
517                       VCMsg,
518                       VCAtt,
519                       "select_user_to_edit", 
520                       ChrPtr(roomname));
521         FreeStrBuf(&roomname);
522 }
523
524
525 void display_edituser(const char *supplied_username, int is_new) {
526         const char *Pos;
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_ServGetln(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                 Pos = ChrPtr(Buf) + 4;
551                 UL = NewUserListOneEntry(Buf, Pos);
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_ServGetln(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_ServGetln(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_ServGetln(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_ServGetln(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 }