Remove obsolete client config option
[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> directory      (Consult the Global Address Book)\n");
387         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
388         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
389         keyopt(" <7> masq domains   (Domains as which users are allowed to masquerade)\n");
390         keyopt(" <8> ClamAV         (Address of ClamAV clamd server)\n");
391         sel = intprompt("Which one", 1, 1, 8);
392         switch(sel) {
393                 case 1: strcpy(buf, "localhost");
394                         return;
395                 case 2: strcpy(buf, "smarthost");
396                         return;
397                 case 3: strcpy(buf, "fallbackhost");
398                         return;
399                 case 4: strcpy(buf, "directory");
400                         return;
401                 case 5: strcpy(buf, "spamassassin");
402                         return;
403                 case 6: strcpy(buf, "rbl");
404                         return;
405                 case 7: strcpy(buf, "masqdomain");
406                         return;
407                 case 8: strcpy(buf, "clamav");
408                         return;
409         }
410 }
411
412
413 /*
414  * Internet mail configuration
415  */
416 void do_internet_configuration(CtdlIPC *ipc)
417 {
418         char buf[256];
419         char *resp = NULL;
420         int num_recs = 0;
421         char **recs = NULL;
422         char ch;
423         int i, j;
424         int quitting = 0;
425         int modified = 0;
426         int r;
427         
428         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
429         if (r / 100 == 1) {
430                 while (!IsEmptyStr(resp)) {
431                         extract_token(buf, resp, 0, '\n', sizeof buf);
432                         remove_token(resp, 0, '\n');
433                         ++num_recs;
434                         if (num_recs == 1) recs = malloc(sizeof(char *));
435                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
436                         recs[num_recs-1] = malloc(strlen(buf) + 1);
437                         strcpy(recs[num_recs-1], buf);
438                 }
439         }
440         if (resp) free(resp);
441
442         do {
443                 scr_printf("\n");
444                 color(BRIGHT_WHITE);
445                 scr_printf("###                    Host or domain                     Record type      \n");
446                 color(DIM_WHITE);
447                 scr_printf("--- -------------------------------------------------- --------------------\n");
448                 for (i=0; i<num_recs; ++i) {
449                 color(DIM_WHITE);
450                 scr_printf("%3d ", i+1);
451                 extract_token(buf, recs[i], 0, '|', sizeof buf);
452                 color(BRIGHT_CYAN);
453                 scr_printf("%-50s ", buf);
454                 extract_token(buf, recs[i], 1, '|', sizeof buf);
455                 color(BRIGHT_MAGENTA);
456                 scr_printf("%-20s\n", buf);
457                 color(DIM_WHITE);
458                 }
459
460                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
461                 switch(ch) {
462                         case 'a':
463                                 newprompt("Enter host name: ",
464                                         buf, 50);
465                                 striplt(buf);
466                                 if (!IsEmptyStr(buf)) {
467                                         ++num_recs;
468                                         if (num_recs == 1)
469                                                 recs = malloc(sizeof(char *));
470                                         else recs = realloc(recs,
471                                                 (sizeof(char *)) * num_recs);
472                                         strcat(buf, "|");
473                                         get_inet_rec_type(ipc,
474                                                         &buf[strlen(buf)]);
475                                         recs[num_recs-1] = strdup(buf);
476                                 }
477                                 modified = 1;
478                                 break;
479                         case 'd':
480                                 i = intprompt("Delete which one",
481                                         1, 1, num_recs) - 1;
482                                 free(recs[i]);
483                                 --num_recs;
484                                 for (j=i; j<num_recs; ++j)
485                                         recs[j] = recs[j+1];
486                                 modified = 1;
487                                 break;
488                         case 's':
489                                 r = 1;
490                                 for (i = 0; i < num_recs; i++)
491                                         r += 1 + strlen(recs[i]);
492                                 resp = (char *)calloc(1, r);
493                                 if (!resp) {
494                                         scr_printf("Can't save config - out of memory!\n");
495                                         logoff(ipc, 1);
496                                 }
497                                 if (num_recs) for (i = 0; i < num_recs; i++) {
498                                         strcat(resp, recs[i]);
499                                         strcat(resp, "\n");
500                                 }
501                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
502                                 if (r / 100 != 4) {
503                                         scr_printf("%s\n", buf);
504                                 } else {
505                                         scr_printf("Wrote %d records.\n", num_recs);
506                                         modified = 0;
507                                 }
508                 free(resp);
509                                 break;
510                         case 'q':
511                                 quitting = !modified || boolprompt(
512                                         "Quit without saving", 0);
513                                 break;
514                         default:
515                                 break;
516                 }
517         } while (!quitting);
518
519         if (recs != NULL) {
520                 for (i=0; i<num_recs; ++i) free(recs[i]);
521                 free(recs);
522         }
523 }
524
525
526
527 /*
528  * Edit network configuration for room sharing, mailing lists, etc.
529  */
530 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
531 {
532         char filename[PATH_MAX];
533         char changefile[PATH_MAX];
534         int e_ex_code;
535         pid_t editor_pid;
536         int cksum;
537         int b, i, tokens;
538         char buf[1024];
539         char instr[1024];
540         char addr[1024];
541         FILE *tempfp;
542         FILE *changefp;
543         char *listing = NULL;
544         int r;
545
546         if (IsEmptyStr(editor_path)) {
547                 scr_printf("You must have an external editor configured in order to use this function.\n");
548                 return;
549         }
550
551         CtdlMakeTempFileName(filename, sizeof filename);
552         CtdlMakeTempFileName(changefile, sizeof changefile);
553
554         tempfp = fopen(filename, "w");
555         if (tempfp == NULL) {
556                 scr_printf("Cannot open %s: %s\n", filename, strerror(errno));
557                 return;
558         }
559
560         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
561         fprintf(tempfp, "# %s\n", comment);
562         fprintf(tempfp, "# Specify one per line.\n"
563                         "\n\n");
564
565         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
566         if (r / 100 == 1) {
567                 while(listing && !IsEmptyStr(listing)) {
568                         extract_token(buf, listing, 0, '\n', sizeof buf);
569                         remove_token(listing, 0, '\n');
570                         extract_token(instr, buf, 0, '|', sizeof instr);
571                         if (!strcasecmp(instr, entrytype)) {
572                                 tokens = num_tokens(buf, '|');
573                                 for (i=1; i<tokens; ++i) {
574                                         extract_token(addr, buf, i, '|', sizeof addr);
575                                         fprintf(tempfp, "%s", addr);
576                                         if (i < (tokens-1)) {
577                                                 fprintf(tempfp, "|");
578                                         }
579                                 }
580                                 fprintf(tempfp, "\n");
581                         }
582                 }
583         }
584         if (listing) {
585                 free(listing);
586                 listing = NULL;
587         }
588         fclose(tempfp);
589
590         e_ex_code = 1;  /* start with a failed exit code */
591         stty_ctdl(SB_RESTORE);
592         editor_pid = fork();
593         cksum = file_checksum(filename);
594         if (editor_pid == 0) {
595                 chmod(filename, 0600);
596                 putenv("WINDOW_TITLE=Network configuration");
597                 execlp(editor_path, editor_path, filename, NULL);
598                 exit(1);
599         }
600         if (editor_pid > 0) {
601                 do {
602                         e_ex_code = 0;
603                         b = ka_wait(&e_ex_code);
604                 } while ((b != editor_pid) && (b >= 0));
605         editor_pid = (-1);
606         stty_ctdl(0);
607         }
608
609         if (file_checksum(filename) == cksum) {
610                 scr_printf("*** No changes to save.\n");
611                 e_ex_code = 1;
612         }
613
614         if (e_ex_code == 0) {           /* Save changes */
615                 changefp = fopen(changefile, "w");
616
617                 /* Load all netconfig entries that are *not* of the type we are editing */
618                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
619                 if (r / 100 == 1) {
620                         while(listing && !IsEmptyStr(listing)) {
621                                 extract_token(buf, listing, 0, '\n', sizeof buf);
622                                 remove_token(listing, 0, '\n');
623                                 extract_token(instr, buf, 0, '|', sizeof instr);
624                                 if (strcasecmp(instr, entrytype)) {
625                                         fprintf(changefp, "%s\n", buf);
626                                 }
627                         }
628                 }
629                 if (listing) {
630                         free(listing);
631                         listing = NULL;
632                 }
633
634                 /* ...and merge that with the data we just edited */
635                 tempfp = fopen(filename, "r");
636                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
637                         for (i=0; i<strlen(buf); ++i) {
638                                 if (buf[i] == '#') buf[i] = 0;
639                         }
640                         striplt(buf);
641                         if (!IsEmptyStr(buf)) {
642                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
643                         }
644                 }
645                 fclose(tempfp);
646                 fclose(changefp);
647
648                 /* now write it to the server... */
649                 changefp = fopen(changefile, "r");
650                 if (changefp != NULL) {
651                         listing = load_message_from_file(changefp);
652                         if (listing) {
653                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
654                                 free(listing);
655                                 listing = NULL;
656                         }
657                         fclose(changefp);
658                 }
659         }
660
661         unlink(filename);               /* Delete the temporary files */
662         unlink(changefile);
663 }
664
665
666 /*
667  * IGnet node configuration
668  */
669 void do_ignet_configuration(CtdlIPC *ipc) {
670         char buf[SIZ];
671         int num_recs = 0;
672         char **recs = NULL;
673         char ch;
674         int i, j;
675         int quitting = 0;
676         int modified = 0;
677         char *listing = NULL;
678         int r;
679
680         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
681         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
682                 extract_token(buf, listing, 0, '\n', sizeof buf);
683                 remove_token(listing, 0, '\n');
684
685                 ++num_recs;
686                 if (num_recs == 1) recs = malloc(sizeof(char *));
687                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
688                 recs[num_recs-1] = malloc(SIZ);
689                 strcpy(recs[num_recs-1], buf);
690         }
691         if (listing) free(listing);
692
693         do {
694                 scr_printf("\n");
695                 color(BRIGHT_WHITE);
696                 scr_printf(     "### "
697                         "   Node          "
698                         "  Secret           "
699                         "          Host or IP             "
700                         "Port#\n");
701                 color(DIM_WHITE);
702                 scr_printf(     "--- "
703                         "---------------- "
704                         "------------------ "
705                         "-------------------------------- "
706                         "-----\n");
707                 for (i=0; i<num_recs; ++i) {
708                 color(DIM_WHITE);
709                 scr_printf("%3d ", i+1);
710                 extract_token(buf, recs[i], 0, '|', sizeof buf);
711                 color(BRIGHT_CYAN);
712                 scr_printf("%-16s ", buf);
713                 extract_token(buf, recs[i], 1, '|', sizeof buf);
714                 color(BRIGHT_MAGENTA);
715                 scr_printf("%-18s ", buf);
716                 extract_token(buf, recs[i], 2, '|', sizeof buf);
717                 color(BRIGHT_CYAN);
718                 scr_printf("%-32s ", buf);
719                 extract_token(buf, recs[i], 3, '|', sizeof buf);
720                 color(BRIGHT_MAGENTA);
721                 scr_printf("%-3s\n", buf);
722                 color(DIM_WHITE);
723                 }
724                 scr_printf("\n");
725
726                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
727                 switch(ch) {
728                         case 'a':
729                                 ++num_recs;
730                                 if (num_recs == 1)
731                                         recs = malloc(sizeof(char *));
732                                 else recs = realloc(recs,
733                                         (sizeof(char *)) * num_recs);
734                                 newprompt("Enter node name    : ", buf, 16);
735                                 strcat(buf, "|");
736                                 newprompt("Enter shared secret: ",
737                                         &buf[strlen(buf)], 18);
738                                 strcat(buf, "|");
739                                 newprompt("Enter host or IP   : ",
740                                         &buf[strlen(buf)], 32);
741                                 strcat(buf, "|504");
742                                 strprompt("Enter port number  : ",
743                                         &buf[strlen(buf)-3], 5);
744                                 recs[num_recs-1] = strdup(buf);
745                                 modified = 1;
746                                 break;
747                         case 'd':
748                                 i = intprompt("Delete which one",
749                                         1, 1, num_recs) - 1;
750                                 free(recs[i]);
751                                 --num_recs;
752                                 for (j=i; j<num_recs; ++j)
753                                         recs[j] = recs[j+1];
754                                 modified = 1;
755                                 break;
756                         case 's':
757                                 r = 1;
758                                 for (i = 0; i < num_recs; ++i)
759                                         r += 1 + strlen(recs[i]);
760                                 listing = (char*) calloc(1, r);
761                                 if (!listing) {
762                                         scr_printf("Can't save config - out of memory!\n");
763                                         logoff(ipc, 1);
764                                 }
765                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
766                                         strcat(listing, recs[i]);
767                                         strcat(listing, "\n");
768                                 }
769                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
770                                 if (r / 100 != 4) {
771                                         scr_printf("%s\n", buf);
772                                 } else {
773                                         scr_printf("Wrote %d records.\n", num_recs);
774                                         modified = 0;
775                                 }
776                 free(listing);
777                                 break;
778                         case 'q':
779                                 quitting = !modified || boolprompt(
780                                         "Quit without saving", 0);
781                                 break;
782                         default:
783                                 break;
784                 }
785         } while (!quitting);
786
787         if (recs != NULL) {
788                 for (i=0; i<num_recs; ++i) free(recs[i]);
789                 free(recs);
790         }
791 }
792
793
794 /*
795  * Filter list configuration
796  */
797 void do_filterlist_configuration(CtdlIPC *ipc)
798 {
799         char buf[SIZ];
800         int num_recs = 0;
801         char **recs = NULL;
802         char ch;
803         int i, j;
804         int quitting = 0;
805         int modified = 0;
806         char *listing = NULL;
807         int r;
808
809         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
810         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
811                 extract_token(buf, listing, 0, '\n', sizeof buf);
812                 remove_token(listing, 0, '\n');
813
814                 ++num_recs;
815                 if (num_recs == 1) recs = malloc(sizeof(char *));
816                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
817                 recs[num_recs-1] = malloc(SIZ);
818                 strcpy(recs[num_recs-1], buf);
819         }
820         if (listing) free(listing);
821
822         do {
823                 scr_printf("\n");
824                 color(BRIGHT_WHITE);
825                 scr_printf(     "### "
826                         "         User name           "
827                         "         Room name           "
828                         "    Node name    "
829                         "\n");
830                 color(DIM_WHITE);
831                 scr_printf(     "--- "
832                         "---------------------------- "
833                         "---------------------------- "
834                         "---------------- "
835                         "\n");
836                 for (i=0; i<num_recs; ++i) {
837                 color(DIM_WHITE);
838                 scr_printf("%3d ", i+1);
839                 extract_token(buf, recs[i], 0, '|', sizeof buf);
840                 color(BRIGHT_CYAN);
841                 scr_printf("%-28s ", buf);
842                 extract_token(buf, recs[i], 1, '|', sizeof buf);
843                 color(BRIGHT_MAGENTA);
844                 scr_printf("%-28s ", buf);
845                 extract_token(buf, recs[i], 2, '|', sizeof buf);
846                 color(BRIGHT_CYAN);
847                 scr_printf("%-16s\n", buf);
848                 extract_token(buf, recs[i], 3, '|', sizeof buf);
849                 color(DIM_WHITE);
850                 }
851
852                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
853                 switch(ch) {
854                         case 'a':
855                                 ++num_recs;
856                                 if (num_recs == 1)
857                                         recs = malloc(sizeof(char *));
858                                 else recs = realloc(recs,
859                                         (sizeof(char *)) * num_recs);
860                                 newprompt("Enter user name: ", buf, 28);
861                                 strcat(buf, "|");
862                                 newprompt("Enter room name: ",
863                                         &buf[strlen(buf)], 28);
864                                 strcat(buf, "|");
865                                 newprompt("Enter node name: ",
866                                         &buf[strlen(buf)], 16);
867                                 strcat(buf, "|");
868                                 recs[num_recs-1] = strdup(buf);
869                                 modified = 1;
870                                 break;
871                         case 'd':
872                                 i = intprompt("Delete which one",
873                                         1, 1, num_recs) - 1;
874                                 free(recs[i]);
875                                 --num_recs;
876                                 for (j=i; j<num_recs; ++j)
877                                         recs[j] = recs[j+1];
878                                 modified = 1;
879                                 break;
880                         case 's':
881                                 r = 1;
882                                 for (i = 0; i < num_recs; ++i)
883                                         r += 1 + strlen(recs[i]);
884                                 listing = (char*) calloc(1, r);
885                                 if (!listing) {
886                                         scr_printf("Can't save config - out of memory!\n");
887                                         logoff(ipc, 1);
888                                 }
889                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
890                                         strcat(listing, recs[i]);
891                                         strcat(listing, "\n");
892                                 }
893                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
894                                 if (r / 100 != 4) {
895                                         scr_printf("%s\n", buf);
896                                 } else {
897                                         scr_printf("Wrote %d records.\n", num_recs);
898                                         modified = 0;
899                                 }
900                 free(listing);
901                                 break;
902                         case 'q':
903                                 quitting = !modified || boolprompt(
904                                         "Quit without saving", 0);
905                                 break;
906                         default:
907                                 break;
908                 }
909         } while (!quitting);
910
911         if (recs != NULL) {
912                 for (i=0; i<num_recs; ++i) free(recs[i]);
913                 free(recs);
914         }
915 }
916
917
918
919
920 /*
921  * POP3 aggregation client configuration
922  */
923 void do_pop3client_configuration(CtdlIPC *ipc)
924 {
925         char buf[SIZ];
926         int num_recs = 0;
927         char **recs = NULL;
928         char ch;
929         int i, j;
930         int quitting = 0;
931         int modified = 0;
932         char *listing = NULL;
933         char *other_listing = NULL;
934         int r;
935         char instr[SIZ];
936
937         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
938         if (r / 100 == 1) {
939                 while(listing && !IsEmptyStr(listing)) {
940                         extract_token(buf, listing, 0, '\n', sizeof buf);
941                         remove_token(listing, 0, '\n');
942                         extract_token(instr, buf, 0, '|', sizeof instr);
943                         if (!strcasecmp(instr, "pop3client")) {
944
945                                 ++num_recs;
946                                 if (num_recs == 1) recs = malloc(sizeof(char *));
947                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
948                                 recs[num_recs-1] = malloc(SIZ);
949                                 strcpy(recs[num_recs-1], buf);
950
951                         }
952                 }
953         }
954         if (listing) {
955                 free(listing);
956                 listing = NULL;
957         }
958
959         do {
960                 scr_printf("\n");
961                 color(BRIGHT_WHITE);
962                 scr_printf(     "### "
963                         "      Remote POP3 host       "
964                         "         User name           "
965                         "Keep on server? "
966                         "\n");
967                 color(DIM_WHITE);
968                 scr_printf(     "--- "
969                         "---------------------------- "
970                         "---------------------------- "
971                         "--------------- "
972                         "\n");
973                 for (i=0; i<num_recs; ++i) {
974                 color(DIM_WHITE);
975                 scr_printf("%3d ", i+1);
976
977                 extract_token(buf, recs[i], 1, '|', sizeof buf);
978                 color(BRIGHT_CYAN);
979                 scr_printf("%-28s ", buf);
980
981                 extract_token(buf, recs[i], 2, '|', sizeof buf);
982                 color(BRIGHT_MAGENTA);
983                 scr_printf("%-28s ", buf);
984
985                 color(BRIGHT_CYAN);
986                 scr_printf("%-15s\n", (extract_int(recs[i], 4) ? "Yes" : "No") );
987                 color(DIM_WHITE);
988                 }
989
990                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
991                 switch(ch) {
992                         case 'a':
993                                 ++num_recs;
994                                 if (num_recs == 1) {
995                                         recs = malloc(sizeof(char *));
996                                 }
997                                 else {
998                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
999                                 }
1000                                 strcpy(buf, "pop3client|");
1001                                 newprompt("Enter host name: ", &buf[strlen(buf)], 28);
1002                                 strcat(buf, "|");
1003                                 newprompt("Enter user name: ", &buf[strlen(buf)], 28);
1004                                 strcat(buf, "|");
1005                                 newprompt("Enter password : ", &buf[strlen(buf)], 16);
1006                                 strcat(buf, "|");
1007                                 scr_printf("Keep messages on server instead of deleting them? ");
1008                                 sprintf(&buf[strlen(buf)], "%d", yesno());
1009                                 strcat(buf, "|");
1010                                 recs[num_recs-1] = strdup(buf);
1011                                 modified = 1;
1012                                 break;
1013                         case 'd':
1014                                 i = intprompt("Delete which one",
1015                                         1, 1, num_recs) - 1;
1016                                 free(recs[i]);
1017                                 --num_recs;
1018                                 for (j=i; j<num_recs; ++j)
1019                                         recs[j] = recs[j+1];
1020                                 modified = 1;
1021                                 break;
1022                         case 's':
1023                                 r = 1;
1024                                 for (i = 0; i < num_recs; ++i) {
1025                                         r += 1 + strlen(recs[i]);
1026                                 }
1027                                 listing = (char*) calloc(1, r);
1028                                 if (!listing) {
1029                                         scr_printf("Can't save config - out of memory!\n");
1030                                         logoff(ipc, 1);
1031                                 }
1032                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1033                                         strcat(listing, recs[i]);
1034                                         strcat(listing, "\n");
1035                                 }
1036
1037                                 /* Retrieve all the *other* records for merging */
1038                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
1039                                 if (r / 100 == 1) {
1040                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
1041                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
1042                                                 if (strncasecmp(buf, "pop3client|", 11)) {
1043                                                         listing = realloc(listing, strlen(listing) +
1044                                                                 strlen(buf) + 10);
1045                                                         strcat(listing, buf);
1046                                                         strcat(listing, "\n");
1047                                                 }
1048                                         }
1049                                 }
1050                                 free(other_listing);
1051                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1052                                 free(listing);
1053                                 listing = NULL;
1054
1055                                 if (r / 100 != 4) {
1056                                         scr_printf("%s\n", buf);
1057                                 } else {
1058                                         scr_printf("Wrote %d records.\n", num_recs);
1059                                         modified = 0;
1060                                 }
1061                                 quitting = 1;
1062                                 break;
1063                         case 'q':
1064                                 quitting = !modified || boolprompt(
1065                                         "Quit without saving", 0);
1066                                 break;
1067                         default:
1068                                 break;
1069                 }
1070         } while (!quitting);
1071
1072         if (recs != NULL) {
1073                 for (i=0; i<num_recs; ++i) free(recs[i]);
1074                 free(recs);
1075         }
1076 }
1077
1078
1079
1080
1081
1082
1083 /*
1084  * RSS feed retrieval client configuration
1085  */
1086 void do_rssclient_configuration(CtdlIPC *ipc)
1087 {
1088         char buf[SIZ];
1089         int num_recs = 0;
1090         char **recs = NULL;
1091         char ch;
1092         int i, j;
1093         int quitting = 0;
1094         int modified = 0;
1095         char *listing = NULL;
1096         char *other_listing = NULL;
1097         int r;
1098         char instr[SIZ];
1099
1100         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
1101         if (r / 100 == 1) {
1102                 while(listing && !IsEmptyStr(listing)) {
1103                         extract_token(buf, listing, 0, '\n', sizeof buf);
1104                         remove_token(listing, 0, '\n');
1105                         extract_token(instr, buf, 0, '|', sizeof instr);
1106                         if (!strcasecmp(instr, "rssclient")) {
1107
1108                                 ++num_recs;
1109                                 if (num_recs == 1) recs = malloc(sizeof(char *));
1110                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1111                                 recs[num_recs-1] = malloc(SIZ);
1112                                 strcpy(recs[num_recs-1], buf);
1113
1114                         }
1115                 }
1116         }
1117         if (listing) {
1118                 free(listing);
1119                 listing = NULL;
1120         }
1121
1122         do {
1123                 scr_printf("\n");
1124                 color(BRIGHT_WHITE);
1125                 scr_printf("### Feed URL\n");
1126                 color(DIM_WHITE);
1127                 scr_printf("--- "
1128                         "---------------------------------------------------------------------------"
1129                         "\n");
1130                 
1131                 for (i=0; i<num_recs; ++i) {
1132                 color(DIM_WHITE);
1133                 scr_printf("%3d ", i+1);
1134
1135                 extract_token(buf, recs[i], 1, '|', sizeof buf);
1136                 color(BRIGHT_CYAN);
1137                 scr_printf("%-75s\n", buf);
1138
1139                 color(DIM_WHITE);
1140                 }
1141
1142                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1143                 switch(ch) {
1144                         case 'a':
1145                                 ++num_recs;
1146                                 if (num_recs == 1) {
1147                                         recs = malloc(sizeof(char *));
1148                                 }
1149                                 else {
1150                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
1151                                 }
1152                                 strcpy(buf, "rssclient|");
1153                                 newprompt("Enter feed URL: ", &buf[strlen(buf)], 75);
1154                                 strcat(buf, "|");
1155                                 recs[num_recs-1] = strdup(buf);
1156                                 modified = 1;
1157                                 break;
1158                         case 'd':
1159                                 i = intprompt("Delete which one", 1, 1, num_recs) - 1;
1160                                 free(recs[i]);
1161                                 --num_recs;
1162                                 for (j=i; j<num_recs; ++j)
1163                                         recs[j] = recs[j+1];
1164                                 modified = 1;
1165                                 break;
1166                         case 's':
1167                                 r = 1;
1168                                 for (i = 0; i < num_recs; ++i) {
1169                                         r += 1 + strlen(recs[i]);
1170                                 }
1171                                 listing = (char*) calloc(1, r);
1172                                 if (!listing) {
1173                                         scr_printf("Can't save config - out of memory!\n");
1174                                         logoff(ipc, 1);
1175                                 }
1176                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1177                                         strcat(listing, recs[i]);
1178                                         strcat(listing, "\n");
1179                                 }
1180
1181                                 /* Retrieve all the *other* records for merging */
1182                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
1183                                 if (r / 100 == 1) {
1184                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
1185                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
1186                                                 if (strncasecmp(buf, "rssclient|", 10)) {
1187                                                         listing = realloc(listing, strlen(listing) +
1188                                                                 strlen(buf) + 10);
1189                                                         strcat(listing, buf);
1190                                                         strcat(listing, "\n");
1191                                                 }
1192                                         }
1193                                 }
1194                                 free(other_listing);
1195                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1196                                 free(listing);
1197                                 listing = NULL;
1198
1199                                 if (r / 100 != 4) {
1200                                         scr_printf("%s\n", buf);
1201                                 } else {
1202                                         scr_printf("Wrote %d records.\n", num_recs);
1203                                         modified = 0;
1204                                 }
1205                                 quitting = 1;
1206                                 break;
1207                         case 'q':
1208                                 quitting = !modified || boolprompt(
1209                                         "Quit without saving", 0);
1210                                 break;
1211                         default:
1212                                 break;
1213                 }
1214         } while (!quitting);
1215
1216         if (recs != NULL) {
1217                 for (i=0; i<num_recs; ++i) free(recs[i]);
1218                 free(recs);
1219         }
1220 }
1221
1222