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