* Holy war on strlen: use IsEmptyStr where apropriate.
[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 && !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; 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[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                 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[256];
493         char buf[256];
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[256];
538
539         newprompt("Filename: ", filename, 31);
540         if (IsEmptyStr(filename))
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[256];
552
553         newprompt("Filename: ", filename, 31);
554         if (IsEmptyStr(filename))
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[256];
570
571         newprompt("Filename: ", filename, 63);
572         if (IsEmptyStr(filename))
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[256];
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 (resp && 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[256];
616         char buf[256];
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 (!IsEmptyStr(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 58
648
649         char buf[256];
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 (!IsEmptyStr(&sc[18][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         /* Commented out because this setting isn't really appropriate to
727          * change while the server is running.
728          *
729          * snprintf(sc[52], sizeof sc[52], "%d", (boolprompt(
730          *      "Use system authentication",
731          *      atoi(&sc[52][0]))));
732          */
733
734         /* Server tuning */
735
736         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
737         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
738         strprompt("Maximum message length", &sc[20][0], 20);
739         strprompt("Minimum number of worker threads", &sc[21][0], 3);
740         strprompt("Maximum number of worker threads", &sc[22][0], 3);
741         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
742                 "Automatically delete committed database logs",
743                 atoi(&sc[43][0]))));
744
745         strprompt("Server IP address (0.0.0.0 for 'any')", &sc[37][0], 15);
746         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
747         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
748         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
749         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
750         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
751         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
752         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
753         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
754         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
755         /* This logic flips the question around, because it's one of those
756          * situations where 0=yes and 1=no
757          */
758         a = atoi(sc[25]);
759         a = (a ? 0 : 1);
760         a = boolprompt("Correct forged From: lines during authenticated SMTP",
761                 a);
762         a = (a ? 0 : 1);
763         snprintf(sc[25], sizeof sc[25], "%d", a);
764         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
765                 "Allow unauthenticated SMTP clients to spoof my domains",
766                 atoi(&sc[45][0]))));
767         snprintf(sc[57], sizeof sc[57], "%d", (boolprompt(
768                 "Perform RBL checks at greeting instead of after RCPT",
769                 atoi(&sc[57][0]))));
770         snprintf(sc[44], sizeof sc[44], "%d", (boolprompt(
771                 "Instantly expunge deleted IMAP messages",
772                 atoi(&sc[44][0]))));
773
774         /* LDAP settings */
775         if (ipc->ServInfo.supports_ldap) {
776                 a = strlen(&sc[32][0]);
777                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
778                 a = boolprompt("Connect this Citadel to an LDAP directory", a);
779                 if (a) {
780                         strprompt("Host name of LDAP server",
781                                 &sc[32][0], 127);
782                         strprompt("Port number of LDAP service",
783                                 &sc[33][0], 5);
784                         strprompt("Base DN", &sc[34][0], 255);
785                         strprompt("Bind DN", &sc[35][0], 255);
786                         strprompt("Password for bind DN", &sc[36][0], 255);
787                 }
788                 else {
789                         strcpy(&sc[32][0], "");
790                 }
791         }
792
793         /* Expiry settings */
794         strprompt("Default user purge time (days)", &sc[16][0], 5);
795         strprompt("Default room purge time (days)", &sc[17][0], 5);
796
797         /* Angels and demons dancing in my head... */
798         do {
799                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
800                 strprompt("System default message expire policy (? for list)",
801                           buf, 1);
802                 if (buf[0] == '?') {
803                         scr_printf("\n"
804                                 "1. Never automatically expire messages\n"
805                                 "2. Expire by message count\n"
806                                 "3. Expire by message age\n");
807                 }
808         } while ((buf[0] < '1') || (buf[0] > '3'));
809         site_expirepolicy->expire_mode = buf[0] - '0';
810
811         /* ...lunatics and monsters underneath my bed */
812         if (site_expirepolicy->expire_mode == 2) {
813                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
814                 strprompt("Keep how many messages online?", buf, 10);
815                 site_expirepolicy->expire_value = atol(buf);
816         }
817         if (site_expirepolicy->expire_mode == 3) {
818                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
819                 strprompt("Keep messages for how many days?", buf, 10);
820                 site_expirepolicy->expire_value = atol(buf);
821         }
822
823         /* Media messiahs preying on my fears... */
824         do {
825                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
826                 strprompt("Mailbox default message expire policy (? for list)",
827                           buf, 1);
828                 if (buf[0] == '?') {
829                         scr_printf("\n"
830                                 "0. Go with the system default\n"
831                                 "1. Never automatically expire messages\n"
832                                 "2. Expire by message count\n"
833                                 "3. Expire by message age\n");
834                 }
835         } while ((buf[0] < '0') || (buf[0] > '3'));
836         mbx_expirepolicy->expire_mode = buf[0] - '0';
837
838         /* ...Pop culture prophets playing in my ears */
839         if (mbx_expirepolicy->expire_mode == 2) {
840                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
841                 strprompt("Keep how many messages online?", buf, 10);
842                 mbx_expirepolicy->expire_value = atol(buf);
843         }
844         if (mbx_expirepolicy->expire_mode == 3) {
845                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
846                 strprompt("Keep messages for how many days?", buf, 10);
847                 mbx_expirepolicy->expire_value = atol(buf);
848         }
849
850         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
851         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
852         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
853                 "Enable full text search index (warning: resource intensive)",
854                 atoi(&sc[42][0]))));
855
856         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
857                 "Perform journaling of email messages",
858                 atoi(&sc[46][0]))));
859         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
860                 "Perform journaling of non-email messages",
861                 atoi(&sc[47][0]))));
862         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
863                 strprompt("Email destination of journalized messages",
864                         &sc[48][0], 127);
865         }
866
867         /* Funambol push stuff */
868         strprompt("Funambol server (blank to disable)", &sc[53][0], 63);
869         strprompt("Funambol server port", &sc[54][0], 5);
870         strprompt("Funambol sync source", &sc[55][0], 63);
871         strprompt("Funambol authentication details (user:pass in Base64)", &sc[56][0],63);
872         
873         /* Save it */
874         scr_printf("Save this configuration? ");
875         if (yesno()) {
876                 r = 1;
877                 for (a = 0; a < NUM_CONFIGS; a++)
878                         r += 1 + strlen(sc[a]);
879                 resp = (char *)calloc(1, r);
880                 if (!resp) {
881                         err_printf("Can't save config - out of memory!\n");
882                         logoff(ipc, 1);
883                 }
884                 for (a = 0; a < NUM_CONFIGS; a++) {
885                         strcat(resp, sc[a]);
886                         strcat(resp, "\n");
887                 }
888                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
889                 if (r / 100 != 4) {
890                         err_printf("%s\n", buf);
891                 }
892                 free(resp);
893
894                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
895                 if (r / 100 != 2) {
896                         err_printf("%s\n", buf);
897                 }
898
899                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
900                 if (r / 100 != 2) {
901                         err_printf("%s\n", buf);
902                 }
903
904         }
905     if (site_expirepolicy) free(site_expirepolicy);
906     if (mbx_expirepolicy) free(mbx_expirepolicy);
907 }
908
909
910 /*
911  * support function for do_internet_configuration()
912  */
913 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
914         int sel;
915
916         keyopt(" <1> localhost      (Alias for this computer)\n");
917         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
918         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
919         keyopt(" <4> directory      (Consult the Global Address Book)\n");
920         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
921         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
922         keyopt(" <7> masq domains   (Domains as which users are allowed to masquerade)\n");
923         sel = intprompt("Which one", 1, 1, 6);
924         switch(sel) {
925                 case 1: strcpy(buf, "localhost");
926                         return;
927                 case 2: strcpy(buf, "gatewaydomain");
928                         return;
929                 case 3: strcpy(buf, "smarthost");
930                         return;
931                 case 4: strcpy(buf, "directory");
932                         return;
933                 case 5: strcpy(buf, "spamassassin");
934                         return;
935                 case 6: strcpy(buf, "rbl");
936                         return;
937                 case 7: strcpy(buf, "masqdomain");
938                         return;
939         }
940 }
941
942
943 /*
944  * Internet mail configuration
945  */
946 void do_internet_configuration(CtdlIPC *ipc)
947 {
948         char buf[256];
949         char *resp = NULL;
950         int num_recs = 0;
951         char **recs = NULL;
952         char ch;
953         int badkey;
954         int i, j;
955         int quitting = 0;
956         int modified = 0;
957         int r;
958         
959         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
960         if (r / 100 == 1) {
961                 while (!IsEmptyStr(resp)) {
962                         extract_token(buf, resp, 0, '\n', sizeof buf);
963                         remove_token(resp, 0, '\n');
964                         ++num_recs;
965                         if (num_recs == 1) recs = malloc(sizeof(char *));
966                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
967                         recs[num_recs-1] = malloc(strlen(buf) + 1);
968                         strcpy(recs[num_recs-1], buf);
969                 }
970         }
971         if (resp) free(resp);
972
973         do {
974                 scr_printf("\n");
975                 color(BRIGHT_WHITE);
976                 scr_printf("###                    Host or domain                     Record type      \n");
977                 color(DIM_WHITE);
978                 scr_printf("--- -------------------------------------------------- --------------------\n");
979                 for (i=0; i<num_recs; ++i) {
980                 color(DIM_WHITE);
981                 scr_printf("%3d ", i+1);
982                 extract_token(buf, recs[i], 0, '|', sizeof buf);
983                 color(BRIGHT_CYAN);
984                 scr_printf("%-50s ", buf);
985                 extract_token(buf, recs[i], 1, '|', sizeof buf);
986                 color(BRIGHT_MAGENTA);
987                 scr_printf("%-20s\n", buf);
988                 color(DIM_WHITE);
989                 }
990
991                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
992                 switch(ch) {
993                         case 'a':
994                                 newprompt("Enter host name: ",
995                                         buf, 50);
996                                 striplt(buf);
997                                 if (!IsEmptyStr(buf)) {
998                                         ++num_recs;
999                                         if (num_recs == 1)
1000                                                 recs = malloc(sizeof(char *));
1001                                         else recs = realloc(recs,
1002                                                 (sizeof(char *)) * num_recs);
1003                                         strcat(buf, "|");
1004                                         get_inet_rec_type(ipc,
1005                                                         &buf[strlen(buf)]);
1006                                         recs[num_recs-1] = strdup(buf);
1007                                 }
1008                                 modified = 1;
1009                                 break;
1010                         case 'd':
1011                                 i = intprompt("Delete which one",
1012                                         1, 1, num_recs) - 1;
1013                                 free(recs[i]);
1014                                 --num_recs;
1015                                 for (j=i; j<num_recs; ++j)
1016                                         recs[j] = recs[j+1];
1017                                 modified = 1;
1018                                 break;
1019                         case 's':
1020                                 r = 1;
1021                                 for (i = 0; i < num_recs; i++)
1022                                         r += 1 + strlen(recs[i]);
1023                                 resp = (char *)calloc(1, r);
1024                                 if (!resp) {
1025                                         err_printf("Can't save config - out of memory!\n");
1026                                         logoff(ipc, 1);
1027                                 }
1028                                 if (num_recs) for (i = 0; i < num_recs; i++) {
1029                                         strcat(resp, recs[i]);
1030                                         strcat(resp, "\n");
1031                                 }
1032                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
1033                                 if (r / 100 != 4) {
1034                                         err_printf("%s\n", buf);
1035                                 } else {
1036                                         scr_printf("Wrote %d records.\n", num_recs);
1037                                         modified = 0;
1038                                 }
1039                 free(resp);
1040                                 break;
1041                         case 'q':
1042                                 quitting = !modified || boolprompt(
1043                                         "Quit without saving", 0);
1044                                 break;
1045                         default:
1046                                 badkey = 1;
1047                 }
1048         } while (!quitting);
1049
1050         if (recs != NULL) {
1051                 for (i=0; i<num_recs; ++i) free(recs[i]);
1052                 free(recs);
1053         }
1054 }
1055
1056
1057
1058 /*
1059  * Edit network configuration for room sharing, mailing lists, etc.
1060  */
1061 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
1062 {
1063         char filename[PATH_MAX];
1064         char changefile[PATH_MAX];
1065         int e_ex_code;
1066         pid_t editor_pid;
1067         int cksum;
1068         int b, i, tokens;
1069         char buf[1024];
1070         char instr[1024];
1071         char addr[1024];
1072         FILE *tempfp;
1073         FILE *changefp;
1074         char *listing = NULL;
1075         int r;
1076
1077         if (IsEmptyStr(editor_paths[0])) {
1078                 scr_printf("You must have an external editor configured in"
1079                         " order to use this function.\n");
1080                 return;
1081         }
1082
1083         CtdlMakeTempFileName(filename, sizeof filename);
1084         CtdlMakeTempFileName(changefile, sizeof changefile);
1085
1086         tempfp = fopen(filename, "w");
1087         if (tempfp == NULL) {
1088                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
1089                 return;
1090         }
1091
1092         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
1093         fprintf(tempfp, "# %s\n", comment);
1094         fprintf(tempfp, "# Specify one per line.\n"
1095                         "\n\n");
1096
1097         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1098         if (r / 100 == 1) {
1099                 while(listing && !IsEmptyStr(listing)) {
1100                         extract_token(buf, listing, 0, '\n', sizeof buf);
1101                         remove_token(listing, 0, '\n');
1102                         extract_token(instr, buf, 0, '|', sizeof instr);
1103                         if (!strcasecmp(instr, entrytype)) {
1104                                 tokens = num_tokens(buf, '|');
1105                                 for (i=1; i<tokens; ++i) {
1106                                         extract_token(addr, buf, i, '|', sizeof addr);
1107                                         fprintf(tempfp, "%s", addr);
1108                                         if (i < (tokens-1)) {
1109                                                 fprintf(tempfp, "|");
1110                                         }
1111                                 }
1112                                 fprintf(tempfp, "\n");
1113                         }
1114                 }
1115         }
1116         if (listing) {
1117                 free(listing);
1118                 listing = NULL;
1119         }
1120         fclose(tempfp);
1121
1122         e_ex_code = 1;  /* start with a failed exit code */
1123         screen_reset();
1124         stty_ctdl(SB_RESTORE);
1125         editor_pid = fork();
1126         cksum = file_checksum(filename);
1127         if (editor_pid == 0) {
1128                 chmod(filename, 0600);
1129                 putenv("WINDOW_TITLE=Network configuration");
1130                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
1131                 exit(1);
1132         }
1133         if (editor_pid > 0) {
1134                 do {
1135                         e_ex_code = 0;
1136                         b = ka_wait(&e_ex_code);
1137                 } while ((b != editor_pid) && (b >= 0));
1138         editor_pid = (-1);
1139         stty_ctdl(0);
1140         screen_set();
1141         }
1142
1143         if (file_checksum(filename) == cksum) {
1144                 err_printf("*** No changes to save.\n");
1145                 e_ex_code = 1;
1146         }
1147
1148         if (e_ex_code == 0) {           /* Save changes */
1149                 changefp = fopen(changefile, "w");
1150
1151                 /* Load all netconfig entries that are *not* of the type we are editing */
1152                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1153                 if (r / 100 == 1) {
1154                         while(listing && !IsEmptyStr(listing)) {
1155                                 extract_token(buf, listing, 0, '\n', sizeof buf);
1156                                 remove_token(listing, 0, '\n');
1157                                 extract_token(instr, buf, 0, '|', sizeof instr);
1158                                 if (strcasecmp(instr, entrytype)) {
1159                                         fprintf(changefp, "%s\n", buf);
1160                                 }
1161                         }
1162                 }
1163                 if (listing) {
1164                         free(listing);
1165                         listing = NULL;
1166                 }
1167
1168                 /* ...and merge that with the data we just edited */
1169                 tempfp = fopen(filename, "r");
1170                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1171                         for (i=0; i<strlen(buf); ++i) {
1172                                 if (buf[i] == '#') buf[i] = 0;
1173                         }
1174                         striplt(buf);
1175                         if (!IsEmptyStr(buf)) {
1176                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1177                         }
1178                 }
1179                 fclose(tempfp);
1180                 fclose(changefp);
1181
1182                 /* now write it to the server... */
1183                 changefp = fopen(changefile, "r");
1184                 if (changefp != NULL) {
1185                         listing = load_message_from_file(changefp);
1186                         if (listing) {
1187                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1188                                 free(listing);
1189                                 listing = NULL;
1190                         }
1191                         fclose(changefp);
1192                 }
1193         }
1194
1195         unlink(filename);               /* Delete the temporary files */
1196         unlink(changefile);
1197 }
1198
1199
1200 /*
1201  * IGnet node configuration
1202  */
1203 void do_ignet_configuration(CtdlIPC *ipc) {
1204         char buf[SIZ];
1205         int num_recs = 0;
1206         char **recs = NULL;
1207         char ch;
1208         int badkey;
1209         int i, j;
1210         int quitting = 0;
1211         int modified = 0;
1212         char *listing = NULL;
1213         int r;
1214
1215         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
1216         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
1217                 extract_token(buf, listing, 0, '\n', sizeof buf);
1218                 remove_token(listing, 0, '\n');
1219
1220                 ++num_recs;
1221                 if (num_recs == 1) recs = malloc(sizeof(char *));
1222                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1223                 recs[num_recs-1] = malloc(SIZ);
1224                 strcpy(recs[num_recs-1], buf);
1225         }
1226         if (listing) free(listing);
1227
1228         do {
1229                 scr_printf("\n");
1230                 color(BRIGHT_WHITE);
1231                 scr_printf(     "### "
1232                         "   Node          "
1233                         "  Secret           "
1234                         "          Host or IP             "
1235                         "Port#\n");
1236                 color(DIM_WHITE);
1237                 scr_printf(     "--- "
1238                         "---------------- "
1239                         "------------------ "
1240                         "-------------------------------- "
1241                         "-----\n");
1242                 for (i=0; i<num_recs; ++i) {
1243                 color(DIM_WHITE);
1244                 scr_printf("%3d ", i+1);
1245                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1246                 color(BRIGHT_CYAN);
1247                 scr_printf("%-16s ", buf);
1248                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1249                 color(BRIGHT_MAGENTA);
1250                 scr_printf("%-18s ", buf);
1251                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1252                 color(BRIGHT_CYAN);
1253                 scr_printf("%-32s ", buf);
1254                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1255                 color(BRIGHT_MAGENTA);
1256                 scr_printf("%-3s\n", buf);
1257                 color(DIM_WHITE);
1258                 }
1259                 scr_printf("\n");
1260
1261                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1262                 switch(ch) {
1263                         case 'a':
1264                                 ++num_recs;
1265                                 if (num_recs == 1)
1266                                         recs = malloc(sizeof(char *));
1267                                 else recs = realloc(recs,
1268                                         (sizeof(char *)) * num_recs);
1269                                 newprompt("Enter node name    : ", buf, 16);
1270                                 strcat(buf, "|");
1271                                 newprompt("Enter shared secret: ",
1272                                         &buf[strlen(buf)], 18);
1273                                 strcat(buf, "|");
1274                                 newprompt("Enter host or IP   : ",
1275                                         &buf[strlen(buf)], 32);
1276                                 strcat(buf, "|504");
1277                                 strprompt("Enter port number  : ",
1278                                         &buf[strlen(buf)-3], 5);
1279                                 recs[num_recs-1] = strdup(buf);
1280                                 modified = 1;
1281                                 break;
1282                         case 'd':
1283                                 i = intprompt("Delete which one",
1284                                         1, 1, num_recs) - 1;
1285                                 free(recs[i]);
1286                                 --num_recs;
1287                                 for (j=i; j<num_recs; ++j)
1288                                         recs[j] = recs[j+1];
1289                                 modified = 1;
1290                                 break;
1291                         case 's':
1292                                 r = 1;
1293                                 for (i = 0; i < num_recs; ++i)
1294                                         r += 1 + strlen(recs[i]);
1295                                 listing = (char*) calloc(1, r);
1296                                 if (!listing) {
1297                                         err_printf("Can't save config - out of memory!\n");
1298                                         logoff(ipc, 1);
1299                                 }
1300                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1301                                         strcat(listing, recs[i]);
1302                                         strcat(listing, "\n");
1303                                 }
1304                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
1305                                 if (r / 100 != 4) {
1306                                         scr_printf("%s\n", buf);
1307                                 } else {
1308                                         scr_printf("Wrote %d records.\n", num_recs);
1309                                         modified = 0;
1310                                 }
1311                 free(listing);
1312                                 break;
1313                         case 'q':
1314                                 quitting = !modified || boolprompt(
1315                                         "Quit without saving", 0);
1316                                 break;
1317                         default:
1318                                 badkey = 1;
1319                 }
1320         } while (!quitting);
1321
1322         if (recs != NULL) {
1323                 for (i=0; i<num_recs; ++i) free(recs[i]);
1324                 free(recs);
1325         }
1326 }
1327
1328 /*
1329  * Filter list configuration
1330  */
1331 void do_filterlist_configuration(CtdlIPC *ipc)
1332 {
1333         char buf[SIZ];
1334         int num_recs = 0;
1335         char **recs = NULL;
1336         char ch;
1337         int badkey;
1338         int i, j;
1339         int quitting = 0;
1340         int modified = 0;
1341         char *listing = NULL;
1342         int r;
1343
1344         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
1345         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
1346                 extract_token(buf, listing, 0, '\n', sizeof buf);
1347                 remove_token(listing, 0, '\n');
1348
1349                 ++num_recs;
1350                 if (num_recs == 1) recs = malloc(sizeof(char *));
1351                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1352                 recs[num_recs-1] = malloc(SIZ);
1353                 strcpy(recs[num_recs-1], buf);
1354         }
1355         if (listing) free(listing);
1356
1357         do {
1358                 scr_printf("\n");
1359                 color(BRIGHT_WHITE);
1360                 scr_printf(     "### "
1361                         "         User name           "
1362                         "         Room name           "
1363                         "    Node name    "
1364                         "\n");
1365                 color(DIM_WHITE);
1366                 scr_printf(     "--- "
1367                         "---------------------------- "
1368                         "---------------------------- "
1369                         "---------------- "
1370                         "\n");
1371                 for (i=0; i<num_recs; ++i) {
1372                 color(DIM_WHITE);
1373                 scr_printf("%3d ", i+1);
1374                 extract_token(buf, recs[i], 0, '|', sizeof buf);
1375                 color(BRIGHT_CYAN);
1376                 scr_printf("%-28s ", buf);
1377                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1378                 color(BRIGHT_MAGENTA);
1379                 scr_printf("%-28s ", buf);
1380                 extract_token(buf, recs[i], 2, '|', sizeof buf);
1381                 color(BRIGHT_CYAN);
1382                 scr_printf("%-16s\n", buf);
1383                 extract_token(buf, recs[i], 3, '|', sizeof buf);
1384                 color(DIM_WHITE);
1385                 }
1386
1387                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1388                 switch(ch) {
1389                         case 'a':
1390                                 ++num_recs;
1391                                 if (num_recs == 1)
1392                                         recs = malloc(sizeof(char *));
1393                                 else recs = realloc(recs,
1394                                         (sizeof(char *)) * num_recs);
1395                                 newprompt("Enter user name: ", buf, 28);
1396                                 strcat(buf, "|");
1397                                 newprompt("Enter room name: ",
1398                                         &buf[strlen(buf)], 28);
1399                                 strcat(buf, "|");
1400                                 newprompt("Enter node name: ",
1401                                         &buf[strlen(buf)], 16);
1402                                 strcat(buf, "|");
1403                                 recs[num_recs-1] = strdup(buf);
1404                                 modified = 1;
1405                                 break;
1406                         case 'd':
1407                                 i = intprompt("Delete which one",
1408                                         1, 1, num_recs) - 1;
1409                                 free(recs[i]);
1410                                 --num_recs;
1411                                 for (j=i; j<num_recs; ++j)
1412                                         recs[j] = recs[j+1];
1413                                 modified = 1;
1414                                 break;
1415                         case 's':
1416                                 r = 1;
1417                                 for (i = 0; i < num_recs; ++i)
1418                                         r += 1 + strlen(recs[i]);
1419                                 listing = (char*) calloc(1, r);
1420                                 if (!listing) {
1421                                         err_printf("Can't save config - out of memory!\n");
1422                                         logoff(ipc, 1);
1423                                 }
1424                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1425                                         strcat(listing, recs[i]);
1426                                         strcat(listing, "\n");
1427                                 }
1428                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
1429                                 if (r / 100 != 4) {
1430                                         scr_printf("%s\n", buf);
1431                                 } else {
1432                                         scr_printf("Wrote %d records.\n", num_recs);
1433                                         modified = 0;
1434                                 }
1435                 free(listing);
1436                                 break;
1437                         case 'q':
1438                                 quitting = !modified || boolprompt(
1439                                         "Quit without saving", 0);
1440                                 break;
1441                         default:
1442                                 badkey = 1;
1443                 }
1444         } while (!quitting);
1445
1446         if (recs != NULL) {
1447                 for (i=0; i<num_recs; ++i) free(recs[i]);
1448                 free(recs);
1449         }
1450 }
1451
1452