fbfeb4e6a8ad7af6c8cd44f76b79daa01868c585
[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         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
779         sel = intprompt("Which one", 1, 1, 6);
780         switch(sel) {
781                 case 1: strcpy(buf, "localhost");
782                         return;
783                 case 2: strcpy(buf, "gatewaydomain");
784                         return;
785                 case 3: strcpy(buf, "smarthost");
786                         return;
787                 case 4: strcpy(buf, "directory");
788                         return;
789                 case 5: strcpy(buf, "spamassassin");
790                         return;
791                 case 6: strcpy(buf, "rbl");
792                         return;
793         }
794 }
795
796
797 /*
798  * Internet mail configuration
799  */
800 void do_internet_configuration(CtdlIPC *ipc)
801 {
802         char buf[SIZ];
803         char *resp = NULL;
804         int num_recs = 0;
805         char **recs = NULL;
806         char ch;
807         int badkey;
808         int i, j;
809         int quitting = 0;
810         int r;
811         
812         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
813         if (r / 100 == 1) {
814                 while (strlen(resp)) {
815                         extract_token(buf, resp, 0, '\n');
816                         remove_token(resp, 0, '\n');
817                         ++num_recs;
818                         if (num_recs == 1) recs = malloc(sizeof(char *));
819                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
820                         recs[num_recs-1] = malloc(strlen(buf) + 1);
821                         strcpy(recs[num_recs-1], buf);
822                 }
823         }
824         if (resp) free(resp);
825
826         do {
827                 scr_printf("\n");
828                 color(BRIGHT_WHITE);
829                 scr_printf("###                    Host or domain                     Record type      \n");
830                 color(DIM_WHITE);
831                 scr_printf("--- -------------------------------------------------- --------------------\n");
832                 for (i=0; i<num_recs; ++i) {
833                 color(DIM_WHITE);
834                 scr_printf("%3d ", i+1);
835                 extract(buf, recs[i], 0);
836                 color(BRIGHT_CYAN);
837                 scr_printf("%-50s ", buf);
838                 extract(buf, recs[i], 1);
839                 color(BRIGHT_MAGENTA);
840                 scr_printf("%-20s\n", buf);
841                 color(DIM_WHITE);
842                 }
843
844                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
845                 switch(ch) {
846                         case 'a':
847                                 newprompt("Enter host name: ",
848                                         buf, 50);
849                                 striplt(buf);
850                                 if (strlen(buf) > 0) {
851                                         ++num_recs;
852                                         if (num_recs == 1)
853                                                 recs = malloc(sizeof(char *));
854                                         else recs = realloc(recs,
855                                                 (sizeof(char *)) * num_recs);
856                                         strcat(buf, "|");
857                                         get_inet_rec_type(ipc,
858                                                         &buf[strlen(buf)]);
859                                         recs[num_recs-1] = strdup(buf);
860                                 }
861                                 break;
862                         case 'd':
863                                 i = intprompt("Delete which one",
864                                         1, 1, num_recs) - 1;
865                                 free(recs[i]);
866                                 --num_recs;
867                                 for (j=i; j<num_recs; ++j)
868                                         recs[j] = recs[j+1];
869                                 break;
870                         case 's':
871                                 r = 1;
872                                 for (i = 0; i < num_recs; i++)
873                                         r += 1 + strlen(recs[i]);
874                                 resp = (char *)calloc(1, r);
875                                 if (!resp) {
876                                         err_printf("Can't save config - out of memory!\n");
877                                         logoff(ipc, 1);
878                                 }
879                                 for (i = 0; i < num_recs; i++) {
880                                         strcat(resp, recs[i]);
881                                         strcat(resp, "\n");
882                                 }
883                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
884                                 if (r / 100 != 4) {
885                                         err_printf("%s\n", buf);
886                                 }
887                                 quitting = 1;
888                                 break;
889                         case 'q':
890                                 quitting = boolprompt(
891                                         "Quit without saving", 0);
892                                 break;
893                         default:
894                                 badkey = 1;
895                 }
896         } while (quitting == 0);
897
898         if (recs != NULL) {
899                 for (i=0; i<num_recs; ++i) free(recs[i]);
900                 free(recs);
901         }
902 }
903
904
905
906 /*
907  * Edit network configuration for room sharing, mailing lists, etc.
908  */
909 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
910 {
911         char filename[PATH_MAX];
912         char changefile[PATH_MAX];
913         int e_ex_code;
914         pid_t editor_pid;
915         int cksum;
916         int b, i;
917         char buf[SIZ];
918         char instr[SIZ];
919         char addr[SIZ];
920         FILE *tempfp;
921         FILE *changefp;
922
923         if (strlen(editor_paths[0]) == 0) {
924                 scr_printf("You must have an external editor configured in"
925                         " order to use this function.\n");
926                 return;
927         }
928
929         snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
930         snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
931
932         tempfp = fopen(filename, "w");
933         if (tempfp == NULL) {
934                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
935                 return;
936         }
937
938         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
939         fprintf(tempfp, "# %s\n", comment);
940         fprintf(tempfp, "# Specify one per line.\n"
941                         "\n\n");
942
943         CtdlIPC_putline(ipc, "GNET");
944         CtdlIPC_getline(ipc, buf);
945         if (buf[0] == '1') {
946                 while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
947                         extract(instr, buf, 0);
948                         if (!strcasecmp(instr, entrytype)) {
949                                 extract(addr, buf, 1);
950                                 fprintf(tempfp, "%s\n", addr);
951                         }
952                 }
953         }
954         fclose(tempfp);
955
956         e_ex_code = 1;  /* start with a failed exit code */
957         screen_reset();
958         sttybbs(SB_RESTORE);
959         editor_pid = fork();
960         cksum = file_checksum(filename);
961         if (editor_pid == 0) {
962                 chmod(filename, 0600);
963                 putenv("WINDOW_TITLE=Network configuration");
964                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
965                 exit(1);
966         }
967         if (editor_pid > 0) {
968                 do {
969                         e_ex_code = 0;
970                         b = ka_wait(&e_ex_code);
971                 } while ((b != editor_pid) && (b >= 0));
972         editor_pid = (-1);
973         sttybbs(0);
974         screen_set();
975         }
976
977         if (file_checksum(filename) == cksum) {
978                 err_printf("*** Not saving changes.\n");
979                 e_ex_code = 1;
980         }
981
982         if (e_ex_code == 0) {           /* Save changes */
983                 changefp = fopen(changefile, "w");
984                 CtdlIPC_putline(ipc, "GNET");
985                 CtdlIPC_getline(ipc, buf);
986                 if (buf[0] == '1') {
987                         while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
988                                 extract(instr, buf, 0);
989                                 if (strcasecmp(instr, entrytype)) {
990                                         fprintf(changefp, "%s\n", buf);
991                                 }
992                         }
993                 }
994                 tempfp = fopen(filename, "r");
995                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
996                         for (i=0; i<strlen(buf); ++i) {
997                                 if (buf[i] == '#') buf[i] = 0;
998                         }
999                         striplt(buf);
1000                         if (strlen(buf) > 0) {
1001                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1002                         }
1003                 }
1004                 fclose(tempfp);
1005                 fclose(changefp);
1006
1007                 /* now write it to the server... */
1008                 CtdlIPC_putline(ipc, "SNET");
1009                 CtdlIPC_getline(ipc, buf);
1010                 if (buf[0] == '4') {
1011                         changefp = fopen(changefile, "r");
1012                         if (changefp != NULL) {
1013                                 while (fgets(buf, sizeof buf,
1014                                        changefp) != NULL) {
1015                                         buf[strlen(buf) - 1] = 0;
1016                                         CtdlIPC_putline(ipc, buf);
1017                                 }
1018                                 fclose(changefp);
1019                         }
1020                         CtdlIPC_putline(ipc, "000");
1021                 }
1022         }
1023
1024         unlink(filename);               /* Delete the temporary files */
1025         unlink(changefile);
1026 }
1027
1028
1029 /*
1030  * IGnet node configuration
1031  */
1032 void do_ignet_configuration(CtdlIPC *ipc) {
1033         char buf[SIZ];
1034         int num_recs = 0;
1035         char **recs = NULL;
1036         char ch;
1037         int badkey;
1038         int i, j;
1039         int quitting = 0;
1040         
1041
1042         snprintf(buf, sizeof buf, "CONF getsys|%s", IGNETCFG);
1043         CtdlIPC_putline(ipc, buf);
1044         CtdlIPC_getline(ipc, buf);
1045         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1046                 ++num_recs;
1047                 if (num_recs == 1) recs = malloc(sizeof(char *));
1048                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1049                 recs[num_recs-1] = malloc(SIZ);
1050                 strcpy(recs[num_recs-1], buf);
1051         }
1052
1053         do {
1054                 scr_printf("\n");
1055                 color(BRIGHT_WHITE);
1056                 scr_printf(     "### "
1057                         "   Node          "
1058                         "  Secret           "
1059                         "          Host or IP             "
1060                         "Port#\n");
1061                 color(DIM_WHITE);
1062                 scr_printf(     "--- "
1063                         "---------------- "
1064                         "------------------ "
1065                         "-------------------------------- "
1066                         "-----\n");
1067                 for (i=0; i<num_recs; ++i) {
1068                 color(DIM_WHITE);
1069                 scr_printf("%3d ", i+1);
1070                 extract(buf, recs[i], 0);
1071                 color(BRIGHT_CYAN);
1072                 scr_printf("%-16s ", buf);
1073                 extract(buf, recs[i], 1);
1074                 color(BRIGHT_MAGENTA);
1075                 scr_printf("%-18s ", buf);
1076                 extract(buf, recs[i], 2);
1077                 color(BRIGHT_CYAN);
1078                 scr_printf("%-32s ", buf);
1079                 extract(buf, recs[i], 3);
1080                 color(BRIGHT_MAGENTA);
1081                 scr_printf("%-3s\n", buf);
1082                 color(DIM_WHITE);
1083                 }
1084
1085                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1086                 switch(ch) {
1087                         case 'a':
1088                                 ++num_recs;
1089                                 if (num_recs == 1)
1090                                         recs = malloc(sizeof(char *));
1091                                 else recs = realloc(recs,
1092                                         (sizeof(char *)) * num_recs);
1093                                 newprompt("Enter node name    : ", buf, 16);
1094                                 strcat(buf, "|");
1095                                 newprompt("Enter shared secret: ",
1096                                         &buf[strlen(buf)], 18);
1097                                 strcat(buf, "|");
1098                                 newprompt("Enter host or IP   : ",
1099                                         &buf[strlen(buf)], 32);
1100                                 strcat(buf, "|504");
1101                                 strprompt("Enter port number  : ",
1102                                         &buf[strlen(buf)-3], 5);
1103                                 recs[num_recs-1] = strdup(buf);
1104                                 break;
1105                         case 'd':
1106                                 i = intprompt("Delete which one",
1107                                         1, 1, num_recs) - 1;
1108                                 free(recs[i]);
1109                                 --num_recs;
1110                                 for (j=i; j<num_recs; ++j)
1111                                         recs[j] = recs[j+1];
1112                                 break;
1113                         case 's':
1114                                 snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
1115                                 CtdlIPC_putline(ipc, buf);
1116                                 CtdlIPC_getline(ipc, buf);
1117                                 if (buf[0] == '4') {
1118                                         for (i=0; i<num_recs; ++i) {
1119                                                 CtdlIPC_putline(ipc, recs[i]);
1120                                         }
1121                                         CtdlIPC_putline(ipc, "000");
1122                                 }
1123                                 else {
1124                                         scr_printf("%s\n", &buf[4]);
1125                                 }
1126                                 quitting = 1;
1127                                 break;
1128                         case 'q':
1129                                 quitting = boolprompt(
1130                                         "Quit without saving", 0);
1131                                 break;
1132                         default:
1133                                 badkey = 1;
1134                 }
1135         } while (quitting == 0);
1136
1137         if (recs != NULL) {
1138                 for (i=0; i<num_recs; ++i) free(recs[i]);
1139                 free(recs);
1140         }
1141 }
1142
1143 /*
1144  * Filter list configuration
1145  */
1146 void do_filterlist_configuration(CtdlIPC *ipc)
1147 {
1148         char buf[SIZ];
1149         int num_recs = 0;
1150         char **recs = NULL;
1151         char ch;
1152         int badkey;
1153         int i, j;
1154         int quitting = 0;
1155         
1156
1157         snprintf(buf, sizeof buf, "CONF getsys|%s", FILTERLIST);
1158         CtdlIPC_putline(ipc, buf);
1159         CtdlIPC_getline(ipc, buf);
1160         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1161                 ++num_recs;
1162                 if (num_recs == 1) recs = malloc(sizeof(char *));
1163                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1164                 recs[num_recs-1] = malloc(SIZ);
1165                 strcpy(recs[num_recs-1], buf);
1166         }
1167
1168         do {
1169                 scr_printf("\n");
1170                 color(BRIGHT_WHITE);
1171                 scr_printf(     "### "
1172                         "         User name           "
1173                         "         Room name           "
1174                         "    Node name    "
1175                         "\n");
1176                 color(DIM_WHITE);
1177                 scr_printf(     "--- "
1178                         "---------------------------- "
1179                         "---------------------------- "
1180                         "---------------- "
1181                         "\n");
1182                 for (i=0; i<num_recs; ++i) {
1183                 color(DIM_WHITE);
1184                 scr_printf("%3d ", i+1);
1185                 extract(buf, recs[i], 0);
1186                 color(BRIGHT_CYAN);
1187                 scr_printf("%-28s ", buf);
1188                 extract(buf, recs[i], 1);
1189                 color(BRIGHT_MAGENTA);
1190                 scr_printf("%-28s ", buf);
1191                 extract(buf, recs[i], 2);
1192                 color(BRIGHT_CYAN);
1193                 scr_printf("%-16s\n", buf);
1194                 extract(buf, recs[i], 3);
1195                 color(DIM_WHITE);
1196                 }
1197
1198                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1199                 switch(ch) {
1200                         case 'a':
1201                                 ++num_recs;
1202                                 if (num_recs == 1)
1203                                         recs = malloc(sizeof(char *));
1204                                 else recs = realloc(recs,
1205                                         (sizeof(char *)) * num_recs);
1206                                 newprompt("Enter user name: ", buf, 28);
1207                                 strcat(buf, "|");
1208                                 newprompt("Enter room name: ",
1209                                         &buf[strlen(buf)], 28);
1210                                 strcat(buf, "|");
1211                                 newprompt("Enter node name: ",
1212                                         &buf[strlen(buf)], 16);
1213                                 strcat(buf, "|");
1214                                 recs[num_recs-1] = strdup(buf);
1215                                 break;
1216                         case 'd':
1217                                 i = intprompt("Delete which one",
1218                                         1, 1, num_recs) - 1;
1219                                 free(recs[i]);
1220                                 --num_recs;
1221                                 for (j=i; j<num_recs; ++j)
1222                                         recs[j] = recs[j+1];
1223                                 break;
1224                         case 's':
1225                                 snprintf(buf, sizeof buf, "CONF putsys|%s", FILTERLIST);
1226                                 CtdlIPC_putline(ipc, buf);
1227                                 CtdlIPC_getline(ipc, buf);
1228                                 if (buf[0] == '4') {
1229                                         for (i=0; i<num_recs; ++i) {
1230                                                 CtdlIPC_putline(ipc, recs[i]);
1231                                         }
1232                                         CtdlIPC_putline(ipc, "000");
1233                                 }
1234                                 else {
1235                                         scr_printf("%s\n", &buf[4]);
1236                                 }
1237                                 quitting = 1;
1238                                 break;
1239                         case 'q':
1240                                 quitting = boolprompt(
1241                                         "Quit without saving", 0);
1242                                 break;
1243                         default:
1244                                 badkey = 1;
1245                 }
1246         } while (quitting == 0);
1247
1248         if (recs != NULL) {
1249                 for (i=0; i<num_recs; ++i) free(recs[i]);
1250                 free(recs);
1251         }
1252 }
1253
1254