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.
26 * These functions are also used by the rest of the code to maintain
31 * At start of a query, we have a (possibly empty) list of UIDs to be
32 * considered seen in `oldsaved'. These are messages that were left in
33 * the mailbox and *not deleted* on previous queries (we don't need to
34 * remember the UIDs of deleted messages because ... well, they're gone!)
35 * This list is initially set up by initialized_saved_list() from the
38 * Early in the query, during the execution of the protocol-specific
39 * getrange code, the driver expects that the host's `newsaved' member
40 * will be filled with a list of UIDs and message numbers representing
41 * the mailbox state. If this list is empty, the server did
42 * not respond to the request for a UID listing.
44 * Each time a message is fetched, we can check its UID against the
45 * `oldsaved' list to see if it is old. If not, it should be downloaded
46 * (and possibly deleted). It should be downloaded anyway if --all
47 * is on. It should not be deleted if --keep is on.
49 * Each time a message is deleted, we mark its id UID_DELETED from the
50 * `newsaved' member. When we want to assert that an expunge has been
51 * done on the server, we call expunge_uid() to register that all
52 * deleted messages are gone by marking them UID_EXPUNGED.
54 * At the end of the query, the `newsaved' member becomes the
55 * `oldsaved' list. The old `oldsaved' list is freed.
57 * At the end of the fetchmail run, non-EXPUNGED members of all
58 * current `oldsaved' lists are flushed out to the .fetchids file to
59 * be picked up by the next run. (The UID_EXPUNGED test means that a
60 * message marked UID_DELETED may still have its ID go to disk if
61 * there has been no intervening expunge operation. This typically
62 * comes up if the query was aborted by a line hit before a quit or
63 * expunge was sent to the server.) If there are no un-expunged
64 * messages, the file is deleted.
66 * Note: all comparisons are caseblind!
69 /* UIDs associated with un-queried hosts */
70 static struct idlist *scratchlist;
73 void initialize_saved_lists(struct query *hostlist, const char *idfile)
74 /* read file of saved IDs and attach to each host */
80 /* make sure lists are initially empty */
81 for (ctl = hostlist; ctl; ctl = ctl->next)
82 ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
84 /* let's get stored message UIDs from previous queries */
85 if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
86 char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
88 while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
90 /* possible lossage here with very old versions of sscanf(3)... */
91 if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
93 for (ctl = hostlist; ctl; ctl = ctl->next)
95 if (ctl->server.truename &&
96 strcasecmp(host, ctl->server.truename) == 0
97 && strcasecmp(user, ctl->remotename) == 0)
99 save_str(&ctl->oldsaved, UID_KEPT, id);
104 /* if it's not in a host we're querying, save it anyway */
105 if (ctl == (struct query *)NULL)
106 save_str(&scratchlist, UID_KEPT, buf);
112 #endif /* POP3_ENABLE */
114 struct idlist *save_str(struct idlist **idl, int num, const char *str)
115 /* save a number/UID pair on the given UID list */
119 /* do it nonrecursively so the list is in the right order */
120 for (end = idl; *end; end = &(*end)->next)
123 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
124 (*end)->val.num = num;
125 (*end)->id = str ? xstrdup(str) : (char *)NULL;
131 void free_str_list(struct idlist **idl)
132 /* free the given UID list */
134 if (*idl == (struct idlist *)NULL)
137 free_str_list(&(*idl)->next);
140 *idl = (struct idlist *)NULL;
143 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
144 /* save an ID pair on the given list */
148 /* do it nonrecursively so the list is in the right order */
149 for (end = idl; *end; end = &(*end)->next)
152 *end = (struct idlist *)xmalloc(sizeof(struct idlist));
153 (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
155 (*end)->val.id2 = xstrdup(str2);
157 (*end)->val.id2 = (char *)NULL;
158 (*end)->next = (struct idlist *)NULL;
162 void free_str_pair_list(struct idlist **idl)
163 /* free the given ID pair list */
165 if (*idl == (struct idlist *)NULL)
168 free_idpair_list(&(*idl)->next);
170 free ((*idl)->val.id2);
172 *idl = (struct idlist *)NULL;
176 int str_in_list(struct idlist **idl, const char *str)
177 /* is a given ID in the given list? (comparison is caseblind) */
179 if (*idl == (struct idlist *)NULL || str == (char *) NULL)
181 else if (strcasecmp(str, (*idl)->id) == 0)
184 return(str_in_list(&(*idl)->next, str));
187 int str_nr_in_list( struct idlist **idl, const char *str )
188 /* return the position of str in idl */
194 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
195 if( strcasecmp( str, walk->id) == 0 )
200 int str_nr_last_in_list( struct idlist **idl, const char *str )
201 /* return the last position of str in idl */
207 for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
208 if( strcasecmp( str, walk->id) == 0 )
213 int count_list( struct idlist **idl )
214 /* count the number of elements in the list */
218 return 1 + count_list( &(*idl)->next );
221 char* str_from_nr_list( struct idlist **idl, int number )
222 /* return the number'th string in idl */
224 if( !*idl || number < 0)
228 return str_from_nr_list(&(*idl)->next, number-1);
232 char *str_find(struct idlist **idl, int number)
233 /* return the id of the given number in the given list. */
235 if (*idl == (struct idlist *) 0)
237 else if (number == (*idl)->val.num)
240 return(str_find(&(*idl)->next, number));
243 char *idpair_find(struct idlist **idl, const char *id)
244 /* return the id of the given id in the given list (caseblind comparison) */
246 if (*idl == (struct idlist *) 0)
248 else if (strcasecmp(id, (*idl)->id) == 0)
249 return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
251 return(idpair_find(&(*idl)->next, id));
254 int delete_str(struct idlist **idl, int num)
255 /* delete given message from given list */
257 #ifdef HARD_DELETE /* not used */
258 if (*idl == (struct idlist *)NULL)
260 else if ((*idl)->val.num == num)
262 struct idlist *next = (*idl)->next;
270 return(delete_str(&(*idl)->next, num));
274 for (idp = *idl; idp; idp = idp->next)
275 if (idp->val.num == num)
277 idp->val.num = UID_DELETED;
281 #endif /* HARD_DELETE */
284 void append_str_list(struct idlist **idl, struct idlist **nidl)
285 /* append nidl to idl (does not copy *) */
287 if ((*idl) == (struct idlist *)NULL)
289 else if ((*idl)->next == (struct idlist *)NULL)
290 (*idl)->next = *nidl;
291 else if ((*idl)->next != *nidl)
292 append_str_list(&(*idl)->next, nidl);
296 void expunge_uids(struct query *ctl)
297 /* assert that all UIDs marked deleted have actually been expunged */
301 for (idl = ctl->newsaved; idl; idl = idl->next)
302 if (idl->val.num == UID_DELETED)
303 idl->val.num = UID_EXPUNGED;
306 void update_str_lists(struct query *ctl)
307 /* perform end-of-query actions on UID lists */
309 free_str_list(&ctl->oldsaved);
310 ctl->oldsaved = ctl->newsaved;
311 ctl->newsaved = (struct idlist *) NULL;
314 void write_saved_lists(struct query *hostlist, const char *idfile)
315 /* perform end-of-run write of seen-messages list */
322 /* if all lists are empty, nuke the file */
324 for (ctl = hostlist; ctl; ctl = ctl->next) {
329 /* either nuke the file or write updated last-seen IDs */
333 if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
334 for (ctl = hostlist; ctl; ctl = ctl->next) {
335 for (idp = ctl->oldsaved; idp; idp = idp->next)
336 if (idp->val.num != UID_EXPUNGED)
337 fprintf(tmpfp, "%s@%s %s\n",
338 ctl->remotename, ctl->server.truename, idp->id);
340 for (idp = scratchlist; idp; idp = idp->next)
341 fputs(idp->id, tmpfp);
345 #endif /* POP3_ENABLE */
347 /* uid.c ends here */