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-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.
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 = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
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...
94 if (lstat(idfile, &statbuf) < 0) {
97 report(stderr, _("lstat: %s: %s\n"), idfile, strerror(errno));
102 /* let's get stored message UIDs from previous queries */
103 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
105 char buf[POPBUFSIZE+1];
106 char *host = NULL; /* pacify -Wall */
109 char *atsign; /* temp pointer used in parsing user and host */
113 char saveddelim2 = '\0'; /* pacify -Wall */
115 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
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
125 /* first, skip leading spaces */
126 user = buf + strspn(buf, " \t");
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.
135 if ((id = strchr(user, ' ')) != NULL )
137 for (delimp1 = id; delimp1 >= user; delimp1--)
138 if ((*delimp1 != ' ') && (*delimp1 != '\t'))
142 * It should be safe to assume that id starts after
143 * the " " - after all, we're writing the " "
144 * ourselves in write_saved_lists() :-)
146 id = id + strspn(id, " ");
148 delimp1++; /* but what if there is only white space ?!? */
149 saveddelim1 = *delimp1; /* save char after token */
150 *delimp1 = '\0'; /* delimit token with \0 */
153 /* now remove trailing white space chars from id */
154 if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
155 saveddelim2 = *delimp2;
158 atsign = strrchr(user, '@');
164 for (ctl = hostlist; ctl; ctl = ctl->next) {
165 if (ctl->server.truename &&
166 strcasecmp(host, ctl->server.truename) == 0
167 && strcasecmp(user, ctl->remotename) == 0) {
169 save_str(&ctl->oldsaved, id, UID_SEEN);
173 /* if it's not in a host we're querying,
175 if (ctl == (struct query *)NULL) {
177 *delimp1 = saveddelim1;
179 if (delimp2 != NULL) {
180 *delimp2 = saveddelim2;
182 save_str(&scratchlist, buf, UID_SEEN);
187 fclose(tmpfp); /* not checking should be safe, mode was "r" */
190 if (outlevel >= O_DEBUG)
195 for (ctl = hostlist; ctl; ctl = ctl->next)
196 if (ctl->server.uidl)
198 report_build(stdout, _("Old UID list from %s:"),
199 ctl->server.pollname);
200 for (idp = ctl->oldsaved; idp; idp = idp->next)
201 report_build(stdout, " %s", idp->id);
203 report_build(stdout, _(" <empty>"));
204 report_complete(stdout, "\n");
210 report_build(stdout, _("Scratch list of UIDs:"));
211 for (idp = scratchlist; idp; idp = idp->next)
212 report_build(stdout, " %s", idp->id);
214 report_build(stdout, _(" <empty>"));
215 report_complete(stdout, "\n");
219 #endif /* POP3_ENABLE */
221 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
222 /* save a number/UID pair on the given UID list */
226 /* do it nonrecursively so the list is in the right order */
227 for (end = idl; *end; end = &(*end)->next)
230 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
231 (*end)->val.status.mark = status;
232 (*end)->id = str ? xstrdup(str) : (char *)NULL;
238 void free_str_list(struct idlist **idl)
239 /* free the given UID list */
241 if (*idl == (struct idlist *)NULL)
244 free_str_list(&(*idl)->next);
247 *idl = (struct idlist *)NULL;
250 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
251 /* save an ID pair on the given list */
255 /* do it nonrecursively so the list is in the right order */
256 for (end = idl; *end; end = &(*end)->next)
259 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
260 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
262 (*end)->val.id2 = xstrdup(str2);
264 (*end)->val.id2 = (char *)NULL;
265 (*end)->next = (struct idlist *)NULL;
269 void free_str_pair_list(struct idlist **idl)
270 /* free the given ID pair list */
272 if (*idl == (struct idlist *)NULL)
275 free_idpair_list(&(*idl)->next);
277 free ((*idl)->val.id2);
279 *idl = (struct idlist *)NULL;
283 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
284 /* is a given ID in the given list? (comparison may be caseblind) */
286 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
288 else if (!caseblind && strcmp(str, (*idl)->id) == 0)
290 else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
293 return(str_in_list(&(*idl)->next, str, caseblind));
296 int str_nr_in_list( struct idlist **idl, const char *str )
297 /* return the position of str in idl */
303 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
304 if( strcmp( str, walk->id) == 0 )
309 int str_nr_last_in_list( struct idlist **idl, const char *str)
310 /* return the last position of str in idl */
316 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
317 if( strcmp( str, walk->id) == 0 )
322 void str_set_mark( struct idlist **idl, const char *str, const flag val)
323 /* update the mark on an of an id to given value */
329 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
330 if (strcmp(str, walk->id) == 0)
331 walk->val.status.mark = val;
334 int count_list( struct idlist **idl)
335 /* count the number of elements in the list */
339 return 1 + count_list( &(*idl)->next );
342 char *str_from_nr_list(struct idlist **idl, int number)
343 /* return the number'th string in idl */
345 if( !*idl || number < 0)
349 return str_from_nr_list(&(*idl)->next, number-1);
353 char *str_find(struct idlist **idl, int number)
354 /* return the id of the given number in the given list. */
356 if (*idl == (struct idlist *) 0)
358 else if (number == (*idl)->val.status.num)
361 return(str_find(&(*idl)->next, number));
364 char *idpair_find(struct idlist **idl, const char *id)
365 /* return the id of the given id in the given list (caseblind comparison) */
367 if (*idl == (struct idlist *) 0)
369 else if (strcasecmp(id, (*idl)->id) == 0)
370 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
372 return(idpair_find(&(*idl)->next, id));
375 int delete_str(struct idlist **idl, int num)
376 /* delete given message from given list */
380 for (idp = *idl; idp; idp = idp->next)
381 if (idp->val.status.num == num)
383 idp->val.status.mark = UID_DELETED;
389 struct idlist *copy_str_list(struct idlist *idl)
390 /* copy the given UID list */
392 struct idlist *newnode ;
394 if (idl == (struct idlist *)NULL)
398 newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
399 memcpy(newnode, idl, sizeof(struct idlist));
400 newnode->next = copy_str_list(idl->next);
405 void append_str_list(struct idlist **idl, struct idlist **nidl)
406 /* append nidl to idl (does not copy *) */
408 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
410 else if ((*idl) == (struct idlist *)NULL)
412 else if ((*idl)->next == (struct idlist *)NULL)
413 (*idl)->next = *nidl;
414 else if ((*idl)->next != *nidl)
415 append_str_list(&(*idl)->next, nidl);
419 void expunge_uids(struct query *ctl)
420 /* assert that all UIDs marked deleted have actually been expunged */
424 for (idl = ctl->newsaved; idl; idl = idl->next)
425 if (idl->val.status.mark == UID_DELETED)
426 idl->val.status.mark = UID_EXPUNGED;
429 void uid_swap_lists(struct query *ctl)
433 if (ctl->server.uidl && outlevel >= O_DEBUG)
437 report_build(stdout, _("New UID list from %s:"), ctl->server.pollname);
438 for (idp = ctl->newsaved; idp; idp = idp->next)
439 report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
441 report_build(stdout, _(" <empty>"));
442 report_complete(stdout, "\n");
446 * Don't swap UID lists unless we've actually seen UIDLs.
447 * This is necessary in order to keep UIDL information
448 * from being heedlessly deleted later on.
452 /* old state of mailbox may now be irrelevant */
453 if (outlevel >= O_DEBUG)
454 report(stdout, _("swapping UID lists\n"));
455 free_str_list(&ctl->oldsaved);
456 free_str_list(&scratchlist);
457 ctl->oldsaved = ctl->newsaved;
458 ctl->newsaved = (struct idlist *) NULL;
460 else if (outlevel >= O_DEBUG)
461 report(stdout, _("not swapping UID lists, no UIDs seen this query\n"));
464 void write_saved_lists(struct query *hostlist, const char *idfile)
465 /* perform end-of-run write of seen-messages list */
472 /* if all lists are empty, nuke the file */
474 for (ctl = hostlist; ctl; ctl = ctl->next) {
475 for (idp = ctl->oldsaved; idp; idp = idp->next)
476 if (idp->val.status.mark == UID_SEEN
477 || idp->val.status.mark == UID_DELETED)
481 /* either nuke the file or write updated last-seen IDs */
482 if (!idcount && !scratchlist)
484 if (outlevel >= O_DEBUG)
485 report(stdout, _("Deleting fetchids file.\n"));
490 if (outlevel >= O_DEBUG)
491 report(stdout, _("Writing fetchids file.\n"));
492 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
493 for (ctl = hostlist; ctl; ctl = ctl->next) {
494 for (idp = ctl->oldsaved; idp; idp = idp->next)
495 if (idp->val.status.mark == UID_SEEN
496 || idp->val.status.mark == UID_DELETED)
497 fprintf(tmpfp, "%s@%s %s\n",
498 ctl->remotename, ctl->server.truename, idp->id);
500 for (idp = scratchlist; idp; idp = idp->next)
501 fputs(idp->id, tmpfp);
506 #endif /* POP3_ENABLE */
508 /* uid.c ends here */