1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef FETCH_H
20 #define FETCH_H
21 
22 /* Fetch return codes. */
23 #define FETCH_AGAIN 1
24 #define FETCH_BLOCK 2
25 #define FETCH_ERROR 3
26 #define FETCH_MAIL 4
27 #define FETCH_EXIT 5
28 
29 /* Fetch flags. */
30 #define FETCH_PURGE 0x1
31 #define FETCH_EMPTY 0x2
32 #define FETCH_POLL 0x4
33 
34 /* Fetch context. */
35 struct fetch_ctx {
36 	int		 (*state)(struct account *, struct fetch_ctx *);
37 	int		 flags;
38 
39 	struct mail	*mail;
40 
41 	size_t		 llen;
42 	char		*lbuf;
43 };
44 
45 /* Fetch functions. */
46 struct fetch {
47 	const char	*name;
48 	int		 (*first)(struct account *, struct fetch_ctx *);
49 
50 	void		 (*fill)(struct account *, struct iolist *);
51 	int		 (*commit)(struct account *, struct mail *);
52 	void		 (*abort)(struct account *);
53 	u_int		 (*total)(struct account *);
54 	void		 (*desc)(struct account *, char *, size_t);
55 };
56 
57 /* Ranges of mail. */
58 enum fetch_only {
59 	FETCH_ONLY_NEW,
60 	FETCH_ONLY_OLD,
61 	FETCH_ONLY_ALL
62 };
63 
64 /* Fetch maildir data. */
65 struct fetch_maildir_data {
66 	struct strings	*maildirs;
67 
68 	u_int		 total;
69 
70 	struct strings	 unlinklist;
71 
72 	struct strings	*paths;
73 	u_int		 index;
74 	DIR		*dirp;
75 };
76 
77 struct fetch_maildir_mail {
78 	char		 path[MAXPATHLEN];
79 };
80 
81 /* Fetch mbox data. */
82 struct fetch_mbox_data {
83 	struct strings	*mboxes;
84 
85 	ARRAY_DECL(, struct fetch_mbox_mbox *) fmboxes;
86 	u_int		 index;
87 
88 	size_t		 off;
89 
90 	TAILQ_HEAD(, fetch_mbox_mail) kept;
91 };
92 
93 struct fetch_mbox_mbox {
94 	char		*path;
95 	u_int		 reference;
96 	u_int		 total;
97 
98 	int		 fd;
99 	char		*base;
100 	size_t		 size;
101 };
102 
103 struct fetch_mbox_mail {
104 	size_t		 off;
105 	size_t		 size;
106 
107 	struct fetch_mbox_mbox *fmbox;
108 
109 	TAILQ_ENTRY(fetch_mbox_mail) entry;
110 };
111 
112 /* NNTP group entry. */
113 struct fetch_nntp_group {
114 	char		*name;
115 	int		 ignore;
116 
117 	u_int		 size;
118 	u_int		 last;
119 	char		*id;
120 };
121 
122 /* Fetch nntp data. */
123 struct fetch_nntp_data {
124 	char		*path;
125 
126 	char		*user;
127 	char		*pass;
128 	struct server	 server;
129 	struct strings	*names;
130 
131 	u_int		 group;
132 	ARRAY_DECL(, struct fetch_nntp_group *) groups;
133 
134 	int		 flushing;
135 
136 	struct io	*io;
137 };
138 
139 /* Fetch pop3 queues and trees. */
140 TAILQ_HEAD(fetch_pop3_queue, fetch_pop3_mail);
141 RB_HEAD(fetch_pop3_tree, fetch_pop3_mail);
142 
143 /* Fetch pop3 data. */
144 struct fetch_pop3_data {
145 	char		*path;
146 	enum fetch_only	 only;
147 
148 	char		*user;
149 	char		*pass;
150 	struct server	 server;
151 	char		*pipecmd;
152 	int		 starttls;
153 	int		 apop;
154 	int		 uidl;
155 
156 	u_int		 cur;
157 	u_int		 num;
158 
159 	u_int		 total;
160 	u_int		 committed;
161 
162 	/* Mails on the server. */
163 	struct fetch_pop3_tree	serverq;
164 
165 	/* Mails in the cache file. */
166 	struct fetch_pop3_tree	cacheq;
167 
168 	/* Mails to fetch from the server. */
169 	struct fetch_pop3_queue	wantq;
170 
171 	/* Mails ready to be dropped. */
172 	struct fetch_pop3_queue dropq;
173 
174 	int		 flushing;
175 	size_t		 size;
176 
177 	struct io	*io;
178 	struct cmd	*cmd;
179 
180 	char		*src;
181 	int		 (*connect)(struct account *);
182 	void		 (*disconnect)(struct account *);
183 	int		 (*getln)(
184 			      struct account *, struct fetch_ctx *, char **);
185 	int		 (*putln)(struct account *, const char *, va_list);
186 };
187 
188 struct fetch_pop3_mail {
189 	char		*uid;
190 	u_int		 idx;
191 
192 	TAILQ_ENTRY(fetch_pop3_mail) qentry;
193 	RB_ENTRY(fetch_pop3_mail) tentry;
194 };
195 
196 /* Fetch imap data. */
197 struct fetch_imap_data {
198 	enum fetch_only	 only;
199 
200 	char		*user;
201 	char		*pass;
202 	struct server	 server;
203 	char		*pipecmd;
204 	int		 starttls;
205 	int		 nocrammd5;
206 	int		 nologin;
207 
208 	u_int		 folder;
209 	struct strings	*folders;
210 	u_int		 folders_total; /* total mail count */
211 
212 	int		 capa;
213 	int		 tag;
214 
215 	ARRAY_DECL(, u_int) wanted;
216 	ARRAY_DECL(, u_int) dropped;
217 	ARRAY_DECL(, u_int) kept;
218 
219 	u_int		 total;
220 	u_int		 committed;
221 
222 	int		 flushing;
223 	size_t		 size;
224 	u_int		 lines;
225 
226 	struct io	*io;
227 	struct cmd	*cmd;
228 
229 	char		*src;
230 	int		 (*connect)(struct account *);
231 	void		 (*disconnect)(struct account *);
232 	int		 (*getln)(
233 			      struct account *, struct fetch_ctx *, char **);
234 	int		 (*putln)(struct account *, const char *, va_list);
235 };
236 
237 struct fetch_imap_mail {
238 	u_int		 uid;
239 };
240 
241 #define IMAP_TAG_NONE -1
242 #define IMAP_TAG_CONTINUE -2
243 #define IMAP_TAG_ERROR -3
244 
245 #define IMAP_TAGGED 0
246 #define IMAP_CONTINUE 1
247 #define IMAP_UNTAGGED 2
248 #define IMAP_RAW 3
249 
250 #define IMAP_CAPA_AUTH_CRAM_MD5 0x1
251 #define IMAP_CAPA_XYZZY 0x2
252 #define IMAP_CAPA_STARTTLS 0x4
253 #define IMAP_CAPA_NOSPACE 0x8
254 #define IMAP_CAPA_GMEXT 0x10
255 
256 /* fetch-maildir.c */
257 extern struct fetch	 fetch_maildir;
258 
259 /* fetch-mbx.c */
260 extern struct fetch	 fetch_mbox;
261 
262 /* fetch-stdin.c */
263 extern struct fetch	 fetch_stdin;
264 
265 /* fetch-nntp.c */
266 extern struct fetch	 fetch_nntp;
267 
268 /* fetch-pop3.c */
269 extern struct fetch	 fetch_pop3;
270 
271 /* fetch-pop3pipe.c */
272 extern struct fetch	 fetch_pop3pipe;
273 
274 /* fetch-imap.c */
275 extern struct fetch	 fetch_imap;
276 int	fetch_imap_putln(struct account *, const char *, va_list);
277 int	fetch_imap_getln(struct account *, struct fetch_ctx *, char **);
278 int	fetch_imap_state_init(struct account *, struct fetch_ctx *);
279 
280 /* fetch-imappipe.c */
281 extern struct fetch	 fetch_imappipe;
282 
283 /* imap-common.c */
284 int	imap_tag(char *);
285 int	imap_putln(struct account *, const char *, ...);
286 int	imap_getln(struct account *, struct fetch_ctx *, int, char **);
287 int	imap_okay(char *);
288 int	imap_no(char *);
289 int	imap_bad(struct account *, const char *);
290 int	imap_invalid(struct account *, const char *);
291 int	imap_state_init(struct account *, struct fetch_ctx *);
292 int	imap_state_connected(struct account *, struct fetch_ctx *);
293 int	imap_state_select1(struct account *, struct fetch_ctx *);
294 int	imap_commit(struct account *, struct mail *);
295 void	imap_abort(struct account *);
296 u_int	imap_total(struct account *);
297 
298 /* pop3-common.c */
299 int	pop3_state_init(struct account *, struct fetch_ctx *);
300 int	pop3_commit(struct account *, struct mail *);
301 void	pop3_abort(struct account *);
302 u_int	pop3_total(struct account *);
303 
304 #endif
305