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