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