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