Diffstat (limited to 'libetpan/src/driver/interface') (more/less context) (ignore whitespace changes)
22 files changed, 6045 insertions, 0 deletions
diff --git a/libetpan/src/driver/interface/maildriver.c b/libetpan/src/driver/interface/maildriver.c new file mode 100644 index 0000000..5b6c4f2 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver.c | |||
@@ -0,0 +1,383 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "maildriver.h" | ||
37 | #include <ctype.h> | ||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | /* *********************************************************************** */ | ||
42 | /* mail session */ | ||
43 | |||
44 | mailsession * mailsession_new(mailsession_driver * sess_driver) | ||
45 | { | ||
46 | mailsession * session; | ||
47 | int r; | ||
48 | |||
49 | session = malloc(sizeof(* session)); | ||
50 | |||
51 | session->sess_data = NULL; | ||
52 | |||
53 | if (sess_driver->sess_initialize != NULL) { | ||
54 | r = sess_driver->sess_initialize(session); | ||
55 | if (r != MAIL_NO_ERROR) | ||
56 | goto free; | ||
57 | } | ||
58 | |||
59 | session->sess_driver = sess_driver; | ||
60 | |||
61 | return session; | ||
62 | |||
63 | free: | ||
64 | free(session); | ||
65 | return NULL; | ||
66 | } | ||
67 | |||
68 | void mailsession_free(mailsession * session) | ||
69 | { | ||
70 | if (session->sess_driver->sess_uninitialize != NULL) | ||
71 | session->sess_driver->sess_uninitialize(session); | ||
72 | free(session); | ||
73 | } | ||
74 | |||
75 | int mailsession_parameters(mailsession * session, | ||
76 | int id, void * value) | ||
77 | { | ||
78 | if (session->sess_driver->sess_parameters == NULL) | ||
79 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
80 | |||
81 | return session->sess_driver->sess_parameters(session, id, value); | ||
82 | } | ||
83 | |||
84 | int mailsession_connect_stream(mailsession * session, mailstream * s) | ||
85 | { | ||
86 | if (session->sess_driver->sess_connect_stream == NULL) | ||
87 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
88 | |||
89 | return session->sess_driver->sess_connect_stream(session, s); | ||
90 | } | ||
91 | |||
92 | int mailsession_connect_path(mailsession * session, char * path) | ||
93 | { | ||
94 | if (session->sess_driver->sess_connect_path == NULL) | ||
95 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
96 | |||
97 | return session->sess_driver->sess_connect_path(session, path); | ||
98 | } | ||
99 | |||
100 | int mailsession_starttls(mailsession * session) | ||
101 | { | ||
102 | if (session->sess_driver->sess_starttls == NULL) | ||
103 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
104 | |||
105 | return session->sess_driver->sess_starttls(session); | ||
106 | } | ||
107 | |||
108 | int mailsession_login(mailsession * session, | ||
109 | char * userid, char * password) | ||
110 | { | ||
111 | if (session->sess_driver->sess_login == NULL) | ||
112 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
113 | |||
114 | return session->sess_driver->sess_login(session, userid, password); | ||
115 | } | ||
116 | |||
117 | int mailsession_logout(mailsession * session) | ||
118 | { | ||
119 | if (session->sess_driver->sess_logout == NULL) | ||
120 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
121 | |||
122 | return session->sess_driver->sess_logout(session); | ||
123 | } | ||
124 | |||
125 | int mailsession_noop(mailsession * session) | ||
126 | { | ||
127 | if (session->sess_driver->sess_noop == NULL) | ||
128 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
129 | |||
130 | return session->sess_driver->sess_noop(session); | ||
131 | } | ||
132 | |||
133 | /* folders operations */ | ||
134 | |||
135 | int mailsession_build_folder_name(mailsession * session, char * mb, | ||
136 | char * name, char ** result) | ||
137 | { | ||
138 | if (session->sess_driver->sess_build_folder_name == NULL) | ||
139 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
140 | |||
141 | return session->sess_driver->sess_build_folder_name(session, | ||
142 | mb, name, result); | ||
143 | } | ||
144 | |||
145 | int mailsession_create_folder(mailsession * session, char * mb) | ||
146 | { | ||
147 | if (session->sess_driver->sess_create_folder == NULL) | ||
148 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
149 | |||
150 | return session->sess_driver->sess_create_folder(session, mb); | ||
151 | } | ||
152 | |||
153 | int mailsession_delete_folder(mailsession * session, char * mb) | ||
154 | { | ||
155 | if (session->sess_driver->sess_delete_folder == NULL) | ||
156 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
157 | |||
158 | return session->sess_driver->sess_delete_folder(session, mb); | ||
159 | } | ||
160 | |||
161 | int mailsession_rename_folder(mailsession * session, | ||
162 | char * mb, char * new_name) | ||
163 | { | ||
164 | if (session->sess_driver->sess_rename_folder == NULL) | ||
165 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
166 | |||
167 | return session->sess_driver->sess_rename_folder(session, mb, new_name); | ||
168 | } | ||
169 | |||
170 | int mailsession_check_folder(mailsession * session) | ||
171 | { | ||
172 | if (session->sess_driver->sess_check_folder == NULL) | ||
173 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
174 | |||
175 | return session->sess_driver->sess_check_folder(session); | ||
176 | } | ||
177 | |||
178 | int mailsession_examine_folder(mailsession * session, char * mb) | ||
179 | { | ||
180 | if (session->sess_driver->sess_examine_folder == NULL) | ||
181 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
182 | |||
183 | return session->sess_driver->sess_examine_folder(session, mb); | ||
184 | } | ||
185 | |||
186 | int mailsession_select_folder(mailsession * session, char * mb) | ||
187 | { | ||
188 | if (session->sess_driver->sess_select_folder == NULL) | ||
189 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
190 | |||
191 | return session->sess_driver->sess_select_folder(session, mb); | ||
192 | } | ||
193 | |||
194 | int mailsession_expunge_folder(mailsession * session) | ||
195 | { | ||
196 | if (session->sess_driver->sess_expunge_folder == NULL) | ||
197 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
198 | |||
199 | return session->sess_driver->sess_expunge_folder(session); | ||
200 | } | ||
201 | |||
202 | int mailsession_status_folder(mailsession * session, char * mb, | ||
203 | uint32_t * result_messages, uint32_t * result_recent, | ||
204 | uint32_t * result_unseen) | ||
205 | { | ||
206 | if (session->sess_driver->sess_status_folder == NULL) | ||
207 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
208 | |||
209 | return session->sess_driver->sess_status_folder(session, mb, | ||
210 | result_messages, result_recent, result_unseen); | ||
211 | } | ||
212 | |||
213 | int mailsession_messages_number(mailsession * session, char * mb, | ||
214 | uint32_t * result) | ||
215 | { | ||
216 | if (session->sess_driver->sess_messages_number == NULL) | ||
217 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
218 | |||
219 | return session->sess_driver->sess_messages_number(session, mb, result); | ||
220 | } | ||
221 | |||
222 | int mailsession_recent_number(mailsession * session, char * mb, | ||
223 | uint32_t * result) | ||
224 | { | ||
225 | if (session->sess_driver->sess_recent_number == NULL) | ||
226 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
227 | |||
228 | return session->sess_driver->sess_recent_number(session, mb, result); | ||
229 | } | ||
230 | |||
231 | int mailsession_unseen_number(mailsession * session, char * mb, | ||
232 | uint32_t * result) | ||
233 | { | ||
234 | if (session->sess_driver->sess_unseen_number == NULL) | ||
235 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
236 | |||
237 | return session->sess_driver->sess_recent_number(session, mb, result); | ||
238 | } | ||
239 | |||
240 | int mailsession_list_folders(mailsession * session, char * mb, | ||
241 | struct mail_list ** result) | ||
242 | { | ||
243 | if (session->sess_driver->sess_list_folders == NULL) | ||
244 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
245 | |||
246 | return session->sess_driver->sess_list_folders(session, mb, result); | ||
247 | } | ||
248 | |||
249 | int mailsession_lsub_folders(mailsession * session, char * mb, | ||
250 | struct mail_list ** result) | ||
251 | { | ||
252 | if (session->sess_driver->sess_lsub_folders == NULL) | ||
253 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
254 | |||
255 | return session->sess_driver->sess_lsub_folders(session, mb, result); | ||
256 | } | ||
257 | |||
258 | int mailsession_subscribe_folder(mailsession * session, char * mb) | ||
259 | { | ||
260 | if (session->sess_driver->sess_subscribe_folder == NULL) | ||
261 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
262 | |||
263 | return session->sess_driver->sess_subscribe_folder(session, mb); | ||
264 | } | ||
265 | |||
266 | int mailsession_unsubscribe_folder(mailsession * session, char * mb) | ||
267 | { | ||
268 | if (session->sess_driver->sess_unsubscribe_folder == NULL) | ||
269 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
270 | |||
271 | return session->sess_driver->sess_unsubscribe_folder(session, mb); | ||
272 | } | ||
273 | |||
274 | /* message */ | ||
275 | |||
276 | int mailsession_append_message(mailsession * session, | ||
277 | char * message, size_t size) | ||
278 | { | ||
279 | if (session->sess_driver->sess_append_message == NULL) | ||
280 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
281 | |||
282 | return session->sess_driver->sess_append_message(session, message, size); | ||
283 | } | ||
284 | |||
285 | int mailsession_append_message_flags(mailsession * session, | ||
286 | char * message, size_t size, struct mail_flags * flags) | ||
287 | { | ||
288 | if (session->sess_driver->sess_append_message_flags == NULL) | ||
289 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
290 | |||
291 | return session->sess_driver->sess_append_message_flags(session, | ||
292 | message, size, flags); | ||
293 | } | ||
294 | |||
295 | int mailsession_copy_message(mailsession * session, | ||
296 | uint32_t num, char * mb) | ||
297 | { | ||
298 | if (session->sess_driver->sess_copy_message == NULL) | ||
299 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
300 | |||
301 | return session->sess_driver->sess_copy_message(session, num, mb); | ||
302 | } | ||
303 | |||
304 | int mailsession_move_message(mailsession * session, | ||
305 | uint32_t num, char * mb) | ||
306 | { | ||
307 | if (session->sess_driver->sess_move_message == NULL) { | ||
308 | int r; | ||
309 | |||
310 | if ((session->sess_driver->sess_copy_message == NULL) && | ||
311 | (session->sess_driver->sess_remove_message == NULL)) | ||
312 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
313 | |||
314 | r = mailsession_copy_message(session, num, mb); | ||
315 | if (r != MAIL_NO_ERROR) | ||
316 | return r; | ||
317 | |||
318 | r = mailsession_remove_message(session, num); | ||
319 | if (r != MAIL_NO_ERROR) | ||
320 | return r; | ||
321 | |||
322 | return MAIL_NO_ERROR; | ||
323 | } | ||
324 | |||
325 | return session->sess_driver->sess_move_message(session, num, mb); | ||
326 | } | ||
327 | |||
328 | int mailsession_get_envelopes_list(mailsession * session, | ||
329 | struct mailmessage_list * env_list) | ||
330 | { | ||
331 | if (session->sess_driver->sess_get_envelopes_list == NULL) | ||
332 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
333 | |||
334 | return session->sess_driver->sess_get_envelopes_list(session, env_list); | ||
335 | } | ||
336 | |||
337 | int mailsession_get_messages_list(mailsession * session, | ||
338 | struct mailmessage_list ** result) | ||
339 | { | ||
340 | if (session->sess_driver->sess_get_messages_list == NULL) | ||
341 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
342 | |||
343 | return session->sess_driver->sess_get_messages_list(session, result); | ||
344 | } | ||
345 | |||
346 | int mailsession_remove_message(mailsession * session, uint32_t num) | ||
347 | { | ||
348 | if (session->sess_driver->sess_remove_message == NULL) | ||
349 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
350 | |||
351 | return session->sess_driver->sess_remove_message(session, num); | ||
352 | } | ||
353 | |||
354 | #if 0 | ||
355 | int mailsession_search_messages(mailsession * session, char * charset, | ||
356 | struct mail_search_key * key, | ||
357 | struct mail_search_result ** result) | ||
358 | { | ||
359 | if (session->sess_driver->sess_search_messages == NULL) | ||
360 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
361 | |||
362 | return session->sess_driver->sess_search_messages(session, | ||
363 | charset, key, result); | ||
364 | } | ||
365 | #endif | ||
366 | |||
367 | int mailsession_get_message(mailsession * session, | ||
368 | uint32_t num, mailmessage ** result) | ||
369 | { | ||
370 | if (session->sess_driver->sess_get_message == NULL) | ||
371 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
372 | |||
373 | return session->sess_driver->sess_get_message(session, num, result); | ||
374 | } | ||
375 | |||
376 | int mailsession_get_message_by_uid(mailsession * session, | ||
377 | const char * uid, mailmessage ** result) | ||
378 | { | ||
379 | if (session->sess_driver->sess_get_message_by_uid == NULL) | ||
380 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
381 | |||
382 | return session->sess_driver->sess_get_message_by_uid(session, uid, result); | ||
383 | } | ||
diff --git a/libetpan/src/driver/interface/maildriver.h b/libetpan/src/driver/interface/maildriver.h new file mode 100644 index 0000000..16e830b --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver.h | |||
@@ -0,0 +1,546 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILDRIVER_H | ||
37 | |||
38 | #define MAILDRIVER_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | #include <libetpan/maildriver_types_helper.h> | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | /* mailsession */ | ||
48 | |||
49 | /* | ||
50 | mailsession_new creates a new session, using the given driver | ||
51 | |||
52 | @return the created session is returned | ||
53 | */ | ||
54 | |||
55 | mailsession * mailsession_new(mailsession_driver * sess_driver); | ||
56 | |||
57 | /* | ||
58 | mailsession_free release the memory used by the session | ||
59 | */ | ||
60 | |||
61 | void mailsession_free(mailsession * session); | ||
62 | |||
63 | /* | ||
64 | mailsession_parameters is used to make calls specific to the driver | ||
65 | |||
66 | @param id is the command to send to the driver, | ||
67 | usually, commands can be found in the header of the driver | ||
68 | |||
69 | @param value is the parameter of the specific call | ||
70 | |||
71 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
72 | on error | ||
73 | */ | ||
74 | |||
75 | int mailsession_parameters(mailsession * session, | ||
76 | int id, void * value); | ||
77 | |||
78 | /* | ||
79 | There are drivers of two kinds : stream drivers (driver that connects | ||
80 | to servers through TCP or other means of connection) and file drivers | ||
81 | (driver that are based on filesystem) | ||
82 | |||
83 | The following function can only be used by stream drivers. | ||
84 | mailsession_connect_stream connects a stream to the session | ||
85 | |||
86 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
87 | on error | ||
88 | */ | ||
89 | |||
90 | int mailsession_connect_stream(mailsession * session, mailstream * s); | ||
91 | |||
92 | /* | ||
93 | The following function can only be used by file drivers. | ||
94 | mailsession_connect_path selects the main path of the session | ||
95 | |||
96 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
97 | on error | ||
98 | */ | ||
99 | |||
100 | int mailsession_connect_path(mailsession * session, char * path); | ||
101 | |||
102 | /* | ||
103 | NOTE: works only on stream drivers | ||
104 | |||
105 | mailsession_starttls switches the current connection to TLS (secure layer) | ||
106 | |||
107 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
108 | on error | ||
109 | */ | ||
110 | |||
111 | int mailsession_starttls(mailsession * session); | ||
112 | |||
113 | /* | ||
114 | mailsession_login notifies the login and the password to authenticate | ||
115 | to the session | ||
116 | |||
117 | @param userid the given string is only needed at this function call | ||
118 | (it will be duplicated if necessary) | ||
119 | @param password the given string is only needed at this function call | ||
120 | (it will be duplicated if necessary) | ||
121 | |||
122 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
123 | on error | ||
124 | */ | ||
125 | |||
126 | int mailsession_login(mailsession * session, | ||
127 | char * userid, char * password); | ||
128 | |||
129 | /* | ||
130 | NOTE: this function doesn't often work on filsystem drivers | ||
131 | |||
132 | mailsession_logout deconnects the session and closes the stream. | ||
133 | |||
134 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
135 | on error | ||
136 | */ | ||
137 | |||
138 | int mailsession_logout(mailsession * session); | ||
139 | |||
140 | /* | ||
141 | mailsession_noop does no operation on the session, but it can be | ||
142 | used to poll for the status of the connection. | ||
143 | |||
144 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
145 | on error | ||
146 | */ | ||
147 | |||
148 | int mailsession_noop(mailsession * session); | ||
149 | |||
150 | /* | ||
151 | NOTE: driver's specific should be used | ||
152 | |||
153 | mailsession_build_folder_name will return an allocated string with | ||
154 | that contains the complete path of the folder to create | ||
155 | |||
156 | @param session the sesion | ||
157 | @param mb is the parent mailbox | ||
158 | @param name is the name of the folder to create | ||
159 | @param result the complete path of the folder to create will be | ||
160 | stored in (* result), this name have to be freed with free() | ||
161 | |||
162 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
163 | on error | ||
164 | */ | ||
165 | |||
166 | int mailsession_build_folder_name(mailsession * session, char * mb, | ||
167 | char * name, char ** result); | ||
168 | |||
169 | /* | ||
170 | NOTE: driver's specific should be used | ||
171 | |||
172 | mailsession_create_folder creates the folder that corresponds to the | ||
173 | given name | ||
174 | |||
175 | @param session the session | ||
176 | @param mb is the name of the mailbox | ||
177 | |||
178 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
179 | on error | ||
180 | */ | ||
181 | |||
182 | int mailsession_create_folder(mailsession * session, char * mb); | ||
183 | |||
184 | |||
185 | /* | ||
186 | NOTE: driver's specific should be used | ||
187 | |||
188 | mailsession_delete_folder deletes the folder that corresponds to the | ||
189 | given name | ||
190 | |||
191 | @param session the session | ||
192 | @param mb is the name of the mailbox | ||
193 | |||
194 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
195 | on error | ||
196 | */ | ||
197 | |||
198 | int mailsession_delete_folder(mailsession * session, char * mb); | ||
199 | |||
200 | |||
201 | /* | ||
202 | mailsession_rename_folder changes the name of the folder | ||
203 | |||
204 | @param session the session | ||
205 | @param mb is the name of the mailbox whose name has to be changed | ||
206 | @param new_name is the destination name (the parent | ||
207 | of the new folder folder can be other) | ||
208 | |||
209 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
210 | on error | ||
211 | */ | ||
212 | |||
213 | int mailsession_rename_folder(mailsession * session, | ||
214 | char * mb, char * new_name); | ||
215 | |||
216 | /* | ||
217 | mailsession_check_folder makes a checkpoint of the session | ||
218 | |||
219 | @param session the session | ||
220 | |||
221 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
222 | on error | ||
223 | */ | ||
224 | |||
225 | int mailsession_check_folder(mailsession * session); | ||
226 | |||
227 | |||
228 | /* | ||
229 | NOTE: this function is not implemented in most drivers | ||
230 | |||
231 | mailsession_examine_folder selects a mailbox as readonly | ||
232 | |||
233 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
234 | on error | ||
235 | */ | ||
236 | |||
237 | int mailsession_examine_folder(mailsession * session, char * mb); | ||
238 | |||
239 | |||
240 | /* | ||
241 | mailsession_select_folder selects a mailbox | ||
242 | |||
243 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
244 | on error | ||
245 | */ | ||
246 | |||
247 | int mailsession_select_folder(mailsession * session, char * mb); | ||
248 | |||
249 | |||
250 | /* | ||
251 | mailsession_expunge_folder deletes all messages marked \Deleted | ||
252 | |||
253 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
254 | on error | ||
255 | */ | ||
256 | |||
257 | int mailsession_expunge_folder(mailsession * session); | ||
258 | |||
259 | |||
260 | /* | ||
261 | mailsession_status_folder queries the status of the folder | ||
262 | (number of messages, number of recent messages, number of unseen messages) | ||
263 | |||
264 | @param session the session | ||
265 | @param mb mailbox to query | ||
266 | @param result_messages the number of messages is stored | ||
267 | in (* result_messages) | ||
268 | @param result_recent the number of messages is stored | ||
269 | in (* result_recent) | ||
270 | @param result_unseen the number of messages is stored | ||
271 | in (* result_unseen) | ||
272 | |||
273 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
274 | on error | ||
275 | */ | ||
276 | |||
277 | int mailsession_status_folder(mailsession * session, char * mb, | ||
278 | uint32_t * result_messages, uint32_t * result_recent, | ||
279 | uint32_t * result_unseen); | ||
280 | |||
281 | |||
282 | /* | ||
283 | mailsession_messages_number queries the number of messages in the folder | ||
284 | |||
285 | @param session the session | ||
286 | @param mb mailbox to query | ||
287 | @param result the number of messages is stored in (* result) | ||
288 | |||
289 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
290 | on error | ||
291 | */ | ||
292 | |||
293 | int mailsession_messages_number(mailsession * session, char * mb, | ||
294 | uint32_t * result); | ||
295 | |||
296 | /* | ||
297 | mailsession_recent_number queries the number of recent messages in the folder | ||
298 | |||
299 | @param session the session | ||
300 | @param mb mailbox to query | ||
301 | @param result the number of recent messages is stored in (* result) | ||
302 | |||
303 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
304 | on error | ||
305 | */ | ||
306 | |||
307 | int mailsession_recent_number(mailsession * session, | ||
308 | char * mb, uint32_t * result); | ||
309 | |||
310 | /* | ||
311 | mailsession_unseen_number queries the number of unseen messages in the folder | ||
312 | |||
313 | @param session the session | ||
314 | @param mb mailbox to query | ||
315 | @param result the number of unseen messages is stored in (* result) | ||
316 | |||
317 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
318 | on error | ||
319 | */ | ||
320 | |||
321 | int mailsession_unseen_number(mailsession * session, char * mb, | ||
322 | uint32_t * result); | ||
323 | |||
324 | /* | ||
325 | NOTE: driver's specific should be used | ||
326 | |||
327 | mailsession_list_folders returns the list of all sub-mailboxes | ||
328 | of the given mailbox | ||
329 | |||
330 | @param session the session | ||
331 | @param mb the mailbox | ||
332 | @param result list of mailboxes if stored in (* result), | ||
333 | this structure have to be freed with mail_list_free() | ||
334 | |||
335 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
336 | on error | ||
337 | */ | ||
338 | |||
339 | int mailsession_list_folders(mailsession * session, char * mb, | ||
340 | struct mail_list ** result); | ||
341 | |||
342 | /* | ||
343 | NOTE: driver's specific should be used | ||
344 | |||
345 | mailsession_lsub_folders returns the list of subscribed | ||
346 | sub-mailboxes of the given mailbox | ||
347 | |||
348 | @param session the session | ||
349 | @param mb the mailbox | ||
350 | @param result list of mailboxes if stored in (* result), | ||
351 | this structure have to be freed with mail_list_free() | ||
352 | |||
353 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
354 | on error | ||
355 | */ | ||
356 | |||
357 | int mailsession_lsub_folders(mailsession * session, char * mb, | ||
358 | struct mail_list ** result); | ||
359 | |||
360 | /* | ||
361 | NOTE: driver's specific should be used | ||
362 | |||
363 | mailsession_subscribe_folder subscribes to the given mailbox | ||
364 | |||
365 | @param session the session | ||
366 | @param mb the mailbox | ||
367 | |||
368 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
369 | on error | ||
370 | */ | ||
371 | |||
372 | int mailsession_subscribe_folder(mailsession * session, char * mb); | ||
373 | |||
374 | /* | ||
375 | NOTE: driver's specific should be used | ||
376 | |||
377 | mailsession_unsubscribe_folder unsubscribes to the given mailbox | ||
378 | |||
379 | @param session the session | ||
380 | @param mb the mailbox | ||
381 | |||
382 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
383 | on error | ||
384 | */ | ||
385 | |||
386 | int mailsession_unsubscribe_folder(mailsession * session, char * mb); | ||
387 | |||
388 | /* | ||
389 | mailsession_append_message adds a RFC 2822 message to the current | ||
390 | given mailbox | ||
391 | |||
392 | @param session the session | ||
393 | @param message is a string that contains the RFC 2822 message | ||
394 | @param size this is the size of the message | ||
395 | |||
396 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
397 | on error | ||
398 | */ | ||
399 | |||
400 | int mailsession_append_message(mailsession * session, | ||
401 | char * message, size_t size); | ||
402 | |||
403 | int mailsession_append_message_flags(mailsession * session, | ||
404 | char * message, size_t size, struct mail_flags * flags); | ||
405 | |||
406 | /* | ||
407 | NOTE: some drivers does not implement this | ||
408 | |||
409 | mailsession_copy_message copies a message whose number is given to | ||
410 | a given mailbox. The mailbox must be accessible from the same session. | ||
411 | |||
412 | @param session the session | ||
413 | @param num the message number | ||
414 | @param mb the destination mailbox | ||
415 | |||
416 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
417 | on error | ||
418 | */ | ||
419 | |||
420 | int mailsession_copy_message(mailsession * session, | ||
421 | uint32_t num, char * mb); | ||
422 | |||
423 | /* | ||
424 | NOTE: some drivers does not implement this | ||
425 | |||
426 | mailsession_move_message copies a message whose number is given to | ||
427 | a given mailbox. The mailbox must be accessible from the same session. | ||
428 | |||
429 | @param session the session | ||
430 | @param num the message number | ||
431 | @param mb the destination mailbox | ||
432 | |||
433 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
434 | on error | ||
435 | */ | ||
436 | |||
437 | int mailsession_move_message(mailsession * session, | ||
438 | uint32_t num, char * mb); | ||
439 | |||
440 | /* | ||
441 | mailsession_get_messages_list returns the list of message numbers | ||
442 | of the current mailbox. | ||
443 | |||
444 | @param session the session | ||
445 | @param result the list of message numbers will be stored in (* result), | ||
446 | this structure have to be freed with mailmessage_list_free() | ||
447 | |||
448 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
449 | on error | ||
450 | */ | ||
451 | |||
452 | int mailsession_get_messages_list(mailsession * session, | ||
453 | struct mailmessage_list ** result); | ||
454 | |||
455 | /* | ||
456 | mailsession_get_envelopes_list fills the parsed fields in the | ||
457 | mailmessage structures of the mailmessage_list. | ||
458 | |||
459 | @param session the session | ||
460 | @param result this is the list of mailmessage structures | ||
461 | |||
462 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
463 | on error | ||
464 | */ | ||
465 | |||
466 | int mailsession_get_envelopes_list(mailsession * session, | ||
467 | struct mailmessage_list * result); | ||
468 | |||
469 | /* | ||
470 | NOTE: some drivers does not implement this | ||
471 | |||
472 | mailsession_remove_message removes the given message from the mailbox. | ||
473 | The message is permanently deleted. | ||
474 | |||
475 | @param session the session | ||
476 | @param num is the message number | ||
477 | |||
478 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
479 | on error | ||
480 | */ | ||
481 | |||
482 | int mailsession_remove_message(mailsession * session, uint32_t num); | ||
483 | |||
484 | |||
485 | /* | ||
486 | NOTE: this function is not implemented in most drivers | ||
487 | |||
488 | mailsession_search_message returns a list of message numbers that | ||
489 | corresponds to the given criteria. | ||
490 | |||
491 | @param session the session | ||
492 | @param charset is the charset to use (it can be NULL) | ||
493 | @param key is the list of criteria | ||
494 | @param result the search result is stored in (* result), | ||
495 | this structure have to be freed with mail_search_result_free() | ||
496 | |||
497 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
498 | on error | ||
499 | */ | ||
500 | |||
501 | #if 0 | ||
502 | int mailsession_search_messages(mailsession * session, char * charset, | ||
503 | struct mail_search_key * key, | ||
504 | struct mail_search_result ** result); | ||
505 | #endif | ||
506 | |||
507 | /* | ||
508 | mailsession_get_message returns a mailmessage structure that corresponds | ||
509 | to the given message number. | ||
510 | * WARNING * mailsession_get_message_by_uid() should be used instead. | ||
511 | |||
512 | @param session the session | ||
513 | @param num the message number | ||
514 | @param result the allocated mailmessage structure will be stored | ||
515 | in (* result), this structure have to be freed with mailmessage_free() | ||
516 | |||
517 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
518 | on error | ||
519 | */ | ||
520 | |||
521 | int mailsession_get_message(mailsession * session, | ||
522 | uint32_t num, mailmessage ** result); | ||
523 | |||
524 | /* | ||
525 | mailsession_get_message_by_uid returns a mailmessage structure | ||
526 | that corresponds to the given message unique identifier. | ||
527 | This is currently implemented only for cached drivers. | ||
528 | * WARNING * That will deprecates the use of mailsession_get_message() | ||
529 | |||
530 | @param session the session | ||
531 | @param uid the message unique identifier | ||
532 | @param result the allocated mailmessage structure will be stored | ||
533 | in (* result), this structure have to be freed with mailmessage_free() | ||
534 | |||
535 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
536 | on error | ||
537 | */ | ||
538 | |||
539 | int mailsession_get_message_by_uid(mailsession * session, | ||
540 | const char * uid, mailmessage ** result); | ||
541 | |||
542 | #ifdef __cplusplus | ||
543 | } | ||
544 | #endif | ||
545 | |||
546 | #endif | ||
diff --git a/libetpan/src/driver/interface/maildriver_errors.h b/libetpan/src/driver/interface/maildriver_errors.h new file mode 100644 index 0000000..99ec25c --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_errors.h | |||
@@ -0,0 +1,102 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILDRIVER_ERRORS_H | ||
37 | |||
38 | #define MAILDRIVER_ERRORS_H | ||
39 | |||
40 | enum { | ||
41 | MAIL_NO_ERROR = 0, | ||
42 | MAIL_NO_ERROR_AUTHENTICATED, | ||
43 | MAIL_NO_ERROR_NON_AUTHENTICATED, | ||
44 | MAIL_ERROR_NOT_IMPLEMENTED, | ||
45 | MAIL_ERROR_UNKNOWN, | ||
46 | MAIL_ERROR_CONNECT, | ||
47 | MAIL_ERROR_BAD_STATE, | ||
48 | MAIL_ERROR_FILE, | ||
49 | MAIL_ERROR_STREAM, | ||
50 | MAIL_ERROR_LOGIN, | ||
51 | MAIL_ERROR_CREATE, /* 10 */ | ||
52 | MAIL_ERROR_DELETE, | ||
53 | MAIL_ERROR_LOGOUT, | ||
54 | MAIL_ERROR_NOOP, | ||
55 | MAIL_ERROR_RENAME, | ||
56 | MAIL_ERROR_CHECK, | ||
57 | MAIL_ERROR_EXAMINE, | ||
58 | MAIL_ERROR_SELECT, | ||
59 | MAIL_ERROR_MEMORY, | ||
60 | MAIL_ERROR_STATUS, | ||
61 | MAIL_ERROR_SUBSCRIBE, /* 20 */ | ||
62 | MAIL_ERROR_UNSUBSCRIBE, | ||
63 | MAIL_ERROR_LIST, | ||
64 | MAIL_ERROR_LSUB, | ||
65 | MAIL_ERROR_APPEND, | ||
66 | MAIL_ERROR_COPY, | ||
67 | MAIL_ERROR_FETCH, | ||
68 | MAIL_ERROR_STORE, | ||
69 | MAIL_ERROR_SEARCH, | ||
70 | MAIL_ERROR_DISKSPACE, | ||
71 | MAIL_ERROR_MSG_NOT_FOUND, /* 30 */ | ||
72 | MAIL_ERROR_PARSE, | ||
73 | MAIL_ERROR_INVAL, | ||
74 | MAIL_ERROR_PART_NOT_FOUND, | ||
75 | MAIL_ERROR_REMOVE, | ||
76 | MAIL_ERROR_FOLDER_NOT_FOUND, | ||
77 | MAIL_ERROR_MOVE, | ||
78 | MAIL_ERROR_STARTTLS, | ||
79 | MAIL_ERROR_CACHE_MISS, | ||
80 | MAIL_ERROR_NO_TLS, | ||
81 | MAIL_ERROR_EXPUNGE, /* 40 */ | ||
82 | /* misc errors */ | ||
83 | MAIL_ERROR_MISC, | ||
84 | MAIL_ERROR_PROTOCOL, | ||
85 | MAIL_ERROR_CAPABILITY, | ||
86 | MAIL_ERROR_CLOSE, | ||
87 | MAIL_ERROR_FATAL, | ||
88 | MAIL_ERROR_READONLY, | ||
89 | MAIL_ERROR_NO_APOP, | ||
90 | MAIL_ERROR_COMMAND_NOT_SUPPORTED, | ||
91 | MAIL_ERROR_NO_PERMISSION, | ||
92 | MAIL_ERROR_PROGRAM_ERROR, /* 50 */ | ||
93 | MAIL_ERROR_SUBJECT_NOT_FOUND, | ||
94 | MAIL_ERROR_CHAR_ENCODING_FAILED, | ||
95 | MAIL_ERROR_SEND, | ||
96 | MAIL_ERROR_COMMAND, | ||
97 | MAIL_ERROR_SYSTEM, | ||
98 | MAIL_ERROR_UNABLE, | ||
99 | MAIL_ERROR_FOLDER, | ||
100 | }; | ||
101 | |||
102 | #endif | ||
diff --git a/libetpan/src/driver/interface/maildriver_tools.c b/libetpan/src/driver/interface/maildriver_tools.c new file mode 100644 index 0000000..4501b23 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_tools.c | |||
@@ -0,0 +1,841 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "maildriver_tools.h" | ||
37 | |||
38 | #include "libetpan-config.h" | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <ctype.h> | ||
42 | #include <string.h> | ||
43 | #include <dirent.h> | ||
44 | #include <unistd.h> | ||
45 | |||
46 | #include "maildriver.h" | ||
47 | #include "mailmessage.h" | ||
48 | #include "mailstream.h" | ||
49 | #include "mailmime.h" | ||
50 | #include "mail_cache_db.h" | ||
51 | |||
52 | /* ********************************************************************* */ | ||
53 | /* tools */ | ||
54 | |||
55 | |||
56 | int | ||
57 | maildriver_generic_get_envelopes_list(mailsession * session, | ||
58 | struct mailmessage_list * env_list) | ||
59 | { | ||
60 | int r; | ||
61 | unsigned i; | ||
62 | #if 0 | ||
63 | uint32_t j; | ||
64 | uint32_t last_msg; | ||
65 | |||
66 | last_msg = 0; | ||
67 | #endif | ||
68 | |||
69 | #if 0 | ||
70 | j = 0; | ||
71 | i = 0; | ||
72 | while (i < env_list->tab->len) { | ||
73 | mailmessage * msg; | ||
74 | uint32_t index; | ||
75 | |||
76 | msg = carray_get(env_list->tab, i); | ||
77 | |||
78 | index = msg->index; | ||
79 | |||
80 | if (msg->fields == NULL) { | ||
81 | struct mailimf_fields * fields; | ||
82 | |||
83 | r = mailmessage_fetch_envelope(msg, &fields); | ||
84 | if (r != MAIL_NO_ERROR) { | ||
85 | /* do nothing */ | ||
86 | } | ||
87 | else { | ||
88 | msg->fields = fields; | ||
89 | |||
90 | carray_set(env_list->tab, j, msg); | ||
91 | |||
92 | j ++; | ||
93 | last_msg = i + 1; | ||
94 | } | ||
95 | mailmessage_flush(msg); | ||
96 | } | ||
97 | else { | ||
98 | j ++; | ||
99 | last_msg = i + 1; | ||
100 | } | ||
101 | |||
102 | i ++; | ||
103 | } | ||
104 | |||
105 | for(i = last_msg ; i < env_list->tab->len ; i ++) { | ||
106 | mailmessage_free(carray_get(env_list->tab, i)); | ||
107 | carray_set(env_list->tab, i, NULL); | ||
108 | } | ||
109 | |||
110 | r = carray_set_size(env_list->tab, j); | ||
111 | if (r < 0) | ||
112 | return MAIL_ERROR_MEMORY; | ||
113 | #endif | ||
114 | |||
115 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
116 | mailmessage * msg; | ||
117 | |||
118 | msg = carray_get(env_list->msg_tab, i); | ||
119 | |||
120 | if (msg->msg_fields == NULL) { | ||
121 | struct mailimf_fields * fields; | ||
122 | |||
123 | r = mailmessage_fetch_envelope(msg, &fields); | ||
124 | if (r != MAIL_NO_ERROR) { | ||
125 | /* do nothing */ | ||
126 | } | ||
127 | else { | ||
128 | msg->msg_fields = fields; | ||
129 | } | ||
130 | mailmessage_flush(msg); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | return MAIL_NO_ERROR; | ||
135 | } | ||
136 | |||
137 | |||
138 | #if 0 | ||
139 | static int is_search_header_only(struct mail_search_key * key) | ||
140 | { | ||
141 | clistiter * cur; | ||
142 | int result; | ||
143 | |||
144 | switch (key->type) { | ||
145 | case MAIL_SEARCH_KEY_ANSWERED: | ||
146 | case MAIL_SEARCH_KEY_BCC: | ||
147 | case MAIL_SEARCH_KEY_BEFORE: | ||
148 | case MAIL_SEARCH_KEY_CC: | ||
149 | case MAIL_SEARCH_KEY_DELETED: | ||
150 | case MAIL_SEARCH_KEY_FLAGGED: | ||
151 | case MAIL_SEARCH_KEY_FROM: | ||
152 | case MAIL_SEARCH_KEY_NEW: | ||
153 | case MAIL_SEARCH_KEY_OLD: | ||
154 | case MAIL_SEARCH_KEY_ON: | ||
155 | case MAIL_SEARCH_KEY_RECENT: | ||
156 | case MAIL_SEARCH_KEY_SEEN: | ||
157 | case MAIL_SEARCH_KEY_SINCE: | ||
158 | case MAIL_SEARCH_KEY_SUBJECT: | ||
159 | case MAIL_SEARCH_KEY_TO: | ||
160 | case MAIL_SEARCH_KEY_UNANSWERED: | ||
161 | case MAIL_SEARCH_KEY_UNDELETED: | ||
162 | case MAIL_SEARCH_KEY_UNFLAGGED: | ||
163 | case MAIL_SEARCH_KEY_UNSEEN: | ||
164 | case MAIL_SEARCH_KEY_HEADER: | ||
165 | case MAIL_SEARCH_KEY_LARGER: | ||
166 | case MAIL_SEARCH_KEY_NOT: | ||
167 | case MAIL_SEARCH_KEY_SMALLER: | ||
168 | case MAIL_SEARCH_KEY_ALL: | ||
169 | return TRUE; | ||
170 | |||
171 | case MAIL_SEARCH_KEY_BODY: | ||
172 | case MAIL_SEARCH_KEY_TEXT: | ||
173 | return FALSE; | ||
174 | |||
175 | case MAIL_SEARCH_KEY_OR: | ||
176 | return (is_search_header_only(key->or1) && | ||
177 | is_search_header_only(key->or2)); | ||
178 | |||
179 | case MAIL_SEARCH_KEY_MULTIPLE: | ||
180 | result = TRUE; | ||
181 | for (cur = clist_begin(key->multiple) ; cur != NULL ; | ||
182 | cur = clist_next(cur)) | ||
183 | result = result && is_search_header_only(clist_content(cur)); | ||
184 | return result; | ||
185 | |||
186 | default: | ||
187 | return TRUE; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | static int match_header(struct mailimf_fields * fields, | ||
192 | char * name, char * value) | ||
193 | { | ||
194 | clistiter * cur; | ||
195 | |||
196 | for(cur = clist_begin(fields->list) ; cur != NULL ; | ||
197 | cur = clist_content(cur)) { | ||
198 | struct mailimf_field * field; | ||
199 | struct mailimf_optional_field * opt_field; | ||
200 | |||
201 | field = clist_content(cur); | ||
202 | opt_field = field->optional_field; | ||
203 | if ((char) toupper((unsigned char) opt_field->name[0]) == | ||
204 | (char) toupper((unsigned char) name[0])) { | ||
205 | if (strcasecmp(opt_field->name, name) == 0) | ||
206 | if (strstr(opt_field->value, value) != NULL) | ||
207 | return TRUE; | ||
208 | } | ||
209 | } | ||
210 | return FALSE; | ||
211 | } | ||
212 | |||
213 | static int comp_date(struct mailimf_fields * fields, | ||
214 | struct mailimf_date_time * ref_date) | ||
215 | { | ||
216 | clistiter * cur; | ||
217 | struct mailimf_date_time * date; | ||
218 | int r; | ||
219 | |||
220 | date = NULL; | ||
221 | for(cur = clist_begin(fields->list) ; cur != NULL ; | ||
222 | cur = clist_content(cur)) { | ||
223 | struct mailimf_field * field; | ||
224 | struct mailimf_optional_field * opt_field; | ||
225 | |||
226 | field = clist_content(cur); | ||
227 | opt_field = field->optional_field; | ||
228 | if ((char) toupper((unsigned char) opt_field->name[0]) == 'D') { | ||
229 | if (strcasecmp(opt_field->name, "Date") == 0) { | ||
230 | size_t cur_token; | ||
231 | |||
232 | cur_token = 0; | ||
233 | r = mailimf_date_time_parse(opt_field->value, strlen(opt_field->value), | ||
234 | &cur_token, &date); | ||
235 | if (r == MAILIMF_NO_ERROR) | ||
236 | break; | ||
237 | else if (r == MAILIMF_ERROR_PARSE) { | ||
238 | /* do nothing */ | ||
239 | } | ||
240 | else | ||
241 | break; | ||
242 | } | ||
243 | } | ||
244 | } | ||
245 | |||
246 | if (date == NULL) | ||
247 | return 0; | ||
248 | |||
249 | return mailimf_date_time_comp(date, ref_date); | ||
250 | } | ||
251 | |||
252 | static int match_messages(char * message, | ||
253 | size_t size, | ||
254 | struct mailimf_fields * fields, | ||
255 | int32_t flags, | ||
256 | char * charset, | ||
257 | struct mail_search_key * key) | ||
258 | { | ||
259 | clistiter * cur; | ||
260 | size_t length; | ||
261 | size_t cur_token; | ||
262 | int r; | ||
263 | |||
264 | switch (key->type) { | ||
265 | |||
266 | /* flags */ | ||
267 | case MAIL_SEARCH_KEY_ANSWERED: | ||
268 | return ((flags & MAIL_FLAG_ANSWERED) != 0); | ||
269 | |||
270 | case MAIL_SEARCH_KEY_FLAGGED: | ||
271 | return ((flags & MAIL_FLAG_FLAGGED) != 0); | ||
272 | |||
273 | case MAIL_SEARCH_KEY_DELETED: | ||
274 | return ((flags & MAIL_FLAG_DELETED) != 0); | ||
275 | |||
276 | case MAIL_SEARCH_KEY_RECENT: | ||
277 | return ((flags & MAIL_FLAG_NEW) != 0) && | ||
278 | ((flags & MAIL_FLAG_SEEN) == 0); | ||
279 | |||
280 | case MAIL_SEARCH_KEY_SEEN: | ||
281 | return ((flags & MAIL_FLAG_SEEN) != 0); | ||
282 | |||
283 | case MAIL_SEARCH_KEY_NEW: | ||
284 | return ((flags & MAIL_FLAG_NEW) != 0); | ||
285 | |||
286 | case MAIL_SEARCH_KEY_OLD: | ||
287 | return ((flags & MAIL_FLAG_NEW) == 0); | ||
288 | |||
289 | case MAIL_SEARCH_KEY_UNANSWERED: | ||
290 | return ((flags & MAIL_FLAG_ANSWERED) == 0); | ||
291 | |||
292 | case MAIL_SEARCH_KEY_UNDELETED: | ||
293 | return ((flags & MAIL_FLAG_DELETED) == 0); | ||
294 | |||
295 | case MAIL_SEARCH_KEY_UNFLAGGED: | ||
296 | return ((flags & MAIL_FLAG_FLAGGED) == 0); | ||
297 | |||
298 | case MAIL_SEARCH_KEY_UNSEEN: | ||
299 | return ((flags & MAIL_FLAG_SEEN) == 0); | ||
300 | |||
301 | /* headers */ | ||
302 | case MAIL_SEARCH_KEY_BCC: | ||
303 | return match_header(fields, "Bcc", key->bcc); | ||
304 | |||
305 | case MAIL_SEARCH_KEY_CC: | ||
306 | return match_header(fields, "Cc", key->cc); | ||
307 | |||
308 | case MAIL_SEARCH_KEY_FROM: | ||
309 | return match_header(fields, "From", key->from); | ||
310 | |||
311 | case MAIL_SEARCH_KEY_SUBJECT: | ||
312 | return match_header(fields, "Subject", key->subject); | ||
313 | |||
314 | case MAIL_SEARCH_KEY_TO: | ||
315 | return match_header(fields, "To", key->to); | ||
316 | |||
317 | case MAIL_SEARCH_KEY_HEADER: | ||
318 | return match_header(fields, key->header_name, key->header_value); | ||
319 | |||
320 | /* date */ | ||
321 | case MAIL_SEARCH_KEY_BEFORE: | ||
322 | return (comp_date(fields, key->before) <= 0); | ||
323 | |||
324 | case MAIL_SEARCH_KEY_ON: | ||
325 | return (comp_date(fields, key->before) == 0); | ||
326 | |||
327 | case MAIL_SEARCH_KEY_SINCE: | ||
328 | return (comp_date(fields, key->before) >= 0); | ||
329 | |||
330 | /* boolean */ | ||
331 | case MAIL_SEARCH_KEY_NOT: | ||
332 | return (!match_messages(message, size, fields, flags, charset, key->not)); | ||
333 | case MAIL_SEARCH_KEY_OR: | ||
334 | return (match_messages(message, size, fields, flags, charset, key->or1) || | ||
335 | match_messages(message, size, fields, flags, charset, key->or2)); | ||
336 | |||
337 | case MAIL_SEARCH_KEY_MULTIPLE: | ||
338 | for(cur = clist_begin(key->multiple) ; cur != NULL ; | ||
339 | cur = clist_next(cur)) { | ||
340 | if (!match_messages(message, size, fields, flags, charset, | ||
341 | clist_content(cur))) | ||
342 | return FALSE; | ||
343 | } | ||
344 | |||
345 | return TRUE; | ||
346 | |||
347 | /* size */ | ||
348 | case MAIL_SEARCH_KEY_SMALLER: | ||
349 | return (size <= key->smaller); | ||
350 | |||
351 | case MAIL_SEARCH_KEY_LARGER: | ||
352 | return (size >= key->larger); | ||
353 | |||
354 | case MAIL_SEARCH_KEY_BODY: | ||
355 | length = strlen(message); | ||
356 | |||
357 | cur_token = 0; | ||
358 | while (1) { | ||
359 | r = mailimf_ignore_field_parse(message, length, &cur_token); | ||
360 | if (r == MAILIMF_NO_ERROR) { | ||
361 | /* do nothing */ | ||
362 | } | ||
363 | else | ||
364 | break; | ||
365 | } | ||
366 | |||
367 | return (strstr(message + cur_token, key->body) != NULL); | ||
368 | |||
369 | case MAIL_SEARCH_KEY_TEXT: | ||
370 | return (strstr(message, key->body) != NULL); | ||
371 | |||
372 | case MAIL_SEARCH_KEY_ALL: | ||
373 | default: | ||
374 | return TRUE; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | int maildriver_generic_search_messages(mailsession * session, char * charset, | ||
379 | struct mail_search_key * key, | ||
380 | struct mail_search_result ** result) | ||
381 | { | ||
382 | int header; | ||
383 | clist * list; | ||
384 | struct mail_search_result * search_result; | ||
385 | int r; | ||
386 | struct mailmessage_list * env_list; | ||
387 | int res; | ||
388 | unsigned int i; | ||
389 | |||
390 | header = is_search_header_only(key); | ||
391 | |||
392 | r = mailsession_get_messages_list(session, &env_list); | ||
393 | if (r != MAIL_NO_ERROR) | ||
394 | return r; | ||
395 | |||
396 | list = NULL; | ||
397 | for(i = 0 ; i < carray_count(env_list->tab) ; i ++) { | ||
398 | char * message; | ||
399 | size_t length; | ||
400 | struct mail_info * info; | ||
401 | uint32_t flags; | ||
402 | struct mailimf_fields * fields; | ||
403 | size_t cur_token; | ||
404 | |||
405 | info = carray_get(env_list->tab, i); | ||
406 | |||
407 | if (!header) { | ||
408 | r = mailsession_fetch_message(session, info->index, &message, &length); | ||
409 | if (r != MAIL_NO_ERROR) { | ||
410 | res = r; | ||
411 | goto free_list; | ||
412 | } | ||
413 | |||
414 | cur_token = 0; | ||
415 | r = mailimf_optional_fields_parse(message, length, | ||
416 | &cur_token, &fields); | ||
417 | if (r != MAILIMF_NO_ERROR) { | ||
418 | res = MAIL_ERROR_PARSE; | ||
419 | goto free_list; | ||
420 | } | ||
421 | } | ||
422 | else { | ||
423 | char * msg_header; | ||
424 | int r; | ||
425 | size_t cur_token; | ||
426 | size_t header_len; | ||
427 | |||
428 | r = mailsession_fetch_message_header(session, info->index, &msg_header, | ||
429 | &header_len); | ||
430 | if (r != MAIL_NO_ERROR) { | ||
431 | res = r; | ||
432 | goto free_list; | ||
433 | } | ||
434 | |||
435 | message = NULL; | ||
436 | cur_token = 0; | ||
437 | r = mailimf_optional_fields_parse(msg_header, header_len, | ||
438 | &cur_token, &fields); | ||
439 | if (r != MAILIMF_NO_ERROR) { | ||
440 | res = MAIL_ERROR_PARSE; | ||
441 | goto free_list; | ||
442 | } | ||
443 | |||
444 | mailsession_fetch_result_free(session, msg_header); | ||
445 | } | ||
446 | |||
447 | r = mailsession_get_message_flags(session, info->index, &flags); | ||
448 | if (r != MAIL_NO_ERROR) { | ||
449 | res = r; | ||
450 | goto free_list; | ||
451 | } | ||
452 | |||
453 | if (match_messages(message, info->size, fields, flags, | ||
454 | charset, key)) { | ||
455 | uint32_t * pnum; | ||
456 | |||
457 | pnum = malloc(sizeof(* pnum)); | ||
458 | if (pnum == NULL) { | ||
459 | if (message != NULL) | ||
460 | mailsession_fetch_result_free(session, message); | ||
461 | res = MAIL_ERROR_MEMORY; | ||
462 | goto free_list; | ||
463 | } | ||
464 | |||
465 | * pnum = info->index; | ||
466 | |||
467 | r = clist_append(list, pnum); | ||
468 | if (r < 0) { | ||
469 | free(pnum); | ||
470 | if (message != NULL) | ||
471 | mailsession_fetch_result_free(session, message); | ||
472 | res = MAIL_ERROR_MEMORY; | ||
473 | goto free_list; | ||
474 | } | ||
475 | } | ||
476 | |||
477 | if (message != NULL) | ||
478 | mailsession_fetch_result_free(session, message); | ||
479 | } | ||
480 | |||
481 | search_result = mail_search_result_new(list); | ||
482 | if (search_result == NULL) { | ||
483 | res = MAIL_ERROR_MEMORY; | ||
484 | goto free_list; | ||
485 | } | ||
486 | |||
487 | * result = search_result; | ||
488 | |||
489 | return MAIL_NO_ERROR; | ||
490 | |||
491 | free_list: | ||
492 | clist_foreach(list, (clist_func) free, NULL); | ||
493 | clist_free(list); | ||
494 | mailmessage_list_free(env_list); | ||
495 | return res; | ||
496 | } | ||
497 | #endif | ||
498 | |||
499 | #if 0 | ||
500 | int maildriver_generic_search_messages(mailsession * session, char * charset, | ||
501 | struct mail_search_key * key, | ||
502 | struct mail_search_result ** result) | ||
503 | { | ||
504 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
505 | } | ||
506 | #endif | ||
507 | |||
508 | int | ||
509 | maildriver_env_list_to_msg_list(struct mailmessage_list * env_list, | ||
510 | clist ** result) | ||
511 | { | ||
512 | clist * msg_list; | ||
513 | int r; | ||
514 | int res; | ||
515 | unsigned int i; | ||
516 | |||
517 | msg_list = clist_new(); | ||
518 | if (msg_list == NULL) { | ||
519 | res = MAIL_ERROR_MEMORY; | ||
520 | goto err; | ||
521 | } | ||
522 | |||
523 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
524 | mailmessage * msg; | ||
525 | |||
526 | msg = carray_get(env_list->msg_tab, i); | ||
527 | |||
528 | if (msg->msg_fields == NULL) { | ||
529 | uint32_t * pindex; | ||
530 | |||
531 | pindex = malloc(sizeof(* pindex)); | ||
532 | if (pindex == NULL) { | ||
533 | res = MAIL_ERROR_MEMORY; | ||
534 | goto free_msg_list; | ||
535 | } | ||
536 | |||
537 | * pindex = msg->msg_index; | ||
538 | |||
539 | r = clist_append(msg_list, pindex); | ||
540 | if (r < 0) { | ||
541 | free(pindex); | ||
542 | res = MAIL_ERROR_MEMORY; | ||
543 | goto free_msg_list; | ||
544 | } | ||
545 | |||
546 | } | ||
547 | } | ||
548 | |||
549 | * result = msg_list; | ||
550 | |||
551 | return MAIL_NO_ERROR; | ||
552 | |||
553 | free_msg_list: | ||
554 | clist_foreach(msg_list, (clist_func) free, NULL); | ||
555 | clist_free(msg_list); | ||
556 | err: | ||
557 | return res; | ||
558 | } | ||
559 | |||
560 | |||
561 | int | ||
562 | maildriver_env_list_to_msg_list_no_flags(struct mailmessage_list * env_list, | ||
563 | clist ** result) | ||
564 | { | ||
565 | clist * msg_list; | ||
566 | int r; | ||
567 | int res; | ||
568 | unsigned int i; | ||
569 | |||
570 | msg_list = clist_new(); | ||
571 | if (msg_list == NULL) { | ||
572 | res = MAIL_ERROR_MEMORY; | ||
573 | goto err; | ||
574 | } | ||
575 | |||
576 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
577 | mailmessage * msg; | ||
578 | |||
579 | msg = carray_get(env_list->msg_tab, i); | ||
580 | |||
581 | if (msg->msg_flags == NULL) { | ||
582 | uint32_t * pindex; | ||
583 | |||
584 | pindex = malloc(sizeof(* pindex)); | ||
585 | if (pindex == NULL) { | ||
586 | res = MAIL_ERROR_MEMORY; | ||
587 | goto free_msg_list; | ||
588 | } | ||
589 | |||
590 | * pindex = msg->msg_index; | ||
591 | |||
592 | r = clist_append(msg_list, pindex); | ||
593 | if (r < 0) { | ||
594 | free(pindex); | ||
595 | res = MAIL_ERROR_MEMORY; | ||
596 | goto free_msg_list; | ||
597 | } | ||
598 | |||
599 | } | ||
600 | } | ||
601 | |||
602 | * result = msg_list; | ||
603 | |||
604 | return MAIL_NO_ERROR; | ||
605 | |||
606 | free_msg_list: | ||
607 | clist_foreach(msg_list, (clist_func) free, NULL); | ||
608 | clist_free(msg_list); | ||
609 | err: | ||
610 | return res; | ||
611 | } | ||
612 | |||
613 | |||
614 | |||
615 | int maildriver_imf_error_to_mail_error(int error) | ||
616 | { | ||
617 | switch (error) { | ||
618 | case MAILIMF_NO_ERROR: | ||
619 | return MAIL_NO_ERROR; | ||
620 | |||
621 | case MAILIMF_ERROR_PARSE: | ||
622 | return MAIL_ERROR_PARSE; | ||
623 | |||
624 | case MAILIMF_ERROR_MEMORY: | ||
625 | return MAIL_ERROR_MEMORY; | ||
626 | |||
627 | case MAILIMF_ERROR_INVAL: | ||
628 | return MAIL_ERROR_INVAL; | ||
629 | |||
630 | case MAILIMF_ERROR_FILE: | ||
631 | return MAIL_ERROR_FILE; | ||
632 | |||
633 | default: | ||
634 | return MAIL_ERROR_INVAL; | ||
635 | } | ||
636 | } | ||
637 | |||
638 | char * maildriver_quote_mailbox(char * mb) | ||
639 | { | ||
640 | MMAPString * gstr; | ||
641 | char * str; | ||
642 | |||
643 | gstr = mmap_string_new(""); | ||
644 | if (gstr == NULL) | ||
645 | return NULL; | ||
646 | |||
647 | while (* mb != 0) { | ||
648 | char hex[3]; | ||
649 | |||
650 | if (((* mb >= 'a') && (* mb <= 'z')) || | ||
651 | ((* mb >= 'A') && (* mb <= 'Z')) || | ||
652 | ((* mb >= '0') && (* mb <= '9'))) | ||
653 | mmap_string_append_c(gstr, * mb); | ||
654 | else { | ||
655 | if (mmap_string_append_c(gstr, '%') == NULL) | ||
656 | goto free; | ||
657 | snprintf(hex, 3, "%02x", (unsigned char) (* mb)); | ||
658 | if (mmap_string_append(gstr, hex) == NULL) | ||
659 | goto free; | ||
660 | } | ||
661 | mb ++; | ||
662 | } | ||
663 | |||
664 | str = strdup(gstr->str); | ||
665 | if (str == NULL) | ||
666 | goto free; | ||
667 | |||
668 | mmap_string_free(gstr); | ||
669 | |||
670 | return str; | ||
671 | |||
672 | free: | ||
673 | mmap_string_free(gstr); | ||
674 | return NULL; | ||
675 | } | ||
676 | |||
677 | |||
678 | |||
679 | int maildriver_cache_clean_up(struct mail_cache_db * cache_db_env, | ||
680 | struct mail_cache_db * cache_db_flags, | ||
681 | struct mailmessage_list * env_list) | ||
682 | { | ||
683 | chash * hash_exist; | ||
684 | int res; | ||
685 | int r; | ||
686 | char keyname[PATH_MAX]; | ||
687 | unsigned int i; | ||
688 | |||
689 | /* flush cache */ | ||
690 | |||
691 | hash_exist = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL); | ||
692 | if (hash_exist == NULL) { | ||
693 | res = MAIL_ERROR_MEMORY; | ||
694 | goto err; | ||
695 | } | ||
696 | |||
697 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
698 | mailmessage * msg; | ||
699 | chashdatum key; | ||
700 | chashdatum value; | ||
701 | |||
702 | msg = carray_get(env_list->msg_tab, i); | ||
703 | |||
704 | value.data = NULL; | ||
705 | value.len = 0; | ||
706 | |||
707 | if (cache_db_env != NULL) { | ||
708 | snprintf(keyname, PATH_MAX, "%s-envelope", msg->msg_uid); | ||
709 | |||
710 | key.data = keyname; | ||
711 | key.len = strlen(keyname); | ||
712 | r = chash_set(hash_exist, &key, &value, NULL); | ||
713 | if (r < 0) { | ||
714 | res = MAIL_ERROR_MEMORY; | ||
715 | goto free; | ||
716 | } | ||
717 | } | ||
718 | |||
719 | if (cache_db_flags != NULL) { | ||
720 | snprintf(keyname, PATH_MAX, "%s-flags", msg->msg_uid); | ||
721 | |||
722 | key.data = keyname; | ||
723 | key.len = strlen(keyname); | ||
724 | r = chash_set(hash_exist, &key, &value, NULL); | ||
725 | if (r < 0) { | ||
726 | res = MAIL_ERROR_MEMORY; | ||
727 | goto free; | ||
728 | } | ||
729 | } | ||
730 | } | ||
731 | |||
732 | /* clean up */ | ||
733 | if (cache_db_env != NULL) | ||
734 | mail_cache_db_clean_up(cache_db_env, hash_exist); | ||
735 | if (cache_db_flags != NULL) | ||
736 | mail_cache_db_clean_up(cache_db_flags, hash_exist); | ||
737 | |||
738 | chash_free(hash_exist); | ||
739 | |||
740 | return MAIL_NO_ERROR; | ||
741 | |||
742 | free: | ||
743 | chash_free(hash_exist); | ||
744 | err: | ||
745 | return res; | ||
746 | } | ||
747 | |||
748 | |||
749 | /* | ||
750 | maildriver_message_cache_clean_up() | ||
751 | |||
752 | remove files in cache_dir that does not correspond to a message. | ||
753 | |||
754 | get_uid_from_filename() modifies the given filename so that it | ||
755 | is a uid when returning from the function. If get_uid_from_filename() | ||
756 | clears the content of file (set to empty string), this means that | ||
757 | this file should not be deleted. | ||
758 | */ | ||
759 | |||
760 | int maildriver_message_cache_clean_up(char * cache_dir, | ||
761 | struct mailmessage_list * env_list, | ||
762 | void (* get_uid_from_filename)(char *)) | ||
763 | { | ||
764 | chash * hash_exist; | ||
765 | DIR * d; | ||
766 | char cached_filename[PATH_MAX]; | ||
767 | struct dirent * ent; | ||
768 | char keyname[PATH_MAX]; | ||
769 | unsigned int i; | ||
770 | int res; | ||
771 | int r; | ||
772 | |||
773 | /* remove files */ | ||
774 | |||
775 | hash_exist = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYALL); | ||
776 | if (hash_exist == NULL) { | ||
777 | res = MAIL_ERROR_MEMORY; | ||
778 | goto err; | ||
779 | } | ||
780 | |||
781 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
782 | mailmessage * msg; | ||
783 | chashdatum key; | ||
784 | chashdatum value; | ||
785 | |||
786 | msg = carray_get(env_list->msg_tab, i); | ||
787 | |||
788 | key.data = msg->msg_uid; | ||
789 | key.len = strlen(msg->msg_uid); | ||
790 | value.data = NULL; | ||
791 | value.len = 0; | ||
792 | r = chash_set(hash_exist, &key, &value, NULL); | ||
793 | if (r < 0) { | ||
794 | res = MAIL_ERROR_MEMORY; | ||
795 | goto free; | ||
796 | } | ||
797 | } | ||
798 | |||
799 | d = opendir(cache_dir); | ||
800 | while ((ent = readdir(d)) != NULL) { | ||
801 | chashdatum key; | ||
802 | chashdatum value; | ||
803 | |||
804 | if (strcmp(ent->d_name, ".") == 0) | ||
805 | continue; | ||
806 | |||
807 | if (strcmp(ent->d_name, "..") == 0) | ||
808 | continue; | ||
809 | |||
810 | if (strstr(ent->d_name, ".db") != NULL) | ||
811 | continue; | ||
812 | |||
813 | strncpy(keyname, ent->d_name, sizeof(keyname)); | ||
814 | keyname[sizeof(keyname) - 1] = '\0'; | ||
815 | |||
816 | get_uid_from_filename(keyname); | ||
817 | |||
818 | if (* keyname == '\0') | ||
819 | continue; | ||
820 | |||
821 | key.data = keyname; | ||
822 | key.len = strlen(keyname); | ||
823 | |||
824 | r = chash_get(hash_exist, &key, &value); | ||
825 | if (r < 0) { | ||
826 | snprintf(cached_filename, sizeof(cached_filename), | ||
827 | "%s/%s", cache_dir, ent->d_name); | ||
828 | unlink(cached_filename); | ||
829 | } | ||
830 | } | ||
831 | closedir(d); | ||
832 | |||
833 | chash_free(hash_exist); | ||
834 | |||
835 | return MAIL_NO_ERROR; | ||
836 | |||
837 | free: | ||
838 | chash_free(hash_exist); | ||
839 | err: | ||
840 | return res; | ||
841 | } | ||
diff --git a/libetpan/src/driver/interface/maildriver_tools.h b/libetpan/src/driver/interface/maildriver_tools.h new file mode 100644 index 0000000..a92af42 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_tools.h | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILDRIVER_TOOLS_H | ||
37 | |||
38 | #define MAILDRIVER_TOOLS_H | ||
39 | |||
40 | #include "maildriver_types.h" | ||
41 | #include "mail_cache_db_types.h" | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | int | ||
48 | maildriver_generic_get_envelopes_list(mailsession * session, | ||
49 | struct mailmessage_list * env_list); | ||
50 | |||
51 | #if 0 | ||
52 | int maildriver_generic_search_messages(mailsession * session, char * charset, | ||
53 | struct mail_search_key * key, | ||
54 | struct mail_search_result ** result); | ||
55 | #endif | ||
56 | |||
57 | int | ||
58 | maildriver_env_list_to_msg_list(struct mailmessage_list * env_list, | ||
59 | clist ** result); | ||
60 | |||
61 | int maildriver_imf_error_to_mail_error(int error); | ||
62 | |||
63 | char * maildriver_quote_mailbox(char * mb); | ||
64 | |||
65 | int | ||
66 | maildriver_env_list_to_msg_list_no_flags(struct mailmessage_list * env_list, | ||
67 | clist ** result); | ||
68 | |||
69 | int maildriver_cache_clean_up(struct mail_cache_db * cache_db_env, | ||
70 | struct mail_cache_db * cache_db_flags, | ||
71 | struct mailmessage_list * env_list); | ||
72 | |||
73 | int maildriver_message_cache_clean_up(char * cache_dir, | ||
74 | struct mailmessage_list * env_list, | ||
75 | void (* get_uid_from_filename)(char *)); | ||
76 | |||
77 | #ifdef __cplusplus | ||
78 | } | ||
79 | #endif | ||
80 | |||
81 | #endif | ||
diff --git a/libetpan/src/driver/interface/maildriver_types.c b/libetpan/src/driver/interface/maildriver_types.c new file mode 100644 index 0000000..43f5c4f --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_types.c | |||
@@ -0,0 +1,340 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "maildriver_types.h" | ||
37 | #include <time.h> | ||
38 | #include <stdlib.h> | ||
39 | #include "mailmessage.h" | ||
40 | |||
41 | struct mailmessage_list * mailmessage_list_new(carray * msg_tab) | ||
42 | { | ||
43 | struct mailmessage_list * env_list; | ||
44 | |||
45 | env_list = malloc(sizeof(* env_list)); | ||
46 | if (env_list == NULL) | ||
47 | return NULL; | ||
48 | |||
49 | env_list->msg_tab = msg_tab; | ||
50 | |||
51 | return env_list; | ||
52 | } | ||
53 | |||
54 | void mailmessage_list_free(struct mailmessage_list * env_list) | ||
55 | { | ||
56 | unsigned int i; | ||
57 | |||
58 | for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { | ||
59 | mailmessage * msg; | ||
60 | |||
61 | msg = carray_get(env_list->msg_tab, i); | ||
62 | if (msg != NULL) | ||
63 | mailmessage_free(msg); | ||
64 | } | ||
65 | carray_free(env_list->msg_tab); | ||
66 | free(env_list); | ||
67 | } | ||
68 | |||
69 | struct mail_list * mail_list_new(clist * list) | ||
70 | { | ||
71 | struct mail_list * resp; | ||
72 | |||
73 | resp = malloc(sizeof(* resp)); | ||
74 | if (resp == NULL) | ||
75 | return NULL; | ||
76 | resp->mb_list = list; | ||
77 | |||
78 | return resp; | ||
79 | } | ||
80 | |||
81 | void mail_list_free(struct mail_list * resp) | ||
82 | { | ||
83 | clist_foreach(resp->mb_list, (clist_func) free, NULL); | ||
84 | clist_free(resp->mb_list); | ||
85 | free(resp); | ||
86 | } | ||
87 | |||
88 | static int32_t mailimf_date_time_to_int(struct mailimf_date_time * date) | ||
89 | { | ||
90 | return date->dt_year * 12 * 30 * 24 * 60 * 60 + | ||
91 | date->dt_month * 30 * 24 * 60 * 60 + date->dt_day * 24 * 60 * 60 + | ||
92 | (date->dt_hour - date->dt_zone) * 60 * 60 + | ||
93 | date->dt_min * 60 + date->dt_sec; | ||
94 | } | ||
95 | |||
96 | int32_t mailimf_date_time_comp(struct mailimf_date_time * date1, | ||
97 | struct mailimf_date_time * date2) | ||
98 | { | ||
99 | return mailimf_date_time_to_int(date1) - mailimf_date_time_to_int(date2); | ||
100 | } | ||
101 | |||
102 | |||
103 | |||
104 | |||
105 | |||
106 | |||
107 | |||
108 | #if 0 | ||
109 | struct mail_search_key * | ||
110 | mail_search_key_new(int sk_type, | ||
111 | char * sk_bcc, | ||
112 | struct mailimf_date_time * sk_before, | ||
113 | char * sk_body, | ||
114 | char * sk_cc, | ||
115 | char * sk_from, | ||
116 | struct mailimf_date_time * sk_on, | ||
117 | struct mailimf_date_time * sk_since, | ||
118 | char * sk_subject, | ||
119 | char * sk_text, | ||
120 | char * sk_to, | ||
121 | char * sk_header_name, | ||
122 | char * sk_header_value, | ||
123 | size_t sk_larger, | ||
124 | struct mail_search_key * sk_not, | ||
125 | struct mail_search_key * sk_or1, | ||
126 | struct mail_search_key * sk_or2, | ||
127 | size_t sk_smaller, | ||
128 | clist * sk_multiple) | ||
129 | { | ||
130 | struct mail_search_key * key; | ||
131 | |||
132 | key = malloc(sizeof(* key)); | ||
133 | if (key == NULL) | ||
134 | return NULL; | ||
135 | |||
136 | key->sk_type = sk_type; | ||
137 | key->sk_bcc = sk_bcc; | ||
138 | key->sk_before = sk_before; | ||
139 | key->sk_body = sk_body; | ||
140 | key->sk_cc = sk_cc; | ||
141 | key->sk_from = sk_from; | ||
142 | key->sk_on = sk_on; | ||
143 | key->sk_since = sk_since; | ||
144 | key->sk_subject = sk_subject; | ||
145 | key->sk_text = sk_text; | ||
146 | key->sk_to = sk_to; | ||
147 | key->sk_header_name = sk_header_name; | ||
148 | key->sk_header_value = sk_header_value; | ||
149 | key->sk_larger = sk_larger; | ||
150 | key->sk_not = sk_not; | ||
151 | key->sk_or1 = sk_or1; | ||
152 | key->sk_or2 = sk_or2; | ||
153 | key->sk_smaller = sk_smaller; | ||
154 | key->sk_multiple = sk_multiple; | ||
155 | |||
156 | return key; | ||
157 | } | ||
158 | |||
159 | |||
160 | void mail_search_key_free(struct mail_search_key * key) | ||
161 | { | ||
162 | if (key->sk_bcc) | ||
163 | free(key->sk_bcc); | ||
164 | if (key->sk_before) | ||
165 | mailimf_date_time_free(key->sk_before); | ||
166 | if (key->sk_body) | ||
167 | free(key->sk_body); | ||
168 | if (key->sk_cc) | ||
169 | free(key->sk_cc); | ||
170 | if (key->sk_from) | ||
171 | free(key->sk_from); | ||
172 | if (key->sk_on) | ||
173 | mailimf_date_time_free(key->sk_on); | ||
174 | if (key->sk_since) | ||
175 | mailimf_date_time_free(key->sk_since); | ||
176 | if (key->sk_subject) | ||
177 | free(key->sk_subject); | ||
178 | if (key->sk_text) | ||
179 | free(key->sk_text); | ||
180 | if (key->sk_to) | ||
181 | free(key->sk_to); | ||
182 | if (key->sk_header_name) | ||
183 | free(key->sk_header_name); | ||
184 | if (key->sk_header_value) | ||
185 | free(key->sk_header_value); | ||
186 | if (key->sk_not) | ||
187 | mail_search_key_free(key->sk_not); | ||
188 | if (key->sk_or1) | ||
189 | mail_search_key_free(key->sk_or1); | ||
190 | if (key->sk_or2) | ||
191 | mail_search_key_free(key->sk_or2); | ||
192 | if (key->sk_multiple) { | ||
193 | clist_foreach(key->sk_multiple, (clist_func) mail_search_key_free, NULL); | ||
194 | clist_free(key->sk_multiple); | ||
195 | } | ||
196 | |||
197 | free(key); | ||
198 | } | ||
199 | |||
200 | |||
201 | struct mail_search_result * mail_search_result_new(clist * list) | ||
202 | { | ||
203 | struct mail_search_result * search_result; | ||
204 | |||
205 | search_result = malloc(sizeof(* search_result)); | ||
206 | if (search_result == NULL) | ||
207 | return NULL; | ||
208 | search_result->list = list; | ||
209 | |||
210 | return search_result; | ||
211 | } | ||
212 | |||
213 | void mail_search_result_free(struct mail_search_result * search_result) | ||
214 | { | ||
215 | clist_foreach(search_result->list, (clist_func) free, NULL); | ||
216 | clist_free(search_result->list); | ||
217 | free(search_result); | ||
218 | } | ||
219 | #endif | ||
220 | |||
221 | struct error_message { | ||
222 | int code; | ||
223 | char * message; | ||
224 | }; | ||
225 | |||
226 | static struct error_message message_tab[] = { | ||
227 | { MAIL_NO_ERROR, "no error" }, | ||
228 | { MAIL_NO_ERROR_AUTHENTICATED, "no error - authenticated" }, | ||
229 | { MAIL_NO_ERROR_NON_AUTHENTICATED, "no error - not authenticated" }, | ||
230 | { MAIL_ERROR_NOT_IMPLEMENTED, "not implemented" }, | ||
231 | { MAIL_ERROR_UNKNOWN, "unknown"}, | ||
232 | { MAIL_ERROR_CONNECT, "connect"}, | ||
233 | { MAIL_ERROR_BAD_STATE, "bad state"}, | ||
234 | { MAIL_ERROR_FILE, "file error - file could not be accessed" }, | ||
235 | { MAIL_ERROR_STREAM, "stream error - socket could not be read or written" }, | ||
236 | { MAIL_ERROR_LOGIN, "login error" }, | ||
237 | { MAIL_ERROR_CREATE, "create error" }, | ||
238 | { MAIL_ERROR_DELETE, /* 10 */ "delete error" }, | ||
239 | { MAIL_ERROR_LOGOUT, "logout error" }, | ||
240 | { MAIL_ERROR_NOOP, "noop error" }, | ||
241 | { MAIL_ERROR_RENAME, "rename error" }, | ||
242 | { MAIL_ERROR_CHECK, "check error" }, | ||
243 | { MAIL_ERROR_EXAMINE, "examine error" }, | ||
244 | { MAIL_ERROR_SELECT, "select error - folder does not exist" }, | ||
245 | { MAIL_ERROR_MEMORY, "not enough memory" }, | ||
246 | { MAIL_ERROR_STATUS, "status error" }, | ||
247 | { MAIL_ERROR_SUBSCRIBE, "subscribe error" }, | ||
248 | { MAIL_ERROR_UNSUBSCRIBE, /* 20 */ "unsubscribe error" }, | ||
249 | { MAIL_ERROR_LIST, "list error" }, | ||
250 | { MAIL_ERROR_LSUB, "lsub error" }, | ||
251 | { MAIL_ERROR_APPEND, "append error - mail could not be appended" }, | ||
252 | { MAIL_ERROR_COPY, "copy error" }, | ||
253 | { MAIL_ERROR_FETCH, "fetch error" }, | ||
254 | { MAIL_ERROR_STORE, "store error" }, | ||
255 | { MAIL_ERROR_SEARCH, "search error" }, | ||
256 | { MAIL_ERROR_DISKSPACE, " error: not enough diskspace" }, | ||
257 | { MAIL_ERROR_MSG_NOT_FOUND, "message not found" }, | ||
258 | { MAIL_ERROR_PARSE, /* 30 */ "parse error" }, | ||
259 | { MAIL_ERROR_INVAL, "invalid parameter for the function" }, | ||
260 | { MAIL_ERROR_PART_NOT_FOUND, "mime part of the message is not found" }, | ||
261 | { MAIL_ERROR_REMOVE, "remove error - the message did not exist" }, | ||
262 | { MAIL_ERROR_FOLDER_NOT_FOUND, "folder not found" }, | ||
263 | { MAIL_ERROR_MOVE, "move error" }, | ||
264 | { MAIL_ERROR_STARTTLS, "starttls error" }, | ||
265 | { MAIL_ERROR_CACHE_MISS, "mail cache missed" }, | ||
266 | { MAIL_ERROR_NO_TLS, "no starttls" }, | ||
267 | { MAIL_ERROR_EXPUNGE, "expunge error" }, | ||
268 | { MAIL_ERROR_PROTOCOL, "protocol error - server did not respect the protocol" }, | ||
269 | { MAIL_ERROR_CAPABILITY, "capability error" }, | ||
270 | { MAIL_ERROR_CLOSE, "close error" }, | ||
271 | { MAIL_ERROR_FATAL, "fatal error" }, | ||
272 | { MAIL_ERROR_READONLY, "mailbox is readonly" }, | ||
273 | { MAIL_ERROR_NO_APOP, "pop3 error - no apop" }, | ||
274 | { MAIL_ERROR_COMMAND_NOT_SUPPORTED, "nntp error - command not supported" }, | ||
275 | { MAIL_ERROR_NO_PERMISSION, "nntp error - no permission" }, | ||
276 | { MAIL_ERROR_PROGRAM_ERROR, "nntp error - program error" }, | ||
277 | { MAIL_ERROR_SUBJECT_NOT_FOUND, "internal threading error - subject not found" }}; | ||
278 | |||
279 | const char * maildriver_strerror(int err) | ||
280 | { | ||
281 | int count; | ||
282 | int i; | ||
283 | |||
284 | count = sizeof(message_tab) / sizeof(struct error_message); | ||
285 | |||
286 | for(i = 0 ; i < count ; i++) { | ||
287 | if (message_tab[i].code == err) { | ||
288 | return message_tab[i].message; | ||
289 | } | ||
290 | } | ||
291 | |||
292 | return "unknown error"; | ||
293 | } | ||
294 | |||
295 | |||
296 | struct mail_flags * mail_flags_new_empty(void) | ||
297 | { | ||
298 | struct mail_flags * flags; | ||
299 | |||
300 | flags = malloc(sizeof(* flags)); | ||
301 | if (flags == NULL) | ||
302 | goto err; | ||
303 | |||
304 | flags->fl_flags = MAIL_FLAG_NEW; | ||
305 | flags->fl_extension = clist_new(); | ||
306 | if (flags->fl_extension == NULL) | ||
307 | goto free; | ||
308 | |||
309 | return flags; | ||
310 | |||
311 | free: | ||
312 | free(flags); | ||
313 | err: | ||
314 | return NULL; | ||
315 | } | ||
316 | |||
317 | struct mail_flags * mail_flags_new(uint32_t fl_flags, clist * fl_extension) | ||
318 | { | ||
319 | struct mail_flags * flags; | ||
320 | |||
321 | flags = malloc(sizeof(* flags)); | ||
322 | if (flags == NULL) | ||
323 | goto err; | ||
324 | |||
325 | flags->fl_flags = fl_flags; | ||
326 | flags->fl_extension = fl_extension; | ||
327 | |||
328 | return flags; | ||
329 | |||
330 | err: | ||
331 | return NULL; | ||
332 | } | ||
333 | |||
334 | void mail_flags_free(struct mail_flags * flags) | ||
335 | { | ||
336 | clist_foreach(flags->fl_extension, (clist_func) free, NULL); | ||
337 | clist_free(flags->fl_extension); | ||
338 | free(flags); | ||
339 | } | ||
340 | |||
diff --git a/libetpan/src/driver/interface/maildriver_types.h b/libetpan/src/driver/interface/maildriver_types.h new file mode 100644 index 0000000..2225236 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_types.h | |||
@@ -0,0 +1,795 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILDRIVER_TYPES_H | ||
37 | |||
38 | #define MAILDRIVER_TYPES_H | ||
39 | |||
40 | #include <inttypes.h> | ||
41 | #include <sys/types.h> | ||
42 | |||
43 | #include <libetpan/mailstream.h> | ||
44 | #include <libetpan/mailimf.h> | ||
45 | #include <libetpan/mailmime.h> | ||
46 | #include <libetpan/carray.h> | ||
47 | |||
48 | #include <libetpan/mailthread_types.h> | ||
49 | #include <libetpan/maildriver_errors.h> | ||
50 | |||
51 | #ifdef __cplusplus | ||
52 | extern "C" { | ||
53 | #endif | ||
54 | |||
55 | typedef struct mailsession_driver mailsession_driver; | ||
56 | |||
57 | typedef struct mailsession mailsession; | ||
58 | |||
59 | typedef struct mailmessage_driver mailmessage_driver; | ||
60 | |||
61 | typedef struct mailmessage mailmessage; | ||
62 | |||
63 | |||
64 | /* | ||
65 | mailmessage_list is a list of mailmessage | ||
66 | |||
67 | - tab is an array of mailmessage structures | ||
68 | */ | ||
69 | |||
70 | struct mailmessage_list { | ||
71 | carray * msg_tab; /* elements are (mailmessage *) */ | ||
72 | }; | ||
73 | |||
74 | struct mailmessage_list * mailmessage_list_new(carray * msg_tab); | ||
75 | void mailmessage_list_free(struct mailmessage_list * env_list); | ||
76 | |||
77 | /* | ||
78 | mail_list is a list of mailbox names | ||
79 | |||
80 | - list is a list of mailbox names | ||
81 | */ | ||
82 | |||
83 | struct mail_list { | ||
84 | clist * mb_list; /* elements are (char *) */ | ||
85 | }; | ||
86 | |||
87 | struct mail_list * mail_list_new(clist * mb_list); | ||
88 | void mail_list_free(struct mail_list * resp); | ||
89 | |||
90 | /* | ||
91 | This is a flag value. | ||
92 | Flags can be combined with OR operation | ||
93 | */ | ||
94 | |||
95 | enum { | ||
96 | MAIL_FLAG_NEW = 1 << 0, | ||
97 | MAIL_FLAG_SEEN = 1 << 1, | ||
98 | MAIL_FLAG_FLAGGED = 1 << 2, | ||
99 | MAIL_FLAG_DELETED = 1 << 3, | ||
100 | MAIL_FLAG_ANSWERED = 1 << 4, | ||
101 | MAIL_FLAG_FORWARDED = 1 << 5, | ||
102 | MAIL_FLAG_CANCELLED = 1 << 6, | ||
103 | }; | ||
104 | |||
105 | /* | ||
106 | mail_flags is the value of a flag related to a message. | ||
107 | |||
108 | - flags is the standard flags value | ||
109 | |||
110 | - extension is a list of unknown flags for libEtPan! | ||
111 | */ | ||
112 | |||
113 | struct mail_flags { | ||
114 | uint32_t fl_flags; | ||
115 | clist * fl_extension; /* elements are (char *) */ | ||
116 | }; | ||
117 | |||
118 | struct mail_flags * mail_flags_new(uint32_t fl_flags, clist * fl_ext); | ||
119 | void mail_flags_free(struct mail_flags * flags); | ||
120 | |||
121 | /* | ||
122 | This function creates a flag for a new message | ||
123 | */ | ||
124 | |||
125 | struct mail_flags * mail_flags_new_empty(void); | ||
126 | |||
127 | |||
128 | /* | ||
129 | mailimf_date_time_comp compares two dates | ||
130 | |||
131 | |||
132 | */ | ||
133 | |||
134 | int32_t mailimf_date_time_comp(struct mailimf_date_time * date1, | ||
135 | struct mailimf_date_time * date2); | ||
136 | |||
137 | /* | ||
138 | this is type type of the search criteria | ||
139 | */ | ||
140 | |||
141 | enum { | ||
142 | MAIL_SEARCH_KEY_ALL, /* all messages correspond */ | ||
143 | MAIL_SEARCH_KEY_ANSWERED, /* messages with flag \Answered */ | ||
144 | MAIL_SEARCH_KEY_BCC, /* messages which Bcc field contains | ||
145 | a given string */ | ||
146 | MAIL_SEARCH_KEY_BEFORE, /* messages which internal date is earlier | ||
147 | than the specified date */ | ||
148 | MAIL_SEARCH_KEY_BODY, /* message that contains the given string | ||
149 | (in header and text parts) */ | ||
150 | MAIL_SEARCH_KEY_CC, /* messages whose Cc field contains the | ||
151 | given string */ | ||
152 | MAIL_SEARCH_KEY_DELETED, /* messages with the flag \Deleted */ | ||
153 | MAIL_SEARCH_KEY_FLAGGED, /* messages with the flag \Flagged */ | ||
154 | MAIL_SEARCH_KEY_FROM, /* messages whose From field contains the | ||
155 | given string */ | ||
156 | MAIL_SEARCH_KEY_NEW, /* messages with the flag \Recent and not | ||
157 | the \Seen flag */ | ||
158 | MAIL_SEARCH_KEY_OLD, /* messages that do not have the | ||
159 | \Recent flag set */ | ||
160 | MAIL_SEARCH_KEY_ON, /* messages whose internal date is the | ||
161 | specified date */ | ||
162 | MAIL_SEARCH_KEY_RECENT, /* messages with the flag \Recent */ | ||
163 | MAIL_SEARCH_KEY_SEEN, /* messages with the flag \Seen */ | ||
164 | MAIL_SEARCH_KEY_SINCE, /* messages whose internal date is later | ||
165 | than specified date */ | ||
166 | MAIL_SEARCH_KEY_SUBJECT, /* messages whose Subject field contains the | ||
167 | given string */ | ||
168 | MAIL_SEARCH_KEY_TEXT, /* messages whose text part contains the | ||
169 | given string */ | ||
170 | MAIL_SEARCH_KEY_TO, /* messages whose To field contains the | ||
171 | given string */ | ||
172 | MAIL_SEARCH_KEY_UNANSWERED, /* messages with no flag \Answered */ | ||
173 | MAIL_SEARCH_KEY_UNDELETED, /* messages with no flag \Deleted */ | ||
174 | MAIL_SEARCH_KEY_UNFLAGGED, /* messages with no flag \Flagged */ | ||
175 | MAIL_SEARCH_KEY_UNSEEN, /* messages with no flag \Seen */ | ||
176 | MAIL_SEARCH_KEY_HEADER, /* messages whose given field | ||
177 | contains the given string */ | ||
178 | MAIL_SEARCH_KEY_LARGER, /* messages whose size is larger then | ||
179 | the given size */ | ||
180 | MAIL_SEARCH_KEY_NOT, /* not operation of the condition */ | ||
181 | MAIL_SEARCH_KEY_OR, /* or operation between two conditions */ | ||
182 | MAIL_SEARCH_KEY_SMALLER, /* messages whose size is smaller than | ||
183 | the given size */ | ||
184 | MAIL_SEARCH_KEY_MULTIPLE /* the boolean operator between the | ||
185 | conditions is AND */ | ||
186 | }; | ||
187 | |||
188 | /* | ||
189 | mail_search_key is the condition on the messages to return | ||
190 | |||
191 | - type is the type of the condition | ||
192 | |||
193 | - bcc is the text to search in the Bcc field when type is | ||
194 | MAIL_SEARCH_KEY_BCC, should be allocated with malloc() | ||
195 | |||
196 | - before is a date when type is MAIL_SEARCH_KEY_BEFORE | ||
197 | |||
198 | - body is the text to search in the message when type is | ||
199 | MAIL_SEARCH_KEY_BODY, should be allocated with malloc() | ||
200 | |||
201 | - cc is the text to search in the Cc field when type is | ||
202 | MAIL_SEARCH_KEY_CC, should be allocated with malloc() | ||
203 | |||
204 | - from is the text to search in the From field when type is | ||
205 | MAIL_SEARCH_KEY_FROM, should be allocated with malloc() | ||
206 | |||
207 | - on is a date when type is MAIL_SEARCH_KEY_ON | ||
208 | |||
209 | - since is a date when type is MAIL_SEARCH_KEY_SINCE | ||
210 | |||
211 | - subject is the text to search in the Subject field when type is | ||
212 | MAILIMAP_SEARCH_KEY_SUBJECT, should be allocated with malloc() | ||
213 | |||
214 | - text is the text to search in the text part of the message when | ||
215 | type is MAILIMAP_SEARCH_KEY_TEXT, should be allocated with malloc() | ||
216 | |||
217 | - to is the text to search in the To field when type is | ||
218 | MAILIMAP_SEARCH_KEY_TO, should be allocated with malloc() | ||
219 | |||
220 | - header_name is the header name when type is MAILIMAP_SEARCH_KEY_HEADER, | ||
221 | should be allocated with malloc() | ||
222 | |||
223 | - header_value is the text to search in the given header when type is | ||
224 | MAILIMAP_SEARCH_KEY_HEADER, should be allocated with malloc() | ||
225 | |||
226 | - larger is a size when type is MAILIMAP_SEARCH_KEY_LARGER | ||
227 | |||
228 | - not is a condition when type is MAILIMAP_SEARCH_KEY_NOT | ||
229 | |||
230 | - or1 is a condition when type is MAILIMAP_SEARCH_KEY_OR | ||
231 | |||
232 | - or2 is a condition when type is MAILIMAP_SEARCH_KEY_OR | ||
233 | |||
234 | - sentbefore is a date when type is MAILIMAP_SEARCH_KEY_SENTBEFORE | ||
235 | |||
236 | - senton is a date when type is MAILIMAP_SEARCH_KEY_SENTON | ||
237 | |||
238 | - sentsince is a date when type is MAILIMAP_SEARCH_KEY_SENTSINCE | ||
239 | |||
240 | - smaller is a size when type is MAILIMAP_SEARCH_KEY_SMALLER | ||
241 | |||
242 | - multiple is a set of message when type is MAILIMAP_SEARCH_KEY_MULTIPLE | ||
243 | */ | ||
244 | |||
245 | #if 0 | ||
246 | struct mail_search_key { | ||
247 | int sk_type; | ||
248 | union { | ||
249 | char * sk_bcc; | ||
250 | struct mailimf_date_time * sk_before; | ||
251 | char * sk_body; | ||
252 | char * sk_cc; | ||
253 | char * sk_from; | ||
254 | struct mailimf_date_time * sk_on; | ||
255 | struct mailimf_date_time * sk_since; | ||
256 | char * sk_subject; | ||
257 | char * sk_text; | ||
258 | char * sk_to; | ||
259 | char * sk_header_name; | ||
260 | char * sk_header_value; | ||
261 | size_t sk_larger; | ||
262 | struct mail_search_key * sk_not; | ||
263 | struct mail_search_key * sk_or1; | ||
264 | struct mail_search_key * sk_or2; | ||
265 | size_t sk_smaller; | ||
266 | clist * sk_multiple; /* list of (struct mailimap_search_key *) */ | ||
267 | } sk_data; | ||
268 | }; | ||
269 | |||
270 | |||
271 | struct mail_search_key * | ||
272 | mail_search_key_new(int sk_type, | ||
273 | char * sk_bcc, struct mailimf_date_time * sk_before, | ||
274 | char * sk_body, char * sk_cc, char * sk_from, | ||
275 | struct mailimf_date_time * sk_on, struct mailimf_date_time * sk_since, | ||
276 | char * sk_subject, char * sk_text, char * sk_to, | ||
277 | char * sk_header_name, char * sk_header_value, size_t sk_larger, | ||
278 | struct mail_search_key * sk_not, struct mail_search_key * sk_or1, | ||
279 | struct mail_search_key * sk_or2, size_t sk_smaller, | ||
280 | clist * sk_multiple); | ||
281 | |||
282 | void mail_search_key_free(struct mail_search_key * key); | ||
283 | #endif | ||
284 | |||
285 | /* | ||
286 | mail_search_result is a list of message numbers that is returned | ||
287 | by the mailsession_search_messages function() | ||
288 | */ | ||
289 | |||
290 | #if 0 | ||
291 | struct mail_search_result { | ||
292 | clist * sr_list; /* list of (uint32_t *) */ | ||
293 | }; | ||
294 | |||
295 | struct mail_search_result * mail_search_result_new(clist * sr_list); | ||
296 | |||
297 | void mail_search_result_free(struct mail_search_result * search_result); | ||
298 | #endif | ||
299 | |||
300 | |||
301 | /* | ||
302 | There is three kinds of identities : | ||
303 | - storage | ||
304 | - folders | ||
305 | - session | ||
306 | |||
307 | A storage (struct mailstorage) represents whether a server or | ||
308 | a main path, | ||
309 | |||
310 | A storage can be an IMAP server, the root path of a MH or a mbox file. | ||
311 | |||
312 | Folders (struct mailfolder) are the mailboxes we can | ||
313 | choose in the server or as sub-folder of the main path. | ||
314 | |||
315 | Folders for IMAP are the IMAP mailboxes, for MH this is one of the | ||
316 | folder of the MH storage, for mbox, there is only one folder, the | ||
317 | mbox file content; | ||
318 | |||
319 | A mail session (struct mailsession) is whether a connection to a server | ||
320 | or a path that is open. It is the abstraction lower folders and storage. | ||
321 | It allow us to send commands. | ||
322 | |||
323 | We have a session driver for mail session for each kind of storage. | ||
324 | |||
325 | From a session, we can get a message (struct mailmessage) to read. | ||
326 | We have a message driver for each kind of storage. | ||
327 | */ | ||
328 | |||
329 | /* | ||
330 | maildriver is the driver structure for mail sessions | ||
331 | |||
332 | - name is the name of the driver | ||
333 | |||
334 | - initialize() is the function that will initializes a data structure | ||
335 | specific to the driver, it returns a value that will be stored | ||
336 | in the field data of the session. | ||
337 | The field data of the session is the state of the session, | ||
338 | the internal data structure used by the driver. | ||
339 | It is called when creating the mailsession structure with | ||
340 | mailsession_new(). | ||
341 | |||
342 | - uninitialize() frees the structure created with initialize() | ||
343 | |||
344 | - parameters() implements functions specific to the given mail access | ||
345 | |||
346 | - connect_stream() connects a stream to the session | ||
347 | |||
348 | - connect_path() notify a main path to the session | ||
349 | |||
350 | - starttls() changes the current stream to a TLS stream | ||
351 | |||
352 | - login() notifies the user and the password to authenticate to the | ||
353 | session | ||
354 | |||
355 | - logout() exits the session and closes the stream | ||
356 | |||
357 | - noop() does no operation on the session, but it can be | ||
358 | used to poll for the status of the connection. | ||
359 | |||
360 | - build_folder_name() will return an allocated string with | ||
361 | that contains the complete path of the folder to create | ||
362 | |||
363 | - create_folder() creates the folder that corresponds to the | ||
364 | given name | ||
365 | |||
366 | - delete_folder() deletes the folder that corresponds to the | ||
367 | given name | ||
368 | |||
369 | - rename_folder() change the name of the folder | ||
370 | |||
371 | - check_folder() makes a checkpoint of the session | ||
372 | |||
373 | - examine_folder() selects a mailbox as readonly | ||
374 | |||
375 | - select_folder() selects a mailbox | ||
376 | |||
377 | - expunge_folder() deletes all messages marked \Deleted | ||
378 | |||
379 | - status_folder() queries the status of the folder | ||
380 | (number of messages, number of recent messages, number of | ||
381 | unseen messages) | ||
382 | |||
383 | - messages_number() queries the number of messages in the folder | ||
384 | |||
385 | - recent_number() queries the number of recent messages in the folder | ||
386 | |||
387 | - unseen_number() queries the number of unseen messages in the folder | ||
388 | |||
389 | - list_folders() returns the list of all sub-mailboxes | ||
390 | of the given mailbox | ||
391 | |||
392 | - lsub_folders() returns the list of subscribed | ||
393 | sub-mailboxes of the given mailbox | ||
394 | |||
395 | - subscribe_folder() subscribes to the given mailbox | ||
396 | |||
397 | - unsubscribe_folder() unsubscribes to the given mailbox | ||
398 | |||
399 | - append_message() adds a RFC 2822 message to the current | ||
400 | given mailbox | ||
401 | |||
402 | - copy_message() copies a message whose number is given to | ||
403 | a given mailbox. The mailbox must be accessible from | ||
404 | the same session. | ||
405 | |||
406 | - move_message() copies a message whose number is given to | ||
407 | a given mailbox. The mailbox must be accessible from the | ||
408 | same session. | ||
409 | |||
410 | - get_messages_list() returns the list of message numbers | ||
411 | of the current mailbox. | ||
412 | |||
413 | - get_envelopes_list() fills the parsed fields in the | ||
414 | mailmessage structures of the mailmessage_list. | ||
415 | |||
416 | - remove_message() removes the given message from the mailbox. | ||
417 | The message is permanently deleted. | ||
418 | |||
419 | - search_message() returns a list of message numbers that | ||
420 | corresponds to the given criteria. | ||
421 | |||
422 | - get_message returns a mailmessage structure that corresponds | ||
423 | to the given message number. | ||
424 | |||
425 | - get_message_by_uid returns a mailmessage structure that corresponds | ||
426 | to the given message unique identifier. | ||
427 | |||
428 | * mandatory functions are the following : | ||
429 | |||
430 | - connect_stream() of connect_path() | ||
431 | - logout() | ||
432 | - get_messages_list() | ||
433 | - get_envelopes_list() | ||
434 | |||
435 | * we advise you to implement these functions : | ||
436 | |||
437 | - select_folder() (in case a session can access several folders) | ||
438 | - noop() (to check if the server is responding) | ||
439 | - check_folder() (to make a checkpoint of the session) | ||
440 | - status_folder(), messages_number(), recent_number(), unseen_number() | ||
441 | (to get stat of the folder) | ||
442 | - append_message() (but can't be done in the case of POP3 at least) | ||
443 | - login() in a case of an authenticated driver. | ||
444 | - starttls() in a case of a stream driver, if the procotol supports | ||
445 | STARTTLS. | ||
446 | - get_message_by_uid() so that the application can remember the message | ||
447 | by UID and build its own list of messages. | ||
448 | |||
449 | * drivers' specific : | ||
450 | |||
451 | Everything that is specific to the driver will be implemented in this | ||
452 | function : | ||
453 | |||
454 | - parameters() | ||
455 | */ | ||
456 | |||
457 | struct mailsession_driver { | ||
458 | char * sess_name; | ||
459 | |||
460 | int (* sess_initialize)(mailsession * session); | ||
461 | void (* sess_uninitialize)(mailsession * session); | ||
462 | |||
463 | int (* sess_parameters)(mailsession * session, | ||
464 | int id, void * value); | ||
465 | |||
466 | int (* sess_connect_stream)(mailsession * session, mailstream * s); | ||
467 | int (* sess_connect_path)(mailsession * session, char * path); | ||
468 | |||
469 | int (* sess_starttls)(mailsession * session); | ||
470 | |||
471 | int (* sess_login)(mailsession * session, char * userid, char * password); | ||
472 | int (* sess_logout)(mailsession * session); | ||
473 | int (* sess_noop)(mailsession * session); | ||
474 | |||
475 | /* folders operations */ | ||
476 | |||
477 | int (* sess_build_folder_name)(mailsession * session, char * mb, | ||
478 | char * name, char ** result); | ||
479 | |||
480 | int (* sess_create_folder)(mailsession * session, char * mb); | ||
481 | int (* sess_delete_folder)(mailsession * session, char * mb); | ||
482 | int (* sess_rename_folder)(mailsession * session, char * mb, | ||
483 | char * new_name); | ||
484 | int (* sess_check_folder)(mailsession * session); | ||
485 | int (* sess_examine_folder)(mailsession * session, char * mb); | ||
486 | int (* sess_select_folder)(mailsession * session, char * mb); | ||
487 | int (* sess_expunge_folder)(mailsession * session); | ||
488 | int (* sess_status_folder)(mailsession * session, char * mb, | ||
489 | uint32_t * result_num, uint32_t * result_recent, | ||
490 | uint32_t * result_unseen); | ||
491 | int (* sess_messages_number)(mailsession * session, char * mb, | ||
492 | uint32_t * result); | ||
493 | int (* sess_recent_number)(mailsession * session, char * mb, | ||
494 | uint32_t * result); | ||
495 | int (* sess_unseen_number)(mailsession * session, char * mb, | ||
496 | uint32_t * result); | ||
497 | |||
498 | int (* sess_list_folders)(mailsession * session, char * mb, | ||
499 | struct mail_list ** result); | ||
500 | int (* sess_lsub_folders)(mailsession * session, char * mb, | ||
501 | struct mail_list ** result); | ||
502 | |||
503 | int (* sess_subscribe_folder)(mailsession * session, char * mb); | ||
504 | int (* sess_unsubscribe_folder)(mailsession * session, char * mb); | ||
505 | |||
506 | /* messages operations */ | ||
507 | |||
508 | int (* sess_append_message)(mailsession * session, | ||
509 | char * message, size_t size); | ||
510 | int (* sess_append_message_flags)(mailsession * session, | ||
511 | char * message, size_t size, struct mail_flags * flags); | ||
512 | int (* sess_copy_message)(mailsession * session, | ||
513 | uint32_t num, char * mb); | ||
514 | int (* sess_move_message)(mailsession * session, | ||
515 | uint32_t num, char * mb); | ||
516 | |||
517 | int (* sess_get_message)(mailsession * session, | ||
518 | uint32_t num, mailmessage ** result); | ||
519 | |||
520 | int (* sess_get_message_by_uid)(mailsession * session, | ||
521 | const char * uid, mailmessage ** result); | ||
522 | |||
523 | int (* sess_get_messages_list)(mailsession * session, | ||
524 | struct mailmessage_list ** result); | ||
525 | int (* sess_get_envelopes_list)(mailsession * session, | ||
526 | struct mailmessage_list * env_list); | ||
527 | int (* sess_remove_message)(mailsession * session, uint32_t num); | ||
528 | #if 0 | ||
529 | int (* sess_search_messages)(mailsession * session, char * charset, | ||
530 | struct mail_search_key * key, | ||
531 | struct mail_search_result ** result); | ||
532 | #endif | ||
533 | }; | ||
534 | |||
535 | |||
536 | /* | ||
537 | session is the data structure for a mail session. | ||
538 | |||
539 | - data is the internal data structure used by the driver | ||
540 | It is called when initializing the mailsession structure. | ||
541 | |||
542 | - driver is the driver used for the session | ||
543 | */ | ||
544 | |||
545 | struct mailsession { | ||
546 | void * sess_data; | ||
547 | mailsession_driver * sess_driver; | ||
548 | }; | ||
549 | |||
550 | |||
551 | |||
552 | |||
553 | /* | ||
554 | mailmessage_driver is the driver structure to get information from messages. | ||
555 | |||
556 | - name is the name of the driver | ||
557 | |||
558 | - initialize() is the function that will initializes a data structure | ||
559 | specific to the driver, it returns a value that will be stored | ||
560 | in the field data of the mailsession. | ||
561 | The field data of the session is the state of the session, | ||
562 | the internal data structure used by the driver. | ||
563 | It is called when initializing the mailmessage structure with | ||
564 | mailmessage_init(). | ||
565 | |||
566 | - uninitialize() frees the structure created with initialize(). | ||
567 | It will be called by mailmessage_free(). | ||
568 | |||
569 | - flush() will free from memory all temporary structures of the message | ||
570 | (for example, the MIME structure of the message). | ||
571 | |||
572 | - fetch_result_free() will free all strings resulted by fetch() or | ||
573 | any fetch_xxx() functions that returns a string. | ||
574 | |||
575 | - fetch() returns the content of the message (headers and text). | ||
576 | |||
577 | - fetch_header() returns the content of the headers. | ||
578 | |||
579 | - fetch_body() returns the message text (message content without headers) | ||
580 | |||
581 | - fetch_size() returns the size of the message content. | ||
582 | |||
583 | - get_bodystructure() returns the MIME structure of the message. | ||
584 | |||
585 | - fetch_section() returns the content of a given MIME part | ||
586 | |||
587 | - fetch_section_header() returns the header of the message | ||
588 | contained by the given MIME part. | ||
589 | |||
590 | - fetch_section_mime() returns the MIME headers of the | ||
591 | given MIME part. | ||
592 | |||
593 | - fetch_section_body() returns the text (if this is a message, this is the | ||
594 | message content without headers) of the given MIME part. | ||
595 | |||
596 | - fetch_envelope() returns a mailimf_fields structure, with a list of | ||
597 | fields chosen by the driver. | ||
598 | |||
599 | - get_flags() returns a the flags related to the message. | ||
600 | When you want to get flags of a message, you have to make sure to | ||
601 | call get_flags() at least once before using directly message->flags. | ||
602 | */ | ||
603 | |||
604 | #define LIBETPAN_MAIL_MESSAGE_CHECK | ||
605 | |||
606 | struct mailmessage_driver { | ||
607 | char * msg_name; | ||
608 | |||
609 | int (* msg_initialize)(mailmessage * msg_info); | ||
610 | |||
611 | void (* msg_uninitialize)(mailmessage * msg_info); | ||
612 | |||
613 | void (* msg_flush)(mailmessage * msg_info); | ||
614 | |||
615 | void (* msg_check)(mailmessage * msg_info); | ||
616 | |||
617 | void (* msg_fetch_result_free)(mailmessage * msg_info, | ||
618 | char * msg); | ||
619 | |||
620 | int (* msg_fetch)(mailmessage * msg_info, | ||
621 | char ** result, | ||
622 | size_t * result_len); | ||
623 | |||
624 | int (* msg_fetch_header)(mailmessage * msg_info, | ||
625 | char ** result, | ||
626 | size_t * result_len); | ||
627 | |||
628 | int (* msg_fetch_body)(mailmessage * msg_info, | ||
629 | char ** result, size_t * result_len); | ||
630 | |||
631 | int (* msg_fetch_size)(mailmessage * msg_info, | ||
632 | size_t * result); | ||
633 | |||
634 | int (* msg_get_bodystructure)(mailmessage * msg_info, | ||
635 | struct mailmime ** result); | ||
636 | |||
637 | int (* msg_fetch_section)(mailmessage * msg_info, | ||
638 | struct mailmime * mime, | ||
639 | char ** result, size_t * result_len); | ||
640 | |||
641 | int (* msg_fetch_section_header)(mailmessage * msg_info, | ||
642 | struct mailmime * mime, | ||
643 | char ** result, | ||
644 | size_t * result_len); | ||
645 | |||
646 | int (* msg_fetch_section_mime)(mailmessage * msg_info, | ||
647 | struct mailmime * mime, | ||
648 | char ** result, | ||
649 | size_t * result_len); | ||
650 | |||
651 | int (* msg_fetch_section_body)(mailmessage * msg_info, | ||
652 | struct mailmime * mime, | ||
653 | char ** result, | ||
654 | size_t * result_len); | ||
655 | |||
656 | int (* msg_fetch_envelope)(mailmessage * msg_info, | ||
657 | struct mailimf_fields ** result); | ||
658 | |||
659 | int (* msg_get_flags)(mailmessage * msg_info, | ||
660 | struct mail_flags ** result); | ||
661 | }; | ||
662 | |||
663 | |||
664 | /* | ||
665 | mailmessage is a data structure to get information from messages | ||
666 | |||
667 | - session is the session linked to the given message, it can be NULL | ||
668 | |||
669 | - driver is the message driver | ||
670 | |||
671 | - index is the message number | ||
672 | |||
673 | - uid, when it is not NULL, it means that the folder | ||
674 | the folder has persistant message numbers, the string is | ||
675 | the unique message number in the folder. | ||
676 | uid should be implemented if possible. | ||
677 | for drivers where we cannot generate real uid, | ||
678 | a suggestion is "AAAA-IIII" where AAAA is some | ||
679 | random session number and IIII the content of index field. | ||
680 | |||
681 | - size, when it is not 0, is the size of the message content. | ||
682 | |||
683 | - fields, when it is not NULL, are the header fields of the message. | ||
684 | |||
685 | - flags, when it is not NULL, are the flags related to the message. | ||
686 | |||
687 | - single_fields, when resolved != 0, is filled with the data of fields. | ||
688 | |||
689 | - mime, when it is not NULL | ||
690 | |||
691 | - cached is != 0 when the header fields were read from the cache. | ||
692 | |||
693 | - data is data specific to the driver, this is internal data structure, | ||
694 | some state of the message. | ||
695 | */ | ||
696 | |||
697 | struct mailmessage { | ||
698 | mailsession * msg_session; | ||
699 | mailmessage_driver * msg_driver; | ||
700 | uint32_t msg_index; | ||
701 | char * msg_uid; | ||
702 | |||
703 | size_t msg_size; | ||
704 | struct mailimf_fields * msg_fields; | ||
705 | struct mail_flags * msg_flags; | ||
706 | |||
707 | int msg_resolved; | ||
708 | struct mailimf_single_fields msg_single_fields; | ||
709 | struct mailmime * msg_mime; | ||
710 | |||
711 | /* internal data */ | ||
712 | |||
713 | int msg_cached; | ||
714 | void * msg_data; | ||
715 | |||
716 | /* | ||
717 | msg_folder field : | ||
718 | used to reference the mailfolder, this is a workaround due | ||
719 | to the problem with initial conception, where folder notion | ||
720 | did not exist. | ||
721 | */ | ||
722 | void * msg_folder; | ||
723 | /* user data */ | ||
724 | void * msg_user_data; | ||
725 | }; | ||
726 | |||
727 | |||
728 | /* | ||
729 | mailmessage_tree is a node in the messages tree (thread) | ||
730 | |||
731 | - parent is the parent of the message, it is NULL if the message | ||
732 | is the root of the message tree. | ||
733 | |||
734 | - date is the date of the message in number of second elapsed | ||
735 | since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC). | ||
736 | |||
737 | - msg is the message structure that is stored referenced by the node. | ||
738 | is msg is NULL, this is a dummy node. | ||
739 | |||
740 | - children is an array that contains all the children of the node. | ||
741 | children are mailmessage_tree structures. | ||
742 | |||
743 | - is_reply is != 0 when the message is a reply or a forward | ||
744 | |||
745 | - base_subject is the extracted subject of the message. | ||
746 | |||
747 | - index is the message number. | ||
748 | */ | ||
749 | |||
750 | struct mailmessage_tree { | ||
751 | struct mailmessage_tree * node_parent; | ||
752 | char * node_msgid; | ||
753 | time_t node_date; | ||
754 | mailmessage * node_msg; | ||
755 | carray * node_children; /* array of (struct mailmessage_tree *) */ | ||
756 | |||
757 | /* private, used for threading */ | ||
758 | int node_is_reply; | ||
759 | char * node_base_subject; | ||
760 | }; | ||
761 | |||
762 | |||
763 | struct mailmessage_tree * | ||
764 | mailmessage_tree_new(char * node_msgid, time_t node_date, | ||
765 | mailmessage * node_msg); | ||
766 | |||
767 | void mailmessage_tree_free(struct mailmessage_tree * tree); | ||
768 | |||
769 | /* | ||
770 | mailmessage_tree_free_recursive | ||
771 | |||
772 | if you want to release memory of the given tree and all the sub-trees, | ||
773 | you can use this function. | ||
774 | */ | ||
775 | |||
776 | void mailmessage_tree_free_recursive(struct mailmessage_tree * tree); | ||
777 | |||
778 | |||
779 | struct generic_message_t { | ||
780 | int (* msg_prefetch)(mailmessage * msg_info); | ||
781 | void (* msg_prefetch_free)(struct generic_message_t * msg); | ||
782 | int msg_fetched; | ||
783 | char * msg_message; | ||
784 | size_t msg_length; | ||
785 | void * msg_data; | ||
786 | }; | ||
787 | |||
788 | |||
789 | const char * maildriver_strerror(int err); | ||
790 | |||
791 | #ifdef __cplusplus | ||
792 | } | ||
793 | #endif | ||
794 | |||
795 | #endif | ||
diff --git a/libetpan/src/driver/interface/maildriver_types_helper.c b/libetpan/src/driver/interface/maildriver_types_helper.c new file mode 100644 index 0000000..6e3abf4 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_types_helper.c | |||
@@ -0,0 +1,104 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 200 - 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 AUTHORS 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 AUTHORS 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 "maildriver_types_helper.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | |||
40 | #include "clist.h" | ||
41 | #include <string.h> | ||
42 | #include <stdlib.h> | ||
43 | |||
44 | int mail_flags_add_extension(struct mail_flags * flags, | ||
45 | char * ext_flag) | ||
46 | { | ||
47 | char * str; | ||
48 | int r; | ||
49 | |||
50 | if (mail_flags_has_extension(flags, ext_flag)) | ||
51 | return MAIL_NO_ERROR; | ||
52 | |||
53 | str = strdup(ext_flag); | ||
54 | if (str == NULL) | ||
55 | return MAIL_ERROR_MEMORY; | ||
56 | |||
57 | r = clist_append(flags->fl_extension, str); | ||
58 | if (r < 0) { | ||
59 | free(str); | ||
60 | return MAIL_ERROR_MEMORY; | ||
61 | } | ||
62 | |||
63 | return MAIL_NO_ERROR; | ||
64 | } | ||
65 | |||
66 | int mail_flags_remove_extension(struct mail_flags * flags, | ||
67 | char * ext_flag) | ||
68 | { | ||
69 | clistiter * cur; | ||
70 | |||
71 | cur = clist_begin(flags->fl_extension); | ||
72 | while (cur != NULL) { | ||
73 | char * flag_name; | ||
74 | |||
75 | flag_name = clist_content(cur); | ||
76 | |||
77 | if (strcasecmp(flag_name, ext_flag) == 0) { | ||
78 | free(flag_name); | ||
79 | cur = clist_delete(flags->fl_extension, cur); | ||
80 | } | ||
81 | else | ||
82 | cur = clist_next(cur); | ||
83 | } | ||
84 | |||
85 | return MAIL_NO_ERROR; | ||
86 | } | ||
87 | |||
88 | int mail_flags_has_extension(struct mail_flags * flags, | ||
89 | char * ext_flag) | ||
90 | { | ||
91 | clistiter * cur; | ||
92 | |||
93 | for(cur = clist_begin(flags->fl_extension) ; cur != NULL ; | ||
94 | cur = clist_next(cur)) { | ||
95 | char * flag_name; | ||
96 | |||
97 | flag_name = clist_content(cur); | ||
98 | |||
99 | if (strcasecmp(flag_name, ext_flag) == 0) | ||
100 | return TRUE; | ||
101 | } | ||
102 | |||
103 | return FALSE; | ||
104 | } | ||
diff --git a/libetpan/src/driver/interface/maildriver_types_helper.h b/libetpan/src/driver/interface/maildriver_types_helper.h new file mode 100644 index 0000000..50ccc70 --- a/dev/null +++ b/libetpan/src/driver/interface/maildriver_types_helper.h | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILDRIVER_TYPES_HELPER_H | ||
37 | |||
38 | #define MAILDRIVER_TYPES_HELPER_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* | ||
47 | mail_flags_add_extension adds the given flag if it does not exists in | ||
48 | the flags. | ||
49 | |||
50 | @param flags this is the flag to change | ||
51 | |||
52 | @param ext_flag this is the name of an extension flag | ||
53 | the given flag name is duplicated and is no more needed after | ||
54 | the function call. | ||
55 | |||
56 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
57 | on error | ||
58 | */ | ||
59 | |||
60 | int mail_flags_add_extension(struct mail_flags * flags, | ||
61 | char * ext_flag); | ||
62 | |||
63 | /* | ||
64 | mail_flags_remove_extension removes the given flag if it does not exists in | ||
65 | the flags. | ||
66 | |||
67 | @param flags this is the flag to change | ||
68 | |||
69 | @param ext_flag this is the name of an extension flag | ||
70 | the given flag name is no more needed after the function call. | ||
71 | |||
72 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
73 | on error | ||
74 | */ | ||
75 | |||
76 | int mail_flags_remove_extension(struct mail_flags * flags, | ||
77 | char * ext_flag); | ||
78 | |||
79 | /* | ||
80 | mail_flags_has_extension returns 1 if the flags is in the given flags, | ||
81 | 0 is returned otherwise. | ||
82 | |||
83 | @param flags this is the flag to change | ||
84 | |||
85 | @param ext_flag this is the name of an extension flag | ||
86 | the given flag name is no more needed after the function call. | ||
87 | |||
88 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
89 | on error | ||
90 | */ | ||
91 | |||
92 | int mail_flags_has_extension(struct mail_flags * flags, | ||
93 | char * ext_flag); | ||
94 | |||
95 | #ifdef __cplusplus | ||
96 | } | ||
97 | #endif | ||
98 | |||
99 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailfolder.c b/libetpan/src/driver/interface/mailfolder.c new file mode 100644 index 0000000..eb69d7f --- a/dev/null +++ b/libetpan/src/driver/interface/mailfolder.c | |||
@@ -0,0 +1,138 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailfolder.h" | ||
37 | |||
38 | #include "maildriver.h" | ||
39 | |||
40 | int mailfolder_noop(struct mailfolder * folder) | ||
41 | { | ||
42 | return mailsession_noop(folder->fld_session); | ||
43 | } | ||
44 | |||
45 | int mailfolder_check(struct mailfolder * folder) | ||
46 | { | ||
47 | return mailsession_check_folder(folder->fld_session); | ||
48 | } | ||
49 | |||
50 | int mailfolder_expunge(struct mailfolder * folder) | ||
51 | { | ||
52 | return mailsession_expunge_folder(folder->fld_session); | ||
53 | } | ||
54 | |||
55 | int mailfolder_status(struct mailfolder * folder, | ||
56 | uint32_t * result_messages, uint32_t * result_recent, | ||
57 | uint32_t * result_unseen) | ||
58 | { | ||
59 | return mailsession_status_folder(folder->fld_session, | ||
60 | folder->fld_pathname, result_messages, | ||
61 | result_recent, result_unseen); | ||
62 | } | ||
63 | |||
64 | int mailfolder_append_message(struct mailfolder * folder, | ||
65 | char * message, size_t size) | ||
66 | { | ||
67 | return mailsession_append_message(folder->fld_session, message, size); | ||
68 | } | ||
69 | |||
70 | int mailfolder_append_message_flags(struct mailfolder * folder, | ||
71 | char * message, size_t size, struct mail_flags * flags) | ||
72 | { | ||
73 | return mailsession_append_message_flags(folder->fld_session, message, | ||
74 | size, flags); | ||
75 | } | ||
76 | |||
77 | int mailfolder_get_messages_list(struct mailfolder * folder, | ||
78 | struct mailmessage_list ** result) | ||
79 | { | ||
80 | int r; | ||
81 | struct mailmessage_list * msg_list; | ||
82 | unsigned int i; | ||
83 | |||
84 | r = mailsession_get_messages_list(folder->fld_session, &msg_list); | ||
85 | if (r != MAIL_NO_ERROR) | ||
86 | return r; | ||
87 | |||
88 | for(i = 0 ; i < carray_count(msg_list->msg_tab) ; i ++) { | ||
89 | mailmessage * msg; | ||
90 | |||
91 | msg = carray_get(msg_list->msg_tab, i); | ||
92 | msg->msg_folder = folder; | ||
93 | } | ||
94 | |||
95 | * result = msg_list; | ||
96 | |||
97 | return MAIL_NO_ERROR; | ||
98 | } | ||
99 | |||
100 | int mailfolder_get_envelopes_list(struct mailfolder * folder, | ||
101 | struct mailmessage_list * result) | ||
102 | { | ||
103 | return mailsession_get_envelopes_list(folder->fld_session, result); | ||
104 | } | ||
105 | |||
106 | int mailfolder_get_message(struct mailfolder * folder, | ||
107 | uint32_t num, mailmessage ** result) | ||
108 | { | ||
109 | mailmessage * msg; | ||
110 | int r; | ||
111 | |||
112 | r = mailsession_get_message(folder->fld_session, num, &msg); | ||
113 | if (r != MAIL_NO_ERROR) | ||
114 | return r; | ||
115 | |||
116 | msg->msg_folder = folder; | ||
117 | |||
118 | * result = msg; | ||
119 | |||
120 | return MAIL_NO_ERROR; | ||
121 | } | ||
122 | |||
123 | int mailfolder_get_message_by_uid(struct mailfolder * folder, | ||
124 | const char * uid, mailmessage ** result) | ||
125 | { | ||
126 | mailmessage * msg; | ||
127 | int r; | ||
128 | |||
129 | r = mailsession_get_message_by_uid(folder->fld_session, uid, &msg); | ||
130 | if (r != MAIL_NO_ERROR) | ||
131 | return r; | ||
132 | |||
133 | msg->msg_folder = folder; | ||
134 | |||
135 | * result = msg; | ||
136 | |||
137 | return MAIL_NO_ERROR; | ||
138 | } | ||
diff --git a/libetpan/src/driver/interface/mailfolder.h b/libetpan/src/driver/interface/mailfolder.h new file mode 100644 index 0000000..55ea3be --- a/dev/null +++ b/libetpan/src/driver/interface/mailfolder.h | |||
@@ -0,0 +1,70 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILFOLDER_H | ||
37 | |||
38 | #define MAILFOLDER_H | ||
39 | |||
40 | #include "mailstorage_types.h" | ||
41 | |||
42 | int mailfolder_noop(struct mailfolder * folder); | ||
43 | |||
44 | int mailfolder_check(struct mailfolder * folder); | ||
45 | |||
46 | int mailfolder_expunge(struct mailfolder * folder); | ||
47 | |||
48 | int mailfolder_status(struct mailfolder * folder, | ||
49 | uint32_t * result_messages, uint32_t * result_recent, | ||
50 | uint32_t * result_unseen); | ||
51 | |||
52 | int mailfolder_append_message(struct mailfolder * folder, | ||
53 | char * message, size_t size); | ||
54 | |||
55 | int mailfolder_append_message_flags(struct mailfolder * folder, | ||
56 | char * message, size_t size, struct mail_flags * flags); | ||
57 | |||
58 | int mailfolder_get_messages_list(struct mailfolder * folder, | ||
59 | struct mailmessage_list ** result); | ||
60 | |||
61 | int mailfolder_get_envelopes_list(struct mailfolder * folder, | ||
62 | struct mailmessage_list * result); | ||
63 | |||
64 | int mailfolder_get_message(struct mailfolder * folder, | ||
65 | uint32_t num, mailmessage ** result); | ||
66 | |||
67 | int mailfolder_get_message_by_uid(struct mailfolder * folder, | ||
68 | const char * uid, mailmessage ** result); | ||
69 | |||
70 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailmessage.c b/libetpan/src/driver/interface/mailmessage.c new file mode 100644 index 0000000..b4921e5 --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage.c | |||
@@ -0,0 +1,240 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailmessage.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | |||
40 | #include <string.h> | ||
41 | |||
42 | int mailmessage_init(mailmessage * msg_info, | ||
43 | mailsession * msg_session, | ||
44 | mailmessage_driver * msg_driver, | ||
45 | uint32_t msg_index, size_t msg_size) | ||
46 | { | ||
47 | int r; | ||
48 | int res; | ||
49 | |||
50 | msg_info->msg_driver = msg_driver; | ||
51 | msg_info->msg_session = msg_session; | ||
52 | msg_info->msg_index = msg_index; | ||
53 | msg_info->msg_uid = NULL; | ||
54 | |||
55 | msg_info->msg_cached = FALSE; | ||
56 | msg_info->msg_size = msg_size; | ||
57 | msg_info->msg_fields = NULL; | ||
58 | memset(&msg_info->msg_single_fields, 0, | ||
59 | sizeof(struct mailimf_single_fields)); | ||
60 | msg_info->msg_resolved = FALSE; | ||
61 | msg_info->msg_flags = NULL; | ||
62 | |||
63 | msg_info->msg_mime = NULL; | ||
64 | msg_info->msg_data = NULL; | ||
65 | msg_info->msg_folder = NULL; | ||
66 | msg_info->msg_user_data = NULL; | ||
67 | |||
68 | if (msg_driver->msg_initialize != NULL) { | ||
69 | r = msg_driver->msg_initialize(msg_info); | ||
70 | if (r != MAIL_NO_ERROR) { | ||
71 | res = r; | ||
72 | goto err; | ||
73 | } | ||
74 | } | ||
75 | |||
76 | return MAIL_NO_ERROR; | ||
77 | |||
78 | err: | ||
79 | msg_info->msg_driver = NULL; | ||
80 | msg_info->msg_session = NULL; | ||
81 | return res; | ||
82 | } | ||
83 | |||
84 | int mailmessage_flush(mailmessage * msg_info) | ||
85 | { | ||
86 | if (msg_info->msg_driver->msg_flush == NULL) | ||
87 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
88 | |||
89 | msg_info->msg_driver->msg_flush(msg_info); | ||
90 | |||
91 | return MAIL_NO_ERROR; | ||
92 | } | ||
93 | |||
94 | int mailmessage_check(mailmessage * msg_info) | ||
95 | { | ||
96 | if (msg_info->msg_driver->msg_check == NULL) | ||
97 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
98 | |||
99 | msg_info->msg_driver->msg_check(msg_info); | ||
100 | |||
101 | return MAIL_NO_ERROR; | ||
102 | } | ||
103 | |||
104 | int mailmessage_fetch_result_free(mailmessage * msg_info, | ||
105 | char * msg) | ||
106 | { | ||
107 | if (msg_info->msg_driver->msg_fetch_result_free == NULL) | ||
108 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
109 | |||
110 | msg_info->msg_driver->msg_fetch_result_free(msg_info, msg); | ||
111 | |||
112 | return MAIL_NO_ERROR; | ||
113 | } | ||
114 | |||
115 | int mailmessage_fetch(mailmessage * msg_info, | ||
116 | char ** result, | ||
117 | size_t * result_len) | ||
118 | { | ||
119 | if (msg_info->msg_driver->msg_fetch == NULL) | ||
120 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
121 | |||
122 | return msg_info->msg_driver->msg_fetch(msg_info, result, result_len); | ||
123 | } | ||
124 | |||
125 | int mailmessage_fetch_header(mailmessage * msg_info, | ||
126 | char ** result, | ||
127 | size_t * result_len) | ||
128 | { | ||
129 | if (msg_info->msg_driver->msg_fetch_header == NULL) | ||
130 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
131 | |||
132 | return msg_info->msg_driver->msg_fetch_header(msg_info, result, result_len); | ||
133 | } | ||
134 | |||
135 | int mailmessage_fetch_body(mailmessage * msg_info, | ||
136 | char ** result, size_t * result_len) | ||
137 | { | ||
138 | if (msg_info->msg_driver->msg_fetch_body == NULL) | ||
139 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
140 | |||
141 | return msg_info->msg_driver->msg_fetch_body(msg_info, result, result_len); | ||
142 | } | ||
143 | |||
144 | int mailmessage_fetch_size(mailmessage * msg_info, | ||
145 | size_t * result) | ||
146 | { | ||
147 | if (msg_info->msg_driver->msg_fetch_size == NULL) | ||
148 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
149 | |||
150 | return msg_info->msg_driver->msg_fetch_size(msg_info, result); | ||
151 | } | ||
152 | |||
153 | int mailmessage_get_bodystructure(mailmessage * msg_info, | ||
154 | struct mailmime ** result) | ||
155 | { | ||
156 | if (msg_info->msg_driver->msg_get_bodystructure == NULL) | ||
157 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
158 | |||
159 | return msg_info->msg_driver->msg_get_bodystructure(msg_info, result); | ||
160 | } | ||
161 | |||
162 | int mailmessage_fetch_section(mailmessage * msg_info, | ||
163 | struct mailmime * mime, | ||
164 | char ** result, size_t * result_len) | ||
165 | { | ||
166 | if (msg_info->msg_driver->msg_fetch_section == NULL) | ||
167 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
168 | |||
169 | return msg_info->msg_driver->msg_fetch_section(msg_info, mime, result, result_len); | ||
170 | } | ||
171 | |||
172 | int mailmessage_fetch_section_header(mailmessage * msg_info, | ||
173 | struct mailmime * mime, | ||
174 | char ** result, | ||
175 | size_t * result_len) | ||
176 | { | ||
177 | if (msg_info->msg_driver->msg_fetch_section_header == NULL) | ||
178 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
179 | |||
180 | return msg_info->msg_driver->msg_fetch_section_header(msg_info, mime, | ||
181 | result, result_len); | ||
182 | } | ||
183 | |||
184 | int mailmessage_fetch_section_mime(mailmessage * msg_info, | ||
185 | struct mailmime * mime, | ||
186 | char ** result, | ||
187 | size_t * result_len) | ||
188 | { | ||
189 | if (msg_info->msg_driver->msg_fetch_section_mime == NULL) | ||
190 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
191 | |||
192 | return msg_info->msg_driver->msg_fetch_section_mime(msg_info, mime, | ||
193 | result, result_len); | ||
194 | } | ||
195 | |||
196 | int mailmessage_fetch_section_body(mailmessage * msg_info, | ||
197 | struct mailmime * mime, | ||
198 | char ** result, | ||
199 | size_t * result_len) | ||
200 | { | ||
201 | if (msg_info->msg_driver->msg_fetch_section_body == NULL) | ||
202 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
203 | |||
204 | return msg_info->msg_driver->msg_fetch_section_body(msg_info, mime, | ||
205 | result, result_len); | ||
206 | } | ||
207 | |||
208 | int mailmessage_fetch_envelope(mailmessage * msg_info, | ||
209 | struct mailimf_fields ** result) | ||
210 | { | ||
211 | if (msg_info->msg_driver->msg_fetch_envelope == NULL) | ||
212 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
213 | |||
214 | return msg_info->msg_driver->msg_fetch_envelope(msg_info, result); | ||
215 | } | ||
216 | |||
217 | int mailmessage_get_flags(mailmessage * msg_info, | ||
218 | struct mail_flags ** result) | ||
219 | { | ||
220 | struct mail_flags * dummy; | ||
221 | |||
222 | if (msg_info->msg_driver->msg_get_flags == NULL) | ||
223 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
224 | |||
225 | if (result != NULL) | ||
226 | return msg_info->msg_driver->msg_get_flags(msg_info, result); | ||
227 | else | ||
228 | return msg_info->msg_driver->msg_get_flags(msg_info, &dummy); | ||
229 | } | ||
230 | |||
231 | void mailmessage_resolve_single_fields(mailmessage * msg_info) | ||
232 | { | ||
233 | if (!msg_info->msg_resolved) { | ||
234 | if (msg_info->msg_fields != NULL) { | ||
235 | mailimf_single_fields_init(&msg_info->msg_single_fields, | ||
236 | msg_info->msg_fields); | ||
237 | msg_info->msg_resolved = TRUE; | ||
238 | } | ||
239 | } | ||
240 | } | ||
diff --git a/libetpan/src/driver/interface/mailmessage.h b/libetpan/src/driver/interface/mailmessage.h new file mode 100644 index 0000000..02b351b --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage.h | |||
@@ -0,0 +1,379 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 <libetpan/mailmessage_types.h> | ||
37 | |||
38 | #ifndef MAILMESSAGE_H | ||
39 | |||
40 | #define MAILMESSAGE_H | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* | ||
47 | mailmessage_new | ||
48 | |||
49 | This function will initializes a new empty message. | ||
50 | |||
51 | @return a new empty message will be returned. | ||
52 | */ | ||
53 | |||
54 | mailmessage * mailmessage_new(void); | ||
55 | |||
56 | /* | ||
57 | mailmessage_free | ||
58 | |||
59 | This function will release the memory used by this message. | ||
60 | */ | ||
61 | |||
62 | void mailmessage_free(mailmessage * info); | ||
63 | |||
64 | /* | ||
65 | mailmessage_init | ||
66 | |||
67 | This function will initializes a mailmessage structure | ||
68 | with a message from a given session. | ||
69 | |||
70 | @param msg_info This is the message to initialize. | ||
71 | |||
72 | @param session This is the source session of the message. It | ||
73 | can be NULL if the message does not get the information | ||
74 | through the session. | ||
75 | |||
76 | @param driver This is the driver to use for the message. | ||
77 | |||
78 | @param index This is the message number in the session. 0 can | ||
79 | be given if the message is not attached to a session. | ||
80 | |||
81 | @param size is an optional parameter, 0 can be given. | ||
82 | This is informational. This is the size of message content. | ||
83 | |||
84 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
85 | on error | ||
86 | */ | ||
87 | |||
88 | int mailmessage_init(mailmessage * msg_info, | ||
89 | mailsession * session, | ||
90 | mailmessage_driver * driver, | ||
91 | uint32_t index, size_t size); | ||
92 | |||
93 | /* | ||
94 | mailmessage_flush | ||
95 | |||
96 | This function will release all the temporary resources that are not | ||
97 | necessary to use the mailmessage structure from memory. These | ||
98 | resources are for example cached information, such as the MIME | ||
99 | structure. | ||
100 | |||
101 | @param info is the message to clean. | ||
102 | |||
103 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
104 | on error. We can assume that MAIL_NO_ERROR is always returned. | ||
105 | */ | ||
106 | |||
107 | int mailmessage_flush(mailmessage * info); | ||
108 | |||
109 | /* | ||
110 | mailmessage_check | ||
111 | |||
112 | This function will notify the new value of the flags to the session, | ||
113 | it must be called before mailsession_check_folder() in case the flags have | ||
114 | been changed. | ||
115 | |||
116 | @param info is the message to checkpoint. | ||
117 | |||
118 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
119 | on error. We can assume that MAIL_NO_ERROR is always returned. | ||
120 | */ | ||
121 | |||
122 | int mailmessage_check(mailmessage * info); | ||
123 | |||
124 | /* | ||
125 | mailmessage_fetch_result_free | ||
126 | |||
127 | This function releases the memory used by a message returned | ||
128 | by any of the fetch function that returns a (char *). | ||
129 | |||
130 | @param msg_info is the message which the given buffer is from. | ||
131 | |||
132 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
133 | on error. We can assume that MAIL_NO_ERROR is always returned. | ||
134 | */ | ||
135 | |||
136 | int mailmessage_fetch_result_free(mailmessage * msg_info, | ||
137 | char * msg); | ||
138 | |||
139 | /* | ||
140 | mailmessage_fetch | ||
141 | |||
142 | This function returns the content of the message (headers and text). | ||
143 | |||
144 | @param msg_info is the message from which we want to fetch information. | ||
145 | |||
146 | @param result The content of the message is returned in (* result) | ||
147 | |||
148 | @param result_len The length of the returned string is stored | ||
149 | in (* result_len). | ||
150 | |||
151 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
152 | on error. | ||
153 | */ | ||
154 | |||
155 | int mailmessage_fetch(mailmessage * msg_info, | ||
156 | char ** result, | ||
157 | size_t * result_len); | ||
158 | |||
159 | /* | ||
160 | mailmessage_fetch_header | ||
161 | |||
162 | This function returns the header of the message as a string. | ||
163 | |||
164 | @param msg_info is the message from which we want to fetch information. | ||
165 | |||
166 | @param result The header of the message is returned in (* result) | ||
167 | |||
168 | @param result_len The length of the returned string is stored | ||
169 | in (* result_len). | ||
170 | |||
171 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
172 | on error. | ||
173 | */ | ||
174 | |||
175 | int mailmessage_fetch_header(mailmessage * msg_info, | ||
176 | char ** result, | ||
177 | size_t * result_len); | ||
178 | |||
179 | /* | ||
180 | mailmessage_fetch_body | ||
181 | |||
182 | This function returns the content of the message (without headers). | ||
183 | |||
184 | @param msg_info is the message from which we want to fetch information. | ||
185 | @param result The message text (without headers) is returned | ||
186 | in (* result) | ||
187 | @param result_len The length of the returned string is stored | ||
188 | in (* result_len). | ||
189 | |||
190 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
191 | on error. | ||
192 | */ | ||
193 | |||
194 | int mailmessage_fetch_body(mailmessage * msg_info, | ||
195 | char ** result, size_t * result_len); | ||
196 | |||
197 | /* | ||
198 | mailmessage_fetch_size | ||
199 | |||
200 | This function returns the size of the message content. | ||
201 | |||
202 | @param msg_info is the message from which we want to fetch information. | ||
203 | |||
204 | @param result The length of the message content is stored in (* result). | ||
205 | |||
206 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
207 | on error. | ||
208 | */ | ||
209 | |||
210 | int mailmessage_fetch_size(mailmessage * msg_info, | ||
211 | size_t * result); | ||
212 | |||
213 | /* | ||
214 | mailmessage_get_bodystructure | ||
215 | |||
216 | This functions returns the MIME structure of the message. | ||
217 | The returned information MUST not be freed by hand. It is freed by | ||
218 | mailmessage_flush() or mailmessage_free(). | ||
219 | |||
220 | @param msg_info is the message from which we want to fetch information. | ||
221 | |||
222 | @param result The MIME structure is stored in (* result). | ||
223 | |||
224 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
225 | on error. | ||
226 | */ | ||
227 | |||
228 | int mailmessage_get_bodystructure(mailmessage * msg_info, | ||
229 | struct mailmime ** result); | ||
230 | |||
231 | /* | ||
232 | mailmessage_fetch_section | ||
233 | |||
234 | This function returns the content of a MIME part. | ||
235 | |||
236 | @param msg_info is the message from which we want to fetch information. | ||
237 | |||
238 | @param mime is the MIME part identifier. | ||
239 | |||
240 | @param result The content is returned in (* result) | ||
241 | |||
242 | @param result_len The length of the returned string is stored | ||
243 | in (* result_len). | ||
244 | |||
245 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
246 | on error. | ||
247 | */ | ||
248 | |||
249 | int mailmessage_fetch_section(mailmessage * msg_info, | ||
250 | struct mailmime * mime, | ||
251 | char ** result, size_t * result_len); | ||
252 | |||
253 | /* | ||
254 | mailmessage_fetch_section_header | ||
255 | |||
256 | This function returns the header of the message contained | ||
257 | in the given MIME part. | ||
258 | |||
259 | @param msg_info is the message from which we want to fetch information. | ||
260 | |||
261 | @param mime is the MIME part identifier. | ||
262 | |||
263 | @param result The header is returned in (* result) | ||
264 | |||
265 | @param result_len The length of the returned string is stored | ||
266 | in (* result_len). | ||
267 | |||
268 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
269 | on error. | ||
270 | */ | ||
271 | |||
272 | int mailmessage_fetch_section_header(mailmessage * msg_info, | ||
273 | struct mailmime * mime, | ||
274 | char ** result, | ||
275 | size_t * result_len); | ||
276 | |||
277 | /* | ||
278 | mailmessage_fetch_section_mime | ||
279 | |||
280 | This function returns the MIME header of the given MIME part. | ||
281 | |||
282 | @param msg_info is the message from which we want to fetch information. | ||
283 | |||
284 | @param mime is the MIME part identifier. | ||
285 | |||
286 | @param result The MIME header is returned in (* result) | ||
287 | |||
288 | @param result_len The length of the returned string is stored | ||
289 | in (* result_len). | ||
290 | |||
291 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
292 | on error. | ||
293 | */ | ||
294 | |||
295 | int mailmessage_fetch_section_mime(mailmessage * msg_info, | ||
296 | struct mailmime * mime, | ||
297 | char ** result, | ||
298 | size_t * result_len); | ||
299 | |||
300 | /* | ||
301 | mailmessage_fetch_section_body | ||
302 | |||
303 | This function returns the text part of the message contained | ||
304 | in the given MIME part. | ||
305 | |||
306 | @param msg_info is the message from which we want to fetch information. | ||
307 | |||
308 | @param mime is the MIME part identifier. | ||
309 | |||
310 | @param result The message text is returned in (* result) | ||
311 | |||
312 | @param result_len The length of the returned string is stored | ||
313 | in (* result_len). | ||
314 | |||
315 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
316 | on error. | ||
317 | */ | ||
318 | |||
319 | int mailmessage_fetch_section_body(mailmessage * msg_info, | ||
320 | struct mailmime * mime, | ||
321 | char ** result, | ||
322 | size_t * result_len); | ||
323 | |||
324 | /* | ||
325 | mailmessage_fetch_envelope | ||
326 | |||
327 | This function returns a list of parsed fields of the message, | ||
328 | chosen by the driver. | ||
329 | The returned structure must be freed with mailimf_fields_free(). | ||
330 | |||
331 | @param msg_info is the message from which we want to fetch information. | ||
332 | |||
333 | @param result The headers list is returned in (* result) | ||
334 | |||
335 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
336 | on error. | ||
337 | */ | ||
338 | |||
339 | int mailmessage_fetch_envelope(mailmessage * msg_info, | ||
340 | struct mailimf_fields ** result); | ||
341 | |||
342 | |||
343 | /* | ||
344 | mailmessage_get_flags | ||
345 | |||
346 | This function returns the flags related to the message. | ||
347 | The returned information MUST not be freed by hand. It is freed by | ||
348 | mailmessage_free(). | ||
349 | |||
350 | @param msg_info is the message from which we want to fetch information. | ||
351 | |||
352 | @param result The flags are stored in (* result). | ||
353 | |||
354 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
355 | on error. | ||
356 | */ | ||
357 | |||
358 | int mailmessage_get_flags(mailmessage * msg_info, | ||
359 | struct mail_flags ** result); | ||
360 | |||
361 | /* | ||
362 | mailmessage_resolve_single_fields | ||
363 | |||
364 | This function will use the fields information to fill the single_fields | ||
365 | structure in the mailmessage structure. | ||
366 | |||
367 | @param msg_info This is the msg_info to process. | ||
368 | |||
369 | @return MAIL_NO_ERROR is returned on success, MAIL_ERROR_XXX is returned | ||
370 | on error. | ||
371 | */ | ||
372 | |||
373 | void mailmessage_resolve_single_fields(mailmessage * msg_info); | ||
374 | |||
375 | #ifdef __cplusplus | ||
376 | } | ||
377 | #endif | ||
378 | |||
379 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailmessage_tools.c b/libetpan/src/driver/interface/mailmessage_tools.c new file mode 100644 index 0000000..9e53173 --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage_tools.c | |||
@@ -0,0 +1,600 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailmessage_tools.h" | ||
37 | #include "mailmessage.h" | ||
38 | |||
39 | #include <stdlib.h> | ||
40 | |||
41 | #include "maildriver.h" | ||
42 | #include "maildriver_tools.h" | ||
43 | |||
44 | int | ||
45 | mailmessage_generic_initialize(mailmessage * msg_info) | ||
46 | { | ||
47 | struct generic_message_t * msg; | ||
48 | |||
49 | msg = malloc(sizeof(* msg)); | ||
50 | |||
51 | if (msg == NULL) { | ||
52 | return MAIL_ERROR_MEMORY; | ||
53 | } | ||
54 | |||
55 | msg->msg_fetched = 0; | ||
56 | msg->msg_message = NULL; | ||
57 | msg->msg_length = 0; | ||
58 | |||
59 | msg->msg_prefetch = NULL; | ||
60 | msg->msg_prefetch_free = NULL; | ||
61 | msg->msg_data = NULL; | ||
62 | |||
63 | msg_info->msg_data = msg; | ||
64 | |||
65 | return MAIL_NO_ERROR; | ||
66 | } | ||
67 | |||
68 | void mailmessage_generic_flush(mailmessage * msg_info) | ||
69 | { | ||
70 | struct generic_message_t * msg; | ||
71 | |||
72 | if (msg_info->msg_mime != NULL) { | ||
73 | mailmime_free(msg_info->msg_mime); | ||
74 | msg_info->msg_mime = NULL; | ||
75 | } | ||
76 | msg = msg_info->msg_data; | ||
77 | if (msg != NULL) { | ||
78 | if (msg->msg_prefetch_free != NULL) | ||
79 | msg->msg_prefetch_free(msg); | ||
80 | msg->msg_fetched = 0; | ||
81 | } | ||
82 | } | ||
83 | |||
84 | void mailmessage_generic_uninitialize(mailmessage * msg_info) | ||
85 | { | ||
86 | struct generic_message_t * msg; | ||
87 | |||
88 | mailmessage_generic_flush(msg_info); | ||
89 | |||
90 | msg = msg_info->msg_data; | ||
91 | msg_info->msg_data = NULL; | ||
92 | free(msg); | ||
93 | } | ||
94 | |||
95 | static inline int | ||
96 | mailmessage_generic_prefetch(mailmessage * msg_info) | ||
97 | { | ||
98 | struct generic_message_t * msg; | ||
99 | int r; | ||
100 | |||
101 | msg = msg_info->msg_data; | ||
102 | |||
103 | if (msg->msg_fetched) | ||
104 | return MAIL_NO_ERROR; | ||
105 | |||
106 | #if 0 | ||
107 | if (msg->message != NULL) | ||
108 | return MAIL_NO_ERROR; | ||
109 | #endif | ||
110 | |||
111 | r = msg->msg_prefetch(msg_info); | ||
112 | if (r != MAIL_NO_ERROR) | ||
113 | return r; | ||
114 | |||
115 | msg->msg_fetched = 1; | ||
116 | |||
117 | return MAIL_NO_ERROR; | ||
118 | } | ||
119 | |||
120 | static int | ||
121 | mailmessage_generic_prefetch_bodystructure(mailmessage * msg_info) | ||
122 | { | ||
123 | size_t length; | ||
124 | char * message; | ||
125 | size_t cur_token; | ||
126 | struct mailmime * mime; | ||
127 | int r; | ||
128 | int res; | ||
129 | struct generic_message_t * msg; | ||
130 | |||
131 | if (msg_info->msg_mime != NULL) { | ||
132 | /* it has already been fetched */ | ||
133 | return MAIL_NO_ERROR; | ||
134 | } | ||
135 | |||
136 | #if 0 | ||
137 | msg = msg_info->data; | ||
138 | if (msg->message == NULL) { | ||
139 | r = mailmessage_generic_prefetch(msg_info); | ||
140 | if (r != MAIL_NO_ERROR) { | ||
141 | res = r; | ||
142 | goto err; | ||
143 | } | ||
144 | } | ||
145 | #endif | ||
146 | r = mailmessage_generic_prefetch(msg_info); | ||
147 | if (r != MAIL_NO_ERROR) { | ||
148 | res = r; | ||
149 | goto err; | ||
150 | } | ||
151 | |||
152 | msg = msg_info->msg_data; | ||
153 | message = msg->msg_message; | ||
154 | length = msg->msg_length; | ||
155 | cur_token = 0; | ||
156 | r = mailmime_parse(message, length, &cur_token, &mime); | ||
157 | if (r != MAILIMF_NO_ERROR) { | ||
158 | res = MAIL_ERROR_PARSE; | ||
159 | goto err; | ||
160 | } | ||
161 | |||
162 | msg_info->msg_mime = mime; | ||
163 | |||
164 | return MAIL_NO_ERROR; | ||
165 | |||
166 | err: | ||
167 | return res; | ||
168 | } | ||
169 | |||
170 | void | ||
171 | mailmessage_generic_fetch_result_free(mailmessage * msg_info, char * msg) | ||
172 | { | ||
173 | int r; | ||
174 | |||
175 | r = mmap_string_unref(msg); | ||
176 | } | ||
177 | |||
178 | int mailmessage_generic_fetch(mailmessage * msg_info, | ||
179 | char ** result, | ||
180 | size_t * result_len) | ||
181 | { | ||
182 | int r; | ||
183 | char * message; | ||
184 | size_t cur_token; | ||
185 | size_t length; | ||
186 | MMAPString * mmapstr; | ||
187 | int res; | ||
188 | struct generic_message_t * msg; | ||
189 | |||
190 | msg = msg_info->msg_data; | ||
191 | r = mailmessage_generic_prefetch(msg_info); | ||
192 | if (r != MAIL_NO_ERROR) { | ||
193 | res = r; | ||
194 | goto err; | ||
195 | } | ||
196 | |||
197 | message = msg->msg_message; | ||
198 | length = msg->msg_length; | ||
199 | cur_token = 0; | ||
200 | |||
201 | mmapstr = mmap_string_new_len(message, length); | ||
202 | if (mmapstr == NULL) { | ||
203 | res = MAIL_ERROR_MEMORY; | ||
204 | goto err; | ||
205 | } | ||
206 | |||
207 | r = mmap_string_ref(mmapstr); | ||
208 | if (r < 0) { | ||
209 | res = MAIL_ERROR_MEMORY; | ||
210 | goto free_mmap; | ||
211 | } | ||
212 | |||
213 | * result = mmapstr->str; | ||
214 | * result_len = length; | ||
215 | |||
216 | return MAIL_NO_ERROR; | ||
217 | |||
218 | free_mmap: | ||
219 | mmap_string_free(mmapstr); | ||
220 | err: | ||
221 | return res; | ||
222 | } | ||
223 | |||
224 | int mailmessage_generic_fetch_header(mailmessage * msg_info, | ||
225 | char ** result, | ||
226 | size_t * result_len) | ||
227 | { | ||
228 | int r; | ||
229 | char * message; | ||
230 | size_t cur_token; | ||
231 | size_t length; | ||
232 | MMAPString * mmapstr; | ||
233 | char * headers; | ||
234 | int res; | ||
235 | struct generic_message_t * msg; | ||
236 | |||
237 | msg = msg_info->msg_data; | ||
238 | r = mailmessage_generic_prefetch(msg_info); | ||
239 | if (r != MAIL_NO_ERROR) { | ||
240 | res = r; | ||
241 | goto err; | ||
242 | } | ||
243 | |||
244 | message = msg->msg_message; | ||
245 | length = msg->msg_length; | ||
246 | cur_token = 0; | ||
247 | |||
248 | while (1) { | ||
249 | r = mailimf_ignore_field_parse(message, length, &cur_token); | ||
250 | if (r == MAILIMF_NO_ERROR) { | ||
251 | /* do nothing */ | ||
252 | } | ||
253 | else | ||
254 | break; | ||
255 | } | ||
256 | mailimf_crlf_parse(message, length, &cur_token); | ||
257 | |||
258 | mmapstr = mmap_string_new_len(message, cur_token); | ||
259 | if (mmapstr == NULL) { | ||
260 | res = MAIL_ERROR_MEMORY; | ||
261 | goto err; | ||
262 | } | ||
263 | |||
264 | r = mmap_string_ref(mmapstr); | ||
265 | if (r < 0) { | ||
266 | res = MAIL_ERROR_MEMORY; | ||
267 | goto free_mmap; | ||
268 | } | ||
269 | |||
270 | headers = mmapstr->str; | ||
271 | |||
272 | * result = headers; | ||
273 | * result_len = cur_token; | ||
274 | |||
275 | return MAIL_NO_ERROR; | ||
276 | |||
277 | free_mmap: | ||
278 | mmap_string_free(mmapstr); | ||
279 | err: | ||
280 | return res; | ||
281 | } | ||
282 | |||
283 | int mailmessage_generic_fetch_body(mailmessage * msg_info, | ||
284 | char ** result, size_t * result_len) | ||
285 | { | ||
286 | int r; | ||
287 | char * message; | ||
288 | size_t cur_token; | ||
289 | MMAPString * mmapstr; | ||
290 | size_t length; | ||
291 | int res; | ||
292 | struct generic_message_t * msg; | ||
293 | |||
294 | msg = msg_info->msg_data; | ||
295 | r = mailmessage_generic_prefetch(msg_info); | ||
296 | if (r != MAIL_NO_ERROR) { | ||
297 | res = r; | ||
298 | goto err; | ||
299 | } | ||
300 | |||
301 | message = msg->msg_message; | ||
302 | length = msg->msg_length; | ||
303 | cur_token = 0; | ||
304 | |||
305 | while (1) { | ||
306 | r = mailimf_ignore_field_parse(message, length, &cur_token); | ||
307 | if (r == MAILIMF_NO_ERROR) { | ||
308 | /* do nothing */ | ||
309 | } | ||
310 | else | ||
311 | break; | ||
312 | } | ||
313 | mailimf_crlf_parse(message, length, &cur_token); | ||
314 | |||
315 | mmapstr = mmap_string_new_len(message + cur_token, length - cur_token); | ||
316 | if (mmapstr == NULL) { | ||
317 | res = MAIL_ERROR_MEMORY; | ||
318 | goto err; | ||
319 | } | ||
320 | |||
321 | r = mmap_string_ref(mmapstr); | ||
322 | if (r < 0) { | ||
323 | res = MAIL_ERROR_MEMORY; | ||
324 | goto free_mmap; | ||
325 | } | ||
326 | |||
327 | * result = mmapstr->str; | ||
328 | * result_len = length - cur_token; | ||
329 | |||
330 | return MAIL_NO_ERROR; | ||
331 | |||
332 | free_mmap: | ||
333 | mmap_string_free(mmapstr); | ||
334 | err: | ||
335 | return res; | ||
336 | } | ||
337 | |||
338 | |||
339 | |||
340 | |||
341 | int | ||
342 | mailmessage_generic_get_bodystructure(mailmessage * msg_info, | ||
343 | struct mailmime ** result) | ||
344 | { | ||
345 | int r; | ||
346 | |||
347 | r = mailmessage_generic_prefetch_bodystructure(msg_info); | ||
348 | if (r != MAIL_NO_ERROR) | ||
349 | return r; | ||
350 | |||
351 | * result = msg_info->msg_mime; | ||
352 | |||
353 | return MAIL_NO_ERROR; | ||
354 | } | ||
355 | |||
356 | |||
357 | |||
358 | |||
359 | int | ||
360 | mailmessage_generic_fetch_section(mailmessage * msg_info, | ||
361 | struct mailmime * mime, | ||
362 | char ** result, size_t * result_len) | ||
363 | { | ||
364 | MMAPString * mmapstr; | ||
365 | int r; | ||
366 | int res; | ||
367 | |||
368 | mmapstr = mmap_string_new_len(mime->mm_body->dt_data.dt_text.dt_data, | ||
369 | mime->mm_body->dt_data.dt_text.dt_length); | ||
370 | if (mmapstr == NULL) { | ||
371 | res = MAIL_ERROR_MEMORY; | ||
372 | goto err; | ||
373 | } | ||
374 | |||
375 | r = mmap_string_ref(mmapstr); | ||
376 | if (r < 0) { | ||
377 | res = MAIL_ERROR_MEMORY; | ||
378 | goto free_mmap; | ||
379 | } | ||
380 | |||
381 | * result = mmapstr->str; | ||
382 | * result_len = mmapstr->len; | ||
383 | |||
384 | return MAIL_NO_ERROR; | ||
385 | |||
386 | free_mmap: | ||
387 | mmap_string_free(mmapstr); | ||
388 | err: | ||
389 | return res; | ||
390 | } | ||
391 | |||
392 | int | ||
393 | mailmessage_generic_fetch_section_header(mailmessage * msg_info, | ||
394 | struct mailmime * mime, | ||
395 | char ** result, | ||
396 | size_t * result_len) | ||
397 | { | ||
398 | MMAPString * mmapstr; | ||
399 | int r; | ||
400 | int res; | ||
401 | size_t cur_token; | ||
402 | |||
403 | /* skip mime */ | ||
404 | |||
405 | cur_token = 0; | ||
406 | |||
407 | if (mime->mm_type == MAILMIME_MESSAGE) { | ||
408 | |||
409 | while (1) { | ||
410 | r = mailimf_ignore_field_parse(mime->mm_body->dt_data.dt_text.dt_data, | ||
411 | mime->mm_body->dt_data.dt_text.dt_length, &cur_token); | ||
412 | if (r == MAILIMF_NO_ERROR) { | ||
413 | /* do nothing */ | ||
414 | } | ||
415 | else | ||
416 | break; | ||
417 | } | ||
418 | |||
419 | r = mailimf_crlf_parse(mime->mm_body->dt_data.dt_text.dt_data, | ||
420 | mime->mm_body->dt_data.dt_text.dt_length, &cur_token); | ||
421 | if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { | ||
422 | res = maildriver_imf_error_to_mail_error(r); | ||
423 | goto err; | ||
424 | } | ||
425 | } | ||
426 | |||
427 | mmapstr = mmap_string_new_len(mime->mm_body->dt_data.dt_text.dt_data, | ||
428 | cur_token); | ||
429 | if (mmapstr == NULL) { | ||
430 | res = MAIL_ERROR_MEMORY; | ||
431 | goto err; | ||
432 | } | ||
433 | |||
434 | r = mmap_string_ref(mmapstr); | ||
435 | if (r < 0) { | ||
436 | res = MAIL_ERROR_MEMORY; | ||
437 | goto free_mmap; | ||
438 | } | ||
439 | |||
440 | * result = mmapstr->str; | ||
441 | * result_len = mmapstr->len; | ||
442 | |||
443 | return MAIL_NO_ERROR; | ||
444 | |||
445 | free_mmap: | ||
446 | mmap_string_free(mmapstr); | ||
447 | err: | ||
448 | return res; | ||
449 | } | ||
450 | |||
451 | int | ||
452 | mailmessage_generic_fetch_section_mime(mailmessage * msg_info, | ||
453 | struct mailmime * mime, | ||
454 | char ** result, | ||
455 | size_t * result_len) | ||
456 | { | ||
457 | MMAPString * mmapstr; | ||
458 | int r; | ||
459 | int res; | ||
460 | size_t cur_token; | ||
461 | |||
462 | cur_token = 0; | ||
463 | |||
464 | /* skip header */ | ||
465 | |||
466 | while (1) { | ||
467 | r = mailimf_ignore_field_parse(mime->mm_mime_start, | ||
468 | mime->mm_length, &cur_token); | ||
469 | if (r == MAILIMF_NO_ERROR) { | ||
470 | /* do nothing */ | ||
471 | } | ||
472 | else | ||
473 | break; | ||
474 | } | ||
475 | |||
476 | r = mailimf_crlf_parse(mime->mm_mime_start, mime->mm_length, &cur_token); | ||
477 | if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { | ||
478 | res = maildriver_imf_error_to_mail_error(r); | ||
479 | goto err; | ||
480 | } | ||
481 | |||
482 | mmapstr = mmap_string_new_len(mime->mm_mime_start, cur_token); | ||
483 | if (mmapstr == NULL) { | ||
484 | res = MAIL_ERROR_MEMORY; | ||
485 | goto err; | ||
486 | } | ||
487 | |||
488 | r = mmap_string_ref(mmapstr); | ||
489 | if (r < 0) { | ||
490 | res = MAIL_ERROR_MEMORY; | ||
491 | goto free_mmap; | ||
492 | } | ||
493 | |||
494 | * result = mmapstr->str; | ||
495 | * result_len = mmapstr->len; | ||
496 | |||
497 | return MAIL_NO_ERROR; | ||
498 | |||
499 | free_mmap: | ||
500 | mmap_string_free(mmapstr); | ||
501 | err: | ||
502 | return res; | ||
503 | } | ||
504 | |||
505 | int | ||
506 | mailmessage_generic_fetch_section_body(mailmessage * msg_info, | ||
507 | struct mailmime * mime, | ||
508 | char ** result, | ||
509 | size_t * result_len) | ||
510 | { | ||
511 | MMAPString * mmapstr; | ||
512 | int r; | ||
513 | int res; | ||
514 | size_t cur_token; | ||
515 | |||
516 | cur_token = 0; | ||
517 | |||
518 | if (mime->mm_type == MAILMIME_MESSAGE) { | ||
519 | |||
520 | /* skip header */ | ||
521 | |||
522 | while (1) { | ||
523 | r = mailimf_ignore_field_parse(mime->mm_body->dt_data.dt_text.dt_data, | ||
524 | mime->mm_body->dt_data.dt_text.dt_length, &cur_token); | ||
525 | if (r == MAILIMF_NO_ERROR) { | ||
526 | /* do nothing */ | ||
527 | } | ||
528 | else | ||
529 | break; | ||
530 | } | ||
531 | |||
532 | r = mailimf_crlf_parse(mime->mm_body->dt_data.dt_text.dt_data, | ||
533 | mime->mm_body->dt_data.dt_text.dt_length, &cur_token); | ||
534 | if ((r != MAILIMF_NO_ERROR) && (r != MAILIMF_ERROR_PARSE)) { | ||
535 | res = maildriver_imf_error_to_mail_error(r); | ||
536 | goto err; | ||
537 | } | ||
538 | } | ||
539 | |||
540 | mmapstr = mmap_string_new_len(mime->mm_body->dt_data.dt_text.dt_data + | ||
541 | cur_token, mime->mm_body->dt_data.dt_text.dt_length - cur_token); | ||
542 | if (mmapstr == NULL) { | ||
543 | res = MAIL_ERROR_MEMORY; | ||
544 | goto err; | ||
545 | } | ||
546 | |||
547 | r = mmap_string_ref(mmapstr); | ||
548 | if (r < 0) { | ||
549 | res = MAIL_ERROR_MEMORY; | ||
550 | goto free_mmap; | ||
551 | } | ||
552 | |||
553 | * result = mmapstr->str; | ||
554 | * result_len = mmapstr->len; | ||
555 | |||
556 | return MAIL_NO_ERROR; | ||
557 | |||
558 | free_mmap: | ||
559 | mmap_string_free(mmapstr); | ||
560 | err: | ||
561 | return res; | ||
562 | } | ||
563 | |||
564 | int mailmessage_generic_fetch_envelope(mailmessage * msg_info, | ||
565 | struct mailimf_fields ** result) | ||
566 | { | ||
567 | int r; | ||
568 | int res; | ||
569 | size_t cur_token; | ||
570 | char * header; | ||
571 | size_t length; | ||
572 | struct mailimf_fields * fields; | ||
573 | |||
574 | r = mailmessage_fetch_header(msg_info, &header, &length); | ||
575 | if (r != MAIL_NO_ERROR) { | ||
576 | res = r; | ||
577 | goto err; | ||
578 | } | ||
579 | |||
580 | cur_token = 0; | ||
581 | |||
582 | r = mailimf_envelope_fields_parse(header, length, &cur_token, | ||
583 | &fields); | ||
584 | if (r != MAILIMF_NO_ERROR) { | ||
585 | res = maildriver_imf_error_to_mail_error(r); | ||
586 | goto free; | ||
587 | /* do nothing */ | ||
588 | } | ||
589 | |||
590 | mailmessage_fetch_result_free(msg_info, header); | ||
591 | |||
592 | * result = fields; | ||
593 | |||
594 | return MAIL_NO_ERROR; | ||
595 | |||
596 | free: | ||
597 | mailmessage_fetch_result_free(msg_info, header); | ||
598 | err: | ||
599 | return res; | ||
600 | } | ||
diff --git a/libetpan/src/driver/interface/mailmessage_tools.h b/libetpan/src/driver/interface/mailmessage_tools.h new file mode 100644 index 0000000..fd055c7 --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage_tools.h | |||
@@ -0,0 +1,103 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILMESSAGE_TOOLS_H | ||
37 | |||
38 | #define MAILMESSAGE_TOOLS_H | ||
39 | |||
40 | #include "mailmessage_types.h" | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | int | ||
47 | mailmessage_generic_initialize(mailmessage * | ||
48 | msg_info); | ||
49 | |||
50 | void mailmessage_generic_uninitialize(mailmessage * | ||
51 | msg_info); | ||
52 | |||
53 | void mailmessage_generic_flush(mailmessage * msg_info); | ||
54 | |||
55 | void mailmessage_generic_fetch_result_free(mailmessage * msg_info, | ||
56 | char * msg); | ||
57 | |||
58 | int mailmessage_generic_fetch(mailmessage * msg_info, | ||
59 | char ** result, | ||
60 | size_t * result_len); | ||
61 | |||
62 | int mailmessage_generic_fetch_header(mailmessage * msg_info, | ||
63 | char ** result, | ||
64 | size_t * result_len); | ||
65 | |||
66 | int mailmessage_generic_fetch_body(mailmessage * msg_info, | ||
67 | char ** result, size_t * result_len); | ||
68 | |||
69 | int mailmessage_generic_get_bodystructure(mailmessage * | ||
70 | msg_info, | ||
71 | struct mailmime ** result); | ||
72 | |||
73 | int | ||
74 | mailmessage_generic_fetch_section(mailmessage * msg_info, | ||
75 | struct mailmime * mime, | ||
76 | char ** result, size_t * result_len); | ||
77 | |||
78 | int | ||
79 | mailmessage_generic_fetch_section_header(mailmessage * msg_info, | ||
80 | struct mailmime * mime, | ||
81 | char ** result, | ||
82 | size_t * result_len); | ||
83 | |||
84 | int | ||
85 | mailmessage_generic_fetch_section_mime(mailmessage * msg_info, | ||
86 | struct mailmime * mime, | ||
87 | char ** result, | ||
88 | size_t * result_len); | ||
89 | |||
90 | int | ||
91 | mailmessage_generic_fetch_section_body(mailmessage * msg_info, | ||
92 | struct mailmime * mime, | ||
93 | char ** result, | ||
94 | size_t * result_len); | ||
95 | |||
96 | int mailmessage_generic_fetch_envelope(mailmessage * msg_info, | ||
97 | struct mailimf_fields ** result); | ||
98 | |||
99 | #ifdef __cplusplus | ||
100 | } | ||
101 | #endif | ||
102 | |||
103 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailmessage_types.c b/libetpan/src/driver/interface/mailmessage_types.c new file mode 100644 index 0000000..e0955e4 --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage_types.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailmessage_types.h" | ||
37 | |||
38 | #include "mail.h" | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | |||
43 | mailmessage * mailmessage_new(void) | ||
44 | { | ||
45 | mailmessage * msg_info; | ||
46 | |||
47 | msg_info = malloc(sizeof(* msg_info)); | ||
48 | if (msg_info == NULL) | ||
49 | goto err; | ||
50 | |||
51 | msg_info->msg_driver = NULL; | ||
52 | msg_info->msg_session = NULL; | ||
53 | msg_info->msg_index = 0; | ||
54 | msg_info->msg_uid = NULL; | ||
55 | |||
56 | msg_info->msg_cached = FALSE; | ||
57 | msg_info->msg_size = 0; | ||
58 | msg_info->msg_fields = NULL; | ||
59 | memset(&msg_info->msg_single_fields, | ||
60 | 0, sizeof(struct mailimf_single_fields)); | ||
61 | msg_info->msg_resolved = FALSE; | ||
62 | msg_info->msg_flags = NULL; | ||
63 | |||
64 | msg_info->msg_mime = NULL; | ||
65 | msg_info->msg_data = NULL; | ||
66 | |||
67 | msg_info->msg_folder = NULL; | ||
68 | msg_info->msg_user_data = NULL; | ||
69 | |||
70 | return msg_info; | ||
71 | |||
72 | err: | ||
73 | return NULL; | ||
74 | } | ||
75 | |||
76 | void mailmessage_free(mailmessage * msg_info) | ||
77 | { | ||
78 | if (msg_info->msg_driver != NULL) { | ||
79 | if (msg_info->msg_driver->msg_uninitialize != NULL) | ||
80 | msg_info->msg_driver->msg_uninitialize(msg_info); | ||
81 | } | ||
82 | |||
83 | if (msg_info->msg_fields != NULL) | ||
84 | mailimf_fields_free(msg_info->msg_fields); | ||
85 | if (msg_info->msg_mime != NULL) | ||
86 | mailmime_free(msg_info->msg_mime); | ||
87 | if (msg_info->msg_flags != NULL) | ||
88 | mail_flags_free(msg_info->msg_flags); | ||
89 | if (msg_info->msg_uid != NULL) | ||
90 | free(msg_info->msg_uid); | ||
91 | free(msg_info); | ||
92 | } | ||
diff --git a/libetpan/src/driver/interface/mailmessage_types.h b/libetpan/src/driver/interface/mailmessage_types.h new file mode 100644 index 0000000..c3ed2c4 --- a/dev/null +++ b/libetpan/src/driver/interface/mailmessage_types.h | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILMESSAGE_TYPES_H | ||
37 | |||
38 | #define MAILMESSAGE_TYPES_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | #ifdef __cplusplus | ||
47 | } | ||
48 | #endif | ||
49 | |||
50 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailstorage.c b/libetpan/src/driver/interface/mailstorage.c new file mode 100644 index 0000000..2e2ddcb --- a/dev/null +++ b/libetpan/src/driver/interface/mailstorage.c | |||
@@ -0,0 +1,341 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailstorage.h" | ||
37 | |||
38 | #include "maildriver.h" | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | |||
43 | static int mailstorage_get_folder(struct mailstorage * storage, | ||
44 | char * pathname, mailsession ** result); | ||
45 | |||
46 | struct mailfolder * mailfolder_new(struct mailstorage * storage, | ||
47 | char * pathname, char * virtual_name) | ||
48 | { | ||
49 | struct mailfolder * folder; | ||
50 | |||
51 | folder = malloc(sizeof(struct mailfolder)); | ||
52 | if (folder == NULL) | ||
53 | goto err; | ||
54 | |||
55 | if (pathname != NULL) { | ||
56 | folder->fld_pathname = strdup(pathname); | ||
57 | if (folder->fld_pathname == NULL) | ||
58 | goto free; | ||
59 | } | ||
60 | else | ||
61 | folder->fld_pathname = NULL; | ||
62 | |||
63 | if (virtual_name != NULL) { | ||
64 | folder->fld_virtual_name = strdup(virtual_name); | ||
65 | if (folder->fld_virtual_name == NULL) | ||
66 | goto free_pathname; | ||
67 | } | ||
68 | else | ||
69 | folder->fld_virtual_name = NULL; | ||
70 | |||
71 | folder->fld_storage = storage; | ||
72 | |||
73 | folder->fld_session = NULL; | ||
74 | folder->fld_shared_session = 0; | ||
75 | folder->fld_pos = NULL; | ||
76 | |||
77 | folder->fld_parent = NULL; | ||
78 | folder->fld_sibling_index = 0; | ||
79 | folder->fld_children = carray_new(128); | ||
80 | if (folder->fld_children == NULL) | ||
81 | goto free_virtualname; | ||
82 | |||
83 | return folder; | ||
84 | |||
85 | free_virtualname: | ||
86 | if (folder->fld_virtual_name != NULL) | ||
87 | free(folder->fld_virtual_name); | ||
88 | free_pathname: | ||
89 | if (folder->fld_pathname != NULL) | ||
90 | free(folder->fld_pathname); | ||
91 | free: | ||
92 | free(folder); | ||
93 | err: | ||
94 | return NULL; | ||
95 | } | ||
96 | |||
97 | void mailfolder_free(struct mailfolder * folder) | ||
98 | { | ||
99 | if (folder->fld_parent != NULL) | ||
100 | mailfolder_detach_parent(folder); | ||
101 | |||
102 | while (carray_count(folder->fld_children) > 0) { | ||
103 | struct mailfolder * child; | ||
104 | |||
105 | child = carray_get(folder->fld_children, 0); | ||
106 | mailfolder_detach_parent(child); | ||
107 | } | ||
108 | |||
109 | carray_free(folder->fld_children); | ||
110 | |||
111 | if (folder->fld_session != NULL) | ||
112 | mailfolder_disconnect(folder); | ||
113 | |||
114 | if (folder->fld_virtual_name != NULL) | ||
115 | free(folder->fld_virtual_name); | ||
116 | if (folder->fld_pathname != NULL) | ||
117 | free(folder->fld_pathname); | ||
118 | free(folder); | ||
119 | } | ||
120 | |||
121 | int mailfolder_connect(struct mailfolder * folder) | ||
122 | { | ||
123 | mailsession * session; | ||
124 | int res; | ||
125 | int r; | ||
126 | |||
127 | if (folder->fld_storage == NULL) { | ||
128 | res = MAIL_ERROR_INVAL; | ||
129 | goto err; | ||
130 | } | ||
131 | |||
132 | if (folder->fld_storage->sto_session == NULL) { | ||
133 | r = mailstorage_connect(folder->fld_storage); | ||
134 | if (r != MAIL_NO_ERROR) { | ||
135 | res = r; | ||
136 | goto err; | ||
137 | } | ||
138 | } | ||
139 | |||
140 | if (folder->fld_session != NULL) { | ||
141 | if ((folder->fld_pathname != NULL) && (folder->fld_shared_session)) { | ||
142 | if (folder->fld_session->sess_driver->sess_select_folder != NULL) { | ||
143 | r = mailsession_select_folder(folder->fld_session, | ||
144 | folder->fld_pathname); | ||
145 | if (r != MAIL_NO_ERROR) { | ||
146 | res = r; | ||
147 | goto err; | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | |||
152 | return MAIL_NO_ERROR; | ||
153 | } | ||
154 | |||
155 | r = mailstorage_get_folder(folder->fld_storage, folder->fld_pathname, | ||
156 | &session); | ||
157 | if (r != MAIL_NO_ERROR) { | ||
158 | res = r; | ||
159 | goto err; | ||
160 | } | ||
161 | folder->fld_session = session; | ||
162 | folder->fld_shared_session = (session == folder->fld_storage->sto_session); | ||
163 | if (folder->fld_shared_session) { | ||
164 | r = clist_append(folder->fld_storage->sto_shared_folders, folder); | ||
165 | if (r < 0) { | ||
166 | folder->fld_session = NULL; | ||
167 | res = MAIL_ERROR_MEMORY; | ||
168 | goto err; | ||
169 | } | ||
170 | folder->fld_pos = clist_end(folder->fld_storage->sto_shared_folders); | ||
171 | } | ||
172 | |||
173 | return MAIL_NO_ERROR; | ||
174 | |||
175 | err: | ||
176 | return res; | ||
177 | } | ||
178 | |||
179 | void mailfolder_disconnect(struct mailfolder * folder) | ||
180 | { | ||
181 | if (folder->fld_session == NULL) | ||
182 | return; | ||
183 | |||
184 | if (folder->fld_shared_session) { | ||
185 | clist_delete(folder->fld_storage->sto_shared_folders, folder->fld_pos); | ||
186 | folder->fld_pos = NULL; | ||
187 | } | ||
188 | else { | ||
189 | mailsession_logout(folder->fld_session); | ||
190 | mailsession_free(folder->fld_session); | ||
191 | } | ||
192 | |||
193 | folder->fld_session = NULL; | ||
194 | } | ||
195 | |||
196 | int mailfolder_add_child(struct mailfolder * parent, | ||
197 | struct mailfolder * child) | ||
198 | { | ||
199 | unsigned int index; | ||
200 | int r; | ||
201 | |||
202 | r = carray_add(parent->fld_children, child, &index); | ||
203 | if (r < 0) | ||
204 | return MAIL_ERROR_MEMORY; | ||
205 | |||
206 | child->fld_sibling_index = index; | ||
207 | child->fld_parent = parent; | ||
208 | |||
209 | return MAIL_NO_ERROR; | ||
210 | } | ||
211 | |||
212 | int mailfolder_detach_parent(struct mailfolder * folder) | ||
213 | { | ||
214 | unsigned int i; | ||
215 | int r; | ||
216 | |||
217 | if (folder->fld_parent == NULL) | ||
218 | return MAIL_ERROR_INVAL; | ||
219 | |||
220 | r = carray_delete_slow(folder->fld_parent->fld_children, | ||
221 | folder->fld_sibling_index); | ||
222 | if (r < 0) | ||
223 | return MAIL_ERROR_INVAL; | ||
224 | |||
225 | for(i = 0 ; i < carray_count(folder->fld_parent->fld_children) ; i ++) { | ||
226 | struct mailfolder * child; | ||
227 | |||
228 | child = carray_get(folder->fld_parent->fld_children, i); | ||
229 | child->fld_sibling_index = i; | ||
230 | } | ||
231 | |||
232 | folder->fld_parent = NULL; | ||
233 | folder->fld_sibling_index = 0; | ||
234 | |||
235 | return MAIL_NO_ERROR; | ||
236 | } | ||
237 | |||
238 | struct mailstorage * mailstorage_new(char * sto_id) | ||
239 | { | ||
240 | struct mailstorage * storage; | ||
241 | |||
242 | storage = malloc(sizeof(struct mailstorage)); | ||
243 | if (storage == NULL) | ||
244 | goto err; | ||
245 | |||
246 | if (sto_id != NULL) { | ||
247 | storage->sto_id = strdup(sto_id); | ||
248 | if (storage->sto_id == NULL) | ||
249 | goto free; | ||
250 | } | ||
251 | else | ||
252 | storage->sto_id = NULL; | ||
253 | |||
254 | storage->sto_data = NULL; | ||
255 | storage->sto_session = NULL; | ||
256 | storage->sto_driver = NULL; | ||
257 | storage->sto_shared_folders = clist_new(); | ||
258 | if (storage->sto_shared_folders == NULL) | ||
259 | goto free_id; | ||
260 | |||
261 | return storage; | ||
262 | |||
263 | free_id: | ||
264 | if (storage->sto_id != NULL) | ||
265 | free(storage->sto_id); | ||
266 | free: | ||
267 | free(storage); | ||
268 | err: | ||
269 | return NULL; | ||
270 | } | ||
271 | |||
272 | void mailstorage_free(struct mailstorage * storage) | ||
273 | { | ||
274 | if (storage->sto_session != NULL) | ||
275 | mailstorage_disconnect(storage); | ||
276 | |||
277 | if (storage->sto_driver != NULL) { | ||
278 | if (storage->sto_driver->sto_uninitialize != NULL) | ||
279 | storage->sto_driver->sto_uninitialize(storage); | ||
280 | } | ||
281 | |||
282 | clist_free(storage->sto_shared_folders); | ||
283 | |||
284 | if (storage->sto_id != NULL) | ||
285 | free(storage->sto_id); | ||
286 | |||
287 | free(storage); | ||
288 | } | ||
289 | |||
290 | int mailstorage_connect(struct mailstorage * storage) | ||
291 | { | ||
292 | if (storage->sto_session != NULL) | ||
293 | return MAIL_NO_ERROR; | ||
294 | |||
295 | if (!clist_isempty(storage->sto_shared_folders)) | ||
296 | return MAIL_ERROR_BAD_STATE; | ||
297 | |||
298 | if (storage->sto_driver->sto_connect == NULL) | ||
299 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
300 | |||
301 | return storage->sto_driver->sto_connect(storage); | ||
302 | } | ||
303 | |||
304 | |||
305 | void mailstorage_disconnect(struct mailstorage * storage) | ||
306 | { | ||
307 | int r; | ||
308 | clistiter * cur; | ||
309 | |||
310 | while ((cur = clist_begin(storage->sto_shared_folders)) != NULL) { | ||
311 | struct mailfolder * folder; | ||
312 | |||
313 | folder = cur->data; | ||
314 | mailfolder_disconnect(folder); | ||
315 | } | ||
316 | |||
317 | if (storage->sto_session == NULL) | ||
318 | return; | ||
319 | |||
320 | r = mailsession_logout(storage->sto_session); | ||
321 | |||
322 | mailsession_free(storage->sto_session); | ||
323 | storage->sto_session = NULL; | ||
324 | } | ||
325 | |||
326 | |||
327 | int mailstorage_noop(struct mailstorage * storage) | ||
328 | { | ||
329 | return mailsession_noop(storage->sto_session); | ||
330 | } | ||
331 | |||
332 | |||
333 | static int mailstorage_get_folder(struct mailstorage * storage, | ||
334 | char * pathname, mailsession ** result) | ||
335 | { | ||
336 | if (storage->sto_driver->sto_get_folder_session == NULL) | ||
337 | return MAIL_ERROR_NOT_IMPLEMENTED; | ||
338 | |||
339 | return storage->sto_driver->sto_get_folder_session(storage, | ||
340 | pathname, result); | ||
341 | } | ||
diff --git a/libetpan/src/driver/interface/mailstorage.h b/libetpan/src/driver/interface/mailstorage.h new file mode 100644 index 0000000..e8cbda3 --- a/dev/null +++ b/libetpan/src/driver/interface/mailstorage.h | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAIL_STORAGE_H | ||
37 | |||
38 | #define MAIL_STORAGE_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | #include <libetpan/mailstorage_types.h> | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | /* storage */ | ||
48 | |||
49 | /* | ||
50 | mailstorage_new | ||
51 | |||
52 | This function creates an empty storage. This storage have to be initialized. | ||
53 | The "driver" and "data" fields should be initialized. | ||
54 | |||
55 | @param id is the name of the storage. It can be NULL. | ||
56 | The given parameter is no more needed when the creation is finished. | ||
57 | The given string is duplicated. | ||
58 | |||
59 | @return The mail storage is returned. | ||
60 | */ | ||
61 | |||
62 | struct mailstorage * mailstorage_new(char * sto_id); | ||
63 | |||
64 | void mailstorage_free(struct mailstorage * storage); | ||
65 | |||
66 | /* | ||
67 | session will be initialized on success. | ||
68 | */ | ||
69 | |||
70 | int mailstorage_connect(struct mailstorage * storage); | ||
71 | |||
72 | void mailstorage_disconnect(struct mailstorage * storage); | ||
73 | |||
74 | int mailstorage_noop(struct mailstorage * storage); | ||
75 | |||
76 | |||
77 | /* folder */ | ||
78 | |||
79 | struct mailfolder * mailfolder_new(struct mailstorage * fld_storage, | ||
80 | char * fld_pathname, char * fld_virtual_name); | ||
81 | |||
82 | void mailfolder_free(struct mailfolder * folder); | ||
83 | |||
84 | int mailfolder_add_child(struct mailfolder * parent, | ||
85 | struct mailfolder * child); | ||
86 | |||
87 | int mailfolder_detach_parent(struct mailfolder * folder); | ||
88 | |||
89 | int mailfolder_connect(struct mailfolder * folder); | ||
90 | |||
91 | void mailfolder_disconnect(struct mailfolder * folder); | ||
92 | |||
93 | #ifdef __cplusplus | ||
94 | } | ||
95 | #endif | ||
96 | |||
97 | #endif | ||
98 | |||
99 | |||
diff --git a/libetpan/src/driver/interface/mailstorage_tools.c b/libetpan/src/driver/interface/mailstorage_tools.c new file mode 100644 index 0000000..9d39cab --- a/dev/null +++ b/libetpan/src/driver/interface/mailstorage_tools.c | |||
@@ -0,0 +1,372 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailstorage_tools.h" | ||
37 | |||
38 | #include "libetpan-config.h" | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <netdb.h> | ||
42 | #include <netinet/in.h> | ||
43 | #include <sys/socket.h> | ||
44 | #include <unistd.h> | ||
45 | #include <stdlib.h> | ||
46 | #include <sys/wait.h> | ||
47 | #include <sys/ioctl.h> | ||
48 | #include <fcntl.h> | ||
49 | #include <string.h> | ||
50 | |||
51 | #include "mail.h" | ||
52 | #include "mailmessage.h" | ||
53 | #include "maildriver.h" | ||
54 | |||
55 | /* tools */ | ||
56 | |||
57 | /* connection to TCP/IP server */ | ||
58 | |||
59 | static int tcp_connect(char * server, uint16_t port) | ||
60 | { | ||
61 | struct hostent * remotehost; | ||
62 | struct sockaddr_in sa; | ||
63 | int s; | ||
64 | int r; | ||
65 | |||
66 | s = socket(PF_INET, SOCK_STREAM, 0); | ||
67 | if (s == -1) | ||
68 | goto err; | ||
69 | |||
70 | remotehost = gethostbyname(server); | ||
71 | if (remotehost == NULL) | ||
72 | goto close_socket; | ||
73 | |||
74 | sa.sin_family = AF_INET; | ||
75 | sa.sin_port = htons(port); | ||
76 | memcpy(&sa.sin_addr, remotehost->h_addr, remotehost->h_length); | ||
77 | |||
78 | r = connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)); | ||
79 | if (r == -1) | ||
80 | goto close_socket; | ||
81 | |||
82 | return s; | ||
83 | |||
84 | close_socket: | ||
85 | close(s); | ||
86 | err: | ||
87 | return -1; | ||
88 | } | ||
89 | |||
90 | |||
91 | /* connection through a shell command */ | ||
92 | |||
93 | static void do_exec_command(int fd, const char *command, | ||
94 | char *servername, uint16_t port) | ||
95 | { | ||
96 | int i, maxopen; | ||
97 | |||
98 | if (fork() > 0) { | ||
99 | /* Fork again to become a child of init rather than | ||
100 | the etpan client. */ | ||
101 | exit(0); | ||
102 | } | ||
103 | |||
104 | if (servername) | ||
105 | setenv("ETPANSERVER", servername, 1); | ||
106 | else | ||
107 | unsetenv("ETPANSERVER"); | ||
108 | |||
109 | if (port) { | ||
110 | char porttext[20]; | ||
111 | |||
112 | snprintf(porttext, sizeof(porttext), "%d", port); | ||
113 | setenv("ETPANPORT", porttext, 1); | ||
114 | } | ||
115 | else { | ||
116 | unsetenv("ETPANPORT"); | ||
117 | } | ||
118 | |||
119 | /* Not a lot we can do if there's an error other than bail. */ | ||
120 | if (dup2(fd, 0) == -1) | ||
121 | exit(1); | ||
122 | if (dup2(fd, 1) == -1) | ||
123 | exit(1); | ||
124 | |||
125 | /* Should we close stderr and reopen /dev/null? */ | ||
126 | |||
127 | maxopen = sysconf(_SC_OPEN_MAX); | ||
128 | for (i=3; i < maxopen; i++) | ||
129 | close(i); | ||
130 | |||
131 | #ifdef TIOCNOTTY | ||
132 | /* Detach from the controlling tty if we have one. Otherwise, | ||
133 | SSH might do something stupid like trying to use it instead | ||
134 | of running $SSH_ASKPASS. Doh. */ | ||
135 | fd = open("/dev/tty", O_RDONLY); | ||
136 | if (fd != -1) { | ||
137 | ioctl(fd, TIOCNOTTY, NULL); | ||
138 | close(fd); | ||
139 | } | ||
140 | #endif /* TIOCNOTTY */ | ||
141 | |||
142 | execl("/bin/sh", "/bin/sh", "-c", command, NULL); | ||
143 | |||
144 | /* Eep. Shouldn't reach this */ | ||
145 | exit(1); | ||
146 | } | ||
147 | |||
148 | static int subcommand_connect(char *command, char *servername, uint16_t port) | ||
149 | { | ||
150 | int sockfds[2]; | ||
151 | pid_t childpid; | ||
152 | |||
153 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfds)) | ||
154 | return -1; | ||
155 | |||
156 | childpid = fork(); | ||
157 | if (!childpid) { | ||
158 | do_exec_command(sockfds[1], command, servername, port); | ||
159 | } | ||
160 | else if (childpid == -1) { | ||
161 | close(sockfds[0]); | ||
162 | close(sockfds[1]); | ||
163 | return -1; | ||
164 | } | ||
165 | |||
166 | close(sockfds[1]); | ||
167 | |||
168 | /* Reap child, leaving grandchild process to run */ | ||
169 | waitpid(childpid, NULL, 0); | ||
170 | |||
171 | return sockfds[0]; | ||
172 | } | ||
173 | |||
174 | int mailstorage_generic_connect(mailsession_driver * driver, | ||
175 | char * servername, | ||
176 | uint16_t port, | ||
177 | char * command, | ||
178 | int connection_type, | ||
179 | int cache_function_id, | ||
180 | char * cache_directory, | ||
181 | int flags_function_id, | ||
182 | char * flags_directory, | ||
183 | mailsession ** result) | ||
184 | { | ||
185 | int r; | ||
186 | int res; | ||
187 | mailstream * stream; | ||
188 | int fd; | ||
189 | mailsession * session; | ||
190 | int connect_result; | ||
191 | |||
192 | switch (connection_type) { | ||
193 | case CONNECTION_TYPE_PLAIN: | ||
194 | case CONNECTION_TYPE_TRY_STARTTLS: | ||
195 | case CONNECTION_TYPE_STARTTLS: | ||
196 | case CONNECTION_TYPE_TLS: | ||
197 | fd = tcp_connect(servername, port); | ||
198 | if (fd == -1) { | ||
199 | res = MAIL_ERROR_CONNECT; | ||
200 | goto err; | ||
201 | } | ||
202 | break; | ||
203 | |||
204 | case CONNECTION_TYPE_COMMAND: | ||
205 | case CONNECTION_TYPE_COMMAND_TRY_STARTTLS: | ||
206 | case CONNECTION_TYPE_COMMAND_STARTTLS: | ||
207 | case CONNECTION_TYPE_COMMAND_TLS: | ||
208 | fd = subcommand_connect(command, servername, port); | ||
209 | break; | ||
210 | |||
211 | default: | ||
212 | fd = -1; | ||
213 | break; | ||
214 | } | ||
215 | |||
216 | if (fd == -1) { | ||
217 | res = MAIL_ERROR_INVAL; | ||
218 | goto err; | ||
219 | } | ||
220 | |||
221 | switch (connection_type) { | ||
222 | case CONNECTION_TYPE_PLAIN: | ||
223 | case CONNECTION_TYPE_TRY_STARTTLS: | ||
224 | case CONNECTION_TYPE_STARTTLS: | ||
225 | case CONNECTION_TYPE_COMMAND: | ||
226 | case CONNECTION_TYPE_COMMAND_TRY_STARTTLS: | ||
227 | case CONNECTION_TYPE_COMMAND_STARTTLS: | ||
228 | stream = mailstream_socket_open(fd); | ||
229 | break; | ||
230 | |||
231 | case CONNECTION_TYPE_TLS: | ||
232 | case CONNECTION_TYPE_COMMAND_TLS: | ||
233 | stream = mailstream_ssl_open(fd); | ||
234 | break; | ||
235 | |||
236 | default: | ||
237 | stream = NULL; | ||
238 | break; | ||
239 | } | ||
240 | |||
241 | if (stream == NULL) { | ||
242 | res = MAIL_ERROR_STREAM; | ||
243 | close(fd); | ||
244 | goto err; | ||
245 | } | ||
246 | |||
247 | session = mailsession_new(driver); | ||
248 | if (session == NULL) { | ||
249 | res = MAIL_ERROR_MEMORY; | ||
250 | goto close_stream; | ||
251 | } | ||
252 | |||
253 | if (cache_directory != NULL) { | ||
254 | char cache_directory_server[PATH_MAX]; | ||
255 | |||
256 | snprintf(cache_directory_server, PATH_MAX, "%s/%s", | ||
257 | cache_directory, servername); | ||
258 | |||
259 | r = mailsession_parameters(session, | ||
260 | cache_function_id, | ||
261 | cache_directory_server); | ||
262 | if (r != MAIL_NO_ERROR) { | ||
263 | res = r; | ||
264 | goto close_stream; | ||
265 | } | ||
266 | } | ||
267 | |||
268 | if (flags_directory != NULL) { | ||
269 | char flags_directory_server[PATH_MAX]; | ||
270 | |||
271 | snprintf(flags_directory_server, PATH_MAX, "%s/%s", | ||
272 | flags_directory, servername); | ||
273 | |||
274 | r = mailsession_parameters(session, | ||
275 | flags_function_id, | ||
276 | flags_directory_server); | ||
277 | if (r != MAIL_NO_ERROR) { | ||
278 | res = r; | ||
279 | goto close_stream; | ||
280 | } | ||
281 | } | ||
282 | |||
283 | r = mailsession_connect_stream(session, stream); | ||
284 | switch (r) { | ||
285 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
286 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
287 | case MAIL_NO_ERROR: | ||
288 | break; | ||
289 | default: | ||
290 | res = r; | ||
291 | goto free; | ||
292 | } | ||
293 | |||
294 | connect_result = r; | ||
295 | |||
296 | switch (connection_type) { | ||
297 | case CONNECTION_TYPE_TRY_STARTTLS: | ||
298 | case CONNECTION_TYPE_COMMAND_TRY_STARTTLS: | ||
299 | r = mailsession_starttls(session); | ||
300 | if ((r != MAIL_NO_ERROR) && (r != MAIL_ERROR_NO_TLS)) { | ||
301 | res = r; | ||
302 | goto free; | ||
303 | } | ||
304 | break; | ||
305 | |||
306 | case CONNECTION_TYPE_STARTTLS: | ||
307 | case CONNECTION_TYPE_COMMAND_STARTTLS: | ||
308 | r = mailsession_starttls(session); | ||
309 | if (r != MAIL_NO_ERROR) { | ||
310 | res = r; | ||
311 | goto free; | ||
312 | } | ||
313 | } | ||
314 | |||
315 | * result = session; | ||
316 | |||
317 | return connect_result; | ||
318 | |||
319 | close_stream: | ||
320 | mailstream_close(stream); | ||
321 | free: | ||
322 | mailsession_free(session); | ||
323 | err: | ||
324 | return res; | ||
325 | } | ||
326 | |||
327 | |||
328 | |||
329 | |||
330 | |||
331 | int mailstorage_generic_auth(mailsession * session, | ||
332 | int connect_result, | ||
333 | int auth_type, | ||
334 | char * login, | ||
335 | char * password) | ||
336 | { | ||
337 | int must_auth; | ||
338 | int r; | ||
339 | int res; | ||
340 | |||
341 | r = connect_result; | ||
342 | |||
343 | must_auth = FALSE; | ||
344 | switch (r) { | ||
345 | case MAIL_NO_ERROR_NON_AUTHENTICATED: | ||
346 | must_auth = TRUE; | ||
347 | break; | ||
348 | case MAIL_NO_ERROR_AUTHENTICATED: | ||
349 | case MAIL_NO_ERROR: | ||
350 | break; | ||
351 | default: | ||
352 | res = r; | ||
353 | goto err; | ||
354 | } | ||
355 | |||
356 | if ((login == NULL) || (password == NULL)) | ||
357 | must_auth = FALSE; | ||
358 | |||
359 | if (must_auth) { | ||
360 | r = mailsession_login(session, login, password); | ||
361 | if (r != MAIL_NO_ERROR) { | ||
362 | mailsession_logout(session); | ||
363 | res = r; | ||
364 | goto err; | ||
365 | } | ||
366 | } | ||
367 | |||
368 | return MAIL_NO_ERROR; | ||
369 | |||
370 | err: | ||
371 | return res; | ||
372 | } | ||
diff --git a/libetpan/src/driver/interface/mailstorage_tools.h b/libetpan/src/driver/interface/mailstorage_tools.h new file mode 100644 index 0000000..113dcdf --- a/dev/null +++ b/libetpan/src/driver/interface/mailstorage_tools.h | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 "mailstorage.h" | ||
37 | |||
38 | #ifndef MAILSTORAGE_TOOLS_H | ||
39 | |||
40 | #define MAILSTORAGE_TOOLS_H | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | int mailstorage_generic_connect(mailsession_driver * driver, | ||
47 | char * servername, | ||
48 | uint16_t port, | ||
49 | char * command, | ||
50 | int connection_type, | ||
51 | int cache_function_id, | ||
52 | char * cache_directory, | ||
53 | int flags_function_id, | ||
54 | char * flags_directory, | ||
55 | mailsession ** result); | ||
56 | |||
57 | int mailstorage_generic_auth(mailsession * session, | ||
58 | int connect_result, | ||
59 | int auth_type, | ||
60 | char * login, | ||
61 | char * password); | ||
62 | |||
63 | #ifdef __cplusplus | ||
64 | } | ||
65 | #endif | ||
66 | |||
67 | #endif | ||
diff --git a/libetpan/src/driver/interface/mailstorage_types.h b/libetpan/src/driver/interface/mailstorage_types.h new file mode 100644 index 0000000..d0d18c8 --- a/dev/null +++ b/libetpan/src/driver/interface/mailstorage_types.h | |||
@@ -0,0 +1,203 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2005 - 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 AUTHORS 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 AUTHORS 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 | #ifndef MAILSTORAGE_TYPES_H | ||
37 | |||
38 | #define MAILSTORAGE_TYPES_H | ||
39 | |||
40 | #include <libetpan/maildriver_types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | struct mailstorage; | ||
47 | |||
48 | typedef struct mailstorage_driver mailstorage_driver; | ||
49 | |||
50 | |||
51 | /* | ||
52 | There is three kinds of identities : | ||
53 | - storage | ||
54 | - folders | ||
55 | - session | ||
56 | |||
57 | A storage (struct mailstorage) represents whether a server or | ||
58 | a main path, | ||
59 | |||
60 | A storage can be an IMAP server, the root path of a MH or a mbox file. | ||
61 | |||
62 | Folders (struct mailfolder) are the mailboxes we can | ||
63 | choose in the server or as sub-folder of the main path. | ||
64 | |||
65 | Folders for IMAP are the IMAP mailboxes, for MH this is one of the | ||
66 | folder of the MH storage, for mbox, there is only one folder, the | ||
67 | mbox file content; | ||
68 | |||
69 | A mail session (struct mailsession) is whether a connection to a server | ||
70 | or a path that is open. It is the abstraction lower folders and storage. | ||
71 | It allow us to send commands. | ||
72 | |||
73 | We have a session driver for mail session for each kind of storage. | ||
74 | |||
75 | From a session, we can get a message (struct mailmessage) to read. | ||
76 | We have a message driver for each kind of storage. | ||
77 | */ | ||
78 | |||
79 | /* | ||
80 | mailstorage_driver is the driver structure for mail storages | ||
81 | |||
82 | - name is the name of the driver | ||
83 | |||
84 | - connect() connects the storage to the remote access or to | ||
85 | the path in the local filesystem. | ||
86 | |||
87 | - get_folder() can have two kinds of behaviour. | ||
88 | Either it creates a new session and independant from the session | ||
89 | used by the storage and select the given mailbox or | ||
90 | it selects the given mailbox in the current session. | ||
91 | It depends on the efficiency of the mail driver. | ||
92 | |||
93 | - uninitialize() frees the data created with mailstorage constructor. | ||
94 | */ | ||
95 | |||
96 | struct mailstorage_driver { | ||
97 | char * sto_name; | ||
98 | int (* sto_connect)(struct mailstorage * storage); | ||
99 | int (* sto_get_folder_session)(struct mailstorage * storage, | ||
100 | char * pathname, mailsession ** result); | ||
101 | void (* sto_uninitialize)(struct mailstorage * storage); | ||
102 | }; | ||
103 | |||
104 | /* | ||
105 | mailstorage is the data structure for a storage | ||
106 | |||
107 | - id is the name of the storage, it can be NULL. | ||
108 | |||
109 | - data is the data specific to the driver. | ||
110 | This is the internal state of the storage. | ||
111 | |||
112 | - session is the session related to the storage. | ||
113 | |||
114 | - driver is the driver for the storage. | ||
115 | |||
116 | - shared_folders is the list of folders returned by the storage. | ||
117 | */ | ||
118 | |||
119 | struct mailstorage { | ||
120 | char * sto_id; | ||
121 | void * sto_data; | ||
122 | mailsession * sto_session; | ||
123 | mailstorage_driver * sto_driver; | ||
124 | clist * sto_shared_folders; /* list of (struct mailfolder *) */ | ||
125 | |||
126 | void * sto_user_data; | ||
127 | }; | ||
128 | |||
129 | |||
130 | |||
131 | /* | ||
132 | mailfolder is the data structure for a mailbox | ||
133 | |||
134 | - pathname is the path of the mailbox on the storage | ||
135 | |||
136 | - virtual_name is the folder identifier, it can be a path, | ||
137 | a name or NULL. | ||
138 | |||
139 | - storage is the storage to which the folder belongs to. | ||
140 | |||
141 | - session is the session related to the folder. It can be | ||
142 | different of the session of the storage. | ||
143 | |||
144 | - shared_session is != 0 if the session is the same as the | ||
145 | session of the storage. | ||
146 | |||
147 | - pos is the position of the folder in the "shared_folders" field | ||
148 | of the storage. | ||
149 | |||
150 | folders can be chained into a tree. | ||
151 | |||
152 | - parent is the parent of the folder. | ||
153 | |||
154 | - sibling_index is the index of the folder in the list of children | ||
155 | of the parent. | ||
156 | |||
157 | - children is the folder. | ||
158 | */ | ||
159 | |||
160 | struct mailfolder { | ||
161 | char * fld_pathname; | ||
162 | char * fld_virtual_name; | ||
163 | |||
164 | struct mailstorage * fld_storage; | ||
165 | |||
166 | mailsession * fld_session; | ||
167 | int fld_shared_session; | ||
168 | clistiter * fld_pos; | ||
169 | |||
170 | struct mailfolder * fld_parent; | ||
171 | unsigned int fld_sibling_index; | ||
172 | carray * fld_children; /* array of (struct mailfolder *) */ | ||
173 | |||
174 | void * fld_user_data; | ||
175 | }; | ||
176 | |||
177 | /* | ||
178 | this is the type of socket connection | ||
179 | */ | ||
180 | |||
181 | enum { | ||
182 | CONNECTION_TYPE_PLAIN, /* when the connection is plain text */ | ||
183 | CONNECTION_TYPE_STARTTLS, /* when the connection is first plain, | ||
184 | then, we want to switch to | ||
185 | TLS (secure connection) */ | ||
186 | CONNECTION_TYPE_TRY_STARTTLS, /* the connection is first plain, | ||
187 | then, we will try to switch to TLS */ | ||
188 | CONNECTION_TYPE_TLS, /* the connection is over TLS */ | ||
189 | CONNECTION_TYPE_COMMAND, /* the connection is over a shell command */ | ||
190 | CONNECTION_TYPE_COMMAND_STARTTLS, /* the connection is over a shell | ||
191 | command and STARTTLS will be used */ | ||
192 | CONNECTION_TYPE_COMMAND_TRY_STARTTLS, /* the connection is over | ||
193 | a shell command and STARTTLS will | ||
194 | be tried */ | ||
195 | CONNECTION_TYPE_COMMAND_TLS, /* the connection is over a shell | ||
196 | command in TLS */ | ||
197 | }; | ||
198 | |||
199 | #ifdef __cplusplus | ||
200 | } | ||
201 | #endif | ||
202 | |||
203 | #endif | ||