]> Pileus Git - ~andy/fetchmail/blob - uid.c
a33f21b69f85971a8fc6c4edac0633b487d35946
[~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 <sys/stat.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #if defined(STDC_HEADERS)
14 #include <stdlib.h>
15 #include <string.h>
16 #endif
17 #if defined(HAVE_UNISTD_H)
18 #include <unistd.h>
19 #endif
20
21 #include "fetchmail.h"
22 #include "i18n.h"
23
24 /*
25  * Machinery for handling UID lists live here.  This is mainly to support
26  * RFC1725-conformant POP3 servers without a LAST command, but may also be
27  * useful for making the IMAP4 querying logic UID-oriented, if a future
28  * revision of IMAP forces me to.
29  *
30  * These functions are also used by the rest of the code to maintain
31  * string lists.
32  *
33  * Here's the theory:
34  *
35  * At start of a query, we have a (possibly empty) list of UIDs to be
36  * considered seen in `oldsaved'.  These are messages that were left in
37  * the mailbox and *not deleted* on previous queries (we don't need to
38  * remember the UIDs of deleted messages because ... well, they're gone!)
39  * This list is initially set up by initialize_saved_list() from the
40  * .fetchids file.
41  *
42  * Early in the query, during the execution of the protocol-specific 
43  * getrange code, the driver expects that the host's `newsaved' member
44  * will be filled with a list of UIDs and message numbers representing
45  * the mailbox state.  If this list is empty, the server did
46  * not respond to the request for a UID listing.
47  *
48  * Each time a message is fetched, we can check its UID against the
49  * `oldsaved' list to see if it is old.
50  *
51  * Each time a message-id is seen, we mark it with MARK_SEEN.
52  *
53  * Each time a message is deleted, we mark its id UID_DELETED in the
54  * `newsaved' member.  When we want to assert that an expunge has been
55  * done on the server, we call expunge_uid() to register that all
56  * deleted messages are gone by marking them UID_EXPUNGED.
57  *
58  * At the end of the query, the `newsaved' member becomes the
59  * `oldsaved' list.  The old `oldsaved' list is freed.
60  *
61  * At the end of the fetchmail run, seen and non-EXPUNGED members of all
62  * current `oldsaved' lists are flushed out to the .fetchids file to
63  * be picked up by the next run.  If there are no un-expunged
64  * messages, the file is deleted.
65  *
66  * Note: some comparisons (those used for DNS address lists) are caseblind!  
67  */
68
69 /* UIDs associated with un-queried hosts */
70 static struct idlist *scratchlist;
71
72 #ifdef POP3_ENABLE
73 void initialize_saved_lists(struct query *hostlist, const char *idfile)
74 /* read file of saved IDs and attach to each host */
75 {
76     struct stat statbuf;
77     FILE        *tmpfp;
78     struct query *ctl;
79
80     /* make sure lists are initially empty */
81     for (ctl = hostlist; ctl; ctl = ctl->next)
82         ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
83
84     errno = 0;
85
86     /*
87      * Croak if the uidl directory does not exist.
88      * This probably means an NFS mount failed and we can't
89      * see a uidl file that ought to be there.
90      * Question: is this a portable check? It's not clear
91      * that all implementations of lstat() will return ENOTDIR
92      * rather than plain ENOENT in this case...
93      */
94    if (lstat(idfile, &statbuf) < 0) {
95      if (errno == ENOTDIR) 
96     {
97       report(stderr, GT_("lstat: %s: %s\n"), idfile, strerror(errno));
98       exit(PS_IOERR);
99     }
100    }
101
102     /* let's get stored message UIDs from previous queries */
103     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
104     {
105         char buf[POPBUFSIZE+1];
106         char *host = NULL;      /* pacify -Wall */
107         char *user;
108         char *id;
109         char *atsign;   /* temp pointer used in parsing user and host */
110         char *delimp1;
111         char saveddelim1;
112         char *delimp2;
113         char saveddelim2 = '\0';        /* pacify -Wall */
114
115         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
116         {
117             /*
118              * At this point, we assume the bug has two fields -- a user@host 
119              * part, and an ID part. Either field may contain spurious @ signs.
120              * The previous version of this code presumed one could split at 
121              * the rightmost '@'.  This is not correct, as InterMail puts an 
122              * '@' in the UIDL.
123              */
124           
125             /* first, skip leading spaces */
126             user = buf + strspn(buf, " \t");
127
128             /*
129              * First, we split the buf into a userhost part and an id
130              * part ... but id doesn't necessarily start with a '<',
131              * espescially if the POP server returns an X-UIDL header
132              * instead of a Message-ID, as GMX's (www.gmx.net) POP3
133              * StreamProxy V1.0 does.
134              */
135             if ((id = strchr(user, ' ')) != NULL )
136             {
137
138               /*
139                * this is one other trick. The userhost part 
140                * may contain ' ' in the user part, at least in
141                * the lotus notes case.
142                * So we start looking for the '@' after which the
143                * host will follow with the ' ' seperator finaly id.
144                */
145                 delimp1 = strchr(user, '@');
146                 id = strchr(delimp1,' ');
147                 for (delimp1 = id; delimp1 >= user; delimp1--)
148                     if ((*delimp1 != ' ') && (*delimp1 != '\t'))
149                         break;
150
151                 /* 
152                  * It should be safe to assume that id starts after
153                  * the " " - after all, we're writing the " "
154                  * ourselves in write_saved_lists() :-)
155                  */
156                 id = id + strspn(id, " ");
157
158                 delimp1++; /* but what if there is only white space ?!? */
159                 saveddelim1 = *delimp1; /* save char after token */
160                 *delimp1 = '\0';                /* delimit token with \0 */
161                 if (id != NULL) 
162                 {
163                     /* now remove trailing white space chars from id */
164                     if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
165                         saveddelim2 = *delimp2;
166                         *delimp2 = '\0';
167                     }
168                     atsign = strrchr(user, '@');
169                     if (atsign) {
170                         *atsign = '\0';
171                         host = atsign + 1;
172
173                     }
174                     for (ctl = hostlist; ctl; ctl = ctl->next) {
175                         if (ctl->server.truename &&
176                             strcasecmp(host, ctl->server.truename) == 0
177                             && strcasecmp(user, ctl->remotename) == 0) {
178         
179                             save_str(&ctl->oldsaved, id, UID_SEEN);
180                             break;
181                         }
182                     }
183                     /* 
184                      * If it's not in a host we're querying,
185                      * save it anyway.  Otherwise we'd lose UIDL
186                      * information any time we queried an explicit
187                      * subset of hosts.
188                      */
189                     if (ctl == (struct query *)NULL) {
190                                 /* restore string */
191                         *delimp1 = saveddelim1;
192                         *atsign = '@';
193                         if (delimp2 != NULL) {
194                             *delimp2 = saveddelim2;
195                         }
196                         save_str(&scratchlist, buf, UID_SEEN);
197                     }
198                 }
199             }
200         }
201         fclose(tmpfp);  /* not checking should be safe, mode was "r" */
202     }
203
204     if (outlevel >= O_DEBUG)
205     {
206         struct idlist   *idp;
207         int uidlcount = 0;
208
209         for (ctl = hostlist; ctl; ctl = ctl->next)
210             if (ctl->server.uidl)
211             {
212                 report_build(stdout, GT_("Old UID list from %s:"), 
213                              ctl->server.pollname);
214                 for (idp = ctl->oldsaved; idp; idp = idp->next)
215                     report_build(stdout, " %s", idp->id);
216                 if (!idp)
217                     report_build(stdout, GT_(" <empty>"));
218                 report_complete(stdout, "\n");
219                 uidlcount++;
220             }
221
222         if (uidlcount)
223         {
224             report_build(stdout, GT_("Scratch list of UIDs:"));
225             for (idp = scratchlist; idp; idp = idp->next)
226                 report_build(stdout, " %s", idp->id);
227             if (!idp)
228                 report_build(stdout, GT_(" <empty>"));
229             report_complete(stdout, "\n");
230         }
231     }
232 }
233 #endif /* POP3_ENABLE */
234
235 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
236 /* save a number/UID pair on the given UID list */
237 {
238     struct idlist **end;
239
240     /* do it nonrecursively so the list is in the right order */
241     for (end = idl; *end; end = &(*end)->next)
242         continue;
243
244     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
245     (*end)->val.status.mark = status;
246     (*end)->id = str ? xstrdup(str) : (char *)NULL;
247     (*end)->next = NULL;
248
249     return(*end);
250 }
251
252 void free_str_list(struct idlist **idl)
253 /* free the given UID list */
254 {
255     if (*idl == (struct idlist *)NULL)
256         return;
257
258     free_str_list(&(*idl)->next);
259     free ((*idl)->id);
260     free(*idl);
261     *idl = (struct idlist *)NULL;
262 }
263
264 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
265 /* save an ID pair on the given list */
266 {
267     struct idlist **end;
268
269     /* do it nonrecursively so the list is in the right order */
270     for (end = idl; *end; end = &(*end)->next)
271         continue;
272
273     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
274     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
275     if (str2)
276         (*end)->val.id2 = xstrdup(str2);
277     else
278         (*end)->val.id2 = (char *)NULL;
279     (*end)->next = (struct idlist *)NULL;
280 }
281
282 #ifdef __UNUSED__
283 void free_str_pair_list(struct idlist **idl)
284 /* free the given ID pair list */
285 {
286     if (*idl == (struct idlist *)NULL)
287         return;
288
289     free_idpair_list(&(*idl)->next);
290     free ((*idl)->id);
291     free ((*idl)->val.id2);
292     free(*idl);
293     *idl = (struct idlist *)NULL;
294 }
295 #endif
296
297 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
298 /* is a given ID in the given list? (comparison may be caseblind) */
299 {
300     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
301         return(0);
302     else if (!caseblind && strcmp(str, (*idl)->id) == 0)
303         return(1);
304     else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
305         return(1);
306     else
307         return(str_in_list(&(*idl)->next, str, caseblind));
308 }
309
310 int str_nr_in_list( struct idlist **idl, const char *str )
311   /* return the position of str in idl */
312 {
313     int nr;
314     struct idlist *walk;
315     if ( !str )
316         return -1;
317     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
318         if( strcmp( str, walk->id) == 0 )
319             return nr;
320     return -1;
321 }
322
323 int str_nr_last_in_list( struct idlist **idl, const char *str)
324 /* return the last position of str in idl */
325 {
326     int nr, ret = -1;
327     struct idlist *walk;
328     if ( !str )
329         return -1;
330     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
331         if( strcmp( str, walk->id) == 0 )
332             ret = nr;
333     return ret;
334 }
335
336 void str_set_mark( struct idlist **idl, const char *str, const flag val)
337 /* update the mark on an of an id to given value */
338 {
339     int nr;
340     struct idlist *walk;
341     if (!str)
342         return;
343     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
344         if (strcmp(str, walk->id) == 0)
345             walk->val.status.mark = val;
346 }
347
348 int count_list( struct idlist **idl)
349 /* count the number of elements in the list */
350 {
351   if( !*idl )
352     return 0;
353   return 1 + count_list( &(*idl)->next );
354 }
355
356 char *str_from_nr_list(struct idlist **idl, int number)
357 /* return the number'th string in idl */
358 {
359     if( !*idl  || number < 0)
360         return 0;
361     if( number == 0 )
362         return (*idl)->id;
363     return str_from_nr_list(&(*idl)->next, number-1);
364 }
365
366     
367 char *str_find(struct idlist **idl, int number)
368 /* return the id of the given number in the given list. */
369 {
370     if (*idl == (struct idlist *) 0)
371         return((char *) 0);
372     else if (number == (*idl)->val.status.num)
373         return((*idl)->id);
374     else
375         return(str_find(&(*idl)->next, number));
376 }
377
378 char *idpair_find(struct idlist **idl, const char *id)
379 /* return the id of the given id in the given list (caseblind comparison) */
380 {
381     if (*idl == (struct idlist *) 0)
382         return((char *) 0);
383     else if (strcasecmp(id, (*idl)->id) == 0)
384         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
385     else
386         return(idpair_find(&(*idl)->next, id));
387 }
388
389 int delete_str(struct idlist **idl, int num)
390 /* delete given message from given list */
391 {
392     struct idlist       *idp;
393
394     for (idp = *idl; idp; idp = idp->next)
395         if (idp->val.status.num == num)
396         {
397             idp->val.status.mark = UID_DELETED;
398             return(1);
399         }
400     return(0);
401 }
402
403 struct idlist *copy_str_list(struct idlist *idl)
404 /* copy the given UID list */
405 {
406     struct idlist *newnode ;
407
408     if (idl == (struct idlist *)NULL)
409         return(NULL);
410     else
411     {
412         newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
413         memcpy(newnode, idl, sizeof(struct idlist));
414         newnode->next = copy_str_list(idl->next);
415         return(newnode);
416     }
417 }
418
419 void append_str_list(struct idlist **idl, struct idlist **nidl)
420 /* append nidl to idl (does not copy *) */
421 {
422     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
423         return;
424     else if ((*idl) == (struct idlist *)NULL)
425         *idl = *nidl;
426     else if ((*idl)->next == (struct idlist *)NULL)
427         (*idl)->next = *nidl;
428     else if ((*idl)->next != *nidl)
429         append_str_list(&(*idl)->next, nidl);
430 }
431
432 #ifdef POP3_ENABLE
433 void expunge_uids(struct query *ctl)
434 /* assert that all UIDs marked deleted have actually been expunged */
435 {
436     struct idlist *idl;
437
438     for (idl = ctl->newsaved; idl; idl = idl->next)
439         if (idl->val.status.mark == UID_DELETED)
440             idl->val.status.mark = UID_EXPUNGED;
441 }
442
443 void uid_swap_lists(struct query *ctl) 
444 /* finish a query */
445 {
446     /* debugging code */
447     if (ctl->server.uidl && outlevel >= O_DEBUG)
448     {
449         struct idlist *idp;
450
451         report_build(stdout, GT_("New UID list from %s:"), ctl->server.pollname);
452         for (idp = ctl->newsaved; idp; idp = idp->next)
453             report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
454         if (!idp)
455             report_build(stdout, GT_(" <empty>"));
456         report_complete(stdout, "\n");
457     }
458
459     /*
460      * Don't swap UID lists unless we've actually seen UIDLs.
461      * This is necessary in order to keep UIDL information
462      * from being heedlessly deleted later on.
463      *
464      * Older versions of fetchmail did
465      *
466      *     free_str_list(&scratchlist);
467      *
468      * after swap.  This was wrong; we need to preserve the UIDL information
469      * from unqueried hosts.  Unfortunately, not doing this means that
470      * under some circumstances UIDLs can end up being stored forever --
471      * specifically, if a user description is removed from .fetchmailrc
472      * with UIDLs from that account in .fetchids, there is no way for
473      * them to ever get garbage-collected.
474      */
475     if (ctl->newsaved)
476     {
477         /* old state of mailbox may now be irrelevant */
478         if (outlevel >= O_DEBUG)
479             report(stdout, GT_("swapping UID lists\n"));
480         free_str_list(&ctl->oldsaved);
481         ctl->oldsaved = ctl->newsaved;
482         ctl->newsaved = (struct idlist *) NULL;
483     }
484     else if (outlevel >= O_DEBUG)
485         report(stdout, GT_("not swapping UID lists, no UIDs seen this query\n"));
486 }
487
488 void write_saved_lists(struct query *hostlist, const char *idfile)
489 /* perform end-of-run write of seen-messages list */
490 {
491     int         idcount;
492     FILE        *tmpfp;
493     struct query *ctl;
494     struct idlist *idp;
495
496     /* if all lists are empty, nuke the file */
497     idcount = 0;
498     for (ctl = hostlist; ctl; ctl = ctl->next) {
499         for (idp = ctl->oldsaved; idp; idp = idp->next)
500             if (idp->val.status.mark == UID_SEEN
501                                 || idp->val.status.mark == UID_DELETED)
502                 idcount++;
503     }
504
505     /* either nuke the file or write updated last-seen IDs */
506     if (!idcount && !scratchlist)
507     {
508         if (outlevel >= O_DEBUG)
509             report(stdout, GT_("Deleting fetchids file.\n"));
510         unlink(idfile);
511     }
512     else
513     {
514         if (outlevel >= O_DEBUG)
515             report(stdout, GT_("Writing fetchids file.\n"));
516         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
517             for (ctl = hostlist; ctl; ctl = ctl->next) {
518                 for (idp = ctl->oldsaved; idp; idp = idp->next)
519                     if (idp->val.status.mark == UID_SEEN
520                                 || idp->val.status.mark == UID_DELETED)
521                         fprintf(tmpfp, "%s@%s %s\n", 
522                             ctl->remotename, ctl->server.truename, idp->id);
523             }
524             for (idp = scratchlist; idp; idp = idp->next)
525                 fputs(idp->id, tmpfp);
526             fclose(tmpfp);
527         }
528     }
529 }
530 #endif /* POP3_ENABLE */
531
532 /* uid.c ends here */