]> Pileus Git - ~andy/fetchmail/blob - uid.c
06acd5a9820c46e56a4b5cba97dc0987f4c1e04a
[~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 I feel
33  * sufficiently motivated at some point.
34  *
35  * Here's the theory:
36  *
37  * At start of a query, we have a (possibly empty) list of UIDs to be
38  * considered `already seen'.  These are messages that were left in
39  * the mailbox and *not deleted* on previous queries (we don't need to
40  * remember the UIDs of deleted messages because ... well, they're gone!).
41  * This list is set up by initialized_saved_list() from the .fetchids 
42  * file and hangs off the host's `saved' member.
43  *
44  * Early in the query, during the execution of the protocol-specific 
45  * getrange code, the driver expects that the host's `listed' member
46  * will be filled with a list of UIDs and message numbers representing
47  * the current mailbox state.  If this list is empty, the server did
48  * not respond to the request for a UID listing.
49  *
50  * Each time a message is fetched, we can check its UID against the
51  * `saved' list to see if it is old.  If not, it should be downloaded
52  * (and possibly deleted).  It should be downloaded anyway if --all
53  * is on.  It should not be deleted if --keep is on.
54  *
55  * Each time a message is deleted, we remove its id from the `listed'
56  * member.
57  *
58  * At the end of the query, whatever remains in the `listed' member
59  * (because it was not deleted) becomes the `saved' list.  The old
60  * `saved' list is freed.
61  */
62
63 /* UIDs associated with un-queried hosts */
64 static struct idlist *scratchlist;
65
66 void initialize_saved_lists(hostlist, idfile)
67 /* read file of saved IDs and attach to each host */
68 struct hostrec *hostlist;
69 char *idfile;
70 {
71     int st;
72     FILE        *tmpfp;
73     struct hostrec *hostp;
74
75     /* make sure lists are initially empty */
76     for (hostp = hostlist; hostp; hostp = hostp->next)
77         hostp->saved = hostp->mailbox = (struct idlist *)NULL;
78
79     /* let's get stored message UIDs from previous queries */
80     if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL) {
81         char buf[POPBUFSIZE+1], host[HOSTLEN+1], id[IDLEN+1];
82
83         while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
84         {
85             if ((st = sscanf(buf, "%s %s\n", host, id)) == 2)
86             {
87                 for (hostp = hostlist; hostp; hostp = hostp->next)
88                 {
89                     if (strcmp(host, hostp->servername) == 0)
90                     {
91                         save_uid(&hostp->saved, -1, id);
92                         break;
93                     }
94                 }
95
96                 /* if it's not in a host we're querying, save it anyway */
97                 if (hostp == (struct hostrec *)NULL)
98                     save_uid(&scratchlist, -1, buf);
99             }
100         }
101         fclose(tmpfp);
102     }
103 }
104
105 void save_uid(idl, num, str)
106 /* save a number/UID pair on the given UID list */
107 struct idlist **idl;
108 int num;
109 char *str;
110 {
111     struct idlist *new;
112
113     new = (struct idlist *)xmalloc(sizeof(struct idlist));
114     new->num = num;
115     new->id = strdup(str);
116     new->next = *idl;
117     *idl = new;
118 }
119
120 void free_uid_list(idl)
121 /* free the given UID list */
122 struct idlist **idl;
123 {
124     if (*idl == (struct idlist *)NULL)
125         return;
126
127     free_uid_list(&(*idl)->next);
128     free ((*idl)->id);
129     free(*idl);
130     *idl = (struct idlist *)NULL;
131 }
132
133 int uid_in_list(idl, str)
134 /* is a given ID in the given list? */
135 struct idlist **idl;
136 char *str;
137 {
138     if (*idl == (struct idlist *)NULL)
139         return(0);
140     else if (strcmp(str, (*idl)->id) == 0)
141         return(1);
142     else
143         return(uid_in_list(&(*idl)->next, str));
144 }
145
146 int delete_uid(idl, str)
147 /* delete given UID from given list */
148 struct idlist **idl;
149 char *str;
150 {
151     if (*idl == (struct idlist *)NULL)
152         return(0);
153     else if (strcmp((*idl)->id, str) == 0)
154     {
155         struct idlist   *next = (*idl)->next;
156
157         free ((*idl)->id);
158         free(*idl);
159         *idl = next;
160         return(1);
161     }
162     else
163         return(delete_uid(&(*idl)->next, str));
164     return(0);
165 }
166
167 void update_uid_lists(hostp)
168 /* perform end-of-query actions on UID lists */
169 struct hostrec *hostp;
170 {
171     /*
172      * Replace `saved' list with `mailbox' list as modified by deletions.
173      */
174     free_uid_list(&hostp->saved);
175     hostp->saved = hostp->mailbox;
176 }
177
178 void write_saved_lists(hostlist, idfile)
179 struct hostrec *hostlist;
180 char *idfile;
181 {
182     int st, idcount;
183     FILE        *tmpfp;
184     struct hostrec *hostp;
185     struct idlist *idp;
186
187     /* if all lists are empty, nuke the file */
188     idcount = 0;
189     for (hostp = hostlist; hostp; hostp = hostp->next) {
190         if (hostp->saved)
191             idcount++;
192     }
193
194     /* either nuke the file or write updated last-seen IDs */
195     if (!idcount)
196         unlink(idfile);
197     else
198         if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
199             for (hostp = hostlist; hostp; hostp = hostp->next) {
200                 for (idp = hostp->saved; idp; idp = idp->next)
201                     fprintf(tmpfp, "%s %s\n", hostp->servername, idp->id);
202             }
203             for (idp = scratchlist; idp; idp = idp->next)
204                 fputs(idp->id, tmpfp);
205             fclose(tmpfp);
206         }
207 }