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