Mailing list header changes (fuck you Google)
[citadel.git] / webcit / useredit.c
1 /*
2  * Copyright (c) 1996-2020 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include "webcit.h"
14 #include "webserver.h"
15
16 CtxType CTX_USERLIST = CTX_NONE;
17 /*
18  *  show a list of available users to edit them
19  *  message the header message???
20  *  preselect = which user should be selected in the browser
21  */
22 void select_user_to_edit(const char *preselect)
23 {
24         output_headers(1, 0, 0, 0, 1, 0);
25         do_template("aide_edituser_select");
26         end_burst();
27 }
28
29
30 typedef struct _UserListEntry {
31         int UID;
32         int AccessLevel;
33         int nLogons;
34         int nPosts;
35
36         StrBuf *UserName;
37         StrBuf *Passvoid;
38         time_t LastLogonT;
39         /* Just available for Single users to view: */
40         unsigned int Flags;
41         int DaysTillPurge;
42         int HasBio;
43
44         StrBuf *PrimaryEmail;
45         StrBuf *OtherEmails;
46
47 } UserListEntry;
48
49
50 UserListEntry* NewUserListOneEntry(StrBuf *SerializedUser, const char *Pos)
51 {
52         UserListEntry *ul;
53
54         if (StrLength(SerializedUser) < 8) 
55                 return NULL;
56
57         ul = (UserListEntry*) malloc(sizeof(UserListEntry));
58         ul->UserName = NewStrBuf();
59         ul->Passvoid = NewStrBuf();
60         ul->PrimaryEmail = NewStrBuf();
61         ul->OtherEmails = NewStrBuf();
62
63         StrBufExtract_NextToken(ul->UserName,               SerializedUser, &Pos, '|');
64         StrBufExtract_NextToken(ul->Passvoid,               SerializedUser, &Pos, '|');
65         ul->Flags         = StrBufExtractNext_unsigned_long(SerializedUser, &Pos, '|');
66         ul->nLogons       = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
67         ul->nPosts        = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
68         ul->AccessLevel   = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
69         ul->UID           = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
70         ul->LastLogonT    = StrBufExtractNext_long(         SerializedUser, &Pos, '|');
71         ul->DaysTillPurge = StrBufExtractNext_int(          SerializedUser, &Pos, '|');
72         return ul;
73 }
74
75 void DeleteUserListEntry(void *vUserList)
76 {
77         UserListEntry *ul = (UserListEntry*) vUserList;
78         if (!ul) return;
79         FreeStrBuf(&ul->UserName);
80         FreeStrBuf(&ul->Passvoid);
81         FreeStrBuf(&ul->PrimaryEmail);
82         FreeStrBuf(&ul->OtherEmails);
83         free(ul);
84 }
85
86 UserListEntry* NewUserListEntry(StrBuf *SerializedUserList)
87 {
88         const char *Pos = NULL;
89         UserListEntry *ul;
90
91         if (StrLength(SerializedUserList) < 8) 
92                 return NULL;
93
94         ul = (UserListEntry*) malloc(sizeof(UserListEntry));
95         ul->UserName = NewStrBuf();
96         ul->Passvoid = NewStrBuf();
97         ul->PrimaryEmail = NewStrBuf();
98         ul->OtherEmails = NewStrBuf();
99
100         StrBufExtract_NextToken(ul->UserName,    SerializedUserList, &Pos, '|');
101         ul->AccessLevel = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
102         ul->UID         = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
103         ul->LastLogonT  = StrBufExtractNext_long(SerializedUserList, &Pos, '|');
104         ul->nLogons     = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
105         ul->nPosts      = StrBufExtractNext_int( SerializedUserList, &Pos, '|');
106         StrBufExtract_NextToken(ul->Passvoid,    SerializedUserList, &Pos, '|');
107         ul->Flags = 0;
108         ul->HasBio = 0;
109         ul->DaysTillPurge = -1;
110         return ul;
111 }
112
113 /*
114  * Sort by Username
115  */
116 int CompareUserListName(const void *vUser1, const void *vUser2)
117 {
118         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
119         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
120
121         return strcmp(ChrPtr(u1->UserName), ChrPtr(u2->UserName));
122 }
123
124 int CompareUserListNameRev(const void *vUser1, const void *vUser2)
125 {
126         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
127         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
128         return strcmp(ChrPtr(u2->UserName), ChrPtr(u1->UserName));
129 }
130
131 int GroupchangeUserListName(const void *vUser1, const void *vUser2)
132 {
133         UserListEntry *u1 = (UserListEntry*) vUser1;
134         UserListEntry *u2 = (UserListEntry*) vUser2;
135         return ChrPtr(u2->UserName)[0] != ChrPtr(u1->UserName)[0];
136 }
137
138 /*
139  * Sort by access level
140  */
141 int CompareAccessLevel(const void *vUser1, const void *vUser2)
142 {
143         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
144         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
145
146         return (u1->AccessLevel > u2->AccessLevel);
147 }
148
149 int CompareAccessLevelRev(const void *vUser1, const void *vUser2)
150 {
151         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
152         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
153
154         return (u2->AccessLevel > u1->AccessLevel);
155 }
156
157 int GroupchangeAccessLevel(const void *vUser1, const void *vUser2)
158 {
159         UserListEntry *u1 = (UserListEntry*) vUser1;
160         UserListEntry *u2 = (UserListEntry*) vUser2;
161
162         return u2->AccessLevel != u1->AccessLevel;
163 }
164
165 /*
166  * Sort by UID
167  */
168 int CompareUID(const void *vUser1, const void *vUser2)
169 {
170         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
171         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
172
173         return (u1->UID > u2->UID);
174 }
175
176 int CompareUIDRev(const void *vUser1, const void *vUser2)
177 {
178         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
179         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
180
181         return (u2->UID > u1->UID);
182 }
183
184 int GroupchangeUID(const void *vUser1, const void *vUser2)
185 {
186         UserListEntry *u1 = (UserListEntry*) vUser1;
187         UserListEntry *u2 = (UserListEntry*) vUser2;
188
189         return (u2->UID / 10) != (u1->UID / 10);
190 }
191
192 /*
193  * Sort By Date /// TODO!
194  */
195 int CompareLastLogon(const void *vUser1, const void *vUser2)
196 {
197         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
198         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
199
200         return (u1->LastLogonT > u2->LastLogonT);
201 }
202
203 int CompareLastLogonRev(const void *vUser1, const void *vUser2)
204 {
205         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
206         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
207
208         return (u2->LastLogonT > u1->LastLogonT);
209 }
210
211 int GroupchangeLastLogon(const void *vUser1, const void *vUser2)
212 {
213         UserListEntry *u1 = (UserListEntry*) vUser1;
214         UserListEntry *u2 = (UserListEntry*) vUser2;
215
216         return (u2->LastLogonT != u1->LastLogonT);
217 }
218
219 /*
220  * Sort By Number of Logons
221  */
222 int ComparenLogons(const void *vUser1, const void *vUser2)
223 {
224         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
225         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
226
227         return (u1->nLogons > u2->nLogons);
228 }
229
230 int ComparenLogonsRev(const void *vUser1, const void *vUser2)
231 {
232         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
233         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
234
235         return (u2->nLogons > u1->nLogons);
236 }
237
238 int GroupchangenLogons(const void *vUser1, const void *vUser2)
239 {
240         UserListEntry *u1 = (UserListEntry*) vUser1;
241         UserListEntry *u2 = (UserListEntry*) vUser2;
242
243         return (u2->nLogons / 100) != (u1->nLogons / 100);
244 }
245
246 /*
247  * Sort By Number of Posts
248  */
249 int ComparenPosts(const void *vUser1, const void *vUser2)
250 {
251         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
252         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
253
254         return (u1->nPosts > u2->nPosts);
255 }
256
257 int ComparenPostsRev(const void *vUser1, const void *vUser2)
258 {
259         UserListEntry *u1 = (UserListEntry*) GetSearchPayload(vUser1);
260         UserListEntry *u2 = (UserListEntry*) GetSearchPayload(vUser2);
261
262         return (u2->nPosts > u1->nPosts);
263 }
264
265 int GroupchangenPosts(const void *vUser1, const void *vUser2)
266 {
267         UserListEntry *u1 = (UserListEntry*) vUser1;
268         UserListEntry *u2 = (UserListEntry*) vUser2;
269
270         return (u2->nPosts / 100) != (u1->nPosts / 100);
271 }
272
273
274 HashList *iterate_load_userlist(StrBuf *Target, WCTemplputParams *TP)
275 {
276         int Done = 0;
277         CompareFunc SortIt;
278         HashList *Hash = NULL;
279         StrBuf *Buf;
280         UserListEntry* ul;
281         int len;
282         int UID;
283         void *vData;
284         WCTemplputParams SubTP;
285
286         memset(&SubTP, 0, sizeof(WCTemplputParams));    
287         serv_puts("LIST");
288         Buf = NewStrBuf();
289         StrBuf_ServGetln(Buf);
290         if (GetServerStatus(Buf, NULL) == 1) {
291                 Hash = NewHash(1, Flathash);
292                 Done = 0;
293                 while (!Done) {
294                         len = StrBuf_ServGetln(Buf);
295                         if ((len <0) || 
296                             ((len == 3) &&
297                              !strcmp(ChrPtr(Buf), "000")))
298                         {
299                                 Done = 1;
300                                 break;
301                         }
302                         ul = NewUserListEntry(Buf);
303                         if (ul == NULL)
304                                 continue;
305
306                         Put(Hash, IKEY(ul->UID), ul, DeleteUserListEntry); 
307                 }
308
309                 serv_puts("LBIO 1");
310                 StrBuf_ServGetln(Buf);
311                 if (GetServerStatus(Buf, NULL) == 1) {
312                         Done = 0;
313                         while (!Done) {
314                                 len = StrBuf_ServGetln(Buf);
315                                 if ((len <0) || ((len == 3) && !strcmp(ChrPtr(Buf), "000")))
316                                 {
317                                         Done = 1;
318                                         break;
319                                 }
320                         }
321                         UID = atoi(ChrPtr(Buf));
322                         if (GetHash(Hash, IKEY(UID), &vData) && vData != 0)
323                         {
324                                 ul = (UserListEntry*)vData;
325                                 ul->HasBio = 1;
326                         }
327                 }
328                 SubTP.Filter.ContextType = CTX_USERLIST;
329                 SortIt = RetrieveSort(&SubTP, HKEY("USER"), HKEY("user:uid"), 0);
330                 if (SortIt != NULL)
331                         SortByPayload(Hash, SortIt);
332                 else 
333                         SortByPayload(Hash, CompareUID);
334         }
335         FreeStrBuf(&Buf);
336         return Hash;
337 }
338
339
340 void tmplput_USERLIST_UserName(StrBuf *Target, WCTemplputParams *TP)
341 {
342         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
343         StrBufAppendTemplate(Target, TP, ul->UserName, 0);
344 }
345
346 void tmplput_USERLIST_Password(StrBuf *Target, WCTemplputParams *TP)
347 {
348         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
349         StrBufAppendTemplate(Target, TP, ul->Passvoid, 0);
350 }
351
352 void tmplput_USERLIST_PrimaryEmail(StrBuf *Target, WCTemplputParams *TP)
353 {
354         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
355         StrBufAppendTemplate(Target, TP, ul->PrimaryEmail, 0);
356 }
357
358 void tmplput_USERLIST_OtherEmails(StrBuf *Target, WCTemplputParams *TP)
359 {
360         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
361         StrBufAppendTemplate(Target, TP, ul->OtherEmails, 0);
362 }
363
364 void tmplput_USERLIST_AccessLevelNo(StrBuf *Target, WCTemplputParams *TP)
365 {
366         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
367
368         StrBufAppendPrintf(Target, "%d", ul->AccessLevel, 0);
369 }
370
371 void tmplput_USERLIST_AccessLevelStr(StrBuf *Target, WCTemplputParams *TP)
372 {
373         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
374         
375         StrBufAppendBufPlain(Target, _(axdefs[ul->AccessLevel]), -1, 0);
376 }
377
378 void tmplput_USERLIST_UID(StrBuf *Target, WCTemplputParams *TP)
379 {
380         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
381
382         StrBufAppendPrintf(Target, "%d", ul->UID, 0);
383 }
384
385
386 void tmplput_USERLIST_LastLogonNo(StrBuf *Target, WCTemplputParams *TP)
387 {
388         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
389
390         StrBufAppendPrintf(Target,"%ld", ul->LastLogonT, 0);
391 }
392
393
394 void tmplput_USERLIST_LastLogonStr(StrBuf *Target, WCTemplputParams *TP)
395 {
396         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
397         StrEscAppend(Target, NULL, asctime(localtime(&ul->LastLogonT)), 0, 0);
398 }
399
400
401 void tmplput_USERLIST_nLogons(StrBuf *Target, WCTemplputParams *TP)
402 {
403         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
404
405         StrBufAppendPrintf(Target, "%d", ul->nLogons, 0);
406 }
407
408
409 void tmplput_USERLIST_nPosts(StrBuf *Target, WCTemplputParams *TP)
410 {
411         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
412
413         StrBufAppendPrintf(Target, "%d", ul->nPosts, 0);
414 }
415
416
417 void tmplput_USERLIST_Flags(StrBuf *Target, WCTemplputParams *TP)
418 {
419         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
420
421         StrBufAppendPrintf(Target, "%d", ul->Flags, 0);
422 }
423
424
425 void tmplput_USERLIST_DaysTillPurge(StrBuf *Target, WCTemplputParams *TP)
426 {
427         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
428
429         StrBufAppendPrintf(Target, "%d", ul->DaysTillPurge, 0);
430 }
431
432
433 int ConditionalUser(StrBuf *Target, WCTemplputParams *TP)
434 {
435         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
436         if (havebstr("usernum")) {
437                 return ibstr("usernum") == ul->UID;
438         }
439         else if (havebstr("username")) {
440                 return strcmp(bstr("username"), ChrPtr(ul->UserName)) == 0;
441         }
442         else 
443                 return 0;
444 }
445
446
447 int ConditionalFlagINetEmail(StrBuf *Target, WCTemplputParams *TP)
448 {
449         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
450         return (ul->Flags & US_INTERNET) != 0;
451 }
452
453
454 int ConditionalUserAccess(StrBuf *Target, WCTemplputParams *TP)
455 {
456         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
457         
458         if (ul == NULL)
459                 return 0;
460
461         return GetTemplateTokenNumber(Target, 
462                                       TP, 
463                                       3, 
464                                       AxNewU)
465                 ==
466                 ul->AccessLevel;
467 }
468
469
470 int ConditionalHaveBIO(StrBuf *Target, WCTemplputParams *TP)
471 {
472         UserListEntry *ul = (UserListEntry*) CTX(CTX_USERLIST);
473         
474         if (ul == NULL)
475                 return 0;
476         return ul->HasBio;
477 }
478
479
480 int ConditionalSuppressEmailFields(StrBuf *Target, WCTemplputParams *TP)
481 {
482         return 0;               // FIXME this makes all email fields display
483 }
484
485
486 void tmplput_USER_BIO(StrBuf *Target, WCTemplputParams *TP)
487 {
488         int Done = 0;
489         StrBuf *Buf;
490         const char *who;
491         long len;
492
493         GetTemplateTokenString(Target, TP, 0, &who, &len);
494         if (len == 0) {
495                 who = ChrPtr(WC->wc_fullname);
496         }
497
498         Buf = NewStrBuf();
499         serv_printf("RBIO %s", who);
500         StrBuf_ServGetln(Buf);
501         if (GetServerStatus(Buf, NULL) == 1) {
502                 StrBuf *BioBuf = NewStrBufPlain(NULL, SIZ);
503                 while (!Done && StrBuf_ServGetln(Buf)>=0) {
504                         if ( (StrLength(Buf)==3) && 
505                              !strcmp(ChrPtr(Buf), "000")) 
506                                 Done = 1;
507                         else {
508                                 StrBufAppendBuf(BioBuf, Buf, 0);
509                                 StrBufAppendBufPlain(BioBuf, HKEY("\n"), 0);
510                         }
511                 }
512                 StrBufAppendTemplate(Target, TP, BioBuf, 1);
513                 FreeStrBuf(&BioBuf);
514         }
515         FreeStrBuf(&Buf);
516 }
517
518
519 int Conditional_USER_HAS_PIC(StrBuf *Target, WCTemplputParams *TP)
520 {
521         // ajc 2016apr10 this needs to be re-evaluated with the new protocol
522         return(0);
523 }
524
525
526 /*
527  *  Locate the message number of a user's vCard in the current room
528  *  Returns the message id of his vcard
529  */
530 long locate_user_vcard_in_this_room(message_summary **VCMsg, wc_mime_attachment **VCAtt)
531 {
532         wcsession *WCC = WC;
533         HashPos *at;
534         HashPos *att;
535         const char *HashKey;
536         long HKLen;
537         void *vMsg;
538         message_summary *Msg;
539         wc_mime_attachment *Att;
540         StrBuf *Buf;
541         long vcard_msgnum = (-1L);
542         int already_tried_creating_one = 0;
543         StrBuf *FoundCharset = NewStrBuf();
544         StrBuf *Error = NULL;
545         SharedMessageStatus Stat;
546
547
548         Buf = NewStrBuf();
549 TRYAGAIN:
550         memset(&Stat, 0, sizeof(SharedMessageStatus));
551         Stat.maxload = 10000;
552         Stat.lowest_found = (-1);
553         Stat.highest_found = (-1);
554         /* Search for the user's vCard */
555         if (load_msg_ptrs("MSGS ALL||||1", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0) > 0) {
556                 at = GetNewHashPos(WCC->summ, 0);
557                 while (GetNextHashPos(WCC->summ, at, &HKLen, &HashKey, &vMsg)) {
558                         Msg = (message_summary*) vMsg;          
559                         Msg->MsgBody =  (wc_mime_attachment*) malloc(sizeof(wc_mime_attachment));
560                         memset(Msg->MsgBody, 0, sizeof(wc_mime_attachment));
561                         Msg->MsgBody->msgnum = Msg->msgnum;
562
563                         load_message(Msg, FoundCharset, &Error);
564                         
565                         if (Msg->AllAttach != NULL) {
566                                 att = GetNewHashPos(Msg->AllAttach, 0);
567                                 while (GetNextHashPos(Msg->AllAttach, att, &HKLen, &HashKey, &vMsg) && 
568                                        (vcard_msgnum == -1)) {
569                                         Att = (wc_mime_attachment*) vMsg;
570                                         if (
571                                                 (strcasecmp(ChrPtr(Att->ContentType), "text/x-vcard") == 0)
572                                                 || (strcasecmp(ChrPtr(Att->ContentType), "text/vcard") == 0)
573                                         ) {
574                                                 *VCAtt = Att;
575                                                 *VCMsg = Msg;
576                                                 vcard_msgnum = Msg->msgnum;
577                                                 if (Att->Data == NULL) {
578                                                         MimeLoadData(Att);
579                                                 }
580                                         }
581                                 }
582                                 DeleteHashPos(&att);
583                         }
584                         FreeStrBuf(&Error);     /* don't care... */
585                         
586                 }
587                 DeleteHashPos(&at);             
588         }
589
590         /* If there's no vcard, create one */
591         if ((*VCMsg == NULL) && (already_tried_creating_one == 0)) {
592                 FlushStrBuf(Buf);
593                 already_tried_creating_one = 1;
594                 serv_puts("ENT0 1|||4");
595                 StrBuf_ServGetln(Buf);
596                 if (GetServerStatus(Buf, NULL) == 4) {
597                         serv_puts("Content-type: text/x-vcard");
598                         serv_puts("");
599                         serv_puts("begin:vcard");
600                         serv_puts("end:vcard");
601                         serv_puts("000");
602                 }
603                 else 
604                         syslog(LOG_WARNING, "Error while creating user vcard: %s\n", ChrPtr(Buf));
605                 goto TRYAGAIN;
606         }
607         FreeStrBuf(&Buf);
608         FreeStrBuf(&FoundCharset);
609
610         return(vcard_msgnum);
611 }
612
613
614 /*
615  *  Display the form for editing a user's address book entry
616  *  username the name of the user
617  *  usernum the citadel-uid of the user
618  */
619 void display_edit_address_book_entry(const char *username, long usernum) {
620         message_summary *VCMsg = NULL;
621         wc_mime_attachment *VCAtt = NULL;
622         StrBuf *roomname;
623         StrBuf *Buf;
624         long vcard_msgnum = (-1L);
625
626         /* Locate the user's config room, creating it if necessary */
627         Buf = NewStrBuf();
628         roomname = NewStrBuf();
629         StrBufPrintf(roomname, "%010ld.%s", usernum, USERCONFIGROOM);
630         serv_printf("GOTO %s||1", ChrPtr(roomname));
631         StrBuf_ServGetln(Buf);
632         if (GetServerStatus(Buf, NULL) != 2) {
633                 serv_printf("CRE8 1|%s|5|||1|", ChrPtr(roomname));
634                 StrBuf_ServGetln(Buf);
635                 GetServerStatus(Buf, NULL);
636                 serv_printf("GOTO %s||1", ChrPtr(roomname));
637                 StrBuf_ServGetln(Buf);
638                 if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
639                         select_user_to_edit(username);
640                         FreeStrBuf(&Buf);
641                         FreeStrBuf(&roomname);
642                         return;
643                 }
644         }
645         FreeStrBuf(&Buf);
646
647         locate_user_vcard_in_this_room(&VCMsg, &VCAtt);
648
649         if (VCMsg == NULL) {
650                 AppendImportantMessage(_("An error occurred while trying to create or edit this address book entry."), -1);
651                 select_user_to_edit(username);
652                 FreeStrBuf(&roomname);
653                 return;
654         }
655
656         do_edit_vcard(vcard_msgnum, "1", 
657                       VCMsg,
658                       VCAtt,
659                       "select_user_to_edit", 
660                       ChrPtr(roomname));
661         FreeStrBuf(&roomname);
662 }
663
664 /*
665  *  purge a user 
666  *  username the name of the user to remove
667  */
668 void delete_user(char *username) {
669         StrBuf *Buf;
670         
671         Buf = NewStrBuf();
672         serv_printf("ASUP %s|0|0|0|0|0|", username);
673         StrBuf_ServGetln(Buf);
674         GetServerStatusMsg(Buf, NULL, 1, 2);
675
676         select_user_to_edit( bstr("username"));
677         FreeStrBuf(&Buf);
678 }
679                 
680
681 void display_edituser(const char *supplied_username, int is_new) {
682         const char *Pos;
683         UserListEntry* UL;
684         StrBuf *Buf;
685         char username[256];
686         int i = 0;
687
688         if (supplied_username != NULL) {
689                 safestrncpy(username, supplied_username, sizeof username);
690         }
691         else {
692                 safestrncpy(username, bstr("username"), sizeof username);
693         }
694
695         Buf = NewStrBuf();
696         serv_printf("AGUP %s", username);
697         StrBuf_ServGetln(Buf);
698         if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
699                 select_user_to_edit(username);
700                 FreeStrBuf(&Buf);
701                 return;
702         }
703         else {
704                 Pos = ChrPtr(Buf) + 4;
705                 UL = NewUserListOneEntry(Buf, Pos);
706                 if ((UL != NULL) && havebstr("edit_abe_button")) {
707                         display_edit_address_book_entry(username, UL->UID);
708                 }
709                 else if ((UL != NULL) && havebstr("delete_button")) {
710                         delete_user(username);
711                 }
712                 else if (UL != NULL) {
713
714                         serv_printf("AGEA %s", username);
715                         StrBuf_ServGetln(Buf);
716                         if (GetServerStatusMsg(Buf, NULL, 1, 2) == 1) {
717                                 while(StrBuf_ServGetln(Buf) , strcmp(ChrPtr(Buf), "000")) {
718                                         if (i == 0) {
719                                                 StrBufAppendPrintf(UL->PrimaryEmail, "%s", ChrPtr(Buf));
720                                         }
721                                         if (i > 1) {
722                                                 StrBufAppendPrintf(UL->OtherEmails, ",");
723                                         }
724                                         if (i > 0) {
725                                                 StrBufAppendPrintf(UL->OtherEmails, "%s", ChrPtr(Buf));
726                                         }
727                                         ++i;
728                                 }
729                         }
730
731                         WCTemplputParams SubTP;
732                         memset(&SubTP, 0, sizeof(WCTemplputParams));
733                         SubTP.Filter.ContextType = CTX_USERLIST;
734                         SubTP.Context = UL;
735                         output_headers(1, 0, 0, 0, 1, 0);
736                         DoTemplate(HKEY("aide_edituser_detailview"), NULL, &SubTP);
737                         end_burst();
738                 }
739                 DeleteUserListEntry(UL);
740                 
741         }
742         FreeStrBuf(&Buf);
743 }
744
745 /*
746  *  do the backend operation of the user edit on the server
747  */
748 void edituser(void) {
749         int is_new = 0;
750         unsigned int flags = 0;
751         const char *username;
752
753         is_new = ibstr("is_new");
754         username = bstr("username");
755
756         if (!havebstr("ok_button")) {
757                 AppendImportantMessage(_("Changes were not saved."), -1);
758         }       
759         else {
760                 StrBuf *Buf = NewStrBuf();
761
762                 flags = ibstr("flags");
763                 if (yesbstr("inetmail")) {
764                         flags |= US_INTERNET;
765                 }
766                 else {
767                         flags &= ~US_INTERNET ;
768                 }
769
770                 if ((havebstr("newname")) && (strcasecmp(bstr("username"), bstr("newname")))) {
771                         serv_printf("RENU %s|%s", bstr("username"), bstr("newname"));
772                         StrBuf_ServGetln(Buf);
773                         if (GetServerStatusMsg(Buf, NULL, 1, 2) != 2) {
774                                 username = bstr("newname");
775                         }
776                 }
777
778                 /* Send the new account parameters */
779                 serv_printf("ASUP %s|%s|%d|%s|%s|%s|%s|%s|%s|",
780                         username,
781                         bstr("password"),
782                         flags,
783                         bstr("timescalled"),
784                         bstr("msgsposted"),
785                         bstr("axlevel"),
786                         bstr("usernum"),
787                         bstr("lastcall"),
788                         bstr("purgedays")
789                 );
790                 StrBuf_ServGetln(Buf);
791                 GetServerStatusMsg(Buf, NULL, 1, 2);
792
793                 /* Send the new email addresses.  First make up a delimited list... */
794                 char all_the_emails[512];
795                 snprintf(all_the_emails, sizeof all_the_emails, "%s,%s", bstr("primaryemail"), bstr("otheremails"));
796
797                 /* Replace any commas, semicolons, or spaces with newlines */
798                 char *pos;
799                 for (pos=all_the_emails; *pos!=0; ++pos) {
800                         if ((*pos == ',') || (*pos == ';') || (*pos == ' ')) *pos = '\n' ;
801                 }
802
803                 /* Remove any naughty inappropriate whitespace */
804                 striplt(all_the_emails);
805                 while (pos = strstr(all_the_emails, "\n,"), (pos != NULL)) {
806                         strcpy(pos, pos+1);
807                 }
808                 while (pos = strstr(all_the_emails, ",\n"), (pos != NULL)) {
809                         strcpy(pos+1, pos+2);
810                 }
811                 while (pos = strstr(all_the_emails, "\n\n"), (pos != NULL)) {
812                         strcpy(pos+1, pos+2);
813                 }
814
815                 /* Now send it to the server. */
816                 serv_printf("ASEA %s", username);
817                 StrBuf_ServGetln(Buf);
818                 if (GetServerStatusMsg(Buf, NULL, 1, 2) == 4) {
819                         serv_printf("%s\n000", all_the_emails);
820                 }
821
822                 FreeStrBuf(&Buf);
823         }
824
825         /*
826          * If we are in the middle of creating a new user, move on to
827          * the vCard edit screen.
828          */
829         if (is_new) {
830                 display_edit_address_book_entry(username, lbstr("usernum") );
831         }
832         else {
833                 select_user_to_edit(username);
834         }
835 }
836
837
838 /*
839  * create a new user
840  * (take the web environment username and create it on the citadel server)
841  */
842 void create_user(void) {
843         long FullState;
844         StrBuf *Buf;
845         const char *username;
846
847         Buf = NewStrBuf();
848         username = bstr("username");
849         serv_printf("CREU %s", username);
850         StrBuf_ServGetln(Buf);
851         if (GetServerStatus(Buf, &FullState) == 2) {
852                 AppendImportantMessage(_("A new user has been created."), -1);
853                 display_edituser(username, 1);
854         }
855         else if (FullState == 570) {
856                 AppendImportantMessage(_("You are attempting to create a new user from within Citadel "
857                                          "while running in host based authentication mode.  In this mode, "
858                                          "you must create new users on the host system, not within Citadel."), 
859                                        -1);
860                 select_user_to_edit(NULL);
861         }
862         else {
863                 AppendImportantMessage(ChrPtr(Buf) + 4, StrLength(Buf) - 4);
864                 select_user_to_edit(NULL);
865         }
866         FreeStrBuf(&Buf);
867 }
868
869
870 void display_userpic(void) {
871         off_t bytes;
872         StrBuf *Buf = NewStrBuf();
873         const char *username = bstr("user");
874         serv_printf("DLUI %s", username);
875         StrBuf_ServGetln(Buf);
876         if (GetServerStatus(Buf, NULL) == 6) {
877                 StrBufCutLeft(Buf, 4);
878                 bytes = StrBufExtract_long(Buf, 0, '|');
879                 StrBuf *content_type = NewStrBuf();
880                 StrBufExtract_token(content_type, Buf, 3, '|');
881                 WC->WBuf = NewStrBuf();
882                 StrBuf_ServGetBLOBBuffered(WC->WBuf, bytes);
883                 http_transmit_thing(ChrPtr(content_type), 0);
884                 FreeStrBuf(&content_type);
885         }
886         else {
887                 output_error_pic("", "");
888         }
889         FreeStrBuf(&Buf);
890 }
891
892
893 void _select_user_to_edit(void) {
894         select_user_to_edit(NULL);
895 }
896
897
898 void _display_edituser(void) {
899         display_edituser(NULL, 0);
900 }
901
902
903 void 
904 InitModule_USEREDIT
905 (void)
906 {
907         RegisterCTX(CTX_USERLIST);
908         WebcitAddUrlHandler(HKEY("select_user_to_edit"), "", 0, _select_user_to_edit, 0);
909         WebcitAddUrlHandler(HKEY("display_edituser"), "", 0, _display_edituser, 0);
910         WebcitAddUrlHandler(HKEY("edituser"), "", 0, edituser, 0);
911         WebcitAddUrlHandler(HKEY("create_user"), "", 0, create_user, 0);
912         WebcitAddUrlHandler(HKEY("userpic"), "", 0, display_userpic, 0);
913
914         RegisterNamespace("USERLIST:USERNAME",      0, 1, tmplput_USERLIST_UserName, NULL, CTX_USERLIST);
915         RegisterNamespace("USERLIST:PASSWD",        0, 1, tmplput_USERLIST_Password, NULL, CTX_USERLIST);
916         RegisterNamespace("USERLIST:ACCLVLNO",      0, 0, tmplput_USERLIST_AccessLevelNo, NULL, CTX_USERLIST);
917         RegisterNamespace("USERLIST:ACCLVLSTR",     0, 0, tmplput_USERLIST_AccessLevelStr, NULL, CTX_USERLIST);
918         RegisterNamespace("USERLIST:UID",           0, 0, tmplput_USERLIST_UID, NULL, CTX_USERLIST);
919         RegisterNamespace("USERLIST:LASTLOGON:STR", 0, 0, tmplput_USERLIST_LastLogonStr, NULL, CTX_USERLIST);
920         RegisterNamespace("USERLIST:LASTLOGON:NO",  0, 0, tmplput_USERLIST_LastLogonNo, NULL, CTX_USERLIST);
921         RegisterNamespace("USERLIST:NLOGONS",       0, 0, tmplput_USERLIST_nLogons, NULL, CTX_USERLIST);
922         RegisterNamespace("USERLIST:NPOSTS",        0, 0, tmplput_USERLIST_nPosts, NULL, CTX_USERLIST);
923         RegisterNamespace("USERLIST:PRIMARYEMAIL",  0, 1, tmplput_USERLIST_PrimaryEmail, NULL, CTX_USERLIST);
924         RegisterNamespace("USERLIST:OTHEREMAILS",   0, 1, tmplput_USERLIST_OtherEmails, NULL, CTX_USERLIST);
925         RegisterNamespace("USERLIST:FLAGS",         0, 0, tmplput_USERLIST_Flags, NULL, CTX_USERLIST);
926         RegisterNamespace("USERLIST:DAYSTILLPURGE", 0, 0, tmplput_USERLIST_DaysTillPurge, NULL, CTX_USERLIST);
927
928         RegisterNamespace("USER:BIO", 1, 2, tmplput_USER_BIO,  NULL, CTX_NONE);
929
930         RegisterConditional("COND:USERNAME",                    0,      ConditionalUser,                CTX_USERLIST);
931         RegisterConditional("COND:USERACCESS",                  0,      ConditionalUserAccess,          CTX_USERLIST);
932         RegisterConditional("COND:USERLIST:FLAG:USE_INTERNET",  0,      ConditionalFlagINetEmail,       CTX_USERLIST);
933         RegisterConditional("COND:USERLIST:HAVEBIO",            0,      ConditionalHaveBIO,             CTX_USERLIST);
934         RegisterConditional("COND:USER:PIC",                    1,      Conditional_USER_HAS_PIC,       CTX_NONE);
935
936         RegisterIterator("USERLIST", 0, NULL, iterate_load_userlist, NULL, DeleteHash, CTX_USERLIST, CTX_NONE, IT_FLAG_DETECT_GROUPCHANGE);
937
938         RegisterSortFunc(HKEY("user:name"),
939                          HKEY("userlist"),
940                          CompareUserListName,
941                          CompareUserListNameRev,
942                          GroupchangeUserListName,
943                          CTX_USERLIST);
944         RegisterSortFunc(HKEY("user:accslvl"),
945                          HKEY("userlist"),
946                          CompareAccessLevel,
947                          CompareAccessLevelRev,
948                          GroupchangeAccessLevel,
949                          CTX_USERLIST);
950
951         RegisterSortFunc(HKEY("user:nlogons"),
952                          HKEY("userlist"),
953                          ComparenLogons,
954                          ComparenLogonsRev,
955                          GroupchangenLogons,
956                          CTX_USERLIST);
957
958         RegisterSortFunc(HKEY("user:uid"),
959                          HKEY("userlist"),
960                          CompareUID,
961                          CompareUIDRev,
962                          GroupchangeUID,
963                          CTX_USERLIST);
964
965         RegisterSortFunc(HKEY("user:lastlogon"),
966                          HKEY("userlist"),
967                          CompareLastLogon,
968                          CompareLastLogonRev,
969                          GroupchangeLastLogon,
970                          CTX_USERLIST);
971
972         RegisterSortFunc(HKEY("user:nmsgposts"),
973                          HKEY("userlist"),
974                          ComparenPosts,
975                          ComparenPostsRev,
976                          GroupchangenPosts,
977                          CTX_USERLIST);
978
979         REGISTERTokenParamDefine(AxDeleted);
980         REGISTERTokenParamDefine(AxNewU);
981         REGISTERTokenParamDefine(AxProbU);
982         REGISTERTokenParamDefine(AxLocU);
983         REGISTERTokenParamDefine(AxNetU);
984         REGISTERTokenParamDefine(AxPrefU);
985         REGISTERTokenParamDefine(AxAideU);
986 }
987