640a16e68f53ffe49dbe5cd20bd974b676e232e8
[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                         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         a = fork();
514         if (a == 0) {
515                 screen_reset();
516                 sttybbs(SB_RESTORE);
517                 signal(SIGINT, SIG_DFL);
518                 signal(SIGQUIT, SIG_DFL);
519                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
520                 err_printf("Could not open a shell: %s\n", strerror(errno));
521                 exit(errno);
522         }
523         do {
524                 b = ka_wait(NULL);
525         } while ((a != b) && (a != (-1)));
526         sttybbs(0);
527         screen_set();
528 }
529
530 /*
531  * <.A>ide <F>ile <D>elete command
532  */
533 void deletefile(CtdlIPC *ipc)
534 {
535         char filename[32];
536         char buf[SIZ];
537
538         newprompt("Filename: ", filename, 31);
539         if (strlen(filename) == 0)
540                 return;
541         CtdlIPCDeleteFile(ipc, filename, buf);
542         err_printf("%s\n", buf);
543 }
544
545 /*
546  * <.A>ide <F>ile <S>end command
547  */
548 void netsendfile(CtdlIPC *ipc)
549 {
550         char filename[32], destsys[20], buf[SIZ];
551
552         newprompt("Filename: ", filename, 31);
553         if (strlen(filename) == 0)
554                 return;
555         newprompt("System to send to: ", destsys, 19);
556         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
557         err_printf("%s\n", buf);
558         return;
559 }
560
561 /*
562  * <.A>ide <F>ile <M>ove command
563  */
564 void movefile(CtdlIPC *ipc)
565 {
566         char filename[64];
567         char newroom[ROOMNAMELEN];
568         char buf[SIZ];
569
570         newprompt("Filename: ", filename, 63);
571         if (strlen(filename) == 0)
572                 return;
573         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
574         CtdlIPCMoveFile(ipc, filename, newroom, buf);
575         err_printf("%s\n", buf);
576 }
577
578
579 /* 
580  * list of users who have filled out a bio
581  */
582 void list_bio(CtdlIPC *ipc)
583 {
584         char buf[SIZ];
585         char *resp = NULL;
586         int pos = 1;
587         int r;                  /* IPC response code */
588
589         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
590         if (r / 100 != 1) {
591                 pprintf("%s\n", buf);
592                 return;
593         }
594         while (strlen(resp)) {
595                 extract_token(buf, resp, 0, '\n');
596                 remove_token(resp, 0, '\n');
597                 if ((pos + strlen(buf) + 5) > screenwidth) {
598                         pprintf("\n");
599                         pos = 1;
600                 }
601                 pprintf("%s, ", buf);
602                 pos = pos + strlen(buf) + 2;
603         }
604         pprintf("%c%c  \n\n", 8, 8);
605         if (resp) free(resp);
606 }
607
608
609 /*
610  * read bio
611  */
612 void read_bio(CtdlIPC *ipc)
613 {
614         char who[SIZ];
615         char buf[SIZ];
616         char *resp = NULL;
617         int r;                  /* IPC response code */
618
619         do {
620                 newprompt("Read bio for who ('?' for list) : ", who, 25);
621                 pprintf("\n");
622                 if (!strcmp(who, "?"))
623                         list_bio(ipc);
624         } while (!strcmp(who, "?"));
625
626         r = CtdlIPCGetBio(ipc, who, &resp, buf);
627         if (r / 100 != 1) {
628                 pprintf("%s\n", buf);
629                 return;
630         }
631         while (strlen(resp)) {
632                 extract_token(buf, resp, 0, '\n');
633                 remove_token(resp, 0, '\n');
634                 pprintf("%s\n", buf);
635         }
636         if (resp) free(resp);
637 }
638
639
640 /* 
641  * General system configuration command
642  */
643 void do_system_configuration(CtdlIPC *ipc)
644 {
645
646 #define NUM_CONFIGS 34
647
648         char buf[SIZ];
649         char sc[NUM_CONFIGS][SIZ];
650         char *resp = NULL;
651         struct ExpirePolicy *site_expirepolicy = NULL;
652         struct ExpirePolicy *mbx_expirepolicy = NULL;
653         int a;
654         int logpages = 0;
655         int r;                  /* IPC response code */
656
657         /* Clear out the config buffers */
658         memset(&sc[0][0], 0, sizeof(sc));
659
660         /* Fetch the current config */
661         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
662         if (r / 100 == 1) {
663                 a = 0;
664                 while (strlen(resp)) {
665                         extract_token(buf, resp, 0, '\n');
666                         remove_token(resp, 0, '\n');
667                         if (a < NUM_CONFIGS) {
668                                 strcpy(&sc[a][0], buf);
669                         }
670                         ++a;
671                 }
672         }
673         if (resp) free(resp);
674         resp = NULL;
675         /* Fetch the expire policy (this will silently fail on old servers,
676          * resulting in "default" policy)
677          */
678         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
679         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
680
681         /* Identification parameters */
682
683         strprompt("Node name", &sc[0][0], 15);
684         strprompt("Fully qualified domain name", &sc[1][0], 63);
685         strprompt("Human readable node name", &sc[2][0], 20);
686         strprompt("Modem dialup number", &sc[3][0], 15);
687         strprompt("Geographic location of this system", &sc[12][0], 31);
688         strprompt("Name of system administrator", &sc[13][0], 25);
689         strprompt("Paginator prompt", &sc[10][0], 79);
690
691         /* Security parameters */
692
693         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
694                                     "Require registration for new users",
695                                                     atoi(&sc[7][0]))));
696         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
697               "Disable self-service user account creation",
698                                                      atoi(&sc[29][0]))));
699         strprompt("Initial access level for new users", &sc[6][0], 1);
700         strprompt("Access level required to create rooms", &sc[19][0], 1);
701         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
702                                                     "Automatically give room aide privs to a user who creates a private room",
703                                                     atoi(&sc[4][0]))));
704
705         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
706                  "Automatically move problem user messages to twit room",
707                                                     atoi(&sc[8][0]))));
708
709         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
710         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
711               "Restrict Internet mail to only those with that privilege",
712                                                      atoi(&sc[11][0]))));
713         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
714               "Allow Aides to Zap (forget) rooms",
715                                                      atoi(&sc[26][0]))));
716         snprintf(sc[30], sizeof sc[30], "%d", (boolprompt(
717               "Allow system Aides access to user mailboxes",
718                                                      atoi(&sc[30][0]))));
719
720         if (strlen(&sc[18][0]) > 0) logpages = 1;
721         else logpages = 0;
722         logpages = boolprompt("Log all pages", logpages);
723         if (logpages) {
724                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
725         }
726         else {
727                 sc[18][0] = 0;
728         }
729
730
731         /* Server tuning */
732
733         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
734         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
735         strprompt("Maximum message length", &sc[20][0], 20);
736         strprompt("Minimum number of worker threads", &sc[21][0], 3);
737         strprompt("Maximum number of worker threads", &sc[22][0], 3);
738
739         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
740         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
741         strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
742
743         /* This logic flips the question around, because it's one of those
744          * situations where 0=yes and 1=no
745          */
746         a = atoi(sc[25]);
747         a = (a ? 0 : 1);
748         a = boolprompt("Correct forged From: lines during authenticated SMTP",
749                 a);
750         a = (a ? 0 : 1);
751         snprintf(sc[25], sizeof sc[25], "%d", a);
752
753         /* LDAP settings */
754         if (serv_info.serv_supports_qnop) {
755                 a = strlen(&sc[32][0]);
756                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
757                 a = boolprompt("Connect this Citadel to an LDAP directory", a);
758                 if (a) {
759                         strprompt("Host name of LDAP server",
760                                 &sc[32][0], 127);
761                         strprompt("Port number of LDAP service",
762                                 &sc[33][0], 5);
763                 }
764                 else {
765                         strcpy(&sc[32][0], "");
766                 }
767         }
768
769         /* Expiry settings */
770         strprompt("Default user purge time (days)", &sc[16][0], 5);
771         strprompt("Default room purge time (days)", &sc[17][0], 5);
772
773         /* Angels and demons dancing in my head... */
774         do {
775                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
776                 strprompt("System default message expire policy (? for list)",
777                           buf, 1);
778                 if (buf[0] == '?') {
779                         scr_printf("\n"
780                                 "1. Never automatically expire messages\n"
781                                 "2. Expire by message count\n"
782                                 "3. Expire by message age\n");
783                 }
784         } while ((buf[0] < '1') || (buf[0] > '3'));
785         site_expirepolicy->expire_mode = buf[0] - '0';
786
787         /* ...lunatics and monsters underneath my bed */
788         if (site_expirepolicy->expire_mode == 2) {
789                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
790                 strprompt("Keep how many messages online?", buf, 10);
791                 site_expirepolicy->expire_value = atol(buf);
792         }
793         if (site_expirepolicy->expire_mode == 3) {
794                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
795                 strprompt("Keep messages for how many days?", buf, 10);
796                 site_expirepolicy->expire_value = atol(buf);
797         }
798
799         /* Media messiahs preying on my fears... */
800         do {
801                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
802                 strprompt("Mailbox default message expire policy (? for list)",
803                           buf, 1);
804                 if (buf[0] == '?') {
805                         scr_printf("\n"
806                                 "0. Go with the system default\n"
807                                 "1. Never automatically expire messages\n"
808                                 "2. Expire by message count\n"
809                                 "3. Expire by message age\n");
810                 }
811         } while ((buf[0] < '0') || (buf[0] > '3'));
812         mbx_expirepolicy->expire_mode = buf[0] - '0';
813
814         /* ...Pop culture prophets playing in my ears */
815         if (mbx_expirepolicy->expire_mode == 2) {
816                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
817                 strprompt("Keep how many messages online?", buf, 10);
818                 mbx_expirepolicy->expire_value = atol(buf);
819         }
820         if (mbx_expirepolicy->expire_mode == 3) {
821                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
822                 strprompt("Keep messages for how many days?", buf, 10);
823                 mbx_expirepolicy->expire_value = atol(buf);
824         }
825
826         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
827         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
828
829         /* Save it */
830         scr_printf("Save this configuration? ");
831         if (yesno()) {
832                 r = 1;
833                 for (a = 0; a < NUM_CONFIGS; a++)
834                         r += 1 + strlen(sc[a]);
835                 resp = (char *)calloc(1, r);
836                 if (!resp) {
837                         err_printf("Can't save config - out of memory!\n");
838                         logoff(ipc, 1);
839                 }
840                 for (a = 0; a < NUM_CONFIGS; a++) {
841                         strcat(resp, sc[a]);
842                         strcat(resp, "\n");
843                 }
844                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
845                 if (r / 100 != 4) {
846                         err_printf("%s\n", buf);
847                 }
848                 free(resp);
849
850                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
851                 if (r / 100 != 2) {
852                         err_printf("%s\n", buf);
853                 }
854
855                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
856                 if (r / 100 != 2) {
857                         err_printf("%s\n", buf);
858                 }
859
860         }
861 }
862
863
864 /*
865  * support function for do_internet_configuration()
866  */
867 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
868         int sel;
869
870         keyopt(" <1> localhost      (Alias for this computer)\n");
871         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
872         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
873         keyopt(" <4> directory      (Consult the Global Address Book)\n");
874         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
875         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
876         sel = intprompt("Which one", 1, 1, 6);
877         switch(sel) {
878                 case 1: strcpy(buf, "localhost");
879                         return;
880                 case 2: strcpy(buf, "gatewaydomain");
881                         return;
882                 case 3: strcpy(buf, "smarthost");
883                         return;
884                 case 4: strcpy(buf, "directory");
885                         return;
886                 case 5: strcpy(buf, "spamassassin");
887                         return;
888                 case 6: strcpy(buf, "rbl");
889                         return;
890         }
891 }
892
893
894 /*
895  * Internet mail configuration
896  */
897 void do_internet_configuration(CtdlIPC *ipc)
898 {
899         char buf[SIZ];
900         char *resp = NULL;
901         int num_recs = 0;
902         char **recs = NULL;
903         char ch;
904         int badkey;
905         int i, j;
906         int quitting = 0;
907         int r;
908         
909         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
910         if (r / 100 == 1) {
911                 while (strlen(resp)) {
912                         extract_token(buf, resp, 0, '\n');
913                         remove_token(resp, 0, '\n');
914                         ++num_recs;
915                         if (num_recs == 1) recs = malloc(sizeof(char *));
916                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
917                         recs[num_recs-1] = malloc(strlen(buf) + 1);
918                         strcpy(recs[num_recs-1], buf);
919                 }
920         }
921         if (resp) free(resp);
922
923         do {
924                 scr_printf("\n");
925                 color(BRIGHT_WHITE);
926                 scr_printf("###                    Host or domain                     Record type      \n");
927                 color(DIM_WHITE);
928                 scr_printf("--- -------------------------------------------------- --------------------\n");
929                 for (i=0; i<num_recs; ++i) {
930                 color(DIM_WHITE);
931                 scr_printf("%3d ", i+1);
932                 extract(buf, recs[i], 0);
933                 color(BRIGHT_CYAN);
934                 scr_printf("%-50s ", buf);
935                 extract(buf, recs[i], 1);
936                 color(BRIGHT_MAGENTA);
937                 scr_printf("%-20s\n", buf);
938                 color(DIM_WHITE);
939                 }
940
941                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
942                 switch(ch) {
943                         case 'a':
944                                 newprompt("Enter host name: ",
945                                         buf, 50);
946                                 striplt(buf);
947                                 if (strlen(buf) > 0) {
948                                         ++num_recs;
949                                         if (num_recs == 1)
950                                                 recs = malloc(sizeof(char *));
951                                         else recs = realloc(recs,
952                                                 (sizeof(char *)) * num_recs);
953                                         strcat(buf, "|");
954                                         get_inet_rec_type(ipc,
955                                                         &buf[strlen(buf)]);
956                                         recs[num_recs-1] = strdup(buf);
957                                 }
958                                 break;
959                         case 'd':
960                                 i = intprompt("Delete which one",
961                                         1, 1, num_recs) - 1;
962                                 free(recs[i]);
963                                 --num_recs;
964                                 for (j=i; j<num_recs; ++j)
965                                         recs[j] = recs[j+1];
966                                 break;
967                         case 's':
968                                 r = 1;
969                                 for (i = 0; i < num_recs; i++)
970                                         r += 1 + strlen(recs[i]);
971                                 resp = (char *)calloc(1, r);
972                                 if (!resp) {
973                                         err_printf("Can't save config - out of memory!\n");
974                                         logoff(ipc, 1);
975                                 }
976                                 if (num_recs) for (i = 0; i < num_recs; i++) {
977                                         strcat(resp, recs[i]);
978                                         strcat(resp, "\n");
979                                 }
980                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
981                                 if (r / 100 != 4) {
982                                         err_printf("%s\n", buf);
983                                 }
984                                 quitting = 1;
985                                 break;
986                         case 'q':
987                                 quitting = boolprompt(
988                                         "Quit without saving", 0);
989                                 break;
990                         default:
991                                 badkey = 1;
992                 }
993         } while (quitting == 0);
994
995         if (recs != NULL) {
996                 for (i=0; i<num_recs; ++i) free(recs[i]);
997                 free(recs);
998         }
999 }
1000
1001
1002
1003 /*
1004  * Edit network configuration for room sharing, mailing lists, etc.
1005  */
1006 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
1007 {
1008         char filename[PATH_MAX];
1009         char changefile[PATH_MAX];
1010         int e_ex_code;
1011         pid_t editor_pid;
1012         int cksum;
1013         int b, i;
1014         char buf[SIZ];
1015         char instr[SIZ];
1016         char addr[SIZ];
1017         FILE *tempfp;
1018         FILE *changefp;
1019
1020         if (strlen(editor_paths[0]) == 0) {
1021                 scr_printf("You must have an external editor configured in"
1022                         " order to use this function.\n");
1023                 return;
1024         }
1025
1026         snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
1027         snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
1028
1029         tempfp = fopen(filename, "w");
1030         if (tempfp == NULL) {
1031                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
1032                 return;
1033         }
1034
1035         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
1036         fprintf(tempfp, "# %s\n", comment);
1037         fprintf(tempfp, "# Specify one per line.\n"
1038                         "\n\n");
1039
1040         CtdlIPC_putline(ipc, "GNET");
1041         CtdlIPC_getline(ipc, buf);
1042         if (buf[0] == '1') {
1043                 while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1044                         extract(instr, buf, 0);
1045                         if (!strcasecmp(instr, entrytype)) {
1046                                 extract(addr, buf, 1);
1047                                 fprintf(tempfp, "%s\n", addr);
1048                         }
1049                 }
1050         }
1051         fclose(tempfp);
1052
1053         e_ex_code = 1;  /* start with a failed exit code */
1054         screen_reset();
1055         sttybbs(SB_RESTORE);
1056         editor_pid = fork();
1057         cksum = file_checksum(filename);
1058         if (editor_pid == 0) {
1059                 chmod(filename, 0600);
1060                 putenv("WINDOW_TITLE=Network configuration");
1061                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
1062                 exit(1);
1063         }
1064         if (editor_pid > 0) {
1065                 do {
1066                         e_ex_code = 0;
1067                         b = ka_wait(&e_ex_code);
1068                 } while ((b != editor_pid) && (b >= 0));
1069         editor_pid = (-1);
1070         sttybbs(0);
1071         screen_set();
1072         }
1073
1074         if (file_checksum(filename) == cksum) {
1075                 err_printf("*** Not saving changes.\n");
1076                 e_ex_code = 1;
1077         }
1078
1079         if (e_ex_code == 0) {           /* Save changes */
1080                 changefp = fopen(changefile, "w");
1081                 CtdlIPC_putline(ipc, "GNET");
1082                 CtdlIPC_getline(ipc, buf);
1083                 if (buf[0] == '1') {
1084                         while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1085                                 extract(instr, buf, 0);
1086                                 if (strcasecmp(instr, entrytype)) {
1087                                         fprintf(changefp, "%s\n", buf);
1088                                 }
1089                         }
1090                 }
1091                 tempfp = fopen(filename, "r");
1092                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1093                         for (i=0; i<strlen(buf); ++i) {
1094                                 if (buf[i] == '#') buf[i] = 0;
1095                         }
1096                         striplt(buf);
1097                         if (strlen(buf) > 0) {
1098                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1099                         }
1100                 }
1101                 fclose(tempfp);
1102                 fclose(changefp);
1103
1104                 /* now write it to the server... */
1105                 CtdlIPC_putline(ipc, "SNET");
1106                 CtdlIPC_getline(ipc, buf);
1107                 if (buf[0] == '4') {
1108                         changefp = fopen(changefile, "r");
1109                         if (changefp != NULL) {
1110                                 while (fgets(buf, sizeof buf,
1111                                        changefp) != NULL) {
1112                                         buf[strlen(buf) - 1] = 0;
1113                                         CtdlIPC_putline(ipc, buf);
1114                                 }
1115                                 fclose(changefp);
1116                         }
1117                         CtdlIPC_putline(ipc, "000");
1118                 }
1119         }
1120
1121         unlink(filename);               /* Delete the temporary files */
1122         unlink(changefile);
1123 }
1124
1125
1126 /*
1127  * IGnet node configuration
1128  */
1129 void do_ignet_configuration(CtdlIPC *ipc) {
1130         char buf[SIZ];
1131         int num_recs = 0;
1132         char **recs = NULL;
1133         char ch;
1134         int badkey;
1135         int i, j;
1136         int quitting = 0;
1137         
1138
1139         snprintf(buf, sizeof buf, "CONF getsys|%s", IGNETCFG);
1140         CtdlIPC_putline(ipc, buf);
1141         CtdlIPC_getline(ipc, buf);
1142         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1143                 ++num_recs;
1144                 if (num_recs == 1) recs = malloc(sizeof(char *));
1145                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1146                 recs[num_recs-1] = malloc(SIZ);
1147                 strcpy(recs[num_recs-1], buf);
1148         }
1149
1150         do {
1151                 scr_printf("\n");
1152                 color(BRIGHT_WHITE);
1153                 scr_printf(     "### "
1154                         "   Node          "
1155                         "  Secret           "
1156                         "          Host or IP             "
1157                         "Port#\n");
1158                 color(DIM_WHITE);
1159                 scr_printf(     "--- "
1160                         "---------------- "
1161                         "------------------ "
1162                         "-------------------------------- "
1163                         "-----\n");
1164                 for (i=0; i<num_recs; ++i) {
1165                 color(DIM_WHITE);
1166                 scr_printf("%3d ", i+1);
1167                 extract(buf, recs[i], 0);
1168                 color(BRIGHT_CYAN);
1169                 scr_printf("%-16s ", buf);
1170                 extract(buf, recs[i], 1);
1171                 color(BRIGHT_MAGENTA);
1172                 scr_printf("%-18s ", buf);
1173                 extract(buf, recs[i], 2);
1174                 color(BRIGHT_CYAN);
1175                 scr_printf("%-32s ", buf);
1176                 extract(buf, recs[i], 3);
1177                 color(BRIGHT_MAGENTA);
1178                 scr_printf("%-3s\n", buf);
1179                 color(DIM_WHITE);
1180                 }
1181
1182                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1183                 switch(ch) {
1184                         case 'a':
1185                                 ++num_recs;
1186                                 if (num_recs == 1)
1187                                         recs = malloc(sizeof(char *));
1188                                 else recs = realloc(recs,
1189                                         (sizeof(char *)) * num_recs);
1190                                 newprompt("Enter node name    : ", buf, 16);
1191                                 strcat(buf, "|");
1192                                 newprompt("Enter shared secret: ",
1193                                         &buf[strlen(buf)], 18);
1194                                 strcat(buf, "|");
1195                                 newprompt("Enter host or IP   : ",
1196                                         &buf[strlen(buf)], 32);
1197                                 strcat(buf, "|504");
1198                                 strprompt("Enter port number  : ",
1199                                         &buf[strlen(buf)-3], 5);
1200                                 recs[num_recs-1] = strdup(buf);
1201                                 break;
1202                         case 'd':
1203                                 i = intprompt("Delete which one",
1204                                         1, 1, num_recs) - 1;
1205                                 free(recs[i]);
1206                                 --num_recs;
1207                                 for (j=i; j<num_recs; ++j)
1208                                         recs[j] = recs[j+1];
1209                                 break;
1210                         case 's':
1211                                 snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
1212                                 CtdlIPC_putline(ipc, buf);
1213                                 CtdlIPC_getline(ipc, buf);
1214                                 if (buf[0] == '4') {
1215                                         for (i=0; i<num_recs; ++i) {
1216                                                 CtdlIPC_putline(ipc, recs[i]);
1217                                         }
1218                                         CtdlIPC_putline(ipc, "000");
1219                                 }
1220                                 else {
1221                                         scr_printf("%s\n", &buf[4]);
1222                                 }
1223                                 quitting = 1;
1224                                 break;
1225                         case 'q':
1226                                 quitting = boolprompt(
1227                                         "Quit without saving", 0);
1228                                 break;
1229                         default:
1230                                 badkey = 1;
1231                 }
1232         } while (quitting == 0);
1233
1234         if (recs != NULL) {
1235                 for (i=0; i<num_recs; ++i) free(recs[i]);
1236                 free(recs);
1237         }
1238 }
1239
1240 /*
1241  * Filter list configuration
1242  */
1243 void do_filterlist_configuration(CtdlIPC *ipc)
1244 {
1245         char buf[SIZ];
1246         int num_recs = 0;
1247         char **recs = NULL;
1248         char ch;
1249         int badkey;
1250         int i, j;
1251         int quitting = 0;
1252         
1253
1254         snprintf(buf, sizeof buf, "CONF getsys|%s", FILTERLIST);
1255         CtdlIPC_putline(ipc, buf);
1256         CtdlIPC_getline(ipc, buf);
1257         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1258                 ++num_recs;
1259                 if (num_recs == 1) recs = malloc(sizeof(char *));
1260                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1261                 recs[num_recs-1] = malloc(SIZ);
1262                 strcpy(recs[num_recs-1], buf);
1263         }
1264
1265         do {
1266                 scr_printf("\n");
1267                 color(BRIGHT_WHITE);
1268                 scr_printf(     "### "
1269                         "         User name           "
1270                         "         Room name           "
1271                         "    Node name    "
1272                         "\n");
1273                 color(DIM_WHITE);
1274                 scr_printf(     "--- "
1275                         "---------------------------- "
1276                         "---------------------------- "
1277                         "---------------- "
1278                         "\n");
1279                 for (i=0; i<num_recs; ++i) {
1280                 color(DIM_WHITE);
1281                 scr_printf("%3d ", i+1);
1282                 extract(buf, recs[i], 0);
1283                 color(BRIGHT_CYAN);
1284                 scr_printf("%-28s ", buf);
1285                 extract(buf, recs[i], 1);
1286                 color(BRIGHT_MAGENTA);
1287                 scr_printf("%-28s ", buf);
1288                 extract(buf, recs[i], 2);
1289                 color(BRIGHT_CYAN);
1290                 scr_printf("%-16s\n", buf);
1291                 extract(buf, recs[i], 3);
1292                 color(DIM_WHITE);
1293                 }
1294
1295                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1296                 switch(ch) {
1297                         case 'a':
1298                                 ++num_recs;
1299                                 if (num_recs == 1)
1300                                         recs = malloc(sizeof(char *));
1301                                 else recs = realloc(recs,
1302                                         (sizeof(char *)) * num_recs);
1303                                 newprompt("Enter user name: ", buf, 28);
1304                                 strcat(buf, "|");
1305                                 newprompt("Enter room name: ",
1306                                         &buf[strlen(buf)], 28);
1307                                 strcat(buf, "|");
1308                                 newprompt("Enter node name: ",
1309                                         &buf[strlen(buf)], 16);
1310                                 strcat(buf, "|");
1311                                 recs[num_recs-1] = strdup(buf);
1312                                 break;
1313                         case 'd':
1314                                 i = intprompt("Delete which one",
1315                                         1, 1, num_recs) - 1;
1316                                 free(recs[i]);
1317                                 --num_recs;
1318                                 for (j=i; j<num_recs; ++j)
1319                                         recs[j] = recs[j+1];
1320                                 break;
1321                         case 's':
1322                                 snprintf(buf, sizeof buf, "CONF putsys|%s", FILTERLIST);
1323                                 CtdlIPC_putline(ipc, buf);
1324                                 CtdlIPC_getline(ipc, buf);
1325                                 if (buf[0] == '4') {
1326                                         for (i=0; i<num_recs; ++i) {
1327                                                 CtdlIPC_putline(ipc, recs[i]);
1328                                         }
1329                                         CtdlIPC_putline(ipc, "000");
1330                                 }
1331                                 else {
1332                                         scr_printf("%s\n", &buf[4]);
1333                                 }
1334                                 quitting = 1;
1335                                 break;
1336                         case 'q':
1337                                 quitting = boolprompt(
1338                                         "Quit without saving", 0);
1339                                 break;
1340                         default:
1341                                 badkey = 1;
1342                 }
1343         } while (quitting == 0);
1344
1345         if (recs != NULL) {
1346                 for (i=0; i<num_recs; ++i) free(recs[i]);
1347                 free(recs);
1348         }
1349 }
1350
1351