]> Pileus Git - ~andy/fetchmail/blob - uid.c
Fix embarassing bug.
[~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
11 #if defined(STDC_HEADERS)
12 #include <stdlib.h>
13 #include <string.h>
14 #endif
15
16 #if defined(HAVE_UNISTD_H)
17 #include <unistd.h>
18 #endif
19
20 #include "fetchmail.h"
21
22 /*
23  * Machinery for handling UID lists live here.  This is mainly to support
24  * RFC1725-conformant POP3 servers without a LAST command, but may also be
25  * useful for making the IMAP4 querying logic UID-oriented, if a future
26  * revision of IMAP forces me to.  (This would be bad.  Server-side 
27  * seen bits are better than UIDs, because they track messages seen by
28  * *all* clients.)
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 initialized_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.  If not, it should be downloaded
47  * (and possibly deleted).  It should be downloaded anyway if --all
48  * is on.  It should not be deleted if --keep is on.
49  *
50  * Each time a message is deleted, we remove its id from the `newsaved'
51  * member.
52  *
53  * At the end of the query, whatever remains in the `newsaved' member
54  * (because it was not deleted) becomes the `oldsaved' list.  The old
55  * `oldsaved' list is freed.
56  *
57  * At the end of the fetchmail run, all current `oldsaved' lists are
58  * flushed out to the .fetchids file to be picked up by the next run.
59  * If there are no such messages, the file is deleted.
60  */
61
62 /* UIDs associated with un-queried hosts */
63 static struct idlist *scratchlist;
64
65 void initialize_saved_lists(hostlist, idfile)
66 /* read file of saved IDs and attach to each host */
67 struct hostrec *hostlist;
68 char *idfile;
69 {
70     int st;
71     FILE        *tmpfp;
72     struct hostrec *hostp;
73
74     /* make sure lists are initially empty */
75     for (hostp = hostlist; hostp; hostp = hostp->next)
76         hostp->oldsaved = hostp->newsaved = (struct idlist *)NULL;
77
78     /* let's get stored message UIDs from previous queries */
79     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
80         char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
81
82         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
83         {
84             if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
85             {
86                 for (hostp = hostlist; hostp; hostp = hostp->next)
87                 {
88                     if (strcmp(host, hostp->servername) == 0)
89                     {
90                         save_uid(&hostp->oldsaved, -1, id);
91                         break;
92                     }
93                 }
94
95                 /* if it's not in a host we're querying, save it anyway */
96                 if (hostp == (struct hostrec *)NULL)
97                     save_uid(&scratchlist, -1, buf);
98             }
99         }
100         fclose(tmpfp);
101     }
102 }
103
104 void save_uid(idl, num, str)
105 /* save a number/UID pair on the given UID list */
106 struct idlist **idl;
107 int num;
108 char *str;
109 {
110     struct idlist *new;
111
112     new = (struct idlist *)xmalloc(sizeof(struct idlist));
113     new->val.num = num;
114     new->id = xstrdup(str);
115     new->next = *idl;
116     *idl = new;
117 }
118
119 void free_uid_list(idl)
120 /* free the given UID list */
121 struct idlist **idl;
122 {
123     if (*idl == (struct idlist *)NULL)
124         return;
125
126     free_uid_list(&(*idl)->next);
127     free ((*idl)->id);
128     free(*idl);
129     *idl = (struct idlist *)NULL;
130 }
131
132 void save_id_pair(idl, str1, str2)
133 /* save an ID pair on the given list */
134 struct idlist **idl;
135 char *str1, *str2;
136 {
137     struct idlist *new;
138
139     new = (struct idlist *)xmalloc(sizeof(struct idlist));
140     new->id = xstrdup(str1);
141     if (str2)
142         new->val.id2 = xstrdup(str2);
143     else
144         new->val.id2 = (char *)NULL;
145     new->next = *idl;
146     *idl = new;
147 }
148
149 #ifdef __UNUSED__
150 void free_idpair_list(idl)
151 /* free the given ID pair list */
152 struct idlist **idl;
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 uid_in_list(idl, str)
166 /* is a given ID in the given list? */
167 struct idlist **idl;
168 char *str;
169 {
170     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
171         return(0);
172     else if (strcmp(str, (*idl)->id) == 0)
173         return(1);
174     else
175         return(uid_in_list(&(*idl)->next, str));
176 }
177
178 char *uid_find(idl, number)
179 /* return the id of the given number in the given list. */
180 struct idlist **idl;
181 int number;
182 {
183     if (*idl == (struct idlist *) 0)
184         return((char *) 0);
185     else if (number == (*idl)->val.num)
186         return((*idl)->id);
187     else
188         return(uid_find(&(*idl)->next, number));
189 }
190
191 char *idpair_find(idl, id)
192 /* return the id of the given number in the given list. */
193 struct idlist **idl;
194 char *id;
195 {
196     if (*idl == (struct idlist *) 0)
197         return((char *) 0);
198     else if (strcmp(id, (*idl)->id) == 0)
199         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
200     else
201         return(idpair_find(&(*idl)->next, id));
202 }
203
204 int delete_uid(idl, num)
205 /* delete given message from given list */
206 struct idlist **idl;
207 int num;
208 {
209     if (*idl == (struct idlist *)NULL)
210         return(0);
211     else if ((*idl)->val.num == num)
212     {
213         struct idlist   *next = (*idl)->next;
214
215         free ((*idl)->id);
216         free(*idl);
217         *idl = next;
218         return(1);
219     }
220     else
221         return(delete_uid(&(*idl)->next, num));
222     return(0);
223 }
224
225 void append_uid_list(idl, nidl)
226 /* append nidl to idl (does not copy *) */
227 struct idlist **idl;
228 struct idlist **nidl;
229 {
230     if ((*idl) == (struct idlist *)NULL)
231         *idl = *nidl;
232     else if ((*idl)->next == (struct idlist *)NULL)
233         (*idl)->next = *nidl;
234     else
235         append_uid_list(&(*idl)->next, nidl);
236 }
237
238 void update_uid_lists(hostp)
239 /* perform end-of-query actions on UID lists */
240 struct hostrec *hostp;
241 {
242     free_uid_list(&hostp->oldsaved);
243     hostp->oldsaved = hostp->newsaved;
244     hostp->newsaved = (struct idlist *) NULL;
245 }
246
247 void write_saved_lists(hostlist, idfile)
248 /* perform end-of-run write of seen-messages list */
249 struct hostrec *hostlist;
250 char *idfile;
251 {
252     int st, idcount;
253     FILE        *tmpfp;
254     struct hostrec *hostp;
255     struct idlist *idp;
256
257     /* if all lists are empty, nuke the file */
258     idcount = 0;
259     for (hostp = hostlist; hostp; hostp = hostp->next) {
260         if (hostp->oldsaved)
261             idcount++;
262     }
263
264     /* either nuke the file or write updated last-seen IDs */
265     if (!idcount)
266         unlink(idfile);
267     else
268         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
269             for (hostp = hostlist; hostp; hostp = hostp->next) {
270                 for (idp = hostp->oldsaved; idp; idp = idp->next)
271                     fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
272             }
273             for (idp = scratchlist; idp; idp = idp->next)
274                 fputs(idp->id, tmpfp);
275             fclose(tmpfp);
276         }
277 }
278
279 /* uid.c ends here */