]> Pileus Git - ~andy/fetchmail/blob - uid.c
Instrument the UID code.
[~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 <stdio.h>
10 #include <limits.h>
11 #if defined(STDC_HEADERS)
12 #include <stdlib.h>
13 #include <string.h>
14 #endif
15 #if defined(HAVE_UNISTD_H)
16 #include <unistd.h>
17 #endif
18
19 #include "fetchmail.h"
20
21 /*
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.
26  *
27  * These functions are also used by the rest of the code to maintain
28  * string lists.
29  *
30  * Here's the theory:
31  *
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
37  * .fetchids file.
38  *
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.
44  *
45  * Each time a message is fetched, we can check its UID against the
46  * `oldsaved' list to see if it is old.
47  *
48  * Each time a message-id is seen, we mark it with MARK_SEEN.
49  *
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.
54  *
55  * At the end of the query, the `newsaved' member becomes the
56  * `oldsaved' list.  The old `oldsaved' list is freed.
57  *
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.
62  *
63  * Note: some comparisons (those used for DNS address lists) are caseblind!  
64  */
65
66 /* UIDs associated with un-queried hosts */
67 static struct idlist *scratchlist;
68
69 #ifdef POP3_ENABLE
70 void initialize_saved_lists(struct query *hostlist, const char *idfile)
71 /* read file of saved IDs and attach to each host */
72 {
73     int st;
74     FILE        *tmpfp;
75     struct query *ctl;
76
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;
80
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];
84
85         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
86         {
87             /* possible lossage here with very old versions of sscanf(3)... */
88             if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
89             {
90                 for (ctl = hostlist; ctl; ctl = ctl->next)
91                 {
92                     if (ctl->server.truename &&
93                         strcasecmp(host, ctl->server.truename) == 0
94                                 && strcasecmp(user, ctl->remotename) == 0)
95                     {
96                         save_str(&ctl->oldsaved, id, UID_SEEN);
97                         break;
98                     }
99                 }
100
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);
104             }
105         }
106         fclose(tmpfp);
107     }
108
109     if (outlevel >= O_DEBUG)
110     {
111         struct idlist   *idp;
112
113         for (ctl = hostlist; ctl; ctl = ctl->next)
114         {
115             report(stdout, "Old UID list from %s:", ctl->server.truename);
116             for (idp = ctl->oldsaved; idp; idp = idp->next)
117                 report(stdout, " %s", idp->id);
118             if (!idp)
119                 report(stdout, "<empty>");
120             report(stdout, "\n");
121         }
122
123         report(stdout, "Scratch list of UIDs:");
124         for (idp = scratchlist; idp; idp = idp->next)
125             report(stdout, " %s", idp->id);
126         if (!idp)
127             report(stdout, "<empty>");
128         report(stdout, "\n");
129     }
130 }
131 #endif /* POP3_ENABLE */
132
133 struct idlist *save_str(struct idlist **idl, const char *str, flag status)
134 /* save a number/UID pair on the given UID list */
135 {
136     struct idlist **end;
137
138     /* do it nonrecursively so the list is in the right order */
139     for (end = idl; *end; end = &(*end)->next)
140         continue;
141
142     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
143     (*end)->val.status.mark = status;
144     (*end)->id = str ? xstrdup(str) : (char *)NULL;
145     (*end)->next = NULL;
146
147     return(*end);
148 }
149
150 void free_str_list(struct idlist **idl)
151 /* free the given UID list */
152 {
153     if (*idl == (struct idlist *)NULL)
154         return;
155
156     free_str_list(&(*idl)->next);
157     free ((*idl)->id);
158     free(*idl);
159     *idl = (struct idlist *)NULL;
160 }
161
162 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
163 /* save an ID pair on the given list */
164 {
165     struct idlist **end;
166
167     /* do it nonrecursively so the list is in the right order */
168     for (end = idl; *end; end = &(*end)->next)
169         continue;
170
171     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
172     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
173     if (str2)
174         (*end)->val.id2 = xstrdup(str2);
175     else
176         (*end)->val.id2 = (char *)NULL;
177     (*end)->next = (struct idlist *)NULL;
178 }
179
180 #ifdef __UNUSED__
181 void free_str_pair_list(struct idlist **idl)
182 /* free the given ID pair list */
183 {
184     if (*idl == (struct idlist *)NULL)
185         return;
186
187     free_idpair_list(&(*idl)->next);
188     free ((*idl)->id);
189     free ((*idl)->val.id2);
190     free(*idl);
191     *idl = (struct idlist *)NULL;
192 }
193 #endif
194
195 int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
196 /* is a given ID in the given list? (comparison may be caseblind) */
197 {
198     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
199         return(0);
200     else if (!caseblind && strcmp(str, (*idl)->id) == 0)
201         return(1);
202     else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
203         return(1);
204     else
205         return(str_in_list(&(*idl)->next, str, caseblind));
206 }
207
208 int str_nr_in_list( struct idlist **idl, const char *str )
209   /* return the position of str in idl */
210 {
211     int nr;
212     struct idlist *walk;
213     if ( !str )
214         return -1;
215     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
216         if( strcmp( str, walk->id) == 0 )
217             return nr;
218     return -1;
219 }
220
221 int str_nr_last_in_list( struct idlist **idl, const char *str)
222 /* return the last position of str in idl */
223 {
224     int nr, ret = -1;
225     struct idlist *walk;
226     if ( !str )
227         return -1;
228     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
229         if( strcmp( str, walk->id) == 0 )
230             ret = nr;
231     return ret;
232 }
233
234 void str_set_mark( struct idlist **idl, const char *str, const flag val)
235 /* update the mark on an of an id to given value */
236 {
237     int nr;
238     struct idlist *walk;
239     if (!str)
240         return;
241     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
242         if (strcmp(str, walk->id) == 0)
243             walk->val.status.mark = val;
244 }
245
246 int count_list( struct idlist **idl)
247 /* count the number of elements in the list */
248 {
249   if( !*idl )
250     return 0;
251   return 1 + count_list( &(*idl)->next );
252 }
253
254 char *str_from_nr_list(struct idlist **idl, int number)
255 /* return the number'th string in idl */
256 {
257     if( !*idl  || number < 0)
258         return 0;
259     if( number == 0 )
260         return (*idl)->id;
261     return str_from_nr_list(&(*idl)->next, number-1);
262 }
263
264     
265 char *str_find(struct idlist **idl, int number)
266 /* return the id of the given number in the given list. */
267 {
268     if (*idl == (struct idlist *) 0)
269         return((char *) 0);
270     else if (number == (*idl)->val.status.num)
271         return((*idl)->id);
272     else
273         return(str_find(&(*idl)->next, number));
274 }
275
276 char *idpair_find(struct idlist **idl, const char *id)
277 /* return the id of the given id in the given list (caseblind comparison) */
278 {
279     if (*idl == (struct idlist *) 0)
280         return((char *) 0);
281     else if (strcasecmp(id, (*idl)->id) == 0)
282         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
283     else
284         return(idpair_find(&(*idl)->next, id));
285 }
286
287 int delete_str(struct idlist **idl, int num)
288 /* delete given message from given list */
289 {
290     struct idlist       *idp;
291
292     for (idp = *idl; idp; idp = idp->next)
293         if (idp->val.status.num == num)
294         {
295             idp->val.status.mark = UID_DELETED;
296             return(1);
297         }
298     return(0);
299 }
300
301 void append_str_list(struct idlist **idl, struct idlist **nidl)
302 /* append nidl to idl (does not copy *) */
303 {
304     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
305         return;
306     else if ((*idl) == (struct idlist *)NULL)
307         *idl = *nidl;
308     else if ((*idl)->next == (struct idlist *)NULL)
309         (*idl)->next = *nidl;
310     else if ((*idl)->next != *nidl)
311         append_str_list(&(*idl)->next, nidl);
312 }
313
314 #ifdef POP3_ENABLE
315 void expunge_uids(struct query *ctl)
316 /* assert that all UIDs marked deleted have actually been expunged */
317 {
318     struct idlist *idl;
319
320     for (idl = ctl->newsaved; idl; idl = idl->next)
321         if (idl->val.status.mark == UID_DELETED)
322             idl->val.status.mark = UID_EXPUNGED;
323 }
324
325 void update_str_lists(struct query *ctl)
326 /* perform end-of-query actions on UID lists */
327 {
328     free_str_list(&ctl->oldsaved);
329     ctl->oldsaved = ctl->newsaved;
330     ctl->newsaved = (struct idlist *) NULL;
331
332     if (outlevel >= O_DEBUG)
333     {
334         struct idlist *idp;
335
336         report(stdout, "New UID list from %s:", ctl->server.truename);
337         for (idp = ctl->oldsaved; idp; idp = idp->next)
338             report(stdout, " %s = %d", idp->id, idp->val.status.mark);
339         if (!idp)
340             report(stdout, "<empty>");
341         report(stdout, "\n");
342     }
343 }
344
345 void write_saved_lists(struct query *hostlist, const char *idfile)
346 /* perform end-of-run write of seen-messages list */
347 {
348     int         idcount;
349     FILE        *tmpfp;
350     struct query *ctl;
351     struct idlist *idp;
352
353     /* if all lists are empty, nuke the file */
354     idcount = 0;
355     for (ctl = hostlist; ctl; ctl = ctl->next) {
356         if (ctl->oldsaved)
357             idcount++;
358     }
359
360     /* either nuke the file or write updated last-seen IDs */
361     if (!idcount)
362     {
363         if (outlevel >= O_DEBUG)
364             report(stdout, "Deleting fetchids file.");
365         unlink(idfile);
366     }
367     else
368         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
369             for (ctl = hostlist; ctl; ctl = ctl->next) {
370                 for (idp = ctl->oldsaved; idp; idp = idp->next)
371                     if (idp->val.status.mark == UID_SEEN
372                                 || idp->val.status.mark == UID_DELETED)
373                         fprintf(tmpfp, "%s@%s %s\n", 
374                             ctl->remotename, ctl->server.truename, idp->id);
375             }
376             for (idp = scratchlist; idp; idp = idp->next)
377                 fputs(idp->id, tmpfp);
378             fclose(tmpfp);
379         }
380 }
381 #endif /* POP3_ENABLE */
382
383 /* uid.c ends here */