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 seen in `oldsaved'. 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 initially set up by initialized_saved_list() from the
46 * Early in the query, during the execution of the protocol-specific
47 * getrange code, the driver expects that the host's `newsaved' member
48 * will be filled with a list of UIDs and message numbers representing
49 * the 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 * `oldsaved' 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 `newsaved'
60 * At the end of the query, whatever remains in the `newsaved' member
61 * (because it was not deleted) becomes the `oldsaved' list. The old
62 * `oldsaved' list is freed.
64 * At the end of the fetchmail run, all current `oldsaved' lists are
65 * flushed out to the .fetchids file to be picked up by the next run.
66 * If there are no such messages, the file is deleted.
69 /* UIDs associated with un-queried hosts */
70 static struct idlist *scratchlist;
72 void initialize_saved_lists(hostlist, idfile)
73 /* read file of saved IDs and attach to each host */
74 struct hostrec *hostlist;
79 struct hostrec *hostp;
81 /* make sure lists are initially empty */
82 for (hostp = hostlist; hostp; hostp = hostp->next)
83 hostp->oldsaved = hostp->newsaved = (struct idlist *)NULL;
85 /* let's get stored message UIDs from previous queries */
86 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
87 char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
89 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
91 if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
93 for (hostp = hostlist; hostp; hostp = hostp->next)
95 if (strcmp(host, hostp->servername) == 0)
97 save_uid(&hostp->oldsaved, -1, id);
102 /* if it's not in a host we're querying, save it anyway */
103 if (hostp == (struct hostrec *)NULL)
104 save_uid(&scratchlist, -1, buf);
111 void save_uid(idl, num, str)
112 /* save a number/UID pair on the given UID list */
119 new = (struct idlist *)xmalloc(sizeof(struct idlist));
121 new->id = strdup(str);
126 void free_uid_list(idl)
127 /* free the given UID list */
130 if (*idl == (struct idlist *)NULL)
133 free_uid_list(&(*idl)->next);
136 *idl = (struct idlist *)NULL;
139 int uid_in_list(idl, str)
140 /* is a given ID in the given list? */
144 if (*idl == (struct idlist *)NULL)
146 else if (strcmp(str, (*idl)->id) == 0)
149 return(uid_in_list(&(*idl)->next, str));
152 int delete_uid(idl, num)
153 /* delete given message from given list */
157 if (*idl == (struct idlist *)NULL)
159 else if ((*idl)->num == num)
161 struct idlist *next = (*idl)->next;
169 return(delete_uid(&(*idl)->next, num));
173 void update_uid_lists(hostp)
174 /* perform end-of-query actions on UID lists */
175 struct hostrec *hostp;
177 free_uid_list(&hostp->oldsaved);
178 hostp->oldsaved = hostp->newsaved;
181 void write_saved_lists(hostlist, idfile)
182 /* perform end-of-run write of seen-messages list */
183 struct hostrec *hostlist;
188 struct hostrec *hostp;
191 /* if all lists are empty, nuke the file */
193 for (hostp = hostlist; hostp; hostp = hostp->next) {
198 /* either nuke the file or write updated last-seen IDs */
202 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
203 for (hostp = hostlist; hostp; hostp = hostp->next) {
204 for (idp = hostp->oldsaved; idp; idp = idp->next)
205 fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
207 for (idp = scratchlist; idp; idp = idp->next)
208 fputs(idp->id, tmpfp);