diff options
Diffstat (limited to 'email-core/email-core-gmime.c')
-rwxr-xr-x | email-core/email-core-gmime.c | 589 |
1 files changed, 432 insertions, 157 deletions
diff --git a/email-core/email-core-gmime.c b/email-core/email-core-gmime.c index e4d885d..ebf2f97 100755 --- a/email-core/email-core-gmime.c +++ b/email-core/email-core-gmime.c @@ -63,6 +63,10 @@ INTERNAL_FUNC void emcore_gmime_init(void) #endif g_mime_init(0); +#ifdef __FEATURE_SECURE_PGP__ + setenv("GNUPGHOME", "/opt/usr/apps/org.tizen.email/data/.gnupg", 1); +#endif + EM_DEBUG_FUNC_END(); } @@ -172,7 +176,10 @@ INTERNAL_FUNC int emcore_gmime_pop3_parse_mime(char *eml_path, struct _m_content } -INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path, struct _rfc822header *rfc822_header, struct _m_content_info *cnt_info, int *err_code) +INTERNAL_FUNC int emcore_gmime_eml_parse_mime(char *eml_path, + struct _rfc822header *rfc822_header, + struct _m_content_info *cnt_info, + int *err_code) { EM_DEBUG_FUNC_BEGIN("cnt_info[%p], err_code[%p]", cnt_info, err_code); EM_DEBUG_LOG_SEC("eml_path[%s]", eml_path); @@ -372,6 +379,9 @@ static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject EM_DEBUG_LOG("Multi Part Count:%d", multi_count); EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part)); + if (GMIME_IS_MULTIPART_SIGNED(part)) + cnt_info->text.mime_entity = emcore_gmime_get_mime_entity_signed_message(part); + } else if (GMIME_IS_PART(part)) { EM_DEBUG_LOG("Part"); int content_size = 0; @@ -482,44 +492,20 @@ static void emcore_gmime_pop3_parse_foreach_cb(GMimeObject *parent, GMimeObject /*Figure out TEXT or ATTACHMENT(INLINE) ?*/ int result = false; - if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) { + if (content_id && (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) { + content_disposition_type = INLINE_ATTACHMENT; + EM_DEBUG_LOG("INLINE_ATTACHMENT"); + } else if ((disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) || disposition_filename || ctype_name) { + content_disposition_type = ATTACHMENT; + EM_DEBUG_LOG("ATTACHMENT"); + } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "delivery-status") == 0) { + content_disposition_type = ATTACHMENT; + EM_DEBUG_LOG("ATTACHMENT"); + } else if (ctype_subtype && g_ascii_strcasecmp(ctype_subtype, "calendar") == 0) { content_disposition_type = ATTACHMENT; EM_DEBUG_LOG("ATTACHMENT"); - } else if ((content_id || content_location) && (ctype_name || disposition_filename)) { - if (cnt_info->attachment_only) { - content_disposition_type = ATTACHMENT; - EM_DEBUG_LOG("ATTACHMENT"); - } else { - content_disposition_type = INLINE_ATTACHMENT; - EM_DEBUG_LOG("INLINE_ATTACHMENT"); - } } else { - if (content_id && - (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) { - content_disposition_type = INLINE_ATTACHMENT; - EM_DEBUG_LOG("INLINE_ATTACHMENT"); - } else if (content_id || content_location) { - if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { - EM_DEBUG_LOG("TEXT"); - } else { - if (cnt_info->attachment_only) { - content_disposition_type = ATTACHMENT; - EM_DEBUG_LOG("ATTACHMENT"); - } else { - content_disposition_type = INLINE_ATTACHMENT; - EM_DEBUG_LOG("INLINE_ATTACHMENT"); - } - } - } else { - if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { - EM_DEBUG_LOG("TEXT"); - } else { - content_disposition_type = ATTACHMENT; - EM_DEBUG_LOG("ATTACHMENT"); - } - } + EM_DEBUG_LOG("Not INLINE or ATTACHMENT"); } if (content_disposition_type != ATTACHMENT && content_disposition_type != INLINE_ATTACHMENT) { @@ -681,7 +667,6 @@ FINISH_OFF: EM_DEBUG_FUNC_END(); } - static int emcore_gmime_parse_mime_header(GMimeMessage *message, struct _rfc822header *rfc822_header) { EM_DEBUG_FUNC_BEGIN("message[%p], rfc822header[%p]", message, rfc822_header); @@ -696,7 +681,7 @@ static int emcore_gmime_parse_mime_header(GMimeMessage *message, struct _rfc822h rfc822_header->reply_to = EM_SAFE_STRDUP(g_mime_message_get_reply_to(message)); EM_DEBUG_LOG_SEC("reply_to : [%s]", rfc822_header->reply_to); - rfc822_header->date = g_mime_message_get_date_as_string(message); //PREVENT + rfc822_header->date = EM_SAFE_STRDUP(g_mime_message_get_date_as_string(message)); EM_DEBUG_LOG_SEC("date : [%s]", rfc822_header->date); rfc822_header->subject = EM_SAFE_STRDUP(g_mime_message_get_subject(message)); @@ -794,8 +779,7 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject * if (msg_disposition) { msg_disposition_str = (char *)g_mime_content_disposition_get_disposition(msg_disposition); msg_disposition_filename = (char *)g_mime_content_disposition_get_parameter(msg_disposition, "filename"); - if (EM_SAFE_STRLEN(msg_disposition_filename) == 0) - msg_disposition_filename = NULL; + if (EM_SAFE_STRLEN(msg_disposition_filename) == 0) msg_disposition_filename = NULL; } EM_DEBUG_LOG("RFC822/Message Disposition[%s]", msg_disposition_str); EM_DEBUG_LOG_SEC("RFC822/Message Disposition-Filename[%s]", msg_disposition_filename); @@ -924,6 +908,9 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject * EM_DEBUG_LOG("Multi Part Count:%d", multi_count); EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part)); + if (GMIME_IS_MULTIPART_SIGNED(part)) + cnt_info->text.mime_entity = emcore_gmime_get_mime_entity_signed_message(part); + } else if (GMIME_IS_PART(part)) { EM_DEBUG_LOG("Part"); int content_size = 0; @@ -1182,21 +1169,21 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject * EM_DEBUG_EXCEPTION("emcore_get_content_from_file failed"); } - EM_DEBUG_LOG_SEC("html_charset : [%s]", cnt_info->text.html_charset); - if (strcasecmp(cnt_info->text.html_charset, "UTF-8") != 0) { - cd = g_mime_iconv_open(cnt_info->text.html_charset, "UTF-8"); - if (cd) { - encoding_file_name = g_mime_iconv_strdup(cd, file->name); - } + EM_DEBUG_LOG_SEC("html_charset : [%s]", cnt_info->text.html_charset); + if (strcasecmp(cnt_info->text.html_charset, "UTF-8") != 0) { + cd = g_mime_iconv_open(cnt_info->text.html_charset, "UTF-8"); + if (cd) { + encoding_file_name = g_mime_iconv_strdup(cd, file->name); + } - if (cd) - g_mime_iconv_close(cd); + if (cd) + g_mime_iconv_close(cd); - } else { - encoding_file_name = g_strdup(file->name); - } + } else { + encoding_file_name = g_strdup(file->name); + } - EM_DEBUG_LOG_SEC("File name : [%s], encoding file name : [%s]", file->name, encoding_file_name); + EM_DEBUG_LOG_SEC("File name : [%s], encoding file name : [%s]", file->name, encoding_file_name); if (file_content && html_size > 0) { em_replace_string_ex(&file_content, "cid:", ""); @@ -1209,7 +1196,7 @@ static void emcore_gmime_eml_parse_foreach_cb(GMimeObject *parent, GMimeObject * } g_free(file_content); /* prevent 39110 */ - g_free(encoding_file_name); + g_free(encoding_file_name); } if(ctype_type && ctype_subtype) { @@ -1381,7 +1368,7 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMime int multi_count = g_mime_multipart_get_count(multi_part); EM_DEBUG_LOG("Multi Part Count:%d", multi_count); - EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part)); + EM_DEBUG_LOG("Boundary:%s", g_mime_multipart_get_boundary(multi_part)); } else if (GMIME_IS_PART(part)) { /* a normal leaf part, could be text/plain or @@ -1451,14 +1438,16 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_foreach_cb(GMimeObject *parent, GMime /*Figure out TEXT or ATTACHMENT(INLINE) ?*/ int result = false; if (disposition_str && g_ascii_strcasecmp(disposition_str, GMIME_DISPOSITION_ATTACHMENT) == 0) { - if (content_id && - (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) { + if (content_id && (emcore_search_string_from_file(cnt_info->text.html, + content_id, + NULL, + &result) == EMAIL_ERROR_NONE && result)) { content_disposition_type = INLINE_ATTACHMENT; EM_DEBUG_LOG("INLINE_ATTACHMENT"); - } else { - content_disposition_type = ATTACHMENT; - EM_DEBUG_LOG("ATTACHMENT"); - } + } else { + content_disposition_type = ATTACHMENT; + EM_DEBUG_LOG("ATTACHMENT"); + } } else if ((content_id || content_location) && (ctype_name || disposition_filename)) { if (cnt_info->attachment_only) { content_disposition_type = ATTACHMENT; @@ -1800,7 +1789,7 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, char *msg_tmp_content_path = NULL; int real_size = 0; - if (cnt_info->grab_type != (GRAB_TYPE_TEXT|GRAB_TYPE_ATTACHMENT) && + if (cnt_info->grab_type != (GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT) && cnt_info->grab_type != GRAB_TYPE_ATTACHMENT) { goto FINISH_OFF; } @@ -1900,6 +1889,7 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, int multi_count = g_mime_multipart_get_count(multi_part); EM_DEBUG_LOG("Multi Part Count:%d", multi_count); EM_DEBUG_LOG("Boundary:%s\n\n", g_mime_multipart_get_boundary(multi_part)); + } else if (GMIME_IS_PART(part)) { EM_DEBUG_LOG("Part"); int content_disposition_type = 0; @@ -1968,8 +1958,10 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, EM_DEBUG_LOG("INLINE_ATTACHMENT"); } } else { - if (content_id && - (emcore_search_string_from_file(cnt_info->text.html, content_id, NULL, &result) == EMAIL_ERROR_NONE && result)) { + if (content_id && (emcore_search_string_from_file(cnt_info->text.html, + content_id, + NULL, + &result) == EMAIL_ERROR_NONE && result)) { content_disposition_type = INLINE_ATTACHMENT; EM_DEBUG_LOG("INLINE_ATTACHMENT"); } else if (content_id || content_location) { @@ -1986,8 +1978,9 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_full_foreach_cb(GMimeObject *parent, } } } else { - if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + if (g_ascii_strcasecmp(ctype_type, "text") == 0 && + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { EM_DEBUG_LOG("TEXT"); } else { content_disposition_type = ATTACHMENT; @@ -2343,6 +2336,8 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject } else if (GMIME_IS_MESSAGE_PARTIAL(part)) { EM_DEBUG_LOG("Partial Part"); //TODO + } else if (GMIME_IS_MULTIPART_SIGNED(part)) { + EM_DEBUG_LOG("Multi Part Signed"); } else if (GMIME_IS_MULTIPART(part)) { EM_DEBUG_LOG("Multi Part"); GMimeMultipart *multi_part = NULL; @@ -2426,7 +2421,8 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject } else { if (content_id || content_location) { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { cnt_info->total_body_size += content_size; EM_DEBUG_LOG("TEXT"); } else { @@ -2442,7 +2438,8 @@ INTERNAL_FUNC void emcore_gmime_imap_parse_bodystructure_foreach_cb(GMimeObject } } else { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { cnt_info->total_body_size += content_size; EM_DEBUG_LOG("TEXT"); } else { @@ -2490,7 +2487,9 @@ FINISH_OFF: } -INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data) +INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent, + GMimeObject *part, + gpointer user_data) { EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data); @@ -2558,6 +2557,14 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent EM_DEBUG_LOG("sections <%s>", cnt_info->sections); } else if (GMIME_IS_MESSAGE_PARTIAL(part)) { EM_DEBUG_LOG("Partial Part"); + } else if (GMIME_IS_MULTIPART_SIGNED(part)) { + EM_DEBUG_LOG("Multi Part Signed"); + snprintf(sections, sizeof(sections), "BODY.PEEK[1.mime] BODY.PEEK[1]"); + + cnt_info->content_type = 1; + cnt_info->sections = EM_SAFE_STRDUP(sections); + + EM_DEBUG_LOG("sections <%s>", cnt_info->sections); } else if (GMIME_IS_MULTIPART(part)) { EM_DEBUG_LOG("Multi Part"); } else if (GMIME_IS_PART(part)) { @@ -2593,7 +2600,7 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent goto FINISH_OFF; } /*Content Type - END*/ - + /*Content Disposition*/ disposition = g_mime_object_get_content_disposition(mobject); if (disposition) { @@ -2628,7 +2635,8 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent } else { if (content_id || content_location) { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { EM_DEBUG_LOG("TEXT"); } else { if (cnt_info->attachment_only) { @@ -2641,7 +2649,8 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent } } else { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { EM_DEBUG_LOG("TEXT"); } else { content_disposition_type = ATTACHMENT; @@ -2658,8 +2667,13 @@ INTERNAL_FUNC void emcore_gmime_get_body_sections_foreach_cb(GMimeObject *parent } else { snprintf(sections, sizeof(sections), "BODY.PEEK[%s.MIME] BODY.PEEK[%s]", ctype_section, ctype_section); - + if (cnt_info->sections) { + /* Signed message : Did not need sections */ + if (cnt_info->content_type == 1) { + goto FINISH_OFF; + } + char *tmp_str = NULL; tmp_str = g_strconcat(cnt_info->sections, " ", sections, NULL); @@ -2682,7 +2696,9 @@ FINISH_OFF: } -INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data) +INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *parent, + GMimeObject *part, + gpointer user_data) { EM_DEBUG_FUNC_BEGIN("parent[%p], part[%p], user_data[%p]", parent, part, user_data); @@ -2819,7 +2835,8 @@ INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *p } else { if (content_id || content_location) { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { EM_DEBUG_LOG("TEXT"); } else { if (cnt_info->attachment_only) { @@ -2832,7 +2849,8 @@ INTERNAL_FUNC void emcore_gmime_get_attachment_section_foreach_cb(GMimeObject *p } } else { if (g_ascii_strcasecmp(ctype_type, "text") == 0 && - (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { + (g_ascii_strcasecmp(ctype_subtype, "plain") == 0 || + g_ascii_strcasecmp(ctype_subtype, "html") == 0)) { EM_DEBUG_LOG("TEXT"); } else { content_disposition_type = ATTACHMENT; @@ -2868,7 +2886,7 @@ INTERNAL_FUNC void emcore_gmime_search_section_foreach_cb(GMimeObject *parent, G search_section *search_info = (search_section *)user_data; GMimeContentType *ctype = NULL; - char buf[255] = {0}; + char buf[255] = {0}; char *ctype_section = NULL; if (!search_info) { @@ -2900,23 +2918,47 @@ INTERNAL_FUNC void emcore_gmime_search_section_foreach_cb(GMimeObject *parent, G goto FINISH_OFF; } - SNPRINTF(buf, sizeof(buf), "%s.MIME", ctype_section); + SNPRINTF(buf, sizeof(buf), "%s.MIME", ctype_section); if (g_ascii_strcasecmp(ctype_section, search_info->section) == 0) { EM_DEBUG_LOG("found section"); if (!(search_info->section_object)) search_info->section_object = part; - } else if (g_ascii_strcasecmp(search_info->section, buf) == 0) { - EM_DEBUG_LOG("Mime header"); - if (!(search_info->section_object)) search_info->section_object = part; - } + } else if (g_ascii_strcasecmp(search_info->section, buf) == 0) { + EM_DEBUG_LOG("Mime header"); + if (!(search_info->section_object)) search_info->section_object = part; + } FINISH_OFF: EM_DEBUG_FUNC_END(); } +INTERNAL_FUNC void emcore_gmime_get_mime_entity_cb(GMimeObject *parent, GMimeObject *part, gpointer user_data) +{ + EM_DEBUG_FUNC_BEGIN(); + + struct _m_content_info *cnt_info = (struct _m_content_info *)user_data; + + if (GMIME_IS_MESSAGE_PART(part)) { + EM_DEBUG_LOG("Message Part"); + } else if (GMIME_IS_MESSAGE_PARTIAL(part)) { + EM_DEBUG_LOG("Partial Part"); + } else if (GMIME_IS_MULTIPART_SIGNED(part)) { + EM_DEBUG_LOG("Multi Part Signed"); + cnt_info->text.mime_entity = emcore_gmime_get_mime_entity_signed_message(part); + } else if (GMIME_IS_MULTIPART(part)) { + EM_DEBUG_LOG("Multi Part"); + } else if (GMIME_IS_PART(part)) { + EM_DEBUG_LOG("Part"); + } + + EM_DEBUG_FUNC_END(); +} + INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart, - BODY *body, const char *spec, int *total_mail_size) + BODY *body, + const char *spec, + int *total_mail_size) { EM_DEBUG_FUNC_BEGIN(); PART *part = NULL; @@ -2959,8 +3001,8 @@ INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart, if (EM_SAFE_STRLEN(subspec) > 2) section = EM_SAFE_STRDUP(subspec+2); - EM_DEBUG_LOG("constructing a %s/%s part (%s/%s)", body_types[part->body.type], - part->body.subtype, subspec, section); + EM_DEBUG_LOG("constructing a %s/%s part (%s/%s)", body_types[part->body.type], part->body.subtype, + subspec, section); if (part->body.type == TYPEMULTIPART) { /*multipart*/ @@ -3069,7 +3111,7 @@ INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart, if (part->body.parameter) { PARAMETER *param = part->body.parameter; while(param) { - EM_DEBUG_LOG_SEC("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value); + EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value); if (param->attribute || param->value) g_mime_object_set_content_type_parameter(subpart, param->attribute, param->value); param = param->next; @@ -3130,7 +3172,9 @@ INTERNAL_FUNC void emcore_gmime_construct_multipart (GMimeMultipart *multipart, } INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part, - BODY *body, const char *spec, int *total_mail_size) + BODY *body, + const char *spec, + int *total_mail_size) { EM_DEBUG_FUNC_BEGIN(); GMimeObject *part_object = NULL; @@ -3155,8 +3199,8 @@ INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part, ctype_subtype = (char *)g_mime_content_type_get_media_subtype(ctype); /* Type-Subtype */ - if (g_strcmp0(ctype_type, "text") == 0 && g_strcmp0(ctype_subtype, "plain") == 0 && - body->type >= (unsigned int)0 && body->subtype) { + if (g_strcmp0(ctype_type, "text") == 0 && g_strcmp0(ctype_subtype, "plain") == 0 + && body->subtype) { GMimeContentType *content_type = NULL; char *type = g_ascii_strdown(body_types[body->type], -1); char *subtype = g_ascii_strdown(body->subtype, -1); @@ -3268,18 +3312,20 @@ INTERNAL_FUNC void emcore_gmime_construct_part (GMimePart *part, EM_DEBUG_LOG("Content-ID: %s", body->id); int i = 0; char *cid = EM_SAFE_STRDUP(body->id); - if (cid) g_strstrip(cid); + if (cid) { + g_strstrip(cid); - while (strlen(cid) > 0 && cid[i] != '\0') { - if (cid[i] == '<' || cid[i] == '>') - cid[i] = ' '; - i++; - } + while (strlen(cid) > 0 && cid[i] != '\0') { + if (cid[i] == '<' || cid[i] == '>') + cid[i] = ' '; + i++; + } - g_strstrip(cid); - EM_DEBUG_LOG_DEV("Content-ID stripped: %s", cid); - g_mime_object_set_content_id(part_object, cid); - EM_SAFE_FREE(cid); + g_strstrip(cid); + EM_DEBUG_LOG_DEV("Content-ID stripped: %s", cid); + g_mime_object_set_content_id(part_object, cid); + free(cid); + } } EM_DEBUG_FUNC_END(); @@ -3311,7 +3357,6 @@ INTERNAL_FUNC int emcore_gmime_construct_mime_part_with_bodystructure(BODY *mbod if (mbody->parameter) { PARAMETER *param = mbody->parameter; while(param) { - EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value); if (g_ascii_strcasecmp(param->attribute, "boundary") == 0 && param->value) boundary_ok = 1; param = param->next; @@ -3324,13 +3369,28 @@ INTERNAL_FUNC int emcore_gmime_construct_mime_part_with_bodystructure(BODY *mbod } char *subtype = g_ascii_strdown(mbody->subtype, -1); - multipart = g_mime_multipart_new_with_subtype(subtype); + EM_DEBUG_LOG("Constructing a MULTIPART/%s", subtype); + if (g_ascii_strcasecmp(subtype, "signed") == 0) { + multipart = (GMimeMultipart *)g_mime_multipart_signed_new(); + } else { + multipart = g_mime_multipart_new_with_subtype(subtype); + } /* Fill up mime part of message1 using bodystructure info */ emcore_gmime_construct_multipart(multipart, mbody, spec, &total_size); mime_part = GMIME_OBJECT(multipart); EM_SAFE_FREE(subtype); + + if (mbody->parameter) { + PARAMETER *param = mbody->parameter; + while(param) { + EM_DEBUG_LOG("Content-Type Parameter: attribute[%s], value[%s]", param->attribute, param->value); + if (param->attribute || param->value) + g_mime_object_set_content_type_parameter(mime_part, param->attribute, param->value); + param = param->next; + } + } } else { char *type = g_ascii_strdown(body_types[mbody->type], -1); @@ -3363,7 +3423,8 @@ FINISH_OFF: } INTERNAL_FUNC int emcore_gmime_get_body_sections_from_message(GMimeMessage *message, - struct _m_content_info *cnt_info, char **sections_to_fetch) + struct _m_content_info *cnt_info, + char **sections_to_fetch) { EM_DEBUG_FUNC_BEGIN(); @@ -3461,7 +3522,9 @@ FINISH_OFF: } INTERNAL_FUNC int emcore_gmime_get_attachment_section_from_message(GMimeMessage *message, - struct _m_content_info *cnt_info, int nth, char **section_to_fetch) + struct _m_content_info *cnt_info, + int nth, + char **section_to_fetch) { EM_DEBUG_FUNC_BEGIN(); @@ -3597,7 +3660,9 @@ static int emcore_gmime_get_section_n_bodysize(char *response, char *section, in } INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int msg_uid, int mail_id, - struct _m_content_info *cnt_info, GMimeMessage *message, int event_handle, int auto_download, int *err_code) + struct _m_content_info *cnt_info, + GMimeMessage *message, int event_handle, + int auto_download, int *err_code) { EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], cnt_info[%p], err_code[%p]", stream, msg_uid, cnt_info, err_code); @@ -3624,13 +3689,20 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int int flag_first_write = 1; char *tag_position = NULL; - int part_header = 0; + int part_header = 0; + int signed_message = 0; int download_interval = 0; int download_total_size = 0; int downloaded_size = 0; int download_progress = 0; int last_notified_download_size = 0; + char *full_text = NULL; + char *body_string = NULL; + char *header_string = NULL; + GMimeObject *entity_part = NULL; + GMimeParser *entity_parser = NULL; + GMimeDataWrapper *content = NULL; GMimeStream *content_stream = NULL; GMimeObject *mime_object = NULL; @@ -3648,8 +3720,9 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int } if (!cnt_info || !cnt_info->sections) { - EM_DEBUG_EXCEPTION("invalid parameter detected..."); - err = EMAIL_ERROR_INVALID_PARAM; + EM_DEBUG_LOG("invalid parameter detected..."); + /* Encrypted message did not have a text body So did not have a body section */ + ret = TRUE; goto FINISH_OFF; } @@ -3664,6 +3737,11 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int goto FINISH_OFF; } + if (GMIME_IS_MULTIPART_SIGNED(message->mime_part)) { + EM_DEBUG_LOG("Multi Part Signed"); + signed_message = 1; + } + while (imaplocal->netstream) { /* receive response */ @@ -3686,13 +3764,13 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int server_response_yn = 1; } - part_header = 0; + part_header = 0; flag_first_write = 1; total = 0; memset(encoded, 0x00, sizeof(encoded)); if (!emcore_gmime_get_section_n_bodysize(response, section, &body_size)) { - EM_DEBUG_EXCEPTION("emcore_get_section_body_size failed [%d]", err); + EM_DEBUG_EXCEPTION("emcore_gmime_get_section_body_size failed [%d]", err); err = EMAIL_ERROR_INVALID_RESPONSE; goto FINISH_OFF; } @@ -3751,26 +3829,30 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int mime_part = NULL; mime_object = search_info->section_object; if (GMIME_IS_MESSAGE_PART(mime_object)) { - if (strcasestr(section, "MIME")) - part_header = 1; + if (strcasestr(section, "MIME")) + part_header = 1; - mime_message_part = GMIME_MESSAGE_PART(mime_object); + mime_message_part = GMIME_MESSAGE_PART(mime_object); } else if (GMIME_IS_PART(mime_object)) { - if (strcasestr(section, "MIME")) - part_header = 1; + if (strcasestr(section, "MIME")) + part_header = 1; mime_part = GMIME_PART(mime_object); + } else if (signed_message) { + if (strcasestr(section, "MIME")) + part_header = 1; } else { EM_DEBUG_EXCEPTION("invalid mime part type"); goto FINISH_OFF; } - + if (!part_header) { if (!emcore_get_temp_file_name(&buf, &err) || !buf) { EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err); goto FINISH_OFF; } + EM_DEBUG_LOG("TMP name : [%s]", buf); g_mime_object_set_content_type_parameter(mime_object, "tmp_content_path", buf); if (event_handle > 0) @@ -3780,7 +3862,11 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int if (cnt_info->total_body_size > body_size) { EM_DEBUG_LOG("Multipart body size is [%d]", cnt_info->total_body_size); if (!auto_download) { - if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, cnt_info->total_body_size, 0)) + if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, + mail_id, + buf, + cnt_info->total_body_size, + 0)) EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> "); } @@ -3800,7 +3886,11 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int if (cnt_info->grab_type == (GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT)) { if (!auto_download) { - if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, mail_id, buf, cnt_info->total_mail_size, 0)) + if (!emcore_notify_network_event(NOTI_DOWNLOAD_MULTIPART_BODY, + mail_id, + buf, + cnt_info->total_mail_size, + 0)) EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] Failed >>>> "); } @@ -3819,8 +3909,18 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int goto FINISH_OFF; } - if (GMIME_IS_PART(mime_object) && mime_part) { - content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded)); + if (signed_message) { + content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, + EM_SAFE_STRLEN(encoded)); + entity_parser = g_mime_parser_new_with_stream(content_stream); + if (content_stream) g_object_unref(content_stream); + + entity_part = g_mime_parser_construct_part(entity_parser); + if (entity_parser) g_object_unref(entity_parser); + } + else if (GMIME_IS_PART(mime_object) && mime_part) { + content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, + EM_SAFE_STRLEN(encoded)); //parser = g_mime_parser_new_with_stream(content_stream); content = g_mime_data_wrapper_new_with_stream(content_stream, mime_part->encoding); if (content_stream) g_object_unref (content_stream); @@ -3983,7 +4083,22 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int EM_DEBUG_LOG("%d has been written", EM_SAFE_STRLEN((char *)test_buffer)); } - if (GMIME_IS_PART(mime_object) && mime_part) { + if (signed_message) { + err = em_open(buf, O_RDONLY, 0, &fd); + if (err != EMAIL_ERROR_NONE) { + EM_DEBUG_EXCEPTION("holder open failed : holder is a filename that will be saved."); + goto FINISH_OFF; + } + + content_stream = g_mime_stream_fs_new(fd); + + entity_parser = g_mime_parser_new_with_stream(content_stream); + if (content_stream) g_object_unref(content_stream); + + entity_part = g_mime_parser_construct_part(entity_parser); + if (entity_parser) g_object_unref(entity_parser); + } + else if (GMIME_IS_PART(mime_object) && mime_part) { err = em_open(buf, O_RDONLY, 0, &fd); if (err != EMAIL_ERROR_NONE) { EM_DEBUG_EXCEPTION("holder open failed : holder is a filename that will be saved."); @@ -3996,54 +4111,58 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int if (content_stream) g_object_unref (content_stream); g_mime_part_set_content_object(mime_part, content); if (content) g_object_unref(content); - } + } } } else { - EM_DEBUG_LOG("MIME header"); - - char *file_name = NULL; - - GMimeObject *object_header = NULL; - GMimeParser *parser_header = NULL; - GMimeContentType *ctype_header = NULL; - GMimeContentDisposition *disposition_header = NULL; - - if (net_getbuffer(imaplocal->netstream, (long)body_size, (char *)encoded) <= 0) { - EM_DEBUG_EXCEPTION("net_getbuffer failed..."); - err = EMAIL_ERROR_NO_RESPONSE; - goto FINISH_OFF; - } - - EM_DEBUG_LOG_DEV("Data : [%s]", encoded); + EM_DEBUG_LOG("MIME header"); - content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded)); + char *file_name = NULL; - parser_header = g_mime_parser_new_with_stream(content_stream); - if (content_stream) g_object_unref (content_stream); + GMimeObject *object_header = NULL; + GMimeParser *parser_header = NULL; + GMimeContentType *ctype_header = NULL; + GMimeContentDisposition *disposition_header = NULL; - object_header = g_mime_parser_construct_part(parser_header); - if (parser_header) g_object_unref(parser_header); + if (net_getbuffer(imaplocal->netstream, (long)body_size, (char *)encoded) <= 0) { + EM_DEBUG_EXCEPTION("net_getbuffer failed..."); + err = EMAIL_ERROR_NO_RESPONSE; + goto FINISH_OFF; + } - /* Content type */ - ctype_header = g_mime_object_get_content_type(object_header); - file_name = (char *)g_mime_content_type_get_parameter(ctype_header, "name"); - EM_DEBUG_LOG_DEV("Content name : [%s]", file_name); + EM_DEBUG_LOG_DEV("Data : [%s]", encoded); - if (file_name == NULL) { - /* Content Disposition */ - disposition_header = g_mime_object_get_content_disposition(object_header); - file_name = (char *)g_mime_content_disposition_get_parameter(disposition_header, "filename"); - EM_DEBUG_LOG_DEV("Disposition name : [%s]", file_name); - } + if (signed_message) { + header_string = g_strdup((const gchar *)encoded); + } else { + content_stream = g_mime_stream_mem_new_with_buffer((const char *)encoded, EM_SAFE_STRLEN(encoded)); + parser_header = g_mime_parser_new_with_stream(content_stream); + if (content_stream) g_object_unref(content_stream); + + object_header = g_mime_parser_construct_part(parser_header); + if (parser_header) g_object_unref(parser_header); + + /* Content type */ + ctype_header = g_mime_object_get_content_type(object_header); + file_name = (char *)g_mime_content_type_get_parameter(ctype_header, "name"); + EM_DEBUG_LOG_DEV("Content name : [%s]", file_name); + + if (file_name == NULL) { + /* Content Disposition */ + disposition_header = g_mime_object_get_content_disposition(object_header); + file_name = (char *)g_mime_content_disposition_get_parameter(disposition_header, "filename"); + EM_DEBUG_LOG_DEV("Disposition name : [%s]", file_name); + } - /* Replace the file name (Becase the server sometimes send the invalid name in bodystructure) */ - if (mime_part && file_name) g_mime_part_set_filename(mime_part, file_name); + /* Replace the file name (Becase the server sometimes send the invalid name in bodystructure) */ + if (mime_part && file_name) g_mime_part_set_filename(mime_part, file_name); + } - if (object_header) g_object_unref(object_header); - if (ctype_header) g_object_unref(ctype_header); - if (disposition_header) g_object_unref(disposition_header); - } + if (object_header) g_object_unref(object_header); + if (ctype_header) g_object_unref(ctype_header); + if (disposition_header) g_object_unref(disposition_header); + } + if (buf) g_remove(buf); EM_SAFE_FREE(buf); } else if ((tag_position = g_strrstr(response, tag))) /* end of response */ { @@ -4062,6 +4181,43 @@ INTERNAL_FUNC int emcore_gmime_fetch_imap_body_sections(MAILSTREAM *stream, int EM_SAFE_FREE (response); } + if (signed_message) { + int part_index = -1; + + GMimeStream *tmp_stream = NULL; + GMimeParser *tmp_parser = NULL; + GMimeMessage *tmp_message = NULL; + + body_string = g_mime_object_to_string(entity_part); + if (entity_part) g_object_unref(entity_part); + + full_text = g_strconcat(header_string, body_string, NULL); + tmp_stream = g_mime_stream_mem_new_with_buffer(full_text, EM_SAFE_STRLEN(full_text)); + + tmp_parser = g_mime_parser_new_with_stream(tmp_stream); + if (tmp_stream) g_object_unref(tmp_stream); + + tmp_message = g_mime_parser_construct_message(tmp_parser); + if (tmp_parser) g_object_unref(tmp_parser); + + /* Search get index of multipart for replacing */ + part_index = g_mime_multipart_index_of(GMIME_MULTIPART(message->mime_part), mime_object); + if (part_index == -1) { + EM_DEBUG_EXCEPTION("g_mime_multipart_index_of failed"); + if (tmp_message) g_object_unref(tmp_message); + goto FINISH_OFF; + } + + /* replace the entity part */ + if (g_mime_multipart_replace(GMIME_MULTIPART(message->mime_part), part_index, tmp_message->mime_part) == NULL) { + EM_DEBUG_EXCEPTION("g_mime_multipart_replaced failed"); + if (tmp_message) g_object_unref(tmp_message); + goto FINISH_OFF; + } + + if (tmp_message) g_object_unref(tmp_message); + } + ret = TRUE; FINISH_OFF: @@ -4071,11 +4227,19 @@ FINISH_OFF: mime_iter = NULL; }*/ + EM_SAFE_FREE(header_string); + EM_SAFE_FREE(body_string); + EM_SAFE_FREE(full_text); + + if (entity_part) g_object_unref(entity_part); + if (entity_parser) g_object_unref(entity_parser); + if (search_info) { EM_SAFE_FREE(search_info->section); EM_SAFE_FREE(search_info); } + if (buf) g_remove(buf); EM_SAFE_FREE(buf); EM_SAFE_FREE(response); @@ -4747,3 +4911,114 @@ INTERNAL_FUNC char *emcore_gmime_get_encoding_to_utf8(const char *text) return encoded_text; } + +INTERNAL_FUNC char *emcore_gmime_get_decoding_text(const char *text) +{ + EM_DEBUG_FUNC_BEGIN(); + + if (text == NULL) { + EM_DEBUG_EXCEPTION("Invalid parameter"); + return NULL; + } + + char *decoded_text = NULL; + + decoded_text = g_mime_utils_header_decode_text(text); + if (decoded_text == NULL) { + EM_DEBUG_EXCEPTION("g_mime_utils_header_encode_text failed : [%s]", text); + return NULL; + } + + EM_DEBUG_LOG("decoded_text : [%s]", decoded_text); + + return decoded_text; +} + +INTERNAL_FUNC char *emcore_gmime_get_mime_entity_signed_message(GMimeObject *multipart) +{ + EM_DEBUG_FUNC_BEGIN(); + + int entity_fd = 0; + int error = EMAIL_ERROR_NONE; + char *mime_entity_path = NULL; + + GMimeObject *mime_entity = NULL; + GMimeStream *out_stream = NULL, *filtered = NULL, *stream = NULL; + GMimeFilter *filter = NULL; + + if (!GMIME_IS_MULTIPART_SIGNED(multipart)) { + EM_DEBUG_EXCEPTION("Invalid param"); + error = EMAIL_ERROR_INVALID_PARAM; + goto FINISH_OFF; + } + + if (!emcore_get_temp_file_name(&mime_entity_path, &error)) { + EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed : [%d]", error); + goto FINISH_OFF; + } + + entity_fd = open(mime_entity_path, O_WRONLY|O_CREAT, 0644); + if (entity_fd < 0) { + EM_DEBUG_EXCEPTION("open failed"); + error = EMAIL_ERROR_SYSTEM_FAILURE; + goto FINISH_OFF; + } + + mime_entity = g_mime_multipart_get_part(GMIME_MULTIPART(multipart), GMIME_MULTIPART_SIGNED_CONTENT); + stream = g_mime_stream_mem_new(); + g_mime_object_write_to_stream(mime_entity, stream); + g_mime_stream_reset(stream); + + filtered = g_mime_stream_filter_new(stream); + filter = g_mime_filter_crlf_new(TRUE, FALSE); + g_mime_stream_filter_add(GMIME_STREAM_FILTER(filtered), filter); + + if (filter) g_object_unref(filter); + if (stream) g_object_unref(stream); + if (filtered) g_mime_stream_reset(filtered); + + out_stream = g_mime_stream_fs_new(entity_fd); + g_mime_stream_write_to_stream(filtered, out_stream); + g_mime_stream_flush(out_stream); + + if (filtered) g_object_unref(filtered); + if (out_stream) g_object_unref(out_stream); + +FINISH_OFF: + + if (error != EMAIL_ERROR_NONE) { + EM_SAFE_FREE(mime_entity_path); + return NULL; + } else { + return mime_entity_path; + } +} + +INTERNAL_FUNC char *emcore_gmime_get_mime_entity(int fd) +{ + EM_DEBUG_FUNC_BEGIN(); + char *output_path = NULL; + + GMimeStream *stream = NULL; + GMimeParser *parser = NULL; + GMimeObject *part = NULL; + + stream = g_mime_stream_fs_new(fd); + + parser = g_mime_parser_new_with_stream(stream); + if (stream) g_object_unref(stream); + + part = g_mime_parser_construct_part(parser); + if (parser) g_object_unref(parser); + if (part == NULL) { + EM_DEBUG_EXCEPTION("Data is invalid"); + return NULL; + } + + output_path = emcore_gmime_get_mime_entity_signed_message(part); + + if (part) g_object_unref(part); + + EM_DEBUG_FUNC_END(); + return output_path; +} |