2 * uid.c -- UIDL handling for POP3 servers without LAST
4 * For license terms, see the file COPYING in this directory.
11 #if defined(STDC_HEADERS)
16 #if defined(HAVE_UNISTD_H)
20 #include "fetchmail.h"
23 * Machinery for handling UID lists live here. This is mainly to support
24 * RFC1725-conformant POP3 servers without a LAST command, but may also be
25 * useful for making the IMAP4 querying logic UID-oriented, if a future
26 * revision of IMAP forces me to. (This would be bad. Server-side
27 * seen bits are better than UIDs, because they track messages seen by
32 * At start of a query, we have a (possibly empty) list of UIDs to be
33 * considered seen in `oldsaved'. These are messages that were left in
34 * the mailbox and *not deleted* on previous queries (we don't need to
35 * remember the UIDs of deleted messages because ... well, they're gone!)
36 * This list is initially set up by initialized_saved_list() from the
39 * Early in the query, during the execution of the protocol-specific
40 * getrange code, the driver expects that the host's `newsaved' member
41 * will be filled with a list of UIDs and message numbers representing
42 * the mailbox state. If this list is empty, the server did
43 * not respond to the request for a UID listing.
45 * Each time a message is fetched, we can check its UID against the
46 * `oldsaved' list to see if it is old. If not, it should be downloaded
47 * (and possibly deleted). It should be downloaded anyway if --all
48 * is on. It should not be deleted if --keep is on.
50 * Each time a message is deleted, we remove its id from the `newsaved'
53 * At the end of the query, whatever remains in the `newsaved' member
54 * (because it was not deleted) becomes the `oldsaved' list. The old
55 * `oldsaved' list is freed.
57 * At the end of the fetchmail run, all current `oldsaved' lists are
58 * flushed out to the .fetchids file to be picked up by the next run.
59 * If there are no such messages, the file is deleted.
62 /* UIDs associated with un-queried hosts */
63 static struct idlist *scratchlist;
65 void initialize_saved_lists(hostlist, idfile)
66 /* read file of saved IDs and attach to each host */
67 struct hostrec *hostlist;
72 struct hostrec *hostp;
74 /* make sure lists are initially empty */
75 for (hostp = hostlist; hostp; hostp = hostp->next)
76 hostp->oldsaved = hostp->newsaved = (struct idlist *)NULL;
78 /* let's get stored message UIDs from previous queries */
79 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
80 char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
82 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
84 if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
86 for (hostp = hostlist; hostp; hostp = hostp->next)
88 if (strcmp(host, hostp->servername) == 0)
90 save_uid(&hostp->oldsaved, -1, id);
95 /* if it's not in a host we're querying, save it anyway */
96 if (hostp == (struct hostrec *)NULL)
97 save_uid(&scratchlist, -1, buf);
104 void save_uid(idl, num, str)
105 /* save a number/UID pair on the given UID list */
112 new = (struct idlist *)xmalloc(sizeof(struct idlist));
114 new->id = xstrdup(str);
119 void free_uid_list(idl)
120 /* free the given UID list */
123 if (*idl == (struct idlist *)NULL)
126 free_uid_list(&(*idl)->next);
129 *idl = (struct idlist *)NULL;
132 void save_id_pair(idl, str1, str2)
133 /* save an ID pair on the given list */
139 new = (struct idlist *)xmalloc(sizeof(struct idlist));
140 new->id = xstrdup(str1);
142 new->val.id2 = xstrdup(str2);
144 new->val.id2 = (char *)NULL;
150 void free_idpair_list(idl)
151 /* free the given ID pair list */
154 if (*idl == (struct idlist *)NULL)
157 free_idpair_list(&(*idl)->next);
159 free ((*idl)->val.id2);
161 *idl = (struct idlist *)NULL;
165 int uid_in_list(idl, str)
166 /* is a given ID in the given list? */
170 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
172 else if (strcmp(str, (*idl)->id) == 0)
175 return(uid_in_list(&(*idl)->next, str));
178 char *uid_find(idl, number)
179 /* return the id of the given number in the given list. */
183 if (*idl == (struct idlist *) 0)
185 else if (number == (*idl)->val.num)
188 return(uid_find(&(*idl)->next, number));
191 char *idpair_find(idl, id)
192 /* return the id of the given number in the given list. */
196 if (*idl == (struct idlist *) 0)
198 else if (strcmp(id, (*idl)->id) == 0)
199 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
201 return(idpair_find(&(*idl)->next, id));
204 int delete_uid(idl, num)
205 /* delete given message from given list */
209 if (*idl == (struct idlist *)NULL)
211 else if ((*idl)->val.num == num)
213 struct idlist *next = (*idl)->next;
221 return(delete_uid(&(*idl)->next, num));
225 void append_uid_list(idl, nidl)
226 /* append nidl to idl (does not copy *) */
228 struct idlist **nidl;
230 if ((*idl) == (struct idlist *)NULL)
232 else if ((*idl)->next == (struct idlist *)NULL)
233 (*idl)->next = *nidl;
235 append_uid_list(&(*idl)->next, nidl);
238 void update_uid_lists(hostp)
239 /* perform end-of-query actions on UID lists */
240 struct hostrec *hostp;
242 free_uid_list(&hostp->oldsaved);
243 hostp->oldsaved = hostp->newsaved;
244 hostp->newsaved = (struct idlist *) NULL;
247 void write_saved_lists(hostlist, idfile)
248 /* perform end-of-run write of seen-messages list */
249 struct hostrec *hostlist;
254 struct hostrec *hostp;
257 /* if all lists are empty, nuke the file */
259 for (hostp = hostlist; hostp; hostp = hostp->next) {
264 /* either nuke the file or write updated last-seen IDs */
268 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
269 for (hostp = hostlist; hostp; hostp = hostp->next) {
270 for (idp = hostp->oldsaved; idp; idp = idp->next)
271 fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
273 for (idp = scratchlist; idp; idp = idp->next)
274 fputs(idp->id, tmpfp);
279 /* uid.c ends here */