stringbuf.c: random idle style cleanup.
[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, or disclosure is subject to the GNU General Public License version 3.
6
7 #include "textclient.h"
8
9 extern char temp[];
10 extern char tempdir[];
11 extern char *axdefs[8];
12 extern long highest_msg_read;
13 extern long maxmsgnum;
14 extern unsigned room_flags;
15 extern int screenwidth;
16 char editor_path[PATH_MAX];
17
18
19 // General system configuration command
20 void do_system_configuration(CtdlIPC * ipc) {
21         char buf[256];
22         char sc[NUM_CONFIGS][256];
23         char *resp = NULL;
24         struct ExpirePolicy *site_expirepolicy = NULL;
25         struct ExpirePolicy *mbx_expirepolicy = NULL;
26         int a;
27         int logpages = 0;
28         int r;                  // IPC response code
29         int server_configs = 0;
30
31         // Clear out the config buffers
32         memset(&sc[0][0], 0, sizeof(sc));
33
34         // Fetch the current config
35         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
36         if (r / 100 == 1) {
37                 server_configs = num_tokens(resp, '\n');
38                 for (a = 0; a < server_configs; ++a) {
39                         if (a < NUM_CONFIGS) {
40                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
41                         }
42                 }
43         }
44         if (resp) {
45                 free(resp);
46         }
47         resp = NULL;
48
49         // Fetch the expire policy (this will silently fail on old servers, resulting in "default" policy)
50         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
51         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
52
53         // Identification parameters
54         strprompt("Node name", &sc[0][0], 15);
55         strprompt("Fully qualified domain name", &sc[1][0], 63);
56         strprompt("Human readable node name", &sc[2][0], 20);
57         strprompt("Telephone number", &sc[3][0], 15);
58         strprompt("Geographic location of this system", &sc[12][0], 31);
59         strprompt("Name of system administrator", &sc[13][0], 25);
60         strprompt("Paginator prompt", &sc[10][0], 79);
61
62         // Security parameters
63         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt("Require registration for new users", atoi(&sc[7][0]))));
64         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt("Disable self-service user account creation", atoi(&sc[29][0]))));
65         strprompt("Initial access level for new users", &sc[6][0], 1);
66         strprompt("Access level required to create rooms", &sc[19][0], 1);
67         snprintf(sc[67], sizeof sc[67], "%d", (boolprompt("Allow anonymous guest logins", atoi(&sc[67][0]))));
68         snprintf(sc[4], sizeof sc[4], "%d",
69                  (boolprompt("Automatically give room admin privs to a user who creates a private room", atoi(&sc[4][0]))));
70         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt("Automatically move problem user messages to twit room", atoi(&sc[8][0]))));
71         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
72         snprintf(sc[11], sizeof sc[11], "%d",
73                  (boolprompt("Restrict Internet mail to only those with that privilege", atoi(&sc[11][0]))));
74         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt("Allow admins to Zap (forget) rooms", atoi(&sc[26][0]))));
75
76         if (!IsEmptyStr(&sc[18][0])) {
77                 logpages = 1;
78         }
79         else {
80                 logpages = 0;
81         }
82         logpages = boolprompt("Log all instant messages", logpages);
83         if (logpages) {
84                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
85         }
86         else {
87                 sc[18][0] = 0;
88         }
89
90         // Server tuning
91         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
92         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
93         strprompt("Maximum message length", &sc[20][0], 20);
94         strprompt("Minimum number of worker threads", &sc[21][0], 3);
95         strprompt("Maximum number of worker threads", &sc[22][0], 3);
96         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt("Automatically delete committed database logs", atoi(&sc[43][0]))));
97         strprompt("Server IP address (* for 'any')", &sc[37][0], 15);
98         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
99         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
100         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
101         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
102         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
103         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
104         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
105         snprintf(sc[72], sizeof sc[72], "%d", (boolprompt("Advertise STARTTLS on the SMTP port", atoi(&sc[72][0]))));
106         strprompt("NNTP server port (-1 to disable)", &sc[70][0], 5);
107         strprompt("NNTPS server port (-1 to disable)", &sc[71][0], 5);
108         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
109         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
110         strprompt("XMPP (Jabber) client to server port (-1 to disable)", &sc[62][0], 5);
111         // strprompt("XMPP (Jabber) server to server port (-1 to disable)", &sc[63][0], 5);   This is just a placeholder.
112
113         // This logic flips the question around, because it's one of those situations where 0=yes and 1=no
114         a = atoi(sc[25]);
115         a = (a ? 0 : 1);
116         a = boolprompt("Correct forged From: lines during authenticated SMTP", a);
117         a = (a ? 0 : 1);
118         snprintf(sc[25], sizeof sc[25], "%d", a);
119
120         snprintf(sc[66], sizeof sc[66], "%d", (boolprompt("Flag messages as spam instead of rejecting", atoi(&sc[66][0]))));
121
122         // This logic flips the question around, because it's one of those situations where 0=yes and 1=no
123         a = atoi(sc[61]);
124         a = (a ? 0 : 1);
125         a = boolprompt("Force IMAP posts in public rooms to be from the user who submitted them", a);
126         a = (a ? 0 : 1);
127         snprintf(sc[61], sizeof sc[61], "%d", a);
128
129         snprintf(sc[45], sizeof sc[45], "%d",
130                  (boolprompt("Allow unauthenticated SMTP clients to spoof my domains", atoi(&sc[45][0]))));
131         snprintf(sc[57], sizeof sc[57], "%d",
132                  (boolprompt("Perform RBL checks at greeting instead of after RCPT", atoi(&sc[57][0]))));
133
134         // LDAP settings
135         if (ipc->ServInfo.supports_ldap) {
136                 a = strlen(&sc[32][0]);
137                 a = (a ? 1 : 0);        // Set only to 1 or 0
138                 a = boolprompt("Do you want to configure LDAP authentication?", a);
139                 if (a) {
140                         strprompt("Host name of LDAP server", &sc[32][0], 127);
141                         strprompt("Port number of LDAP service", &sc[33][0], 5);
142                         strprompt("Base DN", &sc[34][0], 255);
143                         strprompt("Bind DN (or blank for anonymous bind)", &sc[35][0], 255);
144                         strprompt("Password for bind DN (or blank for anonymous bind)", &sc[36][0], 255);
145                 }
146                 else {
147                         strcpy(&sc[32][0], "");
148                 }
149         }
150
151         // Expiry settings
152         strprompt("Default user purge time (days)", &sc[16][0], 5);
153         strprompt("Default room purge time (days)", &sc[17][0], 5);
154
155         // Angels and demons dancing in my head...
156         do {
157                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
158                 strprompt("System default message expire policy (? for list)", buf, 1);
159                 if (buf[0] == '?') {
160                         scr_printf("\n"
161                                    "1. Never automatically expire messages\n"
162                                    "2. Expire by message count\n" "3. Expire by message age\n");
163                 }
164         } while ((buf[0] < '1') || (buf[0] > '3'));
165         site_expirepolicy->expire_mode = buf[0] - '0';
166
167         // ...lunatics and monsters underneath my bed
168         if (site_expirepolicy->expire_mode == 2) {
169                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
170                 strprompt("Keep how many messages online?", buf, 10);
171                 site_expirepolicy->expire_value = atol(buf);
172         }
173         if (site_expirepolicy->expire_mode == 3) {
174                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
175                 strprompt("Keep messages for how many days?", buf, 10);
176                 site_expirepolicy->expire_value = atol(buf);
177         }
178
179         // Media messiahs preying on my fears...
180         do {
181                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
182                 strprompt("Mailbox default message expire policy (? for list)", buf, 1);
183                 if (buf[0] == '?') {
184                         scr_printf("\n"
185                                    "0. Go with the system default\n"
186                                    "1. Never automatically expire messages\n"
187                                    "2. Expire by message count\n" "3. Expire by message age\n");
188                 }
189         } while ((buf[0] < '0') || (buf[0] > '3'));
190         mbx_expirepolicy->expire_mode = buf[0] - '0';
191
192         // ...Pop culture prophets playing in my ears
193         if (mbx_expirepolicy->expire_mode == 2) {
194                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
195                 strprompt("Keep how many messages online?", buf, 10);
196                 mbx_expirepolicy->expire_value = atol(buf);
197         }
198         if (mbx_expirepolicy->expire_mode == 3) {
199                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
200                 strprompt("Keep messages for how many days?", buf, 10);
201                 mbx_expirepolicy->expire_value = atol(buf);
202         }
203
204         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
205         strprompt("Default frequency to run POP3 collection (in seconds)", &sc[64][0], 5);
206         strprompt("Fastest frequency to run POP3 collection (in seconds)", &sc[65][0], 5);
207         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
208         snprintf(sc[42], sizeof sc[42], "%d",
209                  (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                         string_trim(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                         string_trim(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                         }
641                         else {
642                                 recs = realloc(recs, (sizeof(char *)) * num_recs);
643                         }
644                         strcpy(buf, "pop3client|");
645                         newprompt("Enter host name: ", &buf[strlen(buf)], 28);
646                         strcat(buf, "|");
647                         newprompt("Enter user name: ", &buf[strlen(buf)], 28);
648                         strcat(buf, "|");
649                         newprompt("Enter password : ", &buf[strlen(buf)], 16);
650                         strcat(buf, "|");
651                         scr_printf("Keep messages on server instead of deleting them? ");
652                         sprintf(&buf[strlen(buf)], "%d", yesno());
653                         strcat(buf, "|");
654                         recs[num_recs - 1] = strdup(buf);
655                         modified = 1;
656                         break;
657                 case 'd':
658                         i = intprompt("Delete which one", 1, 1, num_recs) - 1;
659                         free(recs[i]);
660                         --num_recs;
661                         for (j = i; j < num_recs; ++j)
662                                 recs[j] = recs[j + 1];
663                         modified = 1;
664                         break;
665                 case 's':
666                         r = 1;
667                         for (i = 0; i < num_recs; ++i) {
668                                 r += 1 + strlen(recs[i]);
669                         }
670                         listing = (char *) calloc(1, r);
671                         if (!listing) {
672                                 scr_printf("Can't save config - out of memory!\n");
673                                 logoff(ipc, 1);
674                         }
675                         if (num_recs)
676                                 for (i = 0; i < num_recs; ++i) {
677                                         strcat(listing, recs[i]);
678                                         strcat(listing, "\n");
679                                 }
680
681                         // Retrieve all the *other* records for merging
682                         r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
683                         if (r / 100 == 1) {
684                                 for (i = 0; i < num_tokens(other_listing, '\n'); ++i) {
685                                         extract_token(buf, other_listing, i, '\n', sizeof buf);
686                                         if (strncasecmp(buf, "pop3client|", 11)) {
687                                                 listing = realloc(listing, strlen(listing) + strlen(buf) + 10);
688                                                 strcat(listing, buf);
689                                                 strcat(listing, "\n");
690                                         }
691                                 }
692                         }
693                         free(other_listing);
694                         r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
695                         free(listing);
696                         listing = NULL;
697
698                         if (r / 100 != 4) {
699                                 scr_printf("%s\n", buf);
700                         }
701                         else {
702                                 scr_printf("Wrote %d records.\n", num_recs);
703                                 modified = 0;
704                         }
705                         quitting = 1;
706                         break;
707                 case 'q':
708                         quitting = !modified || boolprompt("Quit without saving", 0);
709                         break;
710                 default:
711                         break;
712                 }
713         } while (!quitting);
714
715         if (recs != NULL) {
716                 for (i = 0; i < num_recs; ++i) {
717                         free(recs[i]);
718                 }
719                 free(recs);
720         }
721 }
722
723
724 // RSS feed retrieval client configuration
725 void do_rssclient_configuration(CtdlIPC * ipc) {
726         char buf[SIZ];
727         int num_recs = 0;
728         char **recs = NULL;
729         char ch;
730         int i, j;
731         int quitting = 0;
732         int modified = 0;
733         char *listing = NULL;
734         char *other_listing = NULL;
735         int r;
736         char instr[SIZ];
737
738         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
739         if (r / 100 == 1) {
740                 while (listing && !IsEmptyStr(listing)) {
741                         extract_token(buf, listing, 0, '\n', sizeof buf);
742                         remove_token(listing, 0, '\n');
743                         extract_token(instr, buf, 0, '|', sizeof instr);
744                         if (!strcasecmp(instr, "rssclient")) {
745
746                                 ++num_recs;
747                                 if (num_recs == 1)
748                                         recs = malloc(sizeof(char *));
749                                 else
750                                         recs = realloc(recs, (sizeof(char *)) * num_recs);
751                                 recs[num_recs - 1] = malloc(SIZ);
752                                 strcpy(recs[num_recs - 1], buf);
753
754                         }
755                 }
756         }
757         if (listing) {
758                 free(listing);
759                 listing = NULL;
760         }
761
762         do {
763                 scr_printf("\n");
764                 color(BRIGHT_WHITE);
765                 scr_printf("### Feed URL\n");
766                 color(DIM_WHITE);
767                 scr_printf("--- " "---------------------------------------------------------------------------" "\n");
768
769                 for (i = 0; i < num_recs; ++i) {
770                         color(DIM_WHITE);
771                         scr_printf("%3d ", i + 1);
772
773                         extract_token(buf, recs[i], 1, '|', sizeof buf);
774                         color(BRIGHT_CYAN);
775                         scr_printf("%-75s\n", buf);
776
777                         color(DIM_WHITE);
778                 }
779
780                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
781                 switch (ch) {
782                 case 'a':
783                         ++num_recs;
784                         if (num_recs == 1) {
785                                 recs = malloc(sizeof(char *));
786                         }
787                         else {
788                                 recs = realloc(recs, (sizeof(char *)) * num_recs);
789                         }
790                         strcpy(buf, "rssclient|");
791                         newprompt("Enter feed URL: ", &buf[strlen(buf)], 75);
792                         strcat(buf, "|");
793                         recs[num_recs - 1] = strdup(buf);
794                         modified = 1;
795                         break;
796                 case 'd':
797                         i = intprompt("Delete which one", 1, 1, num_recs) - 1;
798                         free(recs[i]);
799                         --num_recs;
800                         for (j = i; j < num_recs; ++j)
801                                 recs[j] = recs[j + 1];
802                         modified = 1;
803                         break;
804                 case 's':
805                         r = 1;
806                         for (i = 0; i < num_recs; ++i) {
807                                 r += 1 + strlen(recs[i]);
808                         }
809                         listing = (char *) calloc(1, r);
810                         if (!listing) {
811                                 scr_printf("Can't save config - out of memory!\n");
812                                 logoff(ipc, 1);
813                         }
814                         if (num_recs)
815                                 for (i = 0; i < num_recs; ++i) {
816                                         strcat(listing, recs[i]);
817                                         strcat(listing, "\n");
818                                 }
819
820                         // Retrieve all the *other* records for merging
821                         r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
822                         if (r / 100 == 1) {
823                                 for (i = 0; i < num_tokens(other_listing, '\n'); ++i) {
824                                         extract_token(buf, other_listing, i, '\n', sizeof buf);
825                                         if (strncasecmp(buf, "rssclient|", 10)) {
826                                                 listing = realloc(listing, strlen(listing) + strlen(buf) + 10);
827                                                 strcat(listing, buf);
828                                                 strcat(listing, "\n");
829                                         }
830                                 }
831                         }
832                         free(other_listing);
833                         r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
834                         free(listing);
835                         listing = NULL;
836
837                         if (r / 100 != 4) {
838                                 scr_printf("%s\n", buf);
839                         }
840                         else {
841                                 scr_printf("Wrote %d records.\n", num_recs);
842                                 modified = 0;
843                         }
844                         quitting = 1;
845                         break;
846                 case 'q':
847                         quitting = !modified || boolprompt("Quit without saving", 0);
848                         break;
849                 default:
850                         break;
851                 }
852         } while (!quitting);
853
854         if (recs != NULL) {
855                 for (i = 0; i < num_recs; ++i)
856                         free(recs[i]);
857                 free(recs);
858         }
859 }