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 (ctl->server.truename &&
86 strcasecmp(host, ctl->server.truename) == 0
87 && strcasecmp(user, ctl->remotename) == 0)
89 save_str(&ctl->oldsaved, -1, id);
94 /* if it's not in a host we're querying, save it anyway */
95 if (ctl == (struct query *)NULL)
96 save_str(&scratchlist, -1, buf);
103 struct idlist *save_str(struct idlist **idl, int num, const char *str)
104 /* save a number/UID pair on the given UID list */
108 /* do it nonrecursively so the list is in the right order */
109 for (end = idl; *end; end = &(*end)->next)
112 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
113 (*end)->val.num = num;
114 (*end)->id = str ? xstrdup(str) : (char *)NULL;
120 void free_str_list(struct idlist **idl)
121 /* free the given UID list */
123 if (*idl == (struct idlist *)NULL)
126 free_str_list(&(*idl)->next);
129 *idl = (struct idlist *)NULL;
132 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
133 /* save an ID pair on the given list */
137 /* do it nonrecursively so the list is in the right order */
138 for (end = idl; *end; end = &(*end)->next)
141 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
142 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
144 (*end)->val.id2 = xstrdup(str2);
146 (*end)->val.id2 = (char *)NULL;
147 (*end)->next = (struct idlist *)NULL;
151 void free_str_pair_list(struct idlist **idl)
152 /* 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 str_in_list(struct idlist **idl, const char *str)
166 /* is a given ID in the given list? (comparison is caseblind) */
168 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
170 else if (strcasecmp(str, (*idl)->id) == 0)
173 return(str_in_list(&(*idl)->next, str));
176 int str_nr_in_list( struct idlist **idl, const char *str )
177 /* return the position of str in idl */
183 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
184 if( strcasecmp( str, walk->id) == 0 )
189 int count_list( struct idlist **idl )
190 /* count the number of elements in the list */
194 return 1 + count_list( &(*idl)->next );
197 char* str_from_nr_list( struct idlist **idl, int number )
198 /* return the number'th string in idl */
200 if( !*idl || number < 0)
204 return str_from_nr_list(&(*idl)->next, number-1);
208 char *str_find(struct idlist **idl, int number)
209 /* return the id of the given number in the given list. */
211 if (*idl == (struct idlist *) 0)
213 else if (number == (*idl)->val.num)
216 return(str_find(&(*idl)->next, number));
219 char *idpair_find(struct idlist **idl, const char *id)
220 /* return the id of the given id in the given list (caseblind comparison) */
222 if (*idl == (struct idlist *) 0)
224 else if (strcasecmp(id, (*idl)->id) == 0)
225 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
227 return(idpair_find(&(*idl)->next, id));
230 int delete_str(struct idlist **idl, int num)
231 /* delete given message from given list */
233 if (*idl == (struct idlist *)NULL)
235 else if ((*idl)->val.num == num)
237 struct idlist *next = (*idl)->next;
245 return(delete_str(&(*idl)->next, num));
248 void append_str_list(struct idlist **idl, struct idlist **nidl)
249 /* append nidl to idl (does not copy *) */
251 if ((*idl) == (struct idlist *)NULL)
253 else if ((*idl)->next == (struct idlist *)NULL)
254 (*idl)->next = *nidl;
255 else if ((*idl)->next != *nidl)
256 append_str_list(&(*idl)->next, nidl);
259 void update_str_lists(struct query *ctl)
260 /* perform end-of-query actions on UID lists */
262 free_str_list(&ctl->oldsaved);
263 ctl->oldsaved = ctl->newsaved;
264 ctl->newsaved = (struct idlist *) NULL;
267 void write_saved_lists(struct query *hostlist, const char *idfile)
268 /* perform end-of-run write of seen-messages list */
275 /* if all lists are empty, nuke the file */
277 for (ctl = hostlist; ctl; ctl = ctl->next) {
282 /* either nuke the file or write updated last-seen IDs */
286 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
287 for (ctl = hostlist; ctl; ctl = ctl->next) {
288 for (idp = ctl->oldsaved; idp; idp = idp->next)
289 fprintf(tmpfp, "%s@%s %s\n",
290 ctl->remotename, ctl->server.truename, idp->id);
292 for (idp = scratchlist; idp; idp = idp->next)
293 fputs(idp->id, tmpfp);
298 /* uid.c ends here */