Diffstat (limited to 'kmicromail/libetpan/generic/pop3storage.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kmicromail/libetpan/generic/pop3storage.c | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/kmicromail/libetpan/generic/pop3storage.c b/kmicromail/libetpan/generic/pop3storage.c new file mode 100644 index 0000000..8e7a94e --- a/dev/null +++ b/kmicromail/libetpan/generic/pop3storage.c | |||
@@ -0,0 +1,284 @@ | |||
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 "pop3storage.h" | ||
37 | |||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | #include "mail.h" | ||
42 | #include "mailstorage_tools.h" | ||
43 | #include "maildriver.h" | ||
44 | |||
45 | /* pop3 storage */ | ||
46 | |||
47 | #define POP3_DEFAULT_PORT 110 | ||
48 | #define POP3S_DEFAULT_PORT 995 | ||
49 | |||
50 | static int pop3_mailstorage_connect(struct mailstorage * storage); | ||
51 | static int pop3_mailstorage_get_folder_session(struct mailstorage * storage, | ||
52 | char * pathname, mailsession ** result); | ||
53 | static void pop3_mailstorage_uninitialize(struct mailstorage * storage); | ||
54 | |||
55 | static mailstorage_driver pop3_mailstorage_driver = { | ||
56 | .sto_name = "pop3", | ||
57 | .sto_connect = pop3_mailstorage_connect, | ||
58 | .sto_get_folder_session = pop3_mailstorage_get_folder_session, | ||
59 | .sto_uninitialize = pop3_mailstorage_uninitialize, | ||
60 | }; | ||
61 | |||
62 | int pop3_mailstorage_init(struct mailstorage * storage, | ||
63 | char * pop3_servername, uint16_t pop3_port, | ||
64 | char * pop3_command, | ||
65 | int pop3_connection_type, int pop3_auth_type, | ||
66 | char * pop3_login, char * pop3_password, | ||
67 | int pop3_cached, char * pop3_cache_directory, char * pop3_flags_directory) | ||
68 | { | ||
69 | struct pop3_mailstorage * pop3_storage; | ||
70 | |||
71 | pop3_storage = malloc(sizeof(struct pop3_mailstorage)); | ||
72 | if (pop3_storage == NULL) | ||
73 | goto err; | ||
74 | |||
75 | pop3_storage->pop3_servername = strdup(pop3_servername); | ||
76 | if (pop3_storage->pop3_servername == NULL) | ||
77 | goto free; | ||
78 | |||
79 | pop3_storage->pop3_connection_type = pop3_connection_type; | ||
80 | |||
81 | if (pop3_port == 0) { | ||
82 | switch (pop3_connection_type) { | ||
83 | case CONNECTION_TYPE_PLAIN: | ||
84 | case CONNECTION_TYPE_TRY_STARTTLS: | ||
85 | case CONNECTION_TYPE_STARTTLS: | ||
86 | case CONNECTION_TYPE_COMMAND: | ||
87 | case CONNECTION_TYPE_COMMAND_TRY_STARTTLS: | ||
88 | case CONNECTION_TYPE_COMMAND_STARTTLS: | ||
89 | pop3_port = POP3_DEFAULT_PORT; | ||
90 | break; | ||
91 | |||
92 | case CONNECTION_TYPE_TLS: | ||
93 | case CONNECTION_TYPE_COMMAND_TLS: | ||
94 | pop3_port = POP3S_DEFAULT_PORT; | ||
95 | break; | ||
96 | } | ||
97 | } | ||
98 | |||
99 | pop3_storage->pop3_port = pop3_port; | ||
100 | |||
101 | if (pop3_command != NULL) { | ||
102 | pop3_storage->pop3_command = strdup(pop3_command); | ||
103 | if (pop3_storage->pop3_command == NULL) | ||
104 | goto free_servername; | ||
105 | } | ||
106 | else | ||
107 | pop3_storage->pop3_command = NULL; | ||
108 | |||
109 | pop3_storage->pop3_auth_type = pop3_auth_type; | ||
110 | |||
111 | if (pop3_login != NULL) { | ||
112 | pop3_storage->pop3_login = strdup(pop3_login); | ||
113 | if (pop3_storage->pop3_login == NULL) | ||
114 | goto free_command; | ||
115 | } | ||
116 | else | ||
117 | pop3_storage->pop3_login = NULL; | ||
118 | |||
119 | if (pop3_password != NULL) { | ||
120 | pop3_storage->pop3_password = strdup(pop3_password); | ||
121 | if (pop3_storage->pop3_password == NULL) | ||
122 | goto free_login; | ||
123 | } | ||
124 | else | ||
125 | pop3_storage->pop3_password = NULL; | ||
126 | |||
127 | pop3_storage->pop3_cached = pop3_cached; | ||
128 | |||
129 | if (pop3_cached && (pop3_cache_directory != NULL) && | ||
130 | (pop3_flags_directory != NULL)) { | ||
131 | pop3_storage->pop3_cache_directory = strdup(pop3_cache_directory); | ||
132 | if (pop3_storage->pop3_cache_directory == NULL) | ||
133 | goto free_password; | ||
134 | pop3_storage->pop3_flags_directory = strdup(pop3_flags_directory); | ||
135 | if (pop3_storage->pop3_flags_directory == NULL) | ||
136 | goto free_cache_directory; | ||
137 | } | ||
138 | else { | ||
139 | pop3_storage->pop3_cached = FALSE; | ||
140 | pop3_storage->pop3_cache_directory = NULL; | ||
141 | pop3_storage->pop3_flags_directory = NULL; | ||
142 | } | ||
143 | |||
144 | storage->sto_data = pop3_storage; | ||
145 | storage->sto_driver = &pop3_mailstorage_driver; | ||
146 | |||
147 | return MAIL_NO_ERROR; | ||
148 | |||
149 | free_cache_directory: | ||
150 | free(pop3_storage->pop3_cache_directory); | ||
151 | free_password: | ||
152 | if (pop3_storage->pop3_password != NULL) | ||
153 | free(pop3_storage->pop3_password); | ||
154 | free_login: | ||
155 | if (pop3_storage->pop3_login != NULL) | ||
156 | free(pop3_storage->pop3_login); | ||
157 | free_command: | ||
158 | if (pop3_storage->pop3_command != NULL) | ||
159 | free(pop3_storage->pop3_command); | ||
160 | free_servername: | ||
161 | if (pop3_storage->pop3_servername != NULL) | ||
162 | free(pop3_storage->pop3_servername); | ||
163 | free: | ||
164 | free(pop3_storage); | ||
165 | err: | ||
166 | return MAIL_ERROR_MEMORY; | ||
167 | } | ||
168 | |||
169 | static void pop3_mailstorage_uninitialize(struct mailstorage * storage) | ||
170 | { | ||
171 | struct pop3_mailstorage * pop3_storage; | ||
172 | |||
173 | pop3_storage = storage->sto_data; | ||
174 | |||
175 | if (pop3_storage->pop3_flags_directory != NULL) | ||
176 | free(pop3_storage->pop3_flags_directory); | ||
177 | if (pop3_storage->pop3_cache_directory != NULL) | ||
178 | free(pop3_storage->pop3_cache_directory); | ||
179 | if (pop3_storage->pop3_password != NULL) | ||
180 | free(pop3_storage->pop3_password); | ||
181 | if (pop3_storage->pop3_login != NULL) | ||
182 | free(pop3_storage->pop3_login); | ||
183 | if (pop3_storage->pop3_command != NULL) | ||
184 | free(pop3_storage->pop3_command); | ||
185 | free(pop3_storage->pop3_servername); | ||
186 | free(pop3_storage); | ||
187 | |||
188 | storage->sto_data = pop3_storage; | ||
189 | } | ||
190 | |||
191 | static int pop3_mailstorage_connect(struct mailstorage * storage) | ||
192 | { | ||
193 | struct pop3_mailstorage * pop3_storage; | ||
194 | mailsession_driver * driver; | ||
195 | int r; | ||
196 | int res; | ||
197 | mailsession * session; | ||
198 | int auth_type; | ||
199 | |||
200 | pop3_storage = storage->sto_data; | ||
201 | |||
202 | if (pop3_storage->pop3_cached) | ||
203 | driver = pop3_cached_session_driver; | ||
204 | else | ||
205 | driver = pop3_session_driver; | ||
206 | |||
207 | r = mailstorage_generic_connect(driver, | ||
208 | pop3_storage->pop3_servername, | ||
209 | pop3_storage->pop3_port, pop3_storage->pop3_command, | ||
210 | pop3_storage->pop3_connection_type, | ||
211 | POP3DRIVER_CACHED_SET_CACHE_DIRECTORY, | ||
212 | pop3_storage->pop3_cache_directory, | ||
213 | POP3DRIVER_CACHED_SET_FLAGS_DIRECTORY, | ||
214 | pop3_storage->pop3_flags_directory, | ||
215 | &session); | ||
216 | switch (r) { | ||
217 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
218 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
219 | case MAIL_NO_ERROR: | ||
220 | break; | ||
221 | default: | ||
222 | res = r; | ||
223 | goto err; | ||
224 | } | ||
225 | |||
226 | auth_type = -1; | ||
227 | switch (pop3_storage->pop3_auth_type) { | ||
228 | case POP3_AUTH_TYPE_PLAIN: | ||
229 | auth_type = POP3DRIVER_AUTH_TYPE_PLAIN; | ||
230 | break; | ||
231 | case POP3_AUTH_TYPE_APOP: | ||
232 | auth_type = POP3DRIVER_AUTH_TYPE_APOP; | ||
233 | break; | ||
234 | case POP3_AUTH_TYPE_TRY_APOP: | ||
235 | auth_type = POP3DRIVER_AUTH_TYPE_TRY_APOP; | ||
236 | break; | ||
237 | } | ||
238 | |||
239 | if (auth_type != -1) { | ||
240 | mailsession_parameters(session, POP3DRIVER_SET_AUTH_TYPE, &auth_type); | ||
241 | } | ||
242 | |||
243 | r = mailstorage_generic_auth(session, r, | ||
244 | pop3_storage->pop3_auth_type, | ||
245 | pop3_storage->pop3_login, | ||
246 | pop3_storage->pop3_password); | ||
247 | if (r != MAIL_NO_ERROR) { | ||
248 | if (pop3_storage->pop3_auth_type == POP3_AUTH_TYPE_TRY_APOP) { | ||
249 | /* try in clear authentication */ | ||
250 | mailsession_free(session); | ||
251 | |||
252 | pop3_storage->pop3_auth_type = POP3_AUTH_TYPE_PLAIN; | ||
253 | r = mailstorage_connect(storage); | ||
254 | if (r != MAIL_NO_ERROR) { | ||
255 | res = r; | ||
256 | return res; | ||
257 | } | ||
258 | pop3_storage->pop3_auth_type = POP3_AUTH_TYPE_TRY_APOP; | ||
259 | |||
260 | return MAIL_NO_ERROR; | ||
261 | } | ||
262 | |||
263 | res = r; | ||
264 | goto free; | ||
265 | } | ||
266 | |||
267 | storage->sto_session = session; | ||
268 | |||
269 | return MAIL_NO_ERROR; | ||
270 | |||
271 | free: | ||
272 | mailsession_free(session); | ||
273 | err: | ||
274 | return res; | ||
275 | } | ||
276 | |||
277 | static int pop3_mailstorage_get_folder_session(struct mailstorage * storage, | ||
278 | char * pathname, mailsession ** result) | ||
279 | { | ||
280 | * result = storage->sto_session; | ||
281 | |||
282 | return MAIL_NO_ERROR; | ||
283 | } | ||
284 | |||