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