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: maildriver_types.c,v 1.28 2006/06/16 09:23:38 smarinier Exp $
34 */
35
36 #ifdef HAVE_CONFIG_H
37 # include <config.h>
38 #endif
39
40 #include "maildriver_types.h"
41 #include <time.h>
42 #include <stdlib.h>
43 #include "mailmessage.h"
44
45 LIBETPAN_EXPORT
mailmessage_list_new(carray * msg_tab)46 struct mailmessage_list * mailmessage_list_new(carray * msg_tab)
47 {
48 struct mailmessage_list * env_list;
49
50 env_list = malloc(sizeof(* env_list));
51 if (env_list == NULL)
52 return NULL;
53
54 env_list->msg_tab = msg_tab;
55
56 return env_list;
57 }
58
59 LIBETPAN_EXPORT
mailmessage_list_free(struct mailmessage_list * env_list)60 void mailmessage_list_free(struct mailmessage_list * env_list)
61 {
62 unsigned int i;
63
64 for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
65 mailmessage * msg;
66
67 msg = carray_get(env_list->msg_tab, i);
68 if (msg != NULL)
69 mailmessage_free(msg);
70 }
71 carray_free(env_list->msg_tab);
72 free(env_list);
73 }
74
75 LIBETPAN_EXPORT
mail_list_new(clist * list)76 struct mail_list * mail_list_new(clist * list)
77 {
78 struct mail_list * resp;
79
80 resp = malloc(sizeof(* resp));
81 if (resp == NULL)
82 return NULL;
83 resp->mb_list = list;
84
85 return resp;
86 }
87
88 LIBETPAN_EXPORT
mail_list_free(struct mail_list * resp)89 void mail_list_free(struct mail_list * resp)
90 {
91 clist_foreach(resp->mb_list, (clist_func) free, NULL);
92 clist_free(resp->mb_list);
93 free(resp);
94 }
95
mailimf_date_time_to_int(struct mailimf_date_time * date)96 static int32_t mailimf_date_time_to_int(struct mailimf_date_time * date)
97 {
98 return date->dt_year * 12 * 30 * 24 * 60 * 60 +
99 date->dt_month * 30 * 24 * 60 * 60 + date->dt_day * 24 * 60 * 60 +
100 (date->dt_hour - date->dt_zone) * 60 * 60 +
101 date->dt_min * 60 + date->dt_sec;
102 }
103
104 LIBETPAN_EXPORT
mailimf_date_time_comp(struct mailimf_date_time * date1,struct mailimf_date_time * date2)105 int32_t mailimf_date_time_comp(struct mailimf_date_time * date1,
106 struct mailimf_date_time * date2)
107 {
108 return mailimf_date_time_to_int(date1) - mailimf_date_time_to_int(date2);
109 }
110
111
112
113
114
115
116
117 #if 0
118 struct mail_search_key *
119 mail_search_key_new(int sk_type,
120 char * sk_bcc,
121 struct mailimf_date_time * sk_before,
122 char * sk_body,
123 char * sk_cc,
124 char * sk_from,
125 struct mailimf_date_time * sk_on,
126 struct mailimf_date_time * sk_since,
127 char * sk_subject,
128 char * sk_text,
129 char * sk_to,
130 char * sk_header_name,
131 char * sk_header_value,
132 size_t sk_larger,
133 struct mail_search_key * sk_not,
134 struct mail_search_key * sk_or1,
135 struct mail_search_key * sk_or2,
136 size_t sk_smaller,
137 clist * sk_multiple)
138 {
139 struct mail_search_key * key;
140
141 key = malloc(sizeof(* key));
142 if (key == NULL)
143 return NULL;
144
145 key->sk_type = sk_type;
146 key->sk_bcc = sk_bcc;
147 key->sk_before = sk_before;
148 key->sk_body = sk_body;
149 key->sk_cc = sk_cc;
150 key->sk_from = sk_from;
151 key->sk_on = sk_on;
152 key->sk_since = sk_since;
153 key->sk_subject = sk_subject;
154 key->sk_text = sk_text;
155 key->sk_to = sk_to;
156 key->sk_header_name = sk_header_name;
157 key->sk_header_value = sk_header_value;
158 key->sk_larger = sk_larger;
159 key->sk_not = sk_not;
160 key->sk_or1 = sk_or1;
161 key->sk_or2 = sk_or2;
162 key->sk_smaller = sk_smaller;
163 key->sk_multiple = sk_multiple;
164
165 return key;
166 }
167
168
169 void mail_search_key_free(struct mail_search_key * key)
170 {
171 if (key->sk_bcc)
172 free(key->sk_bcc);
173 if (key->sk_before)
174 mailimf_date_time_free(key->sk_before);
175 if (key->sk_body)
176 free(key->sk_body);
177 if (key->sk_cc)
178 free(key->sk_cc);
179 if (key->sk_from)
180 free(key->sk_from);
181 if (key->sk_on)
182 mailimf_date_time_free(key->sk_on);
183 if (key->sk_since)
184 mailimf_date_time_free(key->sk_since);
185 if (key->sk_subject)
186 free(key->sk_subject);
187 if (key->sk_text)
188 free(key->sk_text);
189 if (key->sk_to)
190 free(key->sk_to);
191 if (key->sk_header_name)
192 free(key->sk_header_name);
193 if (key->sk_header_value)
194 free(key->sk_header_value);
195 if (key->sk_not)
196 mail_search_key_free(key->sk_not);
197 if (key->sk_or1)
198 mail_search_key_free(key->sk_or1);
199 if (key->sk_or2)
200 mail_search_key_free(key->sk_or2);
201 if (key->sk_multiple) {
202 clist_foreach(key->sk_multiple, (clist_func) mail_search_key_free, NULL);
203 clist_free(key->sk_multiple);
204 }
205
206 free(key);
207 }
208
209
210 struct mail_search_result * mail_search_result_new(clist * list)
211 {
212 struct mail_search_result * search_result;
213
214 search_result = malloc(sizeof(* search_result));
215 if (search_result == NULL)
216 return NULL;
217 search_result->list = list;
218
219 return search_result;
220 }
221
222 void mail_search_result_free(struct mail_search_result * search_result)
223 {
224 clist_foreach(search_result->list, (clist_func) free, NULL);
225 clist_free(search_result->list);
226 free(search_result);
227 }
228 #endif
229
230 struct error_message {
231 int code;
232 char * message;
233 };
234
235 static struct error_message message_tab[] = {
236 { MAIL_NO_ERROR, "no error" },
237 { MAIL_NO_ERROR_AUTHENTICATED, "no error - authenticated" },
238 { MAIL_NO_ERROR_NON_AUTHENTICATED, "no error - not authenticated" },
239 { MAIL_ERROR_NOT_IMPLEMENTED, "not implemented" },
240 { MAIL_ERROR_UNKNOWN, "unknown"},
241 { MAIL_ERROR_CONNECT, "connect"},
242 { MAIL_ERROR_BAD_STATE, "bad state"},
243 { MAIL_ERROR_FILE, "file error - file could not be accessed" },
244 { MAIL_ERROR_STREAM, "stream error - socket could not be read or written" },
245 { MAIL_ERROR_LOGIN, "login error" },
246 { MAIL_ERROR_CREATE, "create error" },
247 { MAIL_ERROR_DELETE, /* 10 */ "delete error" },
248 { MAIL_ERROR_LOGOUT, "logout error" },
249 { MAIL_ERROR_NOOP, "noop error" },
250 { MAIL_ERROR_RENAME, "rename error" },
251 { MAIL_ERROR_CHECK, "check error" },
252 { MAIL_ERROR_EXAMINE, "examine error" },
253 { MAIL_ERROR_SELECT, "select error - folder does not exist" },
254 { MAIL_ERROR_MEMORY, "not enough memory" },
255 { MAIL_ERROR_STATUS, "status error" },
256 { MAIL_ERROR_SUBSCRIBE, "subscribe error" },
257 { MAIL_ERROR_UNSUBSCRIBE, /* 20 */ "unsubscribe error" },
258 { MAIL_ERROR_LIST, "list error" },
259 { MAIL_ERROR_LSUB, "lsub error" },
260 { MAIL_ERROR_APPEND, "append error - mail could not be appended" },
261 { MAIL_ERROR_COPY, "copy error" },
262 { MAIL_ERROR_FETCH, "fetch error" },
263 { MAIL_ERROR_STORE, "store error" },
264 { MAIL_ERROR_SEARCH, "search error" },
265 { MAIL_ERROR_DISKSPACE, " error: not enough diskspace" },
266 { MAIL_ERROR_MSG_NOT_FOUND, "message not found" },
267 { MAIL_ERROR_PARSE, /* 30 */ "parse error" },
268 { MAIL_ERROR_INVAL, "invalid parameter for the function" },
269 { MAIL_ERROR_PART_NOT_FOUND, "mime part of the message is not found" },
270 { MAIL_ERROR_REMOVE, "remove error - the message did not exist" },
271 { MAIL_ERROR_FOLDER_NOT_FOUND, "folder not found" },
272 { MAIL_ERROR_MOVE, "move error" },
273 { MAIL_ERROR_STARTTLS, "starttls error" },
274 { MAIL_ERROR_CACHE_MISS, "mail cache missed" },
275 { MAIL_ERROR_NO_TLS, "no starttls" },
276 { MAIL_ERROR_EXPUNGE, "expunge error" },
277 { MAIL_ERROR_PROTOCOL, "protocol error - server did not respect the protocol" },
278 { MAIL_ERROR_CAPABILITY, "capability error" },
279 { MAIL_ERROR_CLOSE, "close error" },
280 { MAIL_ERROR_FATAL, "fatal error" },
281 { MAIL_ERROR_READONLY, "mailbox is readonly" },
282 { MAIL_ERROR_NO_APOP, "pop3 error - no apop" },
283 { MAIL_ERROR_COMMAND_NOT_SUPPORTED, "nntp error - command not supported" },
284 { MAIL_ERROR_NO_PERMISSION, "nntp error - no permission" },
285 { MAIL_ERROR_PROGRAM_ERROR, "nntp error - program error" },
286 { MAIL_ERROR_SUBJECT_NOT_FOUND, "internal threading error - subject not found" }};
287
288 LIBETPAN_EXPORT
maildriver_strerror(int err)289 const char * maildriver_strerror(int err)
290 {
291 int count;
292 int i;
293
294 count = sizeof(message_tab) / sizeof(struct error_message);
295
296 for(i = 0 ; i < count ; i++) {
297 if (message_tab[i].code == err) {
298 return message_tab[i].message;
299 }
300 }
301
302 return "unknown error";
303 }
304
305 LIBETPAN_EXPORT
mail_flags_new_empty(void)306 struct mail_flags * mail_flags_new_empty(void)
307 {
308 struct mail_flags * flags;
309
310 flags = malloc(sizeof(* flags));
311 if (flags == NULL)
312 goto err;
313
314 flags->fl_flags = MAIL_FLAG_NEW;
315 flags->fl_extension = clist_new();
316 if (flags->fl_extension == NULL)
317 goto free;
318
319 return flags;
320
321 free:
322 free(flags);
323 err:
324 return NULL;
325 }
326
327 LIBETPAN_EXPORT
mail_flags_new(uint32_t fl_flags,clist * fl_extension)328 struct mail_flags * mail_flags_new(uint32_t fl_flags, clist * fl_extension)
329 {
330 struct mail_flags * flags;
331
332 flags = malloc(sizeof(* flags));
333 if (flags == NULL)
334 goto err;
335
336 flags->fl_flags = fl_flags;
337 flags->fl_extension = fl_extension;
338
339 return flags;
340
341 err:
342 return NULL;
343 }
344
345 LIBETPAN_EXPORT
mail_flags_free(struct mail_flags * flags)346 void mail_flags_free(struct mail_flags * flags)
347 {
348 clist_foreach(flags->fl_extension, (clist_func) free, NULL);
349 clist_free(flags->fl_extension);
350 free(flags);
351 }
352
353 LIBETPAN_EXPORT
libetpan_malloc(size_t length)354 void *libetpan_malloc(size_t length) {
355 return malloc( length);
356 }
357
358 LIBETPAN_EXPORT
libetpan_free(void * data)359 void libetpan_free(void* data) {
360 free( data);
361 }
362