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