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