Diffstat (limited to 'libetpan/src/low-level/mbox/mailmbox_types.c') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libetpan/src/low-level/mbox/mailmbox_types.c | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/libetpan/src/low-level/mbox/mailmbox_types.c b/libetpan/src/low-level/mbox/mailmbox_types.c new file mode 100644 index 0000000..aaf76dc --- a/dev/null +++ b/libetpan/src/low-level/mbox/mailmbox_types.c | |||
@@ -0,0 +1,251 @@ | |||
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 "mailmbox_types.h" | ||
37 | |||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | #ifndef TRUE | ||
42 | #define TRUE 1 | ||
43 | #endif | ||
44 | |||
45 | #ifndef FALSE | ||
46 | #define FALSE 0 | ||
47 | #endif | ||
48 | |||
49 | /* *********************************************************************** */ | ||
50 | |||
51 | int mailmbox_msg_info_update(struct mailmbox_folder * folder, | ||
52 | size_t msg_start, size_t msg_start_len, | ||
53 | size_t msg_headers, size_t msg_headers_len, | ||
54 | size_t msg_body, size_t msg_body_len, | ||
55 | size_t msg_size, size_t msg_padding, | ||
56 | uint32_t msg_uid) | ||
57 | { | ||
58 | struct mailmbox_msg_info * info; | ||
59 | int res; | ||
60 | chashdatum key; | ||
61 | chashdatum data; | ||
62 | int r; | ||
63 | |||
64 | key.data = &msg_uid; | ||
65 | key.len = sizeof(msg_uid); | ||
66 | r = chash_get(folder->mb_hash, &key, &data); | ||
67 | if (r < 0) { | ||
68 | unsigned int index; | ||
69 | |||
70 | info = mailmbox_msg_info_new(msg_start, msg_start_len, | ||
71 | msg_headers, msg_headers_len, | ||
72 | msg_body, msg_body_len, msg_size, msg_padding, msg_uid); | ||
73 | if (info == NULL) { | ||
74 | res = MAILMBOX_ERROR_MEMORY; | ||
75 | goto err; | ||
76 | } | ||
77 | |||
78 | r = carray_add(folder->mb_tab, info, &index); | ||
79 | if (r < 0) { | ||
80 | mailmbox_msg_info_free(info); | ||
81 | res = MAILMBOX_ERROR_MEMORY; | ||
82 | goto err; | ||
83 | } | ||
84 | |||
85 | if (msg_uid != 0) { | ||
86 | chashdatum key; | ||
87 | chashdatum data; | ||
88 | |||
89 | key.data = &msg_uid; | ||
90 | key.len = sizeof(msg_uid); | ||
91 | data.data = info; | ||
92 | data.len = 0; | ||
93 | |||
94 | r = chash_set(folder->mb_hash, &key, &data, NULL); | ||
95 | if (r < 0) { | ||
96 | mailmbox_msg_info_free(info); | ||
97 | carray_delete(folder->mb_tab, index); | ||
98 | res = MAILMBOX_ERROR_MEMORY; | ||
99 | goto err; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | info->msg_index = index; | ||
104 | } | ||
105 | else { | ||
106 | info = data.data; | ||
107 | |||
108 | info->msg_start = msg_start; | ||
109 | info->msg_start_len = msg_start_len; | ||
110 | info->msg_headers = msg_headers; | ||
111 | info->msg_headers_len = msg_headers_len; | ||
112 | info->msg_body = msg_body; | ||
113 | info->msg_body_len = msg_body_len; | ||
114 | info->msg_size = msg_size; | ||
115 | info->msg_padding = msg_padding; | ||
116 | } | ||
117 | |||
118 | return MAILMBOX_NO_ERROR; | ||
119 | |||
120 | err: | ||
121 | return res; | ||
122 | } | ||
123 | |||
124 | |||
125 | struct mailmbox_msg_info * | ||
126 | mailmbox_msg_info_new(size_t msg_start, size_t msg_start_len, | ||
127 | size_t msg_headers, size_t msg_headers_len, | ||
128 | size_t msg_body, size_t msg_body_len, | ||
129 | size_t msg_size, size_t msg_padding, | ||
130 | uint32_t msg_uid) | ||
131 | { | ||
132 | struct mailmbox_msg_info * info; | ||
133 | |||
134 | info = malloc(sizeof(* info)); | ||
135 | if (info == NULL) | ||
136 | return NULL; | ||
137 | |||
138 | info->msg_index = 0; | ||
139 | info->msg_uid = msg_uid; | ||
140 | if (msg_uid != 0) | ||
141 | info->msg_written_uid = TRUE; | ||
142 | else | ||
143 | info->msg_written_uid = FALSE; | ||
144 | info->msg_deleted = FALSE; | ||
145 | |||
146 | info->msg_start = msg_start; | ||
147 | info->msg_start_len = msg_start_len; | ||
148 | |||
149 | info->msg_headers = msg_headers; | ||
150 | info->msg_headers_len = msg_headers_len; | ||
151 | |||
152 | info->msg_body = msg_body; | ||
153 | info->msg_body_len = msg_body_len; | ||
154 | |||
155 | info->msg_size = msg_size; | ||
156 | |||
157 | info->msg_padding = msg_padding; | ||
158 | |||
159 | return info; | ||
160 | } | ||
161 | |||
162 | void mailmbox_msg_info_free(struct mailmbox_msg_info * info) | ||
163 | { | ||
164 | free(info); | ||
165 | } | ||
166 | |||
167 | |||
168 | /* append info */ | ||
169 | |||
170 | struct mailmbox_append_info * | ||
171 | mailmbox_append_info_new(const char * ai_message, size_t ai_size) | ||
172 | { | ||
173 | struct mailmbox_append_info * info; | ||
174 | |||
175 | info = malloc(sizeof(* info)); | ||
176 | if (info == NULL) | ||
177 | return NULL; | ||
178 | |||
179 | info->ai_message = ai_message; | ||
180 | info->ai_size = ai_size; | ||
181 | info->ai_uid = 0; | ||
182 | |||
183 | return info; | ||
184 | } | ||
185 | |||
186 | void mailmbox_append_info_free(struct mailmbox_append_info * info) | ||
187 | { | ||
188 | free(info); | ||
189 | } | ||
190 | |||
191 | struct mailmbox_folder * mailmbox_folder_new(const char * mb_filename) | ||
192 | { | ||
193 | struct mailmbox_folder * folder; | ||
194 | |||
195 | folder = malloc(sizeof(* folder)); | ||
196 | if (folder == NULL) | ||
197 | goto err; | ||
198 | |||
199 | strncpy(folder->mb_filename, mb_filename, PATH_MAX); | ||
200 | |||
201 | folder->mb_mtime = (time_t) -1; | ||
202 | |||
203 | folder->mb_fd = -1; | ||
204 | folder->mb_read_only = TRUE; | ||
205 | folder->mb_no_uid = TRUE; | ||
206 | |||
207 | folder->mb_changed = FALSE; | ||
208 | folder->mb_deleted_count = 0; | ||
209 | |||
210 | folder->mb_mapping = NULL; | ||
211 | folder->mb_mapping_size = 0; | ||
212 | |||
213 | folder->mb_written_uid = 0; | ||
214 | folder->mb_max_uid = 0; | ||
215 | |||
216 | folder->mb_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY); | ||
217 | if (folder->mb_hash == NULL) | ||
218 | goto free; | ||
219 | |||
220 | folder->mb_tab = carray_new(128); | ||
221 | if (folder->mb_tab == NULL) | ||
222 | goto free_hash; | ||
223 | |||
224 | return folder; | ||
225 | |||
226 | free_hash: | ||
227 | chash_free(folder->mb_hash); | ||
228 | free: | ||
229 | free(folder); | ||
230 | err: | ||
231 | return NULL; | ||
232 | } | ||
233 | |||
234 | void mailmbox_folder_free(struct mailmbox_folder * folder) | ||
235 | { | ||
236 | unsigned int i; | ||
237 | |||
238 | for(i = 0 ; i < carray_count(folder->mb_tab) ; i++) { | ||
239 | struct mailmbox_msg_info * info; | ||
240 | |||
241 | info = carray_get(folder->mb_tab, i); | ||
242 | if (info != NULL) | ||
243 | mailmbox_msg_info_free(info); | ||
244 | } | ||
245 | |||
246 | carray_free(folder->mb_tab); | ||
247 | |||
248 | chash_free(folder->mb_hash); | ||
249 | |||
250 | free(folder); | ||
251 | } | ||