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