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