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