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(struct query *hostlist, const char *idfile)
66 /* read file of saved IDs and attach to each host */
72 /* make sure lists are initially empty */
73 for (ctl = hostlist; ctl; ctl = ctl->next)
74 ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
76 /* let's get stored message UIDs from previous queries */
77 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
78 char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
80 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
82 /* possible lossage here with very old versions of sscanf(3)... */
83 if ((st = sscanf(buf, "%[^@]@%s %s\n", host, user, id)) == 3)
85 for (ctl = hostlist; ctl; ctl = ctl->next)
87 if (strcmp(host, ctl->servernames->id) == 0
88 && strcmp(user, ctl->remotename) == 0)
90 save_uid(&ctl->oldsaved, -1, id);
95 /* if it's not in a host we're querying, save it anyway */
96 if (ctl == (struct query *)NULL)
97 save_uid(&scratchlist, -1, buf);
104 struct idlist *save_uid(struct idlist **idl, int num, const char *str)
105 /* save a number/UID pair on the given UID list */
109 /* do it nonrecursively so the list is in the right order */
110 for (end = idl; *end; end = &(*end)->next)
113 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
114 (*end)->val.num = num;
115 (*end)->id = xstrdup(str);
121 void free_uid_list(struct idlist **idl)
122 /* free the given UID list */
124 if (*idl == (struct idlist *)NULL)
127 free_uid_list(&(*idl)->next);
130 *idl = (struct idlist *)NULL;
133 void save_id_pair(struct idlist **idl, const char *str1, const char *str2)
134 /* save an ID pair on the given list */
138 new = (struct idlist *)xmalloc(sizeof(struct idlist));
139 new->id = xstrdup(str1);
141 new->val.id2 = xstrdup(str2);
143 new->val.id2 = (char *)NULL;
149 void free_idpair_list(struct idlist **idl)
150 /* free the given ID pair list */
152 if (*idl == (struct idlist *)NULL)
155 free_idpair_list(&(*idl)->next);
157 free ((*idl)->val.id2);
159 *idl = (struct idlist *)NULL;
163 int uid_in_list(struct idlist **idl, const char *str)
164 /* is a given ID in the given list? */
166 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
168 else if (strcmp(str, (*idl)->id) == 0)
171 return(uid_in_list(&(*idl)->next, str));
174 char *uid_find(struct idlist **idl, int number)
175 /* return the id of the given number in the given list. */
177 if (*idl == (struct idlist *) 0)
179 else if (number == (*idl)->val.num)
182 return(uid_find(&(*idl)->next, number));
185 char *idpair_find(struct idlist **idl, const char *id)
186 /* return the id of the given number in the given list. */
188 if (*idl == (struct idlist *) 0)
190 else if (strcmp(id, (*idl)->id) == 0)
191 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
193 return(idpair_find(&(*idl)->next, id));
196 int delete_uid(struct idlist **idl, int num)
197 /* delete given message from given list */
199 if (*idl == (struct idlist *)NULL)
201 else if ((*idl)->val.num == num)
203 struct idlist *next = (*idl)->next;
211 return(delete_uid(&(*idl)->next, num));
215 void append_uid_list(struct idlist **idl, struct idlist **nidl)
216 /* append nidl to idl (does not copy *) */
218 if ((*idl) == (struct idlist *)NULL)
220 else if ((*idl)->next == (struct idlist *)NULL)
221 (*idl)->next = *nidl;
223 append_uid_list(&(*idl)->next, nidl);
226 void update_uid_lists(struct query *ctl)
227 /* perform end-of-query actions on UID lists */
229 free_uid_list(&ctl->oldsaved);
230 ctl->oldsaved = ctl->newsaved;
231 ctl->newsaved = (struct idlist *) NULL;
234 void write_saved_lists(struct query *hostlist, const char *idfile)
235 /* perform end-of-run write of seen-messages list */
242 /* if all lists are empty, nuke the file */
244 for (ctl = hostlist; ctl; ctl = ctl->next) {
249 /* either nuke the file or write updated last-seen IDs */
253 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
254 for (ctl = hostlist; ctl; ctl = ctl->next) {
255 for (idp = ctl->oldsaved; idp; idp = idp->next)
256 fprintf(tmpfp, "%s@%s %s\n",
257 ctl->remotename, ctl->servernames->id, idp->id);
259 for (idp = scratchlist; idp; idp = idp->next)
260 fputs(idp->id, tmpfp);
265 /* uid.c ends here */