text client <.A>ide <S>ysconfig <I>nternet , when encountering a "directory" domain...
[citadel.git] / textclient / src / tuiconfig.c
1 /*
2  * Configuration screens that are part of the text mode client.
3  *
4  * Copyright (c) 1987-2017 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <string.h>
23 #include <limits.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <signal.h>
37 #include <pwd.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <libcitadel.h>
41 #include "sysdep.h"
42 #include "citadel_ipc.h"
43 #include "citadel_decls.h"
44 #include "tuiconfig.h"
45 #include "messages.h"
46 #include "routines.h"
47 #include "commands.h"
48 #include "screen.h"
49
50 /* work around solaris include files */
51 #ifdef reg
52 #undef reg
53 #endif
54
55 extern char temp[];
56 extern char tempdir[];
57 extern char *axdefs[8];
58 extern long highest_msg_read;
59 extern long maxmsgnum;
60 extern unsigned room_flags;
61 extern int screenwidth;
62 char editor_path[PATH_MAX];
63
64
65 /* 
66  * General system configuration command
67  */
68 void do_system_configuration(CtdlIPC *ipc)
69 {
70
71         /* NUM_CONFIGS is now defined in citadel.h */
72
73         char buf[256];
74         char sc[NUM_CONFIGS][256];
75         char *resp = NULL;
76         struct ExpirePolicy *site_expirepolicy = NULL;
77         struct ExpirePolicy *mbx_expirepolicy = NULL;
78         int a;
79         int logpages = 0;
80         int r;                  /* IPC response code */
81         int server_configs = 0;
82
83         /* Clear out the config buffers */
84         memset(&sc[0][0], 0, sizeof(sc));
85
86         /* Fetch the current config */
87         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
88         if (r / 100 == 1) {
89                 server_configs = num_tokens(resp, '\n');
90                 for (a=0; a<server_configs; ++a) {
91                         if (a < NUM_CONFIGS) {
92                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
93                         }
94                 }
95         }
96         if (resp) free(resp);
97         resp = NULL;
98         /* Fetch the expire policy (this will silently fail on old servers,
99          * resulting in "default" policy)
100          */
101         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
102         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
103
104         /* Identification parameters */
105
106         strprompt("Node name", &sc[0][0], 15);
107         strprompt("Fully qualified domain name", &sc[1][0], 63);
108         strprompt("Human readable node name", &sc[2][0], 20);
109         strprompt("Telephone number", &sc[3][0], 15);
110         strprompt("Geographic location of this system", &sc[12][0], 31);
111         strprompt("Name of system administrator", &sc[13][0], 25);
112         strprompt("Paginator prompt", &sc[10][0], 79);
113
114         /* Security parameters */
115
116         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt("Require registration for new users", atoi(&sc[7][0]))));
117         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt("Disable self-service user account creation", atoi(&sc[29][0]))));
118         strprompt("Initial access level for new users", &sc[6][0], 1);
119         strprompt("Access level required to create rooms", &sc[19][0], 1);
120         snprintf(sc[67], sizeof sc[67], "%d", (boolprompt("Allow anonymous guest logins", atoi(&sc[67][0]))));
121         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
122                 "Automatically give room admin privs to a user who creates a private room",
123                 atoi(&sc[4][0]))));
124
125         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
126                 "Automatically move problem user messages to twit room",
127                 atoi(&sc[8][0]))));
128
129         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
130         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
131                 "Restrict Internet mail to only those with that privilege",
132                 atoi(&sc[11][0]))));
133         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
134                 "Allow admins to Zap (forget) rooms",
135                 atoi(&sc[26][0]))));
136
137         if (!IsEmptyStr(&sc[18][0])) {
138                 logpages = 1;
139         }
140         else {
141                 logpages = 0;
142         }
143         logpages = boolprompt("Log all instant messages", logpages);
144         if (logpages) {
145                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
146         }
147         else {
148                 sc[18][0] = 0;
149         }
150
151         /* Commented out because this setting isn't really appropriate to
152          * change while the server is running.
153          *
154          * snprintf(sc[52], sizeof sc[52], "%d", (boolprompt(
155          *      "Use system authentication",
156          *      atoi(&sc[52][0]))));
157          */
158
159         /* Server tuning */
160
161         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
162         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
163         strprompt("Maximum message length", &sc[20][0], 20);
164         strprompt("Minimum number of worker threads", &sc[21][0], 3);
165         strprompt("Maximum number of worker threads", &sc[22][0], 3);
166         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
167                 "Automatically delete committed database logs",
168                 atoi(&sc[43][0]))));
169
170         strprompt("Server IP address (* for 'any')", &sc[37][0], 15);
171         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
172         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
173         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
174         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
175         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
176         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
177         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
178         strprompt("NNTP server port (-1 to disable)", &sc[70][0], 5);
179         strprompt("NNTPS server port (-1 to disable)", &sc[71][0], 5);
180         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
181         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
182
183         strprompt("XMPP (Jabber) client to server port (-1 to disable)", &sc[62][0], 5);
184         /* No prompt because we don't implement this service yet, it's just a placeholder */
185         /* strprompt("XMPP (Jabber) server to server port (-1 to disable)", &sc[63][0], 5); */
186
187         /* This logic flips the question around, because it's one of those
188          * situations where 0=yes and 1=no
189          */
190         a = atoi(sc[25]);
191         a = (a ? 0 : 1);
192         a = boolprompt("Correct forged From: lines during authenticated SMTP", a);
193         a = (a ? 0 : 1);
194         snprintf(sc[25], sizeof sc[25], "%d", a);
195
196         snprintf(sc[66], sizeof sc[66], "%d", (boolprompt(
197                 "Flag messages as spam instead of rejecting",
198                 atoi(&sc[66][0]))));
199
200         /* This logic flips the question around, because it's one of those
201          * situations where 0=yes and 1=no
202          */
203         a = atoi(sc[61]);
204         a = (a ? 0 : 1);
205         a = boolprompt("Force IMAP posts in public rooms to be from the user who submitted them", a);
206         a = (a ? 0 : 1);
207         snprintf(sc[61], sizeof sc[61], "%d", a);
208
209         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
210                 "Allow unauthenticated SMTP clients to spoof my domains",
211                 atoi(&sc[45][0]))));
212         snprintf(sc[57], sizeof sc[57], "%d", (boolprompt(
213                 "Perform RBL checks at greeting instead of after RCPT",
214                 atoi(&sc[57][0]))));
215
216         /* LDAP settings */
217         if (ipc->ServInfo.supports_ldap) {
218                 a = strlen(&sc[32][0]);
219                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
220                 a = boolprompt("Do you want to configure LDAP authentication?", a);
221                 if (a) {
222                         strprompt("Host name of LDAP server", &sc[32][0], 127);
223                         strprompt("Port number of LDAP service", &sc[33][0], 5);
224                         strprompt("Base DN", &sc[34][0], 255);
225                         strprompt("Bind DN (or blank for anonymous bind)", &sc[35][0], 255);
226                         strprompt("Password for bind DN (or blank for anonymous bind)", &sc[36][0], 255);
227                 }
228                 else {
229                         strcpy(&sc[32][0], "");
230                 }
231         }
232
233         /* Expiry settings */
234         strprompt("Default user purge time (days)", &sc[16][0], 5);
235         strprompt("Default room purge time (days)", &sc[17][0], 5);
236
237         /* Angels and demons dancing in my head... */
238         do {
239                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
240                 strprompt("System default message expire policy (? for list)",
241                           buf, 1);
242                 if (buf[0] == '?') {
243                         scr_printf("\n"
244                                 "1. Never automatically expire messages\n"
245                                 "2. Expire by message count\n"
246                                 "3. Expire by message age\n");
247                 }
248         } while ((buf[0] < '1') || (buf[0] > '3'));
249         site_expirepolicy->expire_mode = buf[0] - '0';
250
251         /* ...lunatics and monsters underneath my bed */
252         if (site_expirepolicy->expire_mode == 2) {
253                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
254                 strprompt("Keep how many messages online?", buf, 10);
255                 site_expirepolicy->expire_value = atol(buf);
256         }
257         if (site_expirepolicy->expire_mode == 3) {
258                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
259                 strprompt("Keep messages for how many days?", buf, 10);
260                 site_expirepolicy->expire_value = atol(buf);
261         }
262
263         /* Media messiahs preying on my fears... */
264         do {
265                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
266                 strprompt("Mailbox default message expire policy (? for list)",
267                           buf, 1);
268                 if (buf[0] == '?') {
269                         scr_printf("\n"
270                                 "0. Go with the system default\n"
271                                 "1. Never automatically expire messages\n"
272                                 "2. Expire by message count\n"
273                                 "3. Expire by message age\n");
274                 }
275         } while ((buf[0] < '0') || (buf[0] > '3'));
276         mbx_expirepolicy->expire_mode = buf[0] - '0';
277
278         /* ...Pop culture prophets playing in my ears */
279         if (mbx_expirepolicy->expire_mode == 2) {
280                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
281                 strprompt("Keep how many messages online?", buf, 10);
282                 mbx_expirepolicy->expire_value = atol(buf);
283         }
284         if (mbx_expirepolicy->expire_mode == 3) {
285                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
286                 strprompt("Keep messages for how many days?", buf, 10);
287                 mbx_expirepolicy->expire_value = atol(buf);
288         }
289
290         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
291         strprompt("Default frequency to run POP3 collection (in seconds)", &sc[64][0], 5);
292         strprompt("Fastest frequency to run POP3 collection (in seconds)", &sc[65][0], 5);
293         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
294         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
295                 "Enable full text search index (warning: resource intensive)",
296                 atoi(&sc[42][0]))));
297
298         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
299                 "Perform journaling of email messages",
300                 atoi(&sc[46][0]))));
301         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
302                 "Perform journaling of non-email messages",
303                 atoi(&sc[47][0]))));
304         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
305                 strprompt("Email destination of journalized messages",
306                         &sc[48][0], 127);
307         }
308
309         /* No more Funambol */
310         sc[53][0] = 0;
311         sc[54][0] = 0;
312         sc[55][0] = 0;
313         sc[56][0] = 0;
314
315         /* External pager stuff */
316         int yes_pager = 0;
317         if (strlen(sc[60]) > 0) yes_pager = 1;
318         yes_pager = boolprompt("Configure an external pager tool", yes_pager);
319         if (yes_pager) {
320                 strprompt("External pager tool", &sc[60][0], 255);
321         }
322         else {
323                 sc[60][0] = 0;
324         }
325
326         /* Master user account */
327         int yes_muacct = 0;
328         if (strlen(sc[58]) > 0) yes_muacct = 1;
329         yes_muacct = boolprompt("Enable a 'master user' account", yes_muacct);
330         if (yes_muacct) {
331                 strprompt("Master user name", &sc[58][0], 31);
332                 strprompt("Master user password", &sc[59][0], -31);
333         }
334         else {
335                 strcpy(&sc[58][0], "");
336                 strcpy(&sc[59][0], "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
337         }
338
339         /* Save it */
340         scr_printf("Save this configuration? ");
341         if (yesno()) {
342                 r = 1;
343                 for (a = 0; a < NUM_CONFIGS; a++) {
344                         r += 1 + strlen(sc[a]);
345                 }
346                 resp = (char *)calloc(1, r);
347                 if (!resp) {
348                         scr_printf("Can't save config - out of memory!\n");
349                         logoff(ipc, 1);
350                 }
351                 for (a = 0; a < NUM_CONFIGS; a++) {
352                         strcat(resp, sc[a]);
353                         strcat(resp, "\n");
354                 }
355                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
356                 if (r / 100 != 4) {
357                         scr_printf("%s\n", buf);
358                 }
359                 free(resp);
360
361                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
362                 if (r / 100 != 2) {
363                         scr_printf("%s\n", buf);
364                 }
365
366                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
367                 if (r / 100 != 2) {
368                         scr_printf("%s\n", buf);
369                 }
370
371         }
372     if (site_expirepolicy) free(site_expirepolicy);
373     if (mbx_expirepolicy) free(mbx_expirepolicy);
374 }
375
376
377 /*
378  * support function for do_internet_configuration()
379  */
380 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
381         int sel;
382
383         keyopt(" <1> localhost      (Alias for this computer)\n");
384         keyopt(" <2> smart host     (Forward all outbound mail to this host)\n");
385         keyopt(" <3> fallback host  (Send mail to this host only if direct delivery fails)\n");
386         keyopt(" <4> SpamAssassin   (Address of SpamAssassin server)\n");
387         keyopt(" <5> RBL            (domain suffix of spam hunting RBL)\n");
388         keyopt(" <6> masq domains   (Domains as which users are allowed to masquerade)\n");
389         keyopt(" <7> ClamAV         (Address of ClamAV clamd server)\n");
390         sel = intprompt("Which one", 1, 1, 8);
391         switch(sel) {
392                 case 1: strcpy(buf, "localhost");
393                         return;
394                 case 2: strcpy(buf, "smarthost");
395                         return;
396                 case 3: strcpy(buf, "fallbackhost");
397                         return;
398                 case 4: strcpy(buf, "spamassassin");
399                         return;
400                 case 5: strcpy(buf, "rbl");
401                         return;
402                 case 6: strcpy(buf, "masqdomain");
403                         return;
404                 case 7: strcpy(buf, "clamav");
405                         return;
406         }
407 }
408
409
410 /*
411  * Internet mail configuration
412  */
413 void do_internet_configuration(CtdlIPC *ipc)
414 {
415         char buf[256];
416         char *resp = NULL;
417         int num_recs = 0;
418         char **recs = NULL;
419         char ch;
420         int i, j;
421         int quitting = 0;
422         int modified = 0;
423         int r;
424         
425         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
426         if (r / 100 == 1) {
427                 while (!IsEmptyStr(resp)) {
428                         extract_token(buf, resp, 0, '\n', sizeof buf);
429                         remove_token(resp, 0, '\n');
430
431                         // VILE SLEAZY HACK: replace obsolete "directory" domains with "localhost"
432                         char *d = strstr(buf, "|directory");
433                         if (d != NULL) {
434                                 strcpy(d, "|localhost");
435                         }
436
437                         ++num_recs;
438                         if (num_recs == 1) recs = malloc(sizeof(char *));
439                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
440                         recs[num_recs-1] = malloc(strlen(buf) + 1);
441                         strcpy(recs[num_recs-1], buf);
442                 }
443         }
444         if (resp) free(resp);
445
446         do {
447                 scr_printf("\n");
448                 color(BRIGHT_WHITE);
449                 scr_printf("###                    Host or domain                     Record type      \n");
450                 color(DIM_WHITE);
451                 scr_printf("--- -------------------------------------------------- --------------------\n");
452                 for (i=0; i<num_recs; ++i) {
453                 color(DIM_WHITE);
454                 scr_printf("%3d ", i+1);
455                 extract_token(buf, recs[i], 0, '|', sizeof buf);
456                 color(BRIGHT_CYAN);
457                 scr_printf("%-50s ", buf);
458                 extract_token(buf, recs[i], 1, '|', sizeof buf);
459                 color(BRIGHT_MAGENTA);
460                 scr_printf("%-20s\n", buf);
461                 color(DIM_WHITE);
462                 }
463
464                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
465                 switch(ch) {
466                         case 'a':
467                                 newprompt("Enter host name: ", buf, 50);
468                                 striplt(buf);
469                                 if (!IsEmptyStr(buf)) {
470                                         ++num_recs;
471                                         if (num_recs == 1) {
472                                                 recs = malloc(sizeof(char *));
473                                         }
474                                         else {
475                                                 recs = realloc(recs, (sizeof(char *)) * num_recs);
476                                         }
477                                         strcat(buf, "|");
478                                         get_inet_rec_type(ipc, &buf[strlen(buf)]);
479                                         recs[num_recs-1] = strdup(buf);
480                                 }
481                                 modified = 1;
482                                 break;
483                         case 'd':
484                                 i = intprompt("Delete which one", 1, 1, num_recs) - 1;
485                                 free(recs[i]);
486                                 --num_recs;
487                                 for (j=i; j<num_recs; ++j) {
488                                         recs[j] = recs[j+1];
489                                 }
490                                 modified = 1;
491                                 break;
492                         case 's':
493                                 r = 1;
494                                 for (i = 0; i < num_recs; i++)
495                                         r += 1 + strlen(recs[i]);
496                                 resp = (char *)calloc(1, r);
497                                 if (!resp) {
498                                         scr_printf("Can't save config - out of memory!\n");
499                                         logoff(ipc, 1);
500                                 }
501                                 if (num_recs) for (i = 0; i < num_recs; i++) {
502                                         strcat(resp, recs[i]);
503                                         strcat(resp, "\n");
504                                 }
505                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
506                                 if (r / 100 != 4) {
507                                         scr_printf("%s\n", buf);
508                                 } else {
509                                         scr_printf("Wrote %d records.\n", num_recs);
510                                         modified = 0;
511                                 }
512                 free(resp);
513                                 break;
514                         case 'q':
515                                 quitting = !modified || boolprompt(
516                                         "Quit without saving", 0);
517                                 break;
518                         default:
519                                 break;
520                 }
521         } while (!quitting);
522
523         if (recs != NULL) {
524                 for (i=0; i<num_recs; ++i) free(recs[i]);
525                 free(recs);
526         }
527 }
528
529
530
531 /*
532  * Edit network configuration for room sharing, mailing lists, etc.
533  */
534 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
535 {
536         char filename[PATH_MAX];
537         char changefile[PATH_MAX];
538         int e_ex_code;
539         pid_t editor_pid;
540         int cksum;
541         int b, i, tokens;
542         char buf[1024];
543         char instr[1024];
544         char addr[1024];
545         FILE *tempfp;
546         FILE *changefp;
547         char *listing = NULL;
548         int r;
549
550         if (IsEmptyStr(editor_path)) {
551                 scr_printf("You must have an external editor configured in order to use this function.\n");
552                 return;
553         }
554
555         CtdlMakeTempFileName(filename, sizeof filename);
556         CtdlMakeTempFileName(changefile, sizeof changefile);
557
558         tempfp = fopen(filename, "w");
559         if (tempfp == NULL) {
560                 scr_printf("Cannot open %s: %s\n", filename, strerror(errno));
561                 return;
562         }
563
564         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
565         fprintf(tempfp, "# %s\n", comment);
566         fprintf(tempfp, "# Specify one per line.\n"
567                         "\n\n");
568
569         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
570         if (r / 100 == 1) {
571                 while(listing && !IsEmptyStr(listing)) {
572                         extract_token(buf, listing, 0, '\n', sizeof buf);
573                         remove_token(listing, 0, '\n');
574                         extract_token(instr, buf, 0, '|', sizeof instr);
575                         if (!strcasecmp(instr, entrytype)) {
576                                 tokens = num_tokens(buf, '|');
577                                 for (i=1; i<tokens; ++i) {
578                                         extract_token(addr, buf, i, '|', sizeof addr);
579                                         fprintf(tempfp, "%s", addr);
580                                         if (i < (tokens-1)) {
581                                                 fprintf(tempfp, "|");
582                                         }
583                                 }
584                                 fprintf(tempfp, "\n");
585                         }
586                 }
587         }
588         if (listing) {
589                 free(listing);
590                 listing = NULL;
591         }
592         fclose(tempfp);
593
594         e_ex_code = 1;  /* start with a failed exit code */
595         stty_ctdl(SB_RESTORE);
596         editor_pid = fork();
597         cksum = file_checksum(filename);
598         if (editor_pid == 0) {
599                 chmod(filename, 0600);
600                 putenv("WINDOW_TITLE=Network configuration");
601                 execlp(editor_path, editor_path, filename, NULL);
602                 exit(1);
603         }
604         if (editor_pid > 0) {
605                 do {
606                         e_ex_code = 0;
607                         b = ka_wait(&e_ex_code);
608                 } while ((b != editor_pid) && (b >= 0));
609         editor_pid = (-1);
610         stty_ctdl(0);
611         }
612
613         if (file_checksum(filename) == cksum) {
614                 scr_printf("*** No changes to save.\n");
615                 e_ex_code = 1;
616         }
617
618         if (e_ex_code == 0) {           /* Save changes */
619                 changefp = fopen(changefile, "w");
620
621                 /* Load all netconfig entries that are *not* of the type we are editing */
622                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
623                 if (r / 100 == 1) {
624                         while(listing && !IsEmptyStr(listing)) {
625                                 extract_token(buf, listing, 0, '\n', sizeof buf);
626                                 remove_token(listing, 0, '\n');
627                                 extract_token(instr, buf, 0, '|', sizeof instr);
628                                 if (strcasecmp(instr, entrytype)) {
629                                         fprintf(changefp, "%s\n", buf);
630                                 }
631                         }
632                 }
633                 if (listing) {
634                         free(listing);
635                         listing = NULL;
636                 }
637
638                 /* ...and merge that with the data we just edited */
639                 tempfp = fopen(filename, "r");
640                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
641                         for (i=0; i<strlen(buf); ++i) {
642                                 if (buf[i] == '#') buf[i] = 0;
643                         }
644                         striplt(buf);
645                         if (!IsEmptyStr(buf)) {
646                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
647                         }
648                 }
649                 fclose(tempfp);
650                 fclose(changefp);
651
652                 /* now write it to the server... */
653                 changefp = fopen(changefile, "r");
654                 if (changefp != NULL) {
655                         listing = load_message_from_file(changefp);
656                         if (listing) {
657                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
658                                 free(listing);
659                                 listing = NULL;
660                         }
661                         fclose(changefp);
662                 }
663         }
664
665         unlink(filename);               /* Delete the temporary files */
666         unlink(changefile);
667 }
668
669
670 /*
671  * IGnet node configuration
672  */
673 void do_ignet_configuration(CtdlIPC *ipc) {
674         char buf[SIZ];
675         int num_recs = 0;
676         char **recs = NULL;
677         char ch;
678         int i, j;
679         int quitting = 0;
680         int modified = 0;
681         char *listing = NULL;
682         int r;
683
684         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
685         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
686                 extract_token(buf, listing, 0, '\n', sizeof buf);
687                 remove_token(listing, 0, '\n');
688
689                 ++num_recs;
690                 if (num_recs == 1) recs = malloc(sizeof(char *));
691                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
692                 recs[num_recs-1] = malloc(SIZ);
693                 strcpy(recs[num_recs-1], buf);
694         }
695         if (listing) free(listing);
696
697         do {
698                 scr_printf("\n");
699                 color(BRIGHT_WHITE);
700                 scr_printf(     "### "
701                         "   Node          "
702                         "  Secret           "
703                         "          Host or IP             "
704                         "Port#\n");
705                 color(DIM_WHITE);
706                 scr_printf(     "--- "
707                         "---------------- "
708                         "------------------ "
709                         "-------------------------------- "
710                         "-----\n");
711                 for (i=0; i<num_recs; ++i) {
712                 color(DIM_WHITE);
713                 scr_printf("%3d ", i+1);
714                 extract_token(buf, recs[i], 0, '|', sizeof buf);
715                 color(BRIGHT_CYAN);
716                 scr_printf("%-16s ", buf);
717                 extract_token(buf, recs[i], 1, '|', sizeof buf);
718                 color(BRIGHT_MAGENTA);
719                 scr_printf("%-18s ", buf);
720                 extract_token(buf, recs[i], 2, '|', sizeof buf);
721                 color(BRIGHT_CYAN);
722                 scr_printf("%-32s ", buf);
723                 extract_token(buf, recs[i], 3, '|', sizeof buf);
724                 color(BRIGHT_MAGENTA);
725                 scr_printf("%-3s\n", buf);
726                 color(DIM_WHITE);
727                 }
728                 scr_printf("\n");
729
730                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
731                 switch(ch) {
732                         case 'a':
733                                 ++num_recs;
734                                 if (num_recs == 1)
735                                         recs = malloc(sizeof(char *));
736                                 else recs = realloc(recs,
737                                         (sizeof(char *)) * num_recs);
738                                 newprompt("Enter node name    : ", buf, 16);
739                                 strcat(buf, "|");
740                                 newprompt("Enter shared secret: ",
741                                         &buf[strlen(buf)], 18);
742                                 strcat(buf, "|");
743                                 newprompt("Enter host or IP   : ",
744                                         &buf[strlen(buf)], 32);
745                                 strcat(buf, "|504");
746                                 strprompt("Enter port number  : ",
747                                         &buf[strlen(buf)-3], 5);
748                                 recs[num_recs-1] = strdup(buf);
749                                 modified = 1;
750                                 break;
751                         case 'd':
752                                 i = intprompt("Delete which one",
753                                         1, 1, num_recs) - 1;
754                                 free(recs[i]);
755                                 --num_recs;
756                                 for (j=i; j<num_recs; ++j)
757                                         recs[j] = recs[j+1];
758                                 modified = 1;
759                                 break;
760                         case 's':
761                                 r = 1;
762                                 for (i = 0; i < num_recs; ++i)
763                                         r += 1 + strlen(recs[i]);
764                                 listing = (char*) calloc(1, r);
765                                 if (!listing) {
766                                         scr_printf("Can't save config - out of memory!\n");
767                                         logoff(ipc, 1);
768                                 }
769                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
770                                         strcat(listing, recs[i]);
771                                         strcat(listing, "\n");
772                                 }
773                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
774                                 if (r / 100 != 4) {
775                                         scr_printf("%s\n", buf);
776                                 } else {
777                                         scr_printf("Wrote %d records.\n", num_recs);
778                                         modified = 0;
779                                 }
780                 free(listing);
781                                 break;
782                         case 'q':
783                                 quitting = !modified || boolprompt(
784                                         "Quit without saving", 0);
785                                 break;
786                         default:
787                                 break;
788                 }
789         } while (!quitting);
790
791         if (recs != NULL) {
792                 for (i=0; i<num_recs; ++i) free(recs[i]);
793                 free(recs);
794         }
795 }
796
797
798 /*
799  * Filter list configuration
800  */
801 void do_filterlist_configuration(CtdlIPC *ipc)
802 {
803         char buf[SIZ];
804         int num_recs = 0;
805         char **recs = NULL;
806         char ch;
807         int i, j;
808         int quitting = 0;
809         int modified = 0;
810         char *listing = NULL;
811         int r;
812
813         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
814         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
815                 extract_token(buf, listing, 0, '\n', sizeof buf);
816                 remove_token(listing, 0, '\n');
817
818                 ++num_recs;
819                 if (num_recs == 1) recs = malloc(sizeof(char *));
820                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
821                 recs[num_recs-1] = malloc(SIZ);
822                 strcpy(recs[num_recs-1], buf);
823         }
824         if (listing) free(listing);
825
826         do {
827                 scr_printf("\n");
828                 color(BRIGHT_WHITE);
829                 scr_printf(     "### "
830                         "         User name           "
831                         "         Room name           "
832                         "    Node name    "
833                         "\n");
834                 color(DIM_WHITE);
835                 scr_printf(     "--- "
836                         "---------------------------- "
837                         "---------------------------- "
838                         "---------------- "
839                         "\n");
840                 for (i=0; i<num_recs; ++i) {
841                 color(DIM_WHITE);
842                 scr_printf("%3d ", i+1);
843                 extract_token(buf, recs[i], 0, '|', sizeof buf);
844                 color(BRIGHT_CYAN);
845                 scr_printf("%-28s ", buf);
846                 extract_token(buf, recs[i], 1, '|', sizeof buf);
847                 color(BRIGHT_MAGENTA);
848                 scr_printf("%-28s ", buf);
849                 extract_token(buf, recs[i], 2, '|', sizeof buf);
850                 color(BRIGHT_CYAN);
851                 scr_printf("%-16s\n", buf);
852                 extract_token(buf, recs[i], 3, '|', sizeof buf);
853                 color(DIM_WHITE);
854                 }
855
856                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
857                 switch(ch) {
858                         case 'a':
859                                 ++num_recs;
860                                 if (num_recs == 1)
861                                         recs = malloc(sizeof(char *));
862                                 else recs = realloc(recs,
863                                         (sizeof(char *)) * num_recs);
864                                 newprompt("Enter user name: ", buf, 28);
865                                 strcat(buf, "|");
866                                 newprompt("Enter room name: ",
867                                         &buf[strlen(buf)], 28);
868                                 strcat(buf, "|");
869                                 newprompt("Enter node name: ",
870                                         &buf[strlen(buf)], 16);
871                                 strcat(buf, "|");
872                                 recs[num_recs-1] = strdup(buf);
873                                 modified = 1;
874                                 break;
875                         case 'd':
876                                 i = intprompt("Delete which one",
877                                         1, 1, num_recs) - 1;
878                                 free(recs[i]);
879                                 --num_recs;
880                                 for (j=i; j<num_recs; ++j)
881                                         recs[j] = recs[j+1];
882                                 modified = 1;
883                                 break;
884                         case 's':
885                                 r = 1;
886                                 for (i = 0; i < num_recs; ++i)
887                                         r += 1 + strlen(recs[i]);
888                                 listing = (char*) calloc(1, r);
889                                 if (!listing) {
890                                         scr_printf("Can't save config - out of memory!\n");
891                                         logoff(ipc, 1);
892                                 }
893                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
894                                         strcat(listing, recs[i]);
895                                         strcat(listing, "\n");
896                                 }
897                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
898                                 if (r / 100 != 4) {
899                                         scr_printf("%s\n", buf);
900                                 } else {
901                                         scr_printf("Wrote %d records.\n", num_recs);
902                                         modified = 0;
903                                 }
904                 free(listing);
905                                 break;
906                         case 'q':
907                                 quitting = !modified || boolprompt(
908                                         "Quit without saving", 0);
909                                 break;
910                         default:
911                                 break;
912                 }
913         } while (!quitting);
914
915         if (recs != NULL) {
916                 for (i=0; i<num_recs; ++i) free(recs[i]);
917                 free(recs);
918         }
919 }
920
921
922
923
924 /*
925  * POP3 aggregation client configuration
926  */
927 void do_pop3client_configuration(CtdlIPC *ipc)
928 {
929         char buf[SIZ];
930         int num_recs = 0;
931         char **recs = NULL;
932         char ch;
933         int i, j;
934         int quitting = 0;
935         int modified = 0;
936         char *listing = NULL;
937         char *other_listing = NULL;
938         int r;
939         char instr[SIZ];
940
941         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
942         if (r / 100 == 1) {
943                 while(listing && !IsEmptyStr(listing)) {
944                         extract_token(buf, listing, 0, '\n', sizeof buf);
945                         remove_token(listing, 0, '\n');
946                         extract_token(instr, buf, 0, '|', sizeof instr);
947                         if (!strcasecmp(instr, "pop3client")) {
948
949                                 ++num_recs;
950                                 if (num_recs == 1) recs = malloc(sizeof(char *));
951                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
952                                 recs[num_recs-1] = malloc(SIZ);
953                                 strcpy(recs[num_recs-1], buf);
954
955                         }
956                 }
957         }
958         if (listing) {
959                 free(listing);
960                 listing = NULL;
961         }
962
963         do {
964                 scr_printf("\n");
965                 color(BRIGHT_WHITE);
966                 scr_printf(     "### "
967                         "      Remote POP3 host       "
968                         "         User name           "
969                         "Keep on server? "
970                         "\n");
971                 color(DIM_WHITE);
972                 scr_printf(     "--- "
973                         "---------------------------- "
974                         "---------------------------- "
975                         "--------------- "
976                         "\n");
977                 for (i=0; i<num_recs; ++i) {
978                 color(DIM_WHITE);
979                 scr_printf("%3d ", i+1);
980
981                 extract_token(buf, recs[i], 1, '|', sizeof buf);
982                 color(BRIGHT_CYAN);
983                 scr_printf("%-28s ", buf);
984
985                 extract_token(buf, recs[i], 2, '|', sizeof buf);
986                 color(BRIGHT_MAGENTA);
987                 scr_printf("%-28s ", buf);
988
989                 color(BRIGHT_CYAN);
990                 scr_printf("%-15s\n", (extract_int(recs[i], 4) ? "Yes" : "No") );
991                 color(DIM_WHITE);
992                 }
993
994                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
995                 switch(ch) {
996                         case 'a':
997                                 ++num_recs;
998                                 if (num_recs == 1) {
999                                         recs = malloc(sizeof(char *));
1000                                 }
1001                                 else {
1002                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
1003                                 }
1004                                 strcpy(buf, "pop3client|");
1005                                 newprompt("Enter host name: ", &buf[strlen(buf)], 28);
1006                                 strcat(buf, "|");
1007                                 newprompt("Enter user name: ", &buf[strlen(buf)], 28);
1008                                 strcat(buf, "|");
1009                                 newprompt("Enter password : ", &buf[strlen(buf)], 16);
1010                                 strcat(buf, "|");
1011                                 scr_printf("Keep messages on server instead of deleting them? ");
1012                                 sprintf(&buf[strlen(buf)], "%d", yesno());
1013                                 strcat(buf, "|");
1014                                 recs[num_recs-1] = strdup(buf);
1015                                 modified = 1;
1016                                 break;
1017                         case 'd':
1018                                 i = intprompt("Delete which one",
1019                                         1, 1, num_recs) - 1;
1020                                 free(recs[i]);
1021                                 --num_recs;
1022                                 for (j=i; j<num_recs; ++j)
1023                                         recs[j] = recs[j+1];
1024                                 modified = 1;
1025                                 break;
1026                         case 's':
1027                                 r = 1;
1028                                 for (i = 0; i < num_recs; ++i) {
1029                                         r += 1 + strlen(recs[i]);
1030                                 }
1031                                 listing = (char*) calloc(1, r);
1032                                 if (!listing) {
1033                                         scr_printf("Can't save config - out of memory!\n");
1034                                         logoff(ipc, 1);
1035                                 }
1036                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1037                                         strcat(listing, recs[i]);
1038                                         strcat(listing, "\n");
1039                                 }
1040
1041                                 /* Retrieve all the *other* records for merging */
1042                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
1043                                 if (r / 100 == 1) {
1044                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
1045                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
1046                                                 if (strncasecmp(buf, "pop3client|", 11)) {
1047                                                         listing = realloc(listing, strlen(listing) +
1048                                                                 strlen(buf) + 10);
1049                                                         strcat(listing, buf);
1050                                                         strcat(listing, "\n");
1051                                                 }
1052                                         }
1053                                 }
1054                                 free(other_listing);
1055                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1056                                 free(listing);
1057                                 listing = NULL;
1058
1059                                 if (r / 100 != 4) {
1060                                         scr_printf("%s\n", buf);
1061                                 } else {
1062                                         scr_printf("Wrote %d records.\n", num_recs);
1063                                         modified = 0;
1064                                 }
1065                                 quitting = 1;
1066                                 break;
1067                         case 'q':
1068                                 quitting = !modified || boolprompt(
1069                                         "Quit without saving", 0);
1070                                 break;
1071                         default:
1072                                 break;
1073                 }
1074         } while (!quitting);
1075
1076         if (recs != NULL) {
1077                 for (i=0; i<num_recs; ++i) free(recs[i]);
1078                 free(recs);
1079         }
1080 }
1081
1082
1083
1084
1085
1086
1087 /*
1088  * RSS feed retrieval client configuration
1089  */
1090 void do_rssclient_configuration(CtdlIPC *ipc)
1091 {
1092         char buf[SIZ];
1093         int num_recs = 0;
1094         char **recs = NULL;
1095         char ch;
1096         int i, j;
1097         int quitting = 0;
1098         int modified = 0;
1099         char *listing = NULL;
1100         char *other_listing = NULL;
1101         int r;
1102         char instr[SIZ];
1103
1104         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1105         if (r / 100 == 1) {
1106                 while(listing && !IsEmptyStr(listing)) {
1107                         extract_token(buf, listing, 0, '\n', sizeof buf);
1108                         remove_token(listing, 0, '\n');
1109                         extract_token(instr, buf, 0, '|', sizeof instr);
1110                         if (!strcasecmp(instr, "rssclient")) {
1111
1112                                 ++num_recs;
1113                                 if (num_recs == 1) recs = malloc(sizeof(char *));
1114                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1115                                 recs[num_recs-1] = malloc(SIZ);
1116                                 strcpy(recs[num_recs-1], buf);
1117
1118                         }
1119                 }
1120         }
1121         if (listing) {
1122                 free(listing);
1123                 listing = NULL;
1124         }
1125
1126         do {
1127                 scr_printf("\n");
1128                 color(BRIGHT_WHITE);
1129                 scr_printf("### Feed URL\n");
1130                 color(DIM_WHITE);
1131                 scr_printf("--- "
1132                         "---------------------------------------------------------------------------"
1133                         "\n");
1134                 
1135                 for (i=0; i<num_recs; ++i) {
1136                 color(DIM_WHITE);
1137                 scr_printf("%3d ", i+1);
1138
1139                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1140                 color(BRIGHT_CYAN);
1141                 scr_printf("%-75s\n", buf);
1142
1143                 color(DIM_WHITE);
1144                 }
1145
1146                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1147                 switch(ch) {
1148                         case 'a':
1149                                 ++num_recs;
1150                                 if (num_recs == 1) {
1151                                         recs = malloc(sizeof(char *));
1152                                 }
1153                                 else {
1154                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
1155                                 }
1156                                 strcpy(buf, "rssclient|");
1157                                 newprompt("Enter feed URL: ", &buf[strlen(buf)], 75);
1158                                 strcat(buf, "|");
1159                                 recs[num_recs-1] = strdup(buf);
1160                                 modified = 1;
1161                                 break;
1162                         case 'd':
1163                                 i = intprompt("Delete which one", 1, 1, num_recs) - 1;
1164                                 free(recs[i]);
1165                                 --num_recs;
1166                                 for (j=i; j<num_recs; ++j)
1167                                         recs[j] = recs[j+1];
1168                                 modified = 1;
1169                                 break;
1170                         case 's':
1171                                 r = 1;
1172                                 for (i = 0; i < num_recs; ++i) {
1173                                         r += 1 + strlen(recs[i]);
1174                                 }
1175                                 listing = (char*) calloc(1, r);
1176                                 if (!listing) {
1177                                         scr_printf("Can't save config - out of memory!\n");
1178                                         logoff(ipc, 1);
1179                                 }
1180                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1181                                         strcat(listing, recs[i]);
1182                                         strcat(listing, "\n");
1183                                 }
1184
1185                                 /* Retrieve all the *other* records for merging */
1186                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
1187                                 if (r / 100 == 1) {
1188                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
1189                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
1190                                                 if (strncasecmp(buf, "rssclient|", 10)) {
1191                                                         listing = realloc(listing, strlen(listing) +
1192                                                                 strlen(buf) + 10);
1193                                                         strcat(listing, buf);
1194                                                         strcat(listing, "\n");
1195                                                 }
1196                                         }
1197                                 }
1198                                 free(other_listing);
1199                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1200                                 free(listing);
1201                                 listing = NULL;
1202
1203                                 if (r / 100 != 4) {
1204                                         scr_printf("%s\n", buf);
1205                                 } else {
1206                                         scr_printf("Wrote %d records.\n", num_recs);
1207                                         modified = 0;
1208                                 }
1209                                 quitting = 1;
1210                                 break;
1211                         case 'q':
1212                                 quitting = !modified || boolprompt(
1213                                         "Quit without saving", 0);
1214                                 break;
1215                         default:
1216                                 break;
1217                 }
1218         } while (!quitting);
1219
1220         if (recs != NULL) {
1221                 for (i=0; i<num_recs; ++i) free(recs[i]);
1222                 free(recs);
1223         }
1224 }
1225
1226