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