2 * uid.c -- UIDL handling for POP3 servers without LAST
4 * For license terms, see the file COPYING in this directory.
10 #if defined(STDC_HEADERS)
14 #if defined(HAVE_UNISTD_H)
18 #include "fetchmail.h"
21 * Machinery for handling UID lists live here. This is mainly to support
22 * RFC1725-conformant POP3 servers without a LAST command, but may also be
23 * useful for making the IMAP4 querying logic UID-oriented, if a future
24 * revision of IMAP forces me to.
28 * At start of a query, we have a (possibly empty) list of UIDs to be
29 * considered seen in `oldsaved'. These are messages that were left in
30 * the mailbox and *not deleted* on previous queries (we don't need to
31 * remember the UIDs of deleted messages because ... well, they're gone!)
32 * This list is initially set up by initialized_saved_list() from the
35 * Early in the query, during the execution of the protocol-specific
36 * getrange code, the driver expects that the host's `newsaved' member
37 * will be filled with a list of UIDs and message numbers representing
38 * the mailbox state. If this list is empty, the server did
39 * not respond to the request for a UID listing.
41 * Each time a message is fetched, we can check its UID against the
42 * `oldsaved' list to see if it is old. If not, it should be downloaded
43 * (and possibly deleted). It should be downloaded anyway if --all
44 * is on. It should not be deleted if --keep is on.
46 * Each time a message is deleted, we remove its id from the `newsaved'
49 * At the end of the query, whatever remains in the `newsaved' member
50 * (because it was not deleted) becomes the `oldsaved' list. The old
51 * `oldsaved' list is freed.
53 * At the end of the fetchmail run, all current `oldsaved' lists are
54 * flushed out to the .fetchids file to be picked up by the next run.
55 * If there are no such messages, the file is deleted.
57 * Note: all comparisons are caseblind!
60 /* UIDs associated with un-queried hosts */
61 static struct idlist *scratchlist;
63 void initialize_saved_lists(struct query *hostlist, const char *idfile)
64 /* read file of saved IDs and attach to each host */
70 /* make sure lists are initially empty */
71 for (ctl = hostlist; ctl; ctl = ctl->next)
72 ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
74 /* let's get stored message UIDs from previous queries */
75 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
76 char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
78 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
80 /* possible lossage here with very old versions of sscanf(3)... */
81 if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
83 for (ctl = hostlist; ctl; ctl = ctl->next)
85 if (strcasecmp(host, ctl->server.names->id) == 0
86 && strcasecmp(user, ctl->remotename) == 0)
88 save_str(&ctl->oldsaved, -1, id);
93 /* if it's not in a host we're querying, save it anyway */
94 if (ctl == (struct query *)NULL)
95 save_str(&scratchlist, -1, buf);
102 struct idlist *save_str(struct idlist **idl, int num, const char *str)
103 /* save a number/UID pair on the given UID list */
107 /* do it nonrecursively so the list is in the right order */
108 for (end = idl; *end; end = &(*end)->next)
111 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
112 (*end)->val.num = num;
113 (*end)->id = str ? xstrdup(str) : (char *)NULL;
119 void free_str_list(struct idlist **idl)
120 /* free the given UID list */
122 if (*idl == (struct idlist *)NULL)
125 free_str_list(&(*idl)->next);
128 *idl = (struct idlist *)NULL;
131 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
132 /* save an ID pair on the given list */
136 /* do it nonrecursively so the list is in the right order */
137 for (end = idl; *end; end = &(*end)->next)
140 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
141 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
143 (*end)->val.id2 = xstrdup(str2);
145 (*end)->val.id2 = (char *)NULL;
146 (*end)->next = (struct idlist *)NULL;
150 void free_str_pair_list(struct idlist **idl)
151 /* free the given ID pair list */
153 if (*idl == (struct idlist *)NULL)
156 free_idpair_list(&(*idl)->next);
158 free ((*idl)->val.id2);
160 *idl = (struct idlist *)NULL;
164 int str_in_list(struct idlist **idl, const char *str)
165 /* is a given ID in the given list? (comparison is caseblind) */
167 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
169 else if (strcasecmp(str, (*idl)->id) == 0)
172 return(str_in_list(&(*idl)->next, str));
175 char *str_find(struct idlist **idl, int number)
176 /* return the id of the given number in the given list. */
178 if (*idl == (struct idlist *) 0)
180 else if (number == (*idl)->val.num)
183 return(str_find(&(*idl)->next, number));
186 char *idpair_find(struct idlist **idl, const char *id)
187 /* return the id of the given id in the given list (caseblind comparison) */
189 if (*idl == (struct idlist *) 0)
191 else if (strcasecmp(id, (*idl)->id) == 0)
192 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
194 return(idpair_find(&(*idl)->next, id));
197 int delete_str(struct idlist **idl, int num)
198 /* delete given message from given list */
200 if (*idl == (struct idlist *)NULL)
202 else if ((*idl)->val.num == num)
204 struct idlist *next = (*idl)->next;
212 return(delete_str(&(*idl)->next, num));
216 void append_str_list(struct idlist **idl, struct idlist **nidl)
217 /* append nidl to idl (does not copy *) */
219 if ((*idl) == (struct idlist *)NULL)
221 else if ((*idl)->next == (struct idlist *)NULL)
222 (*idl)->next = *nidl;
223 else if ((*idl)->next != *nidl)
224 append_str_list(&(*idl)->next, nidl);
227 void update_str_lists(struct query *ctl)
228 /* perform end-of-query actions on UID lists */
230 free_str_list(&ctl->oldsaved);
231 ctl->oldsaved = ctl->newsaved;
232 ctl->newsaved = (struct idlist *) NULL;
235 void write_saved_lists(struct query *hostlist, const char *idfile)
236 /* perform end-of-run write of seen-messages list */
243 /* if all lists are empty, nuke the file */
245 for (ctl = hostlist; ctl; ctl = ctl->next) {
250 /* either nuke the file or write updated last-seen IDs */
254 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
255 for (ctl = hostlist; ctl; ctl = ctl->next) {
256 for (idp = ctl->oldsaved; idp; idp = idp->next)
257 fprintf(tmpfp, "%s@%s %s\n",
258 ctl->remotename, ctl->server.names->id, idp->id);
260 for (idp = scratchlist; idp; idp = idp->next)
261 fputs(idp->id, tmpfp);
266 /* uid.c ends here */