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.
23 /******************************************************************************
24 * File : email-core-mailbox-sync.c
25 * Desc : Mail Header Sync
28 *****************************************************************************/
35 #include "email-internal-types.h"
37 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
38 #include <vconf/vconf-internal-email-keys.h>
39 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
44 #include "email-utilities.h"
45 #include "email-convert.h"
46 #include "email-core-mailbox-sync.h"
47 #include "email-core-global.h"
48 #include "email-core-imap-mailbox.h"
49 #include "email-core-event.h"
50 #include "email-core-mailbox.h"
51 #include "email-core-mail.h"
52 #include "email-core-mime.h"
53 #include "email-core-utils.h"
54 #include "email-core-smtp.h"
55 #include "email-core-account.h"
56 #include "email-storage.h"
57 #include "email-core-signal.h"
59 #include "email-debug-log.h"
61 #define MAX_CHARSET_VALUE 256
63 static char g_append_uid_rsp[129]; /* added for getting server response */
65 extern void imap_parse_body_structure (MAILSTREAM *stream, BODY *body, unsigned char **txtptr, IMAPPARSEDREPLY *reply);
67 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
68 static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buffer, int item_count);
69 static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream, char *tag, IMAPPARSEDREPLY **reply, int input_download_size, int item_count);
70 static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int mailbox_id, int *err_code);
73 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
74 static char g_append_uid_rsp[129]; /* added for getting server response */
78 int pop3_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code)
80 EM_DEBUG_FUNC_BEGIN();
83 int err = EMAIL_ERROR_NONE;
85 POP3LOCAL *pop3local = NULL;
87 char *response = NULL;
89 if (!stream || !size) {
90 EM_DEBUG_EXCEPTION(" stream[%p], msgno[%d], size[%p]\n", stream, msgno, size);
92 err = EMAIL_ERROR_INVALID_PARAM;
96 if (!(pop3local = stream->local) || !pop3local->netstream) {
97 err = EMAIL_ERROR_INVALID_STREAM;
101 memset(command, 0x00, sizeof(command));
103 SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
105 /* EM_DEBUG_LOG(" [POP3] >>> %s", command); */
107 /* send command : get rfc822 size by msgno */
108 if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command))) {
109 EM_DEBUG_EXCEPTION(" net_sout failed...");
111 err = EMAIL_ERROR_INVALID_RESPONSE;
115 /* receive response */
116 if (!(response = net_getline(pop3local->netstream))) {
117 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN; */
121 /* EM_DEBUG_LOG(" [POP3] <<< %s", response); */
123 if (*response == '+') { /* "+ OK" */
126 if (!(p = strchr(response + strlen("+OK "), ' '))) {
127 err = EMAIL_ERROR_INVALID_RESPONSE;
133 else if (*response == '-') { /* "- ERR" */
134 err = EMAIL_ERROR_POP3_LIST_FAILURE;
138 err = EMAIL_ERROR_INVALID_RESPONSE;
145 EM_SAFE_FREE(response);
147 if (err_code != NULL)
150 EM_DEBUG_FUNC_END("ret [%d]", ret);
154 int imap4_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code)
156 EM_DEBUG_FUNC_BEGIN();
159 int err = EMAIL_ERROR_NONE;
161 IMAPLOCAL *imaplocal = NULL;
162 char tag[32], command[128];
163 char *response = NULL;
165 if (!stream || !size) {
166 EM_DEBUG_EXCEPTION("stream[%p], msgno[%d], size[%p]", stream, msgno, size);
168 err = EMAIL_ERROR_INVALID_PARAM;
172 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
173 err = EMAIL_ERROR_INVALID_STREAM;
177 memset(tag, 0x00, sizeof(tag));
178 memset(command, 0x00, sizeof(command));
180 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
181 SNPRINTF(command, sizeof(command), "%s FETCH %d RFC822.SIZE\015\012", tag, msgno);
183 /* EM_DEBUG_LOG(" [IMAP4] >>> %s", command); */
185 /* send command : get rfc822 size by msgno */
186 if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
187 EM_DEBUG_EXCEPTION(" net_sout failed...");
189 err = EMAIL_ERROR_INVALID_RESPONSE;
193 while (imaplocal->netstream) {
197 /* receive response */
198 if (!(response = net_getline(imaplocal->netstream)))
201 /* EM_DEBUG_LOG(" [IMAP4] <<< %s", response); */
203 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
204 if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
205 EM_SAFE_FREE(response);
208 else { /* 'NO' or 'BAD' */
209 err = EMAIL_ERROR_IMAP4_FETCH_SIZE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
213 else { /* untagged response */
214 if (*response == '*') {
215 if (!(t = strstr(response, "FETCH (RFC822.SIZE "))) {
216 EM_SAFE_FREE(response);
220 s = t + strlen("FETCH (RFC822.SIZE ");
222 if (!(t = strchr(s, ' '))) {
223 err = EMAIL_ERROR_INVALID_RESPONSE;
233 EM_SAFE_FREE(response);
239 EM_SAFE_FREE(response);
241 if (err_code != NULL)
244 EM_DEBUG_FUNC_END("ret [%d]", ret);
248 int pop3_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
250 EM_PROFILE_BEGIN(pop3MailboxGetuid);
251 EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
254 int err = EMAIL_ERROR_NONE;
256 POP3LOCAL *pop3local = NULL;
258 char *response = NULL;
259 emcore_uid_list *uid_elem = NULL;
261 if (!stream || !uid_list) {
262 EM_DEBUG_EXCEPTION("stream[%p], uid_list[%p]n", stream, uid_list);
263 err = EMAIL_ERROR_INVALID_PARAM;
267 if (!(pop3local = stream->local) || !pop3local->netstream) {
268 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
269 err = EMAIL_ERROR_INVALID_STREAM;
273 memset(command, 0x00, sizeof(command));
275 SNPRINTF(command, sizeof(command), "UIDL\015\012");
277 #ifdef FEATURE_CORE_DEBUG
278 EM_DEBUG_LOG(" [POP3] >>> [%s]", command);
281 /* send command : get msgno/uid for all message */
282 if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command))) {
283 EM_DEBUG_EXCEPTION("net_sout failed...");
284 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN; */
290 while (pop3local->netstream) {
293 /* receive response */
294 if (!(response = net_getline(pop3local->netstream))) {
295 EM_DEBUG_EXCEPTION("net_getline failed...");
296 err = EMAIL_ERROR_INVALID_RESPONSE;
300 #ifdef FEATURE_CORE_DEBUG
301 EM_DEBUG_LOG(" [POP3] <<< [%s]", response);
304 if (*response == '-') { /* "-ERR" */
305 err = EMAIL_ERROR_POP3_UIDL_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
309 if (*response == '+') { /* "+OK" */
310 free(response); response = NULL;
314 if (*response == '.') {
315 free(response); response = NULL;
319 if ((p = strchr(response, ' '))) {
322 if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
323 EM_DEBUG_EXCEPTION("malloc failed...");
324 err = EMAIL_ERROR_OUT_OF_MEMORY;
328 uid_elem->msgno = atoi(response);
329 uid_elem->uid = EM_SAFE_STRDUP(p + 1);
331 if (*uid_list != NULL)
332 uid_elem->next = *uid_list; /* prepend new data to table */
334 *uid_list = uid_elem;
337 err = EMAIL_ERROR_INVALID_RESPONSE;
341 free(response); response = NULL;
347 if (response != NULL)
350 if (err_code != NULL)
353 EM_PROFILE_END(pop3MailboxGetuid);
354 EM_DEBUG_FUNC_END("ret [%d]", ret);
358 int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
360 EM_PROFILE_BEGIN(ImapMailboxGetUids);
361 EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
364 int err = EMAIL_ERROR_NONE;
366 IMAPLOCAL *imaplocal = NULL;
367 char tag[16], command[64];
368 char *response = NULL;
369 emcore_uid_list *uid_elem = NULL;
371 if (!stream || !uid_list) {
372 EM_DEBUG_EXCEPTION("stream[%p], uid_list[%p]", stream, uid_list);
373 err = EMAIL_ERROR_INVALID_PARAM;
377 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
378 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
379 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN */
383 if (stream->nmsgs == 0){
384 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
387 memset(tag, 0x00, sizeof(tag));
388 memset(command, 0x00, sizeof(command));
390 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
391 SNPRINTF(command, sizeof(command), "%s FETCH 1:* (FLAGS UID)\015\012", tag);
392 EM_DEBUG_LOG("COMMAND [%s] \n", command);
393 #ifdef FEATURE_CORE_DEBUG
394 EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
397 /* send command : get msgno/uid for all message */
398 if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
399 EM_DEBUG_EXCEPTION(" net_sout failed...\n");
400 err = EMAIL_ERROR_CONNECTION_BROKEN;
406 while (imaplocal->netstream) {
412 /* receive response */
413 if (!(response = net_getline(imaplocal->netstream))) {
414 EM_DEBUG_EXCEPTION("net_getline failed...");
415 err = EMAIL_ERROR_INVALID_RESPONSE;
419 #ifdef FEATURE_CORE_DEBUG
420 EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
423 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
424 if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
425 free(response); response = NULL;
428 else { /* 'NO' or 'BAD' */
429 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
434 if ((p = strstr(response, " FETCH ("))) {
435 if (!strstr(p, "\\Deleted")) { /* undeleted only */
436 *p = '\0'; p += strlen(" FETCH ");
438 seen = strstr(p, "\\Seen") ? 1 : 0;
439 draft = strstr(p, "\\Draft") ? 1 : 0;
440 forwarded = strstr(p, "$Forwarded") ? 1 : 0;
442 if ((p = strstr(p, "UID "))) {
443 s = p + strlen("UID ");
450 if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
451 EM_DEBUG_EXCEPTION("em_malloc failed...");
452 err = EMAIL_ERROR_OUT_OF_MEMORY;
456 uid_elem->msgno = atoi(response + strlen("* "));
457 uid_elem->uid = EM_SAFE_STRDUP(p + strlen("UID "));
458 uid_elem->flag.seen = seen;
459 uid_elem->flag.draft = draft;
460 uid_elem->flag.forwarded = forwarded;
461 if (*uid_list != NULL)
462 uid_elem->next = *uid_list; /* prepend new data to list */
464 *uid_list = uid_elem;
467 err = EMAIL_ERROR_INVALID_RESPONSE;
473 err = EMAIL_ERROR_INVALID_RESPONSE;
477 EM_SAFE_FREE(response);;
483 EM_SAFE_FREE(response);
485 if (err_code != NULL)
488 EM_PROFILE_END(ImapMailboxGetUids);
489 EM_DEBUG_FUNC_END("ret [%d]", ret);
493 static char *__em_get_month_in_string(int month)
495 EM_DEBUG_FUNC_BEGIN("month [%d]", month);
540 int imap4_mailbox_get_uids_by_timestamp(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
542 EM_PROFILE_BEGIN(emCoreMailboxuidsbystamp);
543 EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
546 int err = EMAIL_ERROR_NONE;
548 IMAPLOCAL *imaplocal = NULL;
549 char tag[16], command[64];
550 char *response = NULL;
551 emcore_uid_list *uid_elem = NULL;
555 struct tm *timeinfo = NULL;
557 time_t week_before_RawTime = 0;
558 char date_string[16];
561 if (!stream || !uid_list) {
562 EM_DEBUG_EXCEPTION(" stream[%p], uid_list[%p]", stream, uid_list);
563 err = EMAIL_ERROR_INVALID_PARAM;
567 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
568 EM_DEBUG_EXCEPTION(" invalid IMAP4 stream detected...");
569 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN */
573 /* Fetch the System time and Retrieve the a Week before time */
577 EM_DEBUG_LOG("RawTime Info [%lu]", RawTime);
579 timeinfo = localtime (&RawTime);
581 EM_DEBUG_LOG(">>>>>Current TIme %d %d %d %d %d %d", 1900+timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
583 week_before_RawTime = RawTime - 604800;
585 /* Reading the current timeinfo */
586 timeinfo = localtime (&week_before_RawTime);
588 EM_DEBUG_LOG(">>>>>Mobile Date a Week before %d %d %d %d %d %d", 1900 + timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
590 memset(&date_string, 0x00, 16);
592 mon = __em_get_month_in_string(timeinfo->tm_mon);
595 snprintf(date_string, 16, "%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
596 EM_DEBUG_LOG("DATE IS [ %s ] ", date_string);
600 memset(tag, 0x00, sizeof(tag));
601 memset(command, 0x00, sizeof(command));
603 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
604 SNPRINTF(command, sizeof(command), "%s UID SEARCH 1:* SINCE %s\015\012", tag, date_string);
605 EM_DEBUG_LOG("COMMAND [%s] ", command);
607 #ifdef FEATURE_CORE_DEBUG
608 EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
611 /* send command : get msgno/uid for all message */
612 if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
613 EM_DEBUG_EXCEPTION(" net_sout failed...");
614 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN */
620 while (imaplocal->netstream) {
622 /* receive response */
623 if (!(response = net_getline(imaplocal->netstream))) {
624 EM_DEBUG_EXCEPTION(" net_getline failed...");
625 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
628 #ifdef FEATURE_CORE_DEBUG
629 EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
632 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
633 if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
634 free(response); response = NULL;
637 else { /* 'NO' or 'BAD' */
638 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
643 if ((p = strstr(response, " SEARCH "))){
644 *p = '\0'; p += strlen(" SEARCH ");
646 result = strtok(p, delims);
648 while (result != NULL)
650 EM_DEBUG_LOG("UID VALUE DEEP is [%s]", result);
652 if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
653 EM_DEBUG_EXCEPTION(" malloc failed...");
654 err = EMAIL_ERROR_OUT_OF_MEMORY;
658 uid_elem->uid = EM_SAFE_STRDUP(result);
660 if (*uid_list != NULL)
661 uid_elem->next = *uid_list;
662 *uid_list = uid_elem;
663 result = strtok(NULL, delims);
666 EM_SAFE_FREE(response);
670 err = EMAIL_ERROR_INVALID_RESPONSE;
673 /* Delete the dead code */
679 if (response != NULL)
682 if (err_code != NULL)
684 EM_PROFILE_END(emCoreMailboxuidsbystamp);
685 EM_DEBUG_FUNC_END("ret [%d]", ret);
689 #define PARSE_BUFFER_LENGTH 4096
690 static int emcore_parse_header(char *rfc822_header, int *req_read_receipt, int *priority, int *err_code)
692 EM_DEBUG_FUNC_BEGIN("rfc822_header[%p], req_read_receipt[%p], priority[%p], err_code[%p]", rfc822_header, req_read_receipt, priority, err_code);
694 if (!rfc822_header || !priority)
698 *err_code = EMAIL_ERROR_NONE;
700 EM_PROFILE_BEGIN(emCoreMailboxParseHeader);
702 char buf[PARSE_BUFFER_LENGTH];
705 EM_DEBUG_LOG("Buffer length [%d]", PARSE_BUFFER_LENGTH);
709 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
711 for (len = EM_SAFE_STRLEN(rfc822_header), i = 0, j = 0; i < len; i++) {
712 if (rfc822_header[i] == CR && rfc822_header[i+1] == LF){
713 if (j + 3 < PARSE_BUFFER_LENGTH) /* '3' include CR LF NULL */
714 strncpy(buf + j, CRLF_STRING, PARSE_BUFFER_LENGTH - (j + 2)); /* '3' include CR LF */
716 EM_DEBUG_EXCEPTION("buf is too small.");
722 em_upper_string(buf);
724 /* disposition_notification_to */
725 if (buf[0] == 'D' && buf[11] == '-' && buf[12] == 'N' && buf[24] == '-' && buf[25] == 'T') {
726 if (req_read_receipt)
727 *req_read_receipt = 1;
728 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
733 if (buf[0] == 'X' && buf[2] == 'P' && buf[9] == 'Y'){
734 size_t len_2 = EM_SAFE_STRLEN(buf);
736 buf[len_2 - 2] = '\0';
737 *priority = atoi(buf + 11);
738 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
743 /* x-msmail-priority */
744 if (buf[0] == 'X' && buf[2] == 'M' && buf[9] == 'P' && buf[16] == 'Y'){
745 if (strstr(buf, "HIGH"))
747 if (strstr(buf, "NORMAL"))
749 if (strstr(buf, "LOW"))
751 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
755 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
759 if (j + 1 < PARSE_BUFFER_LENGTH)
760 buf[j++] = rfc822_header[i];
762 EM_DEBUG_EXCEPTION("buf is too small.");
765 EM_PROFILE_END(emCoreMailboxParseHeader);
768 *err_code = EMAIL_ERROR_NONE;
774 static int emcore_get_mail_extra_info(MAILSTREAM *stream, int msgno, int *req_read_receipt, int *priority, int *err_code)
776 EM_PROFILE_BEGIN(emCoreMailGetExtraInfo);
777 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], req_read_receipt[%p], priority[%p], err_code[%p]", stream, msgno, req_read_receipt, priority, err_code);
780 int err = EMAIL_ERROR_NONE;
781 char *rfc822_header = NULL;
782 unsigned long len = 0;
784 EM_PROFILE_BEGIN(MaiFetchHeader);
785 #ifdef __FEATURE_HEADER_OPTIMIZATION__
786 /* Check if header already available in cache */
787 if (stream && stream->cache && stream->cache[msgno-1]->private.msg.header.text.data){
788 EM_DEBUG_LOG("I found the header in stream->cache!!");
789 rfc822_header = (char *) stream->cache[msgno-1]->private.msg.header.text.data;
792 EM_DEBUG_LOG("I couldn't find the header. I'll fetch the header from server again.");
794 rfc822_header = mail_fetch_header(stream, msgno, NULL, NULL, &len, FT_PEEK);
798 rfc822_header = mail_fetch_header(stream, msgno, NULL, NULL, &len, FT_PEEK);
800 EM_PROFILE_END(MaiFetchHeader);
802 if (!rfc822_header || !*rfc822_header) {
803 EM_DEBUG_EXCEPTION("mail_fetch_header failed...");
804 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE; /* EMAIL_ERROR_UNKNOWN; */
808 if (!emcore_parse_header(rfc822_header, req_read_receipt, priority, &err)) {
809 EM_DEBUG_EXCEPTION("emcore_parse_header falied - %d", err);
816 if (err_code != NULL)
819 EM_PROFILE_END(emCoreMailGetExtraInfo);
820 EM_DEBUG_FUNC_END("ret [%d]", ret);
824 static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *account, emstorage_mailbox_tbl_t *input_mailbox_tbl, int limit_count, emcore_uid_list** uid_list, int *uids, int retrieve_mode , int *err_code)
826 EM_PROFILE_BEGIN(emCoreGetUidsDownload);
827 EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_tbl[%p], limit_count[%d], uid_list[%p], err_code[%p]", account, input_mailbox_tbl, limit_count, uid_list, err_code);
830 int err = EMAIL_ERROR_NONE;
832 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
833 int i = 0, j = 0, uid_count = 0, uid_to_be_downloaded_count = 0;
834 emcore_uid_list *uid_elem = NULL;
835 emcore_uid_list *head_uid_elem = NULL, *end = NULL;
836 emcore_uid_list *next_uid_elem = NULL;
837 emstorage_mail_tbl_t *mail = NULL;
839 if (!account || !input_mailbox_tbl || !uid_list) {
840 EM_DEBUG_EXCEPTION("account[%p], input_mailbox_tbl[%p], uid_list[%p]", account, input_mailbox_tbl, uid_list);
841 err = EMAIL_ERROR_INVALID_PARAM;
847 if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
848 if (!pop3_mailbox_get_uids(stream, uid_list, &err)) {
849 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed - %d", err);
853 else { /* EMAIL_SERVER_TYPE_IMAP4 */
854 /* sowmya.kr commented , since imap4_mailbox_get_uids_by_timestamp will fetch mails since last week and not all mails */
856 EM_DEBUG_LOG("calling imap4_mailbox_get_uids");
857 if (!imap4_mailbox_get_uids(stream, uid_list, &err)) {
858 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
859 if (err != EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
864 if (!emstorage_get_downloaded_list(input_mailbox_tbl->account_id,
865 (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ? 0 : input_mailbox_tbl->mailbox_id,
866 &downloaded_uids, &j, true, &err)) {
867 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
871 EM_DEBUG_LOG("Number of Mails in Downloaded Table [%d]", j);
873 uid_elem = *uid_list;
876 if(!uid_elem) { /* If there is no mail in the input_mailbox_tbl, remove all mails in the input_mailbox_tbl */
877 for (i = 0; i < j; i++) {
878 downloaded_uids[i].reserved = 0;
882 EM_PROFILE_BEGIN(emCoreGetUidsDownloadWhilwLoop);
885 next_uid_elem = uid_elem->next;
887 if ((account->retrieval_mode == EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (uid_elem->flag.seen != 0)){ /* already seen */
894 int to_be_downloaded = 1;
896 if (limit_count > 0 && uid_count >= limit_count){
897 /* EM_DEBUG_LOG("hit the limit[%d] for [%s]", limit_count, uid_elem->uid); */
898 to_be_downloaded = 0;
901 for (i = j; i > 0; i--) {
902 if (downloaded_uids[i - 1].reserved == 0 && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
903 downloaded_uids[i - 1].reserved = uid_elem->flag.seen ? 2 : 1;
904 to_be_downloaded = 0;
910 /* EM_DEBUG_LOG("Is uid[%s] going to be downloded ? [%d]", uid_elem->uid, to_be_downloaded); */
912 if (to_be_downloaded) {
913 if (retrieve_mode == EMAIL_SYNC_OLDEST_MAILS_FIRST){
914 uid_elem->next = head_uid_elem;
915 head_uid_elem = uid_elem;
917 else{ /* if retrieve_mode is EMAIL_SYNC_LATEST_MAILS_FIRST, add uid elem to end so that latest mails are in front of list */
918 if (head_uid_elem == NULL){
919 uid_elem->next = head_uid_elem;
920 head_uid_elem = uid_elem;
924 end->next = uid_elem;
925 uid_elem->next = NULL;
929 uid_to_be_downloaded_count++;
941 uid_elem = next_uid_elem;
944 EM_PROFILE_END(emCoreGetUidsDownloadWhilwLoop);
945 EM_PROFILE_BEGIN(emCoreGetUidsDownloadForLoop);
947 for (i = 0; i < j; i++) {
948 /* EM_DEBUG_LOG("input_mailbox_tbl[%s] reserved[%d]", input_mailbox_tbl->name, downloaded_uids[i].reserved); */
949 if (downloaded_uids[i].reserved == 0) { /* deleted on server */
950 if (!emstorage_get_maildata_by_servermailid(input_mailbox_tbl->account_id, downloaded_uids[i].s_uid, &mail, true, &err)){
951 EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid for uid[%s] Failed [%d] \n ", downloaded_uids[i].s_uid, err);
952 if (err == EMAIL_ERROR_MAIL_NOT_FOUND){
957 if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
958 if (!emcore_delete_mails_from_local_storage(input_mailbox_tbl->account_id, &(mail->mail_id), 1, EMAIL_DELETED_FROM_SERVER, false, &err)) {
959 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage falied - %d", err);
962 /* emcore_delete_notification_for_read_mail(mail->mail_id); */
963 emcore_display_unread_in_badge();
966 if (!emstorage_remove_downloaded_mail(input_mailbox_tbl->account_id, input_mailbox_tbl->mailbox_name, downloaded_uids[i].s_uid, true, &err)) { /* remove uid from uid list */
967 EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
969 /* goto FINISH_OFF; */
973 else if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && downloaded_uids[i].reserved == 1) {
974 /* unseen on server */
975 if (!emstorage_get_mail_by_id(downloaded_uids[i].local_uid, &mail, true, &err)){
976 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed for [%d] - [%d]", downloaded_uids[i].local_uid, err);
981 if (mail->body_download_status && mail->flags_seen_field){
982 EM_DEBUG_LOG("Set flag as seen on server");
983 mail_setflag_full(stream, downloaded_uids[i].s_uid, "\\Seen", ST_UID);
988 emstorage_free_mail(&mail, 1, NULL);
991 EM_PROFILE_END(emCoreGetUidsDownloadForLoop);
993 *uid_list = head_uid_elem;
994 *uids = uid_to_be_downloaded_count;
1001 emcore_free_uids(head_uid_elem, NULL);
1004 if (downloaded_uids != NULL)
1005 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
1008 emstorage_free_mail(&mail, 1, NULL);
1010 if (err_code != NULL)
1013 EM_PROFILE_END(emCoreGetUidsDownload);
1014 EM_DEBUG_FUNC_END("ret [%d]", ret);
1018 /* insert received mail UID to read mail uid table */
1019 static int emcore_add_read_mail_uid(emstorage_mailbox_tbl_t *input_maibox_data, char *server_mailbox_name, int mail_id, char *uid, int rfc822_size, int rule_id, int *err_code)
1021 EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], server_mailbox_name[%s], uid[%s], rfc822_size[%d], rule_id[%d], err_code[%p]", input_maibox_data, server_mailbox_name, uid, rfc822_size, rule_id, err_code);
1024 int err = EMAIL_ERROR_NONE;
1026 emstorage_read_mail_uid_tbl_t read_mail_uid = { 0 };
1027 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1029 if (!input_maibox_data || !uid) {
1030 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1031 err = EMAIL_ERROR_INVALID_PARAM;
1035 read_mail_uid.account_id = input_maibox_data->account_id;
1037 if (!(input_maibox_data->mailbox_id) || !(server_mailbox_name)){
1038 if (!emstorage_get_mailbox_by_mailbox_type(input_maibox_data->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl, false, &err)) {
1039 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1044 if (input_maibox_data->mailbox_id)
1045 read_mail_uid.mailbox_id = input_maibox_data->mailbox_id;
1047 read_mail_uid.mailbox_id = mailbox_tbl->mailbox_id;
1049 read_mail_uid.local_uid = mail_id;
1050 EM_DEBUG_LOG("MAIL ID [%d] LOCAL_UID [%d]", mail_id, read_mail_uid.local_uid);
1052 if (server_mailbox_name)
1053 read_mail_uid.mailbox_name = server_mailbox_name;
1055 read_mail_uid.mailbox_name = mailbox_tbl->mailbox_name;
1057 read_mail_uid.s_uid = uid;
1058 read_mail_uid.data1 = rfc822_size;
1059 read_mail_uid.flag = rule_id;
1061 if (!emstorage_add_downloaded_mail(&read_mail_uid, false, &err)) {
1062 EM_DEBUG_EXCEPTION("emstorage_add_downloaded_mail failed [%d]", err);
1071 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1076 EM_DEBUG_FUNC_END("ret [%d]", ret);
1080 int emcore_add_mail_to_mailbox(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, int *output_mail_id, int *output_thread_id)
1082 EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], input_new_mail_tbl_data[%p], uid_elem[%p], output_mail_id[%p], output_thread_id[%p]", mail_stream, input_maibox_data, input_new_mail_tbl_data, output_mail_id, output_thread_id);
1084 int err = EMAIL_ERROR_NONE;
1086 int thread_item_count = 0;
1087 int latest_mail_id_in_thread = -1;
1089 if (!input_maibox_data || !input_new_mail_tbl_data) {
1090 EM_DEBUG_EXCEPTION("Invalid Parameter");
1091 err = EMAIL_ERROR_INVALID_PARAM;
1095 input_new_mail_tbl_data->account_id = input_maibox_data->account_id;
1096 input_new_mail_tbl_data->mailbox_id = input_maibox_data->mailbox_id;
1097 input_new_mail_tbl_data->mailbox_name = EM_SAFE_STRDUP(input_maibox_data->mailbox_name);
1098 input_new_mail_tbl_data->mailbox_type = input_maibox_data->mailbox_type;
1100 emstorage_begin_transaction(NULL, NULL, NULL);
1102 /* Get the Mail_id */
1103 if (!emstorage_increase_mail_id(&(input_new_mail_tbl_data->mail_id), false, &err)) {
1104 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
1108 if (emstorage_get_thread_id_of_thread_mails(input_new_mail_tbl_data, &thread_id, &latest_mail_id_in_thread, &thread_item_count) != EMAIL_ERROR_NONE)
1109 EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
1111 if (thread_id == -1){
1112 input_new_mail_tbl_data->thread_id = input_new_mail_tbl_data->mail_id;
1113 input_new_mail_tbl_data->thread_item_count = thread_item_count = 1;
1116 input_new_mail_tbl_data->thread_id = thread_id;
1117 thread_item_count++;
1120 if (!emstorage_add_mail(input_new_mail_tbl_data, 0, false, &err)) {
1121 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
1122 emstorage_rollback_transaction(NULL, NULL, NULL);
1126 if (thread_item_count > 1){
1127 if (!emstorage_update_latest_thread_mail(input_new_mail_tbl_data->account_id, input_new_mail_tbl_data->thread_id, 0, 0, false, &err)) {
1128 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
1129 emstorage_rollback_transaction(NULL, NULL, NULL);
1133 if (output_thread_id)
1134 *output_thread_id = input_new_mail_tbl_data->thread_id;
1136 if (output_mail_id != NULL)
1137 *output_mail_id = input_new_mail_tbl_data->mail_id;
1139 EM_DEBUG_LOG("mail_table_data.mail_id [%d]", input_new_mail_tbl_data->mail_id);
1140 EM_DEBUG_LOG("mail_table_data.thread_id [%d]", input_new_mail_tbl_data->thread_id);
1142 if (!emcore_add_read_mail_uid(input_maibox_data, input_maibox_data->mailbox_name, input_new_mail_tbl_data->mail_id, input_new_mail_tbl_data->server_mail_id, input_new_mail_tbl_data->mail_size, 0, &err)) {
1143 EM_DEBUG_EXCEPTION("emcore_add_read_mail_uid failed [%d]", err);
1144 emstorage_rollback_transaction(NULL, NULL, NULL);
1148 emstorage_commit_transaction(NULL, NULL, NULL);
1152 EM_DEBUG_FUNC_END("err [%d]", err);
1156 int emcore_check_rule(const char *input_full_address_from, const char *input_subject, emstorage_rule_tbl_t *rule, int rule_len, int *matched, int *err_code)
1158 EM_DEBUG_FUNC_BEGIN("input_full_address_from [%p], input_subject [%p], rule [%p], rule_len [%d], matched [%p], err_code [%p]", input_full_address_from, input_subject, rule, rule_len, matched, err_code);
1160 int ret = false, err = EMAIL_ERROR_NONE, i;
1162 char *src = NULL; /* string which will be compared with rules */
1163 char *from_address = NULL;
1164 ADDRESS *addr = NULL;
1166 if (!matched || !input_full_address_from || !input_subject) {
1167 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1168 err = EMAIL_ERROR_INVALID_PARAM;
1174 rfc822_parse_adrlist(&addr, (char*)input_full_address_from, NULL);
1177 EM_DEBUG_LOG("rule : full_address_from[%s], addr->mailbox[%s], addr->host[%s]", input_full_address_from, addr->mailbox, addr->host);
1180 len = EM_SAFE_STRLEN(addr->mailbox);
1182 len += EM_SAFE_STRLEN(addr->host);
1185 if (!(from_address = em_malloc(len))) {
1186 EM_DEBUG_EXCEPTION("em_malloc failed...");
1187 err = EMAIL_ERROR_OUT_OF_MEMORY;
1191 SNPRINTF(from_address, len, "%s@%s", addr->mailbox, addr->host);
1194 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed.");
1195 err = EMAIL_ERROR_INVALID_ADDRESS;
1199 for (i = 0; i < rule_len; i++) {
1201 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1202 err = EMAIL_ERROR_INVALID_PARAM;
1206 EM_DEBUG_LOG("rule[%d].flag1(rule id[%d]) is %d", i, rule[i].rule_id, rule[i].flag1);
1210 EM_DEBUG_LOG("rule[%d].flag2(rule id[%d]) is %d", i, rule[i].rule_id, rule[i].flag2);
1211 switch (rule[i].type) {
1212 case EMAIL_FILTER_FROM:
1215 case EMAIL_FILTER_SUBJECT:
1216 src = (char*)input_subject;
1218 case EMAIL_FILTER_BODY:
1219 err = EMAIL_ERROR_NOT_SUPPORTED;
1223 EM_DEBUG_LOG("rule src[%s], value[%s]\n", src, rule[i].value);
1225 if (src && rule[i].value) {
1226 if (RULE_TYPE_INCLUDES == rule[i].flag2) {
1227 if (strstr(src, rule[i].value)) {
1232 else if (RULE_TYPE_EXACTLY == rule[i].flag2) {
1233 if (!strcmp(src, rule[i].value)) {
1241 EM_DEBUG_LOG("Invald src or rule[i].value");
1245 EM_DEBUG_LOG("i [%d], matched [%d]", i, *matched);
1248 EM_SAFE_FREE(from_address);
1251 mail_free_address(&addr);
1253 if (err_code != NULL)
1256 EM_DEBUG_FUNC_END("ret [%d]", ret);
1260 static int emcore_get_utf8_address(char **dest, ADDRESS *address, int *err_code)
1262 EM_DEBUG_FUNC_BEGIN("dest[%p], address[%p], err_code[%p]", dest, address, err_code);
1264 if (!dest || !address) {
1265 EM_DEBUG_EXCEPTION("dest[%p], address[%p]", dest, address);
1266 if (err_code != NULL)
1267 *err_code = EMAIL_ERROR_INVALID_PARAM;
1272 int err = EMAIL_ERROR_NONE;
1274 gchar *concatenated = NULL;
1275 gchar *utf8_address = NULL;
1277 char *nickname = NULL;
1280 EM_DEBUG_LOG("address->mailbox[%s], address->host[%s]", address->mailbox, address->host);
1281 if (!address->mailbox || !address->host) {
1282 address = address->next;
1285 EM_DEBUG_LOG("address->mailbox[%p]", address->personal);
1286 if (address->personal) {
1287 if (!(nickname = emcore_decode_rfc2047_text(address->personal, &err))) {
1288 EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_text failed - %d", err);
1291 EM_DEBUG_LOG("nickname[%s]", nickname);
1292 if (*nickname != '\0')
1293 utf8_address = g_strdup_printf("\"%s\" <%s@%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1295 utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1297 EM_SAFE_FREE(nickname);
1300 utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1302 EM_DEBUG_LOG("utf8_address[%s]", utf8_address);
1304 if (concatenated != NULL) {
1305 temp = concatenated;
1306 concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
1310 concatenated = g_strdup(utf8_address);
1312 g_free(utf8_address);
1313 utf8_address = NULL;
1315 address = address->next;
1318 *dest = concatenated;
1323 EM_SAFE_FREE(nickname);
1324 EM_DEBUG_FUNC_END("ret[%d]", ret);
1329 int emcore_make_mail_tbl_data_from_envelope(MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data, int *err_code)
1331 EM_PROFILE_BEGIN(emCoreParseEnvelope);
1332 EM_DEBUG_FUNC_BEGIN("input_envelope[%p], input_uid_elem [%p], output_mail_tbl_data[%p], err_code[%p]", input_envelope, input_uid_elem, output_mail_tbl_data, err_code);
1336 int err = EMAIL_ERROR_NONE;
1338 int req_read_receipt = 0;
1339 struct tm temp_time_info;
1340 MESSAGECACHE *mail_cache_element = NULL;
1341 emstorage_mail_tbl_t *temp_mail_tbl_data = NULL;
1343 if (!output_mail_tbl_data) {
1344 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1345 err = EMAIL_ERROR_INVALID_PARAM;
1349 if (!(temp_mail_tbl_data = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
1350 EM_DEBUG_EXCEPTION("em_malloc failed...");
1351 err = EMAIL_ERROR_OUT_OF_MEMORY;
1355 if (!(mail_cache_element = mail_elt(mail_stream, input_uid_elem->msgno))) {
1356 EM_DEBUG_EXCEPTION("mail_elt failed...");
1357 err = EMAIL_ERROR_UNKNOWN;
1361 if (!emcore_get_mail_extra_info(mail_stream, input_uid_elem->msgno, &req_read_receipt, &priority, &err)) {
1362 EM_DEBUG_EXCEPTION("emcore_get_mail_extra_info failed [%d]", err);
1366 if (input_envelope->subject) {
1367 temp_mail_tbl_data->subject = emcore_decode_rfc2047_text(input_envelope->subject, &err);
1368 EM_DEBUG_LOG("subject[%s]", temp_mail_tbl_data->subject);
1371 if (input_envelope->from) {
1372 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_from, input_envelope->from, &err)) {
1373 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1377 EM_DEBUG_LOG("full_address_from[%s]", temp_mail_tbl_data->full_address_from);
1380 if (input_envelope->to) {
1381 EM_DEBUG_LOG("input_envelope->to");
1382 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_to, input_envelope->to, &err)) {
1383 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1387 EM_DEBUG_LOG("full_address_to[%s]", temp_mail_tbl_data->full_address_to);
1390 if (input_envelope->cc) {
1391 EM_DEBUG_LOG("input_envelope->cc");
1392 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_cc, input_envelope->cc, &err)) {
1393 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1397 EM_DEBUG_LOG("full_address_cc [%s]", temp_mail_tbl_data->full_address_cc);
1400 if (input_envelope->bcc) {
1401 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_bcc, input_envelope->bcc, &err)) {
1402 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1406 EM_DEBUG_LOG("full_address_bcc [%s]", temp_mail_tbl_data->full_address_bcc);
1409 if (input_envelope->reply_to) {
1410 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_reply, input_envelope->reply_to, &err)) {
1411 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1415 EM_DEBUG_LOG("full_address_reply [%s]\n", temp_mail_tbl_data->full_address_reply);
1418 if (input_envelope->return_path) {
1419 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_return , input_envelope->return_path, &err)) {
1420 EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1423 EM_DEBUG_LOG("full_address_return[%s]", temp_mail_tbl_data->full_address_return);
1426 temp_mail_tbl_data->message_id = EM_SAFE_STRDUP(input_envelope->message_id);
1428 memset((void*)&temp_time_info, 0, sizeof(struct tm));
1430 EM_DEBUG_LOG("tm_hour[%d] zhours [%d] tm_min[%d] zminutes[%d] zoocident[%d]", mail_cache_element->hours, mail_cache_element->zhours, mail_cache_element->minutes, mail_cache_element->zminutes, mail_cache_element->zoccident);
1432 zone_hour = mail_cache_element->zhours * (mail_cache_element->zoccident?-1:1);
1434 temp_time_info.tm_sec = mail_cache_element->seconds;
1435 temp_time_info.tm_min = mail_cache_element->minutes + mail_cache_element->zminutes;
1436 temp_time_info.tm_hour = mail_cache_element->hours - zone_hour;
1438 if(temp_time_info.tm_hour < 0) {
1439 temp_time_info.tm_mday = mail_cache_element->day - 1;
1440 temp_time_info.tm_hour += 24;
1443 temp_time_info.tm_mday = mail_cache_element->day;
1445 temp_time_info.tm_mon = mail_cache_element->month - 1;
1446 temp_time_info.tm_year = mail_cache_element->year + 70;
1448 temp_mail_tbl_data->date_time = timegm(&temp_time_info);
1450 temp_mail_tbl_data->server_mail_status = 1;
1451 temp_mail_tbl_data->server_mail_id = EM_SAFE_STRDUP(input_uid_elem->uid);
1452 temp_mail_tbl_data->mail_size = mail_cache_element->rfc822_size;
1453 temp_mail_tbl_data->flags_seen_field = input_uid_elem->flag.seen;
1454 temp_mail_tbl_data->flags_deleted_field = mail_cache_element->deleted;
1455 temp_mail_tbl_data->flags_flagged_field = mail_cache_element->flagged;
1456 temp_mail_tbl_data->flags_answered_field = mail_cache_element->answered;
1457 temp_mail_tbl_data->flags_recent_field = mail_cache_element->recent;
1458 temp_mail_tbl_data->flags_draft_field = mail_cache_element->draft;
1459 temp_mail_tbl_data->flags_forwarded_field = input_uid_elem->flag.forwarded;
1460 temp_mail_tbl_data->priority = priority;
1461 temp_mail_tbl_data->report_status = (req_read_receipt ? EMAIL_MAIL_REQUEST_MDN : 0);
1462 temp_mail_tbl_data->attachment_count = 0;
1464 emcore_fill_address_information_of_mail_tbl(temp_mail_tbl_data);
1466 *output_mail_tbl_data = temp_mail_tbl_data;
1467 temp_mail_tbl_data = NULL;
1474 EM_SAFE_FREE(temp_mail_tbl_data);
1476 if (err_code != NULL)
1479 EM_PROFILE_END(emCoreParseEnvelope);
1480 EM_DEBUG_FUNC_END("ret [%d]", ret);
1484 INTERNAL_FUNC int emcore_sync_header(emstorage_mailbox_tbl_t *input_mailbox_tbl, emstorage_mailbox_tbl_t *input_mailbox_tbl_spam, void *stream_recycle, emcore_uid_list **input_uid_list, int *unread_mail, int *err_code)
1486 EM_PROFILE_BEGIN(emCoreSyncHeader);
1487 EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], input_mailbox_tbl_spam[%p], input_uid_list [%p], err_code[%p]", input_mailbox_tbl, input_mailbox_tbl_spam, input_uid_list, err_code);
1490 int err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
1491 int err_from_vconf = 0;
1492 int status = EMAIL_LIST_FAIL;
1493 int download_limit_count;
1494 email_account_t *account_ref = NULL;
1495 emstorage_rule_tbl_t *rule = NULL;
1496 emcore_uid_list *uid_list = NULL;
1497 emcore_uid_list *uid_elem = NULL;
1498 emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
1499 ENVELOPE *env = NULL;
1500 int account_id = 0, mail_id = 0, rule_len = 1000, total = 0, unread = 0, i = 0, percentage = 0, thread_id = -1;
1501 void *stream = NULL;
1502 char *uid_range = NULL;
1503 char mailbox_id_param_string[10] = {0,};
1505 if (!input_mailbox_tbl) {
1506 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM:input_mailbox_tbl[%p]", input_mailbox_tbl);
1507 err = EMAIL_ERROR_INVALID_PARAM;
1511 account_id = input_mailbox_tbl->account_id;
1512 account_ref = emcore_get_account_reference(account_id);
1514 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
1515 err = EMAIL_ERROR_INVALID_ACCOUNT;
1519 if(account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && input_mailbox_tbl->local_yn == 1) {
1520 EM_DEBUG_EXCEPTION("local_yn flag is false for this mailbox");
1521 err = EMAIL_ERROR_INVALID_MAILBOX;
1525 FINISH_OFF_IF_CANCELED;
1527 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
1528 if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 1)) != 0 ) {
1529 EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
1531 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
1533 #ifndef __FEATURE_KEEP_CONNECTION__
1534 /* h.gahlaut : Recycling of stream is taken care internally in emcore_connect_to_remote_mailbox so no need of this code here */
1536 stream = stream_recycle; /* set stream for recycling connection. */
1539 if (!emcore_connect_to_remote_mailbox(account_id, input_mailbox_tbl->mailbox_id, (void **)&stream, &err) || !stream){
1540 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
1541 status = EMAIL_LIST_CONNECTION_FAIL;
1545 FINISH_OFF_IF_CANCELED;
1547 /* save total mail count on server to DB */
1548 if (!emstorage_update_mailbox_total_count(account_id, input_mailbox_tbl->mailbox_id, ((MAILSTREAM *)stream)->nmsgs, 1, &err)){
1549 EM_DEBUG_EXCEPTION("emstorage_update_mailbox_total_count failed [%d]", err);
1554 /* if (((MAILSTREAM *)stream)->nmsgs > 0) */
1556 email_option_t *opt_ref = &account_ref->options;
1557 EM_DEBUG_LOG("block_address = %d, block_subject = %d", opt_ref->block_address, opt_ref->block_subject);
1559 if (opt_ref->block_address || opt_ref->block_subject) {
1560 int is_completed = false;
1563 if (!opt_ref->block_address)
1564 type = EMAIL_FILTER_SUBJECT;
1565 else if (!opt_ref->block_subject)
1566 type = EMAIL_FILTER_FROM;
1568 if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
1569 EM_DEBUG_EXCEPTION("emstorage_get_rule failed - %d", err);
1571 download_limit_count = input_mailbox_tbl->mail_slot_size;
1572 if (!emcore_get_uids_to_download(stream, account_ref, input_mailbox_tbl, download_limit_count, &uid_list, &total, EMAIL_SYNC_LATEST_MAILS_FIRST, &err)){
1573 EM_DEBUG_EXCEPTION("emcore_get_uids_to_download failed [%d]", err);
1578 FINISH_OFF_IF_CANCELED;
1580 if (input_uid_list && *input_uid_list){
1581 emcore_free_uids(*input_uid_list, NULL);
1582 *input_uid_list = uid_list;
1584 uid_elem = uid_list;
1586 EM_PROFILE_BEGIN(emCoreSyncHeaderwhileloop);
1588 #ifdef __FEATURE_HEADER_OPTIMIZATION__
1589 /* g.shyamakshi@samsung.com : Bulk fetch of headers only if the recieving server type is IMAP */
1591 EM_DEBUG_LOG("((MAILSTREAM *)stream)->nmsgs [%d]", ((MAILSTREAM *)stream)->nmsgs);
1592 EM_DEBUG_LOG("uid_list [%p]", uid_list);
1593 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list != NULL){
1594 emcore_uid_list *uid_list_prev = NULL;
1595 emcore_uid_list *uid_list_fast = uid_list;
1597 int msg_count = total;
1598 int uid_range_size = msg_count * 8 + 1000;
1600 EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
1601 uid_range = malloc(sizeof(char) * uid_range_size);
1603 if (uid_range == NULL){
1604 EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
1605 err = EMAIL_ERROR_OUT_OF_MEMORY;
1609 uid_list_prev = uid_list_fast;
1611 if (uid_list_fast->next == NULL){
1612 /* Single list entry */
1613 snprintf(uid_range, uid_range_size, "%d", atoi(uid_list_fast->uid));
1616 /* forming range of uids to be passed */
1617 while (uid_list_fast != NULL){
1618 /* uid_list_fast = uid_list_fast->next; */
1620 if ((uid_list_fast->next != NULL) && (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1)){
1621 index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
1623 uid_list_fast = uid_list_fast->next;
1624 uid_list_prev = uid_list_fast;
1626 /* to make UID range string "abc, XX : YY" */
1627 while (uid_list_fast != NULL){
1628 if (uid_list_fast->next == NULL)
1630 if (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1){
1631 uid_list_fast = uid_list_fast->next;
1632 uid_list_prev = uid_list_fast;
1637 if ((uid_list_fast != NULL) && (uid_list_fast->next != NULL))
1638 index += snprintf(uid_range+index, uid_range_size, ":%d,", atoi(uid_list_prev->uid));
1640 index += snprintf(uid_range+index, uid_range_size, ":%d", atoi(uid_list_prev->uid));
1642 uid_list_fast = uid_list_fast->next;
1643 uid_list_prev = uid_list_fast;
1646 if (uid_list_fast->next != NULL)
1647 index += snprintf(uid_range+index, uid_range_size, "%d,", atoi(uid_list_prev->uid));
1649 index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
1650 uid_list_fast = uid_list_fast->next;
1651 uid_list_prev = uid_list_fast;
1656 /* h.gahlaut : [Start] */
1657 /* Adding this check here to check if application has called cancel job. */
1658 /* This checking should be done because fetching 50 headers will take time */
1659 FINISH_OFF_IF_CANCELED;
1661 EM_DEBUG_LOG("index [%d]", index);
1663 /* h.gahlaut : [End] */
1664 uid_elem = uid_list;
1665 if (stream && uid_elem){
1666 EM_DEBUG_LOG("msgno : %d", uid_elem->msgno);
1667 ((MAILSTREAM *)stream)->nmsgs = uid_elem->msgno;
1670 EM_DEBUG_LOG("Uid List Null");
1672 EM_DEBUG_LOG("Calling ... mail_fetch_fast. uid_range [%s]", uid_range);
1673 mail_fetch_fast(stream, uid_range, FT_UID | FT_PEEK | FT_NEEDENV);
1674 EM_SAFE_FREE(uid_range);
1678 /* h.gahlaut@samsung.com : Clear the event queue of partial body download thread before starting fetching new headers */
1679 #ifndef __PARTIAL_BODY_FOR_POP3__
1680 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
1681 #endif /* __PARTIAL_BODY_FOR_POP3__ */
1682 /* Partial body download feature is only for IMAP accounts */
1683 if (false == emcore_clear_partial_body_thd_event_que(&err))
1684 EM_DEBUG_LOG("emcore_clear_partial_body_thd_event_que failed [%d]", err);
1685 #ifndef __PARTIAL_BODY_FOR_POP3__
1687 #endif /* __PARTIAL_BODY_FOR_POP3__ */
1689 EM_PROFILE_BEGIN(emCoreSyncHeaderEachMail);
1690 EM_DEBUG_LOG("mail_fetchstructure_full : uid_elem->msgno[%d]", uid_elem->msgno);
1694 if (uid_elem->msgno > ((MAILSTREAM *)stream)->nmsgs)
1695 EM_DEBUG_EXCEPTION("Warnings! msgno[%d] can't be greater than nmsgs[%d]. It might cause crash.", uid_elem->msgno, ((MAILSTREAM *)stream)->nmsgs);
1698 #ifdef __FEATURE_HEADER_OPTIMIZATION__
1699 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* Fetch env from cache in case of IMAP */
1700 env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 0);
1701 else /* Fetch header from network in case of POP */
1702 env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 1);
1704 env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK);
1707 FINISH_OFF_IF_CANCELED;
1712 if (!emcore_make_mail_tbl_data_from_envelope(stream, env, uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
1713 EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
1717 if (rule && input_mailbox_tbl_spam && !emcore_check_rule(new_mail_tbl_data->full_address_from, new_mail_tbl_data->subject, rule, rule_len, &matched, &err)) {
1718 EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
1722 if (matched >= 0 && input_mailbox_tbl_spam){ /* add filtered mails to SPAMBOX */
1723 EM_DEBUG_LOG("mail[%d] added to spambox", mail_id);
1725 if ( (err = emcore_add_mail_to_mailbox(input_mailbox_tbl_spam, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
1726 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
1730 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
1731 if (!emcore_move_mail_on_server(account_id, input_mailbox_tbl->mailbox_id, &mail_id, 1, input_mailbox_tbl_spam->mailbox_name, &err)){
1732 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server falied [%d]", err);
1737 /* add mails to specified mail box */
1738 EM_DEBUG_LOG("mail[%d] moved to input_mailbox_tbl [%s]", mail_id, input_mailbox_tbl->mailbox_name);
1739 if ( (err = emcore_add_mail_to_mailbox(input_mailbox_tbl, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
1740 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
1744 /*h.gahlaut : Start partial body dowload using partial body thread only for IMAP accounts*/
1745 #ifndef __PARTIAL_BODY_FOR_POP3__
1746 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1747 #endif /* __PARTIAL_BODY_FOR_POP3__ */
1748 if (account_ref->auto_download_size != 0) {
1749 if (false == emcore_initiate_pbd(stream, account_id, mail_id, uid_elem->uid, input_mailbox_tbl->mailbox_id, &err))
1750 EM_DEBUG_LOG("Partial body download initiation failed [%d]", err);
1752 #ifndef __PARTIAL_BODY_FOR_POP3__
1754 #endif /* __PARTIAL_BODY_FOR_POP3__ */
1756 if (!uid_elem->flag.seen && input_mailbox_tbl->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX)
1757 emcore_add_notification_for_unread_mail(new_mail_tbl_data);
1759 FINISH_OFF_IF_CANCELED;
1761 if (!uid_elem->flag.seen)
1764 percentage = ((i+1) * 100) / total ;
1765 EM_DEBUG_LOG("Header Percentage Completed [%d] : [%d/%d] mail_id [%d]", percentage, i+1, total, mail_id);
1766 SNPRINTF(mailbox_id_param_string, 10, "%d", input_mailbox_tbl->mailbox_id);
1768 if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id))
1769 EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
1772 /* Set contact log */
1773 switch (input_mailbox_tbl->mailbox_type) {
1774 case EMAIL_MAILBOX_TYPE_INBOX :
1775 if ((err = emcore_set_received_contacts_log(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
1776 EM_DEBUG_EXCEPTION("emcore_set_received_contacts_log failed : [%d]", err);
1779 case EMAIL_MAILBOX_TYPE_SENTBOX:
1780 case EMAIL_MAILBOX_TYPE_OUTBOX:
1781 if ((err = emcore_set_sent_contacts_log(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
1782 EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
1786 EM_DEBUG_LOG("Mailbox type : [%d]", input_mailbox_tbl->mailbox_type);
1790 /* Release for envelope is not required and it may cause crash. Don't free the memory for envelope here. */
1791 /* Envelope data will be freed by garbage collector in mail_close_full */
1792 if (new_mail_tbl_data){
1793 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
1794 new_mail_tbl_data = NULL;
1797 FINISH_OFF_IF_CANCELED;
1800 uid_elem = uid_elem->next;
1802 EM_PROFILE_END(emCoreSyncHeaderEachMail);
1804 EM_PROFILE_END(emCoreSyncHeaderwhileloop);
1810 *unread_mail = unread;
1813 emcore_free_account(account_ref);
1814 EM_SAFE_FREE(account_ref);
1817 if (!emcore_remove_overflowed_mails(input_mailbox_tbl, &err_2))
1818 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed - %d", err_2);
1820 if (input_mailbox_tbl)
1821 emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_tbl->mailbox_id, 1);
1823 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
1824 if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 0)) != 0 ) {
1825 EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
1827 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
1829 if (new_mail_tbl_data)
1830 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
1832 if (uid_list != NULL){
1833 emcore_free_uids(uid_list, NULL);
1834 /* uid_list point to the same memory with input_mailbox_tbl->user_data. */
1835 /* input_mailbox_tbl->user_data should be set NULL if uid_list is freed */
1836 *input_uid_list = NULL;
1839 EM_SAFE_FREE(uid_range);
1842 emstorage_free_rule(&rule, rule_len, NULL);
1844 #ifdef __FEATURE_KEEP_CONNECTION__
1845 if (stream != NULL) {
1846 #else /* __FEATURE_KEEP_CONNECTION__ */
1847 if (stream != NULL && stream_recycle == NULL) {
1848 #endif /* __FEATURE_KEEP_CONNECTION__ */
1849 emcore_close_mailbox(0, stream);
1853 if (err_code != NULL)
1856 EM_PROFILE_END(emCoreSyncHeader);
1857 EM_DEBUG_FUNC_END("ret [%d]", ret);
1862 emcore_uid_list *__ReverseList(emcore_uid_list *uid_list)
1864 emcore_uid_list *temp, *current, *result;
1870 while (current != NULL){
1871 temp = current->next;
1872 current->next = result;
1882 int emcore_download_uid_all(email_internal_mailbox_t *mailbox, emcore_uid_list** uid_list, int *total, emstorage_read_mail_uid_tbl_t *downloaded_uids, int count, emcore_get_uids_for_delete_t for_delete, int *err_code)
1884 EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid_list[%p], total[%p], downloaded_uids[%p], count[%d], for_delete[%d], err_code[%p]", mailbox, uid_list, total, downloaded_uids, count, for_delete, err_code);
1887 int err = EMAIL_ERROR_NONE;
1889 MAILSTREAM *stream = NULL;
1890 email_account_t *ref_account = NULL;
1891 emcore_uid_list *uid_elem = NULL;
1892 emcore_uid_list *fetch_data_p = NULL;
1893 void *tmp_stream = NULL;
1894 char cmd[64] = {0x00, };
1897 if (!mailbox || !uid_list) {
1898 EM_DEBUG_EXCEPTION("mailbox[%p], uid_list[%p], total[%p], downloaded_uids[%p], count[%d], for_delete[%d]", mailbox, uid_list, total, downloaded_uids, count, for_delete);
1900 err = EMAIL_ERROR_INVALID_PARAM;
1904 if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
1905 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", mailbox->account_id);
1907 err = EMAIL_ERROR_INVALID_ACCOUNT;
1911 if (!mailbox->mail_stream) {
1912 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)){
1913 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed...");
1918 stream = (MAILSTREAM *)tmp_stream;
1921 stream = mailbox->mail_stream;
1923 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) { /* POP3 */
1924 POP3LOCAL *pop3local = NULL;
1926 if (!stream || !(pop3local = stream->local) || !pop3local->netstream) {
1927 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN; */
1932 memset(cmd, 0x00, sizeof(cmd));
1934 SNPRINTF(cmd, sizeof(cmd), "UIDL\015\012");
1936 #ifdef FEATURE_CORE_DEBUG
1937 EM_DEBUG_LOG("[POP3] >>> [%s]", cmd);
1940 if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
1941 EM_DEBUG_EXCEPTION("net_sout failed...");
1943 err = EMAIL_ERROR_CONNECTION_BROKEN;
1947 /* get uid from replied data */
1948 while (pop3local->netstream) {
1951 if (!(p = net_getline(pop3local->netstream)))
1954 #ifdef FEATURE_CORE_DEBUG
1955 EM_DEBUG_LOG(" [POP3] <<< [%s]", p);
1958 /* replied error "-ERR" */
1960 err = EMAIL_ERROR_MAIL_NOT_FOUND;
1964 /* replied success "+OK" */
1970 /* end of command */
1974 /* EM_DEBUG_LOG("UID list [%s]", p); */
1976 uid_elem = (emcore_uid_list *)malloc(sizeof(emcore_uid_list));
1978 EM_DEBUG_EXCEPTION("malloc falied...");
1980 err = EMAIL_ERROR_OUT_OF_MEMORY;
1984 memset(uid_elem, 0x00, sizeof(emcore_uid_list));
1986 /* format : "1 AAA6FHEAAAQrB6c1ymXxty04yks7hcQ7" */
1992 uid_elem->msgno = atoi(p);
1993 uid_elem->uid = EM_SAFE_STRDUP(s+1);
1996 /* check downloaded_uids */
1997 if (downloaded_uids) {
1999 for (i = 0; i < count; ++i) {
2000 if (!strcmp(uid_elem->uid, downloaded_uids[i].s_uid)) {
2001 downloaded_uids[i].flag = 1;
2008 fetch_data_p = *uid_list;
2010 while (fetch_data_p->next)
2011 fetch_data_p = fetch_data_p->next;
2013 fetch_data_p->next = uid_elem;
2016 *uid_list = uid_elem;
2025 IMAPLOCAL *imaplocal = NULL;
2030 if (!stream || !(imaplocal = stream->local) || !imaplocal->netstream) {
2031 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN; */
2035 /* send FETCH UID */
2036 memset(tag, 0x00, sizeof(tag));
2037 memset(cmd, 0x00, sizeof(cmd));
2039 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
2040 SNPRINTF(cmd, sizeof(cmd), "%s UID FETCH %d:* (FLAGS)\015\012", tag, 1);
2041 #ifdef FEATURE_CORE_DEBUG
2042 EM_DEBUG_LOG("[IMAP] >>> %s", cmd);
2044 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
2045 EM_DEBUG_EXCEPTION("net_sout failed...");
2047 err = EMAIL_ERROR_CONNECTION_BROKEN;
2051 /* get uid from replied data */
2052 while (imaplocal->netstream) {
2053 if (!(p = net_getline(imaplocal->netstream)))
2056 /* EM_DEBUG_LOG(" [IMAP] <<< %s", p); */
2058 /* tagged line - end of command */
2059 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))
2062 /* check that reply is reply to our command */
2063 /* format : "* 9 FETCH (UID 68)" */
2064 if (!strstr(p, "FETCH (FLAGS")) {
2069 if (for_delete == EM_CORE_GET_UIDS_FOR_NO_DELETE) {
2070 if ((ref_account->retrieval_mode == EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (strstr(p, "\\Seen"))) {
2076 uid_elem = (emcore_uid_list *)malloc(sizeof(emcore_uid_list));
2078 EM_DEBUG_EXCEPTION("malloc failed...");
2080 err = EMAIL_ERROR_OUT_OF_MEMORY;
2084 memset(uid_elem, 0x00, sizeof(emcore_uid_list));
2085 /* parse uid, sequence, flags from replied data */
2087 /* parse uid from replied data */
2092 err = EMAIL_ERROR_INVALID_RESPONSE;
2099 uid_elem->msgno = atoi(s);
2101 if (strstr(++t, "\\Deleted"))
2102 uid_elem->flag.deleted = 1;
2105 s = strstr(++t, "UID ");
2107 s += strlen("UID ");
2111 err = EMAIL_ERROR_INVALID_RESPONSE;
2118 err = EMAIL_ERROR_INVALID_RESPONSE;
2123 uid_elem->uid = EM_SAFE_STRDUP(s);
2125 /* check downloaded_uids */
2126 if (downloaded_uids) {
2128 for (i = 0; i < count; ++i) {
2129 if (uid_elem->uid && !strcmp(uid_elem->uid, downloaded_uids[i].s_uid)) {
2130 downloaded_uids[i].flag = 1;
2131 free(uid_elem->uid);
2132 free(uid_elem); uid_elem = NULL;
2140 fetch_data_p = *uid_list;
2142 while (fetch_data_p->next)
2143 fetch_data_p = fetch_data_p->next;
2145 fetch_data_p->next = uid_elem;
2148 *uid_list = uid_elem;
2162 if (uid_elem && ret == false)
2168 if (mailbox && !mailbox->mail_stream) {
2169 emcore_close_mailbox(0, stream);
2174 emcore_free_account(ref_account);
2175 EM_SAFE_FREE(ref_account);
2181 EM_DEBUG_FUNC_END("ret [%d]", ret);
2185 int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
2187 EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid[%p], msgno[%p], err_code[%p]", mailbox, uid, msgno, err_code);
2190 int err = EMAIL_ERROR_NONE;
2191 MAILSTREAM *stream = NULL;
2192 IMAPLOCAL *imaplocal = NULL;
2193 email_account_t *ref_account = NULL;
2194 void *tmp_stream = NULL;
2195 char tag[32], cmd[64];
2198 if (!mailbox || !uid) {
2199 EM_DEBUG_EXCEPTION("mailbox[%p], uid[%p], msgno[%p]", mailbox, uid, msgno);
2201 err = EMAIL_ERROR_INVALID_PARAM;
2205 if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
2206 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
2208 err = EMAIL_ERROR_INVALID_ACCOUNT;
2212 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
2213 err = EMAIL_ERROR_INVALID_ACCOUNT; /* EMAIL_ERROR_INVALID_PARAM; */
2217 if (!mailbox->mail_stream) {
2218 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)) {
2219 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
2224 stream = (MAILSTREAM *)tmp_stream;
2227 stream = mailbox->mail_stream;
2229 if (!stream || !(imaplocal = stream->local) || !imaplocal->netstream) {
2230 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN; */
2234 /* send SEARCH UID */
2235 memset(tag, 0x00, sizeof(tag));
2236 memset(cmd, 0x00, sizeof(cmd));
2238 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
2239 SNPRINTF(cmd, sizeof(cmd), "%s SEARCH UID %s\015\012", tag, uid);
2241 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
2242 EM_DEBUG_EXCEPTION("net_sout failed...");
2244 err = EMAIL_ERROR_CONNECTION_BROKEN;
2248 /* get message number from replied data */
2249 while (imaplocal->netstream) {
2250 if (!(p = net_getline(imaplocal->netstream)))
2253 /* tagged line - end of command */
2254 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))
2257 /* check that reply is reply to our command */
2258 /* format : "* SEARCH 68", if not found, "* SEARCH" */
2259 if (!strstr(p, "SEARCH ") || (p[9] < '0' || p[9] > '9')) {
2273 err = EMAIL_ERROR_MAIL_NOT_FOUND;
2279 if (mailbox && !mailbox->mail_stream){
2280 emcore_close_mailbox(0, stream);
2285 emcore_free_account(ref_account);
2286 EM_SAFE_FREE(ref_account);
2292 EM_DEBUG_FUNC_END("ret [%d]", ret);
2296 int emcore_get_msgno(emcore_uid_list *uid_list, char *uid, int *msgno, int *err_code)
2298 EM_DEBUG_FUNC_BEGIN("uid_list[%p], uid[%s], msgno[%p], err_code[%p]", uid_list, uid, msgno, err_code);
2301 int err = EMAIL_ERROR_MAIL_NOT_FOUND; /* EMAIL_ERROR_NONE; */
2303 if (!uid || !msgno || !uid_list) {
2304 EM_DEBUG_EXCEPTION("uid_list[%p], uid[%p], msgno[%p]", uid_list, uid, msgno);
2306 err = EMAIL_ERROR_INVALID_PARAM;
2310 EM_DEBUG_LOG(" >> uid[%s]", uid);
2313 if (!strcmp(uid_list->uid, uid)) {
2314 *msgno = uid_list->msgno;
2316 EM_DEBUG_LOG("*msgno[%d]", *msgno);
2318 err = EMAIL_ERROR_NONE;
2324 uid_list = uid_list->next;
2328 if (err_code != NULL)
2331 EM_DEBUG_FUNC_END("ret [%d]", ret);
2335 int emcore_get_uid(emcore_uid_list *uid_list, int msgno, char **uid, int *err_code)
2337 EM_DEBUG_FUNC_BEGIN();
2339 int ret = false, err = EMAIL_ERROR_NONE;
2341 if (!uid || msgno <= 0 || !uid_list){
2342 err = EMAIL_ERROR_INVALID_PARAM;
2347 if (uid_list->msgno == msgno) {
2349 if (!(*uid = EM_SAFE_STRDUP(uid_list->uid))) {
2350 EM_DEBUG_EXCEPTION("strdup failed...");
2351 err = EMAIL_ERROR_OUT_OF_MEMORY;
2360 uid_list = uid_list->next;
2364 err = EMAIL_ERROR_MAIL_NOT_FOUND;
2370 EM_DEBUG_FUNC_END("ret [%d]", ret);
2374 int emcore_free_uids(emcore_uid_list *uid_list, int *err_code)
2376 EM_DEBUG_FUNC_BEGIN("uid_list[%p], err_code[%p]", uid_list, err_code);
2379 int err = EMAIL_ERROR_NONE;
2380 emcore_uid_list *p = NULL;
2383 EM_DEBUG_EXCEPTION(" uid_list[%p]\n", uid_list);
2384 err = EMAIL_ERROR_INVALID_PARAM;
2390 uid_list = uid_list->next;
2391 EM_SAFE_FREE(p->uid);
2402 EM_DEBUG_FUNC_END("ret [%d]", ret);
2407 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
2408 /* callback for GET_APPENDUID - shasikala.p */
2409 void mail_appenduid(char *mailbox, unsigned long uidvalidity, SEARCHSET *set)
2411 EM_DEBUG_FUNC_BEGIN("mailbox - %s", mailbox);
2412 EM_DEBUG_LOG("UID - %ld", set->first);
2414 memset(g_append_uid_rsp, 0x00, 129);
2416 sprintf(g_append_uid_rsp, "%ld", set->first);
2417 EM_DEBUG_LOG("append uid - %s", g_append_uid_rsp);
2420 INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
2422 EM_DEBUG_FUNC_BEGIN("mail_id [%d]", mail_id);
2424 int err = EMAIL_ERROR_NONE;
2427 int attachment_tbl_count = 0;
2429 char *long_enc_path = NULL;
2431 char set_flags[100] = { 0, };
2432 ENVELOPE *envelope = NULL;
2435 MAILSTREAM *stream = NULL;
2436 email_account_t *account_ref = NULL;
2437 emstorage_mail_tbl_t *mail_table_data = NULL;
2438 emstorage_attachment_tbl_t *attachment_tbl_data = NULL;
2439 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
2441 /* get a mail from mail table */
2442 if (!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
2443 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2447 account_ref = emcore_get_account_reference(mail_table_data->account_id);
2449 if(account_ref == NULL || account_ref->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
2450 EM_DEBUG_EXCEPTION("This account doesn't support sync");
2451 err = EMAIL_ERROR_INVALID_ACCOUNT;
2455 if ((err = emstorage_get_attachment_list(mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
2456 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2460 EM_DEBUG_LOG("mailbox_name [%s]", mail_table_data->mailbox_name);
2461 if (!emstorage_get_mailbox_by_name(mail_table_data->account_id, 0, mail_table_data->mailbox_name, &mailbox_tbl, false, &err) || !mailbox_tbl){
2462 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
2466 if (mailbox_tbl->local_yn == 0) {
2467 EM_DEBUG_EXCEPTION("The mailbox [%s] is not on server.", mail_table_data->mailbox_name);
2468 err = EMAIL_ERROR_INVALID_MAILBOX;
2472 if (!emcore_get_long_encoded_path(mail_table_data->account_id, mail_table_data->mailbox_name, '/', &long_enc_path, &err)) {
2473 EM_DEBUG_EXCEPTION(">>emcore_get_long_encoded_path : Failed [%d] ", err);
2477 if (!long_enc_path) {
2478 EM_DEBUG_EXCEPTION(">>long_enc_path : NULL ");
2482 if (!emcore_make_rfc822_file_from_mail(mail_table_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fname, NULL, &err)){
2483 EM_DEBUG_EXCEPTION(" emcore_make_rfc822_file_from_mail failed [%d]", err);
2488 if (!(fp = fopen(fname, "a+"))) {
2489 EM_DEBUG_EXCEPTION("fopen failed - %s", fname);
2490 err = EMAIL_ERROR_SYSTEM_FAILURE;
2496 EM_DEBUG_EXCEPTION("fp is NULL..!");
2497 err = EMAIL_ERROR_SYSTEM_FAILURE;
2503 /* get file length */
2504 if(fseek(fp, 0, SEEK_END) != 0 || (len = ftell(fp)) == -1) {
2505 EM_DEBUG_EXCEPTION("fseek or ftell failed");
2506 err = EMAIL_ERROR_SYSTEM_FAILURE;
2511 EM_DEBUG_LOG("Composed file name [%s] and file size [%d]", fname, len);
2516 if (!emcore_connect_to_remote_mailbox(mail_table_data->account_id, 0, (void **)&stream, &err)){
2517 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : Mailbox open[%d]", err);
2521 /* added for copying server UID to DB */
2522 mail_parameters(stream, SET_APPENDUID, mail_appenduid);
2524 data = (char *)malloc(len + 1);
2525 /* copy data from file to data */
2526 read_size = fread(data, sizeof (char), len, fp);
2527 if (read_size != len){
2529 EM_DEBUG_EXCEPTION("Read from file failed");
2532 INIT(&str, mail_string, data, len);
2534 sprintf(set_flags, "\\Seen");
2536 if (mail_table_data->flags_seen_field){
2537 if (!mail_append_full(stream, long_enc_path, set_flags, NULL, &str)) {
2538 EM_DEBUG_EXCEPTION("mail_append failed -");
2539 err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
2544 if (!mail_append_full(stream, long_enc_path, NULL, NULL, &str)) {
2545 EM_DEBUG_EXCEPTION("mail_append failed -");
2546 err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
2551 /* Update read_mail_uid tbl */
2552 if (!emcore_add_read_mail_uid(mailbox_tbl, mail_table_data->mailbox_name, mail_table_data->mail_id, g_append_uid_rsp, mail_table_data->mail_size, 0, &err)) {
2553 EM_DEBUG_EXCEPTION(" emcore_add_read_mail_uid failed [%d]", err);
2554 emstorage_rollback_transaction(NULL, NULL, NULL);
2561 #ifdef __FEATURE_LOCAL_ACTIVITY__
2563 emstorage_activity_tbl_t new_activity;
2564 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
2565 new_activity.activity_type = ACTIVITY_SAVEMAIL;
2566 new_activity.account_id = account_id;
2567 new_activity.mail_id = mail_id;
2568 new_activity.dest_mbox = NULL;
2569 new_activity.server_mailid = NULL;
2570 new_activity.src_mbox = NULL;
2572 if (!emcore_delete_activity(&new_activity, &err)){
2573 EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_SAVEMAIL] [%d] ", err);
2576 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2579 EM_SAFE_FREE(long_enc_path);
2582 emcore_free_account(account_ref);
2583 EM_SAFE_FREE(account_ref);
2590 mail_free_envelope(&envelope);
2592 if (mail_table_data)
2593 emstorage_free_mail(&mail_table_data, 1, NULL);
2595 if (attachment_tbl_data)
2596 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
2599 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
2603 EM_SAFE_FREE(fname);
2607 emcore_close_mailbox(0, stream);
2611 EM_DEBUG_FUNC_END("err [%d]", err);
2617 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2619 static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int input_maibox_id, int *err_code)
2621 EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_id[%d], uid[%p], input_maibox_id[%d]", account_id, mail_id, uid, input_maibox_id);
2624 int err = EMAIL_ERROR_NONE;
2625 email_account_t *account_ref = NULL;
2626 emstorage_mailbox_tbl_t* mailbox = NULL;
2628 if (account_id < FIRST_ACCOUNT_ID || mail_id < 0 || NULL == uid || 0 == input_maibox_id){
2629 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2630 err = EMAIL_ERROR_INVALID_PARAM;
2634 account_ref = emcore_get_account_reference(account_id);
2636 email_event_partial_body_thd pbd_event;
2638 memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
2640 pbd_event.account_id = account_id;
2641 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_POP3)
2642 pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_POP3_WAIT;
2644 pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_IMAP4;
2646 if ( (err = emstorage_get_mailbox_by_id(input_maibox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
2647 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
2651 pbd_event.mailbox_id = input_maibox_id;
2652 pbd_event.mail_id = mail_id;
2653 pbd_event.server_mail_id = strtoul(uid, NULL, 0);
2654 pbd_event.mailbox_name = EM_SAFE_STRDUP(mailbox->mailbox_name);
2656 EM_DEBUG_LOG("input_mailbox_name name [%d]", pbd_event.mailbox_id);
2657 EM_DEBUG_LOG("uid [%s]", uid);
2658 EM_DEBUG_LOG("pbd_event.account_id[%d], pbd_event.mail_id[%d], pbd_event.server_mail_id [%d]", pbd_event.account_id, pbd_event.mail_id , pbd_event.server_mail_id);
2660 if (!emcore_insert_pbd_activity(&pbd_event, &pbd_event.activity_id, &err)){
2661 EM_DEBUG_EXCEPTION("Inserting Partial Body Download activity failed with error[%d]", err);
2665 if (false == emcore_is_partial_body_thd_que_full()){
2666 /* h.gahaut : Before inserting the event into event queue activity_type should be made 0
2667 Because partial body thread differentiates events coming from DB and event queue
2668 on the basis of activity_type and event_type fields */
2670 pbd_event.activity_type = 0;
2671 pbd_event.event_type = EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD;
2673 if (!emcore_insert_partial_body_thread_event(&pbd_event, &err)){
2674 EM_DEBUG_EXCEPTION("Inserting Partial body thread event failed with error[%d]", err);
2678 /*h.gahlaut : Partial body thread has created a copy of event for itself so this local event should be freed here*/
2679 if (!emcore_free_partial_body_thd_event(&pbd_event, &err))
2680 EM_DEBUG_EXCEPTION("Freeing Partial body thread event failed with error[%d]", err);
2683 EM_DEBUG_LOG(" Activity inserted only in DB .. Queue is Full");
2692 emstorage_free_mailbox(&mailbox, 1, &err);
2696 emcore_free_account(account_ref);
2697 EM_SAFE_FREE(account_ref);
2700 if (NULL != err_code)
2703 EM_DEBUG_FUNC_END("ret [%d]", ret);
2707 static int emcore_parse_bodystructure(void *stream, IMAPPARSEDREPLY *reply_from_server, char *bodystructure, BODY **body, struct _m_content_info **cnt_info, int *total_mail_size, unsigned long *uid_no)
2709 EM_DEBUG_FUNC_BEGIN("stream:[%p], reply_from_server:[%p], bodystructure:[%p]", stream, reply_from_server, bodystructure);
2711 int err = EMAIL_ERROR_NONE;
2713 if (!stream || !reply_from_server || !bodystructure) {
2714 EM_DEBUG_EXCEPTION("Invalid paramter");
2715 err = EMAIL_ERROR_INVALID_PARAM;
2719 int p_total_mail_size = 0;
2720 unsigned long p_uid_no = 0;
2721 char *uid_string = NULL;
2722 char *bodystructure_start = NULL;
2723 char *bodystructure_string = NULL;
2724 char *bodystructure_uid_start = NULL;
2725 BODY *p_body = NULL;
2726 struct _m_content_info *p_cnt_info = NULL;
2728 /* Get the body strcuture string */
2729 bodystructure_start = strstr(bodystructure, "BODYSTRUCTURE (") + strlen("BODYSTRUCTURE");
2730 if (bodystructure_start == NULL) {
2731 EM_DEBUG_EXCEPTION("Invalid bodystructure");
2732 err = EMAIL_ERROR_INVALID_PARAM;
2736 bodystructure_string = strdup(bodystructure_start);
2739 bodystructure_uid_start = strcasestr(bodystructure, "UID ") + strlen("UID ");
2741 uid_string = em_malloc((bodystructure_start - bodystructure_uid_start) + 1);
2742 if (uid_string == NULL) {
2743 EM_DEBUG_EXCEPTION("em_malloc failed");
2744 err = EMAIL_ERROR_OUT_OF_MEMORY;
2748 memcpy(uid_string, bodystructure_uid_start, bodystructure_start - bodystructure_uid_start);
2750 p_uid_no = strtoul(uid_string, NULL, 0);
2751 EM_DEBUG_LOG("UID number : [%d]", p_uid_no);
2753 /* Parse the bodystructure string */
2754 p_body = mail_newbody();
2755 if (p_body == NULL) {
2756 EM_DEBUG_EXCEPTION("New body creationg failed");
2757 err = EMAIL_ERROR_OUT_OF_MEMORY;
2761 imap_parse_body_structure(stream, p_body, (unsigned char **)&bodystructure_string, reply_from_server);
2763 /* Get the total mail size */
2764 if (emcore_set_fetch_body_section(p_body, true, &p_total_mail_size, &err) < 0) {
2765 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed:[%d]", err);
2769 /* Fill the content_info structure */
2770 if (!(p_cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2771 EM_DEBUG_EXCEPTION("em_malloc failed");
2772 err = EMAIL_ERROR_OUT_OF_MEMORY;
2776 if (emcore_get_body(stream, 0, 0, 0, p_body, p_cnt_info, &err) < 0 || !p_cnt_info) {
2777 EM_DEBUG_EXCEPTION("emcore_get_body failed[%d]", err);
2778 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;
2784 EM_SAFE_FREE(uid_string);
2785 // EM_SAFE_FREE(bodystructure_string);
2787 if (err != EMAIL_ERROR_NONE) {
2789 emcore_free_content_info(p_cnt_info);
2792 mail_free_body(&p_body);
2795 *cnt_info = p_cnt_info;
2800 if (total_mail_size)
2801 *total_mail_size = p_total_mail_size;
2807 EM_DEBUG_FUNC_END("Err:[%d]", err);
2811 static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_info, int account_id, int mail_id, int mailbox_id, int *output_total_attachment_size, int *output_attachment_count)
2813 EM_DEBUG_FUNC_BEGIN("cnt_info : [%p], account_id : [%d], mail_id : [%d], mailbox_id : [%d]", cnt_info, account_id, mail_id, mailbox_id);
2814 int err = EMAIL_ERROR_NONE;
2816 if (!cnt_info || !account_id || !mail_id || !mailbox_id) {
2817 EM_DEBUG_EXCEPTION("Invalid parameter");
2818 err = EMAIL_ERROR_INVALID_PARAM;
2823 if (!cnt_info->file || !cnt_info->file->name) {
2824 EM_DEBUG_LOG("Not include attachment");
2828 int attachment_count = 0;
2829 int total_attach_size = 0;
2830 int inline_attachment_count = 0;
2831 emstorage_attachment_tbl_t attachment_tbl = {0};
2832 struct attachment_info *attach_info = NULL;
2834 attachment_tbl.account_id = account_id;
2835 attachment_tbl.mail_id = mail_id;
2836 attachment_tbl.mailbox_id = mailbox_id;
2837 attachment_tbl.attachment_save_status = 0;
2839 for (attachment_count = 1, attach_info = cnt_info->file; attach_info; attach_info = attach_info->next, attachment_count++) {
2840 if (attach_info->type == INLINE_ATTACHMENT) {
2841 EM_DEBUG_LOG("INLINE ATTACHMENT");
2842 inline_attachment_count++;
2846 total_attach_size += attach_info->size;
2847 attachment_tbl.attachment_size = attach_info->size;
2848 attachment_tbl.attachment_path = attach_info->save;
2849 attachment_tbl.attachment_name = attach_info->name;
2850 attachment_tbl.attachment_drm_type = attach_info->drm;
2851 attachment_tbl.attachment_mime_type = attach_info->attachment_mime_type;
2852 #ifdef __ATTACHMENT_OPTI__
2853 attachment_tbl.encoding = attach_info->encoding;
2854 attachment_tbl.section = attach_info->section;
2857 EM_DEBUG_LOG("attachment_count [%d]", attachment_count);
2859 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
2860 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed : [%d]", err);
2867 if (output_attachment_count)
2868 *output_attachment_count = attachment_count - inline_attachment_count;
2870 if (output_total_attachment_size)
2871 *output_total_attachment_size = total_attach_size;
2873 EM_DEBUG_FUNC_END("err : [%d]", err);
2876 #define UID_RANGE_STRING_LENGTH 3000
2877 #define TEMP_STRING_LENGTH 50
2879 static int emcore_parse_html_part_for_partial_body(char *start_header, char *boundary_string, char *bufsendforparse, char *text_html, int body_size)
2881 EM_DEBUG_FUNC_BEGIN("start_header [%s], boundary_string [%s], bufsendforparse [%s], text_html [%s], body_size [%d]", start_header, boundary_string, bufsendforparse, text_html, body_size);
2883 int err = EMAIL_ERROR_NONE;
2885 int iEncodingHeader = 0;
2886 int enc_type = ENCOTHER, dec_len = 0, html_length = 0;
2887 char EncodingHeader[40] = {0};
2888 char Encoding[30] = {0};
2889 char *pEncodingHeaderEnd = NULL;
2890 char *txt_html = NULL;
2891 char *pHeaderStart = NULL;
2892 char *start = NULL, *end = NULL;
2893 char *temp_enc1 = NULL;
2895 EM_DEBUG_LOG("Content-Type : text/html or message/rfc822 or text/rfc822-headers");
2897 pHeaderStart = start_header;
2898 pHeaderStart = pHeaderStart-2;
2901 } while (*pHeaderStart != LF && bufsendforparse < pHeaderStart);
2905 memcpy(EncodingHeader, pHeaderStart, 25);
2907 if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
2908 pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
2909 memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart+27));
2910 iEncodingHeader = 1;
2913 /* HTML Content found */
2915 if(!start_header) { /*prevent 27448*/
2916 EM_DEBUG_EXCEPTION("start_header NULL");
2919 txt_html = start_header;
2920 txt_html = strstr(txt_html, CRLF_STRING CRLF_STRING);
2922 if (txt_html != NULL){
2923 txt_html += 4; /* txt_html points at html content */
2925 char multipart_boundary[100] = {0};
2926 char *multipart_related_boundry = NULL;
2927 char *multipart_related_boundry_end = NULL;
2928 if (iEncodingHeader == 1)
2929 multipart_related_boundry = pHeaderStart;
2931 multipart_related_boundry = start_header;
2933 multipart_related_boundry = multipart_related_boundry - 3;
2934 multipart_related_boundry_end = multipart_related_boundry;
2936 while (bufsendforparse < multipart_related_boundry && *multipart_related_boundry != LF && *multipart_related_boundry != NULL_CHAR)
2937 multipart_related_boundry -= 1;
2939 multipart_related_boundry += 1;
2940 memcpy(multipart_boundary, multipart_related_boundry, multipart_related_boundry_end - multipart_related_boundry);
2942 EM_DEBUG_LOG("multipart_boundary [%s], boundary_string [%s]", multipart_boundary, boundary_string);
2944 if (strcmp(multipart_boundary, boundary_string) == 0)
2945 end = strstr(txt_html, boundary_string);
2947 end = strstr(txt_html, multipart_boundary);
2949 memset(multipart_boundary, 0, EM_SAFE_STRLEN(multipart_boundary));
2951 EM_DEBUG_LOG("end [%p], txt_html [%p]", end, txt_html);
2954 EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
2956 end = txt_html + body_size - (txt_html - bufsendforparse);
2959 else if(end == txt_html) { /* empty multipart */
2960 EM_DEBUG_LOG("Emtpy HTML multipart");
2964 if ((*(end-2) == CR) && (*(end-1) == LF))
2966 else if ((*(end-2) == CR) && (*(end-1) == LF)
2967 && (*(end-4) == CR) && (*(end-3) == LF))
2970 EM_DEBUG_EXCEPTION(" Content not per as grammar.");
2973 EM_DEBUG_LOG("end [%p], txt_html [%p]", end, txt_html);
2975 EM_DEBUG_LOG("iEncodingHeader [%d]", iEncodingHeader);
2977 if (iEncodingHeader == 1){
2978 enc_type = ENCOTHER;
2979 if (strncasecmp(Encoding, "base64", strlen("base64")) == 0)
2980 enc_type = ENCBASE64;
2981 else if (strncasecmp(Encoding, "quoted-printable", strlen("quoted-printable")) == 0)
2982 enc_type = ENCQUOTEDPRINTABLE;
2984 EM_DEBUG_LOG("enc_type [%d]", enc_type);
2986 memcpy(text_html, start, end - txt_html);
2988 if (emcore_decode_body_text(text_html, end - txt_html, enc_type , &dec_len, &err) < 0)
2989 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
2991 else if ((temp_enc1 = (char *)strcasestr(start_header, "Content-transfer-encoding:")) && (temp_enc1 < end)){ /*prevent 27448*/
2993 start_header = temp_enc1;
2995 start_header += strlen("Content-Transfer-Encoding:");
2996 start_header = em_skip_whitespace_without_strdup(start_header);
2999 EM_DEBUG_EXCEPTION(" Invalid parsing ");
3001 enc_type = ENCOTHER;
3002 if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3003 enc_type = ENCBASE64;
3004 else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3005 enc_type = ENCQUOTEDPRINTABLE;
3007 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3009 memcpy(text_html, start, end - txt_html);
3011 if (emcore_decode_body_text(text_html, end - txt_html, enc_type , &dec_len, &err) < 0)
3012 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3013 html_length = dec_len;
3016 EM_DEBUG_LOG("Decoded length = %d", dec_len);
3017 EM_DEBUG_LOG("start - %s", start);
3020 memcpy(text_html, start, end-txt_html);
3021 html_length = (end-txt_html);
3024 /* EM_DEBUG_LOG(" Content-Type: text/html [%s]\n", text_html); */
3027 EM_DEBUG_EXCEPTION(" Invalid html body content ");
3029 EM_DEBUG_FUNC_END();
3035 /*For the following scenario
3036 *------= SAMSUNG_mySingle_MIME_MULTIPART_BOUNDARY
3037 *Content-Transfer-Encoding : base64
3038 *Content-Type : text/plain; charset = "windows-1252"
3040 *Message-ID: <11512468.945901271910226702.JavaMail.weblogic@epml03>
3043 #define CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding"
3045 static int emcore_parse_plain_part_for_partial_body(char *header_start_string, char *start_header, char *boundary_string, char *bufsendforparse, char *text_plain, int body_size)
3047 EM_DEBUG_FUNC_BEGIN("header_start_string[%s], start_header[%s], boundary_string [%s], bufsendforparse [%s], text_plain [%s]", header_start_string, start_header, boundary_string, bufsendforparse, text_plain);
3048 int err = EMAIL_ERROR_NONE, iEncodingHeader = 0, enc_type = ENCOTHER;
3049 int dec_len = 0, strcmpret = -1;
3050 char *pHeaderStart = NULL, *pEncodingHeaderEnd = NULL;
3051 char EncodingHeader[40] = {0, };
3052 char Encoding[30] = {0, };
3053 char *temp_text_boundary = NULL;
3054 char *start = NULL, *end = NULL, *txt_plain = NULL, *temp_enc1 = NULL;
3056 EM_DEBUG_LOG("Content-Type : text/plain");
3058 pHeaderStart = header_start_string;
3059 temp_text_boundary = start_header;
3061 memcpy(EncodingHeader, pHeaderStart, 25);
3063 if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
3064 pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
3065 memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart + 27));
3066 iEncodingHeader = 1;
3069 /* Plain text content found */
3070 if(!start_header) { /*prevent 27450*/
3071 EM_DEBUG_EXCEPTION("start_header NULL");
3074 txt_plain = start_header;
3075 txt_plain = strstr(txt_plain, CRLF_STRING CRLF_STRING);
3077 if (txt_plain != NULL){
3078 txt_plain += EM_SAFE_STRLEN(CRLF_STRING CRLF_STRING); /* txt_plain points at plain text content */
3080 /* Fix is done for mail having "Content-Type: text/plain" but there is no content but having only attachment. */
3082 strcmpret = strncmp(txt_plain, boundary_string, EM_SAFE_STRLEN(boundary_string));
3083 if (strcmpret == 0){
3087 end = strstr(txt_plain, boundary_string);
3090 EM_DEBUG_LOG("Text body contents exceeds limited Bytes");
3091 end = txt_plain + body_size - (txt_plain - bufsendforparse);
3094 /* EM_DEBUG_LOG("pbd_event[temp_count].partial_body_complete - %d", partial_body_complete); */
3096 if ((*(end-2) == CR) && (*(end-1) == LF))
3098 else if ((*(end-2) == CR) && (*(end-1) == LF)
3099 && (*(end-4) == CR) && (*(end-3) == LF))
3102 EM_DEBUG_EXCEPTION(" Content not per as grammar.");
3105 if (iEncodingHeader == 1){
3106 enc_type = ENCOTHER;
3107 if (strncasecmp(Encoding, "base64", strlen("base64")) == 0)
3108 enc_type = ENCBASE64;
3109 else if (strncasecmp(Encoding, "quoted-printable", strlen("quoted-printable")) == 0)
3110 enc_type = ENCQUOTEDPRINTABLE;
3112 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3114 memcpy(text_plain, start, end - txt_plain);
3116 if (emcore_decode_body_text(text_plain, end - txt_plain, enc_type , &dec_len, &err) < 0)
3117 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3119 else if (start_header && ((temp_enc1 = (char *)strcasestr(start_header, "Content-transfer-encoding:")) != NULL) && !(temp_enc1 && temp_enc1 >= end)){
3121 start_header = temp_enc1;
3123 start_header += strlen("Content-Transfer-Encoding:");
3124 start_header = em_skip_whitespace_without_strdup(start_header);
3127 EM_DEBUG_EXCEPTION(" Invalid parsing ");
3129 enc_type = ENCOTHER;
3130 if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3131 enc_type = ENCBASE64;
3132 else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3133 enc_type = ENCQUOTEDPRINTABLE;
3135 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3136 memcpy(text_plain, start, end - txt_plain);
3137 if (emcore_decode_body_text(text_plain, end - txt_plain, enc_type , &dec_len, &err) < 0)
3138 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3141 EM_DEBUG_LOG("Decoded length = %d", dec_len);
3142 /* EM_DEBUG_LOG("start - %s", start); */ /* print raw MIME content. */
3145 memcpy(text_plain, start, end-txt_plain);
3147 /* EM_DEBUG_LOG(" Content-type: text/plain [%s]\n", text_plain); */
3151 EM_DEBUG_EXCEPTION(" Invalid text body content ");
3153 EM_DEBUG_FUNC_END();
3159 /* Content-Type: IMAGE/octet-stream; name = Default.png */
3160 /* Content-Transfer-Encoding: BASE64 */
3161 /* Content-ID: <4b0d6810b17291f9438783a8eb9d5228@com.samsung.slp.email> */
3162 /* Content-Disposition: inline; filename = Default.png */
3164 static void emcore_free_email_image_data(email_image_data **image_data, int count)
3166 EM_DEBUG_FUNC_BEGIN("image_data : [%p], count : [%d]", image_data, count);
3168 if ((image_data == NULL) || (*image_data == NULL) || (count <= 0)) {
3169 EM_DEBUG_EXCEPTION("Invalid paramter");
3173 email_image_data *p = *image_data;
3176 for (i = 0; i < count; i++) {
3177 EM_SAFE_FREE(p[i].text_image);
3178 EM_SAFE_FREE(p[i].content_id);
3179 EM_SAFE_FREE(p[i].mime_type);
3185 EM_DEBUG_FUNC_END();
3188 static int emcore_parse_image_part_for_partial_body(char *header_start_string, char *start_header, char *boundary_string, char *bufsendforparse, email_image_data *image_data, int body_size)
3190 EM_DEBUG_FUNC_BEGIN("boundary_string : [%s]", boundary_string);
3192 int err = EMAIL_ERROR_NONE;
3193 char *multiple_image = NULL;
3194 int donot_parse_next_image = 0;
3195 char *image_boundary = NULL;
3196 char *image_boundary_end = NULL;
3197 char *temp_image_boundary = NULL;
3198 int i = 0, ch_image = 0, cidno = 0;
3199 int enc_type = ENCOTHER, dec_len = 0, image_length = 0;
3200 char *p = header_start_string;
3201 char *start = NULL, *end = NULL, *txt_image = NULL;
3202 char *temp_image = NULL;
3203 char *temp_cid1 = NULL;
3204 char *cid_end = NULL;
3205 char *temp_enc1 = NULL;
3206 char *p_boundary_string = NULL;
3208 if(image_data == NULL) {
3209 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3210 err = EMAIL_ERROR_INVALID_PARAM;
3214 image_boundary = start_header;
3215 image_boundary_end = image_boundary - 2;
3216 image_boundary = image_boundary - 2;
3218 EM_DEBUG_LOG("Content-type: image");
3220 while (*image_boundary != LF)
3225 temp_image_boundary = em_malloc((image_boundary_end - image_boundary) + 1);
3226 if (temp_image_boundary == NULL) {
3227 EM_DEBUG_EXCEPTION("em_malloc failed");
3228 err = EMAIL_ERROR_OUT_OF_MEMORY;
3232 if (image_boundary != NULL && image_boundary_end != NULL)
3233 memcpy(temp_image_boundary, image_boundary, image_boundary_end-image_boundary);
3235 if ((char *)strcasestr((const char *)temp_image_boundary, "Content-type:") == NULL)
3236 p_boundary_string = strdup(temp_image_boundary);
3238 p_boundary_string = EM_SAFE_STRDUP(boundary_string);
3240 EM_SAFE_FREE(temp_image_boundary);
3243 if (multiple_image != NULL){
3245 start_header = multiple_image;
3248 emcore_get_content_type_from_mime_string(start_header, &(image_data[i].mime_type));
3250 if ((strcasestr(p, "Content-Disposition: attachment")) || (!strcasestr(p, "Content-ID: <"))){
3251 EM_DEBUG_EXCEPTION(" Body has attachment no need to parse ");
3253 multiple_image = NULL;
3255 else { /* HTML Content found */
3258 int boundarylen = -1;
3260 char *temp_name = NULL;
3261 char *decoded_filename = NULL;
3263 memset(image_data[i].image_file_name, 0, 100);
3265 if(!start_header) { /*prevent 27449*/
3266 EM_DEBUG_EXCEPTION("start_header NULL");
3267 EM_SAFE_FREE(p_boundary_string);
3271 txt_image = start_header;
3272 temp_image = start_header;
3274 temp_name = strstr(txt_image, "name=");
3275 if (temp_name != NULL){
3276 temp_image = temp_name;
3277 if (*(temp_image+5) == '"')
3278 temp_image = temp_image+5;
3280 while (*temp_image != CR){
3282 memcpy(image_data[i].image_file_name+ch_image, temp_image, 1);
3286 if ((*(temp_name+4) == '=') && (*(temp_name+5) == '\"'))
3287 image_data[i].image_file_name[ch_image-2] = '\0';
3289 if ((*(temp_name+4) == '=') && (*(temp_name+5) != '\"'))
3290 image_data[i].image_file_name[ch_image-1] = '\0';
3292 decoded_filename = emcore_decode_rfc2047_text(image_data[i].image_file_name, &err);
3293 if(decoded_filename) {
3294 memset(image_data[i].image_file_name, 0, 100);
3295 memcpy(image_data[i].image_file_name, decoded_filename, EM_SAFE_STRLEN(decoded_filename));
3296 EM_SAFE_FREE(decoded_filename);
3300 if (((temp_cid1 = (char *)strcasestr((const char *)start_header, "Content-ID: <")) != NULL)){
3303 temp_image = temp_cid1;
3307 cid_end = strstr(temp_image, "\076"); /* 076 == '>' */
3309 image_data[i].content_id = (char *)em_malloc(cid_end-cid+5);
3310 if (image_data[i].content_id != NULL){
3311 strcpy(image_data[i].content_id, "cid:");
3312 memcpy(image_data[i].content_id+4, cid, cid_end-cid);
3315 EM_DEBUG_EXCEPTION("em_malloc() failed");
3317 if (image_data[i].image_file_name[0] == '\0')
3318 memcpy(image_data[i].image_file_name, cid, cid_end - cid);
3321 txt_image = strstr(txt_image, CRLF_STRING CRLF_STRING);
3323 if (txt_image != NULL){
3324 txt_image += 4; /* txt_image points at image content */
3326 end = strstr(txt_image, p_boundary_string);
3330 EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
3331 /* end points to end of partial body data */
3332 end = txt_image + body_size - (txt_image-bufsendforparse);
3335 boundarylen = EM_SAFE_STRLEN(p_boundary_string);
3339 if ((temp_enc1 = (char *)strcasestr((const char *)start_header, "Content-transfer-encoding:")) != NULL){ /*prevent 27449*/
3341 start_header = temp_enc1;
3343 start_header += strlen("Content-Transfer-Encoding:");
3344 start_header = em_skip_whitespace_without_strdup(start_header);
3347 EM_DEBUG_EXCEPTION(" Invalid parsing ");
3349 enc_type = ENCOTHER;
3350 if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3351 enc_type = ENCBASE64;
3352 else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3353 enc_type = ENCQUOTEDPRINTABLE;
3355 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3357 image_data[i].text_image = (char *)em_malloc((end-txt_image)+1);
3358 if (image_data[i].text_image){
3359 memcpy(image_data[i].text_image, start, end-txt_image);
3360 if (emcore_decode_body_text(image_data[i].text_image, end-txt_image, enc_type , &(image_data[i].dec_len), &err) < 0)
3361 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3363 image_length = image_data[i].dec_len;
3366 EM_DEBUG_EXCEPTION("em_malloc() failed");
3369 EM_DEBUG_LOG("Decoded length [%d]", dec_len);
3372 image_data[i].text_image = (char *)em_malloc(end-txt_image);
3373 if (image_data[i].text_image)
3374 memcpy(image_data[i].text_image, start, end - txt_image);
3376 EM_DEBUG_EXCEPTION("em_malloc() failed");
3380 donot_parse_next_image = 1;
3381 EM_DEBUG_EXCEPTION(" Invalid html body content ");
3386 multiple_image = (char *)strcasestr((const char *)end, "Content-type: image");
3389 } while (multiple_image != NULL && donot_parse_next_image != 1 && (i < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
3391 EM_SAFE_FREE(p_boundary_string);
3393 EM_DEBUG_FUNC_END();
3397 static int emcore_find_boundary_string_of_the_part(const char *whole_string, const char *first_line_of_part, char **result_boundary_string, int *error)
3399 EM_DEBUG_FUNC_BEGIN("whole_string[%p], first_line_of_part[%p], result_boundary_string[%p]", whole_string, first_line_of_part, result_boundary_string);
3400 int ret = false, err = EMAIL_ERROR_NONE;
3401 char *boundary_cur = NULL, *boundary_end = NULL, *boundary_string = NULL;
3403 if(!whole_string || !first_line_of_part || !result_boundary_string) {
3404 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3405 err = EMAIL_ERROR_INVALID_PARAM;
3409 if(first_line_of_part - 2 > whole_string) {
3410 boundary_cur = (char*)first_line_of_part - 2; /* 2 means CRLF. */
3411 boundary_end = boundary_cur;
3415 } while (whole_string < boundary_cur && *boundary_cur != LF && *boundary_cur != NULL_CHAR);
3419 if(boundary_end > boundary_cur && boundary_cur > whole_string) {
3420 EM_DEBUG_LOG("boundary_end - boundary_cur + 15 [%d]", boundary_end - boundary_cur + 15);
3421 boundary_string = em_malloc(boundary_end - boundary_cur + 15);
3422 if(!boundary_string) {
3423 EM_DEBUG_EXCEPTION("em_malloc failed");
3424 err = EMAIL_ERROR_OUT_OF_MEMORY;
3427 memcpy(boundary_string, boundary_cur, boundary_end - boundary_cur);
3428 EM_DEBUG_LOG("boundary_string [%s]", boundary_string);
3429 *result_boundary_string = boundary_string;
3432 EM_DEBUG_EXCEPTION("There is no string before the part");
3433 err = EMAIL_ERROR_ON_PARSING;
3438 EM_DEBUG_EXCEPTION("There is no string before the part");
3439 err = EMAIL_ERROR_ON_PARSING;
3448 EM_DEBUG_FUNC_END("ret[%d], err[%d]", ret, err);
3452 static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_content_info *cnt_info, int encoding_type, char **output_text_plain, char **output_text_html, email_image_data **output_image_data)
3454 EM_DEBUG_FUNC_BEGIN("body_str:[%p], cnt_info:[%p], encoding_type:[%d]", body_str, cnt_info, encoding_type);
3455 int err = EMAIL_ERROR_NONE;
3457 if (!body_str || strlen(body_str) == 0 || !cnt_info) {
3458 EM_DEBUG_EXCEPTION("Invalid paramter");
3459 err = EMAIL_ERROR_INVALID_PARAM;
3463 if (!output_text_plain || !output_text_html || !output_image_data) {
3464 EM_DEBUG_EXCEPTION("Invalid parameter");
3465 err = EMAIL_ERROR_INVALID_PARAM;
3470 int no_alternative_part_flag = 0;
3472 char *boundary_start = NULL;
3473 char *boundary_end = NULL;
3474 char *boundary_string = NULL;
3475 char *p_body_str = NULL;
3476 char *start_header = NULL;
3477 char *text_plain = NULL;
3478 char *text_html = NULL;
3479 email_image_data *image_data = NULL;
3481 char *temp_alternative_plain_header = NULL;
3482 char *temp_content_type = NULL;
3484 p_body_str = EM_SAFE_STRDUP(body_str);
3486 text_plain = em_malloc(body_size + 1);
3487 text_html = em_malloc(body_size + 1);
3489 if (!text_plain || !text_html) {
3490 EM_DEBUG_EXCEPTION("em_malloc failed");
3491 err = EMAIL_ERROR_OUT_OF_MEMORY;
3495 if (!strcasestr(p_body_str, "Content-Type: ") && (cnt_info->text.plain || cnt_info->text.html)) {
3496 /* Encoded Content-Type: text/html or Content-Type: text/plain */
3497 /* No Partial body has No headers with Content-Type: text/html or Content-Type: text/plain */
3499 EM_DEBUG_LOG("mbody->encoding [%d] ", encoding_type);
3501 if (emcore_decode_body_text(p_body_str, EM_SAFE_STRLEN(p_body_str), encoding_type, &dec_len, &err) < 0) {
3502 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3506 EM_DEBUG_LOG("Decoded length [%d]", dec_len);
3509 if (cnt_info->text.plain) {
3510 memcpy(text_plain, p_body_str, dec_len);
3513 if (cnt_info->text.html) {
3514 memcpy(text_html, p_body_str, dec_len);
3517 } else { /* Partial body has headers with Content-Type: text/html or Content-Type: text/plain */
3518 no_alternative_part_flag = 0;
3519 if (((temp_alternative_plain_header = (char *)strcasestr(p_body_str, "Content-type: multipart/alternative")) != NULL)) { /* Found 'alternative' */
3520 if (((temp_content_type = (char *)strcasestr(p_body_str, "Content-type: text/plain")) != NULL)) {
3521 if (temp_content_type < temp_alternative_plain_header) {
3522 /* This part is text/plain not alternative. */
3524 no_alternative_part_flag = 1;
3526 EM_DEBUG_LOG(" Content-type: multipart/alternative ");
3527 boundary_start = strstr(temp_alternative_plain_header, "--");
3528 if(!boundary_start) goto FINISH_OFF; /*prevent 37946 */
3529 boundary_end = strcasestr(boundary_start, "Content-type:");
3530 if(!boundary_end) goto FINISH_OFF; /*prevent 37946 */
3532 boundary_string = em_malloc(boundary_end - (boundary_start + strlen("--")));
3533 if (boundary_string == NULL) {
3534 EM_DEBUG_EXCEPTION("em_malloc failed");
3535 err = EMAIL_ERROR_OUT_OF_MEMORY;
3539 memcpy(boundary_string, boundary_start, boundary_end - (boundary_start + strlen("--")));
3543 no_alternative_part_flag = 1;
3545 if (no_alternative_part_flag) {
3546 boundary_start = strstr(p_body_str, "--");
3547 if(!boundary_start) goto FINISH_OFF; /*prevent 37946 */
3548 boundary_end = strcasestr(boundary_start, "Content-type:");
3549 if(!boundary_end) goto FINISH_OFF; /*prevent 37946 */
3551 boundary_string = em_malloc(boundary_end - (boundary_start + strlen("--")));
3552 if (boundary_string == NULL) {
3553 EM_DEBUG_EXCEPTION("em_malloc failed");
3554 err = EMAIL_ERROR_OUT_OF_MEMORY;
3558 memcpy(boundary_string, boundary_start, boundary_end - (boundary_start + strlen("--")));
3561 if (boundary_string && boundary_end) { /*prevent 37946 */
3562 EM_DEBUG_LOG("boundary_string : [%s]", boundary_string);
3564 if (((start_header = (char *)strcasestr(boundary_end, "Content-Type: text/html")) != NULL) && (no_html != 1) &&(((char *)strcasestr(boundary_end, "Content-Type: message/rfc822")) == NULL) &&
3565 (((char *)strcasestr(boundary_end, "Content-Type: text/rfc822-headers")) == NULL))
3566 emcore_parse_html_part_for_partial_body(start_header, boundary_string, p_body_str, text_html, body_size);
3568 if (((start_header = (char *)strcasestr(boundary_end, "Content-Type: text/plain")) != NULL)) {
3569 char *internal_boundary_string = NULL;
3571 if(!emcore_find_boundary_string_of_the_part(p_body_str, start_header, &internal_boundary_string, &err)) {
3572 EM_DEBUG_EXCEPTION("internal_boundary_string failed [%d]", err);
3575 if(!internal_boundary_string)
3576 internal_boundary_string = EM_SAFE_STRDUP(boundary_string);
3578 emcore_parse_plain_part_for_partial_body(boundary_end, start_header, internal_boundary_string, p_body_str, text_plain, body_size);
3579 EM_SAFE_FREE(internal_boundary_string);
3582 if (((start_header = (char *)strcasestr((const char *)boundary_end, "Content-type: image/jpeg")) != (char *)NULL) ||
3583 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/jpg")) != (char *)NULL) ||
3584 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/gif")) != (char *)NULL) ||
3585 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/bmp")) != (char *)NULL) ||
3586 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/png")) != (char *)NULL)) {
3587 image_data = em_malloc(sizeof(email_image_data) * IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3588 if (image_data == NULL) {
3589 EM_DEBUG_EXCEPTION("em_malloc failed");
3590 err = EMAIL_ERROR_OUT_OF_MEMORY;
3594 emcore_parse_image_part_for_partial_body(boundary_end, start_header, boundary_string, p_body_str, image_data, body_size);
3601 EM_SAFE_FREE(p_body_str);
3602 EM_SAFE_FREE(boundary_string);
3604 if (err != EMAIL_ERROR_NONE) {
3605 EM_SAFE_FREE(text_plain);
3606 EM_SAFE_FREE(text_html);
3609 emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3611 *output_text_plain = text_plain;
3612 *output_text_html = text_html;
3613 *output_image_data = image_data;
3616 EM_DEBUG_FUNC_END("err : [%d]", err);
3620 #define TAG_LENGTH 16
3621 #define COMMAND_LENGTH 2000
3622 static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3624 EM_DEBUG_FUNC_BEGIN("stream [%p], input_download_size[%d], pbd_event [%p], item_count [%d], error [%p]", stream, input_download_size, pbd_event, item_count, error);
3626 int ret = false, err = EMAIL_ERROR_NONE;
3629 int total_mail_size = 0, total_attachment_size = 0;
3630 int temp_count = 0, attachment_num = 0;
3631 unsigned long uidno = 0;
3632 char buf[512] = {0, };
3633 char *text_plain = NULL;
3634 char *text_html = NULL;
3635 char uid_range_string_to_be_downloaded[UID_RANGE_STRING_LENGTH] = {0, };
3636 char imap_tag[TAG_LENGTH] = {0, };
3637 char command[COMMAND_LENGTH] = {0, };
3638 char *temp_data_html = NULL;
3639 char uid_string[TEMP_STRING_LENGTH] = {0, };
3640 IMAPLOCAL *imaplocal = NULL;
3641 IMAPPARSEDREPLY *reply_from_server = NULL;
3642 emstorage_mail_tbl_t *mail = NULL;
3643 email_partial_buffer *imap_response = NULL;
3645 struct _m_content_info *cnt_info = NULL;
3646 emstorage_attachment_tbl_t attachment_tbl;
3647 email_event_partial_body_thd *stSectionNo = NULL;
3648 email_image_data *image_data = NULL;
3650 if (!(stream) || !(imaplocal = stream->local) || !imaplocal->netstream || !pbd_event) {
3651 EM_DEBUG_EXCEPTION("invalid parameter");
3652 err = EMAIL_ERROR_INVALID_PARAM;
3653 EM_DEBUG_FUNC_END("ret [%d]", ret);
3657 EM_DEBUG_LOG("Start of emcore_get_section_for_partial_download, item_count = %d ", item_count);
3659 /* For constructing UID list which is having 10 UID or less at a time */
3660 for (j = 0, stSectionNo = pbd_event; (stSectionNo != NULL && j < item_count); j++)
3662 EM_DEBUG_LOG("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
3663 j, stSectionNo[j].account_id, stSectionNo[j].mail_id, stSectionNo[j].server_mail_id, stSectionNo[j].activity_id);
3665 if (i32_index >= UID_RANGE_STRING_LENGTH){
3666 EM_DEBUG_EXCEPTION("String length exceeded its limitation!");
3670 if (j == item_count - 1)
3671 i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu", stSectionNo[j].server_mail_id);
3673 i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu,", stSectionNo[j].server_mail_id);
3676 SNPRINTF(imap_tag, TAG_LENGTH, "%08lx", 0xffffffff & (stream->gensym++));
3677 SNPRINTF(command, COMMAND_LENGTH, "%s UID FETCH %s (BODYSTRUCTURE BODY.PEEK[TEXT]<0.%d>)\015\012", imap_tag, uid_range_string_to_be_downloaded, input_download_size);
3679 EM_DEBUG_LOG("command : %s", command);
3681 /* Sending out the IMAP request */
3682 if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
3683 EM_DEBUG_EXCEPTION("net_sout failed...");
3684 err = EMAIL_ERROR_CONNECTION_BROKEN;
3688 /* responce from the server */
3689 imap_response = emcore_get_response_from_server(imaplocal->netstream, imap_tag, &reply_from_server, input_download_size, item_count);
3691 if (!imap_response || !reply_from_server ){
3692 EM_DEBUG_EXCEPTION(" Invalid response from emcore_get_response_from_server");
3696 for (i = 0; i < item_count ; i++) {
3697 total_mail_size = 0;
3698 total_attachment_size = 0;
3702 if ((err = emcore_parse_bodystructure(stream, reply_from_server, imap_response[i].header, &body, &cnt_info, &total_mail_size, &uidno)) != EMAIL_ERROR_NONE) {
3703 EM_DEBUG_EXCEPTION("emcore_parse_bodystructure failed : [%d]", err);
3707 /* Search the account id of pbd_event */
3708 for (temp_count = 0; temp_count <= item_count && pbd_event[temp_count].server_mail_id != uidno; temp_count++)
3711 if (temp_count > item_count) {
3712 EM_DEBUG_EXCEPTION("Can't find proper server_mail_id");
3716 /* Start to parse the body */
3717 EM_DEBUG_LOG("Start partial body");
3719 /* Check the body download status and body size */
3720 SNPRINTF(uid_string, sizeof(uid_string), "%ld", uidno);
3721 if (!emstorage_get_maildata_by_servermailid(pbd_event[temp_count].account_id, uid_string, &mail, true, &err) || !mail) {
3722 EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
3723 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || !mail)
3727 if (mail->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
3728 EM_DEBUG_LOG("Downloaded mail");
3732 /* Update the attachment info except inline attachment */
3733 if ((err = emcore_update_attachment_except_inline(cnt_info, pbd_event[temp_count].account_id, mail->mail_id, pbd_event[temp_count].mailbox_id, &total_attachment_size, &attachment_num)) != EMAIL_ERROR_NONE) {
3734 EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
3738 EM_DEBUG_LOG("total_mail_size:[%d], total_attachment_size:[%d], attachment_num:[%d]", total_mail_size, total_attachment_size, attachment_num);
3739 mail->mail_size = total_mail_size;
3740 mail->attachment_count = attachment_num;
3742 if (imap_response[i].body_len == 0) {
3743 EM_DEBUG_LOG("BODY size is zero");
3747 if ((err = emcore_parse_body_for_imap(imap_response[i].body, imap_response[i].body_len, cnt_info, body->encoding, &text_plain, &text_html, &image_data)) != EMAIL_ERROR_NONE) {
3748 EM_DEBUG_EXCEPTION("emcore_parse_body_for_imap failed");
3752 memset(buf, 0x00, sizeof(buf));
3754 if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3755 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3757 if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err))
3758 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3760 if (!emstorage_create_file(text_plain, EM_SAFE_STRLEN(text_plain), buf, &err))
3761 EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3763 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3764 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3766 if (image_data != NULL && image_data[0].text_image != NULL && image_data[0].text_image[0] != NULL_CHAR) {
3767 char *result_string_of_replcaing = NULL;
3769 int content_index = 0;
3771 temp_data_html = em_malloc(imap_response[i].body_len + 1);
3772 if(!temp_data_html) {
3773 EM_DEBUG_EXCEPTION("em_malloc failed");
3774 err = EMAIL_ERROR_OUT_OF_MEMORY;
3778 memset(buf, 0x00, sizeof(buf));
3779 if (text_html != NULL)
3780 memcpy(temp_data_html, text_html, EM_SAFE_STRLEN(text_html));
3781 /* EM_SAFE_STRNCPY(temp_data_html, text_html, text_html); */
3784 if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3785 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3787 if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, image_data[store_file].image_file_name, buf, &err))
3788 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3790 if (!emstorage_create_file(image_data[store_file].text_image, image_data[store_file].dec_len, buf, &err))
3791 EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3793 if (mail->body_download_status != EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED){
3794 memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3795 attachment_tbl.mail_id = mail->mail_id;
3796 attachment_tbl.account_id = pbd_event[temp_count].account_id;
3797 attachment_tbl.mailbox_id = pbd_event[temp_count].mailbox_id;
3798 attachment_tbl.attachment_name = image_data[store_file].image_file_name;
3799 attachment_tbl.attachment_size = image_data[store_file].dec_len;
3800 attachment_tbl.attachment_path = buf;
3801 attachment_tbl.attachment_save_status = 1;
3802 attachment_tbl.attachment_inline_content_status = 1; /* set to 1 for inline image */
3803 attachment_tbl.attachment_mime_type = image_data[store_file].mime_type;
3804 mail->attachment_count++;
3805 mail->inline_content_count++;
3806 if (!emstorage_add_attachment (&attachment_tbl, false, false, &err))
3807 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed - %d", err);
3811 } while (image_data[store_file].text_image != NULL && image_data[store_file].text_image[0] != NULL_CHAR && (store_file < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
3813 while (image_data[content_index].text_image != NULL && image_data[content_index].text_image[0] != NULL_CHAR &&
3814 image_data[content_index].content_id && image_data[content_index].content_id[0] != NULL_CHAR && (content_index < IMAGE_DISPLAY_PARTIAL_BODY_COUNT)){ /* Finding CID in HTML and replacing with image name. */
3815 result_string_of_replcaing = em_replace_string((char *)temp_data_html, (char *)image_data[content_index].content_id, (char *)image_data[content_index].image_file_name);
3817 EM_SAFE_STRNCPY(temp_data_html, result_string_of_replcaing, EM_SAFE_STRLEN(result_string_of_replcaing));
3818 EM_SAFE_FREE(result_string_of_replcaing);
3820 if (strstr(temp_data_html, image_data[content_index].content_id) != NULL)
3821 continue; /* Replace content id on HTML with same file name one more time. */
3826 memset(text_html, 0, imap_response[i].body_len + 1);
3828 if (temp_data_html != NULL)
3829 memcpy(text_html, temp_data_html, EM_SAFE_STRLEN(temp_data_html));
3831 EM_SAFE_FREE(temp_data_html);
3834 if (EM_SAFE_STRLEN(text_html) > 0) {
3835 memset(buf, 0x00, sizeof(buf));
3836 char html_body[MAX_CHARSET_VALUE] = {0x00, };
3837 if (cnt_info->text.plain_charset != NULL){
3838 if (EM_SAFE_STRLEN(cnt_info->text.plain_charset) < MAX_CHARSET_VALUE)
3839 memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
3841 memcpy(html_body, "UTF-8", strlen("UTF-8"));
3843 if (html_body[0] != NULL_CHAR)
3844 strcat(html_body, HTML_EXTENSION_STRING);
3846 memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3848 if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3849 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3851 if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, html_body, buf, &err))
3852 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3854 if (!emstorage_create_file(text_html, EM_SAFE_STRLEN(text_html), buf, &err))
3855 EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3857 mail->file_path_html = EM_SAFE_STRDUP(buf);
3859 /* free the plain, html, image data */
3860 EM_SAFE_FREE(text_plain);
3861 EM_SAFE_FREE(text_html);
3863 emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3867 mail->body_download_status = (total_mail_size - total_attachment_size < input_download_size) ? 1 : 2;
3868 EM_DEBUG_LOG("mail->body_download_status [%d]", mail->body_download_status);
3870 /* Get preview text */
3871 if ( (err = emcore_get_preview_text_from_file(mail->file_path_plain, mail->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail->preview_text))) != EMAIL_ERROR_NONE)
3872 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file() failed[%d]", err);
3874 /* Update body contents */
3875 if (!emstorage_change_mail_field(mail->mail_id, UPDATE_PARTIAL_BODY_DOWNLOAD, mail, true, &err))
3876 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed - %d", err);
3879 emstorage_free_mail(&mail, 1, NULL);
3881 if (false == emcore_delete_pbd_activity(pbd_event[temp_count].account_id, pbd_event[temp_count].mail_id, pbd_event[temp_count].activity_id, &err)){
3882 EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
3895 EM_DEBUG_EXCEPTION("Failed download for the uid list %s", command);
3897 if(reply_from_server) {
3898 EM_SAFE_FREE(reply_from_server->key);
3899 EM_SAFE_FREE(reply_from_server->line);
3900 EM_SAFE_FREE(reply_from_server->tag);
3901 EM_SAFE_FREE(reply_from_server->text);
3902 EM_SAFE_FREE(reply_from_server);
3905 EM_SAFE_FREE(text_plain);
3906 EM_SAFE_FREE(text_html);
3909 emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3912 emcore_free_content_info(cnt_info);
3915 mail_free_body(&body);
3918 emstorage_free_mail(&mail, 1, NULL);
3921 emcore_free_email_partial_buffer(&imap_response, item_count);
3923 EM_DEBUG_FUNC_END("ret [%d]", ret);
3927 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body_for_pop3(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3929 EM_DEBUG_FUNC_BEGIN("stream [%p], pbd_event [%p], item_count [%d], error [%p]", stream, pbd_event, item_count, error);
3930 int ret = false, err = EMAIL_ERROR_NONE;
3933 if (!stream || !pbd_event) {
3934 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3935 err = EMAIL_ERROR_INVALID_PARAM;
3939 for (i = 0; i < item_count; i++) {
3940 EM_DEBUG_LOG("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
3941 i, pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].server_mail_id, pbd_event[i].activity_id);
3943 if (!emcore_download_body_multi_sections_bulk(stream, pbd_event[i].account_id, pbd_event[i].mail_id, false, false, input_download_size, 0 , &err)){
3944 EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed");
3948 if (false == emcore_delete_pbd_activity(pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].activity_id, &err)){
3949 EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
3960 EM_DEBUG_FUNC_END("ret [%d] err [%d]", ret, err);
3966 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3968 EM_DEBUG_FUNC_BEGIN("stream [%p], pbd_event [%p], item_count [%d], error [%p]", stream, pbd_event, item_count, error);
3969 int ret = false, err = EMAIL_ERROR_NONE;
3970 emstorage_account_tbl_t *pbd_account_tbl = NULL;
3971 int auto_download_size = 0;
3973 if (!stream || !pbd_event) {
3974 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3975 err = EMAIL_ERROR_INVALID_PARAM;
3979 if (!emstorage_get_account_by_id(pbd_event[0].account_id, EMAIL_ACC_GET_OPT_DEFAULT, &pbd_account_tbl, true, &err)) {
3980 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
3984 auto_download_size = (pbd_account_tbl->auto_download_size<4096)?4096:pbd_account_tbl->auto_download_size;
3986 switch (pbd_account_tbl->incoming_server_type){
3987 case EMAIL_SERVER_TYPE_IMAP4:
3988 ret = emcore_download_bulk_partial_mail_body_for_imap(stream, auto_download_size, pbd_event, item_count, &err);
3990 case EMAIL_SERVER_TYPE_POP3:
3991 ret = emcore_download_bulk_partial_mail_body_for_pop3(stream, auto_download_size, pbd_event, item_count, &err);
3994 err = EMAIL_ERROR_NOT_SUPPORTED;
4004 emstorage_free_account(&pbd_account_tbl, 1, NULL);
4006 EM_DEBUG_FUNC_END("ret [%d] err [%d]", ret, err);
4010 static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buffer, int item_count)
4012 EM_DEBUG_FUNC_BEGIN("count : [%d]", item_count);
4014 if (item_count <= 0 || !partial_buffer || !*partial_buffer) {
4015 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4019 email_partial_buffer *p = *partial_buffer;
4022 for (i = 0; i < item_count ; i++, p++) {
4023 EM_SAFE_FREE(p->header);
4024 EM_SAFE_FREE(p->body);
4027 EM_SAFE_FREE(*partial_buffer);
4028 EM_DEBUG_FUNC_END();
4031 static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream, char *tag, IMAPPARSEDREPLY **reply, int input_download_size, int item_count)
4033 EM_DEBUG_FUNC_BEGIN();
4035 if (!nstream || !tag || !reply){
4036 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4040 email_partial_buffer *retPartialBuffer = NULL;
4041 IMAPPARSEDREPLY *ret_reply = NULL;
4042 char *pre_content = NULL;
4049 int header_start = false;
4051 retPartialBuffer = (email_partial_buffer *)em_malloc(sizeof(email_partial_buffer) * item_count);
4052 if (NULL == retPartialBuffer){
4053 EM_DEBUG_EXCEPTION("em_malloc failed");
4058 if (!(pline = net_getline(nstream))) {
4059 EM_DEBUG_EXCEPTION("net_getline failed...");
4063 /* Search the start of header */
4064 if (strcasestr(pline, "BODYSTRUCTURE") != NULL) {
4065 header_len = EM_SAFE_STRLEN(pline);
4066 header_start = true;
4069 /* Search the end of header */
4070 if ((p = strcasestr(pline, "BODY[TEXT]")) != NULL && header_len > 0) {
4071 /* Get the header content */
4072 retPartialBuffer[count].header_len = (p - pline) + EM_SAFE_STRLEN(pre_content);
4073 retPartialBuffer[count].header = em_malloc(retPartialBuffer[count].header_len + 1);
4074 if (retPartialBuffer[count].header == NULL) {
4075 EM_DEBUG_EXCEPTION("em_malloc failed");
4079 if (pre_content != NULL)
4080 SNPRINTF(retPartialBuffer[count].header, retPartialBuffer[count].header_len, "%s%s", pre_content, pline);
4082 SNPRINTF(retPartialBuffer[count].header, retPartialBuffer[count].header_len, "%s", pline);
4084 /* Get the body content */
4085 p += strlen("BODY[TEXT]<0> {");
4086 body_size = atoi(p);
4087 retPartialBuffer[count].body_len = (body_size > input_download_size) ? input_download_size : body_size ;
4088 retPartialBuffer[count].body = em_malloc(retPartialBuffer[count].body_len + 1);
4089 if (retPartialBuffer[count].body == NULL) {
4090 EM_DEBUG_EXCEPTION("em_malloc failed");
4094 if (net_getbuffer(nstream, retPartialBuffer[count].body_len, retPartialBuffer[count].body) <= 0) {
4095 EM_DEBUG_EXCEPTION("net_getbuffer failed");
4100 header_start = false;
4104 pre_content = EM_SAFE_STRDUP(pline);
4106 if (0 == strncmp(pline, tag, EM_SAFE_STRLEN(tag))) {
4108 ret_reply = em_malloc(sizeof(IMAPPARSEDREPLY));
4110 EM_DEBUG_EXCEPTION("em_malloc failed");
4117 if (0 == strncmp(pline + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
4118 ret_reply->line = (unsigned char*)EM_SAFE_STRDUP(tag);
4119 ret_reply->tag = (unsigned char*)EM_SAFE_STRDUP(tag);
4120 ret_reply->key = (unsigned char*)strdup("OK");
4121 ret_reply->text = (unsigned char*)strdup("Success");
4122 EM_SAFE_FREE(pline);
4126 EM_DEBUG_EXCEPTION("Tagged Response not OK. IMAP4 Response -> [%s]", pline);
4127 ret_reply->line = (unsigned char*)EM_SAFE_STRDUP(tag);
4128 ret_reply->tag = (unsigned char*)EM_SAFE_STRDUP(tag);
4129 ret_reply->key = (unsigned char*)strdup("NO");
4130 ret_reply->text = (unsigned char*)strdup("Fail");
4136 EM_SAFE_FREE(pline);
4143 EM_SAFE_FREE(pline);
4144 EM_SAFE_FREE(pre_content);
4147 emcore_free_email_partial_buffer(&retPartialBuffer, item_count);
4150 EM_DEBUG_FUNC_END("retPartialBuffer [%p]", retPartialBuffer);
4151 return retPartialBuffer;
4154 #endif /* __FEATURE_PARTIAL_BODY_DOWNLOAD__ */