efc7affc5a03c83fcf663ff36c87b30413c46cd0
[citadel.git] / textclient / tuiconfig.c
1 /*
2  * Configuration screens that are part of the text mode client.
3  *
4  * Copyright (c) 1987-2018 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "textclient.h"
16
17 extern char temp[];
18 extern char tempdir[];
19 extern char *axdefs[8];
20 extern long highest_msg_read;
21 extern long maxmsgnum;
22 extern unsigned room_flags;
23 extern int screenwidth;
24 char editor_path[PATH_MAX];
25
26
27 /* 
28  * General system configuration command
29  */
30 void do_system_configuration(CtdlIPC *ipc)
31 {
32
33         /* NUM_CONFIGS is now defined in citadel.h */
34
35         char buf[256];
36         char sc[NUM_CONFIGS][256];
37         char *resp = NULL;
38         struct ExpirePolicy *site_expirepolicy = NULL;
39         struct ExpirePolicy *mbx_expirepolicy = NULL;
40         int a;
41         int logpages = 0;
42         int r;                  /* IPC response code */
43         int server_configs = 0;
44
45         /* Clear out the config buffers */
46         memset(&sc[0][0], 0, sizeof(sc));
47
48         /* Fetch the current config */
49         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
50         if (r / 100 == 1) {
51                 server_configs = num_tokens(resp, '\n');
52                 for (a=0; a<server_configs; ++a) {
53                         if (a < NUM_CONFIGS) {
54                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
55                         }
56                 }
57         }
58         if (resp) free(resp);
59         resp = NULL;
60         /* Fetch the expire policy (this will silently fail on old servers,
61          * resulting in "default" policy)
62          */
63         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
64         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
65
66         /* Identification parameters */
67
68         strprompt("Node name", &sc[0][0], 15);
69         strprompt("Fully qualified domain name", &sc[1][0], 63);
70         strprompt("Human readable node name", &sc[2][0], 20);
71         strprompt("Telephone number", &sc[3][0], 15);
72         strprompt("Geographic location of this system", &sc[12][0], 31);
73         strprompt("Name of system administrator", &sc[13][0], 25);
74         strprompt("Paginator prompt", &sc[10][0], 79);
75
76         /* Security parameters */
77
78         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt("Require registration for new users", atoi(&sc[7][0]))));
79         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt("Disable self-service user account creation", atoi(&sc[29][0]))));
80         strprompt("Initial access level for new users", &sc[6][0], 1);
81         strprompt("Access level required to create rooms", &sc[19][0], 1);
82         snprintf(sc[67], sizeof sc[67], "%d", (boolprompt("Allow anonymous guest logins", atoi(&sc[67][0]))));
83         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
84                 "Automatically give room admin privs to a user who creates a private room",
85                 atoi(&sc[4][0]))));
86
87         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
88                 "Automatically move problem user messages to twit room",
89                 atoi(&sc[8][0]))));
90
91         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
92         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
93                 "Restrict Internet mail to only those with that privilege",
94                 atoi(&sc[11][0]))));
95         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
96                 "Allow admins to Zap (forget) rooms",
97                 atoi(&sc[26][0]))));
98
99         if (!IsEmptyStr(&sc[18][0])) {
100                 logpages = 1;
101         }
102         else {
103                 logpages = 0;
104         }
105         logpages = boolprompt("Log all instant messages", logpages);
106         if (logpages) {
107                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
108         }
109         else {
110                 sc[18][0] = 0;
111         }
112
113         /* Commented out because this setting isn't really appropriate to
114          * change while the server is running.
115          *
116          * snprintf(sc[52], sizeof sc[52], "%d", (boolprompt(
117          *      "Use system authentication",
118          *      atoi(&sc[52][0]))));
119          */
120
121         /* Server tuning */
122
123         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
124         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
125         strprompt("Maximum message length", &sc[20][0], 20);
126         strprompt("Minimum number of worker threads", &sc[21][0], 3);
127         strprompt("Maximum number of worker threads", &sc[22][0], 3);
128         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
129                 "Automatically delete committed database logs",
130                 atoi(&sc[43][0]))));
131
132         strprompt("Server IP address (* for 'any')", &sc[37][0], 15);
133         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
134         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
135         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
136         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
137         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
138         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
139         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
140         strprompt("NNTP server port (-1 to disable)", &sc[70][0], 5);
141         strprompt("NNTPS server port (-1 to disable)", &sc[71][0], 5);
142         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
143         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
144
145         strprompt("XMPP (Jabber) client to server port (-1 to disable)", &sc[62][0], 5);
146         /* No prompt because we don't implement this service yet, it's just a placeholder */
147         /* strprompt("XMPP (Jabber) server to server port (-1 to disable)", &sc[63][0], 5); */
148
149         /* This logic flips the question around, because it's one of those
150          * situations where 0=yes and 1=no
151          */
152         a = atoi(sc[25]);
153         a = (a ? 0 : 1);
154         a = boolprompt("Correct forged From: lines during authenticated SMTP", a);
155         a = (a ? 0 : 1);
156         snprintf(sc[25], sizeof sc[25], "%d", a);
157
158         snprintf(sc[66], sizeof sc[66], "%d", (boolprompt(
159                 "Flag messages as spam instead of rejecting",
160                 atoi(&sc[66][0]))));
161
162         /* This logic flips the question around, because it's one of those
163          * situations where 0=yes and 1=no
164          */
165         a = atoi(sc[61]);
166         a = (a ? 0 : 1);
167         a = boolprompt("Force IMAP posts in public rooms to be from the user who submitted them", a);
168         a = (a ? 0 : 1);
169         snprintf(sc[61], sizeof sc[61], "%d", a);
170
171         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
172                 "Allow unauthenticated SMTP clients to spoof my domains",
173                 atoi(&sc[45][0]))));
174         snprintf(sc[57], sizeof sc[57], "%d", (boolprompt(
175                 "Perform RBL checks at greeting instead of after RCPT",
176                 atoi(&sc[57][0]))));
177
178         /* LDAP settings */
179         if (ipc->ServInfo.supports_ldap) {
180                 a = strlen(&sc[32][0]);
181                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
182                 a = boolprompt("Do you want to configure LDAP authentication?", a);
183                 if (a) {
184                         strprompt("Host name of LDAP server", &sc[32][0], 127);
185                         strprompt("Port number of LDAP service", &sc[33][0], 5);
186                         strprompt("Base DN", &sc[34][0], 255);
187                         strprompt("Bind DN (or blank for anonymous bind)", &sc[35][0], 255);
188                         strprompt("Password for bind DN (or blank for anonymous bind)", &sc[36][0], 255);
189                 }
190                 else {
191                         strcpy(&sc[32][0], "");
192                 }
193         }
194
195         /* Expiry settings */
196         strprompt("Default user purge time (days)", &sc[16][0], 5);
197         strprompt("Default room purge time (days)", &sc[17][0], 5);
198
199         /* Angels and demons dancing in my head... */
200         do {
201                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
202                 strprompt("System default message expire policy (? for list)",
203                           buf, 1);
204                 if (buf[0] == '?') {
205                         scr_printf("\n"
206                                 "1. Never automatically expire messages\n"
207                                 "2. Expire by message count\n"
208                                 "3. Expire by message age\n");
209                 }
210         } while ((buf[0] < '1') || (buf[0] > '3'));
211         site_expirepolicy->expire_mode = buf[0] - '0';
212
213         /* ...lunatics and monsters underneath my bed */
214         if (site_expirepolicy->expire_mode == 2) {
215                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
216                 strprompt("Keep how many messages online?", buf, 10);
217                 site_expirepolicy->expire_value = atol(buf);
218         }
219         if (site_expirepolicy->expire_mode == 3) {
220                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
221                 strprompt("Keep messages for how many days?", buf, 10);
222                 site_expirepolicy->expire_value = atol(buf);
223         }
224
225         /* Media messiahs preying on my fears... */
226         do {
227                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
228                 strprompt("Mailbox default message expire policy (? for list)",
229                           buf, 1);
230                 if (buf[0] == '?') {
231                         scr_printf("\n"
232                                 "0. Go with the system default\n"
233                                 "1. Never automatically expire messages\n"
234                                 "2. Expire by message count\n"
235                                 "3. Expire by message age\n");
236                 }
237         } while ((buf[0] < '0') || (buf[0] > '3'));
238         mbx_expirepolicy->expire_mode = buf[0] - '0';
239
240         /* ...Pop culture prophets playing in my ears */
241         if (mbx_expirepolicy->expire_mode == 2) {
242                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
243                 strprompt("Keep how many messages online?", buf, 10);
244                 mbx_expirepolicy->expire_value = atol(buf);
245         }
246         if (mbx_expirepolicy->expire_mode == 3) {
247                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
248                 strprompt("Keep messages for how many days?", buf, 10);
249                 mbx_expirepolicy->expire_value = atol(buf);
250         }
251
252         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
253         strprompt("Default frequency to run POP3 collection (in seconds)", &sc[64][0], 5);
254         strprompt("Fastest frequency to run POP3 collection (in seconds)", &sc[65][0], 5);
255         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
256         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
257                 "Enable full text search index (warning: resource intensive)",
258                 atoi(&sc[42][0]))));
259
260         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
261                 "Perform journaling of email messages",
262                 atoi(&sc[46][0]))));
263         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
264                 "Perform journaling of non-email messages",
265                 atoi(&sc[47][0]))));
266         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
267                 strprompt("Email destination of journalized messages",
268                         &sc[48][0], 127);
269         }
270
271         /* No more Funambol */
272         sc[53][0] = 0;
273         sc[54][0] = 0;
274         sc[55][0] = 0;
275         sc[56][0] = 0;
276
277         /* External pager stuff */
278         int yes_pager = 0;
279         if (strlen(sc[60]) > 0) yes_pager = 1;
280         yes_pager = boolprompt("Configure an external pager tool", yes_pager);
281         if (yes_pager) {
282                 strprompt("External pager tool", &sc[60][0], 255);
283         }
284         else {
285                 sc[60][0] = 0;
286         }
287
288         /* Master user account */
289         int yes_muacct = 0;
290         if (strlen(sc[58]) > 0) yes_muacct = 1;
291         yes_muacct = boolprompt("Enable a 'master user' account", yes_muacct);
292         if (yes_muacct) {
293                 strprompt("Master user name", &sc[58][0], 31);
294                 strprompt("Master user password", &sc[59][0], -31);
295         }
296         else {
297                 strcpy(&sc[58][0], "");
298                 strcpy(&sc[59][0], "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
299         }
300
301         /* Save it */
302         scr_printf("Save this configuration? ");
303         if (yesno()) {
304                 r = 1;
305                 for (a = 0; a < NUM_CONFIGS; a++) {
306                         r += 1 + strlen(sc[a]);
307                 }
308                 resp = (char *)calloc(1, r);
309                 if (!resp) {
310                         scr_printf("Can't save config - out of memory!\n");
311                         logoff(ipc, 1);
312                 }
313                 for (a = 0; a < NUM_CONFIGS; a++) {
314                         strcat(resp, sc[a]);
315                         strcat(resp, "\n");
316                 }
317                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
318                 if (r / 100 != 4) {
319                         scr_printf("%s\n", buf);
320                 }
321                 free(resp);
322
323                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
324                 if (r / 100 != 2) {
325                         scr_printf("%s\n", buf);
326                 }
327
328                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
329                 if (r / 100 != 2) {
330                         scr_printf("%s\n", buf);
331                 }
332
333         }
334     if (site_expirepolicy) free(site_expirepolicy);
335     if (mbx_expirepolicy) free(mbx_expirepolicy);
336 }
337
338
339 /*
340  * support function for do_internet_configuration()
341  */
342 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
343         int sel;
344
345         keyopt(" <1> localhost      (Alias for this computer)\n");
346         keyopt(" <2> smart host     (Forward all outbound mail to this host)\n");
347         keyopt(" <3> fallback host  (Send mail to this host only if direct delivery fails)\n");
348         keyopt(" <4> SpamAssassin   (Address of SpamAssassin server)\n");
349         keyopt(" <5> RBL            (domain suffix of spam hunting RBL)\n");
350         keyopt(" <6> masq domains   (Domains as which users are allowed to masquerade)\n");
351         keyopt(" <7> ClamAV         (Address of ClamAV clamd server)\n");
352         sel = intprompt("Which one", 1, 1, 8);
353         switch(sel) {
354                 case 1: strcpy(buf, "localhost");
355                         return;
356                 case 2: strcpy(buf, "smarthost");
357                         return;
358                 case 3: strcpy(buf, "fallbackhost");
359                         return;
360                 case 4: strcpy(buf, "spamassassin");
361                         return;
362                 case 5: strcpy(buf, "rbl");
363                         return;
364                 case 6: strcpy(buf, "masqdomain");
365                         return;
366                 case 7: strcpy(buf, "clamav");
367                         return;
368         }
369 }
370
371
372 /*
373  * Internet mail configuration
374  */
375 void do_internet_configuration(CtdlIPC *ipc)
376 {
377         char buf[256];
378         char *resp = NULL;
379         int num_recs = 0;
380         char **recs = NULL;
381         char ch;
382         int i, j;
383         int quitting = 0;
384         int modified = 0;
385         int r;
386         
387         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
388         if (r / 100 == 1) {
389                 while (!IsEmptyStr(resp)) {
390                         extract_token(buf, resp, 0, '\n', sizeof buf);
391                         remove_token(resp, 0, '\n');
392
393                         // VILE SLEAZY HACK: replace obsolete "directory" domains with "localhost"
394                         char *d = strstr(buf, "|directory");
395                         if (d != NULL) {
396                                 strcpy(d, "|localhost");
397                         }
398
399                         ++num_recs;
400                         if (num_recs == 1) recs = malloc(sizeof(char *));
401                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
402                         recs[num_recs-1] = malloc(strlen(buf) + 1);
403                         strcpy(recs[num_recs-1], buf);
404                 }
405         }
406         if (resp) free(resp);
407
408         do {
409                 scr_printf("\n");
410                 color(BRIGHT_WHITE);
411                 scr_printf("###                    Host or domain                     Record type      \n");
412                 color(DIM_WHITE);
413                 scr_printf("--- -------------------------------------------------- --------------------\n");
414                 for (i=0; i<num_recs; ++i) {
415                 color(DIM_WHITE);
416                 scr_printf("%3d ", i+1);
417                 extract_token(buf, recs[i], 0, '|', sizeof buf);
418                 color(BRIGHT_CYAN);
419                 scr_printf("%-50s ", buf);
420                 extract_token(buf, recs[i], 1, '|', sizeof buf);
421                 color(BRIGHT_MAGENTA);
422                 scr_printf("%-20s\n", buf);
423                 color(DIM_WHITE);
424                 }
425
426                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
427                 switch(ch) {
428                         case 'a':
429                                 newprompt("Enter host name: ", buf, 50);
430                                 striplt(buf);
431                                 if (!IsEmptyStr(buf)) {
432                                         ++num_recs;
433                                         if (num_recs == 1) {
434                                                 recs = malloc(sizeof(char *));
435                                         }
436                                         else {
437                                                 recs = realloc(recs, (sizeof(char *)) * num_recs);
438                                         }
439                                         strcat(buf, "|");
440                                         get_inet_rec_type(ipc, &buf[strlen(buf)]);
441                                         recs[num_recs-1] = strdup(buf);
442                                 }
443                                 modified = 1;
444                                 break;
445                         case 'd':
446                                 i = intprompt("Delete which one", 1, 1, num_recs) - 1;
447                                 free(recs[i]);
448                                 --num_recs;
449                                 for (j=i; j<num_recs; ++j) {
450                                         recs[j] = recs[j+1];
451                                 }
452                                 modified = 1;
453                                 break;
454                         case 's':
455                                 r = 1;
456                                 for (i = 0; i < num_recs; i++)
457                                         r += 1 + strlen(recs[i]);
458                                 resp = (char *)calloc(1, r);
459                                 if (!resp) {
460                                         scr_printf("Can't save config - out of memory!\n");
461                                         logoff(ipc, 1);
462                                 }
463                                 if (num_recs) for (i = 0; i < num_recs; i++) {
464                                         strcat(resp, recs[i]);
465                                         strcat(resp, "\n");
466                                 }
467                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
468                                 if (r / 100 != 4) {
469                                         scr_printf("%s\n", buf);
470                                 } else {
471                                         scr_printf("Wrote %d records.\n", num_recs);
472                                         modified = 0;
473                                 }
474                 free(resp);
475                                 break;
476                         case 'q':
477                                 quitting = !modified || boolprompt(
478                                         "Quit without saving", 0);
479                                 break;
480                         default:
481                                 break;
482                 }
483         } while (!quitting);
484
485         if (recs != NULL) {
486                 for (i=0; i<num_recs; ++i) free(recs[i]);
487                 free(recs);
488         }
489 }
490
491
492
493 /*
494  * Edit network configuration for room sharing, mailing lists, etc.
495  */
496 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
497 {
498         char filename[PATH_MAX];
499         char changefile[PATH_MAX];
500         int e_ex_code;
501         pid_t editor_pid;
502         int cksum;
503         int b, i, tokens;
504         char buf[1024];
505         char instr[1024];
506         char addr[1024];
507         FILE *tempfp;
508         FILE *changefp;
509         char *listing = NULL;
510         int r;
511
512         if (IsEmptyStr(editor_path)) {
513                 scr_printf("You must have an external editor configured in order to use this function.\n");
514                 return;
515         }
516
517         CtdlMakeTempFileName(filename, sizeof filename);
518         CtdlMakeTempFileName(changefile, sizeof changefile);
519
520         tempfp = fopen(filename, "w");
521         if (tempfp == NULL) {
522                 scr_printf("Cannot open %s: %s\n", filename, strerror(errno));
523                 return;
524         }
525
526         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
527         fprintf(tempfp, "# %s\n", comment);
528         fprintf(tempfp, "# Specify one per line.\n"
529                         "\n\n");
530
531         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
532         if (r / 100 == 1) {
533                 while(listing && !IsEmptyStr(listing)) {
534                         extract_token(buf, listing, 0, '\n', sizeof buf);
535                         remove_token(listing, 0, '\n');
536                         extract_token(instr, buf, 0, '|', sizeof instr);
537                         if (!strcasecmp(instr, entrytype)) {
538                                 tokens = num_tokens(buf, '|');
539                                 for (i=1; i<tokens; ++i) {
540                                         extract_token(addr, buf, i, '|', sizeof addr);
541                                         fprintf(tempfp, "%s", addr);
542                                         if (i < (tokens-1)) {
543                                                 fprintf(tempfp, "|");
544                                         }
545                                 }
546                                 fprintf(tempfp, "\n");
547                         }
548                 }
549         }
550         if (listing) {
551                 free(listing);
552                 listing = NULL;
553         }
554         fclose(tempfp);
555
556         e_ex_code = 1;  /* start with a failed exit code */
557         stty_ctdl(SB_RESTORE);
558         editor_pid = fork();
559         cksum = file_checksum(filename);
560         if (editor_pid == 0) {
561                 chmod(filename, 0600);
562                 putenv("WINDOW_TITLE=Network configuration");
563                 execlp(editor_path, editor_path, filename, NULL);
564                 exit(1);
565         }
566         if (editor_pid > 0) {
567                 do {
568                         e_ex_code = 0;
569                         b = ka_wait(&e_ex_code);
570                 } while ((b != editor_pid) && (b >= 0));
571         editor_pid = (-1);
572         stty_ctdl(0);
573         }
574
575         if (file_checksum(filename) == cksum) {
576                 scr_printf("*** No changes to save.\n");
577                 e_ex_code = 1;
578         }
579
580         if (e_ex_code == 0) {           /* Save changes */
581                 changefp = fopen(changefile, "w");
582
583                 /* Load all netconfig entries that are *not* of the type we are editing */
584                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
585                 if (r / 100 == 1) {
586                         while(listing && !IsEmptyStr(listing)) {
587                                 extract_token(buf, listing, 0, '\n', sizeof buf);
588                                 remove_token(listing, 0, '\n');
589                                 extract_token(instr, buf, 0, '|', sizeof instr);
590                                 if (strcasecmp(instr, entrytype)) {
591                                         fprintf(changefp, "%s\n", buf);
592                                 }
593                         }
594                 }
595                 if (listing) {
596                         free(listing);
597                         listing = NULL;
598                 }
599
600                 /* ...and merge that with the data we just edited */
601                 tempfp = fopen(filename, "r");
602                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
603                         for (i=0; i<strlen(buf); ++i) {
604                                 if (buf[i] == '#') buf[i] = 0;
605                         }
606                         striplt(buf);
607                         if (!IsEmptyStr(buf)) {
608                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
609                         }
610                 }
611                 fclose(tempfp);
612                 fclose(changefp);
613
614                 /* now write it to the server... */
615                 changefp = fopen(changefile, "r");
616                 if (changefp != NULL) {
617                         listing = load_message_from_file(changefp);
618                         if (listing) {
619                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
620                                 free(listing);
621                                 listing = NULL;
622                         }
623                         fclose(changefp);
624                 }
625         }
626
627         unlink(filename);               /* Delete the temporary files */
628         unlink(changefile);
629 }
630
631
632 /*
633  * POP3 aggregation client configuration
634  */
635 void do_pop3client_configuration(CtdlIPC *ipc)
636 {
637         char buf[SIZ];
638         int num_recs = 0;
639         char **recs = NULL;
640         char ch;
641         int i, j;
642         int quitting = 0;
643         int modified = 0;
644         char *listing = NULL;
645         char *other_listing = NULL;
646         int r;
647         char instr[SIZ];
648
649         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
650         if (r / 100 == 1) {
651                 while(listing && !IsEmptyStr(listing)) {
652                         extract_token(buf, listing, 0, '\n', sizeof buf);
653                         remove_token(listing, 0, '\n');
654                         extract_token(instr, buf, 0, '|', sizeof instr);
655                         if (!strcasecmp(instr, "pop3client")) {
656
657                                 ++num_recs;
658                                 if (num_recs == 1) recs = malloc(sizeof(char *));
659                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
660                                 recs[num_recs-1] = malloc(SIZ);
661                                 strcpy(recs[num_recs-1], buf);
662
663                         }
664                 }
665         }
666         if (listing) {
667                 free(listing);
668                 listing = NULL;
669         }
670
671         do {
672                 scr_printf("\n");
673                 color(BRIGHT_WHITE);
674                 scr_printf(     "### "
675                         "      Remote POP3 host       "
676                         "         User name           "
677                         "Keep on server? "
678                         "\n");
679                 color(DIM_WHITE);
680                 scr_printf(     "--- "
681                         "---------------------------- "
682                         "---------------------------- "
683                         "--------------- "
684                         "\n");
685                 for (i=0; i<num_recs; ++i) {
686                 color(DIM_WHITE);
687                 scr_printf("%3d ", i+1);
688
689                 extract_token(buf, recs[i], 1, '|', sizeof buf);
690                 color(BRIGHT_CYAN);
691                 scr_printf("%-28s ", buf);
692
693                 extract_token(buf, recs[i], 2, '|', sizeof buf);
694                 color(BRIGHT_MAGENTA);
695                 scr_printf("%-28s ", buf);
696
697                 color(BRIGHT_CYAN);
698                 scr_printf("%-15s\n", (extract_int(recs[i], 4) ? "Yes" : "No") );
699                 color(DIM_WHITE);
700                 }
701
702                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
703                 switch(ch) {
704                         case 'a':
705                                 ++num_recs;
706                                 if (num_recs == 1) {
707                                         recs = malloc(sizeof(char *));
708                                 }
709                                 else {
710                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
711                                 }
712                                 strcpy(buf, "pop3client|");
713                                 newprompt("Enter host name: ", &buf[strlen(buf)], 28);
714                                 strcat(buf, "|");
715                                 newprompt("Enter user name: ", &buf[strlen(buf)], 28);
716                                 strcat(buf, "|");
717                                 newprompt("Enter password : ", &buf[strlen(buf)], 16);
718                                 strcat(buf, "|");
719                                 scr_printf("Keep messages on server instead of deleting them? ");
720                                 sprintf(&buf[strlen(buf)], "%d", yesno());
721                                 strcat(buf, "|");
722                                 recs[num_recs-1] = strdup(buf);
723                                 modified = 1;
724                                 break;
725                         case 'd':
726                                 i = intprompt("Delete which one",
727                                         1, 1, num_recs) - 1;
728                                 free(recs[i]);
729                                 --num_recs;
730                                 for (j=i; j<num_recs; ++j)
731                                         recs[j] = recs[j+1];
732                                 modified = 1;
733                                 break;
734                         case 's':
735                                 r = 1;
736                                 for (i = 0; i < num_recs; ++i) {
737                                         r += 1 + strlen(recs[i]);
738                                 }
739                                 listing = (char*) calloc(1, r);
740                                 if (!listing) {
741                                         scr_printf("Can't save config - out of memory!\n");
742                                         logoff(ipc, 1);
743                                 }
744                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
745                                         strcat(listing, recs[i]);
746                                         strcat(listing, "\n");
747                                 }
748
749                                 /* Retrieve all the *other* records for merging */
750                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
751                                 if (r / 100 == 1) {
752                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
753                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
754                                                 if (strncasecmp(buf, "pop3client|", 11)) {
755                                                         listing = realloc(listing, strlen(listing) +
756                                                                 strlen(buf) + 10);
757                                                         strcat(listing, buf);
758                                                         strcat(listing, "\n");
759                                                 }
760                                         }
761                                 }
762                                 free(other_listing);
763                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
764                                 free(listing);
765                                 listing = NULL;
766
767                                 if (r / 100 != 4) {
768                                         scr_printf("%s\n", buf);
769                                 } else {
770                                         scr_printf("Wrote %d records.\n", num_recs);
771                                         modified = 0;
772                                 }
773                                 quitting = 1;
774                                 break;
775                         case 'q':
776                                 quitting = !modified || boolprompt(
777                                         "Quit without saving", 0);
778                                 break;
779                         default:
780                                 break;
781                 }
782         } while (!quitting);
783
784         if (recs != NULL) {
785                 for (i=0; i<num_recs; ++i) free(recs[i]);
786                 free(recs);
787         }
788 }
789
790
791
792
793
794
795 /*
796  * RSS feed retrieval client configuration
797  */
798 void do_rssclient_configuration(CtdlIPC *ipc)
799 {
800         char buf[SIZ];
801         int num_recs = 0;
802         char **recs = NULL;
803         char ch;
804         int i, j;
805         int quitting = 0;
806         int modified = 0;
807         char *listing = NULL;
808         char *other_listing = NULL;
809         int r;
810         char instr[SIZ];
811
812         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
813         if (r / 100 == 1) {
814                 while(listing && !IsEmptyStr(listing)) {
815                         extract_token(buf, listing, 0, '\n', sizeof buf);
816                         remove_token(listing, 0, '\n');
817                         extract_token(instr, buf, 0, '|', sizeof instr);
818                         if (!strcasecmp(instr, "rssclient")) {
819
820                                 ++num_recs;
821                                 if (num_recs == 1) recs = malloc(sizeof(char *));
822                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
823                                 recs[num_recs-1] = malloc(SIZ);
824                                 strcpy(recs[num_recs-1], buf);
825
826                         }
827                 }
828         }
829         if (listing) {
830                 free(listing);
831                 listing = NULL;
832         }
833
834         do {
835                 scr_printf("\n");
836                 color(BRIGHT_WHITE);
837                 scr_printf("### Feed URL\n");
838                 color(DIM_WHITE);
839                 scr_printf("--- "
840                         "---------------------------------------------------------------------------"
841                         "\n");
842                 
843                 for (i=0; i<num_recs; ++i) {
844                 color(DIM_WHITE);
845                 scr_printf("%3d ", i+1);
846
847                 extract_token(buf, recs[i], 1, '|', sizeof buf);
848                 color(BRIGHT_CYAN);
849                 scr_printf("%-75s\n", buf);
850
851                 color(DIM_WHITE);
852                 }
853
854                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
855                 switch(ch) {
856                         case 'a':
857                                 ++num_recs;
858                                 if (num_recs == 1) {
859                                         recs = malloc(sizeof(char *));
860                                 }
861                                 else {
862                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
863                                 }
864                                 strcpy(buf, "rssclient|");
865                                 newprompt("Enter feed URL: ", &buf[strlen(buf)], 75);
866                                 strcat(buf, "|");
867                                 recs[num_recs-1] = strdup(buf);
868                                 modified = 1;
869                                 break;
870                         case 'd':
871                                 i = intprompt("Delete which one", 1, 1, num_recs) - 1;
872                                 free(recs[i]);
873                                 --num_recs;
874                                 for (j=i; j<num_recs; ++j)
875                                         recs[j] = recs[j+1];
876                                 modified = 1;
877                                 break;
878                         case 's':
879                                 r = 1;
880                                 for (i = 0; i < num_recs; ++i) {
881                                         r += 1 + strlen(recs[i]);
882                                 }
883                                 listing = (char*) calloc(1, r);
884                                 if (!listing) {
885                                         scr_printf("Can't save config - out of memory!\n");
886                                         logoff(ipc, 1);
887                                 }
888                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
889                                         strcat(listing, recs[i]);
890                                         strcat(listing, "\n");
891                                 }
892
893                                 /* Retrieve all the *other* records for merging */
894                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
895                                 if (r / 100 == 1) {
896                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
897                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
898                                                 if (strncasecmp(buf, "rssclient|", 10)) {
899                                                         listing = realloc(listing, strlen(listing) +
900                                                                 strlen(buf) + 10);
901                                                         strcat(listing, buf);
902                                                         strcat(listing, "\n");
903                                                 }
904                                         }
905                                 }
906                                 free(other_listing);
907                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
908                                 free(listing);
909                                 listing = NULL;
910
911                                 if (r / 100 != 4) {
912                                         scr_printf("%s\n", buf);
913                                 } else {
914                                         scr_printf("Wrote %d records.\n", num_recs);
915                                         modified = 0;
916                                 }
917                                 quitting = 1;
918                                 break;
919                         case 'q':
920                                 quitting = !modified || boolprompt(
921                                         "Quit without saving", 0);
922                                 break;
923                         default:
924                                 break;
925                 }
926         } while (!quitting);
927
928         if (recs != NULL) {
929                 for (i=0; i<num_recs; ++i) free(recs[i]);
930                 free(recs);
931         }
932 }
933
934