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