2 * For license terms, see the file COPYING in this directory.
5 /***********************************************************************
8 programmer: Eric S. Raymond
9 description: UID list handling
11 ***********************************************************************/
17 #if defined(STDC_HEADERS)
22 #if defined(HAVE_UNISTD_H)
26 #include "fetchmail.h"
29 * Machinery for handling UID lists live here. This is mainly to support
30 * RFC1725-conformant POP3 servers without a LAST command, but may also be
31 * useful for making the IMAP4 querying logic UID-oriented, if a future
32 * revision of IMAP forces me to. (This would be bad. Server-side
33 * seen bits are better than UIDs, because they track messages seen by
38 * At start of a query, we have a (possibly empty) list of UIDs to be
39 * considered seen in `oldsaved'. These are messages that were left in
40 * the mailbox and *not deleted* on previous queries (we don't need to
41 * remember the UIDs of deleted messages because ... well, they're gone!)
42 * This list is initially set up by initialized_saved_list() from the
45 * Early in the query, during the execution of the protocol-specific
46 * getrange code, the driver expects that the host's `newsaved' member
47 * will be filled with a list of UIDs and message numbers representing
48 * the mailbox state. If this list is empty, the server did
49 * not respond to the request for a UID listing.
51 * Each time a message is fetched, we can check its UID against the
52 * `oldsaved' list to see if it is old. If not, it should be downloaded
53 * (and possibly deleted). It should be downloaded anyway if --all
54 * is on. It should not be deleted if --keep is on.
56 * Each time a message is deleted, we remove its id from the `newsaved'
59 * At the end of the query, whatever remains in the `newsaved' member
60 * (because it was not deleted) becomes the `oldsaved' list. The old
61 * `oldsaved' list is freed.
63 * At the end of the fetchmail run, all current `oldsaved' lists are
64 * flushed out to the .fetchids file to be picked up by the next run.
65 * If there are no such messages, the file is deleted.
68 /* UIDs associated with un-queried hosts */
69 static struct idlist *scratchlist;
71 void initialize_saved_lists(hostlist, idfile)
72 /* read file of saved IDs and attach to each host */
73 struct hostrec *hostlist;
78 struct hostrec *hostp;
80 /* make sure lists are initially empty */
81 for (hostp = hostlist; hostp; hostp = hostp->next)
82 hostp->oldsaved = hostp->newsaved = (struct idlist *)NULL;
84 /* let's get stored message UIDs from previous queries */
85 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
86 char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
88 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
90 if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
92 for (hostp = hostlist; hostp; hostp = hostp->next)
94 if (strcmp(host, hostp->servername) == 0)
96 save_uid(&hostp->oldsaved, -1, id);
101 /* if it's not in a host we're querying, save it anyway */
102 if (hostp == (struct hostrec *)NULL)
103 save_uid(&scratchlist, -1, buf);
110 void save_uid(idl, num, str)
111 /* save a number/UID pair on the given UID list */
118 new = (struct idlist *)xmalloc(sizeof(struct idlist));
120 new->id = strdup(str);
125 void free_uid_list(idl)
126 /* free the given UID list */
129 if (*idl == (struct idlist *)NULL)
132 free_uid_list(&(*idl)->next);
135 *idl = (struct idlist *)NULL;
138 int uid_in_list(idl, str)
139 /* is a given ID in the given list? */
143 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
145 else if (strcmp(str, (*idl)->id) == 0)
148 return(uid_in_list(&(*idl)->next, str));
151 char *uid_find(idl, number)
152 /* return the id of the given number in the given list. */
156 if (*idl == (struct idlist *) 0)
158 else if (number == (*idl)->num)
161 return(uid_find(&(*idl)->next, number));
164 int delete_uid(idl, num)
165 /* delete given message from given list */
169 if (*idl == (struct idlist *)NULL)
171 else if ((*idl)->num == num)
173 struct idlist *next = (*idl)->next;
181 return(delete_uid(&(*idl)->next, num));
185 void update_uid_lists(hostp)
186 /* perform end-of-query actions on UID lists */
187 struct hostrec *hostp;
189 free_uid_list(&hostp->oldsaved);
190 hostp->oldsaved = hostp->newsaved;
191 hostp->newsaved = (struct idlist *) NULL;
194 void write_saved_lists(hostlist, idfile)
195 /* perform end-of-run write of seen-messages list */
196 struct hostrec *hostlist;
201 struct hostrec *hostp;
204 /* if all lists are empty, nuke the file */
206 for (hostp = hostlist; hostp; hostp = hostp->next) {
211 /* either nuke the file or write updated last-seen IDs */
215 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
216 for (hostp = hostlist; hostp; hostp = hostp->next) {
217 for (idp = hostp->oldsaved; idp; idp = idp->next)
218 fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
220 for (idp = scratchlist; idp; idp = idp->next)
221 fputs(idp->id, tmpfp);