]> Pileus Git - ~andy/fetchmail/blob - uid.c
8b956fc1fc3d294054049165052bf5649734ddb4
[~andy/fetchmail] / uid.c
1 /*
2  * uid.c -- UIDL handling for POP3 servers without LAST
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <sys/stat.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #if defined(STDC_HEADERS)
14 #include <stdlib.h>
15 #include <string.h>
16 #endif
17 #if defined(HAVE_UNISTD_H)
18 #include <unistd.h>
19 #endif
20
21 #include "fetchmail.h"
22
23 /*
24  * Machinery for handling UID lists live here.  This is mainly to support
25  * RFC1725-conformant POP3 servers without a LAST command, but may also be
26  * useful for making the IMAP4 querying logic UID-oriented, if a future
27  * revision of IMAP forces me to.
28  *
29  * These functions are also used by the rest of the code to maintain
30  * string lists.
31  *
32  * Here's the theory:
33  *
34  * At start of a query, we have a (possibly empty) list of UIDs to be
35  * considered seen in `oldsaved'.  These are messages that were left in
36  * the mailbox and *not deleted* on previous queries (we don't need to
37  * remember the UIDs of deleted messages because ... well, they're gone!)
38  * This list is initially set up by initialize_saved_list() from the
39  * .fetchids file.
40  *
41  * Early in the query, during the execution of the protocol-specific 
42  * getrange code, the driver expects that the host's `newsaved' member
43  * will be filled with a list of UIDs and message numbers representing
44  * the mailbox state.  If this list is empty, the server did
45  * not respond to the request for a UID listing.
46  *
47  * Each time a message is fetched, we can check its UID against the
48  * `oldsaved' list to see if it is old.
49  *
50  * Each time a message-id is seen, we mark it with MARK_SEEN.
51  *
52  * Each time a message is deleted, we mark its id UID_DELETED in the
53  * `newsaved' member.  When we want to assert that an expunge has been
54  * done on the server, we call expunge_uid() to register that all
55  * deleted messages are gone by marking them UID_EXPUNGED.
56  *
57  * At the end of the query, the `newsaved' member becomes the
58  * `oldsaved' list.  The old `oldsaved' list is freed.
59  *
60  * At the end of the fetchmail run, seen and non-EXPUNGED members of all
61  * current `oldsaved' lists are flushed out to the .fetchids file to
62  * be picked up by the next run.  If there are no un-expunged
63  * messages, the file is deleted.
64  *
65  * Note: some comparisons (those used for DNS address lists) are caseblind!  
66  */
67
68 /* UIDs associated with un-queried hosts */
69 static struct idlist *scratchlist;
70
71 #ifdef POP3_ENABLE
72 void initialize_saved_lists(struct query *hostlist, const char *idfile)
73 /* read file of saved IDs and attach to each host */
74 {
75     struct stat statbuf;
76     FILE        *tmpfp;
77     struct query *ctl;
78
79     /* make sure lists are initially empty */
80     for (ctl = hostlist; ctl; ctl = ctl->next)
81         ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
82
83     errno = 0;
84
85     /*
86      * Croak if the uidl directory does not exist.
87      * This probably means an NFS mount failed and we can't
88      * see a uidl file that ought to be there.
89      * Question: is this a portable check? It's not clear
90      * that all implementations of lstat() will return ENOTDIR
91      * rather than plain ENOENT in this case...
92      */
93    if (lstat(idfile, &statbuf) < 0) {
94      if (errno == ENOTDIR) 
95     {
96       report(stderr, "lstat: %s: %s\n", idfile, strerror(errno));
97       exit(PS_IOERR);
98     }
99    }
100
101     /* let's get stored message UIDs from previous queries */
102     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
103     {
104         char buf[POPBUFSIZE+1];
105         char *host = NULL;      /* pacify -Wall */
106         char *user;
107         char *id;
108         char *atsign;   /* temp pointer used in parsing user and host */
109         char *delimp1;
110         char saveddelim1;
111         char *delimp2;
112         char saveddelim2 = '\0';        /* pacify -Wall */
113
114         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
115         {
116           /*
117            * At this point, we assume the bug has two fields -- a user@host 
118            * part, and an ID part. Either field may contain spurious @ signs. 
119            * The previous version of this code presumed one could split at 
120            * the rightmost '@'.  This is not correct, as InterMail puts an 
121            * '@' in the UIDL.
122            */
123           
124           /* very first, skip leading spaces */
125           user = buf + strspn(buf, " \t");
126
127           /* First, we split the buf into a userhost part and an id part */
128           if ( (delimp1 = strpbrk(user, " \t")) != NULL ) {
129                 id = delimp1 + strspn(delimp1, " \t");  /* set pointer to id */
130                 saveddelim1 = *delimp1; /* save char after token */
131                 *delimp1 = '\0';                /* delimit token with \0 */
132                 if (id != NULL) {
133                         /* now remove trainling white space chars from id */
134                         if ( (delimp2 = strpbrk(id, " \t\n")) != NULL ) {
135                                 saveddelim2 = *delimp2;
136                                 *delimp2 = '\0';
137                         }
138                         atsign = strrchr(user, '@');
139                         if (atsign) {
140                                 *atsign = '\0';
141                                 host = atsign + 1;
142
143                         }
144                         for (ctl = hostlist; ctl; ctl = ctl->next) {
145                                 if (ctl->server.truename &&
146                                     strcasecmp(host, ctl->server.truename) == 0
147                                     && strcasecmp(user, ctl->remotename) == 0) {
148         
149                                         save_str(&ctl->oldsaved, id, UID_SEEN);
150                                         break;
151                                 }
152                         }
153                         /* if it's not in a host we're querying,
154                         ** save it anyway */
155                         if (ctl == (struct query *)NULL) {
156                                 /* restore string */
157                                 *delimp1 = saveddelim1;
158                                 *atsign = '@';
159                                 if (delimp2 != NULL) {
160                                         *delimp2 = saveddelim2;
161                                 }
162                                 save_str(&scratchlist, buf, UID_SEEN);
163                         }
164                 }
165             }
166         }
167         fclose(tmpfp);
168     }
169
170     if (outlevel >= O_DEBUG)
171     {
172         struct idlist   *idp;
173         int uidlcount = 0;
174
175         for (ctl = hostlist; ctl; ctl = ctl->next)
176             if (ctl->server.uidl)
177             {
178                 report_build(stdout, "Old UID list from %s:",ctl->server.pollname);
179                 for (idp = ctl->oldsaved; idp; idp = idp->next)
180                     report_build(stdout, " %s", idp->id);
181                 if (!idp)
182                     report_build(stdout, " <empty>");
183                 report_complete(stdout, "\n");
184                 uidlcount++;
185             }
186
187         if (uidlcount)
188         {
189             report_build(stdout, "Scratch list of UIDs:");
190             for (idp = scratchlist; idp; idp = idp->next)
191                 report_build(stdout, " %s", idp->id);
192             if (!idp)
193                 report_build(stdout, " <empty>");
194             report_complete(stdout, "\n");
195         }
196     }
197 }
198 #endif /* POP3_ENABLE */
199
200 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
201 /* save a number/UID pair on the given UID list */
202 {
203     struct idlist **end;
204
205     /* do it nonrecursively so the list is in the right order */
206     for (end = idl; *end; end = &(*end)->next)
207         continue;
208
209     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
210     (*end)->val.status.mark = status;
211     (*end)->id = str ? xstrdup(str) : (char *)NULL;
212     (*end)->next = NULL;
213
214     return(*end);
215 }
216
217 void free_str_list(struct idlist **idl)
218 /* free the given UID list */
219 {
220     if (*idl == (struct idlist *)NULL)
221         return;
222
223     free_str_list(&(*idl)->next);
224     free ((*idl)->id);
225     free(*idl);
226     *idl = (struct idlist *)NULL;
227 }
228
229 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
230 /* save an ID pair on the given list */
231 {
232     struct idlist **end;
233
234     /* do it nonrecursively so the list is in the right order */
235     for (end = idl; *end; end = &(*end)->next)
236         continue;
237
238     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
239     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
240     if (str2)
241         (*end)->val.id2 = xstrdup(str2);
242     else
243         (*end)->val.id2 = (char *)NULL;
244     (*end)->next = (struct idlist *)NULL;
245 }
246
247 #ifdef __UNUSED__
248 void free_str_pair_list(struct idlist **idl)
249 /* free the given ID pair list */
250 {
251     if (*idl == (struct idlist *)NULL)
252         return;
253
254     free_idpair_list(&(*idl)->next);
255     free ((*idl)->id);
256     free ((*idl)->val.id2);
257     free(*idl);
258     *idl = (struct idlist *)NULL;
259 }
260 #endif
261
262 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
263 /* is a given ID in the given list? (comparison may be caseblind) */
264 {
265     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
266         return(0);
267     else if (!caseblind && strcmp(str, (*idl)->id) == 0)
268         return(1);
269     else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
270         return(1);
271     else
272         return(str_in_list(&(*idl)->next, str, caseblind));
273 }
274
275 int str_nr_in_list( struct idlist **idl, const char *str )
276   /* return the position of str in idl */
277 {
278     int nr;
279     struct idlist *walk;
280     if ( !str )
281         return -1;
282     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
283         if( strcmp( str, walk->id) == 0 )
284             return nr;
285     return -1;
286 }
287
288 int str_nr_last_in_list( struct idlist **idl, const char *str)
289 /* return the last position of str in idl */
290 {
291     int nr, ret = -1;
292     struct idlist *walk;
293     if ( !str )
294         return -1;
295     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
296         if( strcmp( str, walk->id) == 0 )
297             ret = nr;
298     return ret;
299 }
300
301 void str_set_mark( struct idlist **idl, const char *str, const flag val)
302 /* update the mark on an of an id to given value */
303 {
304     int nr;
305     struct idlist *walk;
306     if (!str)
307         return;
308     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
309         if (strcmp(str, walk->id) == 0)
310             walk->val.status.mark = val;
311 }
312
313 int count_list( struct idlist **idl)
314 /* count the number of elements in the list */
315 {
316   if( !*idl )
317     return 0;
318   return 1 + count_list( &(*idl)->next );
319 }
320
321 char *str_from_nr_list(struct idlist **idl, int number)
322 /* return the number'th string in idl */
323 {
324     if( !*idl  || number < 0)
325         return 0;
326     if( number == 0 )
327         return (*idl)->id;
328     return str_from_nr_list(&(*idl)->next, number-1);
329 }
330
331     
332 char *str_find(struct idlist **idl, int number)
333 /* return the id of the given number in the given list. */
334 {
335     if (*idl == (struct idlist *) 0)
336         return((char *) 0);
337     else if (number == (*idl)->val.status.num)
338         return((*idl)->id);
339     else
340         return(str_find(&(*idl)->next, number));
341 }
342
343 char *idpair_find(struct idlist **idl, const char *id)
344 /* return the id of the given id in the given list (caseblind comparison) */
345 {
346     if (*idl == (struct idlist *) 0)
347         return((char *) 0);
348     else if (strcasecmp(id, (*idl)->id) == 0)
349         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
350     else
351         return(idpair_find(&(*idl)->next, id));
352 }
353
354 int delete_str(struct idlist **idl, int num)
355 /* delete given message from given list */
356 {
357     struct idlist       *idp;
358
359     for (idp = *idl; idp; idp = idp->next)
360         if (idp->val.status.num == num)
361         {
362             idp->val.status.mark = UID_DELETED;
363             return(1);
364         }
365     return(0);
366 }
367
368 void append_str_list(struct idlist **idl, struct idlist **nidl)
369 /* append nidl to idl (does not copy *) */
370 {
371     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
372         return;
373     else if ((*idl) == (struct idlist *)NULL)
374         *idl = *nidl;
375     else if ((*idl)->next == (struct idlist *)NULL)
376         (*idl)->next = *nidl;
377     else if ((*idl)->next != *nidl)
378         append_str_list(&(*idl)->next, nidl);
379 }
380
381 #ifdef POP3_ENABLE
382 void expunge_uids(struct query *ctl)
383 /* assert that all UIDs marked deleted have actually been expunged */
384 {
385     struct idlist *idl;
386
387     for (idl = ctl->newsaved; idl; idl = idl->next)
388         if (idl->val.status.mark == UID_DELETED)
389             idl->val.status.mark = UID_EXPUNGED;
390 }
391
392 void uid_end_query(struct query *ctl) 
393 /* finish a query */
394 {
395     /* old state of mailbox is now irrelevant */
396     free_str_list(&ctl->oldsaved);
397     free_str_list(&scratchlist);
398     ctl->oldsaved = ctl->newsaved;
399     ctl->newsaved = (struct idlist *) NULL;
400
401     /* debugging code */
402     if (ctl->server.uidl && outlevel >= O_DEBUG)
403     {
404         struct idlist *idp;
405
406         report_build(stdout, "New UID list from %s:", ctl->server.pollname);
407         for (idp = ctl->oldsaved; idp; idp = idp->next)
408             report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
409         if (!idp)
410             report_build(stdout, " <empty>");
411         report_complete(stdout, "\n");
412     }
413 }
414
415 void write_saved_lists(struct query *hostlist, const char *idfile)
416 /* perform end-of-run write of seen-messages list */
417 {
418     int         idcount;
419     FILE        *tmpfp;
420     struct query *ctl;
421     struct idlist *idp;
422
423     /* if all lists are empty, nuke the file */
424     idcount = 0;
425     for (ctl = hostlist; ctl; ctl = ctl->next) {
426         if (ctl->oldsaved)
427             idcount++;
428     }
429
430     /* either nuke the file or write updated last-seen IDs */
431     if (!idcount && !scratchlist)
432     {
433         if (outlevel >= O_DEBUG)
434             report(stdout, "Deleting fetchids file.\n");
435         unlink(idfile);
436     }
437     else
438         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
439             for (ctl = hostlist; ctl; ctl = ctl->next) {
440                 for (idp = ctl->oldsaved; idp; idp = idp->next)
441                     if (idp->val.status.mark == UID_SEEN
442                                 || idp->val.status.mark == UID_DELETED)
443                         fprintf(tmpfp, "%s@%s %s\n", 
444                             ctl->remotename, ctl->server.truename, idp->id);
445             }
446             for (idp = scratchlist; idp; idp = idp->next)
447                 fputs(idp->id, tmpfp);
448             fclose(tmpfp);
449         }
450 }
451 #endif /* POP3_ENABLE */
452
453 /* uid.c ends here */