* Bug fixes: Fix numerous char array size mismatches, signed/unsigned
[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 "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <limits.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <signal.h>
31 #include <pwd.h>
32 #include <setjmp.h>
33 #include <errno.h>
34 #include <stdarg.h>
35 #include "citadel.h"
36 #include "citadel_ipc.h"
37 #include "citadel_decls.h"
38 #include "routines2.h"
39 #include "routines.h"
40 #include "commands.h"
41 #include "tools.h"
42 #include "messages.h"
43 #ifndef HAVE_SNPRINTF
44 #include "snprintf.h"
45 #endif
46 #include "screen.h"
47
48 extern char temp[];
49 extern char tempdir[];
50 extern char *axdefs[7];
51 extern long highest_msg_read;
52 extern long maxmsgnum;
53 extern unsigned room_flags;
54 extern int screenwidth;
55
56
57 /*
58 int eopen(char *name, int mode)
59 {
60         int ret;
61         ret = open(name, mode);
62         if (ret < 0) {
63                 err_printf("Cannot open file '%s', mode=%d, errno=%d\n",
64                         name, mode, errno);
65                 interr(errno);
66         }
67         return (ret);
68 }
69 */
70
71
72 int room_prompt(unsigned int qrflags)
73 {                               /* return proper room prompt character */
74         int a;
75         a = '>';
76         if (qrflags & QR_DIRECTORY)
77                 a = ']';
78         if ((a == ']') && (qrflags & QR_NETWORK))
79                 a = '}';
80         if ((a == '>') && (qrflags & QR_NETWORK))
81                 a = ')';
82         return (a);
83 }
84
85 void entregis(CtdlIPC *ipc)
86 {                               /* register with name and address */
87
88         char buf[SIZ];
89         char tmpname[SIZ];
90         char tmpaddr[SIZ];
91         char tmpcity[SIZ];
92         char tmpstate[SIZ];
93         char tmpzip[SIZ];
94         char tmpphone[SIZ];
95         char tmpemail[SIZ];
96         char tmpcountry[SIZ];
97         char diruser[SIZ];
98         char dirnode[SIZ];
99         char holdemail[SIZ];
100         char *reg = NULL;
101         int ok = 0;
102         int r;                          /* IPC response code */
103
104         strcpy(tmpname, "");
105         strcpy(tmpaddr, "");
106         strcpy(tmpcity, "");
107         strcpy(tmpstate, "");
108         strcpy(tmpzip, "");
109         strcpy(tmpphone, "");
110         strcpy(tmpemail, "");
111         strcpy(tmpcountry, "");
112
113         r = CtdlIPCGetUserRegistration(ipc, NULL, &reg, buf);
114         if (r / 100 == 1) {
115                 int a = 0;
116
117                 while (reg && strlen(reg) > 0) {
118
119                         extract_token(buf, reg, 0, '\n');
120                         remove_token(reg, 0, '\n');
121
122                         if (a == 2)
123                                 strcpy(tmpname, buf);
124                         else if (a == 3)
125                                 strcpy(tmpaddr, buf);
126                         else if (a == 4)
127                                 strcpy(tmpcity, buf);
128                         else if (a == 5)
129                                 strcpy(tmpstate, buf);
130                         else if (a == 6)
131                                 strcpy(tmpzip, buf);
132                         else if (a == 7)
133                                 strcpy(tmpphone, buf);
134                         else if (a == 9)
135                                 strcpy(tmpemail, buf);
136                         else if (a == 10)
137                                 strcpy(tmpcountry, buf);
138                         ++a;
139                 }
140         }
141         strprompt("REAL name", tmpname, 29);
142         strprompt("Address", tmpaddr, 24);
143         strprompt("City/town", tmpcity, 14);
144         strprompt("State/province", tmpstate, 2);
145         strprompt("ZIP/Postal Code", tmpzip, 10);
146         strprompt("Country", tmpcountry, 31);
147         strprompt("Telephone number", tmpphone, 14);
148
149         do {
150                 ok = 1;
151                 strcpy(holdemail, tmpemail);
152                 strprompt("Email address", tmpemail, 31);
153                 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
154                 if (r / 100 == 2) {
155                         extract_token(diruser, buf, 0, '@');
156                         extract_token(dirnode, buf, 1, '@');
157                         striplt(diruser);
158                         striplt(dirnode);
159                         if ((strcasecmp(diruser, fullname))
160                            || (strcasecmp(dirnode, serv_info.serv_nodename))) {
161                                 scr_printf(
162                                         "\nYou can't use %s as your address.\n",
163                                         tmpemail);
164                                 scr_printf(
165                                         "It is already in use by %s @ %s.\n",
166                                         diruser, dirnode);
167                                 ok = 0;
168                                 strcpy(tmpemail, holdemail);
169                         }
170                 }
171         } while (ok == 0);
172
173         /* now send the registration info back to the server */
174         reg = (char *)realloc(reg, 4096);       /* Overkill? */
175         if (reg) {
176                 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
177                         tmpname, tmpaddr, tmpcity, tmpstate,
178                         tmpzip, tmpphone, tmpemail, tmpcountry);
179                 r = CtdlIPCSetRegistration(ipc, reg, buf);
180                 if (r / 100 != 4)
181                         scr_printf("%s\n", buf);
182                 free(reg);
183         }
184         scr_printf("\n");
185 }
186
187 void updatels(CtdlIPC *ipc)
188 {                               /* make all messages old in current room */
189         char buf[SIZ];
190         int r;                          /* IPC response code */
191
192         if (rc_alt_semantics) {
193                 if (maxmsgnum == highest_msg_read == 0) {
194                         /* err_printf("maxmsgnum == highest_msg_read == 0\n"); */
195                         return;
196                 }
197                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
198                                  maxmsgnum : highest_msg_read, buf);
199         } else {
200                 r = CtdlIPCSetLastRead(ipc, 0, buf);
201         }
202         if (r / 100 != 2)
203                 scr_printf("%s\n", buf);
204 }
205
206 /*
207  * only make messages old in this room that have been read
208  */
209 void updatelsa(CtdlIPC *ipc)
210 {
211         char buf[SIZ];
212         int r;                          /* IPC response code */
213
214         r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
215         if (r / 100 != 2)
216                 scr_printf("%s\n", &buf[4]);
217 }
218
219
220 /*
221  * client-based uploads (for users with their own clientware)
222  */
223 void cli_upload(CtdlIPC *ipc)
224 {
225         char flnm[SIZ];
226         char desc[151];
227         char buf[SIZ];
228         char tbuf[SIZ];
229         int r;          /* IPC response code */
230         int a;
231         int fd;
232
233         if ((room_flags & QR_UPLOAD) == 0) {
234                 scr_printf("*** You cannot upload to this room.\n");
235                 return;
236         }
237         newprompt("File to be uploaded: ", flnm, 55);
238         fd = open(flnm, O_RDONLY);
239         if (fd < 0) {
240                 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
241                 return;
242         }
243         scr_printf("Enter a description of this file:\n");
244         newprompt(": ", desc, 75);
245
246         /* Keep generating filenames in hope of finding a unique one */
247         a = 0;
248         while (a < 10) {
249                 /* basename of filename */
250                 strcpy(tbuf, flnm);
251                 if (haschar(tbuf, '/'))
252                         strcpy(tbuf, strrchr(tbuf, '/'));
253                 /* filename.1, filename.2, etc */
254                 if (a > 0) {
255                         sprintf(buf + strlen(buf), ".%d", a);
256                 }
257                 /* Try upload */
258                 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
259                 if (r / 100 == 5 || r < 0)
260                         scr_printf("%s\n", buf);
261                 else
262                         break;
263                 ++a;
264         };
265 }
266
267
268 /*
269  * Function used for various image upload commands
270  */
271 void cli_image_upload(CtdlIPC *ipc, char *keyname)
272 {
273         char flnm[SIZ];
274         char buf[SIZ];
275         int r;
276
277         /* Can we upload this image? */
278         r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
279         if (r / 100 != 2) {
280                 err_printf("%s\n", buf);
281                 return;
282         }
283         newprompt("Image file to be uploaded: ", flnm, 55);
284         r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
285         if (r / 100 == 5) {
286                 err_printf("%s\n", buf);
287         } else if (r < 0) {
288                 err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
289         }
290         /* else upload succeeded */
291 }
292
293
294 /*
295  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
296  */
297 void upload(CtdlIPC *ipc, int c)
298 {                               /* c = upload mode */
299         char flnm[SIZ];
300         char desc[151];
301         char buf[SIZ];
302         char tbuf[4096];
303         int xfer_pid;
304         int a, b;
305         FILE *fp, *lsfp;
306         int r;
307
308         if ((room_flags & QR_UPLOAD) == 0) {
309                 scr_printf("*** You cannot upload to this room.\n");
310                 return;
311         }
312         /* we don't need a filename when receiving batch y/z modem */
313         if ((c == 2) || (c == 3))
314                 strcpy(flnm, "x");
315         else
316                 newprompt("Enter filename: ", flnm, 15);
317
318         for (a = 0; a < strlen(flnm); ++a)
319                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
320                     || (flnm[a] == '?') || (flnm[a] == '*')
321                     || (flnm[a] == ';') || (flnm[a] == '&'))
322                         flnm[a] = '_';
323
324         /* create a temporary directory... */
325         if (mkdir(tempdir, 0700) != 0) {
326                 scr_printf("*** Could not create temporary directory %s: %s\n",
327                        tempdir, strerror(errno));
328                 return;
329         }
330         /* now do the transfer ... in a separate process */
331         xfer_pid = fork();
332         if (xfer_pid == 0) {
333                 chdir(tempdir);
334                 switch (c) {
335                 case 0:
336                         sttybbs(0);
337                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
338                         fp = fopen(flnm, "w");
339                         do {
340                                 b = inkey();
341                                 if (b == 13) {
342                                         b = 10;
343                                 }
344                                 if (b != 4) {
345                                         scr_printf("%c", b);
346                                         putc(b, fp);
347                                 }
348                         } while (b != 4);
349                         fclose(fp);
350                         exit(0);
351                 case 1:
352                         screen_reset();
353                         sttybbs(3);
354                         execlp("rx", "rx", flnm, NULL);
355                         exit(1);
356                 case 2:
357                         screen_reset();
358                         sttybbs(3);
359                         execlp("rb", "rb", NULL);
360                         exit(1);
361                 case 3:
362                         screen_reset();
363                         sttybbs(3);
364                         execlp("rz", "rz", NULL);
365                         exit(1);
366                 }
367         } else
368                 do {
369                         b = ka_wait(&a);
370                 } while ((b != xfer_pid) && (b != (-1)));
371         sttybbs(0);
372         screen_set();
373
374         if (a != 0) {
375                 scr_printf("\r*** Transfer unsuccessful.\n");
376                 nukedir(tempdir);
377                 return;
378         }
379         scr_printf("\r*** Transfer successful.\n");
380         snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
381         lsfp = popen(buf, "r");
382         if (lsfp != NULL) {
383                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
384                         flnm[strlen(flnm) - 1] = 0;     /* chop newline */
385                         snprintf(buf, sizeof buf,
386                                  "Enter a short description of '%s':\n: ",
387                                  flnm);
388                         newprompt(buf, desc, 150);
389                         snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
390                         r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
391                         scr_printf("%s\n", tbuf);
392                 }
393                 pclose(lsfp);
394         }
395         nukedir(tempdir);
396 }
397
398 /* 
399  * validate a user
400  */
401 void val_user(CtdlIPC *ipc, char *user, int do_validate)
402 {
403         int a;
404         char cmd[SIZ];
405         char buf[SIZ];
406         int ax = 0;
407         int r;                          /* IPC response code */
408
409         snprintf(cmd, sizeof cmd, "GREG %s", user);
410         CtdlIPC_putline(ipc, cmd);
411         CtdlIPC_getline(ipc, cmd);
412         if (cmd[0] == '1') {
413                 a = 0;
414                 do {
415                         CtdlIPC_getline(ipc, buf);
416                         ++a;
417                         if (a == 1)
418                                 scr_printf("User #%s - %s  ", buf, &cmd[4]);
419                         if (a == 2)
420                                 scr_printf("PW: %s\n", buf);
421                         if (a == 3)
422                                 scr_printf("%s\n", buf);
423                         if (a == 4)
424                                 scr_printf("%s\n", buf);
425                         if (a == 5)
426                                 scr_printf("%s, ", buf);
427                         if (a == 6)
428                                 scr_printf("%s ", buf);
429                         if (a == 7)
430                                 scr_printf("%s\n", buf);
431                         if (a == 8)
432                                 scr_printf("%s\n", buf);
433                         if (a == 9)
434                                 ax = atoi(buf);
435                         if (a == 10)
436                                 scr_printf("%s\n", buf);
437                         if (a == 11)
438                                 scr_printf("%s\n", buf);
439                 } while (strcmp(buf, "000"));
440                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
441         } else {
442                 scr_printf("%-30s\n%s\n", user, &cmd[4]);
443         }
444
445         if (do_validate) {
446                 /* now set the access level */
447                 ax = intprompt("Access level", ax, 0, 6);
448                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
449                 if (r / 100 != 2)
450                         scr_printf("%s\n", cmd);
451         }
452         scr_printf("\n");
453 }
454
455
456 void validate(CtdlIPC *ipc)
457 {                               /* validate new users */
458         char cmd[SIZ];
459         char buf[SIZ];
460         int finished = 0;
461         int r;                          /* IPC response code */
462
463         do {
464                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
465                 if (r / 100 != 3)
466                         finished = 1;
467                 if (r / 100 == 2)
468                         scr_printf("%s\n", cmd);
469                 if (r / 100 == 3) {
470                         extract(buf, cmd, 0);
471                         val_user(ipc, buf, 1);
472                 }
473         } while (finished == 0);
474 }
475
476 void subshell(void)
477 {
478         int a, b;
479         a = fork();
480         if (a == 0) {
481                 screen_reset();
482                 sttybbs(SB_RESTORE);
483                 signal(SIGINT, SIG_DFL);
484                 signal(SIGQUIT, SIG_DFL);
485                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
486                 err_printf("Could not open a shell: %s\n", strerror(errno));
487                 exit(errno);
488         }
489         do {
490                 b = ka_wait(NULL);
491         } while ((a != b) && (a != (-1)));
492         sttybbs(0);
493         screen_set();
494 }
495
496 /*
497  * <.A>ide <F>ile <D>elete command
498  */
499 void deletefile(CtdlIPC *ipc)
500 {
501         char filename[32];
502         char buf[SIZ];
503
504         newprompt("Filename: ", filename, 31);
505         if (strlen(filename) == 0)
506                 return;
507         CtdlIPCDeleteFile(ipc, filename, buf);
508         err_printf("%s\n", buf);
509 }
510
511 /*
512  * <.A>ide <F>ile <S>end command
513  */
514 void netsendfile(CtdlIPC *ipc)
515 {
516         char filename[32], destsys[20], buf[SIZ];
517
518         newprompt("Filename: ", filename, 31);
519         if (strlen(filename) == 0)
520                 return;
521         newprompt("System to send to: ", destsys, 19);
522         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
523         err_printf("%s\n", buf);
524         return;
525 }
526
527 /*
528  * <.A>ide <F>ile <M>ove command
529  */
530 void movefile(CtdlIPC *ipc)
531 {
532         char filename[64];
533         char newroom[ROOMNAMELEN];
534         char buf[SIZ];
535
536         newprompt("Filename: ", filename, 63);
537         if (strlen(filename) == 0)
538                 return;
539         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
540         CtdlIPCMoveFile(ipc, filename, newroom, buf);
541         err_printf("%s\n", buf);
542 }
543
544
545 /* 
546  * list of users who have filled out a bio
547  */
548 void list_bio(CtdlIPC *ipc)
549 {
550         char buf[SIZ];
551         int pos = 1;
552
553         CtdlIPC_putline(ipc, "LBIO");
554         CtdlIPC_getline(ipc, buf);
555         if (buf[0] != '1') {
556                 pprintf("%s\n", &buf[4]);
557                 return;
558         }
559         while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
560                 if ((pos + strlen(buf) + 5) > screenwidth) {
561                         pprintf("\n");
562                         pos = 1;
563                 }
564                 pprintf("%s, ", buf);
565                 pos = pos + strlen(buf) + 2;
566         }
567         pprintf("%c%c  \n\n", 8, 8);
568 }
569
570
571 /*
572  * read bio
573  */
574 void read_bio(CtdlIPC *ipc)
575 {
576         char who[SIZ];
577         char buf[SIZ];
578
579         do {
580                 newprompt("Read bio for who ('?' for list) : ", who, 25);
581                 pprintf("\n");
582                 if (!strcmp(who, "?"))
583                         list_bio(ipc);
584         } while (!strcmp(who, "?"));
585         snprintf(buf, sizeof buf, "RBIO %s", who);
586         CtdlIPC_putline(ipc, buf);
587         CtdlIPC_getline(ipc, buf);
588         if (buf[0] != '1') {
589                 pprintf("%s\n", &buf[4]);
590                 return;
591         }
592         while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
593                 pprintf("%s\n", buf);
594         }
595 }
596
597
598 /* 
599  * General system configuration command
600  */
601 void do_system_configuration(CtdlIPC *ipc)
602 {
603         char buf[SIZ];
604         char sc[31][SIZ];
605         int expire_mode = 0;
606         int expire_value = 0;
607         int a;
608         int logpages = 0;
609
610         /* Clear out the config buffers */
611         memset(&sc[0][0], 0, sizeof(sc));
612
613         /* Fetch the current config */
614         CtdlIPC_putline(ipc, "CONF get");
615         CtdlIPC_getline(ipc, buf);
616         if (buf[0] == '1') {
617                 a = 0;
618                 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
619                         if (a < 31) {
620                                 strcpy(&sc[a][0], buf);
621                         }
622                         ++a;
623                 }
624         }
625         /* Fetch the expire policy (this will silently fail on old servers,
626          * resulting in "default" policy)
627          */
628         CtdlIPC_putline(ipc, "GPEX site");
629         CtdlIPC_getline(ipc, buf);
630         if (buf[0] == '2') {
631                 expire_mode = extract_int(&buf[4], 0);
632                 expire_value = extract_int(&buf[4], 1);
633         }
634
635
636         /* Identification parameters */
637
638         strprompt("Node name", &sc[0][0], 15);
639         strprompt("Fully qualified domain name", &sc[1][0], 63);
640         strprompt("Human readable node name", &sc[2][0], 20);
641         strprompt("Modem dialup number", &sc[3][0], 15);
642         strprompt("Geographic location of this system", &sc[12][0], 31);
643         strprompt("Name of system administrator", &sc[13][0], 25);
644         strprompt("Paginator prompt", &sc[10][0], 79);
645
646         /* Security parameters */
647
648         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
649                                     "Require registration for new users",
650                                                     atoi(&sc[7][0]))));
651         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
652               "Disable self-service user account creation",
653                                                      atoi(&sc[29][0]))));
654         strprompt("Initial access level for new users", &sc[6][0], 1);
655         strprompt("Access level required to create rooms", &sc[19][0], 1);
656         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
657                                                     "Automatically give room aide privs to a user who creates a private room",
658                                                     atoi(&sc[4][0]))));
659
660         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
661                  "Automatically move problem user messages to twit room",
662                                                     atoi(&sc[8][0]))));
663
664         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
665         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
666               "Restrict Internet mail to only those with that privilege",
667                                                      atoi(&sc[11][0]))));
668         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
669               "Allow Aides to Zap (forget) rooms",
670                                                      atoi(&sc[26][0]))));
671         snprintf(sc[30], sizeof sc[29], "%d", (boolprompt(
672               "Allow system Aides access to user mailboxes",
673                                                      atoi(&sc[30][0]))));
674
675         if (strlen(&sc[18][0]) > 0) logpages = 1;
676         else logpages = 0;
677         logpages = boolprompt("Log all pages", logpages);
678         if (logpages) {
679                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
680         }
681         else {
682                 sc[18][0] = 0;
683         }
684
685
686         /* Server tuning */
687
688         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
689         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
690         strprompt("Maximum message length", &sc[20][0], 20);
691         strprompt("Minimum number of worker threads", &sc[21][0], 3);
692         strprompt("Maximum number of worker threads", &sc[22][0], 3);
693
694         /* no longer applicable ... deprecated
695         strprompt("Server-to-server networking password", &sc[15][0], 19);
696         */
697
698         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
699         strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
700         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
701         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
702
703         /* Expiry settings */
704         strprompt("Default user purge time (days)", &sc[16][0], 5);
705         strprompt("Default room purge time (days)", &sc[17][0], 5);
706
707         /* Angels and demons dancing in my head... */
708         do {
709                 snprintf(buf, sizeof buf, "%d", expire_mode);
710                 strprompt("System default message expire policy (? for list)",
711                           buf, 1);
712                 if (buf[0] == '?') {
713                         scr_printf("\n"
714                                 "1. Never automatically expire messages\n"
715                                 "2. Expire by message count\n"
716                                 "3. Expire by message age\n");
717                 }
718         } while ((buf[0] < 49) || (buf[0] > 51));
719         expire_mode = buf[0] - 48;
720
721         /* ...lunatics and monsters underneath my bed */
722         if (expire_mode == 2) {
723                 snprintf(buf, sizeof buf, "%d", expire_value);
724                 strprompt("Keep how many messages online?", buf, 10);
725                 expire_value = atol(buf);
726         }
727         if (expire_mode == 3) {
728                 snprintf(buf, sizeof buf, "%d", expire_value);
729                 strprompt("Keep messages for how many days?", buf, 10);
730                 expire_value = atol(buf);
731         }
732         /* Save it */
733         scr_printf("Save this configuration? ");
734         if (yesno()) {
735                 CtdlIPC_putline(ipc, "CONF set");
736                 CtdlIPC_getline(ipc, buf);
737                 if (buf[0] == '4') {
738                         for (a = 0; a < 31; ++a)
739                                 CtdlIPC_putline(ipc, &sc[a][0]);
740                         CtdlIPC_putline(ipc, "000");
741                 }
742                 snprintf(buf, sizeof buf, "SPEX site|%d|%d",
743                          expire_mode, expire_value);
744                 CtdlIPC_putline(ipc, buf);
745                 CtdlIPC_getline(ipc, buf);
746         }
747 }
748
749
750 /*
751  * support function for do_internet_configuration()
752  */
753 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
754         int sel;
755
756         keyopt(" <1> localhost      (Alias for this computer)\n");
757         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
758         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
759         keyopt(" <4> directory      (Consult the Global Address Book)\n");
760         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
761         sel = intprompt("Which one", 1, 1, 5);
762         switch(sel) {
763                 case 1: strcpy(buf, "localhost");
764                         return;
765                 case 2: strcpy(buf, "gatewaydomain");
766                         return;
767                 case 3: strcpy(buf, "smarthost");
768                         return;
769                 case 4: strcpy(buf, "directory");
770                         return;
771                 case 5: strcpy(buf, "spamassassin");
772                         return;
773         }
774 }
775
776
777 /*
778  * Internet mail configuration
779  */
780 void do_internet_configuration(CtdlIPC *ipc)
781 {
782         char buf[SIZ];
783         int num_recs = 0;
784         char **recs = NULL;
785         char ch;
786         int badkey;
787         int i, j;
788         int quitting = 0;
789         
790
791         snprintf(buf, sizeof buf, "CONF getsys|%s", INTERNETCFG);
792         CtdlIPC_putline(ipc, buf);
793         CtdlIPC_getline(ipc, buf);
794         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
795                 ++num_recs;
796                 if (num_recs == 1) recs = malloc(sizeof(char *));
797                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
798                 recs[num_recs-1] = malloc(SIZ);
799                 strcpy(recs[num_recs-1], buf);
800         }
801
802         do {
803                 scr_printf("\n");
804                 color(BRIGHT_WHITE);
805                 scr_printf("###                    Host or domain                     Record type      \n");
806                 color(DIM_WHITE);
807                 scr_printf("--- -------------------------------------------------- --------------------\n");
808                 for (i=0; i<num_recs; ++i) {
809                 color(DIM_WHITE);
810                 scr_printf("%3d ", i+1);
811                 extract(buf, recs[i], 0);
812                 color(BRIGHT_CYAN);
813                 scr_printf("%-50s ", buf);
814                 extract(buf, recs[i], 1);
815                 color(BRIGHT_MAGENTA);
816                 scr_printf("%-20s\n", buf);
817                 color(DIM_WHITE);
818                 }
819
820                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
821                 switch(ch) {
822                         case 'a':
823                                 ++num_recs;
824                                 if (num_recs == 1)
825                                         recs = malloc(sizeof(char *));
826                                 else recs = realloc(recs,
827                                         (sizeof(char *)) * num_recs);
828                                 newprompt("Enter host name: ",
829                                         buf, 50);
830                                 strcat(buf, "|");
831                                 get_inet_rec_type(ipc, &buf[strlen(buf)]);
832                                 recs[num_recs-1] = strdup(buf);
833                                 break;
834                         case 'd':
835                                 i = intprompt("Delete which one",
836                                         1, 1, num_recs) - 1;
837                                 free(recs[i]);
838                                 --num_recs;
839                                 for (j=i; j<num_recs; ++j)
840                                         recs[j] = recs[j+1];
841                                 break;
842                         case 's':
843                                 snprintf(buf, sizeof buf, "CONF putsys|%s",
844                                         INTERNETCFG);
845                                 CtdlIPC_putline(ipc, buf);
846                                 CtdlIPC_getline(ipc, buf);
847                                 if (buf[0] == '4') {
848                                         for (i=0; i<num_recs; ++i) {
849                                                 CtdlIPC_putline(ipc, recs[i]);
850                                         }
851                                         CtdlIPC_putline(ipc, "000");
852                                 }
853                                 else {
854                                         scr_printf("%s\n", &buf[4]);
855                                 }
856                                 quitting = 1;
857                                 break;
858                         case 'q':
859                                 quitting = boolprompt(
860                                         "Quit without saving", 0);
861                                 break;
862                         default:
863                                 badkey = 1;
864                 }
865         } while (quitting == 0);
866
867         if (recs != NULL) {
868                 for (i=0; i<num_recs; ++i) free(recs[i]);
869                 free(recs);
870         }
871 }
872
873
874
875 /*
876  * Edit network configuration for room sharing, mailing lists, etc.
877  */
878 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
879 {
880         char filename[PATH_MAX];
881         char changefile[PATH_MAX];
882         int e_ex_code;
883         pid_t editor_pid;
884         int cksum;
885         int b, i;
886         char buf[SIZ];
887         char instr[SIZ];
888         char addr[SIZ];
889         FILE *tempfp;
890         FILE *changefp;
891
892         if (strlen(editor_path) == 0) {
893                 scr_printf("You must have an external editor configured in"
894                         " order to use this function.\n");
895                 return;
896         }
897
898         snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
899         snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
900
901         tempfp = fopen(filename, "w");
902         if (tempfp == NULL) {
903                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
904                 return;
905         }
906
907         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
908         fprintf(tempfp, "# %s\n", comment);
909         fprintf(tempfp, "# Specify one per line.\n"
910                         "\n\n");
911
912         CtdlIPC_putline(ipc, "GNET");
913         CtdlIPC_getline(ipc, buf);
914         if (buf[0] == '1') {
915                 while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
916                         extract(instr, buf, 0);
917                         if (!strcasecmp(instr, entrytype)) {
918                                 extract(addr, buf, 1);
919                                 fprintf(tempfp, "%s\n", addr);
920                         }
921                 }
922         }
923         fclose(tempfp);
924
925         e_ex_code = 1;  /* start with a failed exit code */
926         editor_pid = fork();
927         cksum = file_checksum(filename);
928         if (editor_pid == 0) {
929                 chmod(filename, 0600);
930                 screen_reset();
931                 sttybbs(SB_RESTORE);
932                 putenv("WINDOW_TITLE=Network configuration");
933                 execlp(editor_path, editor_path, filename, NULL);
934                 exit(1);
935         }
936         if (editor_pid > 0) {
937                 do {
938                         e_ex_code = 0;
939                         b = ka_wait(&e_ex_code);
940                 } while ((b != editor_pid) && (b >= 0));
941         editor_pid = (-1);
942         sttybbs(0);
943         screen_set();
944         }
945
946         if (file_checksum(filename) == cksum) {
947                 err_printf("*** Not saving changes.\n");
948                 e_ex_code = 1;
949         }
950
951         if (e_ex_code == 0) {           /* Save changes */
952                 changefp = fopen(changefile, "w");
953                 CtdlIPC_putline(ipc, "GNET");
954                 CtdlIPC_getline(ipc, buf);
955                 if (buf[0] == '1') {
956                         while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
957                                 extract(instr, buf, 0);
958                                 if (strcasecmp(instr, entrytype)) {
959                                         fprintf(changefp, "%s\n", buf);
960                                 }
961                         }
962                 }
963                 tempfp = fopen(filename, "r");
964                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
965                         for (i=0; i<strlen(buf); ++i) {
966                                 if (buf[i] == '#') buf[i] = 0;
967                         }
968                         striplt(buf);
969                         if (strlen(buf) > 0) {
970                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
971                         }
972                 }
973                 fclose(tempfp);
974                 fclose(changefp);
975
976                 /* now write it to the server... */
977                 CtdlIPC_putline(ipc, "SNET");
978                 CtdlIPC_getline(ipc, buf);
979                 if (buf[0] == '4') {
980                         changefp = fopen(changefile, "r");
981                         if (changefp != NULL) {
982                                 while (fgets(buf, sizeof buf,
983                                        changefp) != NULL) {
984                                         buf[strlen(buf) - 1] = 0;
985                                         CtdlIPC_putline(ipc, buf);
986                                 }
987                                 fclose(changefp);
988                         }
989                         CtdlIPC_putline(ipc, "000");
990                 }
991         }
992
993         unlink(filename);               /* Delete the temporary files */
994         unlink(changefile);
995 }
996
997
998 /*
999  * IGnet node configuration
1000  */
1001 void do_ignet_configuration(CtdlIPC *ipc) {
1002         char buf[SIZ];
1003         int num_recs = 0;
1004         char **recs = NULL;
1005         char ch;
1006         int badkey;
1007         int i, j;
1008         int quitting = 0;
1009         
1010
1011         snprintf(buf, sizeof buf, "CONF getsys|%s", IGNETCFG);
1012         CtdlIPC_putline(ipc, buf);
1013         CtdlIPC_getline(ipc, buf);
1014         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1015                 ++num_recs;
1016                 if (num_recs == 1) recs = malloc(sizeof(char *));
1017                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1018                 recs[num_recs-1] = malloc(SIZ);
1019                 strcpy(recs[num_recs-1], buf);
1020         }
1021
1022         do {
1023                 scr_printf("\n");
1024                 color(BRIGHT_WHITE);
1025                 scr_printf(     "### "
1026                         "   Node          "
1027                         "  Secret           "
1028                         "          Host or IP             "
1029                         "Port#\n");
1030                 color(DIM_WHITE);
1031                 scr_printf(     "--- "
1032                         "---------------- "
1033                         "------------------ "
1034                         "-------------------------------- "
1035                         "-----\n");
1036                 for (i=0; i<num_recs; ++i) {
1037                 color(DIM_WHITE);
1038                 scr_printf("%3d ", i+1);
1039                 extract(buf, recs[i], 0);
1040                 color(BRIGHT_CYAN);
1041                 scr_printf("%-16s ", buf);
1042                 extract(buf, recs[i], 1);
1043                 color(BRIGHT_MAGENTA);
1044                 scr_printf("%-18s ", buf);
1045                 extract(buf, recs[i], 2);
1046                 color(BRIGHT_CYAN);
1047                 scr_printf("%-32s ", buf);
1048                 extract(buf, recs[i], 3);
1049                 color(BRIGHT_MAGENTA);
1050                 scr_printf("%-3s\n", buf);
1051                 color(DIM_WHITE);
1052                 }
1053
1054                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1055                 switch(ch) {
1056                         case 'a':
1057                                 ++num_recs;
1058                                 if (num_recs == 1)
1059                                         recs = malloc(sizeof(char *));
1060                                 else recs = realloc(recs,
1061                                         (sizeof(char *)) * num_recs);
1062                                 newprompt("Enter node name    : ", buf, 16);
1063                                 strcat(buf, "|");
1064                                 newprompt("Enter shared secret: ",
1065                                         &buf[strlen(buf)], 18);
1066                                 strcat(buf, "|");
1067                                 newprompt("Enter host or IP   : ",
1068                                         &buf[strlen(buf)], 32);
1069                                 strcat(buf, "|504");
1070                                 strprompt("Enter port number  : ",
1071                                         &buf[strlen(buf)-3], 5);
1072                                 recs[num_recs-1] = strdup(buf);
1073                                 break;
1074                         case 'd':
1075                                 i = intprompt("Delete which one",
1076                                         1, 1, num_recs) - 1;
1077                                 free(recs[i]);
1078                                 --num_recs;
1079                                 for (j=i; j<num_recs; ++j)
1080                                         recs[j] = recs[j+1];
1081                                 break;
1082                         case 's':
1083                                 snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
1084                                 CtdlIPC_putline(ipc, buf);
1085                                 CtdlIPC_getline(ipc, buf);
1086                                 if (buf[0] == '4') {
1087                                         for (i=0; i<num_recs; ++i) {
1088                                                 CtdlIPC_putline(ipc, recs[i]);
1089                                         }
1090                                         CtdlIPC_putline(ipc, "000");
1091                                 }
1092                                 else {
1093                                         scr_printf("%s\n", &buf[4]);
1094                                 }
1095                                 quitting = 1;
1096                                 break;
1097                         case 'q':
1098                                 quitting = boolprompt(
1099                                         "Quit without saving", 0);
1100                                 break;
1101                         default:
1102                                 badkey = 1;
1103                 }
1104         } while (quitting == 0);
1105
1106         if (recs != NULL) {
1107                 for (i=0; i<num_recs; ++i) free(recs[i]);
1108                 free(recs);
1109         }
1110 }
1111
1112 /*
1113  * Filter list configuration
1114  */
1115 void do_filterlist_configuration(CtdlIPC *ipc)
1116 {
1117         char buf[SIZ];
1118         int num_recs = 0;
1119         char **recs = NULL;
1120         char ch;
1121         int badkey;
1122         int i, j;
1123         int quitting = 0;
1124         
1125
1126         snprintf(buf, sizeof buf, "CONF getsys|%s", FILTERLIST);
1127         CtdlIPC_putline(ipc, buf);
1128         CtdlIPC_getline(ipc, buf);
1129         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1130                 ++num_recs;
1131                 if (num_recs == 1) recs = malloc(sizeof(char *));
1132                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1133                 recs[num_recs-1] = malloc(SIZ);
1134                 strcpy(recs[num_recs-1], buf);
1135         }
1136
1137         do {
1138                 scr_printf("\n");
1139                 color(BRIGHT_WHITE);
1140                 scr_printf(     "### "
1141                         "         User name           "
1142                         "         Room name           "
1143                         "    Node name    "
1144                         "\n");
1145                 color(DIM_WHITE);
1146                 scr_printf(     "--- "
1147                         "---------------------------- "
1148                         "---------------------------- "
1149                         "---------------- "
1150                         "\n");
1151                 for (i=0; i<num_recs; ++i) {
1152                 color(DIM_WHITE);
1153                 scr_printf("%3d ", i+1);
1154                 extract(buf, recs[i], 0);
1155                 color(BRIGHT_CYAN);
1156                 scr_printf("%-28s ", buf);
1157                 extract(buf, recs[i], 1);
1158                 color(BRIGHT_MAGENTA);
1159                 scr_printf("%-28s ", buf);
1160                 extract(buf, recs[i], 2);
1161                 color(BRIGHT_CYAN);
1162                 scr_printf("%-16s\n", buf);
1163                 extract(buf, recs[i], 3);
1164                 color(DIM_WHITE);
1165                 }
1166
1167                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1168                 switch(ch) {
1169                         case 'a':
1170                                 ++num_recs;
1171                                 if (num_recs == 1)
1172                                         recs = malloc(sizeof(char *));
1173                                 else recs = realloc(recs,
1174                                         (sizeof(char *)) * num_recs);
1175                                 newprompt("Enter user name: ", buf, 28);
1176                                 strcat(buf, "|");
1177                                 newprompt("Enter room name: ",
1178                                         &buf[strlen(buf)], 28);
1179                                 strcat(buf, "|");
1180                                 newprompt("Enter node name: ",
1181                                         &buf[strlen(buf)], 16);
1182                                 strcat(buf, "|");
1183                                 recs[num_recs-1] = strdup(buf);
1184                                 break;
1185                         case 'd':
1186                                 i = intprompt("Delete which one",
1187                                         1, 1, num_recs) - 1;
1188                                 free(recs[i]);
1189                                 --num_recs;
1190                                 for (j=i; j<num_recs; ++j)
1191                                         recs[j] = recs[j+1];
1192                                 break;
1193                         case 's':
1194                                 snprintf(buf, sizeof buf, "CONF putsys|%s", FILTERLIST);
1195                                 CtdlIPC_putline(ipc, buf);
1196                                 CtdlIPC_getline(ipc, buf);
1197                                 if (buf[0] == '4') {
1198                                         for (i=0; i<num_recs; ++i) {
1199                                                 CtdlIPC_putline(ipc, recs[i]);
1200                                         }
1201                                         CtdlIPC_putline(ipc, "000");
1202                                 }
1203                                 else {
1204                                         scr_printf("%s\n", &buf[4]);
1205                                 }
1206                                 quitting = 1;
1207                                 break;
1208                         case 'q':
1209                                 quitting = boolprompt(
1210                                         "Quit without saving", 0);
1211                                 break;
1212                         default:
1213                                 badkey = 1;
1214                 }
1215         } while (quitting == 0);
1216
1217         if (recs != NULL) {
1218                 for (i=0; i<num_recs; ++i) free(recs[i]);
1219                 free(recs);
1220         }
1221 }
1222
1223