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 /* very first, skip leading spaces */
125 user = buf + strspn(buf, " \t");
127 /* First, we split the buf into a userhost part and an id part */
128 if ( (delimp1 = strpbrk(user, " \t")) != NULL ) {
129 id = delimp1 + strspn(delimp1, " \t"); /* set pointer to id */
130 saveddelim1 = *delimp1; /* save char after token */
131 *delimp1 = '\0'; /* delimit token with \0 */
133 /* now remove trainling white space chars from id */
134 if ( (delimp2 = strpbrk(id, " \t\n")) != NULL ) {
135 saveddelim2 = *delimp2;
138 atsign = strrchr(user, '@');
144 for (ctl = hostlist; ctl; ctl = ctl->next) {
145 if (ctl->server.truename &&
146 strcasecmp(host, ctl->server.truename) == 0
147 && strcasecmp(user, ctl->remotename) == 0) {
149 save_str(&ctl->oldsaved, id, UID_SEEN);
153 /* if it's not in a host we're querying,
155 if (ctl == (struct query *)NULL) {
157 *delimp1 = saveddelim1;
159 if (delimp2 != NULL) {
160 *delimp2 = saveddelim2;
162 save_str(&scratchlist, buf, UID_SEEN);
170 if (outlevel >= O_DEBUG)
175 for (ctl = hostlist; ctl; ctl = ctl->next)
176 if (ctl->server.uidl)
178 report_build(stdout, "Old UID list from %s:",ctl->server.pollname);
179 for (idp = ctl->oldsaved; idp; idp = idp->next)
180 report_build(stdout, " %s", idp->id);
182 report_build(stdout, " <empty>");
183 report_complete(stdout, "\n");
189 report_build(stdout, "Scratch list of UIDs:");
190 for (idp = scratchlist; idp; idp = idp->next)
191 report_build(stdout, " %s", idp->id);
193 report_build(stdout, " <empty>");
194 report_complete(stdout, "\n");
198 #endif /* POP3_ENABLE */
200 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
201 /* save a number/UID pair on the given UID list */
205 /* do it nonrecursively so the list is in the right order */
206 for (end = idl; *end; end = &(*end)->next)
209 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
210 (*end)->val.status.mark = status;
211 (*end)->id = str ? xstrdup(str) : (char *)NULL;
217 void free_str_list(struct idlist **idl)
218 /* free the given UID list */
220 if (*idl == (struct idlist *)NULL)
223 free_str_list(&(*idl)->next);
226 *idl = (struct idlist *)NULL;
229 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
230 /* save an ID pair on the given list */
234 /* do it nonrecursively so the list is in the right order */
235 for (end = idl; *end; end = &(*end)->next)
238 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
239 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
241 (*end)->val.id2 = xstrdup(str2);
243 (*end)->val.id2 = (char *)NULL;
244 (*end)->next = (struct idlist *)NULL;
248 void free_str_pair_list(struct idlist **idl)
249 /* free the given ID pair list */
251 if (*idl == (struct idlist *)NULL)
254 free_idpair_list(&(*idl)->next);
256 free ((*idl)->val.id2);
258 *idl = (struct idlist *)NULL;
262 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
263 /* is a given ID in the given list? (comparison may be caseblind) */
265 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
267 else if (!caseblind && strcmp(str, (*idl)->id) == 0)
269 else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
272 return(str_in_list(&(*idl)->next, str, caseblind));
275 int str_nr_in_list( struct idlist **idl, const char *str )
276 /* return the position of str in idl */
282 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
283 if( strcmp( str, walk->id) == 0 )
288 int str_nr_last_in_list( struct idlist **idl, const char *str)
289 /* return the last position of str in idl */
295 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
296 if( strcmp( str, walk->id) == 0 )
301 void str_set_mark( struct idlist **idl, const char *str, const flag val)
302 /* update the mark on an of an id to given value */
308 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
309 if (strcmp(str, walk->id) == 0)
310 walk->val.status.mark = val;
313 int count_list( struct idlist **idl)
314 /* count the number of elements in the list */
318 return 1 + count_list( &(*idl)->next );
321 char *str_from_nr_list(struct idlist **idl, int number)
322 /* return the number'th string in idl */
324 if( !*idl || number < 0)
328 return str_from_nr_list(&(*idl)->next, number-1);
332 char *str_find(struct idlist **idl, int number)
333 /* return the id of the given number in the given list. */
335 if (*idl == (struct idlist *) 0)
337 else if (number == (*idl)->val.status.num)
340 return(str_find(&(*idl)->next, number));
343 char *idpair_find(struct idlist **idl, const char *id)
344 /* return the id of the given id in the given list (caseblind comparison) */
346 if (*idl == (struct idlist *) 0)
348 else if (strcasecmp(id, (*idl)->id) == 0)
349 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
351 return(idpair_find(&(*idl)->next, id));
354 int delete_str(struct idlist **idl, int num)
355 /* delete given message from given list */
359 for (idp = *idl; idp; idp = idp->next)
360 if (idp->val.status.num == num)
362 idp->val.status.mark = UID_DELETED;
368 void append_str_list(struct idlist **idl, struct idlist **nidl)
369 /* append nidl to idl (does not copy *) */
371 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
373 else if ((*idl) == (struct idlist *)NULL)
375 else if ((*idl)->next == (struct idlist *)NULL)
376 (*idl)->next = *nidl;
377 else if ((*idl)->next != *nidl)
378 append_str_list(&(*idl)->next, nidl);
382 void expunge_uids(struct query *ctl)
383 /* assert that all UIDs marked deleted have actually been expunged */
387 for (idl = ctl->newsaved; idl; idl = idl->next)
388 if (idl->val.status.mark == UID_DELETED)
389 idl->val.status.mark = UID_EXPUNGED;
392 void uid_end_query(struct query *ctl)
395 /* old state of mailbox is now irrelevant */
396 free_str_list(&ctl->oldsaved);
397 free_str_list(&scratchlist);
398 ctl->oldsaved = ctl->newsaved;
399 ctl->newsaved = (struct idlist *) NULL;
402 if (ctl->server.uidl && outlevel >= O_DEBUG)
406 report_build(stdout, "New UID list from %s:", ctl->server.pollname);
407 for (idp = ctl->oldsaved; idp; idp = idp->next)
408 report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
410 report_build(stdout, " <empty>");
411 report_complete(stdout, "\n");
415 void write_saved_lists(struct query *hostlist, const char *idfile)
416 /* perform end-of-run write of seen-messages list */
423 /* if all lists are empty, nuke the file */
425 for (ctl = hostlist; ctl; ctl = ctl->next) {
430 /* either nuke the file or write updated last-seen IDs */
431 if (!idcount && !scratchlist)
433 if (outlevel >= O_DEBUG)
434 report(stdout, "Deleting fetchids file.\n");
438 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
439 for (ctl = hostlist; ctl; ctl = ctl->next) {
440 for (idp = ctl->oldsaved; idp; idp = idp->next)
441 if (idp->val.status.mark == UID_SEEN
442 || idp->val.status.mark == UID_DELETED)
443 fprintf(tmpfp, "%s@%s %s\n",
444 ctl->remotename, ctl->server.truename, idp->id);
446 for (idp = scratchlist; idp; idp = idp->next)
447 fputs(idp->id, tmpfp);
451 #endif /* POP3_ENABLE */
453 /* uid.c ends here */