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