summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/fetch-attachment.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/tests/fetch-attachment.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tests/fetch-attachment.c274
1 files changed, 274 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tests/fetch-attachment.c b/kmicromail/libetpan/tests/fetch-attachment.c
new file mode 100644
index 0000000..a79ef8b
--- a/dev/null
+++ b/kmicromail/libetpan/tests/fetch-attachment.c
@@ -0,0 +1,274 @@
1#include <unistd.h>
2#include <string.h>
3#include <stdlib.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6
7#include <libetpan/libetpan.h>
8
9#include "option-parser.h"
10#include "readmsg-common.h"
11
12/* write content to the given filename */
13
14static int etpan_write_data(char * filename, char * data, size_t len)
15{
16 size_t write_len;
17 FILE * f;
18 int res;
19 mode_t old_umask;
20
21 old_umask = umask(0077);
22 f = fopen(filename, "w");
23 umask(old_umask);
24 if (f == NULL) {
25 res = ERROR_FILE;
26 goto err;
27 }
28
29 write_len = fwrite(data, 1, len, f);
30 if (write_len < len) {
31 res = ERROR_FILE;
32 goto close;
33 }
34
35 fclose(f);
36
37 return NO_ERROR;
38
39 close:
40 fclose(f);
41 err:
42 return res;
43}
44
45
46/* save attachment */
47
48static int save_mime_content(mailmessage * msg_info,
49 struct mailmime * mime_part)
50{
51 char * body;
52 size_t body_len;
53 int r;
54 char * filename;
55 struct mailmime_single_fields fields;
56 int res;
57
58 memset(&fields, 0, sizeof(struct mailmime_single_fields));
59 if (mime_part->mm_mime_fields != NULL)
60 mailmime_single_fields_init(&fields, mime_part->mm_mime_fields,
61 mime_part->mm_content_type);
62
63 filename = fields.fld_disposition_filename;
64
65 if (filename == NULL)
66 filename = fields.fld_content_name;
67
68 if (filename == NULL)
69 return ERROR_INVAL;
70
71 r = etpan_fetch_message(msg_info, mime_part, &fields, &body, &body_len);
72 if (r != NO_ERROR) {
73 res = r;
74 goto err;
75 }
76
77 printf("writing %s, %i bytes\n", filename, body_len);
78
79 r = etpan_write_data(filename, body, body_len);
80 if (r != NO_ERROR) {
81 res = r;
82 goto free;
83 }
84
85 mailmime_decoded_part_free(body);
86
87 return NO_ERROR;
88
89 free:
90 mailmime_decoded_part_free(body);
91 err:
92 return res;
93}
94
95
96
97/* fetch attachments */
98
99static int etpan_fetch_mime(FILE * f, mailmessage * msg_info,
100 struct mailmime * mime)
101{
102 int r;
103 clistiter * cur;
104 struct mailmime_single_fields fields;
105 int res;
106
107 memset(&fields, 0, sizeof(struct mailmime_single_fields));
108 if (mime->mm_mime_fields != NULL)
109 mailmime_single_fields_init(&fields, mime->mm_mime_fields,
110 mime->mm_content_type);
111
112 switch(mime->mm_type) {
113 case MAILMIME_SINGLE:
114 save_mime_content(msg_info, mime);
115
116 break;
117
118 case MAILMIME_MULTIPLE:
119
120 for(cur = clist_begin(mime->mm_data.mm_multipart.mm_mp_list) ;
121 cur != NULL ; cur = clist_next(cur)) {
122
123 r = etpan_fetch_mime(f, msg_info, clist_content(cur));
124 if (r != NO_ERROR) {
125 res = r;
126 goto err;
127 }
128 }
129
130 break;
131
132 case MAILMIME_MESSAGE:
133
134 if (mime->mm_data.mm_message.mm_msg_mime != NULL) {
135 r = etpan_fetch_mime(f, msg_info, mime->mm_data.mm_message.mm_msg_mime);
136 if (r != NO_ERROR) {
137 res = r;
138 goto err;
139 }
140 }
141
142 break;
143 }
144
145 return NO_ERROR;
146
147 err:
148 return res;
149}
150
151
152int main(int argc, char ** argv)
153{
154 int r;
155 int driver;
156 char * server;
157 int port;
158 int connection_type;
159 char * user;
160 char * password;
161 int auth_type;
162 char * path;
163 char * cache_directory;
164 char * flags_directory;
165 struct mailstorage * storage;
166 int cached;
167 struct mailfolder * folder;
168
169 /* get options */
170
171 r = parse_options(argc, argv,
172 &driver, &server, &port, &connection_type,
173 &user, &password, &auth_type,
174 &path, &cache_directory, &flags_directory);
175
176 cached = (cache_directory != NULL);
177
178 /* build the storage structure */
179
180 storage = mailstorage_new(NULL);
181 if (storage == NULL) {
182 printf("error initializing storage\n");
183 goto free_opt;
184 }
185
186 r = init_storage(storage, driver, server, port, connection_type,
187 user, password, auth_type, path, cache_directory, flags_directory);
188 if (r != MAIL_NO_ERROR) {
189 printf("error initializing storage\n");
190 goto free_opt;
191 }
192
193 /* get the folder structure */
194
195 folder = mailfolder_new(storage, path, NULL);
196 if (folder == NULL) {
197 printf("error initializing folder\n");
198 goto free_storage;
199 }
200
201 r = mailfolder_connect(folder);
202 if (r != MAIL_NO_ERROR) {
203 printf("error initializing folder\n");
204 goto free_folder;
205 }
206
207 while (optind < argc) {
208 mailmessage * msg;
209 uint32_t msg_num;
210 struct mailmime * mime;
211
212 msg_num = strtoul(argv[optind], NULL, 10);
213
214 r = mailsession_get_message(folder->fld_session, msg_num, &msg);
215 if (r != MAIL_NO_ERROR) {
216 printf("** message %i not found ** - %s\n", msg_num,
217 maildriver_strerror(r));
218 optind ++;
219 continue;
220 }
221
222 r = mailmessage_get_bodystructure(msg, &mime);
223 if (r != MAIL_NO_ERROR) {
224 printf("** message %i not found - %s **\n", msg_num,
225 maildriver_strerror(r));
226 mailmessage_free(msg);
227 optind ++;
228 continue;
229 }
230
231 r = etpan_fetch_mime(stdout, msg, mime);
232
233 mailmessage_free(msg);
234
235 optind ++;
236 }
237
238 mailfolder_free(folder);
239 mailstorage_free(storage);
240
241 if (server != NULL)
242 free(server);
243 if (user != NULL)
244 free(user);
245 if (password != NULL)
246 free(password);
247 if (path != NULL)
248 free(path);
249 if (cache_directory != NULL)
250 free(cache_directory);
251 if (flags_directory != NULL)
252 free(flags_directory);
253
254 return 0;
255
256 free_folder:
257 mailfolder_free(folder);
258 free_storage:
259 mailstorage_free(storage);
260 free_opt:
261 if (server != NULL)
262 free(server);
263 if (user != NULL)
264 free(user);
265 if (password != NULL)
266 free(password);
267 if (path != NULL)
268 free(path);
269 if (cache_directory != NULL)
270 free(cache_directory);
271 if (flags_directory != NULL)
272 free(flags_directory);
273 return -1;
274}