]> Pileus Git - ~andy/fetchmail/blob - uid.c
Fix a "not reached".
[~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  * Here's the theory:
27  *
28  * At start of a query, we have a (possibly empty) list of UIDs to be
29  * considered seen in `oldsaved'.  These are messages that were left in
30  * the mailbox and *not deleted* on previous queries (we don't need to
31  * remember the UIDs of deleted messages because ... well, they're gone!)
32  * This list is initially set up by initialized_saved_list() from the
33  * .fetchids file.
34  *
35  * Early in the query, during the execution of the protocol-specific 
36  * getrange code, the driver expects that the host's `newsaved' member
37  * will be filled with a list of UIDs and message numbers representing
38  * the mailbox state.  If this list is empty, the server did
39  * not respond to the request for a UID listing.
40  *
41  * Each time a message is fetched, we can check its UID against the
42  * `oldsaved' list to see if it is old.  If not, it should be downloaded
43  * (and possibly deleted).  It should be downloaded anyway if --all
44  * is on.  It should not be deleted if --keep is on.
45  *
46  * Each time a message is deleted, we remove its id from the `newsaved'
47  * member.
48  *
49  * At the end of the query, whatever remains in the `newsaved' member
50  * (because it was not deleted) becomes the `oldsaved' list.  The old
51  * `oldsaved' list is freed.
52  *
53  * At the end of the fetchmail run, all current `oldsaved' lists are
54  * flushed out to the .fetchids file to be picked up by the next run.
55  * If there are no such messages, the file is deleted.
56  *
57  * Note: all comparisons are caseblind!
58  */
59
60 /* UIDs associated with un-queried hosts */
61 static struct idlist *scratchlist;
62
63 void initialize_saved_lists(struct query *hostlist, const char *idfile)
64 /* read file of saved IDs and attach to each host */
65 {
66     int st;
67     FILE        *tmpfp;
68     struct query *ctl;
69
70     /* make sure lists are initially empty */
71     for (ctl = hostlist; ctl; ctl = ctl->next)
72         ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
73
74     /* let's get stored message UIDs from previous queries */
75     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
76         char buf[POPBUFSIZE+1],host[HOSTLEN+1],user[USERNAMELEN+1],id[IDLEN+1];
77
78         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
79         {
80             /* possible lossage here with very old versions of sscanf(3)... */
81             if ((st = sscanf(buf, "%[^@]@%s %s\n", user, host, id)) == 3)
82             {
83                 for (ctl = hostlist; ctl; ctl = ctl->next)
84                 {
85                     if (ctl->server.truename &&
86                         strcasecmp(host, ctl->server.truename) == 0
87                                 && strcasecmp(user, ctl->remotename) == 0)
88                     {
89                         save_str(&ctl->oldsaved, -1, id);
90                         break;
91                     }
92                 }
93
94                 /* if it's not in a host we're querying, save it anyway */
95                 if (ctl == (struct query *)NULL)
96                     save_str(&scratchlist, -1, buf);
97             }
98         }
99         fclose(tmpfp);
100     }
101 }
102
103 struct idlist *save_str(struct idlist **idl, int num, const char *str)
104 /* save a number/UID pair on the given UID list */
105 {
106     struct idlist **end;
107
108     /* do it nonrecursively so the list is in the right order */
109     for (end = idl; *end; end = &(*end)->next)
110         continue;
111
112     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
113     (*end)->val.num = num;
114     (*end)->id = str ? xstrdup(str) : (char *)NULL;
115     (*end)->next = NULL;
116
117     return(*end);
118 }
119
120 void free_str_list(struct idlist **idl)
121 /* free the given UID list */
122 {
123     if (*idl == (struct idlist *)NULL)
124         return;
125
126     free_str_list(&(*idl)->next);
127     free ((*idl)->id);
128     free(*idl);
129     *idl = (struct idlist *)NULL;
130 }
131
132 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
133 /* save an ID pair on the given list */
134 {
135     struct idlist **end;
136
137     /* do it nonrecursively so the list is in the right order */
138     for (end = idl; *end; end = &(*end)->next)
139         continue;
140
141     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
142     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
143     if (str2)
144         (*end)->val.id2 = xstrdup(str2);
145     else
146         (*end)->val.id2 = (char *)NULL;
147     (*end)->next = (struct idlist *)NULL;
148 }
149
150 #ifdef __UNUSED__
151 void free_str_pair_list(struct idlist **idl)
152 /* free the given ID pair list */
153 {
154     if (*idl == (struct idlist *)NULL)
155         return;
156
157     free_idpair_list(&(*idl)->next);
158     free ((*idl)->id);
159     free ((*idl)->val.id2);
160     free(*idl);
161     *idl = (struct idlist *)NULL;
162 }
163 #endif
164
165 int str_in_list(struct idlist **idl, const char *str)
166 /* is a given ID in the given list? (comparison is caseblind) */
167 {
168     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
169         return(0);
170     else if (strcasecmp(str, (*idl)->id) == 0)
171         return(1);
172     else
173         return(str_in_list(&(*idl)->next, str));
174 }
175
176 int str_nr_in_list( struct idlist **idl, const char *str )
177   /* return the position of str in idl */
178 {
179     int nr;
180     struct idlist *walk;
181     if ( !str )
182         return -1;
183     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
184         if( strcasecmp( str, walk->id) == 0 )
185             return nr;
186     return -1;
187 }
188
189 int count_list( struct idlist **idl )
190   /* count the number of elements in the list */
191 {
192   if( !*idl )
193     return 0;
194   return 1 + count_list( &(*idl)->next );
195 }
196
197 char* str_from_nr_list( struct idlist **idl, int number )
198   /* return the number'th string in idl */
199 {
200     if( !*idl  || number < 0)
201         return 0;
202     if( number == 0 )
203         return (*idl)->id;
204     return str_from_nr_list(&(*idl)->next, number-1);
205 }
206
207     
208 char *str_find(struct idlist **idl, int number)
209 /* return the id of the given number in the given list. */
210 {
211     if (*idl == (struct idlist *) 0)
212         return((char *) 0);
213     else if (number == (*idl)->val.num)
214         return((*idl)->id);
215     else
216         return(str_find(&(*idl)->next, number));
217 }
218
219 char *idpair_find(struct idlist **idl, const char *id)
220 /* return the id of the given id in the given list (caseblind comparison) */
221 {
222     if (*idl == (struct idlist *) 0)
223         return((char *) 0);
224     else if (strcasecmp(id, (*idl)->id) == 0)
225         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
226     else
227         return(idpair_find(&(*idl)->next, id));
228 }
229
230 int delete_str(struct idlist **idl, int num)
231 /* delete given message from given list */
232 {
233     if (*idl == (struct idlist *)NULL)
234         return(0);
235     else if ((*idl)->val.num == num)
236     {
237         struct idlist   *next = (*idl)->next;
238
239         free ((*idl)->id);
240         free(*idl);
241         *idl = next;
242         return(1);
243     }
244     else
245         return(delete_str(&(*idl)->next, num));
246 }
247
248 void append_str_list(struct idlist **idl, struct idlist **nidl)
249 /* append nidl to idl (does not copy *) */
250 {
251     if ((*idl) == (struct idlist *)NULL)
252         *idl = *nidl;
253     else if ((*idl)->next == (struct idlist *)NULL)
254         (*idl)->next = *nidl;
255     else if ((*idl)->next != *nidl)
256         append_str_list(&(*idl)->next, nidl);
257 }
258
259 void update_str_lists(struct query *ctl)
260 /* perform end-of-query actions on UID lists */
261 {
262     free_str_list(&ctl->oldsaved);
263     ctl->oldsaved = ctl->newsaved;
264     ctl->newsaved = (struct idlist *) NULL;
265 }
266
267 void write_saved_lists(struct query *hostlist, const char *idfile)
268 /* perform end-of-run write of seen-messages list */
269 {
270     int         idcount;
271     FILE        *tmpfp;
272     struct query *ctl;
273     struct idlist *idp;
274
275     /* if all lists are empty, nuke the file */
276     idcount = 0;
277     for (ctl = hostlist; ctl; ctl = ctl->next) {
278         if (ctl->oldsaved)
279             idcount++;
280     }
281
282     /* either nuke the file or write updated last-seen IDs */
283     if (!idcount)
284         unlink(idfile);
285     else
286         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
287             for (ctl = hostlist; ctl; ctl = ctl->next) {
288                 for (idp = ctl->oldsaved; idp; idp = idp->next)
289                     fprintf(tmpfp, "%s@%s %s\n", 
290                             ctl->remotename, ctl->server.truename, idp->id);
291             }
292             for (idp = scratchlist; idp; idp = idp->next)
293                 fputs(idp->id, tmpfp);
294             fclose(tmpfp);
295         }
296 }
297
298 /* uid.c ends here */