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