]> Pileus Git - ~andy/fetchmail/blob - conf.c
3f6e7af109c8e765022126b1bbf4530b9be113b4
[~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
61 static void stringdump(const char *name, const char *member)
62 /* dump a string member with current indent */
63 {
64     indent('\0');
65     fprintf(stdout, "\"%s\":", name);
66     if (member)
67         fprintf(stdout, "\"%s\"", visbuf(member));
68     else
69         fputs("None", stdout);
70     fputs(",\n", stdout);
71 }
72
73 static void numdump(const char *name, const int num)
74 /* dump a numeric quantity at current indent */
75 {
76     indent('\0');
77     fprintf(stdout, "'%s':%d,\n", name, NUM_VALUE_OUT(num));
78 }
79
80 static void booldump(const char *name, const int onoff)
81 /* dump a boolean quantity at current indent */
82 {
83     indent('\0');
84     if (onoff)
85         fprintf(stdout, "'%s':TRUE,\n", name);
86     else
87         fprintf(stdout, "'%s':FALSE,\n", name);
88 }
89
90 static void listdump(const char *name, struct idlist *list)
91 /* dump a string list member with current indent */
92 {
93     indent('\0');
94     fprintf(stdout, "\"%s\":", name);
95
96     if (!list)
97         fputs("None,\n", stdout);
98     else
99     {
100         struct idlist *idp;
101
102         fputs("[", stdout);
103         for (idp = list; idp; idp = idp->next)
104             if (idp->id)
105             {
106                 fprintf(stdout, "\"%s\"", visbuf(idp->id));
107                 if (idp->next)
108                     fputs(", ", stdout);
109             }
110         fputs("],\n", stdout);
111     }
112 }
113
114 /*
115  * Note: this function dumps the entire configuration,
116  * after merging of the defaults record (if any).  It
117  * is intended to produce output parseable by a configuration
118  * front end, not anything especially comfortable for humans.
119  */
120
121 void dump_config(struct runctl *runp, struct query *querylist)
122 /* dump the in-core configuration in recompilable form */
123 {
124     struct query *ctl;
125     struct idlist *idp;
126
127     indent_level = 0;
128
129     fputs("from Tkinter import TRUE, FALSE\n\n", stdout);
130
131     /*
132      * We need this in order to know whether `interface' and `monitor'
133      * are valid options or not.
134      */
135 #if defined(linux)
136     fputs("os_type = 'linux'\n", stdout);
137 #elif defined(__FreeBSD__)
138     fputs("os_type = 'freebsd'\n", stdout);
139 #else
140     fputs("os_type = 'generic'\n", stdout);
141 #endif
142
143     /* 
144      * This should be approximately in sync with the -V option dumping 
145      * in fetchmail.c.
146      */
147     printf("feature_options = (");
148 #ifdef POP2_ENABLE
149     printf("'pop2',");
150 #endif /* POP2_ENABLE */
151 #ifdef POP3_ENABLE
152     printf("'pop3',");
153 #endif /* POP3_ENABLE */
154 #ifdef IMAP_ENABLE
155     printf("'imap',");
156 #endif /* IMAP_ENABLE */
157 #ifdef GSSAPI
158     printf("'gssapi',");
159 #endif /* GSSAPI */
160 #if defined(KERBEROS_V4)
161     printf("'kerberos',");
162 #endif /* defined(IMAP4) */
163 #ifdef RPA_ENABLE
164     printf("'rpa',");
165 #endif /* RPA_ENABLE */
166 #ifdef SDPS_ENABLE
167     printf("'sdps',");
168 #endif /* SDPS_ENABLE */
169 #ifdef ETRN_ENABLE
170     printf("'etrn',");
171 #endif /* ETRN_ENABLE */
172 #ifdef ODMR_ENABLE
173     printf("'odmr',");
174 #endif /* ODMR_ENABLE */
175 #ifdef SSL_ENABLE
176     printf("'ssl',");
177 #endif /* SSL_ENABLE */
178 #if OPIE_ENABLE
179     printf("'opie',");
180 #endif /* OPIE_ENABLE */
181 #if INET6_ENABLE
182     printf("'inet6',");
183 #endif /* INET6_ENABLE */
184 #if NET_SECURITY
185     printf("'netsec',");
186 #endif /* NET_SECURITY */
187     printf(")\n");
188
189     fputs("# Start of configuration initializer\n", stdout);
190     fputs("fetchmailrc = ", stdout);
191     indent('{');
192
193     numdump("poll_interval", runp->poll_interval);
194     stringdump("logfile", runp->logfile);
195     stringdump("idfile", runp->idfile);
196     stringdump("postmaster", runp->postmaster);
197     booldump("bouncemail", runp->bouncemail);
198     booldump("spambounce", runp->spambounce);
199     stringdump("properties", runp->properties);
200     booldump("invisible", runp->invisible);
201     booldump("showdots", runp->showdots);
202     booldump("syslog", runp->use_syslog);
203
204     if (!querylist)
205     {
206         fputs("    'servers': []\n", stdout);
207         goto alldone;
208     }
209
210     indent(0);
211     fputs("# List of server entries begins here\n", stdout);
212     indent(0);
213     fputs("'servers': ", stdout);
214     indent('[');
215
216     for (ctl = querylist; ctl; ctl = ctl->next)
217     {
218         /*
219          * First, the server stuff.
220          */
221         if (!ctl->server.lead_server)
222         {
223             flag        using_kpop;
224
225             /*
226              * Every time we see a leading server entry after the first one,
227              * it implicitly ends the both (a) the list of user structures
228              * associated with the previous entry, and (b) that previous entry.
229              */
230             if (ctl > querylist)
231             {
232                 indent(']');
233                 indent('}');
234                 indent('\0'); 
235                 putc(',', stdout);
236                 putc('\n', stdout);
237             }
238
239             indent(0);
240             fprintf(stdout,"# Entry for site `%s' begins:\n",ctl->server.pollname);
241             indent('{');
242
243             using_kpop =
244                 (ctl->server.protocol == P_POP3 &&
245 #if !INET6_ENABLE
246                  ctl->server.port == KPOP_PORT &&
247 #else
248                  ctl->server.service && !strcmp(ctl->server.service, KPOP_PORT ) &&
249 #endif
250                  ctl->server.authenticate == A_KERBEROS_V4);
251
252             stringdump("pollname", ctl->server.pollname); 
253             booldump("active", !ctl->server.skip); 
254             stringdump("via", ctl->server.via); 
255             stringdump("protocol", 
256                        using_kpop ? "KPOP" : showproto(ctl->server.protocol));
257 #ifndef INET6_ENABLE
258             numdump("port",  ctl->server.port);
259 #else
260             stringdump("port",  ctl->server.service);
261 #endif
262             numdump("timeout",  ctl->server.timeout);
263             numdump("interval", ctl->server.interval);
264
265             if (ctl->server.envelope == STRING_DISABLED)
266                 stringdump("envelope", NULL); 
267             else if (ctl->server.envelope == NULL)
268                 stringdump("envelope", "Received");             
269             else
270                 stringdump("envelope", ctl->server.envelope); 
271             numdump("envskip", ctl->server.envskip);
272             stringdump("qvirtual", ctl->server.qvirtual);
273  
274             if (ctl->server.authenticate == A_ANY)
275                 stringdump("auth", "any");
276             else if (ctl->server.authenticate == A_PASSWORD)
277                 stringdump("auth", "password");
278             else if (ctl->server.authenticate == A_GSSAPI)
279                 stringdump("auth", "gssapi");
280             else if (ctl->server.authenticate == A_KERBEROS_V4)
281                 stringdump("auth", "kerberos_v4");
282             else if (ctl->server.authenticate == A_KERBEROS_V5)
283                 stringdump("auth", "kerberos_v5");
284             else if (ctl->server.authenticate == A_SSH)
285                 stringdump("auth", "ssh");
286
287 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
288             booldump("dns", ctl->server.dns);
289 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
290             booldump("uidl", ctl->server.uidl);
291
292             listdump("aka", ctl->server.akalist);
293             listdump("localdomains", ctl->server.localdomains);
294
295 #if defined(linux) || defined(__FreeBSD__)
296             stringdump("interface", ctl->server.interface);
297             stringdump("monitor", ctl->server.monitor);
298 #endif /* linux || __FreeBSD__ */
299
300             stringdump("plugin", ctl->server.plugin);
301             stringdump("plugout", ctl->server.plugout);
302             stringdump("principal", ctl->server.principal);
303
304             indent(0);
305             fputs("'users': ", stdout);
306             indent('[');
307         }
308
309         indent('{');
310
311         stringdump("remote", ctl->remotename);
312         stringdump("password", ctl->password);
313
314         indent('\0');
315         fprintf(stdout, "'localnames':[");
316         for (idp = ctl->localnames; idp; idp = idp->next)
317         {
318             char        namebuf[USERNAMELEN + 1];
319
320             strncpy(namebuf, visbuf(idp->id), USERNAMELEN);
321             namebuf[USERNAMELEN] = '\0';
322             if (idp->val.id2)
323                 fprintf(stdout, "(\"%s\", %s)", namebuf, visbuf(idp->val.id2));
324             else
325                 fprintf(stdout, "\"%s\"", namebuf);
326             if (idp->next)
327                 fputs(", ", stdout);
328         }
329         if (ctl->wildcard)
330             fputs(", '*'", stdout);
331         fputs("],\n", stdout);
332
333         booldump("fetchall", ctl->fetchall);
334         booldump("keep", ctl->keep);
335         booldump("flush", ctl->flush);
336         booldump("rewrite", ctl->rewrite);
337         booldump("stripcr", ctl->stripcr); 
338         booldump("forcecr", ctl->forcecr);
339         booldump("pass8bits", ctl->pass8bits);
340         booldump("dropstatus", ctl->dropstatus);
341         booldump("dropdelivered", ctl->dropdelivered);
342         booldump("mimedecode", ctl->mimedecode);
343         booldump("idle", ctl->idle);
344
345         stringdump("mda", ctl->mda);
346         stringdump("bsmtp", ctl->bsmtp);
347         indent('\0');
348         if (ctl->listener == LMTP_MODE)
349             fputs("'lmtp':TRUE,\n", stdout);
350         else
351             fputs("'lmtp':FALSE,\n", stdout);
352             
353 #ifdef INET6_ENABLE
354         stringdump("netsec", ctl->server.netsec);
355 #endif /* INET6_ENABLE */
356         stringdump("preconnect", ctl->preconnect);
357         stringdump("postconnect", ctl->postconnect);
358         numdump("limit", ctl->limit);
359         numdump("warnings", ctl->warnings);
360         numdump("fetchlimit", ctl->fetchlimit);
361         numdump("batchlimit", ctl->batchlimit);
362 #ifdef SSL_ENABLE
363         booldump("ssl", ctl->use_ssl);
364         stringdump("sslkey", ctl->sslkey);
365         stringdump("sslcert", ctl->sslcert);
366 #endif /* SSL_ENABLE */
367         numdump("expunge", ctl->expunge);
368         stringdump("properties", ctl->properties);
369         listdump("smtphunt", ctl->smtphunt);
370         stringdump("smtpaddress", ctl->smtpaddress);
371         stringdump("smtpname", ctl->smtpname);
372
373         indent('\0');
374         fprintf(stdout, "'antispam':'");
375         if (!ctl->antispam)
376             fputs("'\n", stdout);
377         else
378         {
379             for (idp = ctl->antispam; idp; idp = idp->next)
380             {
381                 fprintf(stdout, "%d", idp->val.status.num);
382                 if (idp->next)
383                     fputs(" ", stdout);
384             }
385             fputs("',\n", stdout);
386         }
387         listdump("mailboxes", ctl->mailboxes);
388
389         indent('}');
390         indent('\0'); 
391         fputc(',', stdout);
392     }
393
394     /* end last span of user entries and last server entry */
395     indent(']');
396     indent('}');
397
398     /* end array of servers */
399     indent(']');
400
401  alldone:
402     /* end top-level dictionary */
403     indent('}');
404     fputs("# End of initializer\n", stdout);
405 }
406
407 /* conf.c ends here */