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