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