]> Pileus Git - ~andy/fetchmail/blob - conf.c
Initial revision
[~andy/fetchmail] / conf.c
1 /*
2  * conf.c -- main driver module for fetchmailconf
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 /*
25  * Note: this function dumps the entire configuration,
26  * after merging of the defaults record (if any).  It
27  * is intended to produce output parseable by a configuration
28  * front end, not anything especially comfortable for humans.
29  */
30
31 void dump_config(struct runctl *runp, struct query *querylist)
32 /* dump the in-core configuration in recompilable form */
33 {
34     struct query *ctl;
35     struct idlist *idp;
36     time_t now;
37
38     /* now write the edited configuration back to the file */
39     time(&now);
40     fprintf(stdout, "# fetchmail rc file generated at %s", ctime(&now));
41
42     if (runp->poll_interval)
43         fprintf(stdout, "set daemon %d\n", runp->poll_interval);
44     if (runp->use_syslog)
45         fprintf(stdout, "set syslog\n");
46     if (runp->logfile)
47         fprintf(stdout, "set logfile %s\n", runp->logfile);
48     if (runp->idfile)
49         fprintf(stdout, "set idfile %s\n", runp->idfile);
50     if (runp->invisible)
51         fprintf(stdout, "set invisible\n");
52
53     for (ctl = querylist; ctl; ctl = ctl->next)
54     {
55         /*
56          * First, the server stuff.
57          */
58         if (!ctl->server.lead_server)
59         {
60             flag using_kpop =
61                 (ctl->server.protocol == P_POP3 &&
62                  ctl->server.port == KPOP_PORT &&
63                  ctl->server.preauthenticate == A_KERBEROS_V4);
64
65             if (strcmp(ctl->server.pollname, "defaults") == 0)
66                 fputs("defaults ", stdout);
67             else
68                 fprintf(stdout, "%s %s ",
69                     ctl->server.skip ? "skip" : "poll",
70                     visbuf(ctl->server.pollname));
71             if (ctl->server.via)
72                 fprintf(stdout, "via %s ", ctl->server.via);
73             if (ctl->server.protocol != P_AUTO)
74                 fprintf(stdout, "with protocol %s ",
75                         using_kpop ? "KPOP" : showproto(ctl->server.protocol));
76             if (ctl->server.port)
77                 fprintf(stdout, "port %d ", ctl->server.port);
78             if (ctl->server.timeout)
79                 fprintf(stdout, "timeout %d ", ctl->server.timeout);
80             if (ctl->server.interval)
81                 fprintf(stdout, "interval %d ", ctl->server.interval);
82             if (ctl->server.envelope == STRING_DISABLED)
83                 fprintf(stdout, "no envelope ");
84             else if (ctl->server.envelope)
85                 fprintf(stdout, "envelope \"%s\" ", visbuf(ctl->server.envelope));
86             if (ctl->server.qvirtual)
87                 fprintf(stdout, "qvirtual \"%s\" ", visbuf(ctl->server.qvirtual));
88             if (ctl->server.preauthenticate == A_KERBEROS_V4)
89                 fprintf(stdout, "auth kerberos_v4 ");
90 #define DUMPOPT(flag, str) \
91                 if (flag) \
92                     fprintf(stdout, "%s ", str); \
93                 else \
94                     fprintf(stdout, "no %s ", str);
95 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
96             if (ctl->server.dns || ctl->server.uidl)
97 #else
98             if (ctl->server.uidl)
99 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
100                 fputs("and options ", stdout);
101 #if defined(HAVE_GETHOSTBYNAME) && defined(HAVE_RES_SEARCH)
102             DUMPOPT(ctl->server.dns,  "dns");
103 #endif /* HAVE_GETHOSTBYNAME && HAVE_RES_SEARCH */
104             DUMPOPT(ctl->server.uidl, "uidl");
105             fputs("\n", stdout);
106
107             /* AKA and loca-domain declarations */
108             if (ctl->server.akalist || ctl->server.localdomains)
109             {
110                 fputc('\t', stdout);
111                 if (ctl->server.akalist)
112                 {
113                     struct idlist *idp;
114
115                     fprintf(stdout, "aka");
116                     for (idp = ctl->server.akalist; idp; idp = idp->next)
117                         fprintf(stdout, " %s", visbuf(idp->id));
118                 }
119
120                 if (ctl->server.akalist && ctl->server.localdomains)
121                     putc(' ', stdout);
122
123                 if (ctl->server.localdomains)
124                 {
125                     struct idlist *idp;
126
127                     fprintf(stdout, "localdomains");
128                     for (idp = ctl->server.localdomains; idp; idp = idp->next)
129                         fprintf(stdout, " %s", visbuf(idp->id));
130                 }
131                 putc('\n', stdout);
132             }
133
134 #ifdef linux
135             if (ctl->server.monitor || ctl->server.interface)
136             {
137                 putc('\t', stdout);
138                 if (ctl->server.monitor)
139                     fprintf(stdout, "monitor \"%s\" ", ctl->server.monitor);
140                 if (ctl->server.interface)
141                     fprintf(stdout, "interface \"%s\"", ctl->server.interface);
142                 putc('\n', stdout);
143             }
144 #endif /* linux */
145         }
146
147         fputc('\t', stdout);
148         if (ctl->remotename || ctl->password || ctl->localnames)
149         {
150             if (ctl->remotename)
151                 fprintf(stdout, "user \"%s\" ", visbuf(ctl->remotename));
152             if (ctl->remotename && ctl->password)
153                 fputs("with ", stdout);
154             if (ctl->password)
155                 fprintf(stdout, "password \"%s\" ", visbuf(ctl->password));
156             if (ctl->localnames)
157             {
158                 fprintf(stdout, "is ");
159                 for (idp = ctl->localnames; idp; idp = idp->next)
160                     if (idp->val.id2)
161                         fprintf(stdout, "\"%s\"=\"%s\" ", 
162                                 visbuf(idp->id), visbuf(idp->val.id2));
163                     else
164                         fprintf(stdout, "%s ", visbuf(idp->id));
165                 if (ctl->wildcard)
166                     fputs("*", stdout);
167             }
168         }
169
170         if (ctl->fetchall || ctl->keep || ctl->flush || ctl->rewrite
171                         || ctl->stripcr || ctl->forcecr || ctl->pass8bits)
172             fputs("options ", stdout);
173         DUMPOPT(ctl->fetchall,    "fetchall");
174         DUMPOPT(ctl->keep,        "keep");
175         DUMPOPT(ctl->flush,       "flush");
176         DUMPOPT(ctl->rewrite,     "rewrite");
177         DUMPOPT(ctl->stripcr,     "stripcr"); 
178         DUMPOPT(ctl->forcecr,     "forcecr");
179         DUMPOPT(ctl->pass8bits,   "pass8bits");
180         DUMPOPT(ctl->dropstatus,  "dropstatus");
181         DUMPOPT(ctl->mimedecode,  "mimedecode");
182 #undef DUMPOPT
183
184         if (ctl->mda)
185             fprintf(stdout, "mda \"%s\" ", visbuf(ctl->mda));
186 #ifdef INET6
187         if (ctl->netsec)
188             fprintf(stdout, "netsec \"%s\" ", visbuf(ctl->netsec));
189 #endif /* INET6 */
190         if (ctl->preconnect)
191             fprintf(stdout, "preconnect \"%s\" ", visbuf(ctl->preconnect));     
192         if (ctl->postconnect)
193             fprintf(stdout, "postconnect \"%s\" ", visbuf(ctl->postconnect));   
194         if (ctl->fetchlimit)
195             fprintf(stdout, "fetchlimit %d ", ctl->fetchlimit);
196         if (ctl->batchlimit)
197             fprintf(stdout, "batchlimit %d ", ctl->batchlimit);
198
199         if (ctl->smtphunt)
200         {
201             struct idlist *idp;
202
203             fprintf(stdout, "smtphost ");
204             for (idp = ctl->smtphunt; idp; idp = idp->next)
205                 fprintf(stdout, "%s ", visbuf(idp->id));
206         }
207
208         if (ctl->smtpaddress)
209             fprintf(stdout, "smtpaddress \"%s\" ", visbuf(ctl->smtpaddress));
210
211         if (ctl->antispam)
212             fprintf(stdout, "antispam %d ", ctl->antispam);
213
214         if (ctl->mailboxes && ctl->mailboxes->id)
215         {
216             struct idlist *idp;
217
218             fprintf(stdout, "mailboxes ");
219             for (idp = ctl->mailboxes; idp; idp = idp->next)
220                 fprintf(stdout, "%s ", visbuf(idp->id));
221         }
222
223         putc('\n', stdout);
224     }
225 }
226
227 /* conf.c ends here */