]> Pileus Git - ~andy/fetchmail/blob - conf.c
3e683d89135002b8286f23cd3bfdc32f6e659634
[~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     fputs("# Start of initializer\n", stdout);
133     fputs("fetchmailrc = ", stdout);
134     indent('{');
135
136     numdump("poll_interval", runp->poll_interval);
137     booldump("syslog", runp->use_syslog);
138     stringdump("logfile", runp->logfile);
139     stringdump("idfile", runp->idfile);
140     booldump("invisible", runp->invisible);
141
142     if (!querylist)
143         goto alldone;
144
145     indent(0);
146     fputs("# List of server entries begins here\n", stdout);
147     indent(0);
148     fputs("'servers': ", stdout);
149     indent('[');
150
151     for (ctl = querylist; ctl; ctl = ctl->next)
152     {
153         /*
154          * First, the server stuff.
155          */
156         if (!ctl->server.lead_server)
157         {
158             flag        using_kpop;
159
160             /*
161              * Every time we see a leading server entry after the first one,
162              * it implicitly ends the both (a) the list of user structures
163              * associated with the previous entry, and (b) that previous entry.
164              */
165             if (ctl > querylist)
166             {
167                 indent(']');
168                 indent('}');
169                 indent('\0'); 
170                 putc(',', stdout);
171                 putc('\n', stdout);
172             }
173
174             indent(0);
175             fprintf(stdout,"# Entry for site `%s' begins:\n",ctl->server.pollname);
176             indent('{');
177
178             using_kpop =
179                 (ctl->server.protocol == P_POP3 &&
180                  ctl->server.port == KPOP_PORT &&
181                  ctl->server.preauthenticate == A_KERBEROS_V4);
182
183             stringdump("pollname", ctl->server.pollname); 
184             booldump("active", !ctl->server.skip); 
185             stringdump("via", ctl->server.via); 
186             stringdump("protocol", 
187                        using_kpop ? "KPOP" : showproto(ctl->server.protocol));
188             numdump("port",  ctl->server.port);
189             numdump("timeout",  ctl->server.timeout);
190             numdump("interval", ctl->server.interval);
191
192             if (ctl->server.envelope == STRING_DISABLED)
193                 stringdump("envelope", NULL); 
194             else if (ctl->server.envelope == NULL)
195                 stringdump("envelope", "Received");             
196             else
197                 stringdump("envelope", ctl->server.envelope); 
198             numdump("envskip", ctl->server.envskip);
199             stringdump("qvirtual", ctl->server.qvirtual);
200  
201             if (ctl->server.preauthenticate == A_KERBEROS_V4)
202                 stringdump("auth", "kerberos_v4");
203             else if (ctl->server.preauthenticate == A_KERBEROS_V5)
204                 stringdump("auth", "kerberos_v5");
205             else
206                 stringdump("auth", "password");
207
208 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
209             booldump("dns", ctl->server.dns);
210 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
211             booldump("uidl", ctl->server.uidl);
212
213             listdump("aka", ctl->server.akalist);
214             listdump("localdomains", ctl->server.localdomains);
215
216 #ifdef linux
217             stringdump("interface", ctl->server.interface);
218             stringdump("monitor", ctl->server.monitor);
219 #endif /* linux */
220
221             indent(0);
222             fputs("'users': ", stdout);
223             indent('[');
224         }
225
226         indent('{');
227
228         stringdump("remote", ctl->remotename);
229         stringdump("password", ctl->password);
230
231         indent('\0');
232         fprintf(stdout, "'localnames':[");
233         for (idp = ctl->localnames; idp; idp = idp->next)
234         {
235             if (idp->val.id2)
236                 fprintf(stdout, "('%s', %s)", 
237                         visbuf(idp->id), visbuf(idp->val.id2));
238             else
239                 fprintf(stdout, "'%s'", visbuf(idp->id));
240             if (idp->next)
241                 fputs(", ", stdout);
242         }
243         if (ctl->wildcard)
244             fputs(", '*'", stdout);
245         fputs("],\n", stdout);
246
247         booldump("fetchall", ctl->fetchall);
248         booldump("keep", ctl->keep);
249         booldump("flush", ctl->flush);
250         booldump("rewrite", ctl->rewrite);
251         booldump("stripcr", ctl->stripcr); 
252         booldump("forcecr", ctl->forcecr);
253         booldump("pass8bits", ctl->pass8bits);
254         booldump("dropstatus", ctl->dropstatus);
255         booldump("mimedecode", ctl->mimedecode);
256
257         stringdump("mda", ctl->mda);
258 #ifdef INET6
259         stringdump("netsec", ctl->netsec);
260 #endif /* INET6 */
261         stringdump("preconnect", ctl->preconnect);
262         stringdump("postconnect", ctl->postconnect);
263         numdump("limit", ctl->limit);
264         numdump("fetchlimit", ctl->fetchlimit);
265         numdump("batchlimit", ctl->batchlimit);
266         numdump("expunge", ctl->expunge);
267         listdump("smtphunt", ctl->smtphunt);
268         stringdump("smtpaddress", ctl->smtpaddress);
269         numdump("antispam", ctl->antispam);
270         listdump("mailboxes", ctl->mailboxes);
271
272         indent('}');
273         indent('\0'); 
274         fputc(',', stdout);
275     }
276
277     /* end last span of user entries and last server entry */
278     indent(']');
279     indent('}');
280
281     /* end array of servers */
282     indent(']');
283
284  alldone:
285     /* end top-level dictionary */
286     indent('}');
287     fputs("# End of initializer\n", stdout);
288 }
289
290 /* conf.c ends here */