aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/imap/MCIMAPPart.cc
blob: 14b23c3af37e4d7aeaff05cdbc9a6c3a4466912a (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
#include "MCIMAPPart.h"

#include <string.h>
#include <libetpan/libetpan.h>

#include "MCIMAPMessagePart.h"
#include "MCIMAPMultipart.h"
#include "MCMessageHeader.h"

using namespace mailcore;

void IMAPPart::init()
{
    mPartID = NULL;
	mEncoding = Encoding8Bit;
    mSize = 0;
}

IMAPPart::IMAPPart()
{
    init();
}

IMAPPart::IMAPPart(IMAPPart * other) : AbstractPart(other)
{
    init();
    MC_SAFE_REPLACE_COPY(String, mPartID, other->mPartID);
	mEncoding = other->mEncoding;
    mSize = other->mSize;
}

IMAPPart::~IMAPPart()
{
    MC_SAFE_RELEASE(mPartID);
}

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

void IMAPPart::setPartID(String * partID)
{
    MC_SAFE_REPLACE_COPY(String, mPartID, partID);
}

String * IMAPPart::partID()
{
    return mPartID;
}

void IMAPPart::setSize(unsigned int size)
{
    mSize = size;
}

unsigned int IMAPPart::size()
{
    return mSize;
}

void IMAPPart::setEncoding(Encoding encoding)
{
    mEncoding = encoding;
}

Encoding IMAPPart::encoding()
{
    return mEncoding;
}

unsigned int IMAPPart::decodedSize()
{
	switch (mEncoding) {
		case MAILIMAP_BODY_FLD_ENC_BASE64:
            return mSize * 3 / 4;
            
        default:
            return mSize;
	}
}

AbstractPart * IMAPPart::attachmentWithIMAPBody(struct mailimap_body * body)
{
    String * partID;
    AbstractPart * result;
    
    partID = NULL;
	if (body->bd_type == MAILIMAP_BODY_1PART) {
        partID = MCSTR("1");
    }
    result = attachmentWithIMAPBodyInternal(body, partID);
    result->applyUniquePartID();
    
    return result;
}

AbstractPart * IMAPPart::attachmentWithIMAPBodyInternal(struct mailimap_body * body, String * partID)
{
	switch (body->bd_type) {
		case MAILIMAP_BODY_1PART:
        return attachmentWithIMAPBody1Part(body->bd_data.bd_body_1part, partID);
		case MAILIMAP_BODY_MPART:
        return attachmentWithIMAPBodyMultipart(body->bd_data.bd_body_mpart, partID);
	}
	
    return NULL;
}

AbstractPart * IMAPPart::attachmentWithIMAPBody1Part(struct mailimap_body_type_1part * body_1part,
    String * partID)
{
	switch (body_1part->bd_type) {
		case MAILIMAP_BODY_TYPE_1PART_BASIC:
		{
			IMAPPart * attachment;
            
			attachment = attachmentWithIMAPBody1PartBasic(body_1part->bd_data.bd_type_basic,
                body_1part->bd_ext_1part);
            attachment->setPartID(partID);
            return attachment;
		}
		case MAILIMAP_BODY_TYPE_1PART_MSG:
		{
			return attachmentWithIMAPBody1PartMessage(body_1part->bd_data.bd_type_msg,
                body_1part->bd_ext_1part, partID);
		}
		case MAILIMAP_BODY_TYPE_1PART_TEXT:
		{
			IMAPPart * attachment;
			
			attachment = attachmentWithIMAPBody1PartText(body_1part->bd_data.bd_type_text,
                body_1part->bd_ext_1part);
            attachment->setPartID(partID);
            MCLog("attachment %s", MCUTF8(partID));
            return attachment;
		}
	}
	
	return NULL;
}

IMAPMessagePart * IMAPPart::attachmentWithIMAPBody1PartMessage(struct mailimap_body_type_msg * message,
    struct mailimap_body_ext_1part * extension,
    String * partID)
{
	IMAPMessagePart * attachment;
	AbstractPart * subAttachment;
    String * nextPartID;
    
    nextPartID = NULL;
    if (message->bd_body->bd_type == MAILIMAP_BODY_1PART) {
        // msg or 1part
        nextPartID = partID->stringByAppendingUTF8Format(".1");
    }
    else if (message->bd_body->bd_type == MAILIMAP_BODY_MPART) {
        // mpart
        nextPartID = partID;
    }
	
    attachment = new IMAPMessagePart();
    attachment->setPartID(partID);
    attachment->header()->importIMAPEnvelope(message->bd_envelope);
    attachment->importIMAPFields(message->bd_fields, extension);

    subAttachment = attachmentWithIMAPBodyInternal(message->bd_body, nextPartID);
    attachment->setMainPart(subAttachment);
    attachment->setMimeType(MCSTR("message/rfc822"));

    return (IMAPMessagePart *) attachment->autorelease();
}

void IMAPPart::importIMAPFields(struct mailimap_body_fields * fields,
    struct mailimap_body_ext_1part * extension)
{
    AbstractPart::importIMAPFields(fields, extension);
    
    setSize(fields->bd_size);
    if (fields->bd_encoding != NULL) {
        bool isUUEncode;
        
        isUUEncode = false;
        if (fields->bd_encoding->enc_type == MAILIMAP_BODY_FLD_ENC_OTHER) {
            if (strcasecmp(fields->bd_encoding->enc_value, "x-uuencode") == 0) {
                isUUEncode = true;
            }
        }
        if (isUUEncode) {
            setEncoding(EncodingUUEncode);
        }
        else {
            setEncoding((Encoding) fields->bd_encoding->enc_type);
        }
    }
}

IMAPPart * IMAPPart::attachmentWithIMAPBody1PartBasic(struct mailimap_body_type_basic * basic,
    struct mailimap_body_ext_1part * extension)
{
	IMAPPart * attachment;
	String * mimeType;
	
	attachment = new IMAPPart();
    attachment->importIMAPFields(basic->bd_fields, extension);
    
    mimeType = NULL;
	switch (basic->bd_media_basic->med_type) {
		case MAILIMAP_MEDIA_BASIC_APPLICATION:
        mimeType = String::stringWithUTF8Format("application/%s", basic->bd_media_basic->med_subtype);
        break;
		case MAILIMAP_MEDIA_BASIC_AUDIO:
        mimeType = String::stringWithUTF8Format("audio/%s", basic->bd_media_basic->med_subtype);
        break;
		case MAILIMAP_MEDIA_BASIC_IMAGE:
        mimeType = String::stringWithUTF8Format("image/%s", basic->bd_media_basic->med_subtype);
        break;
		case MAILIMAP_MEDIA_BASIC_MESSAGE:
        mimeType = String::stringWithUTF8Format("message/%s", basic->bd_media_basic->med_subtype);
        break;
		case MAILIMAP_MEDIA_BASIC_VIDEO:
        mimeType = String::stringWithUTF8Format("video/%s", basic->bd_media_basic->med_subtype);
        break;
		case MAILIMAP_MEDIA_BASIC_OTHER:
        mimeType = String::stringWithUTF8Format("other/%s", basic->bd_media_basic->med_subtype);
        break;
	}
    attachment->setMimeType(mimeType);
	
    return (IMAPPart *) attachment->autorelease();
}

IMAPPart * IMAPPart::attachmentWithIMAPBody1PartText(struct mailimap_body_type_text * text,
    struct mailimap_body_ext_1part * extension)
{
	IMAPPart * attachment;
	
	attachment = new IMAPPart();
    attachment->importIMAPFields(text->bd_fields, extension);
	attachment->setMimeType(String::stringWithUTF8Format("text/%s", text->bd_media_text));
	
    return (IMAPPart *) attachment->autorelease();
}

IMAPMultipart * IMAPPart::attachmentWithIMAPBodyMultipart(struct mailimap_body_type_mpart * body_mpart,
    String * partID)
{
    clistiter * cur;
    IMAPMultipart * attachment;
    unsigned int count;
    Array * attachments;

    attachments = new Array();

    count = 1;
    for(cur = clist_begin(body_mpart->bd_list) ; cur != NULL ; cur = clist_next(cur)) {
        struct mailimap_body * body;
        AbstractPart * subResult;
        String * nextPartID;

        if (partID == NULL) {
            nextPartID = String::stringWithUTF8Format("%u", count);
        }
        else {
            nextPartID = partID->stringByAppendingUTF8Format(".%u", count);
        }
        body = (struct mailimap_body *) clist_content(cur);
        subResult = attachmentWithIMAPBodyInternal(body, nextPartID);
        attachments->addObject(subResult);

        count ++;
    }

    attachment = new IMAPMultipart();
    attachment->setPartID(partID);
    if (strcasecmp(body_mpart->bd_media_subtype, "alternative") == 0) {
        attachment->setPartType(PartTypeMultipartAlternative);
    }
    else if (strcasecmp(body_mpart->bd_media_subtype, "related") == 0) {
        attachment->setPartType(PartTypeMultipartRelated);
    }
    attachment->setMimeType(String::stringWithUTF8Format("multipart/%s", body_mpart->bd_media_subtype));
    attachment->setParts(attachments);

    attachments->release();

    return (IMAPMultipart *) attachment->autorelease();
}