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