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