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