]> Pileus Git - ~andy/fetchmail/blob - uid.c
Some configuration cleanup.
[~andy/fetchmail] / uid.c
1 /*
2  * uid.c -- UIDL handling for POP3 servers without LAST
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <stdio.h>
10 #if defined(STDC_HEADERS)
11 #include <stdlib.h>
12 #include <string.h>
13 #endif
14 #if defined(HAVE_UNISTD_H)
15 #include <unistd.h>
16 #endif
17
18 #include "fetchmail.h"
19
20 /*
21  * Machinery for handling UID lists live here.  This is mainly to support
22  * RFC1725-conformant POP3 servers without a LAST command, but may also be
23  * useful for making the IMAP4 querying logic UID-oriented, if a future
24  * revision of IMAP forces me to.
25  *
26  * Here's the theory:
27  *
28  * At start of a query, we have a (possibly empty) list of UIDs to be
29  * considered seen in `oldsaved'.  These are messages that were left in
30  * the mailbox and *not deleted* on previous queries (we don't need to
31  * remember the UIDs of deleted messages because ... well, they're gone!)
32  * This list is initially set up by initialized_saved_list() from the
33  * .fetchids file.
34  *
35  * Early in the query, during the execution of the protocol-specific 
36  * getrange code, the driver expects that the host's `newsaved' member
37  * will be filled with a list of UIDs and message numbers representing
38  * the mailbox state.  If this list is empty, the server did
39  * not respond to the request for a UID listing.
40  *
41  * Each time a message is fetched, we can check its UID against the
42  * `oldsaved' list to see if it is old.  If not, it should be downloaded
43  * (and possibly deleted).  It should be downloaded anyway if --all
44  * is on.  It should not be deleted if --keep is on.
45  *
46  * Each time a message is deleted, we remove its id from the `newsaved'
47  * member.
48  *
49  * At the end of the query, whatever remains in the `newsaved' member
50  * (because it was not deleted) becomes the `oldsaved' list.  The old
51  * `oldsaved' list is freed.
52  *
53  * At the end of the fetchmail run, all current `oldsaved' lists are
54  * flushed out to the .fetchids file to be picked up by the next run.
55  * If there are no such messages, the file is deleted.
56  *
57  * Note: all comparisons are caseblind!
58  */
59
60 /* UIDs associated with un-queried hosts */
61 static struct idlist *scratchlist;
62
63 void initialize_saved_lists(struct query *hostlist, const char *idfile)
64 /* read file of saved IDs and attach to each host */
65 {
66     int st;
67     FILE        *tmpfp;
68     struct query *ctl;
69
70     /* make sure lists are initially empty */
71     for (ctl = hostlist; ctl; ctl = ctl->next)
72         ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
73
74     /* let's get stored message UIDs from previous queries */
75     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
76         char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
77
78         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
79         {
80             /* possible lossage here with very old versions of sscanf(3)... */
81             if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
82             {
83                 for (ctl = hostlist; ctl; ctl = ctl->next)
84                 {
85                     if (strcasecmp(host, ctl->server.names->id) == 0
86                                 && strcasecmp(user, ctl->remotename) == 0)
87                     {
88                         save_str(&ctl->oldsaved, -1, id);
89                         break;
90                     }
91                 }
92
93                 /* if it's not in a host we're querying, save it anyway */
94                 if (ctl == (struct query *)NULL)
95                     save_str(&scratchlist, -1, buf);
96             }
97         }
98         fclose(tmpfp);
99     }
100 }
101
102 struct idlist *save_str(struct idlist **idl, int num, const char *str)
103 /* save a number/UID pair on the given UID list */
104 {
105     struct idlist **end;
106
107     /* do it nonrecursively so the list is in the right order */
108     for (end = idl; *end; end = &(*end)->next)
109         continue;
110
111     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
112     (*end)->val.num = num;
113     (*end)->id = str ? xstrdup(str) : (char *)NULL;
114     (*end)->next = NULL;
115
116     return(*end);
117 }
118
119 void free_str_list(struct idlist **idl)
120 /* free the given UID list */
121 {
122     if (*idl == (struct idlist *)NULL)
123         return;
124
125     free_str_list(&(*idl)->next);
126     free ((*idl)->id);
127     free(*idl);
128     *idl = (struct idlist *)NULL;
129 }
130
131 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
132 /* save an ID pair on the given list */
133 {
134     struct idlist **end;
135
136     /* do it nonrecursively so the list is in the right order */
137     for (end = idl; *end; end = &(*end)->next)
138         continue;
139
140     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
141     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
142     if (str2)
143         (*end)->val.id2 = xstrdup(str2);
144     else
145         (*end)->val.id2 = (char *)NULL;
146     (*end)->next = (struct idlist *)NULL;
147 }
148
149 #ifdef __UNUSED__
150 void free_str_pair_list(struct idlist **idl)
151 /* free the given ID pair list */
152 {
153     if (*idl == (struct idlist *)NULL)
154         return;
155
156     free_idpair_list(&(*idl)->next);
157     free ((*idl)->id);
158     free ((*idl)->val.id2);
159     free(*idl);
160     *idl = (struct idlist *)NULL;
161 }
162 #endif
163
164 int str_in_list(struct idlist **idl, const char *str)
165 /* is a given ID in the given list? (comparison is caseblind) */
166 {
167     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
168         return(0);
169     else if (strcasecmp(str, (*idl)->id) == 0)
170         return(1);
171     else
172         return(str_in_list(&(*idl)->next, str));
173 }
174
175 char *str_find(struct idlist **idl, int number)
176 /* return the id of the given number in the given list. */
177 {
178     if (*idl == (struct idlist *) 0)
179         return((char *) 0);
180     else if (number == (*idl)->val.num)
181         return((*idl)->id);
182     else
183         return(str_find(&(*idl)->next, number));
184 }
185
186 char *idpair_find(struct idlist **idl, const char *id)
187 /* return the id of the given id in the given list (caseblind comparison) */
188 {
189     if (*idl == (struct idlist *) 0)
190         return((char *) 0);
191     else if (strcasecmp(id, (*idl)->id) == 0)
192         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
193     else
194         return(idpair_find(&(*idl)->next, id));
195 }
196
197 int delete_str(struct idlist **idl, int num)
198 /* delete given message from given list */
199 {
200     if (*idl == (struct idlist *)NULL)
201         return(0);
202     else if ((*idl)->val.num == num)
203     {
204         struct idlist   *next = (*idl)->next;
205
206         free ((*idl)->id);
207         free(*idl);
208         *idl = next;
209         return(1);
210     }
211     else
212         return(delete_str(&(*idl)->next, num));
213     return(0);
214 }
215
216 void append_str_list(struct idlist **idl, struct idlist **nidl)
217 /* append nidl to idl (does not copy *) */
218 {
219     if ((*idl) == (struct idlist *)NULL)
220         *idl = *nidl;
221     else if ((*idl)->next == (struct idlist *)NULL)
222         (*idl)->next = *nidl;
223     else if ((*idl)->next != *nidl)
224         append_str_list(&(*idl)->next, nidl);
225 }
226
227 void update_str_lists(struct query *ctl)
228 /* perform end-of-query actions on UID lists */
229 {
230     free_str_list(&ctl->oldsaved);
231     ctl->oldsaved = ctl->newsaved;
232     ctl->newsaved = (struct idlist *) NULL;
233 }
234
235 void write_saved_lists(struct query *hostlist, const char *idfile)
236 /* perform end-of-run write of seen-messages list */
237 {
238     int         idcount;
239     FILE        *tmpfp;
240     struct query *ctl;
241     struct idlist *idp;
242
243     /* if all lists are empty, nuke the file */
244     idcount = 0;
245     for (ctl = hostlist; ctl; ctl = ctl->next) {
246         if (ctl->oldsaved)
247             idcount++;
248     }
249
250     /* either nuke the file or write updated last-seen IDs */
251     if (!idcount)
252         unlink(idfile);
253     else
254         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
255             for (ctl = hostlist; ctl; ctl = ctl->next) {
256                 for (idp = ctl->oldsaved; idp; idp = idp->next)
257                     fprintf(tmpfp, "%s@%s %s\n", 
258                             ctl->remotename, ctl->server.names->id, idp->id);
259             }
260             for (idp = scratchlist; idp; idp = idp->next)
261                 fputs(idp->id, tmpfp);
262             fclose(tmpfp);
263         }
264 }
265
266 /* uid.c ends here */