]> Pileus Git - ~andy/fetchmail/blob - conf.c
23826c8ee2bf1e72ad43ac42d4fc40b722e988df
[~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("'imap-gss',");
159 #endif /* GSSAPI */
160 #if defined(IMAP4) && defined(KERBEROS_V4)
161     printf("'imap-k4',");
162 #endif /* defined(IMAP4) && defined(KERBEROS_V4) */
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 #if OPIE
173     printf("'opie',");
174 #endif /* OPIE */
175 #if INET6
176     printf("'inet6',");
177 #endif /* INET6 */
178 #if NET_SECURITY
179     printf("'netsec',");
180 #endif /* NET_SECURITY */
181     printf(")\n");
182
183     fputs("# Start of configuration initializer\n", stdout);
184     fputs("fetchmailrc = ", stdout);
185     indent('{');
186
187     numdump("poll_interval", runp->poll_interval);
188     stringdump("logfile", runp->logfile);
189     stringdump("idfile", runp->idfile);
190     stringdump("postmaster", runp->postmaster);
191     booldump("bouncemail", runp->bouncemail);
192     stringdump("properties", runp->properties);
193     booldump("invisible", runp->invisible);
194     booldump("syslog", runp->use_syslog);
195
196     if (!querylist)
197     {
198         fputs("    'servers': []\n", stdout);
199         goto alldone;
200     }
201
202     indent(0);
203     fputs("# List of server entries begins here\n", stdout);
204     indent(0);
205     fputs("'servers': ", stdout);
206     indent('[');
207
208     for (ctl = querylist; ctl; ctl = ctl->next)
209     {
210         /*
211          * First, the server stuff.
212          */
213         if (!ctl->server.lead_server)
214         {
215             flag        using_kpop;
216
217             /*
218              * Every time we see a leading server entry after the first one,
219              * it implicitly ends the both (a) the list of user structures
220              * associated with the previous entry, and (b) that previous entry.
221              */
222             if (ctl > querylist)
223             {
224                 indent(']');
225                 indent('}');
226                 indent('\0'); 
227                 putc(',', stdout);
228                 putc('\n', stdout);
229             }
230
231             indent(0);
232             fprintf(stdout,"# Entry for site `%s' begins:\n",ctl->server.pollname);
233             indent('{');
234
235             using_kpop =
236                 (ctl->server.protocol == P_POP3 &&
237 #if !INET6
238                  ctl->server.port == KPOP_PORT &&
239 #else
240                  0 == strcmp( ctl->server.service, KPOP_PORT ) &&
241 #endif
242                  ctl->server.preauthenticate == A_KERBEROS_V4);
243
244             stringdump("pollname", ctl->server.pollname); 
245             booldump("active", !ctl->server.skip); 
246             stringdump("via", ctl->server.via); 
247             stringdump("protocol", 
248                        using_kpop ? "KPOP" : showproto(ctl->server.protocol));
249 #if !INET6
250             numdump("port",  ctl->server.port);
251 #else
252             stringdump("service", ctl->server.service); 
253 #endif
254             numdump("timeout",  ctl->server.timeout);
255             numdump("interval", ctl->server.interval);
256
257             if (ctl->server.envelope == STRING_DISABLED)
258                 stringdump("envelope", NULL); 
259             else if (ctl->server.envelope == NULL)
260                 stringdump("envelope", "Received");             
261             else
262                 stringdump("envelope", ctl->server.envelope); 
263             numdump("envskip", ctl->server.envskip);
264             stringdump("qvirtual", ctl->server.qvirtual);
265  
266             if (ctl->server.preauthenticate == A_KERBEROS_V4)
267                 stringdump("auth", "kerberos_v4");
268             else if (ctl->server.preauthenticate == A_KERBEROS_V5)
269                 stringdump("auth", "kerberos_v5");
270             else
271                 stringdump("auth", "password");
272
273 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
274             booldump("dns", ctl->server.dns);
275 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
276             booldump("uidl", ctl->server.uidl);
277
278             listdump("aka", ctl->server.akalist);
279             listdump("localdomains", ctl->server.localdomains);
280
281 #if defined(linux) || defined(__FreeBSD__)
282             stringdump("interface", ctl->server.interface);
283             stringdump("monitor", ctl->server.monitor);
284 #endif /* linux || __FreeBSD__ */
285
286             stringdump("plugin", ctl->server.plugin);
287             stringdump("plugout", ctl->server.plugout);
288
289             indent(0);
290             fputs("'users': ", stdout);
291             indent('[');
292         }
293
294         indent('{');
295
296         stringdump("remote", ctl->remotename);
297         stringdump("password", ctl->password);
298
299         indent('\0');
300         fprintf(stdout, "'localnames':[");
301         for (idp = ctl->localnames; idp; idp = idp->next)
302         {
303             char        namebuf[USERNAMELEN + 1];
304
305             strncpy(namebuf, visbuf(idp->id), USERNAMELEN);
306             namebuf[USERNAMELEN] = '\0';
307             if (idp->val.id2)
308                 fprintf(stdout, "(\"%s\", %s)", namebuf, visbuf(idp->val.id2));
309             else
310                 fprintf(stdout, "\"%s\"", namebuf);
311             if (idp->next)
312                 fputs(", ", stdout);
313         }
314         if (ctl->wildcard)
315             fputs(", '*'", stdout);
316         fputs("],\n", stdout);
317
318         booldump("fetchall", ctl->fetchall);
319         booldump("keep", ctl->keep);
320         booldump("flush", ctl->flush);
321         booldump("rewrite", ctl->rewrite);
322         booldump("stripcr", ctl->stripcr); 
323         booldump("forcecr", ctl->forcecr);
324         booldump("pass8bits", ctl->pass8bits);
325         booldump("dropstatus", ctl->dropstatus);
326         booldump("mimedecode", ctl->mimedecode);
327
328         stringdump("mda", ctl->mda);
329         stringdump("bsmtp", ctl->bsmtp);
330         indent('\0');
331         if (ctl->listener == LMTP_MODE)
332             fputs("'lmtp':TRUE,\n", stdout);
333         else
334             fputs("'lmtp':FALSE,\n", stdout);
335             
336 #ifdef INET6
337         stringdump("netsec", ctl->server.netsec);
338 #endif /* INET6 */
339         stringdump("preconnect", ctl->preconnect);
340         stringdump("postconnect", ctl->postconnect);
341         numdump("limit", ctl->limit);
342         numdump("warnings", ctl->warnings);
343         numdump("fetchlimit", ctl->fetchlimit);
344         numdump("batchlimit", ctl->batchlimit);
345         numdump("expunge", ctl->expunge);
346         stringdump("properties", ctl->properties);
347         listdump("smtphunt", ctl->smtphunt);
348         stringdump("smtpaddress", ctl->smtpaddress);
349
350         indent('\0');
351         fprintf(stdout, "'antispam':'");
352         if (!ctl->antispam)
353             fputs("'\n", stdout);
354         else
355         {
356             for (idp = ctl->antispam; idp; idp = idp->next)
357             {
358                 fprintf(stdout, "%d", idp->val.status.num);
359                 if (idp->next)
360                     fputs(" ", stdout);
361             }
362             fputs("',\n", stdout);
363         }
364         listdump("mailboxes", ctl->mailboxes);
365
366         indent('}');
367         indent('\0'); 
368         fputc(',', stdout);
369     }
370
371     /* end last span of user entries and last server entry */
372     indent(']');
373     indent('}');
374
375     /* end array of servers */
376     indent(']');
377
378  alldone:
379     /* end top-level dictionary */
380     indent('}');
381     fputs("# End of initializer\n", stdout);
382 }
383
384 /* conf.c ends here */