* Converted more routines to new IPC code.
[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         char *resp = NULL;
407         int ax = 0;
408         int r;                          /* IPC response code */
409
410         r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
411         if (r / 100 == 1) {
412                 a = 0;
413                 do {
414                         extract_token(buf, resp, 0, '\n');
415                         remove_token(resp, 0, '\n');
416                         ++a;
417                         if (a == 1)
418                                 scr_printf("User #%s - %s  ", buf, cmd);
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 (strlen(resp));
440                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
441         } else {
442                 scr_printf("%s\n%s\n", user, &cmd[4]);
443         }
444         if (resp) free(resp);
445
446         if (do_validate) {
447                 /* now set the access level */
448                 ax = intprompt("Access level", ax, 0, 6);
449                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
450                 if (r / 100 != 2)
451                         scr_printf("%s\n", cmd);
452         }
453         scr_printf("\n");
454 }
455
456
457 void validate(CtdlIPC *ipc)
458 {                               /* validate new users */
459         char cmd[SIZ];
460         char buf[SIZ];
461         int finished = 0;
462         int r;                          /* IPC response code */
463
464         do {
465                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
466                 if (r / 100 != 3)
467                         finished = 1;
468                 if (r / 100 == 2)
469                         scr_printf("%s\n", cmd);
470                 if (r / 100 == 3) {
471                         extract(buf, cmd, 0);
472                         val_user(ipc, buf, 1);
473                 }
474         } while (finished == 0);
475 }
476
477 void subshell(void)
478 {
479         int a, b;
480         a = fork();
481         if (a == 0) {
482                 screen_reset();
483                 sttybbs(SB_RESTORE);
484                 signal(SIGINT, SIG_DFL);
485                 signal(SIGQUIT, SIG_DFL);
486                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
487                 err_printf("Could not open a shell: %s\n", strerror(errno));
488                 exit(errno);
489         }
490         do {
491                 b = ka_wait(NULL);
492         } while ((a != b) && (a != (-1)));
493         sttybbs(0);
494         screen_set();
495 }
496
497 /*
498  * <.A>ide <F>ile <D>elete command
499  */
500 void deletefile(CtdlIPC *ipc)
501 {
502         char filename[32];
503         char buf[SIZ];
504
505         newprompt("Filename: ", filename, 31);
506         if (strlen(filename) == 0)
507                 return;
508         CtdlIPCDeleteFile(ipc, filename, buf);
509         err_printf("%s\n", buf);
510 }
511
512 /*
513  * <.A>ide <F>ile <S>end command
514  */
515 void netsendfile(CtdlIPC *ipc)
516 {
517         char filename[32], destsys[20], buf[SIZ];
518
519         newprompt("Filename: ", filename, 31);
520         if (strlen(filename) == 0)
521                 return;
522         newprompt("System to send to: ", destsys, 19);
523         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
524         err_printf("%s\n", buf);
525         return;
526 }
527
528 /*
529  * <.A>ide <F>ile <M>ove command
530  */
531 void movefile(CtdlIPC *ipc)
532 {
533         char filename[64];
534         char newroom[ROOMNAMELEN];
535         char buf[SIZ];
536
537         newprompt("Filename: ", filename, 63);
538         if (strlen(filename) == 0)
539                 return;
540         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
541         CtdlIPCMoveFile(ipc, filename, newroom, buf);
542         err_printf("%s\n", buf);
543 }
544
545
546 /* 
547  * list of users who have filled out a bio
548  */
549 void list_bio(CtdlIPC *ipc)
550 {
551         char buf[SIZ];
552         char *resp = NULL;
553         int pos = 1;
554         int r;                  /* IPC response code */
555
556         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
557         if (r / 100 != 1) {
558                 pprintf("%s\n", buf);
559                 return;
560         }
561         while (strlen(resp)) {
562                 extract_token(buf, resp, 0, '\n');
563                 remove_token(resp, 0, '\n');
564                 if ((pos + strlen(buf) + 5) > screenwidth) {
565                         pprintf("\n");
566                         pos = 1;
567                 }
568                 pprintf("%s, ", buf);
569                 pos = pos + strlen(buf) + 2;
570         }
571         pprintf("%c%c  \n\n", 8, 8);
572         if (resp) free(resp);
573 }
574
575
576 /*
577  * read bio
578  */
579 void read_bio(CtdlIPC *ipc)
580 {
581         char who[SIZ];
582         char buf[SIZ];
583         char *resp = NULL;
584         int r;                  /* IPC response code */
585
586         do {
587                 newprompt("Read bio for who ('?' for list) : ", who, 25);
588                 pprintf("\n");
589                 if (!strcmp(who, "?"))
590                         list_bio(ipc);
591         } while (!strcmp(who, "?"));
592
593         r = CtdlIPCGetBio(ipc, who, &resp, buf);
594         if (r / 100 != 1) {
595                 pprintf("%s\n", buf);
596                 return;
597         }
598         while (strlen(resp)) {
599                 extract_token(buf, resp, 0, '\n');
600                 remove_token(resp, 0, '\n');
601                 pprintf("%s\n", buf);
602         }
603         if (resp) free(resp);
604 }
605
606
607 /* 
608  * General system configuration command
609  */
610 void do_system_configuration(CtdlIPC *ipc)
611 {
612         char buf[SIZ];
613         char sc[31][SIZ];
614         char *resp = NULL;
615         struct ExpirePolicy *expirepolicy = NULL;
616         int a;
617         int logpages = 0;
618         int r;                  /* IPC response code */
619
620         /* Clear out the config buffers */
621         memset(&sc[0][0], 0, sizeof(sc));
622
623         /* Fetch the current config */
624         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
625         if (r / 100 == 1) {
626                 a = 0;
627                 while (strlen(resp)) {
628                         extract_token(buf, resp, 0, '\n');
629                         remove_token(resp, 0, '\n');
630                         if (a < 31) {
631                                 strcpy(&sc[a][0], buf);
632                         }
633                         ++a;
634                 }
635         }
636         if (resp) free(resp);
637         resp = NULL;
638         /* Fetch the expire policy (this will silently fail on old servers,
639          * resulting in "default" policy)
640          */
641         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &expirepolicy, buf);
642
643         /* Identification parameters */
644
645         strprompt("Node name", &sc[0][0], 15);
646         strprompt("Fully qualified domain name", &sc[1][0], 63);
647         strprompt("Human readable node name", &sc[2][0], 20);
648         strprompt("Modem dialup number", &sc[3][0], 15);
649         strprompt("Geographic location of this system", &sc[12][0], 31);
650         strprompt("Name of system administrator", &sc[13][0], 25);
651         strprompt("Paginator prompt", &sc[10][0], 79);
652
653         /* Security parameters */
654
655         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
656                                     "Require registration for new users",
657                                                     atoi(&sc[7][0]))));
658         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
659               "Disable self-service user account creation",
660                                                      atoi(&sc[29][0]))));
661         strprompt("Initial access level for new users", &sc[6][0], 1);
662         strprompt("Access level required to create rooms", &sc[19][0], 1);
663         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
664                                                     "Automatically give room aide privs to a user who creates a private room",
665                                                     atoi(&sc[4][0]))));
666
667         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
668                  "Automatically move problem user messages to twit room",
669                                                     atoi(&sc[8][0]))));
670
671         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
672         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
673               "Restrict Internet mail to only those with that privilege",
674                                                      atoi(&sc[11][0]))));
675         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
676               "Allow Aides to Zap (forget) rooms",
677                                                      atoi(&sc[26][0]))));
678         snprintf(sc[30], sizeof sc[29], "%d", (boolprompt(
679               "Allow system Aides access to user mailboxes",
680                                                      atoi(&sc[30][0]))));
681
682         if (strlen(&sc[18][0]) > 0) logpages = 1;
683         else logpages = 0;
684         logpages = boolprompt("Log all pages", logpages);
685         if (logpages) {
686                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
687         }
688         else {
689                 sc[18][0] = 0;
690         }
691
692
693         /* Server tuning */
694
695         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
696         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
697         strprompt("Maximum message length", &sc[20][0], 20);
698         strprompt("Minimum number of worker threads", &sc[21][0], 3);
699         strprompt("Maximum number of worker threads", &sc[22][0], 3);
700
701         /* no longer applicable ... deprecated
702         strprompt("Server-to-server networking password", &sc[15][0], 19);
703         */
704
705         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
706         strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
707         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
708         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
709
710         /* Expiry settings */
711         strprompt("Default user purge time (days)", &sc[16][0], 5);
712         strprompt("Default room purge time (days)", &sc[17][0], 5);
713
714         /* Angels and demons dancing in my head... */
715         do {
716                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_mode);
717                 strprompt("System default message expire policy (? for list)",
718                           buf, 1);
719                 if (buf[0] == '?') {
720                         scr_printf("\n"
721                                 "1. Never automatically expire messages\n"
722                                 "2. Expire by message count\n"
723                                 "3. Expire by message age\n");
724                 }
725         } while ((buf[0] < '1') || (buf[0] > '3'));
726         expirepolicy->expire_mode = buf[0] - '0';
727
728         /* ...lunatics and monsters underneath my bed */
729         if (expirepolicy->expire_mode == 2) {
730                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_value);
731                 strprompt("Keep how many messages online?", buf, 10);
732                 expirepolicy->expire_value = atol(buf);
733         }
734         if (expirepolicy->expire_mode == 3) {
735                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_value);
736                 strprompt("Keep messages for how many days?", buf, 10);
737                 expirepolicy->expire_value = atol(buf);
738         }
739         /* Save it */
740         scr_printf("Save this configuration? ");
741         if (yesno()) {
742                 r = 1;
743                 for (a = 0; a < 31; a++)
744                         r += 1 + strlen(sc[a]);
745                 resp = (char *)calloc(1, r);
746                 if (!resp) {
747                         err_printf("Can't save config - out of memory!\n");
748                         logoff(ipc, 1);
749                 }
750                 for (a = 0; a < 31; a++) {
751                         strcat(resp, sc[a]);
752                         strcat(resp, "\n");
753                 }
754                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
755                 if (r / 100 != 4) {
756                         err_printf("%s\n", buf);
757                 }
758                 free(resp);
759                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, expirepolicy, buf);
760                 if (r / 100 != 2) {
761                         err_printf("%s\n", buf);
762                 }
763         }
764 }
765
766
767 /*
768  * support function for do_internet_configuration()
769  */
770 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
771         int sel;
772
773         keyopt(" <1> localhost      (Alias for this computer)\n");
774         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
775         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
776         keyopt(" <4> directory      (Consult the Global Address Book)\n");
777         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
778         sel = intprompt("Which one", 1, 1, 5);
779         switch(sel) {
780                 case 1: strcpy(buf, "localhost");
781                         return;
782                 case 2: strcpy(buf, "gatewaydomain");
783                         return;
784                 case 3: strcpy(buf, "smarthost");
785                         return;
786                 case 4: strcpy(buf, "directory");
787                         return;
788                 case 5: strcpy(buf, "spamassassin");
789                         return;
790         }
791 }
792
793
794 /*
795  * Internet mail configuration
796  */
797 void do_internet_configuration(CtdlIPC *ipc)
798 {
799         char buf[SIZ];
800         char *resp = NULL;
801         int num_recs = 0;
802         char **recs = NULL;
803         char ch;
804         int badkey;
805         int i, j;
806         int quitting = 0;
807         int r;
808         
809         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
810         if (r / 100 == 1) {
811                 while (strlen(resp)) {
812                         extract_token(buf, resp, 0, '\n');
813                         remove_token(resp, 0, '\n');
814                         ++num_recs;
815                         if (num_recs == 1) recs = malloc(sizeof(char *));
816                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
817                         recs[num_recs-1] = malloc(strlen(buf) + 1);
818                         strcpy(recs[num_recs-1], buf);
819                 }
820         }
821         if (resp) free(resp);
822
823         do {
824                 scr_printf("\n");
825                 color(BRIGHT_WHITE);
826                 scr_printf("###                    Host or domain                     Record type      \n");
827                 color(DIM_WHITE);
828                 scr_printf("--- -------------------------------------------------- --------------------\n");
829                 for (i=0; i<num_recs; ++i) {
830                 color(DIM_WHITE);
831                 scr_printf("%3d ", i+1);
832                 extract(buf, recs[i], 0);
833                 color(BRIGHT_CYAN);
834                 scr_printf("%-50s ", buf);
835                 extract(buf, recs[i], 1);
836                 color(BRIGHT_MAGENTA);
837                 scr_printf("%-20s\n", buf);
838                 color(DIM_WHITE);
839                 }
840
841                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
842                 switch(ch) {
843                         case 'a':
844                                 ++num_recs;
845                                 if (num_recs == 1)
846                                         recs = malloc(sizeof(char *));
847                                 else recs = realloc(recs,
848                                         (sizeof(char *)) * num_recs);
849                                 newprompt("Enter host name: ",
850                                         buf, 50);
851                                 strcat(buf, "|");
852                                 get_inet_rec_type(ipc, &buf[strlen(buf)]);
853                                 recs[num_recs-1] = strdup(buf);
854                                 break;
855                         case 'd':
856                                 i = intprompt("Delete which one",
857                                         1, 1, num_recs) - 1;
858                                 free(recs[i]);
859                                 --num_recs;
860                                 for (j=i; j<num_recs; ++j)
861                                         recs[j] = recs[j+1];
862                                 break;
863                         case 's':
864                                 r = 1;
865                                 for (i = 0; i < num_recs; i++)
866                                         r += 1 + strlen(recs[i]);
867                                 resp = (char *)calloc(1, r);
868                                 if (!resp) {
869                                         err_printf("Can't save config - out of memory!\n");
870                                         logoff(ipc, 1);
871                                 }
872                                 for (i = 0; i < num_recs; i++) {
873                                         strcat(resp, recs[i]);
874                                         strcat(resp, "\n");
875                                 }
876                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
877                                 if (r / 100 != 4) {
878                                         err_printf("%s\n", buf);
879                                 }
880                                 quitting = 1;
881                                 break;
882                         case 'q':
883                                 quitting = boolprompt(
884                                         "Quit without saving", 0);
885                                 break;
886                         default:
887                                 badkey = 1;
888                 }
889         } while (quitting == 0);
890
891         if (recs != NULL) {
892                 for (i=0; i<num_recs; ++i) free(recs[i]);
893                 free(recs);
894         }
895 }
896
897
898
899 /*
900  * Edit network configuration for room sharing, mailing lists, etc.
901  */
902 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
903 {
904         char filename[PATH_MAX];
905         char changefile[PATH_MAX];
906         int e_ex_code;
907         pid_t editor_pid;
908         int cksum;
909         int b, i;
910         char buf[SIZ];
911         char instr[SIZ];
912         char addr[SIZ];
913         FILE *tempfp;
914         FILE *changefp;
915
916         if (strlen(editor_path) == 0) {
917                 scr_printf("You must have an external editor configured in"
918                         " order to use this function.\n");
919                 return;
920         }
921
922         snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
923         snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
924
925         tempfp = fopen(filename, "w");
926         if (tempfp == NULL) {
927                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
928                 return;
929         }
930
931         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
932         fprintf(tempfp, "# %s\n", comment);
933         fprintf(tempfp, "# Specify one per line.\n"
934                         "\n\n");
935
936         CtdlIPC_putline(ipc, "GNET");
937         CtdlIPC_getline(ipc, buf);
938         if (buf[0] == '1') {
939                 while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
940                         extract(instr, buf, 0);
941                         if (!strcasecmp(instr, entrytype)) {
942                                 extract(addr, buf, 1);
943                                 fprintf(tempfp, "%s\n", addr);
944                         }
945                 }
946         }
947         fclose(tempfp);
948
949         e_ex_code = 1;  /* start with a failed exit code */
950         screen_reset();
951         sttybbs(SB_RESTORE);
952         editor_pid = fork();
953         cksum = file_checksum(filename);
954         if (editor_pid == 0) {
955                 chmod(filename, 0600);
956                 putenv("WINDOW_TITLE=Network configuration");
957                 execlp(editor_path, editor_path, filename, NULL);
958                 exit(1);
959         }
960         if (editor_pid > 0) {
961                 do {
962                         e_ex_code = 0;
963                         b = ka_wait(&e_ex_code);
964                 } while ((b != editor_pid) && (b >= 0));
965         editor_pid = (-1);
966         sttybbs(0);
967         screen_set();
968         }
969
970         if (file_checksum(filename) == cksum) {
971                 err_printf("*** Not saving changes.\n");
972                 e_ex_code = 1;
973         }
974
975         if (e_ex_code == 0) {           /* Save changes */
976                 changefp = fopen(changefile, "w");
977                 CtdlIPC_putline(ipc, "GNET");
978                 CtdlIPC_getline(ipc, buf);
979                 if (buf[0] == '1') {
980                         while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
981                                 extract(instr, buf, 0);
982                                 if (strcasecmp(instr, entrytype)) {
983                                         fprintf(changefp, "%s\n", buf);
984                                 }
985                         }
986                 }
987                 tempfp = fopen(filename, "r");
988                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
989                         for (i=0; i<strlen(buf); ++i) {
990                                 if (buf[i] == '#') buf[i] = 0;
991                         }
992                         striplt(buf);
993                         if (strlen(buf) > 0) {
994                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
995                         }
996                 }
997                 fclose(tempfp);
998                 fclose(changefp);
999
1000                 /* now write it to the server... */
1001                 CtdlIPC_putline(ipc, "SNET");
1002                 CtdlIPC_getline(ipc, buf);
1003                 if (buf[0] == '4') {
1004                         changefp = fopen(changefile, "r");
1005                         if (changefp != NULL) {
1006                                 while (fgets(buf, sizeof buf,
1007                                        changefp) != NULL) {
1008                                         buf[strlen(buf) - 1] = 0;
1009                                         CtdlIPC_putline(ipc, buf);
1010                                 }
1011                                 fclose(changefp);
1012                         }
1013                         CtdlIPC_putline(ipc, "000");
1014                 }
1015         }
1016
1017         unlink(filename);               /* Delete the temporary files */
1018         unlink(changefile);
1019 }
1020
1021
1022 /*
1023  * IGnet node configuration
1024  */
1025 void do_ignet_configuration(CtdlIPC *ipc) {
1026         char buf[SIZ];
1027         int num_recs = 0;
1028         char **recs = NULL;
1029         char ch;
1030         int badkey;
1031         int i, j;
1032         int quitting = 0;
1033         
1034
1035         snprintf(buf, sizeof buf, "CONF getsys|%s", IGNETCFG);
1036         CtdlIPC_putline(ipc, buf);
1037         CtdlIPC_getline(ipc, buf);
1038         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1039                 ++num_recs;
1040                 if (num_recs == 1) recs = malloc(sizeof(char *));
1041                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1042                 recs[num_recs-1] = malloc(SIZ);
1043                 strcpy(recs[num_recs-1], buf);
1044         }
1045
1046         do {
1047                 scr_printf("\n");
1048                 color(BRIGHT_WHITE);
1049                 scr_printf(     "### "
1050                         "   Node          "
1051                         "  Secret           "
1052                         "          Host or IP             "
1053                         "Port#\n");
1054                 color(DIM_WHITE);
1055                 scr_printf(     "--- "
1056                         "---------------- "
1057                         "------------------ "
1058                         "-------------------------------- "
1059                         "-----\n");
1060                 for (i=0; i<num_recs; ++i) {
1061                 color(DIM_WHITE);
1062                 scr_printf("%3d ", i+1);
1063                 extract(buf, recs[i], 0);
1064                 color(BRIGHT_CYAN);
1065                 scr_printf("%-16s ", buf);
1066                 extract(buf, recs[i], 1);
1067                 color(BRIGHT_MAGENTA);
1068                 scr_printf("%-18s ", buf);
1069                 extract(buf, recs[i], 2);
1070                 color(BRIGHT_CYAN);
1071                 scr_printf("%-32s ", buf);
1072                 extract(buf, recs[i], 3);
1073                 color(BRIGHT_MAGENTA);
1074                 scr_printf("%-3s\n", buf);
1075                 color(DIM_WHITE);
1076                 }
1077
1078                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1079                 switch(ch) {
1080                         case 'a':
1081                                 ++num_recs;
1082                                 if (num_recs == 1)
1083                                         recs = malloc(sizeof(char *));
1084                                 else recs = realloc(recs,
1085                                         (sizeof(char *)) * num_recs);
1086                                 newprompt("Enter node name    : ", buf, 16);
1087                                 strcat(buf, "|");
1088                                 newprompt("Enter shared secret: ",
1089                                         &buf[strlen(buf)], 18);
1090                                 strcat(buf, "|");
1091                                 newprompt("Enter host or IP   : ",
1092                                         &buf[strlen(buf)], 32);
1093                                 strcat(buf, "|504");
1094                                 strprompt("Enter port number  : ",
1095                                         &buf[strlen(buf)-3], 5);
1096                                 recs[num_recs-1] = strdup(buf);
1097                                 break;
1098                         case 'd':
1099                                 i = intprompt("Delete which one",
1100                                         1, 1, num_recs) - 1;
1101                                 free(recs[i]);
1102                                 --num_recs;
1103                                 for (j=i; j<num_recs; ++j)
1104                                         recs[j] = recs[j+1];
1105                                 break;
1106                         case 's':
1107                                 snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
1108                                 CtdlIPC_putline(ipc, buf);
1109                                 CtdlIPC_getline(ipc, buf);
1110                                 if (buf[0] == '4') {
1111                                         for (i=0; i<num_recs; ++i) {
1112                                                 CtdlIPC_putline(ipc, recs[i]);
1113                                         }
1114                                         CtdlIPC_putline(ipc, "000");
1115                                 }
1116                                 else {
1117                                         scr_printf("%s\n", &buf[4]);
1118                                 }
1119                                 quitting = 1;
1120                                 break;
1121                         case 'q':
1122                                 quitting = boolprompt(
1123                                         "Quit without saving", 0);
1124                                 break;
1125                         default:
1126                                 badkey = 1;
1127                 }
1128         } while (quitting == 0);
1129
1130         if (recs != NULL) {
1131                 for (i=0; i<num_recs; ++i) free(recs[i]);
1132                 free(recs);
1133         }
1134 }
1135
1136 /*
1137  * Filter list configuration
1138  */
1139 void do_filterlist_configuration(CtdlIPC *ipc)
1140 {
1141         char buf[SIZ];
1142         int num_recs = 0;
1143         char **recs = NULL;
1144         char ch;
1145         int badkey;
1146         int i, j;
1147         int quitting = 0;
1148         
1149
1150         snprintf(buf, sizeof buf, "CONF getsys|%s", FILTERLIST);
1151         CtdlIPC_putline(ipc, buf);
1152         CtdlIPC_getline(ipc, buf);
1153         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1154                 ++num_recs;
1155                 if (num_recs == 1) recs = malloc(sizeof(char *));
1156                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1157                 recs[num_recs-1] = malloc(SIZ);
1158                 strcpy(recs[num_recs-1], buf);
1159         }
1160
1161         do {
1162                 scr_printf("\n");
1163                 color(BRIGHT_WHITE);
1164                 scr_printf(     "### "
1165                         "         User name           "
1166                         "         Room name           "
1167                         "    Node name    "
1168                         "\n");
1169                 color(DIM_WHITE);
1170                 scr_printf(     "--- "
1171                         "---------------------------- "
1172                         "---------------------------- "
1173                         "---------------- "
1174                         "\n");
1175                 for (i=0; i<num_recs; ++i) {
1176                 color(DIM_WHITE);
1177                 scr_printf("%3d ", i+1);
1178                 extract(buf, recs[i], 0);
1179                 color(BRIGHT_CYAN);
1180                 scr_printf("%-28s ", buf);
1181                 extract(buf, recs[i], 1);
1182                 color(BRIGHT_MAGENTA);
1183                 scr_printf("%-28s ", buf);
1184                 extract(buf, recs[i], 2);
1185                 color(BRIGHT_CYAN);
1186                 scr_printf("%-16s\n", buf);
1187                 extract(buf, recs[i], 3);
1188                 color(DIM_WHITE);
1189                 }
1190
1191                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1192                 switch(ch) {
1193                         case 'a':
1194                                 ++num_recs;
1195                                 if (num_recs == 1)
1196                                         recs = malloc(sizeof(char *));
1197                                 else recs = realloc(recs,
1198                                         (sizeof(char *)) * num_recs);
1199                                 newprompt("Enter user name: ", buf, 28);
1200                                 strcat(buf, "|");
1201                                 newprompt("Enter room name: ",
1202                                         &buf[strlen(buf)], 28);
1203                                 strcat(buf, "|");
1204                                 newprompt("Enter node name: ",
1205                                         &buf[strlen(buf)], 16);
1206                                 strcat(buf, "|");
1207                                 recs[num_recs-1] = strdup(buf);
1208                                 break;
1209                         case 'd':
1210                                 i = intprompt("Delete which one",
1211                                         1, 1, num_recs) - 1;
1212                                 free(recs[i]);
1213                                 --num_recs;
1214                                 for (j=i; j<num_recs; ++j)
1215                                         recs[j] = recs[j+1];
1216                                 break;
1217                         case 's':
1218                                 snprintf(buf, sizeof buf, "CONF putsys|%s", FILTERLIST);
1219                                 CtdlIPC_putline(ipc, buf);
1220                                 CtdlIPC_getline(ipc, buf);
1221                                 if (buf[0] == '4') {
1222                                         for (i=0; i<num_recs; ++i) {
1223                                                 CtdlIPC_putline(ipc, recs[i]);
1224                                         }
1225                                         CtdlIPC_putline(ipc, "000");
1226                                 }
1227                                 else {
1228                                         scr_printf("%s\n", &buf[4]);
1229                                 }
1230                                 quitting = 1;
1231                                 break;
1232                         case 'q':
1233                                 quitting = boolprompt(
1234                                         "Quit without saving", 0);
1235                                 break;
1236                         default:
1237                                 badkey = 1;
1238                 }
1239         } while (quitting == 0);
1240
1241         if (recs != NULL) {
1242                 for (i=0; i<num_recs; ++i) free(recs[i]);
1243                 free(recs);
1244         }
1245 }
1246
1247