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