4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /* ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***
25 *File : email-core-mime.c
26 *Desc : MIME Operation
31 * 2011.04.14 : created
32 ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ***/
43 #include "email-internal-types.h"
45 #include "email-utilities.h"
46 #include "email-core-utils.h"
47 #include "email-core-mail.h"
48 #include "email-core-mime.h"
49 #include "email-core-gmime.h"
50 #include "email-storage.h"
51 #include "email-core-event.h"
52 #include "email-core-account.h"
53 #include "email-core-signal.h"
54 #include "email-core-mailbox-sync.h"
55 #include "email-core-container.h"
56 #include "email-debug-log.h"
58 #define MIME_MESG_IS_SOCKET
60 #define MIME_LINE_LEN 1024
61 #define BOUNDARY_LEN 256
67 #define TYPE_APPLICATION 5
68 #define TYPE_MULTIPART 6
69 #define TYPE_MESSAGE 7
70 #define TYPE_UNKNOWN 8
72 #define TEXT_STR "TEXT"
73 #define IMAGE_STR "IMAGE"
74 #define AUDIO_STR "AUDIO"
75 #define VIDEO_STR "VIDEO"
76 #define APPLICATION_STR "APPLICATION"
77 #define MULTIPART_STR "MULTIPART"
78 #define MESSAGE_STR "MESSAGE"
80 #define CONTENT_TYPE 1
81 #define CONTENT_SUBTYPE 2
82 #define CONTENT_ENCODING 3
83 #define CONTENT_CHARSET 4
84 #define CONTENT_DISPOSITION 5
85 #define CONTENT_NAME 6
86 #define CONTENT_FILENAME 7
87 #define CONTENT_BOUNDARY 8
88 #define CONTENT_REPORT_TYPE 9
90 #define CONTENT_LOCATION 11
91 #define CONTENT_SMIME_TYPE 12
92 #define CONTENT_PROTOCOL 13
94 #define GRAB_TYPE_TEXT 1 /* retrieve text and attachment name */
95 #define GRAB_TYPE_ATTACHMENT 2 /* retrieve only attachmen */
97 #define SAVE_TYPE_SIZE 1 /* only get content siz */
98 #define SAVE_TYPE_BUFFER 2 /* save content to buffe */
99 #define SAVE_TYPE_FILE 3 /* save content to temporary fil */
101 /* ---------------------------------------------------------------------- */
102 /* Global variable */
103 static int eml_data_count = 0;
105 /* ---------------------------------------------------------------------- */
106 /* External variable */
107 extern int _pop3_receiving_mail_id;
108 extern int _pop3_received_body_size;
109 extern int _pop3_last_notified_body_size;
110 extern int _pop3_total_body_size;
112 extern int _imap4_received_body_size;
113 extern int _imap4_last_notified_body_size;
114 extern int _imap4_total_body_size;
115 extern int _imap4_download_noti_interval_value;
117 extern int multi_part_body_size;
118 extern bool only_body_download;
120 /* ---------------------------------------------------------------------- */
121 __thread BODY **g_inline_list = NULL;
122 __thread int g_inline_count = 0;
124 void emcore_mime_free_param(struct _parameter *param);
125 void emcore_mime_free_part_header(struct _m_part_header *header);
126 void emcore_mime_free_message_header(struct _m_mesg_header *header);
127 void emcore_mime_free_rfc822_header(struct _rfc822header *rfc822header);
128 void emcore_mime_free_part(struct _m_part *part);
129 void emcore_mime_free_part_body(struct _m_body *body);
130 void emcore_mime_free_mime(struct _m_mesg *mmsg);
132 extern long pop3_reply(MAILSTREAM *stream);
133 /* ------------------------------------------------------------------------------------------------- */
135 char *em_get_escaped_str(const char *str)
137 EM_DEBUG_FUNC_BEGIN_SEC("str [%s]", str);
145 char *ret = calloc(1, 2 * len);
151 const char to_be_escaped[10] = {'\\', '\"', '\0',};
153 for (i = 0; i < len; i++) {
154 for (j = 0; to_be_escaped[j] != '\0'; j++) {
155 if (str[i] == to_be_escaped[j]) {
160 ret[ret_i++] = str[i];
168 /* Fix for issue junk characters in mail body */
169 char *em_split_file_path(char *str)
171 EM_DEBUG_FUNC_BEGIN("str [%s]", str);
173 EM_IF_NULL_RETURN_VALUE(str, NULL);
178 char *temp_str = NULL;
179 char *content_id = NULL;
181 char *temp_cid_data = NULL;
182 char *temp_cid = NULL;
184 temp_str = g_strdup(str);
185 buf_length = EM_SAFE_STRLEN(str) + 1024;
186 buf = em_malloc(buf_length);
188 EM_DEBUG_EXCEPTION("em_mallocfailed");
189 EM_SAFE_FREE(temp_str);
193 content_id = temp_str;
194 temp_cid = strstr(temp_str, "\"");
195 if (temp_cid == NULL) {
196 EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain end line for CID ");
201 temp_cid_data = em_malloc((temp_cid-temp_str)+1);
202 if (temp_cid_data == NULL) {
203 EM_DEBUG_EXCEPTION("em_mallocfailed");
208 memcpy(temp_cid_data, temp_str, (temp_cid-temp_str));
210 if (temp_cid_data && !strstr(temp_cid_data, delims)) {
211 EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain @ ");
213 EM_SAFE_FREE(temp_cid_data);
216 result = strstr(temp_str, delims);
217 if (result != NULL) {
220 EM_DEBUG_LOG_SEC("content_id is [ %s ]", content_id);
222 if (strcasestr(content_id, ".bmp") || strcasestr(content_id, ".jpeg") || strcasestr(content_id, ".png") ||
223 strcasestr(content_id, ".jpg") || strcasestr(content_id, ".gif"))
224 snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s\"", content_id);
226 snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s%s", content_id, ".jpeg\"");
228 EM_DEBUG_EXCEPTION(">>>> File Path Doesnot contain end line for CID ");
230 EM_SAFE_FREE(temp_cid_data);
233 result = strstr(result, "\"");
234 if (result != NULL) {
236 snprintf(buf+EM_SAFE_STRLEN(buf), buf_length - EM_SAFE_STRLEN(buf), "%s", result);
240 EM_SAFE_FREE(temp_str);
241 EM_SAFE_FREE(temp_cid_data);
248 * decode body text (quoted-printable, base64)
249 * enc_type : encoding type (base64/quotedprintable)
251 INTERNAL_FUNC int emcore_decode_body_text(char *enc_buf, int enc_len, int enc_type, int *dec_len, int *err_code)
253 EM_DEBUG_FUNC_BEGIN("enc_buf[%p], enc_len[%d], enc_type[%d], dec_len[%p]", enc_buf, enc_len, enc_type, dec_len);
254 unsigned char *content = NULL;
256 /* too many called */
260 case ENCQUOTEDPRINTABLE:
262 content = rfc822_qprint((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
267 content = rfc822_base64((unsigned char *)enc_buf, (unsigned long)enc_len, (unsigned long *)dec_len);
279 if (enc_len < *dec_len) {
280 EM_DEBUG_EXCEPTION("Decoded length is too big to store it");
281 EM_SAFE_FREE(content);
282 if (err_code) *err_code = EMAIL_ERROR_INVALID_DATA;
283 return EMAIL_ERROR_INVALID_DATA;
285 memcpy(enc_buf, content, *dec_len);
286 enc_buf[*dec_len] = '\0';
287 EM_SAFE_FREE(content);
291 if (err_code) *err_code = EMAIL_ERROR_NONE;
292 return EMAIL_ERROR_NONE;
295 /* get temporary file name */
296 char *emcore_mime_get_save_file_name(int *err_code)
298 EM_DEBUG_FUNC_BEGIN();
302 gettimeofday(&tv, NULL);
305 memset(tempname, 0x00, sizeof(tempname));
306 unsigned int seed = time(NULL);
307 SNPRINTF(tempname, sizeof(tempname), "%s%s%d", MAIL_TEMP, DIR_SEPERATOR, rand_r(&seed));
309 return EM_SAFE_STRDUP(tempname);
312 /* get a line from file pointer */
313 char *emcore_get_line_from_file(void *stream, char *buf, int size, int *err_code)
315 if (!fgets(buf, size, (FILE *)stream)) {
316 if (feof((FILE *)stream)) {
317 *err_code = EMAIL_ERROR_NO_MORE_DATA;
325 /* get a line from POP3 mail stream */
326 /* emcore_mail_cmd_read_mail_pop3 must be called before this function */
327 char *emcore_mime_get_line_from_sock(void *stream, char *buf, int size, int *err_code)
329 EM_DEBUG_FUNC_BEGIN("stream[%p], buf[%p]", stream, buf);
330 POP3LOCAL *p_pop3local = NULL;
332 if (!stream || !buf) {
333 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
334 if (err_code != NULL)
335 *err_code = EMAIL_ERROR_INVALID_PARAM;
339 memset(buf, 0x00, size);
341 p_pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
343 EM_DEBUG_EXCEPTION("stream->local[%p]", p_pop3local);
344 if (err_code != NULL)
345 *err_code = EMAIL_ERROR_INVALID_PARAM;
349 if (!pop3_reply((MAILSTREAM *)stream)) { /* if TRUE, check response */
350 EM_DEBUG_LOG_DEV("p_pop3local->response 1[%s]", p_pop3local->response);
351 if (p_pop3local->response) {
352 if (*p_pop3local->response == '.' && EM_SAFE_STRLEN(p_pop3local->response) == 1) {
353 EM_SAFE_FREE(p_pop3local->response);
354 if (err_code != NULL)
355 *err_code = EMAIL_ERROR_NO_MORE_DATA;
356 EM_DEBUG_FUNC_END("end of response");
359 EM_DEBUG_LOG_DEV("Not end of response");
360 strncpy(buf, p_pop3local->response, size-1);
361 strncat(buf, CRLF_STRING, size-(EM_SAFE_STRLEN(buf) + 1));
363 EM_SAFE_FREE(p_pop3local->response);
368 EM_DEBUG_LOG_DEV("p_pop3local->response 2[%s]", p_pop3local->response);
369 if (p_pop3local->response) {
370 /* if response isn't NULL, check whether this response start with '+' */
371 /* if the first character is '+', return error because this response is normal data */
372 strncpy(buf, p_pop3local->response, size-1);
373 strncat(buf, CRLF_STRING, size-(EM_SAFE_STRLEN(buf)+1));
374 EM_SAFE_FREE(p_pop3local->response);
377 EM_DEBUG_EXCEPTION("p_pop3local->response is null. network error... ");
378 if (err_code != NULL)
379 *err_code = EMAIL_ERROR_INVALID_RESPONSE;
388 if (EM_SAFE_STRLEN(buf) >= 2 && buf[0] == '.' && buf[1] == '.') {
389 char *tmp = g_strdup(buf+1);
390 memset(buf, 0x00, size);
391 strncpy(buf, tmp, size-1);
395 int received_percentage, last_notified_percentage;
396 _pop3_received_body_size += EM_SAFE_STRLEN(buf);
398 last_notified_percentage = (double)_pop3_last_notified_body_size / (double)_pop3_total_body_size *100.0;
399 received_percentage = (double)_pop3_received_body_size / (double)_pop3_total_body_size *100.0;
401 EM_DEBUG_LOG_DEV("_pop3_received_body_size = %d, _pop3_total_body_size = %d", _pop3_received_body_size, _pop3_total_body_size);
402 EM_DEBUG_LOG_DEV("received_percentage = %d, last_notified_percentage = %d", received_percentage, last_notified_percentage);
404 if (received_percentage > last_notified_percentage + 5) {
405 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, _pop3_receiving_mail_id, "dummy-file", _pop3_total_body_size, _pop3_received_body_size))
406 EM_DEBUG_EXCEPTION(" emcore_notify_network_event[NOTI_DOWNLOAD_BODY_START] Failed >>>> ");
408 EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", _pop3_received_body_size, _pop3_total_body_size);
409 _pop3_last_notified_body_size = _pop3_received_body_size;
416 void emcore_mime_free_param(struct _parameter *param)
418 struct _parameter *t, *p = param;
419 EM_DEBUG_FUNC_BEGIN();
422 EM_SAFE_FREE(p->name);
423 EM_SAFE_FREE(p->value);
430 void emcore_mime_free_part_header(struct _m_part_header *header)
432 EM_DEBUG_FUNC_BEGIN();
433 if (!header) return ;
434 EM_SAFE_FREE(header->type);
435 if (header->parameter) emcore_mime_free_param(header->parameter);
436 EM_SAFE_FREE(header->subtype);
437 EM_SAFE_FREE(header->encoding);
438 EM_SAFE_FREE(header->desc);
439 EM_SAFE_FREE(header->disp_type);
440 EM_SAFE_FREE(header->content_id);
441 EM_SAFE_FREE(header->content_location);
442 EM_SAFE_FREE(header->priority);
443 EM_SAFE_FREE(header->ms_priority);
444 if (header->disp_parameter) emcore_mime_free_param(header->disp_parameter);
445 EM_SAFE_FREE(header);
449 void emcore_mime_free_message_header(struct _m_mesg_header *header)
451 EM_DEBUG_FUNC_BEGIN();
452 if (!header) return ;
453 EM_SAFE_FREE(header->version);
454 if (header->part_header) emcore_mime_free_part_header(header->part_header);
455 EM_SAFE_FREE(header);
459 void emcore_mime_free_rfc822_header(struct _rfc822header *header)
461 EM_DEBUG_FUNC_BEGIN();
462 if (!header) return ;
463 EM_SAFE_FREE(header->return_path);
464 EM_SAFE_FREE(header->received);
465 EM_SAFE_FREE(header->reply_to);
466 EM_SAFE_FREE(header->date);
467 EM_SAFE_FREE(header->from);
468 EM_SAFE_FREE(header->subject);
469 EM_SAFE_FREE(header->sender);
470 EM_SAFE_FREE(header->to);
471 EM_SAFE_FREE(header->cc);
472 EM_SAFE_FREE(header->bcc);
473 EM_SAFE_FREE(header);
477 void emcore_mime_free_part_body(struct _m_body *body)
479 EM_DEBUG_FUNC_BEGIN();
481 if (body->part_header) emcore_mime_free_part_header(body->part_header);
482 EM_SAFE_FREE(body->text);
483 if (body->nested.body) emcore_mime_free_part_body(body->nested.body);
484 if (body->nested.next) emcore_mime_free_part(body->nested.next);
489 void emcore_mime_free_part(struct _m_part *part)
491 EM_DEBUG_FUNC_BEGIN();
493 if (part->body) emcore_mime_free_part_body(part->body);
494 if (part->next) emcore_mime_free_part(part->next);
499 void emcore_mime_free_mime(struct _m_mesg *mmsg)
501 EM_DEBUG_FUNC_BEGIN();
504 if (mmsg->header) emcore_mime_free_message_header(mmsg->header);
505 if (mmsg->rfc822header) emcore_mime_free_rfc822_header(mmsg->rfc822header);
506 if (mmsg->nested.body) emcore_mime_free_part_body(mmsg->nested.body);
507 if (mmsg->nested.next) emcore_mime_free_part(mmsg->nested.next);
508 EM_SAFE_FREE(mmsg->text);
514 static char *em_parse_filename(char *filename)
516 EM_DEBUG_FUNC_BEGIN("filename [%p] ", filename);
518 EM_DEBUG_EXCEPTION("filename is NULL ");
524 static char parsed_filename[512] = {0, };
526 memset(parsed_filename, 0x00, 512);
528 if (!strstr(filename, delims)) {
529 EM_DEBUG_EXCEPTION("FileName does not contain @ ");
533 result = strtok(filename, delims);
535 if (strcasestr(result, ".bmp") || strcasestr(result, ".jpeg") || strcasestr(result, ".png") || strcasestr(result, ".jpg"))
536 sprintf(parsed_filename + EM_SAFE_STRLEN(parsed_filename), "%s", result);
538 sprintf(parsed_filename + EM_SAFE_STRLEN(parsed_filename), "%s%s", result, ".jpeg");
540 EM_DEBUG_LOG_SEC(">>> FileName [ %s ] ", result);
541 EM_DEBUG_LOG_SEC("parsed_filename [%s] ", parsed_filename);
543 return parsed_filename;
547 #define CONTENT_TYPE_STRING_IN_MIME_HEAEDER "Content-Type:"
549 INTERNAL_FUNC int emcore_get_content_type_from_mime_string(char *input_mime_string, char **output_content_type)
551 EM_DEBUG_FUNC_BEGIN("input_mime_string [%p], output_content_type [%p]", input_mime_string, output_content_type);
553 int err = EMAIL_ERROR_NONE;
554 int temp_mime_header_string_length = 0;
555 char result_content_type[256] = { 0, };
556 char *temp_mime_header_string = NULL;
557 char *temp_content_type_start = NULL;
558 char *temp_content_type_end = NULL;
560 if (input_mime_string == NULL || output_content_type == NULL) {
561 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
562 err = EMAIL_ERROR_INVALID_PARAM;
566 temp_mime_header_string_length = EM_SAFE_STRLEN(input_mime_string);
567 temp_mime_header_string = input_mime_string;
569 EM_DEBUG_LOG("temp_mime_header_string [%s]", temp_mime_header_string);
571 temp_content_type_start = strcasestr(temp_mime_header_string, CONTENT_TYPE_STRING_IN_MIME_HEAEDER);
573 if (temp_content_type_start == NULL) {
574 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_DATA");
575 err = EMAIL_ERROR_INVALID_DATA;
578 temp_content_type_start += EM_SAFE_STRLEN(CONTENT_TYPE_STRING_IN_MIME_HEAEDER);
579 temp_content_type_end = temp_content_type_start;
581 while (temp_content_type_end && temp_content_type_end < (temp_mime_header_string + temp_mime_header_string_length) && *temp_content_type_end != ';')
582 temp_content_type_end++;
584 if (temp_content_type_end && *temp_content_type_end == ';') {
585 if (temp_content_type_end - temp_content_type_start < 256) {
586 memcpy(result_content_type, temp_content_type_start, temp_content_type_end - temp_content_type_start);
587 EM_DEBUG_LOG_SEC("result_content_type [%s]", result_content_type);
588 *output_content_type = EM_SAFE_STRDUP(em_trim_left(result_content_type));
590 EM_DEBUG_EXCEPTION("temp_content_type_end - temp_content_type_start [%ld]", (long int)(temp_content_type_end - temp_content_type_start));
591 err = EMAIL_ERROR_DATA_TOO_LONG;
599 EM_DEBUG_FUNC_END("err [%d]", err);
603 INTERNAL_FUNC int emcore_get_utf8_address(char **dest, ADDRESS *address, int *err_code)
605 EM_DEBUG_FUNC_BEGIN("dest[%p], address[%p], err_code[%p]", dest, address, err_code);
607 if (!dest || !address) {
608 EM_DEBUG_EXCEPTION("dest[%p], address[%p]", dest, address);
609 if (err_code != NULL)
610 *err_code = EMAIL_ERROR_INVALID_PARAM;
615 int err = EMAIL_ERROR_NONE;
617 gchar *concatenated = NULL;
618 gchar *utf8_address = NULL;
620 char *nickname = NULL;
623 if (address->personal) {
624 if (!(nickname = emcore_gmime_get_decoding_text(address->personal))) {
625 EM_DEBUG_EXCEPTION("emcore_gmime_get_decoding_text failed - %d", err);
628 EM_DEBUG_LOG_DEV("nickname[%s]", nickname);
629 if (*nickname != '\0') {
630 char *tmp_nickname = nickname;
631 nickname = em_get_escaped_str(tmp_nickname);
632 EM_SAFE_FREE(tmp_nickname);
633 utf8_address = g_strdup_printf("\"%s\" <%s%s%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
635 utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
638 EM_SAFE_FREE(nickname);
640 if (address->mailbox || address->host)
641 utf8_address = g_strdup_printf("<%s%s%s>", address->mailbox ? address->mailbox : "", address->host ? "@" : "", address->host ? address->host : "");
644 EM_DEBUG_LOG_DEV("utf8_address[%s]", utf8_address);
647 if ((concatenated != NULL)) {
649 concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
652 concatenated = g_strdup(utf8_address);
655 g_free(utf8_address);
658 address = address->next;
661 *dest = concatenated;
666 EM_SAFE_FREE(nickname);
667 EM_DEBUG_FUNC_END("ret[%d]", ret);
671 #define SUBTYPE_STRING_LENGTH 128
673 INTERNAL_FUNC int emcore_get_content_type_from_mail_bodystruct(BODY *input_body, int input_buffer_length, char *output_content_type)
675 EM_DEBUG_FUNC_BEGIN("input_body [%p], input_buffer_length [%d], output_content_type [%p]", input_body, input_buffer_length, output_content_type);
676 int err = EMAIL_ERROR_NONE;
677 char subtype_string[SUBTYPE_STRING_LENGTH] = { 0 , };
679 if (input_body == NULL || output_content_type == NULL || input_buffer_length == 0) {
680 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
681 err = EMAIL_ERROR_INVALID_PARAM;
685 EM_SAFE_STRNCPY(subtype_string, input_body->subtype, SUBTYPE_STRING_LENGTH-1); /* prevent 21983 */
686 em_lower_string(subtype_string);
688 switch (input_body->type) {
690 SNPRINTF(output_content_type, input_buffer_length, "text/%s", subtype_string);
693 SNPRINTF(output_content_type, input_buffer_length, "multipart/%s", subtype_string);
696 SNPRINTF(output_content_type, input_buffer_length, "message/%s", subtype_string);
698 case TYPEAPPLICATION:
699 SNPRINTF(output_content_type, input_buffer_length, "application/%s", subtype_string);
702 SNPRINTF(output_content_type, input_buffer_length, "audio/%s", subtype_string);
705 SNPRINTF(output_content_type, input_buffer_length, "image/%s", subtype_string);
708 SNPRINTF(output_content_type, input_buffer_length, "video/%s", subtype_string);
711 SNPRINTF(output_content_type, input_buffer_length, "model/%s", subtype_string);
714 SNPRINTF(output_content_type, input_buffer_length, "other/%s", subtype_string);
717 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
718 err = EMAIL_ERROR_INVALID_PARAM;
723 EM_DEBUG_LOG_SEC("output_content_type [%s]", output_content_type);
727 EM_DEBUG_FUNC_END("err [%d]", err);
731 INTERNAL_FUNC int emcore_get_attribute_value_of_body_part(PARAMETER *input_param, char *atribute_name, char *output_value, int output_buffer_length, int with_rfc2047_text, int *err_code)
733 EM_DEBUG_FUNC_BEGIN_SEC("input_param [%p], atribute_name [%s], output_buffer_length [%d], with_rfc2047_text [%d]", input_param, atribute_name, output_buffer_length, with_rfc2047_text);
734 PARAMETER *temp_param = input_param;
735 char *decoded_value = NULL, *result_value = NULL;
736 int ret = false, err = EMAIL_ERROR_NONE;
739 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
740 err = EMAIL_ERROR_INVALID_PARAM;
744 memset(output_value, 0, output_buffer_length);
747 if (!strcasecmp(temp_param->attribute, atribute_name)) {
748 if (temp_param->value) {
749 if (with_rfc2047_text) {
750 decoded_value = emcore_gmime_get_decoding_text(temp_param->value);
751 result_value = EM_SAFE_STRDUP(decoded_value);
752 EM_SAFE_FREE(decoded_value);
754 result_value = g_strdup(temp_param->value);
756 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DATA_NOT_FOUND");
757 err = EMAIL_ERROR_DATA_NOT_FOUND;
762 if (output_buffer_length > EM_SAFE_STRLEN(result_value)) {
763 strncpy(output_value, result_value, output_buffer_length);
764 output_value[EM_SAFE_STRLEN(result_value)] = NULL_CHAR;
767 EM_DEBUG_EXCEPTION("buffer is too short");
768 err = EMAIL_ERROR_DATA_TOO_LONG;
775 temp_param = temp_param->next;
779 EM_SAFE_FREE(decoded_value);
780 EM_SAFE_FREE(result_value);
785 EM_DEBUG_FUNC_END("ret [%d]", ret);
789 /* get body structure */
790 INTERNAL_FUNC int emcore_get_body_structure(MAILSTREAM *stream, int msg_uid, BODY **body, int *err_code)
792 EM_DEBUG_FUNC_BEGIN("stream[%p], msg_uid[%d], body[%p], err_code[%p]", stream, msg_uid, body, err_code);
794 EM_IF_NULL_RETURN_VALUE(stream, false);
795 EM_IF_NULL_RETURN_VALUE(body, false);
797 #ifdef __FEATURE_HEADER_OPTIMIZATION__
798 ENVELOPE *env = mail_fetch_structure(stream, msg_uid, body, FT_UID | FT_PEEK | FT_NOLOOKAHEAD, 1);
800 ENVELOPE *env = mail_fetch_structure(stream, msg_uid, body, FT_UID | FT_PEEK | FT_NOLOOKAHEAD);
804 *err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
805 EM_DEBUG_EXCEPTION("mail_fetch_structure failed");
809 #ifdef FEATURE_CORE_DEBUG
810 _print_body(*body, true); /* shasikala.p@partner.samsung.com */
816 int emcore_set_fetch_part_section(BODY *body, char *section_pfx, int section_subno, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code);
818 /* set body section to be fetched */
819 INTERNAL_FUNC int emcore_set_fetch_body_section(BODY *body, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code)
821 EM_DEBUG_FUNC_BEGIN("body[%p], err_code[%p]", body, err_code);
824 EM_DEBUG_EXCEPTION("body [%p]", body);
825 if (err_code != NULL)
826 *err_code = EMAIL_ERROR_INVALID_PARAM;
830 // body->id = cpystr("1"); /* top level body */
831 EM_DEBUG_LOG_SEC("body->id : [%s]", body->id);
833 if (enable_inline_list) {
835 EM_SAFE_FREE(g_inline_list);
838 emcore_set_fetch_part_section(body, (char *)NULL, 0, enable_inline_list, total_mail_size, total_body_size, err_code);
840 if (body && body->id)
841 EM_DEBUG_LOG_SEC(">>>>> FILE NAME [%s] ", body->id);
843 EM_DEBUG_LOG(">>>>> BODY NULL ");
849 /* set part section of body to be fetched */
850 int emcore_set_fetch_part_section(BODY *body, char *section_pfx, int section_subno, int enable_inline_list, int *total_mail_size, int *total_body_size, int *err_code)
852 EM_DEBUG_FUNC_BEGIN("body[%p], section_pfx[%s], section_subno[%d], enable_inline_list[%d], total_mail_size[%p] err_code[%p]", body, section_pfx, section_subno, enable_inline_list, total_mail_size, err_code);
855 char section[64] = {0x00, };
857 /* multipart doesn't have a row to itself */
858 if (body->type == TYPEMULTIPART) {
859 /* if not first time, extend prefix */
861 SNPRINTF(section, sizeof(section), "%s%d.", section_pfx, ++section_subno);
866 for (section_subno = 0, part = body->nested.part; part; part = part->next)
867 emcore_set_fetch_part_section(&part->body, section, section_subno++, enable_inline_list, total_mail_size, total_body_size, err_code);
869 if (!section_pfx) /* dummy prefix if top level */
872 SNPRINTF(section, sizeof(section), "%s%d", section_pfx, ++section_subno);
874 if (enable_inline_list && ((body->disposition.type && (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')) ||
875 (!body->disposition.type && body->id))) {
877 temp = realloc(g_inline_list, sizeof(BODY *) *(g_inline_count + 1));
879 memset(temp+g_inline_count, 0x00, sizeof(BODY *));
880 g_inline_list = temp;
881 g_inline_list[g_inline_count] = body;
885 EM_DEBUG_EXCEPTION("realloc fails");
888 EM_DEBUG_LOG("Update g_inline_list with inline count [%d]", g_inline_count);
891 if (total_mail_size != NULL) {
892 *total_mail_size = *total_mail_size + (int)body->size.bytes;
893 EM_DEBUG_LOG("body->size.bytes [%lu]", body->size.bytes);
894 EM_DEBUG_LOG("total_mail_size [%d]", *total_mail_size);
897 if ((total_body_size != NULL) && !(body->disposition.type && (body->disposition.type[0] == 'a' || body->disposition.type[0] == 'A'))) {
898 *total_body_size = *total_body_size + (int)body->size.bytes;
899 EM_DEBUG_LOG("body->size.bytes [%lu]", body->size.bytes);
900 EM_DEBUG_LOG("total_mail_size [%d]", *total_body_size);
903 /* encapsulated message ? */
904 if ((body->type == TYPEMESSAGE) && !strcasecmp(body->subtype, "RFC822") && (body = ((MESSAGE *)body->nested.msg)->body)) {
905 if (body->type == TYPEMULTIPART) {
907 emcore_set_fetch_part_section(body, section, section_subno-1, enable_inline_list, total_mail_size, total_body_size, err_code);
908 } else { /* build encapsulation prefi */
909 SNPRINTF(section, sizeof(section), "%s%d.", section_pfx, section_subno);
910 emcore_set_fetch_part_section(body, section, 0, enable_inline_list, total_mail_size, total_body_size, err_code);
913 /* set body section */
915 body->sparep = cpystr(section);
921 static int emcore_make_mail_data_from_m_mesg(email_mail_data_t *dst_mail_data, struct _m_mesg *mmsg)
923 EM_DEBUG_FUNC_BEGIN();
924 int err = EMAIL_ERROR_NONE;
926 if (!mmsg || !dst_mail_data) {
927 EM_DEBUG_EXCEPTION("Invalid parameter");
928 err = EMAIL_ERROR_INVALID_PARAM;
932 char *encoded_subject = NULL;
933 char *first_address = NULL;
934 char *first_alias = NULL;
935 struct tm temp_time_info;
936 ADDRESS *from = NULL;
940 ADDRESS *return_path = NULL;
941 ADDRESS *reply_to = NULL;
942 MESSAGECACHE mail_cache_element = {0, };
943 memset(&mail_cache_element, 0x00, sizeof(MESSAGECACHE));
944 memset((void *)&temp_time_info, 0, sizeof(struct tm));
945 /* Set the smime_type */
946 if (mmsg->rfc822header && mmsg->rfc822header->content_type) {
947 if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-mime")) {
948 if (strcasestr(mmsg->rfc822header->content_type, "enveloped-data"))
949 dst_mail_data->smime_type = EMAIL_SMIME_ENCRYPTED;
951 dst_mail_data->smime_type = EMAIL_SMIME_SIGNED_AND_ENCRYPTED;
952 } else if (strcasestr(mmsg->rfc822header->content_type, "encrypted")) {
953 dst_mail_data->smime_type = EMAIL_PGP_ENCRYPTED;
954 } else if (strcasestr(mmsg->rfc822header->content_type, "signed")) {
955 if (strcasestr(mmsg->rfc822header->content_type, "pkcs7-signature"))
956 dst_mail_data->smime_type = EMAIL_SMIME_SIGNED;
958 dst_mail_data->smime_type = EMAIL_PGP_SIGNED;
960 dst_mail_data->digest_type = emcore_get_digest_type(mmsg->rfc822header->content_type);
962 dst_mail_data->smime_type = EMAIL_SMIME_NONE;
966 /* Set the priority */
967 if (mmsg->rfc822header) {
968 if (mmsg->rfc822header->priority) {
969 switch (atoi(mmsg->rfc822header->priority)) {
971 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
974 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
978 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
981 } else if (mmsg->rfc822header->ms_priority) {
982 if (strcasestr(mmsg->rfc822header->ms_priority, "HIGH"))
983 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
984 else if (strcasestr(mmsg->rfc822header->ms_priority, "NORMAL"))
985 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
986 else if (strcasestr(mmsg->rfc822header->ms_priority, "LOW"))
987 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
989 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
991 } else if (mmsg->nested.body) {
992 if (mmsg->nested.body->part_header->priority) {
993 switch (atoi(mmsg->nested.body->part_header->priority)) {
995 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
998 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
1002 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1005 } else if (mmsg->nested.body->part_header->ms_priority) {
1006 if (strcasestr(mmsg->nested.body->part_header->ms_priority, "HIGH"))
1007 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_HIGH;
1008 else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "NORMAL"))
1009 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1010 else if (strcasestr(mmsg->nested.body->part_header->ms_priority, "LOW"))
1011 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_LOW;
1013 dst_mail_data->priority = EMAIL_MAIL_PRIORITY_NORMAL;
1017 if (!mmsg->rfc822header) {
1018 EM_DEBUG_LOG("This mail did not have envelop");
1023 if (mmsg->rfc822header->date) {
1024 EM_DEBUG_LOG("date : [%s]", mmsg->rfc822header->date);
1025 mail_parse_date(&mail_cache_element, (unsigned char *)mmsg->rfc822header->date);
1028 temp_time_info.tm_sec = mail_cache_element.seconds;
1029 temp_time_info.tm_min = mail_cache_element.minutes - mail_cache_element.zminutes;
1030 temp_time_info.tm_hour = mail_cache_element.hours - mail_cache_element.zhours;
1032 if (mail_cache_element.hours - mail_cache_element.zhours < 0) {
1033 temp_time_info.tm_mday = mail_cache_element.day - 1;
1034 temp_time_info.tm_hour += 24;
1036 temp_time_info.tm_mday = mail_cache_element.day;
1038 temp_time_info.tm_mon = mail_cache_element.month - 1;
1039 temp_time_info.tm_year = mail_cache_element.year + 70;
1041 dst_mail_data->date_time = timegm(&temp_time_info);
1044 /* Set the subject */
1045 dst_mail_data->subject = EM_SAFE_STRDUP(mmsg->rfc822header->subject);
1047 /* Set the email address(from, to, cc, bcc, received ...) */
1048 dst_mail_data->email_address_recipient = EM_SAFE_STRDUP(mmsg->rfc822header->received);
1050 if (mmsg->rfc822header->from) {
1051 rfc822_parse_adrlist(&from, mmsg->rfc822header->from, NULL);
1052 if (!emcore_get_utf8_address(&dst_mail_data->full_address_from, from, &err)) {
1053 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->from);
1057 if (mmsg->rfc822header->to) {
1058 rfc822_parse_adrlist(&to, mmsg->rfc822header->to, NULL);
1059 if (!emcore_get_utf8_address(&dst_mail_data->full_address_to, to, &err)) {
1060 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->to);
1064 if (mmsg->rfc822header->cc) {
1065 rfc822_parse_adrlist(&cc, mmsg->rfc822header->cc, NULL);
1066 if (!emcore_get_utf8_address(&dst_mail_data->full_address_cc, cc, &err)) {
1067 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->cc);
1071 if (mmsg->rfc822header->bcc) {
1072 rfc822_parse_adrlist(&bcc, mmsg->rfc822header->bcc, NULL);
1073 if (!emcore_get_utf8_address(&dst_mail_data->full_address_bcc, bcc, &err)) {
1074 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->bcc);
1078 if (mmsg->rfc822header->return_path) {
1079 rfc822_parse_adrlist(&return_path, mmsg->rfc822header->return_path, NULL);
1080 if (!emcore_get_utf8_address(&dst_mail_data->full_address_return, return_path, &err)) {
1081 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->return_path);
1085 if (mmsg->rfc822header->reply_to) {
1086 rfc822_parse_adrlist(&reply_to, mmsg->rfc822header->reply_to, NULL);
1087 if (!emcore_get_utf8_address(&dst_mail_data->full_address_reply, reply_to, &err)) {
1088 EM_DEBUG_EXCEPTION_SEC("emcore_get_utf8_address failed : [%d], [%s]", err, mmsg->rfc822header->reply_to);
1092 if (emcore_get_first_address(dst_mail_data->full_address_from, &first_alias, &first_address) == true) {
1093 dst_mail_data->alias_sender = EM_SAFE_STRDUP(first_alias);
1094 dst_mail_data->email_address_sender = EM_SAFE_STRDUP(first_address);
1097 EM_SAFE_FREE(encoded_subject);
1098 EM_SAFE_FREE(first_alias);
1099 EM_SAFE_FREE(first_address);
1102 mail_free_address(&from);
1105 mail_free_address(&to);
1108 mail_free_address(&cc);
1111 mail_free_address(&bcc);
1114 mail_free_address(&return_path);
1117 mail_free_address(&reply_to);
1118 EM_DEBUG_FUNC_END("err : [%d]", err);
1122 INTERNAL_FUNC int emcore_make_mail_data_from_mime_data(struct _m_mesg *mmsg,
1123 struct _m_content_info *cnt_info,
1124 email_mail_data_t **output_mail_data,
1125 email_attachment_data_t **output_attachment_data,
1126 int *output_attachment_count,
1129 EM_DEBUG_FUNC_BEGIN();
1132 int eml_mail_id = 0;
1133 int err = EMAIL_ERROR_NONE;
1134 int save_status = EMAIL_BODY_DOWNLOAD_STATUS_NONE;
1135 int attachment_num = 0;
1136 int local_attachment_count = 0;
1137 int local_inline_content_count = 0;
1140 char html_body[MAX_PATH] = {0, };
1141 char *multi_user_name = NULL;
1143 struct attachment_info *ai = NULL;
1144 email_attachment_data_t *attachment = NULL;
1145 email_mail_data_t *p_mail_data = NULL;
1147 if (!mmsg || !cnt_info || !output_mail_data || !output_attachment_data) {
1148 EM_DEBUG_EXCEPTION("Invalid parameter");
1149 err = EMAIL_ERROR_INVALID_PARAM;
1153 /* Create rand mail id of eml */
1154 gettimeofday(&tv, NULL);
1156 unsigned int seed = time(NULL);
1157 eml_mail_id = rand_r(&seed);
1159 p_mail_data = (email_mail_data_t *)em_malloc(sizeof(email_mail_data_t));
1160 if (p_mail_data == NULL) {
1161 EM_DEBUG_EXCEPTION("em_mallocfailed");
1162 err = EMAIL_ERROR_OUT_OF_MEMORY;
1166 p_mail_data->mail_id = eml_mail_id;
1167 p_mail_data->account_id = EML_FOLDER;
1168 p_mail_data->mail_size = cnt_info->total_body_size;
1170 if ((err = emcore_make_mail_data_from_m_mesg(p_mail_data, mmsg)) != EMAIL_ERROR_NONE) {
1171 EM_DEBUG_EXCEPTION("emcore_make_mail_data_from_m_mesg failed : [%d]", err);
1174 EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
1176 if (cnt_info->text.plain) {
1177 memset(move_buf, 0x00, sizeof(move_buf));
1178 memset(path_buf, 0x00, sizeof(path_buf));
1179 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
1180 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1181 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1185 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1186 cnt_info->text.plain_charset ? cnt_info->text.plain_charset : UNKNOWN_CHARSET_PLAIN_TEXT_FILE,
1187 move_buf, path_buf, sizeof(path_buf), &err)) {
1188 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1192 if (!emstorage_move_file(cnt_info->text.plain, move_buf, false, &err)) {
1193 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1197 p_mail_data->file_path_plain = EM_SAFE_STRDUP(path_buf);
1198 save_status = cnt_info->text.plain_save_status;
1201 if (cnt_info->text.html) {
1202 memset(move_buf, 0x00, sizeof(move_buf));
1203 memset(path_buf, 0x00, sizeof(path_buf));
1204 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1205 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1209 if (cnt_info->text.html_charset != NULL) {
1210 SNPRINTF(html_body, MAX_PATH, "%s%s", cnt_info->text.html_charset, HTML_EXTENSION_STRING);
1212 g_strlcpy(html_body, UNKNOWN_CHARSET_HTML_TEXT_FILE, MAX_PATH);
1215 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1216 0, html_body, move_buf, path_buf,
1217 sizeof(path_buf), &err)) {
1218 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1222 if (!emstorage_move_file(cnt_info->text.html, move_buf, false, &err)) {
1223 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1227 p_mail_data->file_path_html = EM_SAFE_STRDUP(path_buf);
1228 save_status = cnt_info->text.html_save_status;
1231 if (cnt_info->text.mime_entity) {
1232 memset(move_buf, 0x00, sizeof(move_buf));
1233 memset(path_buf, 0x00, sizeof(path_buf));
1235 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1236 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1240 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1241 0, "mime_entity", move_buf, path_buf,
1242 sizeof(path_buf), &err)) {
1243 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1247 if (!emstorage_move_file(cnt_info->text.mime_entity, move_buf, false, &err)) {
1248 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1252 p_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(path_buf);
1255 if ((err = emcore_get_preview_text_from_file(multi_user_name,
1256 p_mail_data->file_path_plain,
1257 p_mail_data->file_path_html,
1258 MAX_PREVIEW_TEXT_LENGTH,
1259 &p_mail_data->preview_text)) != EMAIL_ERROR_NONE)
1260 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file error [%d]", err);
1262 for (ai = cnt_info->file; ai; ai = ai->next, local_attachment_count++) {}
1263 EM_DEBUG_LOG("local_attachment_count : [%d]", local_attachment_count);
1265 for (ai = cnt_info->inline_file; ai; ai = ai->next, local_inline_content_count++) {}
1266 EM_DEBUG_LOG("local_inline_content_count : [%d]", local_inline_content_count);
1268 attachment_num = local_attachment_count + local_inline_content_count;
1270 if (attachment_num > 0) {
1271 attachment = (email_attachment_data_t *)em_malloc(sizeof(email_attachment_data_t) * attachment_num);
1272 if (attachment == NULL) {
1273 EM_DEBUG_EXCEPTION("em_mallocfailed");
1274 err = EMAIL_ERROR_OUT_OF_MEMORY;
1278 for (ai = cnt_info->file; ai; ai = ai->next, i++) {
1279 attachment[i].attachment_id = i + 1;
1280 attachment[i].attachment_size = ai->size;
1281 attachment[i].attachment_name = EM_SAFE_STRDUP(ai->name);
1282 attachment[i].content_id = EM_SAFE_STRDUP(ai->content_id);
1283 attachment[i].drm_status = ai->drm;
1284 attachment[i].save_status = ai->save_status;
1285 attachment[i].inline_content_status = 0;
1286 attachment[i].attachment_mime_type = EM_SAFE_STRDUP(ai->attachment_mime_type);
1287 #ifdef __ATTACHMENT_OPTI__
1288 attachment[i].encoding = ai->encoding;
1289 attachment[i].section = ai->section;
1291 EM_DEBUG_LOG("attachment[%d].attachment_id[%d]", i, attachment[i].attachment_id);
1292 EM_DEBUG_LOG("attachment[%d].attachment_size[%d]", i, attachment[i].attachment_size);
1293 EM_DEBUG_LOG_SEC("attachment[%d].attachment_name[%s]", i, attachment[i].attachment_name);
1294 EM_DEBUG_LOG("attachment[%d].drm_status[%d]", i, attachment[i].drm_status);
1295 EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
1296 EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
1299 memset(move_buf, 0x00, sizeof(move_buf));
1300 memset(path_buf, 0x00, sizeof(path_buf));
1302 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, i + 1, &err)) {
1303 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1307 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1308 i + 1, attachment[i].attachment_name, move_buf,
1309 path_buf, sizeof(path_buf), &err)) {
1310 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1314 if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
1315 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1317 /* delete all created files. */
1318 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id,
1319 0, NULL, move_buf, path_buf,
1320 sizeof(path_buf), NULL)) {
1321 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
1322 /* goto FINISH_OFF; */
1325 if (!emstorage_delete_dir(move_buf, NULL)) {
1326 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
1327 /* goto FINISH_OFF; */
1334 attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
1337 EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
1338 save_status = ai->save_status;
1341 for (ai = cnt_info->inline_file; ai; ai = ai->next, i++) {
1342 attachment[i].attachment_id = i + 1;
1343 attachment[i].attachment_size = ai->size;
1344 attachment[i].attachment_name = EM_SAFE_STRDUP(ai->name);
1345 attachment[i].content_id = EM_SAFE_STRDUP(ai->content_id);
1346 attachment[i].drm_status = ai->drm;
1347 attachment[i].save_status = ai->save_status;
1348 attachment[i].inline_content_status = INLINE_ATTACHMENT;
1349 attachment[i].attachment_mime_type = EM_SAFE_STRDUP(ai->attachment_mime_type);
1350 #ifdef __ATTACHMENT_OPTI__
1351 attachment[i].encoding = ai->encoding;
1352 attachment[i].section = ai->section;
1354 EM_DEBUG_LOG("attachment[%d].attachment_id[%d]", i, attachment[i].attachment_id);
1355 EM_DEBUG_LOG("attachment[%d].attachment_size[%d]", i, attachment[i].attachment_size);
1356 EM_DEBUG_LOG_SEC("attachment[%d].attachment_name[%s]", i, attachment[i].attachment_name);
1357 EM_DEBUG_LOG("attachment[%d].drm_status[%d]", i, attachment[i].drm_status);
1358 EM_DEBUG_LOG("attachment[%d].inline_content_status[%d]", i, attachment[i].inline_content_status);
1359 EM_DEBUG_LOG("attachment[%d].attachment_mime_type : [%s]", i, attachment[i].attachment_mime_type);
1362 memset(move_buf, 0x00, sizeof(move_buf));
1363 memset(path_buf, 0x00, sizeof(path_buf));
1365 if (!emstorage_create_dir(multi_user_name, EML_FOLDER, eml_mail_id, 0, &err)) {
1366 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
1370 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1371 attachment[i].attachment_name, move_buf, path_buf,
1372 sizeof(path_buf), &err)) {
1373 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
1377 if (!emstorage_move_file(ai->save, move_buf, false, &err)) {
1378 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
1380 /* delete all created files. */
1381 if (!emstorage_get_save_name(multi_user_name, EML_FOLDER, eml_mail_id, 0,
1382 NULL, move_buf, path_buf, sizeof(path_buf), NULL)) {
1383 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
1384 /* goto FINISH_OFF; */
1387 if (!emstorage_delete_dir(move_buf, NULL)) {
1388 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
1389 /* goto FINISH_OFF; */
1396 attachment[i].attachment_path = EM_SAFE_STRDUP(path_buf);
1399 EM_DEBUG_LOG_SEC("attachment[%d].attachment_path[%s]", i, attachment[i].attachment_path);
1400 save_status = ai->save_status;
1403 EM_DEBUG_LOG("Check #1");
1404 EM_DEBUG_LOG("save_status : [%d]", save_status);
1406 p_mail_data->attachment_count = local_attachment_count;
1407 p_mail_data->inline_content_count = local_inline_content_count;
1408 p_mail_data->body_download_status = (save_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) ? 1 : 0;
1410 eml_data_count += 1;
1416 if (output_mail_data)
1417 *output_mail_data = p_mail_data;
1419 if (output_attachment_data)
1420 *output_attachment_data = attachment;
1422 if (output_attachment_count)
1423 *output_attachment_count = attachment_num;
1426 emcore_free_mail_data(p_mail_data);
1427 EM_SAFE_FREE(p_mail_data);
1431 emcore_free_attachment_data(&attachment, attachment_num, NULL);
1438 EM_DEBUG_FUNC_END("ret : [%d]", ret);
1442 INTERNAL_FUNC int emcore_parse_mime_file_to_mail(char *eml_file_path,
1443 email_mail_data_t **output_mail_data,
1444 email_attachment_data_t **output_attachment_data,
1445 int *output_attachment_count,
1448 EM_DEBUG_FUNC_BEGIN_SEC("eml_file_path : [%s], output_mail_data : [%p]", eml_file_path, output_mail_data);
1450 int err = EMAIL_ERROR_NONE;
1452 char *mime_entity = NULL;
1453 struct _m_content_info *cnt_info = NULL;
1454 struct _m_mesg *mmsg = NULL;
1457 if (!eml_file_path || !output_mail_data || !output_attachment_data || !output_attachment_count) {
1458 EM_DEBUG_EXCEPTION("Invalid paramter");
1459 err = EMAIL_ERROR_INVALID_PARAM;
1463 cnt_info = (struct _m_content_info *)em_malloc(sizeof(struct _m_content_info));
1464 if (cnt_info == NULL) {
1465 EM_DEBUG_EXCEPTION("em_mallocfailed...");
1466 err = EMAIL_ERROR_OUT_OF_MEMORY;
1470 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
1472 mmsg = (struct _m_mesg *)em_malloc(sizeof(struct _m_mesg));
1474 EM_DEBUG_EXCEPTION("em_mallocfailed");
1475 err = EMAIL_ERROR_OUT_OF_MEMORY;
1479 mmsg->rfc822header = (struct _rfc822header *)em_malloc(sizeof(struct _rfc822header));
1480 if (mmsg->rfc822header == NULL) {
1481 EM_DEBUG_EXCEPTION("em_mallocfailed");
1482 err = EMAIL_ERROR_OUT_OF_MEMORY;
1486 emcore_gmime_init();
1487 if (!emcore_gmime_eml_parse_mime(eml_file_path, mmsg->rfc822header, cnt_info, &err)) {
1488 emcore_gmime_shutdown();
1489 EM_DEBUG_EXCEPTION("emcore_gmime_parse_mime failed : [%d]", err);
1490 err = EMAIL_ERROR_INVALID_DATA;
1494 if (!emcore_make_mail_data_from_mime_data(mmsg,
1497 output_attachment_data,
1498 output_attachment_count,
1500 emcore_gmime_shutdown();
1501 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_mime failed : [%d]", err);
1505 emcore_gmime_shutdown();
1512 emcore_mime_free_mime(mmsg);
1515 emcore_free_content_info(cnt_info);
1516 EM_SAFE_FREE(cnt_info);
1519 EM_SAFE_FREE(mime_entity);
1524 EM_DEBUG_FUNC_END("err : %d", err);
1528 INTERNAL_FUNC int emcore_delete_parsed_data(char *multi_user_name, email_mail_data_t *input_mail_data, int *err_code)
1530 EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1531 int err = EMAIL_ERROR_NONE;
1533 char buf[MAX_PATH] = {0};
1534 char *prefix_path = NULL;
1536 if (!input_mail_data) {
1537 EM_DEBUG_EXCEPTION("Invliad parameter");
1538 err = EMAIL_ERROR_INVALID_PARAM;
1542 if ((input_mail_data->account_id != EML_FOLDER) && (!input_mail_data->mail_id)) {
1543 EM_DEBUG_EXCEPTION("Invliad parameter: account_id[%d], mail_id[%d]", input_mail_data->account_id, input_mail_data->mail_id);
1544 err = EMAIL_ERROR_INVALID_PARAM;
1547 if (EM_SAFE_STRLEN(multi_user_name) > 0) {
1548 err = emcore_get_container_path(multi_user_name, &prefix_path);
1549 if (err != EMAIL_ERROR_NONE) {
1550 EM_DEBUG_EXCEPTION("emcore_get_container_path failed :[%d]", err);
1554 prefix_path = strdup("");
1557 if (!input_mail_data->mail_id) {
1558 SNPRINTF(buf, sizeof(buf), "%s%s%s%d", prefix_path, MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id);
1560 SNPRINTF(buf, sizeof(buf), "%s%s%s%d%s%d", prefix_path, MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id, DIR_SEPERATOR, input_mail_data->mail_id);
1563 if (!emstorage_delete_dir(buf, &err)) {
1564 EM_DEBUG_LOG("emstorage_delete_dir failed : buf[%s]", buf);
1565 memset(buf, 0x00, sizeof(buf));
1566 SNPRINTF(buf, sizeof(buf), "%s%s%d", MAIL_HOME, DIR_SEPERATOR, input_mail_data->account_id);
1568 if (!emstorage_delete_dir(buf, &err)) {
1569 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed : buf[%s]", buf);
1573 err = EMAIL_ERROR_NONE;
1580 EM_SAFE_FREE(prefix_path);
1588 INTERNAL_FUNC int emcore_get_mime_entity(char *mime_path, char **output_path, int *err_code)
1590 EM_DEBUG_FUNC_BEGIN("mime_path : [%s], output_path : [%p]", mime_path, output_path);
1591 int err = EMAIL_ERROR_NONE;
1594 char *tmp_path = NULL;
1597 EM_DEBUG_EXCEPTION("Invalid parameter");
1598 err = EMAIL_ERROR_INVALID_PARAM;
1602 err = em_open(mime_path, O_RDONLY, 0, &fd);
1603 if (err != EMAIL_ERROR_NONE) {
1604 EM_DEBUG_EXCEPTION("em_open failed");
1608 emcore_gmime_init();
1609 tmp_path = emcore_gmime_get_mime_entity(fd);
1610 emcore_gmime_shutdown();
1612 if (tmp_path == NULL) {
1613 EM_DEBUG_EXCEPTION("emcore_gmime_get_mime_entity failed");
1614 err = EMAIL_ERROR_INVALID_DATA;
1628 *output_path = tmp_path;
1630 EM_DEBUG_FUNC_END();
1634 INTERNAL_FUNC int emcore_get_digest_type(char *micalg_value)
1636 EM_DEBUG_FUNC_BEGIN();
1637 int digest_type = DIGEST_TYPE_NONE;
1639 if (micalg_value == NULL)
1640 return DIGEST_TYPE_NONE;
1642 if (strcasestr(micalg_value, "sha1"))
1643 digest_type = DIGEST_TYPE_SHA1;
1644 else if (strcasestr(micalg_value, "md5"))
1645 digest_type = DIGEST_TYPE_MD5;
1646 else if (strcasestr(micalg_value, "ripemd160"))
1647 digest_type = DIGEST_TYPE_RIPEMD160;
1648 else if (strcasestr(micalg_value, "md2"))
1649 digest_type = DIGEST_TYPE_MD2;
1650 else if (strcasestr(micalg_value, "tiger192"))
1651 digest_type = DIGEST_TYPE_TIGER192;
1652 else if (strcasestr(micalg_value, "haval5160"))
1653 digest_type = DIGEST_TYPE_HAVAL5160;
1654 else if (strcasestr(micalg_value, "sha256"))
1655 digest_type = DIGEST_TYPE_SHA256;
1656 else if (strcasestr(micalg_value, "sha384"))
1657 digest_type = DIGEST_TYPE_SHA384;
1658 else if (strcasestr(micalg_value, "sha512"))
1659 digest_type = DIGEST_TYPE_SHA512;
1660 else if (strcasestr(micalg_value, "sha224"))
1661 digest_type = DIGEST_TYPE_SHA224;
1662 else if (strcasestr(micalg_value, "md4"))
1663 digest_type = DIGEST_TYPE_MD4;
1665 digest_type = DIGEST_TYPE_NONE;
1667 EM_DEBUG_FUNC_END();