]> Pileus Git - ~andy/fetchmail/blob - uid.c
Correct generation of X-Fetchmail-Warning header.
[~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(struct query *hostlist, char *idfile)
66 /* read file of saved IDs and attach to each host */
67 {
68     int st;
69     FILE        *tmpfp;
70     struct query *hostp;
71
72     /* make sure lists are initially empty */
73     for (hostp = hostlist; hostp; hostp = hostp->next)
74         hostp->oldsaved = hostp->newsaved = (struct idlist *)NULL;
75
76     /* let's get stored message UIDs from previous queries */
77     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
78         char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
79
80         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
81         {
82             if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
83             {
84                 for (hostp = hostlist; hostp; hostp = hostp->next)
85                 {
86                     if (strcmp(host, hostp->servername) == 0)
87                     {
88                         save_uid(&hostp->oldsaved, -1, id);
89                         break;
90                     }
91                 }
92
93                 /* if it's not in a host we're querying, save it anyway */
94                 if (hostp == (struct query *)NULL)
95                     save_uid(&scratchlist, -1, buf);
96             }
97         }
98         fclose(tmpfp);
99     }
100 }
101
102 struct idlist *save_uid(struct idlist **idl, int num, char *str)
103 /* save a number/UID pair on the given UID list */
104 {
105     struct idlist *new;
106
107     new = (struct idlist *)xmalloc(sizeof(struct idlist));
108     new->val.num = num;
109     new->id = xstrdup(str);
110     new->next = *idl;
111     *idl = new;
112
113     return(new);
114 }
115
116 void free_uid_list(struct idlist **idl)
117 /* free the given UID list */
118 {
119     if (*idl == (struct idlist *)NULL)
120         return;
121
122     free_uid_list(&(*idl)->next);
123     free ((*idl)->id);
124     free(*idl);
125     *idl = (struct idlist *)NULL;
126 }
127
128 void save_id_pair(struct idlist **idl, char *str1, char *str2)
129 /* save an ID pair on the given list */
130 {
131     struct idlist *new;
132
133     new = (struct idlist *)xmalloc(sizeof(struct idlist));
134     new->id = xstrdup(str1);
135     if (str2)
136         new->val.id2 = xstrdup(str2);
137     else
138         new->val.id2 = (char *)NULL;
139     new->next = *idl;
140     *idl = new;
141 }
142
143 #ifdef __UNUSED__
144 void free_idpair_list(struct idlist **idl)
145 /* free the given ID pair list */
146 {
147     if (*idl == (struct idlist *)NULL)
148         return;
149
150     free_idpair_list(&(*idl)->next);
151     free ((*idl)->id);
152     free ((*idl)->val.id2);
153     free(*idl);
154     *idl = (struct idlist *)NULL;
155 }
156 #endif
157
158 int uid_in_list(struct idlist **idl, char *str)
159 /* is a given ID in the given list? */
160 {
161     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
162         return(0);
163     else if (strcmp(str, (*idl)->id) == 0)
164         return(1);
165     else
166         return(uid_in_list(&(*idl)->next, str));
167 }
168
169 char *uid_find(struct idlist **idl, int number)
170 /* return the id of the given number in the given list. */
171 {
172     if (*idl == (struct idlist *) 0)
173         return((char *) 0);
174     else if (number == (*idl)->val.num)
175         return((*idl)->id);
176     else
177         return(uid_find(&(*idl)->next, number));
178 }
179
180 char *idpair_find(struct idlist **idl, char *id)
181 /* return the id of the given number in the given list. */
182 {
183     if (*idl == (struct idlist *) 0)
184         return((char *) 0);
185     else if (strcmp(id, (*idl)->id) == 0)
186         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
187     else
188         return(idpair_find(&(*idl)->next, id));
189 }
190
191 int delete_uid(struct idlist **idl, int num)
192 /* delete given message from given list */
193 {
194     if (*idl == (struct idlist *)NULL)
195         return(0);
196     else if ((*idl)->val.num == num)
197     {
198         struct idlist   *next = (*idl)->next;
199
200         free ((*idl)->id);
201         free(*idl);
202         *idl = next;
203         return(1);
204     }
205     else
206         return(delete_uid(&(*idl)->next, num));
207     return(0);
208 }
209
210 void append_uid_list(struct idlist **idl, struct idlist **nidl)
211 /* append nidl to idl (does not copy *) */
212 {
213     if ((*idl) == (struct idlist *)NULL)
214         *idl = *nidl;
215     else if ((*idl)->next == (struct idlist *)NULL)
216         (*idl)->next = *nidl;
217     else
218         append_uid_list(&(*idl)->next, nidl);
219 }
220
221 void update_uid_lists(struct query *hostp)
222 /* perform end-of-query actions on UID lists */
223 {
224     free_uid_list(&hostp->oldsaved);
225     hostp->oldsaved = hostp->newsaved;
226     hostp->newsaved = (struct idlist *) NULL;
227 }
228
229 void write_saved_lists(struct query *hostlist, char *idfile)
230 /* perform end-of-run write of seen-messages list */
231 {
232     int         idcount;
233     FILE        *tmpfp;
234     struct query *hostp;
235     struct idlist *idp;
236
237     /* if all lists are empty, nuke the file */
238     idcount = 0;
239     for (hostp = hostlist; hostp; hostp = hostp->next) {
240         if (hostp->oldsaved)
241             idcount++;
242     }
243
244     /* either nuke the file or write updated last-seen IDs */
245     if (!idcount)
246         unlink(idfile);
247     else
248         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
249             for (hostp = hostlist; hostp; hostp = hostp->next) {
250                 for (idp = hostp->oldsaved; idp; idp = idp->next)
251                     fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
252             }
253             for (idp = scratchlist; idp; idp = idp->next)
254                 fputs(idp->id, tmpfp);
255             fclose(tmpfp);
256         }
257 }
258
259 /* uid.c ends here */