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