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