aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/rfc822/MCAttachment.cc
blob: e41339a80281d97e54e154c0d65db5f3a6419655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include "MCAttachment.h"

#include "MCMultipart.h"
#include "MCMessagePart.h"
#include "MCMessageHeader.h"
#include "MCMessageConstants.h"

#include <stdlib.h>
#include <string.h>

using namespace mailcore;

String * Attachment::mimeTypeForFilename(String * filename)
{
#warning read from a file
    String * ext;
    
    ext = filename->pathExtension()->lowercaseString();
	if (ext->isEqual(MCSTR("jpg"))) {
		return MCSTR("image/jpeg");
	}
	else if (ext->isEqual(MCSTR("jpeg"))) {
		return MCSTR("image/jpeg");
	}
	else if (ext->isEqual(MCSTR("png"))) {
		return MCSTR("image/png");
	}
	else if (ext->isEqual(MCSTR("gif"))) {
		return MCSTR("image/gif");
	}
	else if (ext->isEqual(MCSTR("html"))) {
		return MCSTR("text/html");
	}
	else if (ext->isEqual(MCSTR("txt"))) {
		return MCSTR("text/plain");
	}
	return NULL;
}

Attachment * Attachment::attachmentWithContentOfFile(String * filename)
{
    Attachment * attachment;
	String * mimeType;
	Data * data;
	
    attachment = new Attachment();
    data = Data::dataWithContentsOfFile(filename);
    mimeType = Attachment::mimeTypeForFilename(filename);
	if (mimeType != NULL) {
        attachment->setMimeType(mimeType);
	}
	attachment->setFilename(filename->lastPathComponent());
    attachment->setData(data);
	
	return (Attachment *) attachment->autorelease();
}

Attachment * Attachment::attachmentWithHTMLString(String * htmlString)
{
	Data * data;
	Attachment * attachment;
	
    attachment = new Attachment();
	attachment->setInlineAttachment(true);
    attachment->setMimeType(MCSTR("text/html"));
	data = htmlString->dataUsingEncoding("utf-8");
    attachment->setData(data);
	
	return (Attachment *) attachment->autorelease();
}

Attachment * Attachment::attachmentWithRFC822Message(Data * messageData)
{
	Attachment * attachment;
	
    attachment = new Attachment();
    attachment->setMimeType(MCSTR("message/rfc822"));
    attachment->setData(messageData);
	
	return (Attachment *) attachment->autorelease();
}

Attachment * Attachment::attachmentWithText(String * text)
{
	Data * data;
	Attachment * attachment;
	
    attachment = new Attachment();
	attachment->setInlineAttachment(true);
    attachment->setMimeType(MCSTR("text/plain"));
	data = text->dataUsingEncoding("utf-8");
    attachment->setData(data);
	
	return (Attachment *) attachment->autorelease();
}

void Attachment::init()
{
    mData = NULL;
    setMimeType(MCSTR("application/octet-stream"));
}

Attachment::Attachment()
{
    init();
}

Attachment::Attachment(Attachment * other) : AbstractPart(other)
{
    init();
    MC_SAFE_REPLACE_RETAIN(Data, mData, other->mData);
}

Attachment::~Attachment()
{
    MC_SAFE_RELEASE(mData);
}

String * Attachment::description()
{
    String * result = String::string();
    result->appendUTF8Format("<%s:%p\n", className()->UTF8Characters(), this);
    if (filename() != NULL) {
        result->appendUTF8Format("filename: %s\n", filename()->UTF8Characters());
    }
    if (mimeType() != NULL) {
        result->appendUTF8Format("mime type: %s\n", mimeType()->UTF8Characters());
    }
    if (charset() != NULL) {
        result->appendUTF8Format("charset: %s\n", charset()->UTF8Characters());
    }
    if (contentID() != NULL) {
        result->appendUTF8Format("content-ID: %s\n", contentID()->UTF8Characters());
    }
    if (contentLocation() != NULL) {
        result->appendUTF8Format("content-location: %s\n", contentLocation()->UTF8Characters());
    }
    result->appendUTF8Format("inline: %i\n", isInlineAttachment());
    if (mData != NULL) {
        result->appendUTF8Format("data: %i bytes\n", mData->length());
    }
    else {
        result->appendUTF8Format("no data\n");
    }
    result->appendUTF8Format(">");
    
    return result;
}

#if 0
String * Attachment::className()
{
    return MCSTR("Attachment");
}
#endif

Object * Attachment::copy()
{
    return new Attachment(this);
}

void Attachment::setData(Data * data)
{
    MC_SAFE_REPLACE_RETAIN(Data, mData, data);
}

Data * Attachment::data()
{
    return mData;
}

AbstractPart * Attachment::attachmentsWithMIME(struct mailmime * mime)
{
    return attachmentsWithMIMEWithMain(mime, true);
}

void Attachment::fillMultipartSubAttachments(AbstractMultipart * multipart, struct mailmime * mime)
{
	switch (mime->mm_type) {
		case MAILMIME_MULTIPLE:
		{
			clistiter * cur;
            Array * subAttachments = Array::array();
			for(cur = clist_begin(mime->mm_data.mm_multipart.mm_mp_list) ; cur != NULL ; cur = clist_next(cur)) {
				struct mailmime * submime;
                AbstractPart * subAttachment;
				
				submime = (struct mailmime *) clist_content(cur);
                subAttachment = attachmentsWithMIMEWithMain(submime, false);
				subAttachments->addObject(subAttachment);
			}
			
            multipart->setParts(subAttachments);
            break;
	    }
    }
}

AbstractPart * Attachment::attachmentsWithMIMEWithMain(struct mailmime * mime, bool isMain)
{
	switch (mime->mm_type) {
		case MAILMIME_SINGLE:
		{
			Attachment * attachment;
            attachment = attachmentWithSingleMIME(mime);
            return attachment;
		}
		case MAILMIME_MULTIPLE:
		{
			if ((mime->mm_content_type != NULL) && (mime->mm_content_type->ct_subtype != NULL) &&
			(strcasecmp(mime->mm_content_type->ct_subtype, "alternative") == 0)) {
				Multipart * attachment;
                attachment = new Multipart();
                attachment->setPartType(PartTypeMultipartAlternative);
                fillMultipartSubAttachments(attachment, mime);
                return (Multipart *) attachment->autorelease();
			}
			else if ((mime->mm_content_type != NULL) && (mime->mm_content_type->ct_subtype != NULL) &&
            (strcasecmp(mime->mm_content_type->ct_subtype, "related") == 0)) {
				Multipart * attachment;
                attachment = new Multipart();
                attachment->setPartType(PartTypeMultipartRelated);
                fillMultipartSubAttachments(attachment, mime);
                return (Multipart *) attachment->autorelease();
		    }
			else {
				Multipart * attachment;
                attachment = new Multipart();
                fillMultipartSubAttachments(attachment, mime);
                return (Multipart *) attachment->autorelease();
			}
		}
		case MAILMIME_MESSAGE:
		{
            if (isMain) {
                AbstractPart * attachment;
                attachment = attachmentsWithMIMEWithMain(mime->mm_data.mm_message.mm_msg_mime, false);
                return attachment;
            }
            else {
                MessagePart * messagePart;
                messagePart = attachmentWithMessageMIME(mime);
                return messagePart;
            }
		}
	}
	
	return NULL;
}

Encoding Attachment::encodingForMIMEEncoding(struct mailmime_mechanism * mechanism, int defaultMimeEncoding)
{
    Encoding mimeEncoding = (Encoding) defaultMimeEncoding;
    
    if (mechanism != NULL) {
        mimeEncoding = (Encoding) mechanism->enc_type;
    }
    
    switch ((int) mimeEncoding) {
        default:
        case MAILMIME_MECHANISM_ERROR:
            return EncodingOther;
        case MAILMIME_MECHANISM_7BIT:
            return Encoding7Bit;
        case MAILMIME_MECHANISM_8BIT:
            return Encoding8Bit;
        case MAILMIME_MECHANISM_BINARY:
            return EncodingBinary;
        case MAILMIME_MECHANISM_QUOTED_PRINTABLE:
            return EncodingQuotedPrintable;
        case MAILMIME_MECHANISM_BASE64:
            return EncodingBase64;
        case MAILMIME_MECHANISM_TOKEN:
            if (mechanism == NULL)
                return Encoding8Bit;
            if (mechanism->enc_token == NULL)
                return Encoding8Bit;
            
            if (strcasecmp(mechanism->enc_token, "x-uuencode") == 0) {
                return EncodingUUEncode;
            }
            else {
                return EncodingOther;
            }
    }
}

static const char * get_discrete_type(struct mailmime_discrete_type * discrete_type)
{
	switch (discrete_type->dt_type) {
		case MAILMIME_DISCRETE_TYPE_TEXT:
			return "text";
			
		case MAILMIME_DISCRETE_TYPE_IMAGE:
			return "image";
			
		case MAILMIME_DISCRETE_TYPE_AUDIO:
			return "audio";
			
		case MAILMIME_DISCRETE_TYPE_VIDEO:
			return "video";
			
		case MAILMIME_DISCRETE_TYPE_APPLICATION:
			return "application";
			
		case MAILMIME_DISCRETE_TYPE_EXTENSION:
			return discrete_type->dt_extension;
	}
	
	return NULL;
}

static const char *
get_composite_type(struct mailmime_composite_type * composite_type)
{
	switch (composite_type->ct_type) {
		case MAILMIME_COMPOSITE_TYPE_MESSAGE:
			return "message";
			
		case MAILMIME_COMPOSITE_TYPE_MULTIPART:
			return "multipart";
			
		case MAILMIME_COMPOSITE_TYPE_EXTENSION:
			return composite_type->ct_token;
	}
	
	return NULL;
}

static char * get_content_type_str(struct mailmime_content * content)
{
	const char * str;
	char * result;
	const char * subtype;
    
    if (content == NULL) {
        return strdup("unknown/unknown");
    }
    
	str = "unknown";
	
	switch (content->ct_type->tp_type) {
		case MAILMIME_TYPE_DISCRETE_TYPE:
			str = get_discrete_type(content->ct_type->tp_data.tp_discrete_type);
			break;
			
		case MAILMIME_TYPE_COMPOSITE_TYPE:
			str = get_composite_type(content->ct_type->tp_data.tp_composite_type);
			break;
	}
	
	if (str == NULL)
		str = "unknown";
	subtype = content->ct_subtype;
    if (subtype == NULL)
        subtype = "unknown";
    
	result = (char *) malloc(strlen(str) + strlen(subtype) + 2);
	strcpy(result, str);
	strcat(result, "/");
	strcat(result, subtype);
	
	return result;
}

Attachment * Attachment::attachmentWithSingleMIME(struct mailmime * mime)
{
	struct mailmime_data * data;
	const char * bytes;
	size_t length;
	Attachment * result;
	struct mailmime_single_fields single_fields;
	char * str;
	char * name;
	char * filename;
    char * content_id;
    char * loc;
	Encoding encoding;
    
	MCAssert(mime->mm_type == MAILMIME_SINGLE);
	
    result = new Attachment();
	data = mime->mm_data.mm_single;
	bytes = data->dt_data.dt_text.dt_data;
	length = data->dt_data.dt_text.dt_length;
    
	mailmime_single_fields_init(&single_fields, mime->mm_mime_fields, mime->mm_content_type);
    
    encoding = encodingForMIMEEncoding(single_fields.fld_encoding, data->dt_encoding);
    
    Data * mimeData;
    mimeData = Data::dataWithBytes(bytes, (unsigned int) length);
    mimeData = mimeData->decodedDataUsingEncoding(encoding);
    result->setData(mimeData);
	
	str = get_content_type_str(mime->mm_content_type);
	result->setMimeType(String::stringWithUTF8Characters(str));
	free(str);
	
	name = single_fields.fld_content_name;
	filename = single_fields.fld_disposition_filename;
    content_id = single_fields.fld_id;
	loc = single_fields.fld_location;
    
    MCLog("filename %s", filename);
	if (filename != NULL) {
		result->setFilename(String::stringByDecodingMIMEHeaderValue(filename));
	}
	else if (name != NULL) {
        result->setFilename(String::stringByDecodingMIMEHeaderValue(name));
	}
	if (content_id != NULL) {
        result->setContentID(String::stringWithUTF8Characters(content_id));
    }
	if (single_fields.fld_content_charset != NULL) {
        result->setCharset(String::stringByDecodingMIMEHeaderValue(single_fields.fld_content_charset));
	}
    if (loc != NULL) {
        result->setContentLocation(String::stringWithUTF8Characters(loc));
    }
    
	if (single_fields.fld_disposition != NULL) {
		if (single_fields.fld_disposition->dsp_type != NULL) {
			if (single_fields.fld_disposition->dsp_type->dsp_type == MAILMIME_DISPOSITION_TYPE_INLINE) {
                result->setInlineAttachment(true);
			}
		}
	}
	
	return (Attachment *) result->autorelease();
}

MessagePart * Attachment::attachmentWithMessageMIME(struct mailmime * mime)
{
	MessagePart * attachment;
    AbstractPart * mainPart;
	
    attachment = new MessagePart();
    attachment->header()->importIMFFields(mime->mm_data.mm_message.mm_fields);
    mainPart = attachmentsWithMIMEWithMain(mime->mm_data.mm_message.mm_msg_mime, false);
	attachment->setMainPart(mainPart);
	
	return (MessagePart *) attachment->autorelease();
}