2 * uid.c -- UIDL handling for POP3 servers without LAST
4 * For license terms, see the file COPYING in this directory.
13 #if defined(STDC_HEADERS)
17 #if defined(HAVE_UNISTD_H)
21 #include "fetchmail.h"
25 * Machinery for handling UID lists live here. This is mainly to support
26 * RFC1725/RFC1939-conformant POP3 servers without a LAST command, but may also
27 * be useful for making the IMAP4 querying logic UID-oriented, if a future
28 * revision of IMAP forces me to.
30 * These functions are also used by the rest of the code to maintain
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
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.
48 * Each time a message is fetched, we can check its UID against the
49 * `oldsaved' list to see if it is old.
51 * Each time a message-id is seen, we mark it with MARK_SEEN.
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.
58 * At the end of the query, the `newsaved' member becomes the
59 * `oldsaved' list. The old `oldsaved' list is freed.
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.
66 * Note: some comparisons (those used for DNS address lists) are caseblind!
69 /* UIDs associated with un-queried hosts */
70 static struct idlist *scratchlist;
73 void initialize_saved_lists(struct query *hostlist, const char *idfile)
74 /* read file of saved IDs and attach to each host */
80 /* make sure lists are initially empty */
81 for (ctl = hostlist; ctl; ctl = ctl->next) {
82 ctl->skipped = (struct idlist *)NULL;
83 ctl->oldsaved = (struct idlist *)NULL;
84 ctl->newsaved = (struct idlist *)NULL;
85 ctl->oldsavedend = &ctl->oldsaved;
91 * Croak if the uidl directory does not exist.
92 * This probably means an NFS mount failed and we can't
93 * see a uidl file that ought to be there.
94 * Question: is this a portable check? It's not clear
95 * that all implementations of lstat() will return ENOTDIR
96 * rather than plain ENOENT in this case...
98 if (lstat(idfile, &statbuf) < 0) {
101 report(stderr, GT_("lstat: %s: %s\n"), idfile, strerror(errno));
106 /* let's get stored message UIDs from previous queries */
107 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
109 char buf[POPBUFSIZE+1];
110 char *host = NULL; /* pacify -Wall */
113 char *atsign; /* temp pointer used in parsing user and host */
117 char saveddelim2 = '\0'; /* pacify -Wall */
119 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
122 * At this point, we assume the bug has two fields -- a user@host
123 * part, and an ID part. Either field may contain spurious @ signs.
124 * The previous version of this code presumed one could split at
125 * the rightmost '@'. This is not correct, as InterMail puts an
129 /* first, skip leading spaces */
130 user = buf + strspn(buf, " \t");
133 * First, we split the buf into a userhost part and an id
134 * part ... but id doesn't necessarily start with a '<',
135 * espescially if the POP server returns an X-UIDL header
136 * instead of a Message-ID, as GMX's (www.gmx.net) POP3
137 * StreamProxy V1.0 does.
139 if ((id = strchr(user, ' ')) != NULL )
143 * this is one other trick. The userhost part
144 * may contain ' ' in the user part, at least in
145 * the lotus notes case.
146 * So we start looking for the '@' after which the
147 * host will follow with the ' ' seperator finaly id.
149 delimp1 = strchr(user, '@');
150 id = strchr(delimp1,' ');
151 for (delimp1 = id; delimp1 >= user; delimp1--)
152 if ((*delimp1 != ' ') && (*delimp1 != '\t'))
156 * It should be safe to assume that id starts after
157 * the " " - after all, we're writing the " "
158 * ourselves in write_saved_lists() :-)
160 id = id + strspn(id, " ");
162 delimp1++; /* but what if there is only white space ?!? */
163 saveddelim1 = *delimp1; /* save char after token */
164 *delimp1 = '\0'; /* delimit token with \0 */
167 /* now remove trailing white space chars from id */
168 if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
169 saveddelim2 = *delimp2;
172 atsign = strrchr(user, '@');
178 for (ctl = hostlist; ctl; ctl = ctl->next) {
179 if (strcasecmp(host, ctl->server.queryname) == 0
180 && strcasecmp(user, ctl->remotename) == 0) {
182 save_str(&ctl->oldsaved, id, UID_SEEN);
187 * If it's not in a host we're querying,
188 * save it anyway. Otherwise we'd lose UIDL
189 * information any time we queried an explicit
192 if (ctl == (struct query *)NULL) {
194 *delimp1 = saveddelim1;
196 if (delimp2 != NULL) {
197 *delimp2 = saveddelim2;
199 save_str(&scratchlist, buf, UID_SEEN);
204 fclose(tmpfp); /* not checking should be safe, mode was "r" */
207 if (outlevel >= O_DEBUG)
212 for (ctl = hostlist; ctl; ctl = ctl->next)
213 if (ctl->server.uidl)
215 report_build(stdout, GT_("Old UID list from %s:"),
216 ctl->server.pollname);
217 for (idp = ctl->oldsaved; idp; idp = idp->next)
218 report_build(stdout, " %s", idp->id);
220 report_build(stdout, GT_(" <empty>"));
221 report_complete(stdout, "\n");
227 report_build(stdout, GT_("Scratch list of UIDs:"));
228 for (idp = scratchlist; idp; idp = idp->next)
229 report_build(stdout, " %s", idp->id);
231 report_build(stdout, GT_(" <empty>"));
232 report_complete(stdout, "\n");
236 #endif /* POP3_ENABLE */
238 /* return a pointer to the last element of the list to help the quick,
239 * constant-time addition to the list, NOTE: this function does not dup
240 * the string, the caller must do that. */
241 /*@shared@*/ struct idlist **save_str_quick(/*@shared@*/ struct idlist **idl,
242 /*@only@*/ char *str, flag status)
243 /* save a number/UID pair on the given UID list */
247 /* do it nonrecursively so the list is in the right order */
248 for (end = idl; *end; end = &(*end)->next)
251 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
252 (*end)->val.status.mark = status;
253 (*end)->id = (unsigned char *)str;
259 /* return the end list element for direct modification */
260 struct idlist *save_str(struct idlist **idl, const char *str, flag st)
262 return *save_str_quick(idl, str ? xstrdup(str) : NULL,
266 void free_str_list(struct idlist **idl)
267 /* free the given UID list */
269 if (*idl == (struct idlist *)NULL)
272 free_str_list(&(*idl)->next);
275 *idl = (struct idlist *)NULL;
278 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
279 /* save an ID pair on the given list */
283 /* do it nonrecursively so the list is in the right order */
284 for (end = idl; *end; end = &(*end)->next)
287 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
288 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
290 (*end)->val.id2 = xstrdup(str2);
292 (*end)->val.id2 = (char *)NULL;
293 (*end)->next = (struct idlist *)NULL;
297 void free_str_pair_list(struct idlist **idl)
298 /* free the given ID pair list */
300 if (*idl == (struct idlist *)NULL)
303 free_idpair_list(&(*idl)->next);
305 free ((*idl)->val.id2);
307 *idl = (struct idlist *)NULL;
311 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
312 /* is a given ID in the given list? (comparison may be caseblind) */
316 for( walk = *idl; walk; walk = walk->next )
317 if( strcasecmp( str, (char *)walk->id) == 0 )
320 for( walk = *idl; walk; walk = walk->next )
321 if( strcmp( str, (char *)walk->id) == 0 )
327 int str_nr_in_list( struct idlist **idl, const char *str )
328 /* return the position of str in idl */
334 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
335 if( strcmp( str, walk->id) == 0 )
340 int str_nr_last_in_list( struct idlist **idl, const char *str)
341 /* return the last position of str in idl */
347 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
348 if( strcmp( str, walk->id) == 0 )
353 void str_set_mark( struct idlist **idl, const char *str, const flag val)
354 /* update the mark on an of an id to given value */
360 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
361 if (strcmp(str, walk->id) == 0)
362 walk->val.status.mark = val;
365 int count_list( struct idlist **idl)
366 /* count the number of elements in the list */
370 return 1 + count_list( &(*idl)->next );
373 /*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
374 /* return the number'th string in idl */
376 if( !*idl || number < 0)
380 return str_from_nr_list(&(*idl)->next, number-1);
384 char *str_find(struct idlist **idl, long number)
385 /* return the id of the given number in the given list. */
387 if (*idl == (struct idlist *) 0)
389 else if (number == (*idl)->val.status.num)
392 return(str_find(&(*idl)->next, number));
395 char *idpair_find(struct idlist **idl, const char *id)
396 /* return the id of the given id in the given list (caseblind comparison) */
398 if (*idl == (struct idlist *) 0)
400 else if (strcasecmp(id, (*idl)->id) == 0)
401 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
403 return(idpair_find(&(*idl)->next, id));
406 int delete_str(struct idlist **idl, long num)
407 /* delete given message from given list */
411 for (idp = *idl; idp; idp = idp->next)
412 if (idp->val.status.num == num)
414 idp->val.status.mark = UID_DELETED;
420 struct idlist *copy_str_list(struct idlist *idl)
421 /* copy the given UID list */
423 struct idlist *newnode ;
425 if (idl == (struct idlist *)NULL)
429 newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
430 memcpy(newnode, idl, sizeof(struct idlist));
431 newnode->next = copy_str_list(idl->next);
436 void append_str_list(struct idlist **idl, struct idlist **nidl)
437 /* append nidl to idl (does not copy *) */
439 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
441 else if ((*idl) == (struct idlist *)NULL)
443 else if ((*idl)->next == (struct idlist *)NULL)
444 (*idl)->next = *nidl;
445 else if ((*idl)->next != *nidl)
446 append_str_list(&(*idl)->next, nidl);
450 void expunge_uids(struct query *ctl)
451 /* assert that all UIDs marked deleted have actually been expunged */
455 for (idl = ctl->newsaved; idl; idl = idl->next)
456 if (idl->val.status.mark == UID_DELETED)
457 idl->val.status.mark = UID_EXPUNGED;
460 void uid_swap_lists(struct query *ctl)
464 if (ctl->server.uidl && outlevel >= O_DEBUG)
468 report_build(stdout, GT_("New UID list from %s:"), ctl->server.pollname);
469 for (idp = ctl->newsaved; idp; idp = idp->next)
470 report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
472 report_build(stdout, GT_(" <empty>"));
473 report_complete(stdout, "\n");
477 * Don't swap UID lists unless we've actually seen UIDLs.
478 * This is necessary in order to keep UIDL information
479 * from being heedlessly deleted later on.
481 * Older versions of fetchmail did
483 * free_str_list(&scratchlist);
485 * after swap. This was wrong; we need to preserve the UIDL information
486 * from unqueried hosts. Unfortunately, not doing this means that
487 * under some circumstances UIDLs can end up being stored forever --
488 * specifically, if a user description is removed from .fetchmailrc
489 * with UIDLs from that account in .fetchids, there is no way for
490 * them to ever get garbage-collected.
494 /* old state of mailbox may now be irrelevant */
495 if (outlevel >= O_DEBUG)
496 report(stdout, GT_("swapping UID lists\n"));
497 free_str_list(&ctl->oldsaved);
498 ctl->oldsaved = ctl->newsaved;
499 ctl->newsaved = (struct idlist *) NULL;
501 else if (outlevel >= O_DEBUG)
502 report(stdout, GT_("not swapping UID lists, no UIDs seen this query\n"));
505 void write_saved_lists(struct query *hostlist, const char *idfile)
506 /* perform end-of-run write of seen-messages list */
513 /* if all lists are empty, nuke the file */
515 for (ctl = hostlist; ctl; ctl = ctl->next) {
516 for (idp = ctl->oldsaved; idp; idp = idp->next)
517 if (idp->val.status.mark == UID_SEEN
518 || idp->val.status.mark == UID_DELETED)
522 /* either nuke the file or write updated last-seen IDs */
523 if (!idcount && !scratchlist)
525 if (outlevel >= O_DEBUG)
526 report(stdout, GT_("Deleting fetchids file.\n"));
531 if (outlevel >= O_DEBUG)
532 report(stdout, GT_("Writing fetchids file.\n"));
533 /* FIXME: do not overwrite the old idfile */
534 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
535 for (ctl = hostlist; ctl; ctl = ctl->next) {
536 for (idp = ctl->oldsaved; idp; idp = idp->next)
537 if (idp->val.status.mark == UID_SEEN
538 || idp->val.status.mark == UID_DELETED)
539 fprintf(tmpfp, "%s@%s %s\n",
540 ctl->remotename, ctl->server.queryname, idp->id);
542 for (idp = scratchlist; idp; idp = idp->next)
543 fputs(idp->id, tmpfp);
548 #endif /* POP3_ENABLE */
550 /* uid.c ends here */