]> Pileus Git - ~andy/fetchmail/blob - uid.c
8c5040f26d4bb5fab8458966422eba1ed3aec483
[~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 #include <limits.h>
11 #if defined(STDC_HEADERS)
12 #include <stdlib.h>
13 #include <string.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18
19 #include "fetchmail.h"
20
21 /*
22  * Machinery for handling UID lists live here.  This is mainly to support
23  * RFC1725-conformant POP3 servers without a LAST command, but may also be
24  * useful for making the IMAP4 querying logic UID-oriented, if a future
25  * revision of IMAP forces me to.
26  *
27  * These functions are also used by the rest of the code to maintain
28  * string lists.
29  *
30  * Here's the theory:
31  *
32  * At start of a query, we have a (possibly empty) list of UIDs to be
33  * considered seen in `oldsaved'.  These are messages that were left in
34  * the mailbox and *not deleted* on previous queries (we don't need to
35  * remember the UIDs of deleted messages because ... well, they're gone!)
36  * This list is initially set up by initialize_saved_list() from the
37  * .fetchids file.
38  *
39  * Early in the query, during the execution of the protocol-specific 
40  * getrange code, the driver expects that the host's `newsaved' member
41  * will be filled with a list of UIDs and message numbers representing
42  * the mailbox state.  If this list is empty, the server did
43  * not respond to the request for a UID listing.
44  *
45  * Each time a message is fetched, we can check its UID against the
46  * `oldsaved' list to see if it is old.
47  *
48  * Each time a message-id is seen, we mark it with MARK_SEEN.
49  *
50  * Each time a message is deleted, we mark its id UID_DELETED in the
51  * `newsaved' member.  When we want to assert that an expunge has been
52  * done on the server, we call expunge_uid() to register that all
53  * deleted messages are gone by marking them UID_EXPUNGED.
54  *
55  * At the end of the query, the `newsaved' member becomes the
56  * `oldsaved' list.  The old `oldsaved' list is freed.
57  *
58  * At the end of the fetchmail run, seen and non-EXPUNGED members of all
59  * current `oldsaved' lists are flushed out to the .fetchids file to
60  * be picked up by the next run.  If there are no un-expunged
61  * messages, the file is deleted.
62  *
63  * Note: some comparisons (those used for DNS address lists) are caseblind!  
64  */
65
66 /* UIDs associated with un-queried hosts */
67 static struct idlist *scratchlist;
68
69 #ifdef POP3_ENABLE
70 void initialize_saved_lists(struct query *hostlist, const char *idfile)
71 /* read file of saved IDs and attach to each host */
72 {
73     int st;
74     FILE        *tmpfp;
75     struct query *ctl;
76
77     /* make sure lists are initially empty */
78     for (ctl = hostlist; ctl; ctl = ctl->next)
79         ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
80
81     /* let's get stored message UIDs from previous queries */
82     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
83         char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
84
85         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
86         {
87             /*
88              * This inelegant hack was brought to you by the fact that
89              * some dial-up resellers actually use account names with
90              * @ in them.  So we need to split on the rightmost @...
91              */
92             char        *atsign = strrchr(buf, '@');
93
94             if (atsign)
95                 *atsign = ' ';
96
97             if ((st = sscanf(buf, "%s %s %s\n", user, host, id)) == 3)
98             {
99                 for (ctl = hostlist; ctl; ctl = ctl->next)
100                 {
101                     if (ctl->server.truename &&
102                         strcasecmp(host, ctl->server.truename) == 0
103                                 && strcasecmp(user, ctl->remotename) == 0)
104                     {
105                         save_str(&ctl->oldsaved, id, UID_SEEN);
106                         break;
107                     }
108                 }
109
110                 /* if it's not in a host we're querying, save it anyway */
111                 if (ctl == (struct query *)NULL)
112                     save_str(&scratchlist, buf, UID_SEEN);
113             }
114         }
115         fclose(tmpfp);
116     }
117
118     if (outlevel >= O_DEBUG)
119     {
120         struct idlist   *idp;
121         int uidlcount = 0;
122
123         for (ctl = hostlist; ctl; ctl = ctl->next)
124             if (ctl->server.uidl)
125             {
126                 report_build(stdout, "Old UID list from %s:",ctl->server.pollname);
127                 for (idp = ctl->oldsaved; idp; idp = idp->next)
128                     report_build(stdout, " %s", idp->id);
129                 if (!idp)
130                     report_build(stdout, " <empty>");
131                 report_complete(stdout, "\n");
132                 uidlcount++;
133             }
134
135         if (uidlcount)
136         {
137             report_build(stdout, "Scratch list of UIDs:");
138             for (idp = scratchlist; idp; idp = idp->next)
139                 report_build(stdout, " %s", idp->id);
140             if (!idp)
141                 report_build(stdout, " <empty>");
142             report_complete(stdout, "\n");
143         }
144     }
145 }
146 #endif /* POP3_ENABLE */
147
148 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
149 /* save a number/UID pair on the given UID list */
150 {
151     struct idlist **end;
152
153     /* do it nonrecursively so the list is in the right order */
154     for (end = idl; *end; end = &(*end)->next)
155         continue;
156
157     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
158     (*end)->val.status.mark = status;
159     (*end)->id = str ? xstrdup(str) : (char *)NULL;
160     (*end)->next = NULL;
161
162     return(*end);
163 }
164
165 void free_str_list(struct idlist **idl)
166 /* free the given UID list */
167 {
168     if (*idl == (struct idlist *)NULL)
169         return;
170
171     free_str_list(&(*idl)->next);
172     free ((*idl)->id);
173     free(*idl);
174     *idl = (struct idlist *)NULL;
175 }
176
177 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
178 /* save an ID pair on the given list */
179 {
180     struct idlist **end;
181
182     /* do it nonrecursively so the list is in the right order */
183     for (end = idl; *end; end = &(*end)->next)
184         continue;
185
186     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
187     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
188     if (str2)
189         (*end)->val.id2 = xstrdup(str2);
190     else
191         (*end)->val.id2 = (char *)NULL;
192     (*end)->next = (struct idlist *)NULL;
193 }
194
195 #ifdef __UNUSED__
196 void free_str_pair_list(struct idlist **idl)
197 /* free the given ID pair list */
198 {
199     if (*idl == (struct idlist *)NULL)
200         return;
201
202     free_idpair_list(&(*idl)->next);
203     free ((*idl)->id);
204     free ((*idl)->val.id2);
205     free(*idl);
206     *idl = (struct idlist *)NULL;
207 }
208 #endif
209
210 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
211 /* is a given ID in the given list? (comparison may be caseblind) */
212 {
213     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
214         return(0);
215     else if (!caseblind && strcmp(str, (*idl)->id) == 0)
216         return(1);
217     else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
218         return(1);
219     else
220         return(str_in_list(&(*idl)->next, str, caseblind));
221 }
222
223 int str_nr_in_list( struct idlist **idl, const char *str )
224   /* return the position of str in idl */
225 {
226     int nr;
227     struct idlist *walk;
228     if ( !str )
229         return -1;
230     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
231         if( strcmp( str, walk->id) == 0 )
232             return nr;
233     return -1;
234 }
235
236 int str_nr_last_in_list( struct idlist **idl, const char *str)
237 /* return the last position of str in idl */
238 {
239     int nr, ret = -1;
240     struct idlist *walk;
241     if ( !str )
242         return -1;
243     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
244         if( strcmp( str, walk->id) == 0 )
245             ret = nr;
246     return ret;
247 }
248
249 void str_set_mark( struct idlist **idl, const char *str, const flag val)
250 /* update the mark on an of an id to given value */
251 {
252     int nr;
253     struct idlist *walk;
254     if (!str)
255         return;
256     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
257         if (strcmp(str, walk->id) == 0)
258             walk->val.status.mark = val;
259 }
260
261 int count_list( struct idlist **idl)
262 /* count the number of elements in the list */
263 {
264   if( !*idl )
265     return 0;
266   return 1 + count_list( &(*idl)->next );
267 }
268
269 char *str_from_nr_list(struct idlist **idl, int number)
270 /* return the number'th string in idl */
271 {
272     if( !*idl  || number < 0)
273         return 0;
274     if( number == 0 )
275         return (*idl)->id;
276     return str_from_nr_list(&(*idl)->next, number-1);
277 }
278
279     
280 char *str_find(struct idlist **idl, int number)
281 /* return the id of the given number in the given list. */
282 {
283     if (*idl == (struct idlist *) 0)
284         return((char *) 0);
285     else if (number == (*idl)->val.status.num)
286         return((*idl)->id);
287     else
288         return(str_find(&(*idl)->next, number));
289 }
290
291 char *idpair_find(struct idlist **idl, const char *id)
292 /* return the id of the given id in the given list (caseblind comparison) */
293 {
294     if (*idl == (struct idlist *) 0)
295         return((char *) 0);
296     else if (strcasecmp(id, (*idl)->id) == 0)
297         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
298     else
299         return(idpair_find(&(*idl)->next, id));
300 }
301
302 int delete_str(struct idlist **idl, int num)
303 /* delete given message from given list */
304 {
305     struct idlist       *idp;
306
307     for (idp = *idl; idp; idp = idp->next)
308         if (idp->val.status.num == num)
309         {
310             idp->val.status.mark = UID_DELETED;
311             return(1);
312         }
313     return(0);
314 }
315
316 void append_str_list(struct idlist **idl, struct idlist **nidl)
317 /* append nidl to idl (does not copy *) */
318 {
319     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
320         return;
321     else if ((*idl) == (struct idlist *)NULL)
322         *idl = *nidl;
323     else if ((*idl)->next == (struct idlist *)NULL)
324         (*idl)->next = *nidl;
325     else if ((*idl)->next != *nidl)
326         append_str_list(&(*idl)->next, nidl);
327 }
328
329 #ifdef POP3_ENABLE
330 void expunge_uids(struct query *ctl)
331 /* assert that all UIDs marked deleted have actually been expunged */
332 {
333     struct idlist *idl;
334
335     for (idl = ctl->newsaved; idl; idl = idl->next)
336         if (idl->val.status.mark == UID_DELETED)
337             idl->val.status.mark = UID_EXPUNGED;
338 }
339
340 void update_str_lists(struct query *ctl)
341 /* perform end-of-query actions on UID lists */
342 {
343     free_str_list(&ctl->oldsaved);
344     free_str_list(&scratchlist);
345     ctl->oldsaved = ctl->newsaved;
346     ctl->newsaved = (struct idlist *) NULL;
347
348     if (ctl->server.uidl && outlevel >= O_DEBUG)
349     {
350         struct idlist *idp;
351
352         report_build(stdout, "New UID list from %s:", ctl->server.pollname);
353         for (idp = ctl->oldsaved; idp; idp = idp->next)
354             report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
355         if (!idp)
356             report_build(stdout, " <empty>");
357         report_complete(stdout, "\n");
358     }
359 }
360
361 void write_saved_lists(struct query *hostlist, const char *idfile)
362 /* perform end-of-run write of seen-messages list */
363 {
364     int         idcount;
365     FILE        *tmpfp;
366     struct query *ctl;
367     struct idlist *idp;
368
369     /* if all lists are empty, nuke the file */
370     idcount = 0;
371     for (ctl = hostlist; ctl; ctl = ctl->next) {
372         if (ctl->oldsaved)
373             idcount++;
374     }
375
376     /* either nuke the file or write updated last-seen IDs */
377     if (!idcount && !scratchlist)
378     {
379         if (outlevel >= O_DEBUG)
380             report(stdout, "Deleting fetchids file.\n");
381         unlink(idfile);
382     }
383     else
384         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
385             for (ctl = hostlist; ctl; ctl = ctl->next) {
386                 for (idp = ctl->oldsaved; idp; idp = idp->next)
387                     if (idp->val.status.mark == UID_SEEN
388                                 || idp->val.status.mark == UID_DELETED)
389                         fprintf(tmpfp, "%s@%s %s\n", 
390                             ctl->remotename, ctl->server.truename, idp->id);
391             }
392             for (idp = scratchlist; idp; idp = idp->next)
393                 fputs(idp->id, tmpfp);
394             fclose(tmpfp);
395         }
396 }
397 #endif /* POP3_ENABLE */
398
399 /* uid.c ends here */