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