summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/compose-msg.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/tests/compose-msg.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tests/compose-msg.c317
1 files changed, 317 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tests/compose-msg.c b/kmicromail/libetpan/tests/compose-msg.c
new file mode 100644
index 0000000..f68660b
--- a/dev/null
+++ b/kmicromail/libetpan/tests/compose-msg.c
@@ -0,0 +1,317 @@
1#include <libetpan/libetpan.h>
2#include <string.h>
3#include <stdlib.h>
4
5#define DEST_CHARSET "iso-8859-1"
6
7/* build a mime parameter */
8
9static struct mailmime_parameter *
10mailmime_param_new_with_data(char * name, char * value)
11{
12 char * param_name;
13 char * param_value;
14 struct mailmime_parameter * param;
15
16 param_name = strdup(name);
17 if (param_name == NULL)
18 goto err;
19
20 param_value = strdup(value);
21 if (param_value == NULL)
22 goto free_name;
23
24 param = mailmime_parameter_new(param_name, param_value);
25 if (param == NULL)
26 goto free_value;
27
28 return param;
29
30 free_value:
31 free(param_value);
32 free_name:
33 free(param_name);
34 err:
35 return NULL;
36}
37
38
39/* build sample fields */
40
41static struct mailimf_fields * build_fields(void)
42{
43 struct mailimf_mailbox_list * from;
44 struct mailimf_address_list * to;
45 char * subject;
46 int r;
47 struct mailimf_fields * new_fields;
48
49 /* subject field */
50
51 subject = strdup("this is a sample");
52 if (subject == NULL) {
53 goto err;
54 }
55
56 /* from field */
57
58 from = mailimf_mailbox_list_new_empty();
59 if (from == NULL) {
60 goto free_subject;
61 }
62
63 r = mailimf_mailbox_list_add_parse(from,
64 "DINH Viet Hoa <hoa@sourceforge.net>");
65 if (r != MAILIMF_NO_ERROR) {
66 goto free_from;
67 }
68
69 /* to field */
70
71 to = mailimf_address_list_new_empty();
72 if (to == NULL) {
73 goto free_from;
74 }
75
76 r = mailimf_address_list_add_parse(to,
77 "Paul <claws@thewildbeast.co.uk>");
78 if (r != MAILIMF_NO_ERROR) {
79 goto free_to;
80 }
81
82 new_fields = mailimf_fields_new_with_data(from /* from */,
83 NULL /* sender */, NULL /* reply-to */,
84 to, NULL /* cc */, NULL /* bcc */, NULL /* in-reply-to */,
85 NULL /* references */,
86 subject);
87 if (new_fields == NULL)
88 goto free_to;
89
90 return new_fields;
91
92 free_to:
93 mailimf_address_list_free(to);
94 free_from:
95 mailimf_mailbox_list_free(from);
96 free_subject:
97 free(subject);
98 err:
99 return NULL;
100}
101
102
103
104/* text is a string, build a mime part containing this string */
105
106static struct mailmime * build_body_text(char * text)
107{
108 struct mailmime_fields * mime_fields;
109 struct mailmime * mime_sub;
110 struct mailmime_content * content;
111 struct mailmime_parameter * param;
112 int r;
113
114 /* text/plain part */
115
116 mime_fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT);
117 if (mime_fields == NULL) {
118 goto err;
119 }
120
121 content = mailmime_content_new_with_str("text/plain");
122 if (content == NULL) {
123 goto free_fields;
124 }
125
126 param = mailmime_param_new_with_data("charset", DEST_CHARSET);
127 if (param == NULL) {
128 goto free_content;
129 }
130
131 r = clist_append(content->ct_parameters, param);
132 if (r < 0) {
133 mailmime_parameter_free(param);
134 goto free_content;
135 }
136
137 mime_sub = mailmime_new_empty(content, mime_fields);
138 if (mime_sub == NULL) {
139 goto free_content;
140 }
141
142 r = mailmime_set_body_text(mime_sub, text, strlen(text));
143 if (r != MAILIMF_NO_ERROR) {
144 goto free_mime;
145 }
146
147 return mime_sub;
148
149 free_mime:
150 mailmime_free(mime_sub);
151 goto err;
152 free_content:
153 mailmime_content_free(content);
154 free_fields:
155 mailmime_fields_free(mime_fields);
156 err:
157 return NULL;
158}
159
160
161/* build a mime part containing the given file */
162
163static struct mailmime * build_body_file(char * filename)
164{
165 struct mailmime_fields * mime_fields;
166 struct mailmime * mime_sub;
167 struct mailmime_content * content;
168 struct mailmime_parameter * param;
169 char * dup_filename;
170 int r;
171
172 /* text/plain part */
173
174 dup_filename = strdup(filename);
175 if (dup_filename == NULL)
176 goto err;
177
178 mime_fields =
179 mailmime_fields_new_filename(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
180 dup_filename, MAILMIME_MECHANISM_BASE64);
181 if (mime_fields == NULL)
182 goto free_dup_filename;
183
184 content = mailmime_content_new_with_str("text/plain");
185 if (content == NULL) {
186 goto free_fields;
187 }
188
189 param = mailmime_param_new_with_data("charset", DEST_CHARSET);
190 if (param == NULL) {
191 goto free_content;
192 }
193
194 r = clist_append(content->ct_parameters, param);
195 if (r < 0) {
196 mailmime_parameter_free(param);
197 goto free_content;
198 }
199
200 mime_sub = mailmime_new_empty(content, mime_fields);
201 if (mime_sub == NULL) {
202 goto free_content;
203 }
204
205 dup_filename = strdup(filename);
206 if (dup_filename == NULL)
207 goto free_mime;
208
209 r = mailmime_set_body_file(mime_sub, dup_filename);
210 if (r != MAILIMF_NO_ERROR) {
211 goto free_mime;
212 }
213
214 return mime_sub;
215
216 free_mime:
217 mailmime_free(mime_sub);
218 goto err;
219 free_content:
220 mailmime_content_free(content);
221 free_fields:
222 mailmime_fields_free(mime_fields);
223 goto err;
224 free_dup_filename:
225 free(dup_filename);
226 err:
227 return NULL;
228}
229
230
231/* build an empty message */
232
233static struct mailmime * build_message(struct mailimf_fields * fields)
234{
235 struct mailmime * mime;
236
237 /* message */
238
239 mime = mailmime_new_message_data(NULL);
240 if (mime == NULL) {
241 goto err;
242 }
243
244 mailmime_set_imf_fields(mime, fields);
245
246 return mime;
247
248 err:
249 return NULL;
250}
251
252
253int main(int argc, char ** argv)
254{
255 struct mailimf_fields * fields;
256 char * text;
257 char * filename;
258 struct mailmime * message;
259 struct mailmime * text_part;
260 struct mailmime * file_part;
261 int r;
262 int col;
263
264 if (argc < 3) {
265 printf("syntax: compose-msg \"text\" filename\n");
266 return 1;
267 }
268
269 fields = build_fields();
270 if (fields == NULL)
271 goto err;
272
273 message = build_message(fields);
274 if (message == NULL)
275 goto free_fields;
276
277 text = argv[1];
278 text_part = build_body_text(text);
279 if (text_part == NULL)
280 goto free_message;
281
282 filename = argv[2];
283 file_part = build_body_file(filename);
284 if (file_part == NULL)
285 goto free_text;
286
287 r = mailmime_smart_add_part(message, text_part);
288 if (r != MAILIMF_NO_ERROR)
289 goto free_file;
290
291 r = mailmime_smart_add_part(message, file_part);
292 if (r != MAILIMF_NO_ERROR)
293 goto free_file_alone;
294
295 col = 0;
296 mailmime_write(stdout, &col, message);
297
298 mailmime_free(message);
299
300 return 0;
301
302 free_file_alone:
303 mailmime_free(file_part);
304 goto free_text;
305 free_file:
306 mailmime_free(file_part);
307 free_text:
308 mailmime_free(text_part);
309 free_message:
310 mailmime_free(message);
311 goto err;
312 free_fields:
313 mailimf_fields_free(fields);
314 err:
315 printf("error memory\n");
316 return 1;
317}