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