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 a future
33 * revision of IMAP forces me to. (This would be bad. Server-side
34 * seen bits are better than UIDs, because they track messages seen by
39 * At start of a query, we have a (possibly empty) list of UIDs to be
40 * considered `already seen'. These are messages that were left in
41 * the mailbox and *not deleted* on previous queries (we don't need to
42 * remember the UIDs of deleted messages because ... well, they're gone!).
43 * This list is set up by initialized_saved_list() from the .fetchids
44 * file and hangs off the host's `saved' member.
46 * Early in the query, during the execution of the protocol-specific
47 * getrange code, the driver expects that the host's `listed' member
48 * will be filled with a list of UIDs and message numbers representing
49 * the current mailbox state. If this list is empty, the server did
50 * not respond to the request for a UID listing.
52 * Each time a message is fetched, we can check its UID against the
53 * `saved' list to see if it is old. If not, it should be downloaded
54 * (and possibly deleted). It should be downloaded anyway if --all
55 * is on. It should not be deleted if --keep is on.
57 * Each time a message is deleted, we remove its id from the `listed'
60 * At the end of the query, whatever remains in the `listed' member
61 * (because it was not deleted) becomes the `saved' list. The old
62 * `saved' list is freed.
65 /* UIDs associated with un-queried hosts */
66 static struct idlist *scratchlist;
68 void initialize_saved_lists(hostlist, idfile)
69 /* read file of saved IDs and attach to each host */
70 struct hostrec *hostlist;
75 struct hostrec *hostp;
77 /* make sure lists are initially empty */
78 for (hostp = hostlist; hostp; hostp = hostp->next)
79 hostp->saved = hostp->current = (struct idlist *)NULL;
81 /* let's get stored message UIDs from previous queries */
82 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
83 char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
85 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
87 if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
89 for (hostp = hostlist; hostp; hostp = hostp->next)
91 if (strcmp(host, hostp->servername) == 0)
93 save_uid(&hostp->saved, -1, id);
98 /* if it's not in a host we're querying, save it anyway */
99 if (hostp == (struct hostrec *)NULL)
100 save_uid(&scratchlist, -1, buf);
107 void save_uid(idl, num, str)
108 /* save a number/UID pair on the given UID list */
115 new = (struct idlist *)xmalloc(sizeof(struct idlist));
117 new->id = strdup(str);
122 void free_uid_list(idl)
123 /* free the given UID list */
126 if (*idl == (struct idlist *)NULL)
129 free_uid_list(&(*idl)->next);
132 *idl = (struct idlist *)NULL;
135 int uid_in_list(idl, str)
136 /* is a given ID in the given list? */
140 if (*idl == (struct idlist *)NULL)
142 else if (strcmp(str, (*idl)->id) == 0)
145 return(uid_in_list(&(*idl)->next, str));
148 int delete_uid(idl, str)
149 /* delete given UID from given list */
153 if (*idl == (struct idlist *)NULL)
155 else if (strcmp((*idl)->id, str) == 0)
157 struct idlist *next = (*idl)->next;
165 return(delete_uid(&(*idl)->next, str));
169 void update_uid_lists(hostp)
170 /* perform end-of-query actions on UID lists */
171 struct hostrec *hostp;
174 * Replace `saved' list with `current' list as modified by deletions.
176 free_uid_list(&hostp->saved);
177 hostp->saved = hostp->current;
180 void write_saved_lists(hostlist, idfile)
181 struct hostrec *hostlist;
186 struct hostrec *hostp;
189 /* if all lists are empty, nuke the file */
191 for (hostp = hostlist; hostp; hostp = hostp->next) {
196 /* either nuke the file or write updated last-seen IDs */
200 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
201 for (hostp = hostlist; hostp; hostp = hostp->next) {
202 for (idp = hostp->saved; idp; idp = idp->next)
203 fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
205 for (idp = scratchlist; idp; idp = idp->next)
206 fputs(idp->id, tmpfp);