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