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