]> Pileus Git - ~andy/fetchmail/blob - uid.c
186d4b352b578cc748a2a06977fa17725476766f
[~andy/fetchmail] / uid.c
1 /*
2  * For license terms, see the file COPYING in this directory.
3  */
4
5 /***********************************************************************
6   module:       uid.c
7   project:      fetchmail
8   programmer:   Eric S. Raymond
9   description:  UID list handling
10
11  ***********************************************************************/
12
13 #include <config.h>
14
15 #include <stdio.h>
16
17 #if defined(STDC_HEADERS)
18 #include <stdlib.h>
19 #include <string.h>
20 #endif
21
22 #if defined(HAVE_UNISTD_H)
23 #include <unistd.h>
24 #endif
25
26 #include "fetchmail.h"
27
28 /*
29  * Machinery for handling UID lists live here.  This is mainly to support
30  * RFC1725-conformant POP3 servers without a LAST command, but may also be
31  * useful for making the IMAP4 querying logic UID-oriented, if a future
32  * revision of IMAP forces me to.  (This would be bad.  Server-side 
33  * seen bits are better than UIDs, because they track messages seen by
34  * *all* clients.)
35  *
36  * Here's the theory:
37  *
38  * At start of a query, we have a (possibly empty) list of UIDs to be
39  * considered seen in `oldsaved'.  These are messages that were left in
40  * the mailbox and *not deleted* on previous queries (we don't need to
41  * remember the UIDs of deleted messages because ... well, they're gone!)
42  * This list is initially set up by initialized_saved_list() from the
43  * .fetchids file.
44  *
45  * Early in the query, during the execution of the protocol-specific 
46  * getrange code, the driver expects that the host's `newsaved' member
47  * will be filled with a list of UIDs and message numbers representing
48  * the mailbox state.  If this list is empty, the server did
49  * not respond to the request for a UID listing.
50  *
51  * Each time a message is fetched, we can check its UID against the
52  * `oldsaved' list to see if it is old.  If not, it should be downloaded
53  * (and possibly deleted).  It should be downloaded anyway if --all
54  * is on.  It should not be deleted if --keep is on.
55  *
56  * Each time a message is deleted, we remove its id from the `newsaved'
57  * member.
58  *
59  * At the end of the query, whatever remains in the `newsaved' member
60  * (because it was not deleted) becomes the `oldsaved' list.  The old
61  * `oldsaved' list is freed.
62  *
63  * At the end of the fetchmail run, all current `oldsaved' lists are
64  * flushed out to the .fetchids file to be picked up by the next run.
65  * If there are no such messages, the file is deleted.
66  */
67
68 /* UIDs associated with un-queried hosts */
69 static struct idlist *scratchlist;
70
71 void initialize_saved_lists(hostlist, idfile)
72 /* read file of saved IDs and attach to each host */
73 struct hostrec *hostlist;
74 char *idfile;
75 {
76     int st;
77     FILE        *tmpfp;
78     struct hostrec *hostp;
79
80     /* make sure lists are initially empty */
81     for (hostp = hostlist; hostp; hostp = hostp->next)
82         hostp->oldsaved = hostp->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], id[IDLEN+1];
87
88         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
89         {
90             if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
91             {
92                 for (hostp = hostlist; hostp; hostp = hostp->next)
93                 {
94                     if (strcmp(host, hostp->servername) == 0)
95                     {
96                         save_uid(&hostp->oldsaved, -1, id);
97                         break;
98                     }
99                 }
100
101                 /* if it's not in a host we're querying, save it anyway */
102                 if (hostp == (struct hostrec *)NULL)
103                     save_uid(&scratchlist, -1, buf);
104             }
105         }
106         fclose(tmpfp);
107     }
108 }
109
110 void save_uid(idl, num, str)
111 /* save a number/UID pair on the given UID list */
112 struct idlist **idl;
113 int num;
114 char *str;
115 {
116     struct idlist *new;
117
118     new = (struct idlist *)xmalloc(sizeof(struct idlist));
119     new->num = num;
120     new->id = strdup(str);
121     new->next = *idl;
122     *idl = new;
123 }
124
125 void free_uid_list(idl)
126 /* free the given UID list */
127 struct idlist **idl;
128 {
129     if (*idl == (struct idlist *)NULL)
130         return;
131
132     free_uid_list(&(*idl)->next);
133     free ((*idl)->id);
134     free(*idl);
135     *idl = (struct idlist *)NULL;
136 }
137
138 int uid_in_list(idl, str)
139 /* is a given ID in the given list? */
140 struct idlist **idl;
141 char *str;
142 {
143     if (*idl == (struct idlist *)NULL || str == (char *) NULL)
144         return(0);
145     else if (strcmp(str, (*idl)->id) == 0)
146         return(1);
147     else
148         return(uid_in_list(&(*idl)->next, str));
149 }
150
151 char *uid_find(idl, number)
152 /* return the id of the given number in the given list. */
153 struct idlist **idl;
154 int number;
155 {
156     if (*idl == (struct idlist *) 0)
157         return((char *) 0);
158     else if (number == (*idl)->num)
159         return((*idl)->id);
160     else
161         return(uid_find(&(*idl)->next, number));
162 }
163
164 int delete_uid(idl, num)
165 /* delete given message from given list */
166 struct idlist **idl;
167 int num;
168 {
169     if (*idl == (struct idlist *)NULL)
170         return(0);
171     else if ((*idl)->num == num)
172     {
173         struct idlist   *next = (*idl)->next;
174
175         free ((*idl)->id);
176         free(*idl);
177         *idl = next;
178         return(1);
179     }
180     else
181         return(delete_uid(&(*idl)->next, num));
182     return(0);
183 }
184
185 void update_uid_lists(hostp)
186 /* perform end-of-query actions on UID lists */
187 struct hostrec *hostp;
188 {
189     free_uid_list(&hostp->oldsaved);
190     hostp->oldsaved = hostp->newsaved;
191     hostp->newsaved = (struct idlist *) NULL;
192 }
193
194 void write_saved_lists(hostlist, idfile)
195 /* perform end-of-run write of seen-messages list */
196 struct hostrec *hostlist;
197 char *idfile;
198 {
199     int st, idcount;
200     FILE        *tmpfp;
201     struct hostrec *hostp;
202     struct idlist *idp;
203
204     /* if all lists are empty, nuke the file */
205     idcount = 0;
206     for (hostp = hostlist; hostp; hostp = hostp->next) {
207         if (hostp->oldsaved)
208             idcount++;
209     }
210
211     /* either nuke the file or write updated last-seen IDs */
212     if (!idcount)
213         unlink(idfile);
214     else
215         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
216             for (hostp = hostlist; hostp; hostp = hostp->next) {
217                 for (idp = hostp->oldsaved; idp; idp = idp->next)
218                     fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
219             }
220             for (idp = scratchlist; idp; idp = idp->next)
221                 fputs(idp->id, tmpfp);
222             fclose(tmpfp);
223         }
224 }