1 /* Copyright 1993-95 by Carl Harris, Jr. Copyright 1996 by Eric S. Raymond
3 * For license terms, see the file COPYING in this directory.
6 /***********************************************************************
9 programmer: Eric S. Raymond
10 description: UID list handling
12 ***********************************************************************/
18 #if defined(STDC_HEADERS)
23 #if defined(HAVE_UNISTD_H)
27 #include "fetchmail.h"
30 * Machinery for handling UID lists live here. This is mainly to support
31 * RFC1725-conformant POP3 servers without a LAST command, but may also be
32 * useful for making the IMAP4 querying logic UID-oriented, if I feel
33 * sufficiently motivated at some point.
37 * At start of a query, we have a (possibly empty) list of UIDs to be
38 * considered `already seen'. These are messages that were left in
39 * the mailbox and *not deleted* on previous queries (we don't need to
40 * remember the UIDs of deleted messages because ... well, they're gone!).
41 * This list is set up by initialized_saved_list() from the .fetchids
42 * file and hangs off the host's `saved' member.
44 * Early in the query, during the execution of the protocol-specific
45 * getrange code, the driver expects that the host's `listed' member
46 * will be filled with a list of UIDs and message numbers representing
47 * the current mailbox state. If this list is empty, the server did
48 * not respond to the request for a UID listing.
50 * Each time a message is fetched, we can check its UID against the
51 * `saved' list to see if it is old. If not, it should be downloaded
52 * (and possibly deleted). It should be downloaded anyway if --all
53 * is on. It should not be deleted if --keep is on.
55 * Each time a message is deleted, we remove its id from the `listed'
58 * At the end of the query, whatever remains in the `listed' member
59 * (because it was not deleted) becomes the `saved' list. The old
60 * `saved' list is freed.
63 /* UIDs associated with un-queried hosts */
64 static struct idlist *scratchlist;
66 void initialize_saved_lists(hostlist, idfile)
67 /* read file of saved IDs and attach to each host */
68 struct hostrec *hostlist;
73 struct hostrec *hostp;
75 /* make sure lists are initially empty */
76 for (hostp = hostlist; hostp; hostp = hostp->next)
77 hostp->saved = hostp->mailbox = (struct idlist *)NULL;
79 /* let's get stored message UIDs from previous queries */
80 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
81 char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
83 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
85 if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
87 for (hostp = hostlist; hostp; hostp = hostp->next)
89 if (strcmp(host, hostp->servername) == 0)
91 save_uid(&hostp->saved, -1, id);
96 /* if it's not in a host we're querying, save it anyway */
97 if (hostp == (struct hostrec *)NULL)
98 save_uid(&scratchlist, -1, buf);
105 void save_uid(idl, num, str)
106 /* save a number/UID pair on the given UID list */
113 new = (struct idlist *)xmalloc(sizeof(struct idlist));
115 new->id = strdup(str);
120 void free_uid_list(idl)
121 /* free the given UID list */
124 if (*idl == (struct idlist *)NULL)
127 free_uid_list(&(*idl)->next);
130 *idl = (struct idlist *)NULL;
133 int uid_in_list(idl, str)
134 /* is a given ID in the given list? */
138 if (*idl == (struct idlist *)NULL)
140 else if (strcmp(str, (*idl)->id) == 0)
143 return(uid_in_list(&(*idl)->next, str));
146 int delete_uid(idl, str)
147 /* delete given UID from given list */
151 if (*idl == (struct idlist *)NULL)
153 else if (strcmp((*idl)->id, str) == 0)
155 struct idlist *next = (*idl)->next;
163 return(delete_uid(&(*idl)->next, str));
167 void update_uid_lists(hostp)
168 /* perform end-of-query actions on UID lists */
169 struct hostrec *hostp;
172 * Replace `saved' list with `mailbox' list as modified by deletions.
174 free_uid_list(&hostp->saved);
175 hostp->saved = hostp->mailbox;
178 void write_saved_lists(hostlist, idfile)
179 struct hostrec *hostlist;
184 struct hostrec *hostp;
187 /* if all lists are empty, nuke the file */
189 for (hostp = hostlist; hostp; hostp = hostp->next) {
194 /* either nuke the file or write updated last-seen IDs */
198 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
199 for (hostp = hostlist; hostp; hostp = hostp->next) {
200 for (idp = hostp->saved; idp; idp = idp->next)
201 fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
203 for (idp = scratchlist; idp; idp = idp->next)
204 fputs(idp->id, tmpfp);