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"
24 * Machinery for handling UID lists live here. This is mainly to support
25 * RFC1725-conformant POP3 servers without a LAST command, but may also be
26 * useful for making the IMAP4 querying logic UID-oriented, if a future
27 * revision of IMAP forces me to.
29 * These functions are also used by the rest of the code to maintain
34 * At start of a query, we have a (possibly empty) list of UIDs to be
35 * considered seen in `oldsaved'. These are messages that were left in
36 * the mailbox and *not deleted* on previous queries (we don't need to
37 * remember the UIDs of deleted messages because ... well, they're gone!)
38 * This list is initially set up by initialize_saved_list() from the
41 * Early in the query, during the execution of the protocol-specific
42 * getrange code, the driver expects that the host's `newsaved' member
43 * will be filled with a list of UIDs and message numbers representing
44 * the mailbox state. If this list is empty, the server did
45 * not respond to the request for a UID listing.
47 * Each time a message is fetched, we can check its UID against the
48 * `oldsaved' list to see if it is old.
50 * Each time a message-id is seen, we mark it with MARK_SEEN.
52 * Each time a message is deleted, we mark its id UID_DELETED in the
53 * `newsaved' member. When we want to assert that an expunge has been
54 * done on the server, we call expunge_uid() to register that all
55 * deleted messages are gone by marking them UID_EXPUNGED.
57 * At the end of the query, the `newsaved' member becomes the
58 * `oldsaved' list. The old `oldsaved' list is freed.
60 * At the end of the fetchmail run, seen and non-EXPUNGED members of all
61 * current `oldsaved' lists are flushed out to the .fetchids file to
62 * be picked up by the next run. If there are no un-expunged
63 * messages, the file is deleted.
65 * Note: some comparisons (those used for DNS address lists) are caseblind!
68 /* UIDs associated with un-queried hosts */
69 static struct idlist *scratchlist;
72 void initialize_saved_lists(struct query *hostlist, const char *idfile)
73 /* read file of saved IDs and attach to each host */
79 /* make sure lists are initially empty */
80 for (ctl = hostlist; ctl; ctl = ctl->next)
81 ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
86 * Croak if the uidl directory does not exist.
87 * This probably means an NFS mount failed and we can't
88 * see a uidl file that ought to be there.
89 * Question: is this a portable check? It's not clear
90 * that all implementations of lstat() will return ENOTDIR
91 * rather than plain ENOENT in this case...
93 if (lstat(idfile, &statbuf) < 0) {
96 report(stderr, "lstat: %s: %s\n", idfile, strerror(errno));
101 /* let's get stored message UIDs from previous queries */
102 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
104 char buf[POPBUFSIZE+1];
105 char *host = NULL; /* pacify -Wall */
108 char *atsign; /* temp pointer used in parsing user and host */
112 char saveddelim2 = '\0'; /* pacify -Wall */
114 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
117 * At this point, we assume the bug has two fields -- a user@host
118 * part, and an ID part. Either field may contain spurious @ signs.
119 * The previous version of this code presumed one could split at
120 * the rightmost '@'. This is not correct, as InterMail puts an
124 /* first, skip leading spaces */
125 user = buf + strspn(buf, " \t");
128 * First, we split the buf into a userhost part and an id
129 * part ... but id doesn't necessarily start with a '<',
130 * espescially if the POP server returns an X-UIDL header
131 * instead of a Message-ID, as GMX's (www.gmx.net) POP3
132 * StreamProxy V1.0 does.
134 if ((id = strchr(user, ' ')) != NULL )
136 for (delimp1 = id; delimp1 >= user; delimp1--)
137 if ((*delimp1 != ' ') && (*delimp1 != '\t'))
141 * It should be safe to assume that id starts after
142 * the " " - after all, we're writing the " "
143 * ourselves in write_saved_lists() :-)
145 id = id + strspn(id, " ");
147 delimp1++; /* but what if there is only white space ?!? */
148 saveddelim1 = *delimp1; /* save char after token */
149 *delimp1 = '\0'; /* delimit token with \0 */
152 /* now remove trailing white space chars from id */
153 if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
154 saveddelim2 = *delimp2;
157 atsign = strrchr(user, '@');
163 for (ctl = hostlist; ctl; ctl = ctl->next) {
164 if (ctl->server.truename &&
165 strcasecmp(host, ctl->server.truename) == 0
166 && strcasecmp(user, ctl->remotename) == 0) {
168 save_str(&ctl->oldsaved, id, UID_SEEN);
172 /* if it's not in a host we're querying,
174 if (ctl == (struct query *)NULL) {
176 *delimp1 = saveddelim1;
178 if (delimp2 != NULL) {
179 *delimp2 = saveddelim2;
181 save_str(&scratchlist, buf, UID_SEEN);
186 fclose(tmpfp); /* not checking should be safe, mode was "r" */
189 if (outlevel >= O_DEBUG)
194 for (ctl = hostlist; ctl; ctl = ctl->next)
195 if (ctl->server.uidl)
197 report_build(stdout, "Old UID list from %s:",ctl->server.pollname);
198 for (idp = ctl->oldsaved; idp; idp = idp->next)
199 report_build(stdout, " %s", idp->id);
201 report_build(stdout, " <empty>");
202 report_complete(stdout, "\n");
208 report_build(stdout, "Scratch list of UIDs:");
209 for (idp = scratchlist; idp; idp = idp->next)
210 report_build(stdout, " %s", idp->id);
212 report_build(stdout, " <empty>");
213 report_complete(stdout, "\n");
217 #endif /* POP3_ENABLE */
219 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
220 /* save a number/UID pair on the given UID list */
224 /* do it nonrecursively so the list is in the right order */
225 for (end = idl; *end; end = &(*end)->next)
228 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
229 (*end)->val.status.mark = status;
230 (*end)->id = str ? xstrdup(str) : (char *)NULL;
236 void free_str_list(struct idlist **idl)
237 /* free the given UID list */
239 if (*idl == (struct idlist *)NULL)
242 free_str_list(&(*idl)->next);
245 *idl = (struct idlist *)NULL;
248 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
249 /* save an ID pair on the given list */
253 /* do it nonrecursively so the list is in the right order */
254 for (end = idl; *end; end = &(*end)->next)
257 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
258 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
260 (*end)->val.id2 = xstrdup(str2);
262 (*end)->val.id2 = (char *)NULL;
263 (*end)->next = (struct idlist *)NULL;
267 void free_str_pair_list(struct idlist **idl)
268 /* free the given ID pair list */
270 if (*idl == (struct idlist *)NULL)
273 free_idpair_list(&(*idl)->next);
275 free ((*idl)->val.id2);
277 *idl = (struct idlist *)NULL;
281 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
282 /* is a given ID in the given list? (comparison may be caseblind) */
284 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
286 else if (!caseblind && strcmp(str, (*idl)->id) == 0)
288 else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
291 return(str_in_list(&(*idl)->next, str, caseblind));
294 int str_nr_in_list( struct idlist **idl, const char *str )
295 /* return the position of str in idl */
301 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
302 if( strcmp( str, walk->id) == 0 )
307 int str_nr_last_in_list( struct idlist **idl, const char *str)
308 /* return the last position of str in idl */
314 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
315 if( strcmp( str, walk->id) == 0 )
320 void str_set_mark( struct idlist **idl, const char *str, const flag val)
321 /* update the mark on an of an id to given value */
327 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
328 if (strcmp(str, walk->id) == 0)
329 walk->val.status.mark = val;
332 int count_list( struct idlist **idl)
333 /* count the number of elements in the list */
337 return 1 + count_list( &(*idl)->next );
340 char *str_from_nr_list(struct idlist **idl, int number)
341 /* return the number'th string in idl */
343 if( !*idl || number < 0)
347 return str_from_nr_list(&(*idl)->next, number-1);
351 char *str_find(struct idlist **idl, int number)
352 /* return the id of the given number in the given list. */
354 if (*idl == (struct idlist *) 0)
356 else if (number == (*idl)->val.status.num)
359 return(str_find(&(*idl)->next, number));
362 char *idpair_find(struct idlist **idl, const char *id)
363 /* return the id of the given id in the given list (caseblind comparison) */
365 if (*idl == (struct idlist *) 0)
367 else if (strcasecmp(id, (*idl)->id) == 0)
368 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
370 return(idpair_find(&(*idl)->next, id));
373 int delete_str(struct idlist **idl, int num)
374 /* delete given message from given list */
378 for (idp = *idl; idp; idp = idp->next)
379 if (idp->val.status.num == num)
381 idp->val.status.mark = UID_DELETED;
387 struct idlist *copy_str_list(struct idlist *idl)
388 /* copy the given UID list */
390 struct idlist *newnode ;
392 if (idl == (struct idlist *)NULL)
396 newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
397 memcpy(newnode, idl, sizeof(struct idlist));
398 newnode->next = copy_str_list(idl->next);
403 void append_str_list(struct idlist **idl, struct idlist **nidl)
404 /* append nidl to idl (does not copy *) */
406 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
408 else if ((*idl) == (struct idlist *)NULL)
410 else if ((*idl)->next == (struct idlist *)NULL)
411 (*idl)->next = *nidl;
412 else if ((*idl)->next != *nidl)
413 append_str_list(&(*idl)->next, nidl);
417 void expunge_uids(struct query *ctl)
418 /* assert that all UIDs marked deleted have actually been expunged */
422 for (idl = ctl->newsaved; idl; idl = idl->next)
423 if (idl->val.status.mark == UID_DELETED)
424 idl->val.status.mark = UID_EXPUNGED;
427 void uid_swap_lists(struct query *ctl)
431 if (ctl->server.uidl && outlevel >= O_DEBUG)
435 report_build(stdout, "New UID list from %s:", ctl->server.pollname);
436 for (idp = ctl->newsaved; idp; idp = idp->next)
437 report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
439 report_build(stdout, " <empty>");
440 report_complete(stdout, "\n");
444 * Don't swap UID lists unless we've actually seen UIDLs.
445 * This is necessary in order to keep UIDL information
446 * from being heedlessly deleted later on.
450 /* old state of mailbox may now be irrelevant */
451 if (outlevel >= O_DEBUG)
452 report(stdout, "swapping UID lists\n");
453 free_str_list(&ctl->oldsaved);
454 free_str_list(&scratchlist);
455 ctl->oldsaved = ctl->newsaved;
456 ctl->newsaved = (struct idlist *) NULL;
458 else if (outlevel >= O_DEBUG)
459 report(stdout, "not swapping UID lists, no UIDs seen this query\n");
462 void write_saved_lists(struct query *hostlist, const char *idfile)
463 /* perform end-of-run write of seen-messages list */
470 /* if all lists are empty, nuke the file */
472 for (ctl = hostlist; ctl; ctl = ctl->next) {
473 for (idp = ctl->oldsaved; idp; idp = idp->next)
474 if (idp->val.status.mark == UID_SEEN
475 || idp->val.status.mark == UID_DELETED)
479 /* either nuke the file or write updated last-seen IDs */
480 if (!idcount && !scratchlist)
482 if (outlevel >= O_DEBUG)
483 report(stdout, "Deleting fetchids file.\n");
488 if (outlevel >= O_DEBUG)
489 report(stdout, "Writing fetchids file.\n");
490 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
491 for (ctl = hostlist; ctl; ctl = ctl->next) {
492 for (idp = ctl->oldsaved; idp; idp = idp->next)
493 if (idp->val.status.mark == UID_SEEN
494 || idp->val.status.mark == UID_DELETED)
495 fprintf(tmpfp, "%s@%s %s\n",
496 ctl->remotename, ctl->server.truename, idp->id);
498 for (idp = scratchlist; idp; idp = idp->next)
499 fputs(idp->id, tmpfp);
504 #endif /* POP3_ENABLE */
506 /* uid.c ends here */