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