summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/generic/pop3driver.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/generic/pop3driver.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/generic/pop3driver.c387
1 files changed, 387 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/pop3driver.c b/kmicromail/libetpan/generic/pop3driver.c
new file mode 100644
index 0000000..20b0fc2
--- a/dev/null
+++ b/kmicromail/libetpan/generic/pop3driver.c
@@ -0,0 +1,387 @@
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.h"
37
38#include <string.h>
39#include <stdlib.h>
40
41#include "pop3driver_message.h"
42#include "maildriver_tools.h"
43#include "pop3driver_tools.h"
44#include "mailmessage.h"
45
46static int pop3driver_initialize(mailsession * session);
47
48static void pop3driver_uninitialize(mailsession * session);
49
50static int pop3driver_parameters(mailsession * session,
51 int id, void * value);
52
53static int pop3driver_connect_stream(mailsession * session, mailstream * s);
54
55static int pop3driver_starttls(mailsession * session);
56
57static int pop3driver_login(mailsession * session,
58 char * userid, char * password);
59
60static int pop3driver_logout(mailsession * session);
61
62static int pop3driver_noop(mailsession * session);
63
64static int pop3driver_status_folder(mailsession * session, char * mb,
65 uint32_t * result_messages, uint32_t * result_recent,
66 uint32_t * result_unseen);
67
68static int pop3driver_messages_number(mailsession * session, char * mb,
69 uint32_t * result);
70
71static int pop3driver_remove_message(mailsession * session, uint32_t num);
72
73static int pop3driver_get_messages_list(mailsession * session,
74 struct mailmessage_list ** result);
75
76static int pop3driver_get_message(mailsession * session,
77 uint32_t num, mailmessage ** result);
78
79static mailsession_driver local_pop3_session_driver = {
80 .sess_name = "pop3",
81
82 .sess_initialize = pop3driver_initialize,
83 .sess_uninitialize = pop3driver_uninitialize,
84
85 .sess_parameters = pop3driver_parameters,
86
87 .sess_connect_stream = pop3driver_connect_stream,
88 .sess_connect_path = NULL,
89 .sess_starttls = pop3driver_starttls,
90 .sess_login = pop3driver_login,
91 .sess_logout = pop3driver_logout,
92 .sess_noop = pop3driver_noop,
93
94 .sess_build_folder_name = NULL,
95 .sess_create_folder = NULL,
96 .sess_delete_folder = NULL,
97 .sess_rename_folder = NULL,
98 .sess_check_folder = NULL,
99 .sess_examine_folder = NULL,
100 .sess_select_folder = NULL,
101 .sess_expunge_folder = NULL,
102 .sess_status_folder = pop3driver_status_folder,
103 .sess_messages_number = pop3driver_messages_number,
104 .sess_recent_number = pop3driver_messages_number,
105 .sess_unseen_number = pop3driver_messages_number,
106 .sess_list_folders = NULL,
107 .sess_lsub_folders = NULL,
108 .sess_subscribe_folder = NULL,
109 .sess_unsubscribe_folder = NULL,
110
111 .sess_append_message = NULL,
112 .sess_copy_message = NULL,
113 .sess_move_message = NULL,
114
115 .sess_get_messages_list = pop3driver_get_messages_list,
116 .sess_get_envelopes_list = maildriver_generic_get_envelopes_list,
117 .sess_remove_message = pop3driver_remove_message,
118#if 0
119 .sess_search_messages = maildriver_generic_search_messages,
120#endif
121
122 .sess_get_message = pop3driver_get_message,
123 .sess_get_message_by_uid = NULL,
124};
125
126mailsession_driver * pop3_session_driver = &local_pop3_session_driver;
127
128static inline struct pop3_session_state_data *
129get_data(mailsession * session)
130{
131 return session->sess_data;
132}
133
134static mailpop3 * get_pop3_session(mailsession * session)
135{
136 return get_data(session)->pop3_session;
137}
138
139static int pop3driver_initialize(mailsession * session)
140{
141 struct pop3_session_state_data * data;
142 mailpop3 * pop3;
143
144 pop3 = mailpop3_new(0, NULL);
145 if (session == NULL)
146 goto err;
147
148 data = malloc(sizeof(* data));
149 if (data == NULL)
150 goto free;
151
152 data->pop3_session = pop3;
153 data->pop3_auth_type = POP3DRIVER_AUTH_TYPE_PLAIN;
154
155 session->sess_data = data;
156
157 return MAIL_NO_ERROR;
158
159 free:
160 mailpop3_free(pop3);
161 err:
162 return MAIL_ERROR_MEMORY;
163}
164
165static void pop3driver_uninitialize(mailsession * session)
166{
167 struct pop3_session_state_data * data;
168
169 data = get_data(session);
170
171 mailpop3_free(data->pop3_session);
172 free(data);
173
174 session->sess_data = data;
175}
176
177static int pop3driver_connect_stream(mailsession * session, mailstream * s)
178{
179 int r;
180
181 r = mailpop3_connect(get_pop3_session(session), s);
182
183 switch (r) {
184 case MAILPOP3_NO_ERROR:
185 return MAIL_NO_ERROR_NON_AUTHENTICATED;
186
187 default:
188 return pop3driver_pop3_error_to_mail_error(r);
189 }
190}
191
192static int pop3driver_starttls(mailsession * session)
193{
194 int r;
195 int fd;
196 mailstream_low * low;
197 mailstream_low * new_low;
198 mailpop3 * pop3;
199
200 pop3 = get_pop3_session(session);
201
202 r = mailpop3_stls(pop3);
203
204 switch (r) {
205 case MAILPOP3_NO_ERROR:
206 break;
207 default:
208 return pop3driver_pop3_error_to_mail_error(r);
209 }
210
211 low = mailstream_get_low(pop3->pop3_stream);
212 fd = mailstream_low_get_fd(low);
213 if (fd == -1)
214 return MAIL_ERROR_STREAM;
215
216 new_low = mailstream_low_ssl_open(fd);
217 if (new_low == NULL)
218 return MAIL_ERROR_STREAM;
219 mailstream_low_free(low);
220 mailstream_set_low(pop3->pop3_stream, new_low);
221
222 return MAIL_NO_ERROR;
223}
224
225static int pop3driver_parameters(mailsession * session,
226 int id, void * value)
227{
228 struct pop3_session_state_data * data;
229
230 data = get_data(session);
231
232 switch (id) {
233 case POP3DRIVER_SET_AUTH_TYPE:
234 {
235 int * param;
236
237 param = value;
238
239 data->pop3_auth_type = * param;
240 return MAIL_NO_ERROR;
241 }
242 }
243
244 return MAIL_ERROR_INVAL;
245}
246
247static int pop3driver_login(mailsession * session,
248 char * userid, char * password)
249{
250 int r;
251 carray * msg_tab;
252 struct pop3_session_state_data * data;
253
254 data = get_data(session);
255
256 switch (data->pop3_auth_type) {
257 case POP3DRIVER_AUTH_TYPE_TRY_APOP:
258 r = mailpop3_login_apop(get_pop3_session(session), userid, password);
259 if (r != MAILPOP3_NO_ERROR)
260 r = mailpop3_login(get_pop3_session(session), userid, password);
261 break;
262
263 case POP3DRIVER_AUTH_TYPE_APOP:
264 r = mailpop3_login_apop(get_pop3_session(session), userid, password);
265 break;
266
267 default:
268 case POP3DRIVER_AUTH_TYPE_PLAIN:
269 r = mailpop3_login(get_pop3_session(session), userid, password);
270 break;
271 }
272
273 mailpop3_list(get_pop3_session(session), &msg_tab);
274
275 return pop3driver_pop3_error_to_mail_error(r);
276}
277
278static int pop3driver_logout(mailsession * session)
279{
280 int r;
281
282 r = mailpop3_quit(get_pop3_session(session));
283
284 return pop3driver_pop3_error_to_mail_error(r);
285}
286
287static int pop3driver_noop(mailsession * session)
288{
289 int r;
290
291 r = mailpop3_noop(get_pop3_session(session));
292
293 return pop3driver_pop3_error_to_mail_error(r);
294}
295
296static int pop3driver_status_folder(mailsession * session, char * mb,
297 uint32_t * result_messages,
298 uint32_t * result_recent,
299 uint32_t * result_unseen)
300{
301 uint32_t count;
302 int r;
303
304 r = pop3driver_messages_number(session, mb, &count);
305 if (r != MAIL_NO_ERROR)
306 return r;
307
308 * result_messages = count;
309 * result_recent = count;
310 * result_unseen = count;
311
312 return MAIL_NO_ERROR;
313}
314
315static int pop3driver_messages_number(mailsession * session, char * mb,
316 uint32_t * result)
317{
318 carray * msg_tab;
319
320 mailpop3_list(get_pop3_session(session), &msg_tab);
321
322 * result = carray_count(msg_tab) -
323 get_pop3_session(session)->pop3_deleted_count;
324
325 return MAIL_NO_ERROR;
326}
327
328
329/* messages operations */
330
331static int pop3driver_remove_message(mailsession * session, uint32_t num)
332{
333 mailpop3 * pop3;
334 int r;
335
336 pop3 = get_pop3_session(session);
337
338 r = mailpop3_dele(pop3, num);
339 switch (r) {
340 case MAILPOP3_ERROR_BAD_STATE:
341 return MAIL_ERROR_BAD_STATE;
342
343 case MAILPOP3_ERROR_NO_SUCH_MESSAGE:
344 return MAIL_ERROR_MSG_NOT_FOUND;
345
346 case MAILPOP3_ERROR_STREAM:
347 return MAIL_ERROR_STREAM;
348
349 case MAILPOP3_NO_ERROR:
350 return MAIL_NO_ERROR;
351
352 default:
353 return MAIL_ERROR_REMOVE;
354 }
355}
356
357static int pop3driver_get_messages_list(mailsession * session,
358 struct mailmessage_list ** result)
359{
360 mailpop3 * pop3;
361
362 pop3 = get_pop3_session(session);
363
364 return pop3_get_messages_list(pop3, session,
365 pop3_message_driver, result);
366}
367
368static int pop3driver_get_message(mailsession * session,
369 uint32_t num, mailmessage ** result)
370{
371 mailmessage * msg_info;
372 int r;
373
374 msg_info = mailmessage_new();
375 if (msg_info == NULL)
376 return MAIL_ERROR_MEMORY;
377
378 r = mailmessage_init(msg_info, session, pop3_message_driver, num, 0);
379 if (r != MAIL_NO_ERROR) {
380 mailmessage_free(msg_info);
381 return r;
382 }
383
384 * result = msg_info;
385
386 return MAIL_NO_ERROR;
387}