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