The 'master user' account and password may now
[citadel.git] / citadel / routines2.c
1 /* $Id$
2  *
3  * More client-side support functions.
4  * Unlike routines.c, some of these DO use global variables.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <limits.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include "sysdep.h"
34 #include "citadel.h"
35 #include "citadel_ipc.h"
36 #include "citadel_decls.h"
37 #include "routines2.h"
38 #include "routines.h"
39 #include "commands.h"
40 #include "tools.h"
41 #include "messages.h"
42 #ifndef HAVE_SNPRINTF
43 #include "snprintf.h"
44 #endif
45 #include "screen.h"
46
47 /* work around solaris include files */
48 #ifdef reg
49 #undef reg
50 #endif
51
52 extern char temp[];
53 extern char tempdir[];
54 extern char *axdefs[8];
55 extern long highest_msg_read;
56 extern long maxmsgnum;
57 extern unsigned room_flags;
58 extern int screenwidth;
59
60
61 /*
62 int eopen(char *name, int mode)
63 {
64         int ret;
65         ret = open(name, mode);
66         if (ret < 0) {
67                 err_printf("Cannot open file '%s', mode=%d, errno=%d\n",
68                         name, mode, errno);
69                 interr(errno);
70         }
71         return (ret);
72 }
73 */
74
75
76 int room_prompt(unsigned int qrflags)
77 {                               /* return proper room prompt character */
78         int a;
79         a = '>';
80         if (qrflags & QR_DIRECTORY)
81                 a = ']';
82         if ((a == ']') && (qrflags & QR_NETWORK))
83                 a = '}';
84         if ((a == '>') && (qrflags & QR_NETWORK))
85                 a = ')';
86         return (a);
87 }
88
89 void entregis(CtdlIPC *ipc)
90 {                               /* register with name and address */
91
92         char buf[SIZ];
93         char tmpname[30];
94         char tmpaddr[25];
95         char tmpcity[15];
96         char tmpstate[3];
97         char tmpzip[11];
98         char tmpphone[15];
99         char tmpemail[SIZ];
100         char tmpcountry[32];
101         char diruser[256];
102         char dirnode[256];
103         char holdemail[SIZ];
104         char *reg = NULL;
105         int ok = 0;
106         int r;                          /* IPC response code */
107
108         strcpy(tmpname, "");
109         strcpy(tmpaddr, "");
110         strcpy(tmpcity, "");
111         strcpy(tmpstate, "");
112         strcpy(tmpzip, "");
113         strcpy(tmpphone, "");
114         strcpy(tmpemail, "");
115         strcpy(tmpcountry, "");
116
117         r = CtdlIPCGetUserRegistration(ipc, NULL, &reg, buf);
118         if (r / 100 == 1) {
119                 int a = 0;
120
121                 while (reg && !IsEmptyStr(reg)) {
122
123                         extract_token(buf, reg, 0, '\n', sizeof buf);
124                         remove_token(reg, 0, '\n');
125
126                         if (a == 2)
127                                 safestrncpy(tmpname, buf, sizeof tmpname);
128                         else if (a == 3)
129                                 safestrncpy(tmpaddr, buf, sizeof tmpaddr);
130                         else if (a == 4)
131                                 safestrncpy(tmpcity, buf, sizeof tmpcity);
132                         else if (a == 5)
133                                 safestrncpy(tmpstate, buf, sizeof tmpstate);
134                         else if (a == 6)
135                                 safestrncpy(tmpzip, buf, sizeof tmpzip);
136                         else if (a == 7)
137                                 safestrncpy(tmpphone, buf, sizeof tmpphone);
138                         else if (a == 9)
139                                 safestrncpy(tmpemail, buf, sizeof tmpemail);
140                         else if (a == 10)
141                                 safestrncpy(tmpcountry, buf, sizeof tmpcountry);
142                         ++a;
143                 }
144         }
145         strprompt("REAL name", tmpname, 29);
146         strprompt("Address", tmpaddr, 24);
147         strprompt("City/town", tmpcity, 14);
148         strprompt("State/province", tmpstate, 2);
149         strprompt("ZIP/Postal Code", tmpzip, 10);
150         strprompt("Country", tmpcountry, 31);
151         strprompt("Telephone number", tmpphone, 14);
152
153         do {
154                 ok = 1;
155                 safestrncpy(holdemail, tmpemail, sizeof holdemail);
156                 strprompt("Email address", tmpemail, 31);
157                 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
158                 if (r / 100 == 2) {
159                         extract_token(diruser, buf, 0, '@', sizeof diruser);
160                         extract_token(dirnode, buf, 1, '@', sizeof dirnode);
161                         striplt(diruser);
162                         striplt(dirnode);
163                         if ((strcasecmp(diruser, fullname))
164                            || (strcasecmp(dirnode, ipc->ServInfo.nodename))) {
165                                 scr_printf(
166                                         "\nYou can't use %s as your address.\n",
167                                         tmpemail);
168                                 scr_printf(
169                                         "It is already in use by %s @ %s.\n",
170                                         diruser, dirnode);
171                                 ok = 0;
172                                 safestrncpy(tmpemail, holdemail, sizeof tmpemail);
173                         }
174                 }
175         } while (ok == 0);
176
177         /* now send the registration info back to the server */
178         reg = (char *)realloc(reg, SIZ);
179         if (reg) {
180                 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
181                         tmpname, tmpaddr, tmpcity, tmpstate,
182                         tmpzip, tmpphone, tmpemail, tmpcountry);
183                 r = CtdlIPCSetRegistration(ipc, reg, buf);
184                 if (r / 100 != 4)
185                         scr_printf("%s\n", buf);
186                 free(reg);
187         }
188         scr_printf("\n");
189 }
190
191 void updatels(CtdlIPC *ipc)
192 {                               /* make all messages old in current room */
193         char buf[256];
194         int r;                          /* IPC response code */
195
196         if (rc_alt_semantics) {
197                 if (maxmsgnum == 0 && highest_msg_read == 0) {
198                         return;
199                 }
200                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
201                                  maxmsgnum : highest_msg_read, buf);
202         } else {
203                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
204                                  maxmsgnum : highest_msg_read, buf);
205 /*              r = CtdlIPCSetLastRead(ipc, maxmsgnum, buf); */
206 /* This is a quick-and-dirty fix to all msgs becoming new in Mail>.
207  * It will need to be rethought when messages.c is rewritten.
208  */
209         }
210         if (r / 100 != 2)
211                 scr_printf("%s\n", buf);
212 }
213
214 /*
215  * only make messages old in this room that have been read
216  */
217 void updatelsa(CtdlIPC *ipc)
218 {
219         char buf[256];
220         int r;                          /* IPC response code */
221
222         r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
223         if (r / 100 != 2)
224                 scr_printf("%s\n", &buf[4]);
225 }
226
227
228 /*
229  * client-based uploads (for users with their own clientware)
230  */
231 void cli_upload(CtdlIPC *ipc)
232 {
233         char flnm[PATH_MAX];
234         char desc[151];
235         char buf[256];
236         char tbuf[256];
237         int r;          /* IPC response code */
238         int a;
239         int fd;
240
241         if ((room_flags & QR_UPLOAD) == 0) {
242                 scr_printf("*** You cannot upload to this room.\n");
243                 return;
244         }
245         newprompt("File to be uploaded: ", flnm, 55);
246         fd = open(flnm, O_RDONLY);
247         if (fd < 0) {
248                 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
249                 return;
250         }
251         scr_printf("Enter a description of this file:\n");
252         newprompt(": ", desc, 75);
253
254         /* Keep generating filenames in hope of finding a unique one */
255         a = 0;
256         while (a < 10) {
257                 /* basename of filename */
258                 strcpy(tbuf, flnm);
259                 if (haschar(tbuf, '/'))
260                         extract_token(tbuf, flnm,
261                                 num_tokens(tbuf, '/') - 1,
262                                 '/', sizeof tbuf
263                         );
264                 /* filename.1, filename.2, etc */
265                 if (a > 0) {
266                         sprintf(&tbuf[strlen(tbuf)], ".%d", a);
267                 }
268                 /* Try upload */
269                 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
270                 if (r / 100 == 5 || r < 0)
271                         scr_printf("%s\n", buf);
272                 else
273                         break;
274                 ++a;
275         }
276         if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
277 }
278
279
280 /*
281  * Function used for various image upload commands
282  */
283 void cli_image_upload(CtdlIPC *ipc, char *keyname)
284 {
285         char flnm[PATH_MAX];
286         char buf[256];
287         int r;
288
289         /* Can we upload this image? */
290         r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
291         if (r / 100 != 2) {
292                 err_printf("%s\n", buf);
293                 return;
294         }
295         newprompt("Image file to be uploaded: ", flnm, 55);
296         r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
297         if (r / 100 == 5) {
298                 err_printf("%s\n", buf);
299         } else if (r < 0) {
300                 err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
301         }
302         /* else upload succeeded */
303 }
304
305
306 /*
307  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
308  */
309 void upload(CtdlIPC *ipc, int c)
310 {                               /* c = upload mode */
311         char flnm[PATH_MAX];
312         char desc[151];
313         char buf[256];
314         char tbuf[4096];
315         int xfer_pid;
316         int a, b;
317         FILE *fp, *lsfp;
318         int r;
319
320         if ((room_flags & QR_UPLOAD) == 0) {
321                 scr_printf("*** You cannot upload to this room.\n");
322                 return;
323         }
324         /* we don't need a filename when receiving batch y/z modem */
325         if ((c == 2) || (c == 3))
326                 strcpy(flnm, "x");
327         else
328                 newprompt("Enter filename: ", flnm, 15);
329
330         for (a = 0; !IsEmptyStr(&flnm[a]); ++a)
331                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
332                     || (flnm[a] == '?') || (flnm[a] == '*')
333                     || (flnm[a] == ';') || (flnm[a] == '&'))
334                         flnm[a] = '_';
335
336         /* create a temporary directory... */
337         if (mkdir(tempdir, 0700) != 0) {
338                 scr_printf("*** Could not create temporary directory %s: %s\n",
339                        tempdir, strerror(errno));
340                 return;
341         }
342         /* now do the transfer ... in a separate process */
343         xfer_pid = fork();
344         if (xfer_pid == 0) {
345                 chdir(tempdir);
346                 switch (c) {
347                 case 0:
348                         stty_ctdl(0);
349                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
350                         fp = fopen(flnm, "w");
351                         do {
352                                 b = inkey();
353                                 if (b == 13) {
354                                         b = 10;
355                                 }
356                                 if (b != 4) {
357                                         scr_printf("%c", b);
358                                         putc(b, fp);
359                                 }
360                         } while (b != 4);
361                         fclose(fp);
362                         exit(0);
363                 case 1:
364                         screen_reset();
365                         stty_ctdl(3);
366                         execlp("rx", "rx", flnm, NULL);
367                         exit(1);
368                 case 2:
369                         screen_reset();
370                         stty_ctdl(3);
371                         execlp("rb", "rb", NULL);
372                         exit(1);
373                 case 3:
374                         screen_reset();
375                         stty_ctdl(3);
376                         execlp("rz", "rz", NULL);
377                         exit(1);
378                 }
379         } else
380                 do {
381                         b = ka_wait(&a);
382                 } while ((b != xfer_pid) && (b != (-1)));
383         stty_ctdl(0);
384         screen_set();
385
386         if (a != 0) {
387                 scr_printf("\r*** Transfer unsuccessful.\n");
388                 nukedir(tempdir);
389                 return;
390         }
391         scr_printf("\r*** Transfer successful.\n");
392         snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
393         lsfp = popen(buf, "r");
394         if (lsfp != NULL) {
395                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
396                         flnm[strlen(flnm) - 1] = 0;     /* chop newline */
397                         snprintf(buf, sizeof buf,
398                                  "Enter a short description of '%s':\n: ",
399                                  flnm);
400                         newprompt(buf, desc, 150);
401                         snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
402                         r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
403                         scr_printf("%s\n", tbuf);
404                 }
405                 pclose(lsfp);
406         }
407         nukedir(tempdir);
408 }
409
410 /* 
411  * validate a user (returns 0 for successful validation, nonzero if quitting)
412  */
413 int val_user(CtdlIPC *ipc, char *user, int do_validate)
414 {
415         int a;
416         char cmd[256];
417         char buf[256];
418         char *resp = NULL;
419         int ax = 0;
420         char answer[2];
421         int r;                          /* IPC response code */
422
423         scr_printf("\n");
424         r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
425         if (r / 100 == 1) {
426                 a = 0;
427                 do {
428                         extract_token(buf, resp, 0, '\n', sizeof buf);
429                         remove_token(resp, 0, '\n');
430                         ++a;
431                         if (a == 1)
432                                 scr_printf("User #%s - %s  ", buf, cmd);
433                         if (a == 2)
434                                 scr_printf("PW: %s\n", buf);
435                         if (a == 3)
436                                 scr_printf("%s\n", buf);
437                         if (a == 4)
438                                 scr_printf("%s\n", buf);
439                         if (a == 5)
440                                 scr_printf("%s, ", buf);
441                         if (a == 6)
442                                 scr_printf("%s ", buf);
443                         if (a == 7)
444                                 scr_printf("%s\n", buf);
445                         if (a == 8)
446                                 scr_printf("%s\n", buf);
447                         if (a == 9)
448                                 ax = atoi(buf);
449                         if (a == 10)
450                                 scr_printf("%s\n", buf);
451                         if (a == 11)
452                                 scr_printf("%s\n", buf);
453                 } while (!IsEmptyStr(resp));
454
455 /* TODODRW: discrepancy here. Parts of the code refer to axdefs[7] as the highest
456  * but most of it limits it to axdefs[6].
457  * Webcit limits to 6 as does the code here but there are 7 in axdefs.h
458  */
459                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
460         } else {
461                 scr_printf("%s\n%s\n", user, &cmd[4]);
462         }
463         if (resp) free(resp);
464
465         if (do_validate) {
466                 /* now set the access level */
467                 while(1) {
468                         sprintf(answer, "%d", ax);
469                         strprompt("New access level (? for help, q to quit)",
470                                 answer, 1);
471                         if ((answer[0] >= '0') && (answer[0] <= '6')) {
472                                 ax = atoi(answer);
473                                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
474                                 if (r / 100 != 2)
475                                 scr_printf("%s\n\n", cmd);
476                                 return(0);
477                         }
478                         if (tolower(answer[0]) == 'q') {
479                                 scr_printf("*** Aborted.\n\n");
480                                 return(1);
481                         }
482                         if (answer[0] == '?') {
483                                 scr_printf("Available access levels:\n");
484                                 for (a=0; a<7; ++a) {
485                                         scr_printf("%d - %s\n",
486                                                 a, axdefs[a]);
487                                 }
488                         }
489                 }
490         }
491         return(0);
492 }
493
494
495 void validate(CtdlIPC *ipc)
496 {                               /* validate new users */
497         char cmd[256];
498         char buf[256];
499         int finished = 0;
500         int r;                          /* IPC response code */
501
502         do {
503                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
504                 if (r / 100 != 3)
505                         finished = 1;
506                 if (r / 100 == 2)
507                         scr_printf("%s\n", cmd);
508                 if (r / 100 == 3) {
509                         extract_token(buf, cmd, 0, '|', sizeof buf);
510                         if (val_user(ipc, buf, 1) != 0) finished = 1;
511                 }
512         } while (finished == 0);
513 }
514
515 void subshell(void)
516 {
517         int a, b;
518
519         screen_reset();
520         stty_ctdl(SB_RESTORE);
521         a = fork();
522         if (a == 0) {
523                 signal(SIGINT, SIG_DFL);
524                 signal(SIGQUIT, SIG_DFL);
525                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
526                 err_printf("Could not open a shell: %s\n", strerror(errno));
527                 exit(errno);
528         }
529         do {
530                 b = ka_wait(NULL);
531         } while ((a != b) && (a != (-1)));
532         stty_ctdl(0);
533         screen_set();
534 }
535
536 /*
537  * <.A>ide <F>ile <D>elete command
538  */
539 void deletefile(CtdlIPC *ipc)
540 {
541         char filename[32];
542         char buf[256];
543
544         newprompt("Filename: ", filename, 31);
545         if (IsEmptyStr(filename))
546                 return;
547         CtdlIPCDeleteFile(ipc, filename, buf);
548         err_printf("%s\n", buf);
549 }
550
551 /*
552  * <.A>ide <F>ile <S>end command
553  */
554 void netsendfile(CtdlIPC *ipc)
555 {
556         char filename[32], destsys[20], buf[256];
557
558         newprompt("Filename: ", filename, 31);
559         if (IsEmptyStr(filename))
560                 return;
561         newprompt("System to send to: ", destsys, 19);
562         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
563         err_printf("%s\n", buf);
564         return;
565 }
566
567 /*
568  * <.A>ide <F>ile <M>ove command
569  */
570 void movefile(CtdlIPC *ipc)
571 {
572         char filename[64];
573         char newroom[ROOMNAMELEN];
574         char buf[256];
575
576         newprompt("Filename: ", filename, 63);
577         if (IsEmptyStr(filename))
578                 return;
579         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
580         CtdlIPCMoveFile(ipc, filename, newroom, buf);
581         err_printf("%s\n", buf);
582 }
583
584
585 /* 
586  * list of users who have filled out a bio
587  */
588 void list_bio(CtdlIPC *ipc)
589 {
590         char buf[256];
591         char *resp = NULL;
592         int pos = 1;
593         int r;                  /* IPC response code */
594
595         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
596         if (r / 100 != 1) {
597                 pprintf("%s\n", buf);
598                 return;
599         }
600         while (resp && !IsEmptyStr(resp)) {
601                 extract_token(buf, resp, 0, '\n', sizeof buf);
602                 remove_token(resp, 0, '\n');
603                 if ((pos + strlen(buf) + 5) > screenwidth) {
604                         pprintf("\n");
605                         pos = 1;
606                 }
607                 pprintf("%s, ", buf);
608                 pos = pos + strlen(buf) + 2;
609         }
610         pprintf("%c%c  \n\n", 8, 8);
611         if (resp) free(resp);
612 }
613
614
615 /*
616  * read bio
617  */
618 void read_bio(CtdlIPC *ipc)
619 {
620         char who[256];
621         char buf[256];
622         char *resp = NULL;
623         int r;                  /* IPC response code */
624
625         do {
626                 newprompt("Read bio for who ('?' for list) : ", who, 25);
627                 pprintf("\n");
628                 if (!strcmp(who, "?"))
629                         list_bio(ipc);
630         } while (!strcmp(who, "?"));
631
632         r = CtdlIPCGetBio(ipc, who, &resp, buf);
633         if (r / 100 != 1) {
634                 pprintf("%s\n", buf);
635                 return;
636         }
637         while (!IsEmptyStr(resp)) {
638                 extract_token(buf, resp, 0, '\n', sizeof buf);
639                 remove_token(resp, 0, '\n');
640                 pprintf("%s\n", buf);
641         }
642         if (resp) free(resp);
643 }
644
645
646 /* 
647  * General system configuration command
648  */
649 void do_system_configuration(CtdlIPC *ipc)
650 {
651
652 #define NUM_CONFIGS 60
653
654         char buf[256];
655         char sc[NUM_CONFIGS][256];
656         char *resp = NULL;
657         struct ExpirePolicy *site_expirepolicy = NULL;
658         struct ExpirePolicy *mbx_expirepolicy = NULL;
659         int a;
660         int logpages = 0;
661         int r;                  /* IPC response code */
662         int server_configs = 0;
663
664         /* Clear out the config buffers */
665         memset(&sc[0][0], 0, sizeof(sc));
666
667         /* Fetch the current config */
668         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
669         if (r / 100 == 1) {
670                 server_configs = num_tokens(resp, '\n');
671                 for (a=0; a<server_configs; ++a) {
672                         if (a < NUM_CONFIGS) {
673                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
674                         }
675                 }
676         }
677         if (resp) free(resp);
678         resp = NULL;
679         /* Fetch the expire policy (this will silently fail on old servers,
680          * resulting in "default" policy)
681          */
682         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
683         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
684
685         /* Identification parameters */
686
687         strprompt("Node name", &sc[0][0], 15);
688         strprompt("Fully qualified domain name", &sc[1][0], 63);
689         strprompt("Human readable node name", &sc[2][0], 20);
690         strprompt("Telephone number", &sc[3][0], 15);
691         strprompt("Geographic location of this system", &sc[12][0], 31);
692         strprompt("Name of system administrator", &sc[13][0], 25);
693         strprompt("Paginator prompt", &sc[10][0], 79);
694
695         /* Security parameters */
696
697         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
698                 "Require registration for new users",
699                 atoi(&sc[7][0]))));
700         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
701                 "Disable self-service user account creation",
702                 atoi(&sc[29][0]))));
703         strprompt("Initial access level for new users", &sc[6][0], 1);
704         strprompt("Access level required to create rooms", &sc[19][0], 1);
705         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
706                 "Automatically give room aide privs to a user who creates a private room",
707                 atoi(&sc[4][0]))));
708
709         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
710                 "Automatically move problem user messages to twit room",
711                 atoi(&sc[8][0]))));
712
713         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
714         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
715                 "Restrict Internet mail to only those with that privilege",
716                 atoi(&sc[11][0]))));
717         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
718                 "Allow Aides to Zap (forget) rooms",
719                 atoi(&sc[26][0]))));
720
721         if (!IsEmptyStr(&sc[18][0])) logpages = 1;
722         else logpages = 0;
723         logpages = boolprompt("Log all pages", logpages);
724         if (logpages) {
725                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
726         }
727         else {
728                 sc[18][0] = 0;
729         }
730
731         /* Commented out because this setting isn't really appropriate to
732          * change while the server is running.
733          *
734          * snprintf(sc[52], sizeof sc[52], "%d", (boolprompt(
735          *      "Use system authentication",
736          *      atoi(&sc[52][0]))));
737          */
738
739         /* Server tuning */
740
741         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
742         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
743         strprompt("Maximum message length", &sc[20][0], 20);
744         strprompt("Minimum number of worker threads", &sc[21][0], 3);
745         strprompt("Maximum number of worker threads", &sc[22][0], 3);
746         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
747                 "Automatically delete committed database logs",
748                 atoi(&sc[43][0]))));
749
750         strprompt("Server IP address (0.0.0.0 for 'any')", &sc[37][0], 15);
751         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
752         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
753         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
754         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
755         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
756         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
757         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
758         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
759         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
760         /* This logic flips the question around, because it's one of those
761          * situations where 0=yes and 1=no
762          */
763         a = atoi(sc[25]);
764         a = (a ? 0 : 1);
765         a = boolprompt("Correct forged From: lines during authenticated SMTP",
766                 a);
767         a = (a ? 0 : 1);
768         snprintf(sc[25], sizeof sc[25], "%d", a);
769         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
770                 "Allow unauthenticated SMTP clients to spoof my domains",
771                 atoi(&sc[45][0]))));
772         snprintf(sc[57], sizeof sc[57], "%d", (boolprompt(
773                 "Perform RBL checks at greeting instead of after RCPT",
774                 atoi(&sc[57][0]))));
775         snprintf(sc[44], sizeof sc[44], "%d", (boolprompt(
776                 "Instantly expunge deleted IMAP messages",
777                 atoi(&sc[44][0]))));
778
779         /* LDAP settings */
780         if (ipc->ServInfo.supports_ldap) {
781                 a = strlen(&sc[32][0]);
782                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
783                 a = boolprompt("Connect this Citadel to an external LDAP directory", a);
784                 if (a) {
785                         strprompt("Host name of LDAP server",
786                                 &sc[32][0], 127);
787                         strprompt("Port number of LDAP service",
788                                 &sc[33][0], 5);
789                         strprompt("Base DN", &sc[34][0], 255);
790                         strprompt("Bind DN", &sc[35][0], 255);
791                         strprompt("Password for bind DN", &sc[36][0], 255);
792                 }
793                 else {
794                         strcpy(&sc[32][0], "");
795                 }
796         }
797
798         /* Expiry settings */
799         strprompt("Default user purge time (days)", &sc[16][0], 5);
800         strprompt("Default room purge time (days)", &sc[17][0], 5);
801
802         /* Angels and demons dancing in my head... */
803         do {
804                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
805                 strprompt("System default message expire policy (? for list)",
806                           buf, 1);
807                 if (buf[0] == '?') {
808                         scr_printf("\n"
809                                 "1. Never automatically expire messages\n"
810                                 "2. Expire by message count\n"
811                                 "3. Expire by message age\n");
812                 }
813         } while ((buf[0] < '1') || (buf[0] > '3'));
814         site_expirepolicy->expire_mode = buf[0] - '0';
815
816         /* ...lunatics and monsters underneath my bed */
817         if (site_expirepolicy->expire_mode == 2) {
818                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
819                 strprompt("Keep how many messages online?", buf, 10);
820                 site_expirepolicy->expire_value = atol(buf);
821         }
822         if (site_expirepolicy->expire_mode == 3) {
823                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
824                 strprompt("Keep messages for how many days?", buf, 10);
825                 site_expirepolicy->expire_value = atol(buf);
826         }
827
828         /* Media messiahs preying on my fears... */
829         do {
830                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
831                 strprompt("Mailbox default message expire policy (? for list)",
832                           buf, 1);
833                 if (buf[0] == '?') {
834                         scr_printf("\n"
835                                 "0. Go with the system default\n"
836                                 "1. Never automatically expire messages\n"
837                                 "2. Expire by message count\n"
838                                 "3. Expire by message age\n");
839                 }
840         } while ((buf[0] < '0') || (buf[0] > '3'));
841         mbx_expirepolicy->expire_mode = buf[0] - '0';
842
843         /* ...Pop culture prophets playing in my ears */
844         if (mbx_expirepolicy->expire_mode == 2) {
845                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
846                 strprompt("Keep how many messages online?", buf, 10);
847                 mbx_expirepolicy->expire_value = atol(buf);
848         }
849         if (mbx_expirepolicy->expire_mode == 3) {
850                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
851                 strprompt("Keep messages for how many days?", buf, 10);
852                 mbx_expirepolicy->expire_value = atol(buf);
853         }
854
855         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
856         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
857         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
858                 "Enable full text search index (warning: resource intensive)",
859                 atoi(&sc[42][0]))));
860
861         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
862                 "Perform journaling of email messages",
863                 atoi(&sc[46][0]))));
864         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
865                 "Perform journaling of non-email messages",
866                 atoi(&sc[47][0]))));
867         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
868                 strprompt("Email destination of journalized messages",
869                         &sc[48][0], 127);
870         }
871
872         /* Funambol push stuff */
873         int yes_funambol = 0;
874         if (strlen(sc[53]) > 0) yes_funambol = 1;
875         yes_funambol = boolprompt("Connect to an external Funambol sync server", yes_funambol);
876         if (yes_funambol) {
877                 strprompt("Funambol server (blank to disable)", &sc[53][0], 63);
878                 strprompt("Funambol server port", &sc[54][0], 5);
879                 strprompt("Funambol sync source", &sc[55][0], 63);
880                 strprompt("Funambol authentication details (user:pass in Base64)", &sc[56][0],63);
881         }
882         else {
883                 sc[53][0] = 0;
884                 sc[54][0] = 0;
885                 sc[55][0] = 0;
886                 sc[56][0] = 0;
887         }
888
889         /* Master user account */
890         int yes_muacct = 0;
891         if (strlen(sc[58]) > 0) yes_muacct = 1;
892         yes_muacct = boolprompt("Enable a 'master user' account", yes_muacct);
893         if (yes_muacct) {
894                 strprompt("Master user name", &sc[58][0], 31);
895                 strprompt("Master user password", &sc[59][0], -31);
896         }
897         else {
898                 strcpy(&sc[58][0], "");
899                 strcpy(&sc[59][0], "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
900         }
901
902         /* Save it */
903         scr_printf("Save this configuration? ");
904         if (yesno()) {
905                 r = 1;
906                 for (a = 0; a < NUM_CONFIGS; a++)
907                         r += 1 + strlen(sc[a]);
908                 resp = (char *)calloc(1, r);
909                 if (!resp) {
910                         err_printf("Can't save config - out of memory!\n");
911                         logoff(ipc, 1);
912                 }
913                 for (a = 0; a < NUM_CONFIGS; a++) {
914                         strcat(resp, sc[a]);
915                         strcat(resp, "\n");
916                 }
917                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
918                 if (r / 100 != 4) {
919                         err_printf("%s\n", buf);
920                 }
921                 free(resp);
922
923                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
924                 if (r / 100 != 2) {
925                         err_printf("%s\n", buf);
926                 }
927
928                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
929                 if (r / 100 != 2) {
930                         err_printf("%s\n", buf);
931                 }
932
933         }
934     if (site_expirepolicy) free(site_expirepolicy);
935     if (mbx_expirepolicy) free(mbx_expirepolicy);
936 }
937
938
939 /*
940  * support function for do_internet_configuration()
941  */
942 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
943         int sel;
944
945         keyopt(" <1> localhost      (Alias for this computer)\n");
946         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
947         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
948         keyopt(" <4> directory      (Consult the Global Address Book)\n");
949         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
950         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
951         keyopt(" <7> masq domains   (Domains as which users are allowed to masquerade)\n");
952         sel = intprompt("Which one", 1, 1, 6);
953         switch(sel) {
954                 case 1: strcpy(buf, "localhost");
955                         return;
956                 case 2: strcpy(buf, "gatewaydomain");
957                         return;
958                 case 3: strcpy(buf, "smarthost");
959                         return;
960                 case 4: strcpy(buf, "directory");
961                         return;
962                 case 5: strcpy(buf, "spamassassin");
963                         return;
964                 case 6: strcpy(buf, "rbl");
965                         return;
966                 case 7: strcpy(buf, "masqdomain");
967                         return;
968         }
969 }
970
971
972 /*
973  * Internet mail configuration
974  */
975 void do_internet_configuration(CtdlIPC *ipc)
976 {
977         char buf[256];
978         char *resp = NULL;
979         int num_recs = 0;
980         char **recs = NULL;
981         char ch;
982         int badkey;
983         int i, j;
984         int quitting = 0;
985         int modified = 0;
986         int r;
987         
988         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
989         if (r / 100 == 1) {
990                 while (!IsEmptyStr(resp)) {
991                         extract_token(buf, resp, 0, '\n', sizeof buf);
992                         remove_token(resp, 0, '\n');
993                         ++num_recs;
994                         if (num_recs == 1) recs = malloc(sizeof(char *));
995                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
996                         recs[num_recs-1] = malloc(strlen(buf) + 1);
997                         strcpy(recs[num_recs-1], buf);
998                 }
999         }
1000         if (resp) free(resp);
1001
1002         do {
1003                 scr_printf("\n");
1004                 color(BRIGHT_WHITE);
1005                 scr_printf("###                    Host or domain                     Record type      \n");
1006                 color(DIM_WHITE);
1007                 scr_printf("--- -------------------------------------------------- --------------------\n");
1008                 for (i=0; i<num_recs; ++i) {
1009                 color(DIM_WHITE);
1010                 scr_printf("%3d ", i+1);
1011                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1012                 color(BRIGHT_CYAN);
1013                 scr_printf("%-50s ", buf);
1014                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1015                 color(BRIGHT_MAGENTA);
1016                 scr_printf("%-20s\n", buf);
1017                 color(DIM_WHITE);
1018                 }
1019
1020                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1021                 switch(ch) {
1022                         case 'a':
1023                                 newprompt("Enter host name: ",
1024                                         buf, 50);
1025                                 striplt(buf);
1026                                 if (!IsEmptyStr(buf)) {
1027                                         ++num_recs;
1028                                         if (num_recs == 1)
1029                                                 recs = malloc(sizeof(char *));
1030                                         else recs = realloc(recs,
1031                                                 (sizeof(char *)) * num_recs);
1032                                         strcat(buf, "|");
1033                                         get_inet_rec_type(ipc,
1034                                                         &buf[strlen(buf)]);
1035                                         recs[num_recs-1] = strdup(buf);
1036                                 }
1037                                 modified = 1;
1038                                 break;
1039                         case 'd':
1040                                 i = intprompt("Delete which one",
1041                                         1, 1, num_recs) - 1;
1042                                 free(recs[i]);
1043                                 --num_recs;
1044                                 for (j=i; j<num_recs; ++j)
1045                                         recs[j] = recs[j+1];
1046                                 modified = 1;
1047                                 break;
1048                         case 's':
1049                                 r = 1;
1050                                 for (i = 0; i < num_recs; i++)
1051                                         r += 1 + strlen(recs[i]);
1052                                 resp = (char *)calloc(1, r);
1053                                 if (!resp) {
1054                                         err_printf("Can't save config - out of memory!\n");
1055                                         logoff(ipc, 1);
1056                                 }
1057                                 if (num_recs) for (i = 0; i < num_recs; i++) {
1058                                         strcat(resp, recs[i]);
1059                                         strcat(resp, "\n");
1060                                 }
1061                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
1062                                 if (r / 100 != 4) {
1063                                         err_printf("%s\n", buf);
1064                                 } else {
1065                                         scr_printf("Wrote %d records.\n", num_recs);
1066                                         modified = 0;
1067                                 }
1068                 free(resp);
1069                                 break;
1070                         case 'q':
1071                                 quitting = !modified || boolprompt(
1072                                         "Quit without saving", 0);
1073                                 break;
1074                         default:
1075                                 badkey = 1;
1076                 }
1077         } while (!quitting);
1078
1079         if (recs != NULL) {
1080                 for (i=0; i<num_recs; ++i) free(recs[i]);
1081                 free(recs);
1082         }
1083 }
1084
1085
1086
1087 /*
1088  * Edit network configuration for room sharing, mailing lists, etc.
1089  */
1090 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
1091 {
1092         char filename[PATH_MAX];
1093         char changefile[PATH_MAX];
1094         int e_ex_code;
1095         pid_t editor_pid;
1096         int cksum;
1097         int b, i, tokens;
1098         char buf[1024];
1099         char instr[1024];
1100         char addr[1024];
1101         FILE *tempfp;
1102         FILE *changefp;
1103         char *listing = NULL;
1104         int r;
1105
1106         if (IsEmptyStr(editor_paths[0])) {
1107                 scr_printf("You must have an external editor configured in"
1108                         " order to use this function.\n");
1109                 return;
1110         }
1111
1112         CtdlMakeTempFileName(filename, sizeof filename);
1113         CtdlMakeTempFileName(changefile, sizeof changefile);
1114
1115         tempfp = fopen(filename, "w");
1116         if (tempfp == NULL) {
1117                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
1118                 return;
1119         }
1120
1121         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
1122         fprintf(tempfp, "# %s\n", comment);
1123         fprintf(tempfp, "# Specify one per line.\n"
1124                         "\n\n");
1125
1126         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1127         if (r / 100 == 1) {
1128                 while(listing && !IsEmptyStr(listing)) {
1129                         extract_token(buf, listing, 0, '\n', sizeof buf);
1130                         remove_token(listing, 0, '\n');
1131                         extract_token(instr, buf, 0, '|', sizeof instr);
1132                         if (!strcasecmp(instr, entrytype)) {
1133                                 tokens = num_tokens(buf, '|');
1134                                 for (i=1; i<tokens; ++i) {
1135                                         extract_token(addr, buf, i, '|', sizeof addr);
1136                                         fprintf(tempfp, "%s", addr);
1137                                         if (i < (tokens-1)) {
1138                                                 fprintf(tempfp, "|");
1139                                         }
1140                                 }
1141                                 fprintf(tempfp, "\n");
1142                         }
1143                 }
1144         }
1145         if (listing) {
1146                 free(listing);
1147                 listing = NULL;
1148         }
1149         fclose(tempfp);
1150
1151         e_ex_code = 1;  /* start with a failed exit code */
1152         screen_reset();
1153         stty_ctdl(SB_RESTORE);
1154         editor_pid = fork();
1155         cksum = file_checksum(filename);
1156         if (editor_pid == 0) {
1157                 chmod(filename, 0600);
1158                 putenv("WINDOW_TITLE=Network configuration");
1159                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
1160                 exit(1);
1161         }
1162         if (editor_pid > 0) {
1163                 do {
1164                         e_ex_code = 0;
1165                         b = ka_wait(&e_ex_code);
1166                 } while ((b != editor_pid) && (b >= 0));
1167         editor_pid = (-1);
1168         stty_ctdl(0);
1169         screen_set();
1170         }
1171
1172         if (file_checksum(filename) == cksum) {
1173                 err_printf("*** No changes to save.\n");
1174                 e_ex_code = 1;
1175         }
1176
1177         if (e_ex_code == 0) {           /* Save changes */
1178                 changefp = fopen(changefile, "w");
1179
1180                 /* Load all netconfig entries that are *not* of the type we are editing */
1181                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1182                 if (r / 100 == 1) {
1183                         while(listing && !IsEmptyStr(listing)) {
1184                                 extract_token(buf, listing, 0, '\n', sizeof buf);
1185                                 remove_token(listing, 0, '\n');
1186                                 extract_token(instr, buf, 0, '|', sizeof instr);
1187                                 if (strcasecmp(instr, entrytype)) {
1188                                         fprintf(changefp, "%s\n", buf);
1189                                 }
1190                         }
1191                 }
1192                 if (listing) {
1193                         free(listing);
1194                         listing = NULL;
1195                 }
1196
1197                 /* ...and merge that with the data we just edited */
1198                 tempfp = fopen(filename, "r");
1199                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1200                         for (i=0; i<strlen(buf); ++i) {
1201                                 if (buf[i] == '#') buf[i] = 0;
1202                         }
1203                         striplt(buf);
1204                         if (!IsEmptyStr(buf)) {
1205                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1206                         }
1207                 }
1208                 fclose(tempfp);
1209                 fclose(changefp);
1210
1211                 /* now write it to the server... */
1212                 changefp = fopen(changefile, "r");
1213                 if (changefp != NULL) {
1214                         listing = load_message_from_file(changefp);
1215                         if (listing) {
1216                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1217                                 free(listing);
1218                                 listing = NULL;
1219                         }
1220                         fclose(changefp);
1221                 }
1222         }
1223
1224         unlink(filename);               /* Delete the temporary files */
1225         unlink(changefile);
1226 }
1227
1228
1229 /*
1230  * IGnet node configuration
1231  */
1232 void do_ignet_configuration(CtdlIPC *ipc) {
1233         char buf[SIZ];
1234         int num_recs = 0;
1235         char **recs = NULL;
1236         char ch;
1237         int badkey;
1238         int i, j;
1239         int quitting = 0;
1240         int modified = 0;
1241         char *listing = NULL;
1242         int r;
1243
1244         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
1245         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
1246                 extract_token(buf, listing, 0, '\n', sizeof buf);
1247                 remove_token(listing, 0, '\n');
1248
1249                 ++num_recs;
1250                 if (num_recs == 1) recs = malloc(sizeof(char *));
1251                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1252                 recs[num_recs-1] = malloc(SIZ);
1253                 strcpy(recs[num_recs-1], buf);
1254         }
1255         if (listing) free(listing);
1256
1257         do {
1258                 scr_printf("\n");
1259                 color(BRIGHT_WHITE);
1260                 scr_printf(     "### "
1261                         "   Node          "
1262                         "  Secret           "
1263                         "          Host or IP             "
1264                         "Port#\n");
1265                 color(DIM_WHITE);
1266                 scr_printf(     "--- "
1267                         "---------------- "
1268                         "------------------ "
1269                         "-------------------------------- "
1270                         "-----\n");
1271                 for (i=0; i<num_recs; ++i) {
1272                 color(DIM_WHITE);
1273                 scr_printf("%3d ", i+1);
1274                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1275                 color(BRIGHT_CYAN);
1276                 scr_printf("%-16s ", buf);
1277                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1278                 color(BRIGHT_MAGENTA);
1279                 scr_printf("%-18s ", buf);
1280                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1281                 color(BRIGHT_CYAN);
1282                 scr_printf("%-32s ", buf);
1283                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1284                 color(BRIGHT_MAGENTA);
1285                 scr_printf("%-3s\n", buf);
1286                 color(DIM_WHITE);
1287                 }
1288                 scr_printf("\n");
1289
1290                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1291                 switch(ch) {
1292                         case 'a':
1293                                 ++num_recs;
1294                                 if (num_recs == 1)
1295                                         recs = malloc(sizeof(char *));
1296                                 else recs = realloc(recs,
1297                                         (sizeof(char *)) * num_recs);
1298                                 newprompt("Enter node name    : ", buf, 16);
1299                                 strcat(buf, "|");
1300                                 newprompt("Enter shared secret: ",
1301                                         &buf[strlen(buf)], 18);
1302                                 strcat(buf, "|");
1303                                 newprompt("Enter host or IP   : ",
1304                                         &buf[strlen(buf)], 32);
1305                                 strcat(buf, "|504");
1306                                 strprompt("Enter port number  : ",
1307                                         &buf[strlen(buf)-3], 5);
1308                                 recs[num_recs-1] = strdup(buf);
1309                                 modified = 1;
1310                                 break;
1311                         case 'd':
1312                                 i = intprompt("Delete which one",
1313                                         1, 1, num_recs) - 1;
1314                                 free(recs[i]);
1315                                 --num_recs;
1316                                 for (j=i; j<num_recs; ++j)
1317                                         recs[j] = recs[j+1];
1318                                 modified = 1;
1319                                 break;
1320                         case 's':
1321                                 r = 1;
1322                                 for (i = 0; i < num_recs; ++i)
1323                                         r += 1 + strlen(recs[i]);
1324                                 listing = (char*) calloc(1, r);
1325                                 if (!listing) {
1326                                         err_printf("Can't save config - out of memory!\n");
1327                                         logoff(ipc, 1);
1328                                 }
1329                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1330                                         strcat(listing, recs[i]);
1331                                         strcat(listing, "\n");
1332                                 }
1333                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
1334                                 if (r / 100 != 4) {
1335                                         scr_printf("%s\n", buf);
1336                                 } else {
1337                                         scr_printf("Wrote %d records.\n", num_recs);
1338                                         modified = 0;
1339                                 }
1340                 free(listing);
1341                                 break;
1342                         case 'q':
1343                                 quitting = !modified || boolprompt(
1344                                         "Quit without saving", 0);
1345                                 break;
1346                         default:
1347                                 badkey = 1;
1348                 }
1349         } while (!quitting);
1350
1351         if (recs != NULL) {
1352                 for (i=0; i<num_recs; ++i) free(recs[i]);
1353                 free(recs);
1354         }
1355 }
1356
1357 /*
1358  * Filter list configuration
1359  */
1360 void do_filterlist_configuration(CtdlIPC *ipc)
1361 {
1362         char buf[SIZ];
1363         int num_recs = 0;
1364         char **recs = NULL;
1365         char ch;
1366         int badkey;
1367         int i, j;
1368         int quitting = 0;
1369         int modified = 0;
1370         char *listing = NULL;
1371         int r;
1372
1373         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
1374         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
1375                 extract_token(buf, listing, 0, '\n', sizeof buf);
1376                 remove_token(listing, 0, '\n');
1377
1378                 ++num_recs;
1379                 if (num_recs == 1) recs = malloc(sizeof(char *));
1380                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1381                 recs[num_recs-1] = malloc(SIZ);
1382                 strcpy(recs[num_recs-1], buf);
1383         }
1384         if (listing) free(listing);
1385
1386         do {
1387                 scr_printf("\n");
1388                 color(BRIGHT_WHITE);
1389                 scr_printf(     "### "
1390                         "         User name           "
1391                         "         Room name           "
1392                         "    Node name    "
1393                         "\n");
1394                 color(DIM_WHITE);
1395                 scr_printf(     "--- "
1396                         "---------------------------- "
1397                         "---------------------------- "
1398                         "---------------- "
1399                         "\n");
1400                 for (i=0; i<num_recs; ++i) {
1401                 color(DIM_WHITE);
1402                 scr_printf("%3d ", i+1);
1403                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1404                 color(BRIGHT_CYAN);
1405                 scr_printf("%-28s ", buf);
1406                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1407                 color(BRIGHT_MAGENTA);
1408                 scr_printf("%-28s ", buf);
1409                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1410                 color(BRIGHT_CYAN);
1411                 scr_printf("%-16s\n", buf);
1412                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1413                 color(DIM_WHITE);
1414                 }
1415
1416                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1417                 switch(ch) {
1418                         case 'a':
1419                                 ++num_recs;
1420                                 if (num_recs == 1)
1421                                         recs = malloc(sizeof(char *));
1422                                 else recs = realloc(recs,
1423                                         (sizeof(char *)) * num_recs);
1424                                 newprompt("Enter user name: ", buf, 28);
1425                                 strcat(buf, "|");
1426                                 newprompt("Enter room name: ",
1427                                         &buf[strlen(buf)], 28);
1428                                 strcat(buf, "|");
1429                                 newprompt("Enter node name: ",
1430                                         &buf[strlen(buf)], 16);
1431                                 strcat(buf, "|");
1432                                 recs[num_recs-1] = strdup(buf);
1433                                 modified = 1;
1434                                 break;
1435                         case 'd':
1436                                 i = intprompt("Delete which one",
1437                                         1, 1, num_recs) - 1;
1438                                 free(recs[i]);
1439                                 --num_recs;
1440                                 for (j=i; j<num_recs; ++j)
1441                                         recs[j] = recs[j+1];
1442                                 modified = 1;
1443                                 break;
1444                         case 's':
1445                                 r = 1;
1446                                 for (i = 0; i < num_recs; ++i)
1447                                         r += 1 + strlen(recs[i]);
1448                                 listing = (char*) calloc(1, r);
1449                                 if (!listing) {
1450                                         err_printf("Can't save config - out of memory!\n");
1451                                         logoff(ipc, 1);
1452                                 }
1453                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1454                                         strcat(listing, recs[i]);
1455                                         strcat(listing, "\n");
1456                                 }
1457                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
1458                                 if (r / 100 != 4) {
1459                                         scr_printf("%s\n", buf);
1460                                 } else {
1461                                         scr_printf("Wrote %d records.\n", num_recs);
1462                                         modified = 0;
1463                                 }
1464                 free(listing);
1465                                 break;
1466                         case 'q':
1467                                 quitting = !modified || boolprompt(
1468                                         "Quit without saving", 0);
1469                                 break;
1470                         default:
1471                                 badkey = 1;
1472                 }
1473         } while (!quitting);
1474
1475         if (recs != NULL) {
1476                 for (i=0; i<num_recs; ++i) free(recs[i]);
1477                 free(recs);
1478         }
1479 }
1480
1481