* implemented a postfix tcp-dict table, first draft.
[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[7];
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 && strlen(reg) > 0) {
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[SIZ];
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[SIZ];
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[SIZ];
234         char desc[151];
235         char buf[SIZ];
236         char tbuf[SIZ];
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[SIZ];
286         char buf[SIZ];
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[SIZ];
312         char desc[151];
313         char buf[SIZ];
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; a < strlen(flnm); ++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[SIZ];
417         char buf[SIZ];
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 (strlen(resp));
454                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
455         } else {
456                 scr_printf("%s\n%s\n", user, &cmd[4]);
457         }
458         if (resp) free(resp);
459
460         if (do_validate) {
461                 /* now set the access level */
462                 while(1) {
463                         sprintf(answer, "%d", ax);
464                         strprompt("New access level (? for help, q to quit)",
465                                 answer, 1);
466                         if ((answer[0] >= '0') && (answer[0] <= '6')) {
467                                 ax = atoi(answer);
468                                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
469                                 if (r / 100 != 2)
470                                 scr_printf("%s\n\n", cmd);
471                                 return(0);
472                         }
473                         if (tolower(answer[0]) == 'q') {
474                                 scr_printf("*** Aborted.\n\n");
475                                 return(1);
476                         }
477                         if (answer[0] == '?') {
478                                 scr_printf("Available access levels:\n");
479                                 for (a=0; a<7; ++a) {
480                                         scr_printf("%d - %s\n",
481                                                 a, axdefs[a]);
482                                 }
483                         }
484                 }
485         }
486         return(0);
487 }
488
489
490 void validate(CtdlIPC *ipc)
491 {                               /* validate new users */
492         char cmd[SIZ];
493         char buf[SIZ];
494         int finished = 0;
495         int r;                          /* IPC response code */
496
497         do {
498                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
499                 if (r / 100 != 3)
500                         finished = 1;
501                 if (r / 100 == 2)
502                         scr_printf("%s\n", cmd);
503                 if (r / 100 == 3) {
504                         extract_token(buf, cmd, 0, '|', sizeof buf);
505                         if (val_user(ipc, buf, 1) != 0) finished = 1;
506                 }
507         } while (finished == 0);
508 }
509
510 void subshell(void)
511 {
512         int a, b;
513
514         screen_reset();
515         stty_ctdl(SB_RESTORE);
516         a = fork();
517         if (a == 0) {
518                 signal(SIGINT, SIG_DFL);
519                 signal(SIGQUIT, SIG_DFL);
520                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
521                 err_printf("Could not open a shell: %s\n", strerror(errno));
522                 exit(errno);
523         }
524         do {
525                 b = ka_wait(NULL);
526         } while ((a != b) && (a != (-1)));
527         stty_ctdl(0);
528         screen_set();
529 }
530
531 /*
532  * <.A>ide <F>ile <D>elete command
533  */
534 void deletefile(CtdlIPC *ipc)
535 {
536         char filename[32];
537         char buf[SIZ];
538
539         newprompt("Filename: ", filename, 31);
540         if (strlen(filename) == 0)
541                 return;
542         CtdlIPCDeleteFile(ipc, filename, buf);
543         err_printf("%s\n", buf);
544 }
545
546 /*
547  * <.A>ide <F>ile <S>end command
548  */
549 void netsendfile(CtdlIPC *ipc)
550 {
551         char filename[32], destsys[20], buf[SIZ];
552
553         newprompt("Filename: ", filename, 31);
554         if (strlen(filename) == 0)
555                 return;
556         newprompt("System to send to: ", destsys, 19);
557         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
558         err_printf("%s\n", buf);
559         return;
560 }
561
562 /*
563  * <.A>ide <F>ile <M>ove command
564  */
565 void movefile(CtdlIPC *ipc)
566 {
567         char filename[64];
568         char newroom[ROOMNAMELEN];
569         char buf[SIZ];
570
571         newprompt("Filename: ", filename, 63);
572         if (strlen(filename) == 0)
573                 return;
574         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
575         CtdlIPCMoveFile(ipc, filename, newroom, buf);
576         err_printf("%s\n", buf);
577 }
578
579
580 /* 
581  * list of users who have filled out a bio
582  */
583 void list_bio(CtdlIPC *ipc)
584 {
585         char buf[SIZ];
586         char *resp = NULL;
587         int pos = 1;
588         int r;                  /* IPC response code */
589
590         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
591         if (r / 100 != 1) {
592                 pprintf("%s\n", buf);
593                 return;
594         }
595         while (strlen(resp)) {
596                 extract_token(buf, resp, 0, '\n', sizeof buf);
597                 remove_token(resp, 0, '\n');
598                 if ((pos + strlen(buf) + 5) > screenwidth) {
599                         pprintf("\n");
600                         pos = 1;
601                 }
602                 pprintf("%s, ", buf);
603                 pos = pos + strlen(buf) + 2;
604         }
605         pprintf("%c%c  \n\n", 8, 8);
606         if (resp) free(resp);
607 }
608
609
610 /*
611  * read bio
612  */
613 void read_bio(CtdlIPC *ipc)
614 {
615         char who[SIZ];
616         char buf[SIZ];
617         char *resp = NULL;
618         int r;                  /* IPC response code */
619
620         do {
621                 newprompt("Read bio for who ('?' for list) : ", who, 25);
622                 pprintf("\n");
623                 if (!strcmp(who, "?"))
624                         list_bio(ipc);
625         } while (!strcmp(who, "?"));
626
627         r = CtdlIPCGetBio(ipc, who, &resp, buf);
628         if (r / 100 != 1) {
629                 pprintf("%s\n", buf);
630                 return;
631         }
632         while (strlen(resp)) {
633                 extract_token(buf, resp, 0, '\n', sizeof buf);
634                 remove_token(resp, 0, '\n');
635                 pprintf("%s\n", buf);
636         }
637         if (resp) free(resp);
638 }
639
640
641 /* 
642  * General system configuration command
643  */
644 void do_system_configuration(CtdlIPC *ipc)
645 {
646
647 #define NUM_CONFIGS 51
648
649         char buf[SIZ];
650         char sc[NUM_CONFIGS][256];
651         char *resp = NULL;
652         struct ExpirePolicy *site_expirepolicy = NULL;
653         struct ExpirePolicy *mbx_expirepolicy = NULL;
654         int a;
655         int logpages = 0;
656         int r;                  /* IPC response code */
657         int server_configs = 0;
658
659         /* Clear out the config buffers */
660         memset(&sc[0][0], 0, sizeof(sc));
661
662         /* Fetch the current config */
663         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
664         if (r / 100 == 1) {
665                 server_configs = num_tokens(resp, '\n');
666                 for (a=0; a<server_configs; ++a) {
667                         if (a < NUM_CONFIGS) {
668                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
669                         }
670                 }
671         }
672         if (resp) free(resp);
673         resp = NULL;
674         /* Fetch the expire policy (this will silently fail on old servers,
675          * resulting in "default" policy)
676          */
677         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
678         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
679
680         /* Identification parameters */
681
682         strprompt("Node name", &sc[0][0], 15);
683         strprompt("Fully qualified domain name", &sc[1][0], 63);
684         strprompt("Human readable node name", &sc[2][0], 20);
685         strprompt("Telephone number", &sc[3][0], 15);
686         strprompt("Geographic location of this system", &sc[12][0], 31);
687         strprompt("Name of system administrator", &sc[13][0], 25);
688         strprompt("Paginator prompt", &sc[10][0], 79);
689
690         /* Security parameters */
691
692         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
693                 "Require registration for new users",
694                 atoi(&sc[7][0]))));
695         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
696                 "Disable self-service user account creation",
697                 atoi(&sc[29][0]))));
698         strprompt("Initial access level for new users", &sc[6][0], 1);
699         strprompt("Access level required to create rooms", &sc[19][0], 1);
700         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
701                 "Automatically give room aide privs to a user who creates a private room",
702                 atoi(&sc[4][0]))));
703
704         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
705                 "Automatically move problem user messages to twit room",
706                 atoi(&sc[8][0]))));
707
708         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
709         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
710                 "Restrict Internet mail to only those with that privilege",
711                 atoi(&sc[11][0]))));
712         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
713                 "Allow Aides to Zap (forget) rooms",
714                 atoi(&sc[26][0]))));
715
716         if (strlen(&sc[18][0]) > 0) logpages = 1;
717         else logpages = 0;
718         logpages = boolprompt("Log all pages", logpages);
719         if (logpages) {
720                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
721         }
722         else {
723                 sc[18][0] = 0;
724         }
725
726
727         /* Server tuning */
728
729         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
730         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
731         strprompt("Maximum message length", &sc[20][0], 20);
732         strprompt("Minimum number of worker threads", &sc[21][0], 3);
733         strprompt("Maximum number of worker threads", &sc[22][0], 3);
734         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
735                 "Automatically delete committed database logs",
736                 atoi(&sc[43][0]))));
737
738         strprompt("Server IP address (0.0.0.0 for 'any')", &sc[37][0], 15);
739         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
740         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
741         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
742         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
743         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
744         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
745         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
746         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
747
748         /* This logic flips the question around, because it's one of those
749          * situations where 0=yes and 1=no
750          */
751         a = atoi(sc[25]);
752         a = (a ? 0 : 1);
753         a = boolprompt("Correct forged From: lines during authenticated SMTP",
754                 a);
755         a = (a ? 0 : 1);
756         snprintf(sc[25], sizeof sc[25], "%d", a);
757         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
758                 "Allow unauthenticated SMTP clients to spoof my domains",
759                 atoi(&sc[45][0]))));
760         snprintf(sc[44], sizeof sc[44], "%d", (boolprompt(
761                 "Instantly expunge deleted IMAP messages",
762                 atoi(&sc[44][0]))));
763
764         /* LDAP settings */
765         if (ipc->ServInfo.supports_ldap) {
766                 a = strlen(&sc[32][0]);
767                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
768                 a = boolprompt("Connect this Citadel to an LDAP directory", a);
769                 if (a) {
770                         strprompt("Host name of LDAP server",
771                                 &sc[32][0], 127);
772                         strprompt("Port number of LDAP service",
773                                 &sc[33][0], 5);
774                         strprompt("Base DN", &sc[34][0], 255);
775                         strprompt("Bind DN", &sc[35][0], 255);
776                         strprompt("Password for bind DN", &sc[36][0], 255);
777                 }
778                 else {
779                         strcpy(&sc[32][0], "");
780                 }
781         }
782
783         /* Expiry settings */
784         strprompt("Default user purge time (days)", &sc[16][0], 5);
785         strprompt("Default room purge time (days)", &sc[17][0], 5);
786
787         /* Angels and demons dancing in my head... */
788         do {
789                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
790                 strprompt("System default message expire policy (? for list)",
791                           buf, 1);
792                 if (buf[0] == '?') {
793                         scr_printf("\n"
794                                 "1. Never automatically expire messages\n"
795                                 "2. Expire by message count\n"
796                                 "3. Expire by message age\n");
797                 }
798         } while ((buf[0] < '1') || (buf[0] > '3'));
799         site_expirepolicy->expire_mode = buf[0] - '0';
800
801         /* ...lunatics and monsters underneath my bed */
802         if (site_expirepolicy->expire_mode == 2) {
803                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
804                 strprompt("Keep how many messages online?", buf, 10);
805                 site_expirepolicy->expire_value = atol(buf);
806         }
807         if (site_expirepolicy->expire_mode == 3) {
808                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
809                 strprompt("Keep messages for how many days?", buf, 10);
810                 site_expirepolicy->expire_value = atol(buf);
811         }
812
813         /* Media messiahs preying on my fears... */
814         do {
815                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
816                 strprompt("Mailbox default message expire policy (? for list)",
817                           buf, 1);
818                 if (buf[0] == '?') {
819                         scr_printf("\n"
820                                 "0. Go with the system default\n"
821                                 "1. Never automatically expire messages\n"
822                                 "2. Expire by message count\n"
823                                 "3. Expire by message age\n");
824                 }
825         } while ((buf[0] < '0') || (buf[0] > '3'));
826         mbx_expirepolicy->expire_mode = buf[0] - '0';
827
828         /* ...Pop culture prophets playing in my ears */
829         if (mbx_expirepolicy->expire_mode == 2) {
830                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
831                 strprompt("Keep how many messages online?", buf, 10);
832                 mbx_expirepolicy->expire_value = atol(buf);
833         }
834         if (mbx_expirepolicy->expire_mode == 3) {
835                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
836                 strprompt("Keep messages for how many days?", buf, 10);
837                 mbx_expirepolicy->expire_value = atol(buf);
838         }
839
840         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
841         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
842         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
843                 "Enable full text search index (warning: resource intensive)",
844                 atoi(&sc[42][0]))));
845
846         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
847                 "Perform journaling of email messages",
848                 atoi(&sc[46][0]))));
849         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
850                 "Perform journaling of non-email messages",
851                 atoi(&sc[47][0]))));
852         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
853                 strprompt("Email destination of journalized messages",
854                         &sc[48][0], 127);
855         }
856
857         /* Save it */
858         scr_printf("Save this configuration? ");
859         if (yesno()) {
860                 r = 1;
861                 for (a = 0; a < NUM_CONFIGS; a++)
862                         r += 1 + strlen(sc[a]);
863                 resp = (char *)calloc(1, r);
864                 if (!resp) {
865                         err_printf("Can't save config - out of memory!\n");
866                         logoff(ipc, 1);
867                 }
868                 for (a = 0; a < NUM_CONFIGS; a++) {
869                         strcat(resp, sc[a]);
870                         strcat(resp, "\n");
871                 }
872                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
873                 if (r / 100 != 4) {
874                         err_printf("%s\n", buf);
875                 }
876                 free(resp);
877
878                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
879                 if (r / 100 != 2) {
880                         err_printf("%s\n", buf);
881                 }
882
883                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
884                 if (r / 100 != 2) {
885                         err_printf("%s\n", buf);
886                 }
887
888         }
889 }
890
891
892 /*
893  * support function for do_internet_configuration()
894  */
895 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
896         int sel;
897
898         keyopt(" <1> localhost      (Alias for this computer)\n");
899         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
900         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
901         keyopt(" <4> directory      (Consult the Global Address Book)\n");
902         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
903         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
904         sel = intprompt("Which one", 1, 1, 6);
905         switch(sel) {
906                 case 1: strcpy(buf, "localhost");
907                         return;
908                 case 2: strcpy(buf, "gatewaydomain");
909                         return;
910                 case 3: strcpy(buf, "smarthost");
911                         return;
912                 case 4: strcpy(buf, "directory");
913                         return;
914                 case 5: strcpy(buf, "spamassassin");
915                         return;
916                 case 6: strcpy(buf, "rbl");
917                         return;
918         }
919 }
920
921
922 /*
923  * Internet mail configuration
924  */
925 void do_internet_configuration(CtdlIPC *ipc)
926 {
927         char buf[SIZ];
928         char *resp = NULL;
929         int num_recs = 0;
930         char **recs = NULL;
931         char ch;
932         int badkey;
933         int i, j;
934         int quitting = 0;
935         int modified = 0;
936         int r;
937         
938         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
939         if (r / 100 == 1) {
940                 while (strlen(resp)) {
941                         extract_token(buf, resp, 0, '\n', sizeof buf);
942                         remove_token(resp, 0, '\n');
943                         ++num_recs;
944                         if (num_recs == 1) recs = malloc(sizeof(char *));
945                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
946                         recs[num_recs-1] = malloc(strlen(buf) + 1);
947                         strcpy(recs[num_recs-1], buf);
948                 }
949         }
950         if (resp) free(resp);
951
952         do {
953                 scr_printf("\n");
954                 color(BRIGHT_WHITE);
955                 scr_printf("###                    Host or domain                     Record type      \n");
956                 color(DIM_WHITE);
957                 scr_printf("--- -------------------------------------------------- --------------------\n");
958                 for (i=0; i<num_recs; ++i) {
959                 color(DIM_WHITE);
960                 scr_printf("%3d ", i+1);
961                 extract_token(buf, recs[i], 0, '|', sizeof buf);
962                 color(BRIGHT_CYAN);
963                 scr_printf("%-50s ", buf);
964                 extract_token(buf, recs[i], 1, '|', sizeof buf);
965                 color(BRIGHT_MAGENTA);
966                 scr_printf("%-20s\n", buf);
967                 color(DIM_WHITE);
968                 }
969
970                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
971                 switch(ch) {
972                         case 'a':
973                                 newprompt("Enter host name: ",
974                                         buf, 50);
975                                 striplt(buf);
976                                 if (strlen(buf) > 0) {
977                                         ++num_recs;
978                                         if (num_recs == 1)
979                                                 recs = malloc(sizeof(char *));
980                                         else recs = realloc(recs,
981                                                 (sizeof(char *)) * num_recs);
982                                         strcat(buf, "|");
983                                         get_inet_rec_type(ipc,
984                                                         &buf[strlen(buf)]);
985                                         recs[num_recs-1] = strdup(buf);
986                                 }
987                                 modified = 1;
988                                 break;
989                         case 'd':
990                                 i = intprompt("Delete which one",
991                                         1, 1, num_recs) - 1;
992                                 free(recs[i]);
993                                 --num_recs;
994                                 for (j=i; j<num_recs; ++j)
995                                         recs[j] = recs[j+1];
996                                 modified = 1;
997                                 break;
998                         case 's':
999                                 r = 1;
1000                                 for (i = 0; i < num_recs; i++)
1001                                         r += 1 + strlen(recs[i]);
1002                                 resp = (char *)calloc(1, r);
1003                                 if (!resp) {
1004                                         err_printf("Can't save config - out of memory!\n");
1005                                         logoff(ipc, 1);
1006                                 }
1007                                 if (num_recs) for (i = 0; i < num_recs; i++) {
1008                                         strcat(resp, recs[i]);
1009                                         strcat(resp, "\n");
1010                                 }
1011                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
1012                                 if (r / 100 != 4) {
1013                                         err_printf("%s\n", buf);
1014                                 } else {
1015                                         scr_printf("Wrote %d records.\n", num_recs);
1016                                         modified = 0;
1017                                 }
1018                                 break;
1019                         case 'q':
1020                                 quitting = !modified || boolprompt(
1021                                         "Quit without saving", 0);
1022                                 break;
1023                         default:
1024                                 badkey = 1;
1025                 }
1026         } while (!quitting);
1027
1028         if (recs != NULL) {
1029                 for (i=0; i<num_recs; ++i) free(recs[i]);
1030                 free(recs);
1031         }
1032 }
1033
1034
1035
1036 /*
1037  * Edit network configuration for room sharing, mailing lists, etc.
1038  */
1039 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
1040 {
1041         char filename[PATH_MAX];
1042         char changefile[PATH_MAX];
1043         int e_ex_code;
1044         pid_t editor_pid;
1045         int cksum;
1046         int b, i, tokens;
1047         char buf[SIZ];
1048         char instr[SIZ];
1049         char addr[SIZ];
1050         FILE *tempfp;
1051         FILE *changefp;
1052         char *listing = NULL;
1053         int r;
1054
1055         if (strlen(editor_paths[0]) == 0) {
1056                 scr_printf("You must have an external editor configured in"
1057                         " order to use this function.\n");
1058                 return;
1059         }
1060
1061         CtdlMakeTempFileName(filename, sizeof filename);
1062         CtdlMakeTempFileName(changefile, sizeof changefile);
1063
1064         tempfp = fopen(filename, "w");
1065         if (tempfp == NULL) {
1066                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
1067                 return;
1068         }
1069
1070         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
1071         fprintf(tempfp, "# %s\n", comment);
1072         fprintf(tempfp, "# Specify one per line.\n"
1073                         "\n\n");
1074
1075         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1076         if (r / 100 == 1) {
1077                 while(listing && strlen(listing)) {
1078                         extract_token(buf, listing, 0, '\n', sizeof buf);
1079                         remove_token(listing, 0, '\n');
1080                         extract_token(instr, buf, 0, '|', sizeof instr);
1081                         if (!strcasecmp(instr, entrytype)) {
1082                                 tokens = num_tokens(buf, '|');
1083                                 for (i=1; i<tokens; ++i) {
1084                                         extract_token(addr, buf, i, '|', sizeof addr);
1085                                         fprintf(tempfp, "%s", addr);
1086                                         if (i < (tokens-1)) {
1087                                                 fprintf(tempfp, "|");
1088                                         }
1089                                 }
1090                                 fprintf(tempfp, "\n");
1091                         }
1092                 }
1093         }
1094         if (listing) {
1095                 free(listing);
1096                 listing = NULL;
1097         }
1098         fclose(tempfp);
1099
1100         e_ex_code = 1;  /* start with a failed exit code */
1101         screen_reset();
1102         stty_ctdl(SB_RESTORE);
1103         editor_pid = fork();
1104         cksum = file_checksum(filename);
1105         if (editor_pid == 0) {
1106                 chmod(filename, 0600);
1107                 putenv("WINDOW_TITLE=Network configuration");
1108                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
1109                 exit(1);
1110         }
1111         if (editor_pid > 0) {
1112                 do {
1113                         e_ex_code = 0;
1114                         b = ka_wait(&e_ex_code);
1115                 } while ((b != editor_pid) && (b >= 0));
1116         editor_pid = (-1);
1117         stty_ctdl(0);
1118         screen_set();
1119         }
1120
1121         if (file_checksum(filename) == cksum) {
1122                 err_printf("*** Not saving changes.\n");
1123                 e_ex_code = 1;
1124         }
1125
1126         if (e_ex_code == 0) {           /* Save changes */
1127                 changefp = fopen(changefile, "w");
1128                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1129                 if (r / 100 == 1) {
1130                         while(listing && strlen(listing)) {
1131                                 extract_token(buf, listing, 0, '\n', sizeof buf);
1132                                 remove_token(listing, 0, '\n');
1133                                 extract_token(instr, buf, 0, '|', sizeof instr);
1134                                 if (strcasecmp(instr, entrytype)) {
1135                                         fprintf(changefp, "%s\n", buf);
1136                                 }
1137                         }
1138                 }
1139                 if (listing) {
1140                         free(listing);
1141                         listing = NULL;
1142                 }
1143                 tempfp = fopen(filename, "r");
1144                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1145                         for (i=0; i<strlen(buf); ++i) {
1146                                 if (buf[i] == '#') buf[i] = 0;
1147                         }
1148                         striplt(buf);
1149                         if (strlen(buf) > 0) {
1150                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1151                         }
1152                 }
1153                 fclose(tempfp);
1154                 fclose(changefp);
1155
1156                 /* now write it to the server... */
1157                 changefp = fopen(changefile, "r");
1158                 if (changefp != NULL) {
1159                         listing = load_message_from_file(changefp);
1160                         if (listing) {
1161                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1162                                 free(listing);
1163                                 listing = NULL;
1164                         }
1165                         fclose(changefp);
1166                 }
1167         }
1168
1169         unlink(filename);               /* Delete the temporary files */
1170         unlink(changefile);
1171 }
1172
1173
1174 /*
1175  * IGnet node configuration
1176  */
1177 void do_ignet_configuration(CtdlIPC *ipc) {
1178         char buf[SIZ];
1179         int num_recs = 0;
1180         char **recs = NULL;
1181         char ch;
1182         int badkey;
1183         int i, j;
1184         int quitting = 0;
1185         int modified = 0;
1186         char *listing = NULL;
1187         int r;
1188
1189         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
1190         if (r / 100 == 1) while (*listing && strlen(listing)) {
1191                 extract_token(buf, listing, 0, '\n', sizeof buf);
1192                 remove_token(listing, 0, '\n');
1193
1194                 ++num_recs;
1195                 if (num_recs == 1) recs = malloc(sizeof(char *));
1196                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1197                 recs[num_recs-1] = malloc(SIZ);
1198                 strcpy(recs[num_recs-1], buf);
1199         }
1200         if (listing) free(listing);
1201
1202         do {
1203                 scr_printf("\n");
1204                 color(BRIGHT_WHITE);
1205                 scr_printf(     "### "
1206                         "   Node          "
1207                         "  Secret           "
1208                         "          Host or IP             "
1209                         "Port#\n");
1210                 color(DIM_WHITE);
1211                 scr_printf(     "--- "
1212                         "---------------- "
1213                         "------------------ "
1214                         "-------------------------------- "
1215                         "-----\n");
1216                 for (i=0; i<num_recs; ++i) {
1217                 color(DIM_WHITE);
1218                 scr_printf("%3d ", i+1);
1219                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1220                 color(BRIGHT_CYAN);
1221                 scr_printf("%-16s ", buf);
1222                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1223                 color(BRIGHT_MAGENTA);
1224                 scr_printf("%-18s ", buf);
1225                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1226                 color(BRIGHT_CYAN);
1227                 scr_printf("%-32s ", buf);
1228                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1229                 color(BRIGHT_MAGENTA);
1230                 scr_printf("%-3s\n", buf);
1231                 color(DIM_WHITE);
1232                 }
1233                 scr_printf("\n");
1234
1235                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1236                 switch(ch) {
1237                         case 'a':
1238                                 ++num_recs;
1239                                 if (num_recs == 1)
1240                                         recs = malloc(sizeof(char *));
1241                                 else recs = realloc(recs,
1242                                         (sizeof(char *)) * num_recs);
1243                                 newprompt("Enter node name    : ", buf, 16);
1244                                 strcat(buf, "|");
1245                                 newprompt("Enter shared secret: ",
1246                                         &buf[strlen(buf)], 18);
1247                                 strcat(buf, "|");
1248                                 newprompt("Enter host or IP   : ",
1249                                         &buf[strlen(buf)], 32);
1250                                 strcat(buf, "|504");
1251                                 strprompt("Enter port number  : ",
1252                                         &buf[strlen(buf)-3], 5);
1253                                 recs[num_recs-1] = strdup(buf);
1254                                 modified = 1;
1255                                 break;
1256                         case 'd':
1257                                 i = intprompt("Delete which one",
1258                                         1, 1, num_recs) - 1;
1259                                 free(recs[i]);
1260                                 --num_recs;
1261                                 for (j=i; j<num_recs; ++j)
1262                                         recs[j] = recs[j+1];
1263                                 modified = 1;
1264                                 break;
1265                         case 's':
1266                                 r = 1;
1267                                 for (i = 0; i < num_recs; ++i)
1268                                         r += 1 + strlen(recs[i]);
1269                                 listing = (char*) calloc(1, r);
1270                                 if (!listing) {
1271                                         err_printf("Can't save config - out of memory!\n");
1272                                         logoff(ipc, 1);
1273                                 }
1274                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1275                                         strcat(listing, recs[i]);
1276                                         strcat(listing, "\n");
1277                                 }
1278                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
1279                                 if (r / 100 != 4) {
1280                                         scr_printf("%s\n", buf);
1281                                 } else {
1282                                         scr_printf("Wrote %d records.\n", num_recs);
1283                                         modified = 0;
1284                                 }
1285                                 break;
1286                         case 'q':
1287                                 quitting = !modified || boolprompt(
1288                                         "Quit without saving", 0);
1289                                 break;
1290                         default:
1291                                 badkey = 1;
1292                 }
1293         } while (!quitting);
1294
1295         if (recs != NULL) {
1296                 for (i=0; i<num_recs; ++i) free(recs[i]);
1297                 free(recs);
1298         }
1299 }
1300
1301 /*
1302  * Filter list configuration
1303  */
1304 void do_filterlist_configuration(CtdlIPC *ipc)
1305 {
1306         char buf[SIZ];
1307         int num_recs = 0;
1308         char **recs = NULL;
1309         char ch;
1310         int badkey;
1311         int i, j;
1312         int quitting = 0;
1313         int modified = 0;
1314         char *listing = NULL;
1315         int r;
1316
1317         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
1318         if (r / 100 == 1) while (*listing && strlen(listing)) {
1319                 extract_token(buf, listing, 0, '\n', sizeof buf);
1320                 remove_token(listing, 0, '\n');
1321
1322                 ++num_recs;
1323                 if (num_recs == 1) recs = malloc(sizeof(char *));
1324                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1325                 recs[num_recs-1] = malloc(SIZ);
1326                 strcpy(recs[num_recs-1], buf);
1327         }
1328         if (listing) free(listing);
1329
1330         do {
1331                 scr_printf("\n");
1332                 color(BRIGHT_WHITE);
1333                 scr_printf(     "### "
1334                         "         User name           "
1335                         "         Room name           "
1336                         "    Node name    "
1337                         "\n");
1338                 color(DIM_WHITE);
1339                 scr_printf(     "--- "
1340                         "---------------------------- "
1341                         "---------------------------- "
1342                         "---------------- "
1343                         "\n");
1344                 for (i=0; i<num_recs; ++i) {
1345                 color(DIM_WHITE);
1346                 scr_printf("%3d ", i+1);
1347                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1348                 color(BRIGHT_CYAN);
1349                 scr_printf("%-28s ", buf);
1350                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1351                 color(BRIGHT_MAGENTA);
1352                 scr_printf("%-28s ", buf);
1353                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1354                 color(BRIGHT_CYAN);
1355                 scr_printf("%-16s\n", buf);
1356                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1357                 color(DIM_WHITE);
1358                 }
1359
1360                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1361                 switch(ch) {
1362                         case 'a':
1363                                 ++num_recs;
1364                                 if (num_recs == 1)
1365                                         recs = malloc(sizeof(char *));
1366                                 else recs = realloc(recs,
1367                                         (sizeof(char *)) * num_recs);
1368                                 newprompt("Enter user name: ", buf, 28);
1369                                 strcat(buf, "|");
1370                                 newprompt("Enter room name: ",
1371                                         &buf[strlen(buf)], 28);
1372                                 strcat(buf, "|");
1373                                 newprompt("Enter node name: ",
1374                                         &buf[strlen(buf)], 16);
1375                                 strcat(buf, "|");
1376                                 recs[num_recs-1] = strdup(buf);
1377                                 modified = 1;
1378                                 break;
1379                         case 'd':
1380                                 i = intprompt("Delete which one",
1381                                         1, 1, num_recs) - 1;
1382                                 free(recs[i]);
1383                                 --num_recs;
1384                                 for (j=i; j<num_recs; ++j)
1385                                         recs[j] = recs[j+1];
1386                                 modified = 1;
1387                                 break;
1388                         case 's':
1389                                 r = 1;
1390                                 for (i = 0; i < num_recs; ++i)
1391                                         r += 1 + strlen(recs[i]);
1392                                 listing = (char*) calloc(1, r);
1393                                 if (!listing) {
1394                                         err_printf("Can't save config - out of memory!\n");
1395                                         logoff(ipc, 1);
1396                                 }
1397                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1398                                         strcat(listing, recs[i]);
1399                                         strcat(listing, "\n");
1400                                 }
1401                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
1402                                 if (r / 100 != 4) {
1403                                         scr_printf("%s\n", buf);
1404                                 } else {
1405                                         scr_printf("Wrote %d records.\n", num_recs);
1406                                         modified = 0;
1407                                 }
1408                                 break;
1409                         case 'q':
1410                                 quitting = !modified || boolprompt(
1411                                         "Quit without saving", 0);
1412                                 break;
1413                         default:
1414                                 badkey = 1;
1415                 }
1416         } while (!quitting);
1417
1418         if (recs != NULL) {
1419                 for (i=0; i<num_recs; ++i) free(recs[i]);
1420                 free(recs);
1421         }
1422 }
1423
1424