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)
15 #if defined(HAVE_UNISTD_H)
19 #include "fetchmail.h"
22 * Machinery for handling UID lists live here. This is mainly to support
23 * RFC1725-conformant POP3 servers without a LAST command, but may also be
24 * useful for making the IMAP4 querying logic UID-oriented, if a future
25 * revision of IMAP forces me to.
27 * These functions are also used by the rest of the code to maintain
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 initialize_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.
48 * Each time a message-id is seen, we mark it with MARK_SEEN.
50 * Each time a message is deleted, we mark its id UID_DELETED in the
51 * `newsaved' member. When we want to assert that an expunge has been
52 * done on the server, we call expunge_uid() to register that all
53 * deleted messages are gone by marking them UID_EXPUNGED.
55 * At the end of the query, the `newsaved' member becomes the
56 * `oldsaved' list. The old `oldsaved' list is freed.
58 * At the end of the fetchmail run, seen and non-EXPUNGED members of all
59 * current `oldsaved' lists are flushed out to the .fetchids file to
60 * be picked up by the next run. If there are no un-expunged
61 * messages, the file is deleted.
63 * Note: some comparisons (those used for DNS address lists) are caseblind!
66 /* UIDs associated with un-queried hosts */
67 static struct idlist *scratchlist;
70 void initialize_saved_lists(struct query *hostlist, const char *idfile)
71 /* read file of saved IDs and attach to each host */
77 /* make sure lists are initially empty */
78 for (ctl = hostlist; ctl; ctl = ctl->next)
79 ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
81 /* let's get stored message UIDs from previous queries */
82 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
83 char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
85 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
87 /* possible lossage here with very old versions of sscanf(3)... */
88 if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
90 for (ctl = hostlist; ctl; ctl = ctl->next)
92 if (ctl->server.truename &&
93 strcasecmp(host, ctl->server.truename) == 0
94 && strcasecmp(user, ctl->remotename) == 0)
96 save_str(&ctl->oldsaved, id, UID_SEEN);
101 /* if it's not in a host we're querying, save it anyway */
102 if (ctl == (struct query *)NULL)
103 save_str(&scratchlist, buf, UID_SEEN);
109 #endif /* POP3_ENABLE */
111 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
112 /* save a number/UID pair on the given UID list */
116 /* do it nonrecursively so the list is in the right order */
117 for (end = idl; *end; end = &(*end)->next)
120 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
121 (*end)->val.status.mark = status;
122 (*end)->id = str ? xstrdup(str) : (char *)NULL;
128 void free_str_list(struct idlist **idl)
129 /* free the given UID list */
131 if (*idl == (struct idlist *)NULL)
134 free_str_list(&(*idl)->next);
137 *idl = (struct idlist *)NULL;
140 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
141 /* save an ID pair on the given list */
145 /* do it nonrecursively so the list is in the right order */
146 for (end = idl; *end; end = &(*end)->next)
149 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
150 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
152 (*end)->val.id2 = xstrdup(str2);
154 (*end)->val.id2 = (char *)NULL;
155 (*end)->next = (struct idlist *)NULL;
159 void free_str_pair_list(struct idlist **idl)
160 /* free the given ID pair list */
162 if (*idl == (struct idlist *)NULL)
165 free_idpair_list(&(*idl)->next);
167 free ((*idl)->val.id2);
169 *idl = (struct idlist *)NULL;
173 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
174 /* is a given ID in the given list? (comparison may be caseblind) */
176 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
178 else if (!caseblind && strcmp(str, (*idl)->id) == 0)
180 else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
183 return(str_in_list(&(*idl)->next, str, caseblind));
186 int str_nr_in_list( struct idlist **idl, const char *str )
187 /* return the position of str in idl */
193 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
194 if( strcmp( str, walk->id) == 0 )
199 int str_nr_last_in_list( struct idlist **idl, const char *str)
200 /* return the last position of str in idl */
206 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
207 if( strcmp( str, walk->id) == 0 )
212 void str_set_mark( struct idlist **idl, const char *str, const flag val)
213 /* update the mark on an of an id to given value */
219 for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
220 if (strcmp(str, walk->id) == 0)
221 walk->val.status.mark = val;
224 int count_list( struct idlist **idl)
225 /* count the number of elements in the list */
229 return 1 + count_list( &(*idl)->next );
232 char *str_from_nr_list(struct idlist **idl, int number)
233 /* return the number'th string in idl */
235 if( !*idl || number < 0)
239 return str_from_nr_list(&(*idl)->next, number-1);
243 char *str_find(struct idlist **idl, int number)
244 /* return the id of the given number in the given list. */
246 if (*idl == (struct idlist *) 0)
248 else if (number == (*idl)->val.status.num)
251 return(str_find(&(*idl)->next, number));
254 char *idpair_find(struct idlist **idl, const char *id)
255 /* return the id of the given id in the given list (caseblind comparison) */
257 if (*idl == (struct idlist *) 0)
259 else if (strcasecmp(id, (*idl)->id) == 0)
260 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
262 return(idpair_find(&(*idl)->next, id));
265 int delete_str(struct idlist **idl, int num)
266 /* delete given message from given list */
270 for (idp = *idl; idp; idp = idp->next)
271 if (idp->val.status.num == num)
273 idp->val.status.mark = UID_DELETED;
279 void append_str_list(struct idlist **idl, struct idlist **nidl)
280 /* append nidl to idl (does not copy *) */
282 if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
284 else if ((*idl) == (struct idlist *)NULL)
286 else if ((*idl)->next == (struct idlist *)NULL)
287 (*idl)->next = *nidl;
288 else if ((*idl)->next != *nidl)
289 append_str_list(&(*idl)->next, nidl);
293 void expunge_uids(struct query *ctl)
294 /* assert that all UIDs marked deleted have actually been expunged */
298 for (idl = ctl->newsaved; idl; idl = idl->next)
299 if (idl->val.status.mark == UID_DELETED)
300 idl->val.status.mark = UID_EXPUNGED;
303 void update_str_lists(struct query *ctl)
304 /* perform end-of-query actions on UID lists */
306 free_str_list(&ctl->oldsaved);
307 ctl->oldsaved = ctl->newsaved;
308 ctl->newsaved = (struct idlist *) NULL;
311 void write_saved_lists(struct query *hostlist, const char *idfile)
312 /* perform end-of-run write of seen-messages list */
319 /* if all lists are empty, nuke the file */
321 for (ctl = hostlist; ctl; ctl = ctl->next) {
326 /* either nuke the file or write updated last-seen IDs */
330 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
331 for (ctl = hostlist; ctl; ctl = ctl->next) {
332 for (idp = ctl->oldsaved; idp; idp = idp->next)
333 if (idp->val.status.mark == UID_SEEN
334 || idp->val.status.mark == UID_DELETED)
335 fprintf(tmpfp, "%s@%s %s\n",
336 ctl->remotename, ctl->server.truename, idp->id);
338 for (idp = scratchlist; idp; idp = idp->next)
339 fputs(idp->id, tmpfp);
343 #endif /* POP3_ENABLE */
345 /* uid.c ends here */