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