]> Pileus Git - ~andy/fetchmail/blob - conf.c
Fix properties editing.
[~andy/fetchmail] / conf.c
1 /*
2  * conf.c -- dump fetchmail configuration as Python dictionary initializer
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8 #include "tunable.h"
9
10 #include <stdio.h>
11 #include <ctype.h>
12 #if defined(STDC_HEADERS)
13 #include <stdlib.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21
22 #include "fetchmail.h"
23
24 /* Python prettyprinting functions */
25
26 static int indent_level;
27
28 static void indent(char ic)
29 /* indent current line */
30 {
31     int i;
32
33     if (ic == ')' || ic == ']' || ic == '}')
34         indent_level--;
35
36     /*
37      * The guard here is a kluge.  It depends on the fact that in the
38      * particular structure we're dumping, opening [s are always
39      * initializers for dictionary members and thus will be preceded
40      * by a member name.
41      */
42     if (ic != '[')
43     {
44         for (i = 0; i < indent_level / 2; i++)
45             putc('\t', stdout);
46         if (indent_level % 2)
47             fputs("    ", stdout);
48     }
49
50     if (ic)
51     {
52         putc(ic, stdout);
53         putc('\n', stdout);
54     }
55
56     if (ic == '(' || ic == '[' || ic == '{')
57         indent_level++;
58 }
59
60 static void stringdump(const char *name, const char *member)
61 /* dump a string member with current indent */
62 {
63     indent('\0');
64     fprintf(stdout, "\"%s\":", name);
65     if (member)
66         fprintf(stdout, "\"%s\"", visbuf(member));
67     else
68         fputs("None", stdout);
69     fputs(",\n", stdout);
70 }
71
72 static void numdump(const char *name, const int num)
73 /* dump a numeric quantity at current indent */
74 {
75     indent('\0');
76     fprintf(stdout, "'%s':%d,\n", name, num);
77 }
78
79 static void booldump(const char *name, const int onoff)
80 /* dump a boolean quantity at current indent */
81 {
82     indent('\0');
83     if (onoff)
84         fprintf(stdout, "'%s':TRUE,\n", name);
85     else
86         fprintf(stdout, "'%s':FALSE,\n", name);
87 }
88
89 static void listdump(const char *name, struct idlist *list)
90 /* dump a string list member with current indent */
91 {
92     indent('\0');
93     fprintf(stdout, "\"%s\":", name);
94
95     if (!list)
96         fputs("None,\n", stdout);
97     else
98     {
99         struct idlist *idp;
100
101         fputs("[", stdout);
102         for (idp = list; idp; idp = idp->next)
103             if (idp->id)
104             {
105                 fprintf(stdout, "\"%s\"", visbuf(idp->id));
106                 if (idp->next)
107                     fputs(", ", stdout);
108             }
109         fputs("],\n", stdout);
110     }
111 }
112
113 /*
114  * Note: this function dumps the entire configuration,
115  * after merging of the defaults record (if any).  It
116  * is intended to produce output parseable by a configuration
117  * front end, not anything especially comfortable for humans.
118  */
119
120 void dump_config(struct runctl *runp, struct query *querylist)
121 /* dump the in-core configuration in recompilable form */
122 {
123     struct query *ctl;
124     struct idlist *idp;
125
126     indent_level = 0;
127
128     fputs("from Tkinter import TRUE, FALSE\n\n", stdout);
129
130     /*
131      * We need this in order to know whether `interface' and `monitor'
132      * are valid options or not.
133      */
134 #ifdef linux
135     fputs("os_type = 'linux'\n", stdout);
136 #else
137     fputs("os_type = 'generic'\n", stdout);
138 #endif
139
140     /* 
141      * This should be approximately in sync with the -V option dumping 
142      * in fetchmail.c.
143      */
144     printf("feature_options = (");
145 #ifdef POP2_ENABLE
146     printf("'pop2',");
147 #endif /* POP2_ENABLE */
148 #ifdef POP3_ENABLE
149     printf("'pop3',");
150 #endif /* POP3_ENABLE */
151 #ifdef IMAP_ENABLE
152     printf("'imap',");
153 #endif /* IMAP_ENABLE */
154 #ifdef GSSAPI
155     printf("'imap-gss',");
156 #endif /* GSSAPI */
157 #if defined(IMAP4) && defined(KERBEROS_V4)
158     printf("'imap-k4',");
159 #endif /* defined(IMAP4) && defined(KERBEROS_V4) */
160 #ifdef RPA_ENABLE
161     printf("'rpa',");
162 #endif /* RPA_ENABLE */
163 #ifdef SDPS_ENABLE
164     printf("'sdps',");
165 #endif /* SDPS_ENABLE */
166 #ifdef ETRN_ENABLE
167     printf("'etrn',");
168 #endif /* ETRN_ENABLE */
169 #if OPIE
170     printf("'opie',");
171 #endif /* OPIE */
172 #if INET6
173     printf("'inet6',");
174 #endif /* INET6 */
175 #if NET_SECURITY
176     printf("'netsec',");
177 #endif /* NET_SECURITY */
178     printf(")\n");
179
180     fputs("# Start of configuration initializer\n", stdout);
181     fputs("fetchmailrc = ", stdout);
182     indent('{');
183
184     numdump("poll_interval", runp->poll_interval);
185     booldump("syslog", runp->use_syslog);
186     stringdump("logfile", runp->logfile);
187     stringdump("idfile", runp->idfile);
188     stringdump("postmaster", runp->postmaster);
189     booldump("invisible", runp->invisible);
190
191     if (!querylist)
192     {
193         fputs("    'servers': []\n", stdout);
194         goto alldone;
195     }
196
197     indent(0);
198     fputs("# List of server entries begins here\n", stdout);
199     indent(0);
200     fputs("'servers': ", stdout);
201     indent('[');
202
203     for (ctl = querylist; ctl; ctl = ctl->next)
204     {
205         /*
206          * First, the server stuff.
207          */
208         if (!ctl->server.lead_server)
209         {
210             flag        using_kpop;
211
212             /*
213              * Every time we see a leading server entry after the first one,
214              * it implicitly ends the both (a) the list of user structures
215              * associated with the previous entry, and (b) that previous entry.
216              */
217             if (ctl > querylist)
218             {
219                 indent(']');
220                 indent('}');
221                 indent('\0'); 
222                 putc(',', stdout);
223                 putc('\n', stdout);
224             }
225
226             indent(0);
227             fprintf(stdout,"# Entry for site `%s' begins:\n",ctl->server.pollname);
228             indent('{');
229
230             using_kpop =
231                 (ctl->server.protocol == P_POP3 &&
232                  ctl->server.port == KPOP_PORT &&
233                  ctl->server.preauthenticate == A_KERBEROS_V4);
234
235             stringdump("pollname", ctl->server.pollname); 
236             booldump("active", !ctl->server.skip); 
237             stringdump("via", ctl->server.via); 
238             stringdump("protocol", 
239                        using_kpop ? "KPOP" : showproto(ctl->server.protocol));
240             numdump("port",  ctl->server.port);
241             numdump("timeout",  ctl->server.timeout);
242             numdump("interval", ctl->server.interval);
243
244             if (ctl->server.envelope == STRING_DISABLED)
245                 stringdump("envelope", NULL); 
246             else if (ctl->server.envelope == NULL)
247                 stringdump("envelope", "Received");             
248             else
249                 stringdump("envelope", ctl->server.envelope); 
250             numdump("envskip", ctl->server.envskip);
251             stringdump("qvirtual", ctl->server.qvirtual);
252  
253             if (ctl->server.preauthenticate == A_KERBEROS_V4)
254                 stringdump("auth", "kerberos_v4");
255             else if (ctl->server.preauthenticate == A_KERBEROS_V5)
256                 stringdump("auth", "kerberos_v5");
257             else
258                 stringdump("auth", "password");
259
260 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
261             booldump("dns", ctl->server.dns);
262 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
263             booldump("uidl", ctl->server.uidl);
264
265             listdump("aka", ctl->server.akalist);
266             listdump("localdomains", ctl->server.localdomains);
267
268 #ifdef linux
269             stringdump("interface", ctl->server.interface);
270             stringdump("monitor", ctl->server.monitor);
271 #endif /* linux */
272
273             indent(0);
274             fputs("'users': ", stdout);
275             indent('[');
276         }
277
278         indent('{');
279
280         stringdump("remote", ctl->remotename);
281         stringdump("password", ctl->password);
282
283         indent('\0');
284         fprintf(stdout, "'localnames':[");
285         for (idp = ctl->localnames; idp; idp = idp->next)
286         {
287             char        namebuf[USERNAMELEN + 1];
288
289             strncpy(namebuf, visbuf(idp->id), USERNAMELEN);
290             namebuf[USERNAMELEN] = '\0';
291             if (idp->val.id2)
292                 fprintf(stdout, "(\"%s\", %s)", namebuf, visbuf(idp->val.id2));
293             else
294                 fprintf(stdout, "\"%s\"", namebuf);
295             if (idp->next)
296                 fputs(", ", stdout);
297         }
298         if (ctl->wildcard)
299             fputs(", '*'", stdout);
300         fputs("],\n", stdout);
301
302         booldump("fetchall", ctl->fetchall);
303         booldump("keep", ctl->keep);
304         booldump("flush", ctl->flush);
305         booldump("rewrite", ctl->rewrite);
306         booldump("stripcr", ctl->stripcr); 
307         booldump("forcecr", ctl->forcecr);
308         booldump("pass8bits", ctl->pass8bits);
309         booldump("dropstatus", ctl->dropstatus);
310         booldump("mimedecode", ctl->mimedecode);
311
312         stringdump("mda", ctl->mda);
313         stringdump("bsmtp", ctl->bsmtp);
314 #ifdef INET6
315         stringdump("netsec", ctl->netsec);
316 #endif /* INET6 */
317         stringdump("preconnect", ctl->preconnect);
318         stringdump("postconnect", ctl->postconnect);
319         numdump("limit", ctl->limit);
320         numdump("warnings", ctl->warnings);
321         numdump("fetchlimit", ctl->fetchlimit);
322         numdump("batchlimit", ctl->batchlimit);
323         numdump("expunge", ctl->expunge);
324         stringdump("properties", ctl->properties);
325         listdump("smtphunt", ctl->smtphunt);
326         stringdump("smtpaddress", ctl->smtpaddress);
327
328         indent('\0');
329         fprintf(stdout, "'antispam':'");
330         if (!ctl->antispam)
331             fputs("'\n", stdout);
332         else
333         {
334             for (idp = ctl->antispam; idp; idp = idp->next)
335             {
336                 fprintf(stdout, "%d", idp->val.status.num);
337                 if (idp->next)
338                     fputs(" ", stdout);
339             }
340             fputs("',\n", stdout);
341         }
342         listdump("mailboxes", ctl->mailboxes);
343
344         indent('}');
345         indent('\0'); 
346         fputc(',', stdout);
347     }
348
349     /* end last span of user entries and last server entry */
350     indent(']');
351     indent('}');
352
353     /* end array of servers */
354     indent(']');
355
356  alldone:
357     /* end top-level dictionary */
358     indent('}');
359     fputs("# End of initializer\n", stdout);
360 }
361
362 /* conf.c ends here */