author | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
commit | e3b89230f065c48c84b48c88edb6eb088374c487 (patch) (unidiff) | |
tree | 162ea2ef909a6f82ccfcedf45d80d6c821174912 /kmicromail/libetpan/generic/pop3driver_tools.c | |
parent | 2dd6ac0b2d24c91d35ce674a6c26351352df2b15 (diff) | |
download | kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.zip kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.gz kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.bz2 |
Initial revision
Diffstat (limited to 'kmicromail/libetpan/generic/pop3driver_tools.c') (more/less context) (show whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/pop3driver_tools.c | 344 |
1 files changed, 344 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/pop3driver_tools.c b/kmicromail/libetpan/generic/pop3driver_tools.c new file mode 100644 index 0000000..8978b47 --- a/dev/null +++ b/kmicromail/libetpan/generic/pop3driver_tools.c | |||
@@ -0,0 +1,344 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "pop3driver_tools.h" | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | #include <fcntl.h> | ||
41 | #include <unistd.h> | ||
42 | |||
43 | #include "maildriver_types.h" | ||
44 | #include "mailpop3.h" | ||
45 | #include "pop3driver.h" | ||
46 | #include "pop3driver_cached.h" | ||
47 | #include "generic_cache.h" | ||
48 | #include "imfcache.h" | ||
49 | #include "mailmessage.h" | ||
50 | #include "mail_cache_db.h" | ||
51 | |||
52 | int pop3driver_pop3_error_to_mail_error(int error) | ||
53 | { | ||
54 | switch (error) { | ||
55 | case MAILPOP3_NO_ERROR: | ||
56 | return MAIL_NO_ERROR; | ||
57 | |||
58 | case MAILPOP3_ERROR_BAD_STATE: | ||
59 | return MAIL_ERROR_BAD_STATE; | ||
60 | |||
61 | case MAILPOP3_ERROR_UNAUTHORIZED: | ||
62 | return MAIL_ERROR_CONNECT; | ||
63 | |||
64 | case MAILPOP3_ERROR_STREAM: | ||
65 | return MAIL_ERROR_STREAM; | ||
66 | |||
67 | case MAILPOP3_ERROR_DENIED: | ||
68 | return MAIL_ERROR_CONNECT; | ||
69 | |||
70 | case MAILPOP3_ERROR_BAD_USER: | ||
71 | case MAILPOP3_ERROR_BAD_PASSWORD: | ||
72 | return MAIL_ERROR_LOGIN; | ||
73 | |||
74 | case MAILPOP3_ERROR_CANT_LIST: | ||
75 | return MAIL_ERROR_LIST; | ||
76 | |||
77 | case MAILPOP3_ERROR_NO_SUCH_MESSAGE: | ||
78 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
79 | |||
80 | case MAILPOP3_ERROR_MEMORY: | ||
81 | return MAIL_ERROR_MEMORY; | ||
82 | |||
83 | case MAILPOP3_ERROR_CONNECTION_REFUSED: | ||
84 | return MAIL_ERROR_CONNECT; | ||
85 | |||
86 | case MAILPOP3_ERROR_APOP_NOT_SUPPORTED: | ||
87 | return MAIL_ERROR_NO_APOP; | ||
88 | |||
89 | case MAILPOP3_ERROR_CAPA_NOT_SUPPORTED: | ||
90 | return MAIL_ERROR_CAPABILITY; | ||
91 | |||
92 | case MAILPOP3_ERROR_STLS_NOT_SUPPORTED: | ||
93 | return MAIL_ERROR_NO_TLS; | ||
94 | |||
95 | default: | ||
96 | return MAIL_ERROR_INVAL; | ||
97 | } | ||
98 | }; | ||
99 | |||
100 | static inline struct pop3_session_state_data * | ||
101 | session_get_data(mailsession * session) | ||
102 | { | ||
103 | return session->sess_data; | ||
104 | } | ||
105 | |||
106 | static inline mailpop3 * session_get_pop3_session(mailsession * session) | ||
107 | { | ||
108 | return session_get_data(session)->pop3_session; | ||
109 | } | ||
110 | |||
111 | static inline struct pop3_cached_session_state_data * | ||
112 | cached_session_get_data(mailsession * session) | ||
113 | { | ||
114 | return session->sess_data; | ||
115 | } | ||
116 | |||
117 | static inline mailsession * | ||
118 | cached_session_get_ancestor(mailsession * session) | ||
119 | { | ||
120 | return cached_session_get_data(session)->pop3_ancestor; | ||
121 | } | ||
122 | |||
123 | static inline struct pop3_session_state_data * | ||
124 | cached_session_get_ancestor_data(mailsession * session) | ||
125 | { | ||
126 | return session_get_data(cached_session_get_ancestor(session)); | ||
127 | } | ||
128 | |||
129 | static inline mailpop3 * | ||
130 | cached_session_get_pop3_session(mailsession * session) | ||
131 | { | ||
132 | return session_get_pop3_session(cached_session_get_ancestor(session)); | ||
133 | } | ||
134 | |||
135 | |||
136 | int pop3driver_retr(mailsession * session, uint32_t index, | ||
137 | char ** result, size_t * result_len) | ||
138 | { | ||
139 | char * msg_content; | ||
140 | size_t msg_length; | ||
141 | int r; | ||
142 | |||
143 | r = mailpop3_retr(session_get_pop3_session(session), index, | ||
144 | &msg_content, &msg_length); | ||
145 | |||
146 | switch (r) { | ||
147 | case MAILPOP3_NO_ERROR: | ||
148 | break; | ||
149 | default: | ||
150 | return pop3driver_pop3_error_to_mail_error(r); | ||
151 | } | ||
152 | |||
153 | * result = msg_content; | ||
154 | * result_len = msg_length; | ||
155 | |||
156 | return MAIL_NO_ERROR; | ||
157 | } | ||
158 | |||
159 | int pop3driver_header(mailsession * session, uint32_t index, | ||
160 | char ** result, | ||
161 | size_t * result_len) | ||
162 | { | ||
163 | char * headers; | ||
164 | size_t headers_length; | ||
165 | int r; | ||
166 | |||
167 | r = mailpop3_header(session_get_pop3_session(session), | ||
168 | index, &headers, &headers_length); | ||
169 | |||
170 | switch (r) { | ||
171 | case MAILPOP3_NO_ERROR: | ||
172 | break; | ||
173 | default: | ||
174 | return pop3driver_pop3_error_to_mail_error(r); | ||
175 | } | ||
176 | |||
177 | * result = headers; | ||
178 | * result_len = headers_length; | ||
179 | |||
180 | return MAIL_NO_ERROR; | ||
181 | } | ||
182 | |||
183 | int pop3driver_size(mailsession * session, uint32_t index, | ||
184 | size_t * result) | ||
185 | { | ||
186 | mailpop3 * pop3; | ||
187 | carray * msg_tab; | ||
188 | struct mailpop3_msg_info * info; | ||
189 | int r; | ||
190 | |||
191 | pop3 = session_get_pop3_session(session); | ||
192 | |||
193 | mailpop3_list(pop3, &msg_tab); | ||
194 | |||
195 | r = mailpop3_get_msg_info(pop3, index, &info); | ||
196 | switch (r) { | ||
197 | case MAILPOP3_NO_ERROR: | ||
198 | break; | ||
199 | default: | ||
200 | return pop3driver_pop3_error_to_mail_error(r); | ||
201 | } | ||
202 | |||
203 | * result = info->msg_size; | ||
204 | |||
205 | return MAIL_NO_ERROR; | ||
206 | } | ||
207 | |||
208 | int | ||
209 | pop3driver_get_cached_flags(struct mail_cache_db * cache_db, | ||
210 | MMAPString * mmapstr, | ||
211 | mailsession * session, | ||
212 | uint32_t num, | ||
213 | struct mail_flags ** result) | ||
214 | { | ||
215 | int r; | ||
216 | char keyname[PATH_MAX]; | ||
217 | struct mail_flags * flags; | ||
218 | int res; | ||
219 | struct mailpop3_msg_info * info; | ||
220 | |||
221 | r = mailpop3_get_msg_info(cached_session_get_pop3_session(session), | ||
222 | num, &info); | ||
223 | switch (r) { | ||
224 | case MAILPOP3_ERROR_BAD_STATE: | ||
225 | return MAIL_ERROR_BAD_STATE; | ||
226 | case MAILPOP3_ERROR_NO_SUCH_MESSAGE: | ||
227 | return MAIL_ERROR_MSG_NOT_FOUND; | ||
228 | case MAILPOP3_NO_ERROR: | ||
229 | break; | ||
230 | default: | ||
231 | return MAIL_ERROR_FETCH; | ||
232 | } | ||
233 | |||
234 | snprintf(keyname, PATH_MAX, "%s-flags", info->msg_uidl); | ||
235 | |||
236 | r = generic_cache_flags_read(cache_db, mmapstr, keyname, &flags); | ||
237 | if (r != MAIL_NO_ERROR) { | ||
238 | res = r; | ||
239 | goto err; | ||
240 | } | ||
241 | |||
242 | * result = flags; | ||
243 | |||
244 | return MAIL_NO_ERROR; | ||
245 | |||
246 | err: | ||
247 | return res; | ||
248 | } | ||
249 | |||
250 | int | ||
251 | pop3driver_write_cached_flags(struct mail_cache_db * cache_db, | ||
252 | MMAPString * mmapstr, | ||
253 | char * uid, | ||
254 | struct mail_flags * flags) | ||
255 | { | ||
256 | int r; | ||
257 | char keyname[PATH_MAX]; | ||
258 | int res; | ||
259 | |||
260 | snprintf(keyname, PATH_MAX, "%s-flags", uid); | ||
261 | |||
262 | r = generic_cache_flags_write(cache_db, mmapstr, keyname, flags); | ||
263 | if (r != MAIL_NO_ERROR) { | ||
264 | res = r; | ||
265 | goto err; | ||
266 | } | ||
267 | |||
268 | return MAIL_NO_ERROR; | ||
269 | |||
270 | err: | ||
271 | return res; | ||
272 | } | ||
273 | |||
274 | int pop3_get_messages_list(mailpop3 * pop3, | ||
275 | mailsession * session, | ||
276 | mailmessage_driver * driver, | ||
277 | struct mailmessage_list ** result) | ||
278 | { | ||
279 | carray * msg_tab; | ||
280 | carray * tab; | ||
281 | struct mailmessage_list * env_list; | ||
282 | unsigned int i; | ||
283 | int res; | ||
284 | int r; | ||
285 | |||
286 | mailpop3_list(pop3, &msg_tab); | ||
287 | |||
288 | tab = carray_new(128); | ||
289 | if (tab == NULL) { | ||
290 | res = MAIL_ERROR_MEMORY; | ||
291 | goto err; | ||
292 | } | ||
293 | |||
294 | for(i = 0 ; i < carray_count(msg_tab) ; i++) { | ||
295 | struct mailpop3_msg_info * pop3_info; | ||
296 | mailmessage * msg; | ||
297 | |||
298 | pop3_info = carray_get(msg_tab, i); | ||
299 | |||
300 | if (pop3_info == NULL) | ||
301 | continue; | ||
302 | |||
303 | if (pop3_info->msg_deleted) | ||
304 | continue; | ||
305 | |||
306 | msg = mailmessage_new(); | ||
307 | if (msg == NULL) { | ||
308 | res = MAIL_ERROR_MEMORY; | ||
309 | goto free_list; | ||
310 | } | ||
311 | |||
312 | r = mailmessage_init(msg, session, driver, | ||
313 | (uint32_t) pop3_info->msg_index, pop3_info->msg_size); | ||
314 | if (r != MAIL_NO_ERROR) { | ||
315 | mailmessage_free(msg); | ||
316 | res = r; | ||
317 | goto free_list; | ||
318 | } | ||
319 | |||
320 | r = carray_add(tab, msg, NULL); | ||
321 | if (r < 0) { | ||
322 | mailmessage_free(msg); | ||
323 | res = MAIL_ERROR_MEMORY; | ||
324 | goto free_list; | ||
325 | } | ||
326 | } | ||
327 | |||
328 | env_list = mailmessage_list_new(/*list*/ tab); | ||
329 | if (env_list == NULL) { | ||
330 | res = MAIL_ERROR_MEMORY; | ||
331 | goto free_list; | ||
332 | } | ||
333 | |||
334 | * result = env_list; | ||
335 | |||
336 | return MAIL_NO_ERROR; | ||
337 | |||
338 | free_list: | ||
339 | for(i = 0 ; i < carray_count(tab) ; i ++) | ||
340 | mailmessage_free(carray_get(tab, i)); | ||
341 | carray_free(tab); | ||
342 | err: | ||
343 | return res; | ||
344 | } | ||