4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File : email-core-mail.c
26 * Desc : Mail Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
42 #include <sys/types.h>
47 #include <contacts-svc.h>
49 #include "email-internal-types.h"
52 #include "email-utilities.h"
53 #include "email-core-global.h"
54 #include "email-core-utils.h"
55 #include "email-core-mail.h"
56 #include "email-core-mime.h"
57 #include "email-core-mailbox.h"
58 #include "email-storage.h"
59 #include "email-network.h"
60 #include "email-core-mailbox-sync.h"
61 #include "email-core-event.h"
62 #include "email-core-account.h"
64 #include "email-convert.h"
65 #include "email-debug-log.h"
67 #ifdef __FEATURE_DRM__
68 #include <drm_client.h>
69 #endif /* __FEATURE_DRM__ */
71 #define ST_SILENT (long) 0x2 /* don't return results */
72 #define ST_SET (long) 0x4 /* set vs. clear */
74 static char g_new_server_uid[129];
76 bool only_body_download = false;
78 int multi_part_body_size = 0;
79 int is_multi_part_body_download_all = 0;
80 int _pop3_receiving_mail_id = 0;
81 int _pop3_received_body_size = 0;
82 int _pop3_last_notified_body_size = 0;
83 int _pop3_total_body_size = 0;
85 int _imap4_received_body_size = 0;
86 int _imap4_last_notified_body_size = 0;
87 int _imap4_total_body_size = 0;
88 int _imap4_download_noti_interval_value = 0;
90 BODY **g_inline_list = NULL ;
91 int g_inline_count = 0;
94 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count);
95 static int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *total_body_size, int *err_code);
97 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
100 #ifdef FEATURE_CORE_DEBUG
101 static char *_getType(int type)
104 case 0: return "TYPETEXT";
105 case 1: return "TYPEMULTIPART";
106 case 2: return "TYPEMESSAGE";
107 case 3: return "TYPEAPPLICATION";
108 case 4: return "TYPEAUDIO";
109 case 5: return "TYPEVIDEO";
110 case 6: return "TYPEMODEL";
111 case 7: return "TYPEOTHER";
113 return g_strdup_printf("%d", type);
116 static char *_getEncoding(int encoding)
119 case 0: return "ENC7BIT";
120 case 1: return "ENC8BIT";
121 case 2: return "ENCBINARY";
122 case 3: return "ENCBASE64";
123 case 4: return "ENCQUOTEDPRINTABLE";
124 case 5: return "ENCOTHER";
126 return g_strdup_printf("%d", encoding);
129 static void _print_parameter(PARAMETER *param)
131 while (param != NULL) {
132 EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
133 EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
139 static void _print_stringlist(STRINGLIST *stringlist)
141 while (stringlist != NULL) {
142 EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
143 EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
145 stringlist = stringlist->next;
149 static void _print_body(BODY *body, int recursive)
151 EM_DEBUG_LOG(" ========================================================== ");
154 EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
155 EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
156 EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
158 EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
160 _print_parameter(body->parameter);
162 EM_DEBUG_LOG("body->id[%s]", body->id);
163 EM_DEBUG_LOG("body->description[%s]", body->description);
165 EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
166 EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
168 _print_parameter(body->disposition.parameter);
170 EM_DEBUG_LOG("body->language[%p]", body->language);
172 _print_stringlist(body->language);
174 EM_DEBUG_LOG("body->location[%s]", body->location);
176 EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
177 EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
178 EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
180 EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
181 EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
182 EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
184 EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
186 EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
187 EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
189 EM_DEBUG_LOG("body->md5[%s]", body->md5);
190 EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
193 PART *part = body->nested.part;
195 while (part != NULL) {
196 _print_body(&(part->body), recursive);
202 EM_DEBUG_LOG(" ========================================================== ");
204 #endif /* FEATURE_CORE_DEBUG */
207 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
209 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
212 int err = EMAIL_ERROR_NONE;
214 POP3LOCAL *pop3local = NULL;
219 EM_DEBUG_EXCEPTION("stream[%p]", stream);
220 err = EMAIL_ERROR_INVALID_PARAM;
224 if (!(pop3local = stream->local) || !pop3local->netstream) {
225 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
226 err = EMAIL_ERROR_UNKNOWN;
230 memset(cmd, 0x00, sizeof(cmd));
232 SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
234 #ifdef FEATURE_CORE_DEBUG
235 EM_DEBUG_LOG("[POP3] >>> %s", cmd);
238 /* send command : delete specified mail */
239 if (!net_sout(pop3local->netstream, cmd, (int)strlen(cmd))) {
240 EM_DEBUG_EXCEPTION("net_sout failed...");
241 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN; */
245 /* receive response */
246 if (!(p = net_getline(pop3local->netstream))) {
247 EM_DEBUG_EXCEPTION("net_getline failed...");
248 err = EMAIL_ERROR_INVALID_RESPONSE;
252 #ifdef FEATURE_CORE_DEBUG
253 EM_DEBUG_LOG("[POP3] <<< %s", p);
256 if (*p == '-') { /* '-ERR' */
257 err = EMAIL_ERROR_POP3_DELE_FAILURE; /* EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
261 if (*p != '+') { /* '+OK' ... */
262 err = EMAIL_ERROR_INVALID_RESPONSE;
278 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
280 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
283 EM_DEBUG_FUNC_BEGIN();
287 int err = EMAIL_ERROR_NONE;
289 unsigned long first_uid = 0;
290 unsigned long last_uid = 0;
292 unsigned long *old_server_uid = NULL;
293 unsigned long *new_server_uid = NULL;
295 SEARCHSET *temp = sourceset;
297 char old_server_uid_char[129];
298 char new_server_uid_char[129];
300 if (NULL == sourceset || NULL == destset) {
302 sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids
303 if sourceset is NULL then corresponding dest set will be NULL
306 EM_DEBUG_LOG("emcore_mail_copyuid_ex failed : Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
310 /* To get count of mails actually moved */
313 if (temp->first > 0) {
314 first_uid = temp->first;
318 if (temp->last > 0) {
319 last_uid = temp->last;
321 while (first_uid < last_uid) {
334 EM_DEBUG_LOG("Count of mails copied [%d]", count);
335 old_server_uid = em_malloc(count * sizeof(unsigned long));
336 new_server_uid = em_malloc(count * sizeof(unsigned long));
338 /* While loop below will collect all old server uid from sourceset into old_server_uid array */
341 if (sourceset->first > 0) {
342 first_uid = sourceset->first;
345 old_server_uid[index] = first_uid;
347 if (sourceset->last > 0) {
348 last_uid = sourceset->last;
350 while (first_uid < last_uid) {
354 old_server_uid[index] = first_uid;
359 sourceset = sourceset->next;
362 /* While loop below will collect all new server uid from destset into new_server_uid array */
365 first_uid = last_uid = 0;
368 if (destset->first > 0) {
369 first_uid = destset->first;
372 new_server_uid[index] = first_uid;
374 if (destset->last > 0) {
375 last_uid = destset->last;
377 while (first_uid < last_uid) {
381 new_server_uid[index] = first_uid;
386 destset = destset->next;
389 /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
391 for (i = 0; i <= index; ++i) {
393 memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
394 sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
396 EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
398 memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
399 sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
401 EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
403 if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
404 EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
407 if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
408 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
411 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
413 if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
414 EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
420 EM_SAFE_FREE(old_server_uid);
421 EM_SAFE_FREE(new_server_uid);
425 INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, int src_mailbox_id, int mail_ids[], int num, int dest_mailbox_id, int *error_code)
427 EM_DEBUG_FUNC_BEGIN();
428 MAILSTREAM *stream = NULL;
429 int err_code = EMAIL_ERROR_NONE;
430 email_account_t *ref_account = NULL;
433 email_id_set_t *id_set = NULL;
434 int id_set_count = 0;
436 email_uid_range_set *uid_range_set = NULL;
437 int len_of_each_range = 0;
439 email_uid_range_set *uid_range_node = NULL;
441 char **string_list = NULL;
442 int string_count = 0;
443 emstorage_mailbox_tbl_t* dest_mailbox = NULL;
445 if (num <= 0 || account_id <= 0 || src_mailbox_id <= 0 || dest_mailbox_id <= 0 || NULL == mail_ids) {
446 if (error_code != NULL) {
447 *error_code = EMAIL_ERROR_INVALID_PARAM;
449 EM_DEBUG_LOG("Invalid Parameters- num[%d], account_id[%d], src_mailbox_id[%d], dest_mailbox_id[%d], mail_ids[%p]", num, account_id, src_mailbox_id, dest_mailbox_id, mail_ids);
453 ref_account = emcore_get_account_reference(account_id);
455 if (NULL == ref_account) {
456 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
458 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
463 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
464 *error_code = EMAIL_ERROR_INVALID_PARAM;
469 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) {
470 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
475 if (NULL != stream) {
476 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
477 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
478 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
479 /* Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids */
480 /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid */
481 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
483 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code)) {
484 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
488 if ( (err_code = emstorage_get_mailbox_by_id(dest_mailbox_id, &dest_mailbox)) != EMAIL_ERROR_NONE || !dest_mailbox) {
489 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err_code);
493 /* Now execute one by one each comma separated string of mail_ids */
495 for (i = 0; i < string_count; ++i) {
496 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
498 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
499 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
503 /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
505 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
507 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
508 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
512 uid_range_node = uid_range_set;
514 while (uid_range_node != NULL) {
515 /* Remove comma from end of uid_range */
516 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
517 EM_DEBUG_LOG("uid_range_node->uid_range - %s", uid_range_node->uid_range);
518 if (!mail_copy_full(stream, uid_range_node->uid_range, dest_mailbox->mailbox_name, CP_UID | CP_MOVE)) {
519 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be moved failed");
520 EM_DEBUG_EXCEPTION("Mail MOVE failed ");
523 else if (!mail_expunge_full(stream, uid_range_node->uid_range, EX_UID)) {
524 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be expunged after move. failed!");
525 EM_DEBUG_EXCEPTION("Mail Expunge after move failed ");
529 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
532 uid_range_node = uid_range_node->next;
535 emcore_free_uid_range_set(&uid_range_set);
537 EM_SAFE_FREE(id_set);
544 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
552 emcore_close_mailbox(0, stream);
555 #ifdef __FEATURE_LOCAL_ACTIVITY__
556 if (ret || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
557 emstorage_activity_tbl_t new_activity;
558 for (i = 0; i<num ; i++) {
559 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
560 new_activity.activity_type = ACTIVITY_MOVEMAIL;
561 new_activity.account_id = account_id;
562 new_activity.mail_id = mail_ids[i];
563 new_activity.src_mbox = src_mailbox;
564 new_activity.dest_mbox = dest_mailbox;
566 if (!emcore_delete_activity(&new_activity, &err_code)) {
567 EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
574 emstorage_free_mailbox(&dest_mailbox, 1, &err_code);
577 if (error_code != NULL) {
578 *error_code = err_code;
584 int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_server, int *err_code)
586 EM_DEBUG_FUNC_BEGIN();
589 int err = EMAIL_ERROR_NONE;
590 IMAPLOCAL *imaplocal = NULL;
592 MAILSTREAM *stream = NULL;
593 char tag[MAX_TAG_SIZE];
594 char cmd[MAX_IMAP_COMMAND_LENGTH];
595 email_id_set_t *id_set = NULL;
596 int id_set_count = 0;
598 email_uid_range_set *uid_range_set = NULL;
599 int len_of_each_range = 0;
600 email_uid_range_set *uid_range_node = NULL;
601 char **string_list = NULL;
602 int string_count = 0;
603 int delete_success = false;
604 emstorage_mail_tbl_t *mail_tbl_data = NULL;
606 if (num <= 0 || !mail_ids) {
607 EM_DEBUG_EXCEPTION(" Invalid parameter ");
608 err = EMAIL_ERROR_INVALID_PARAM;
612 for(i = 0; i < num; i++) {
613 if (!emstorage_get_downloaded_mail(mail_ids[i], &mail_tbl_data, false, &err) || !mail_tbl_data) {
614 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
616 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
624 if (!emcore_connect_to_remote_mailbox(mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
625 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
629 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
630 /* Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids */
631 /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid */
632 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
634 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
635 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
639 /* Now execute one by one each comma separated string of mail_ids */
641 for (i = 0; i < string_count; ++i) {
642 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
644 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
645 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
649 /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
651 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
653 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
654 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
658 uid_range_node = uid_range_set;
660 while (uid_range_node != NULL) {
661 /* Remove comma from end of uid_range */
663 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
665 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
666 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
668 err = EMAIL_ERROR_CONNECTION_BROKEN;
672 memset(tag, 0x00, sizeof(tag));
673 memset(cmd, 0x00, sizeof(cmd));
675 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
676 SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
678 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
681 /* send command : set deleted flag */
682 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
683 EM_DEBUG_EXCEPTION("net_sout failed...");
685 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN */
690 while (imaplocal->netstream) {
691 /* receive response */
692 if (!(p = net_getline(imaplocal->netstream))) {
693 EM_DEBUG_EXCEPTION("net_getline failed...");
695 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
700 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
702 /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
703 /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl
704 if (strstr(p, "FETCH") != NULL) {
705 EM_DEBUG_LOG(" FETCH Response recieved ");
706 delete_success = true;
713 if (!strncmp(p, tag, strlen(tag))) {
714 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
715 /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
716 EM_DEBUG_LOG(" OK Response recieved ");
717 delete_success = true;
723 delete_success = false;
724 err = EMAIL_ERROR_IMAP4_STORE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
732 memset(tag, 0x00, sizeof(tag));
733 memset(cmd, 0x00, sizeof(cmd));
735 EM_DEBUG_LOG("Calling Expunge");
737 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
738 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
740 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
742 /* send command : EXPUNGE */
743 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
744 EM_DEBUG_EXCEPTION("net_sout failed...");
746 err = EMAIL_ERROR_CONNECTION_BROKEN;
750 while (imaplocal->netstream) {
751 /* receive response */
752 if (!(p = net_getline(imaplocal->netstream))) {
753 EM_DEBUG_EXCEPTION("net_getline failed...");
755 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
759 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
761 if (!strncmp(p, tag, strlen(tag))) {
762 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
763 #ifdef __FEATURE_LOCAL_ACTIVITY__
765 emstorage_mail_tbl_t **mail = NULL;
767 mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
769 EM_DEBUG_EXCEPTION("em_malloc failed");
770 err = EMAIL_ERROR_UNKNOWN;
774 if (delete_success) {
775 for (index = 0 ; index < num; index++) {
776 if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err)) {
777 EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
779 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
780 EM_DEBUG_LOG("EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER : ");
786 /* Clear entry from mail_read_mail_uid_tbl */
787 if (mail[index]->server_mail_id != NULL) {
788 if (!emstorage_remove_downloaded_mail(mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err)) {
789 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
793 /* Delete local activity */
794 emstorage_activity_tbl_t new_activity;
795 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
796 if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
797 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
798 EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
801 new_activity.activity_type = ACTIVITY_DELETEMAIL;
804 new_activity.mail_id = mail[index]->mail_id;
805 new_activity.server_mailid = NULL;
806 new_activity.src_mbox = NULL;
807 new_activity.dest_mbox = NULL;
809 if (!emcore_delete_activity(&new_activity, &err)) {
810 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
814 /* Fix for crash seen while deleting Outbox mails which are moved to Trash. Outbox mails do not have server mail id and are not updated in mail_read_mail_uid_tbl.
815 * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted.
817 /* Delete local activity */
818 emstorage_activity_tbl_t new_activity;
819 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
820 new_activity.activity_type = ACTIVITY_DELETEMAIL;
821 new_activity.mail_id = mail_ids[index]; /* valid mail id passed for outbox mails*/
822 new_activity.server_mailid = NULL;
823 new_activity.src_mbox = NULL;
824 new_activity.dest_mbox = NULL;
826 if (!emcore_delete_activity(&new_activity, &err)) {
827 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
832 for (index = 0; index < num; index++) {
833 if (!emstorage_free_mail(&mail[index], 1, &err)) {
834 EM_DEBUG_EXCEPTION(" emstorage_free_mail [%d]", err);
845 else { /* 'NO' or 'BAD' */
846 err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
853 uid_range_node = uid_range_node->next;
856 emcore_free_uid_range_set(&uid_range_set);
858 EM_SAFE_FREE(id_set);
868 emcore_free_comma_separated_strings(&string_list, &string_count);
871 emcore_free_uid_range_set(&uid_range_set);
888 static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
890 EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
893 int err = EMAIL_ERROR_NONE;
895 IMAPLOCAL *imaplocal = NULL;
896 char tag[16], cmd[64];
899 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
900 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
902 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN */
906 memset(tag, 0x00, sizeof(tag));
907 memset(cmd, 0x00, sizeof(cmd));
909 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
910 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
912 #ifdef FEATURE_CORE_DEBUG
913 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
916 /* send command : delete flaged mail */
917 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
918 EM_DEBUG_EXCEPTION("net_sout failed...");
920 err = EMAIL_ERROR_CONNECTION_BROKEN;
924 while (imaplocal->netstream) {
925 /* receive response */
926 if (!(p = net_getline(imaplocal->netstream))) {
927 EM_DEBUG_EXCEPTION("net_getline failed...");
929 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
933 #ifdef FEATURE_CORE_DEBUG
934 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
937 if (!strncmp(p, tag, strlen(tag))) {
938 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
942 else { /* 'NO' or 'BAD' */
943 err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
962 int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
964 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
967 int err = EMAIL_ERROR_NONE;
969 if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
970 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
974 if (err_code != NULL)
980 INTERNAL_FUNC int emcore_get_mail_display_name(CTSvalue *contact_name_value, char **contact_display_name)
982 EM_DEBUG_FUNC_BEGIN("contact_name_value[%p], contact_display_name[%p]", contact_name_value, contact_display_name);
983 char *display = NULL;
984 const char *first = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_FIRST_STR);
985 const char *last = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_LAST_STR);
987 EM_DEBUG_LOG(">>>>>> first[%s] last[%s]", first, last);
988 if (first != NULL && last != NULL) {
989 /* if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_name_order()) */
990 if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_order(CTS_ORDER_OF_DISPLAY))
991 display = g_strconcat(first, " ", last, NULL);
993 display = g_strconcat(last, " ", first, NULL);
996 else if (first != NULL || last != NULL) {
998 display = (char *)EM_SAFE_STRDUP(first);
1000 display = (char *)EM_SAFE_STRDUP(last);
1003 display = g_strdup(contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_FIRST_STR));
1005 if (contact_display_name != NULL)
1006 *contact_display_name = display;
1011 int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
1013 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
1016 int err = EMAIL_ERROR_NONE;
1017 ADDRESS *addr = NULL;
1018 char *address = NULL;
1019 char *temp_emailaddr = NULL;
1020 int start_text_ascii = 2;
1021 int end_text_ascii = 3;
1023 int is_searched = false;
1024 int address_length = 0;
1025 char *email_address = NULL;
1026 int contact_name_len = 0;
1027 char temp_string[1024] = { 0 , };
1029 CTSstruct *contact = NULL;
1030 CTSvalue *contact_name_value = NULL;
1031 int contact_index = -1;
1032 char *contact_display_name = NULL;
1033 char *contact_display_name_from_contact_info = NULL;
1034 int contact_display_name_len = 0;
1036 int contact_name_buffer_size = 0;
1037 char *contact_name = NULL;
1039 if (!contact_info) {
1040 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1041 err = EMAIL_ERROR_INVALID_PARAM;
1045 if (!full_address) {
1048 temp_emailaddr = NULL;
1051 address_length = 2 * strlen(full_address);
1052 temp_emailaddr = (char *)calloc(1, address_length);
1055 em_skip_whitespace(full_address , &address);
1056 EM_DEBUG_LOG("address[address][%s]", address);
1060 gchar **tokens = g_strsplit(address, ", ", -1);
1061 char *p = g_strjoinv("%2C", tokens);
1066 while (p && p[i] != '\0')
1072 EM_DEBUG_LOG(" 2 converted address %s ", p);
1074 rfc822_parse_adrlist(&addr, p, NULL);
1077 EM_DEBUG_LOG(" 3 full_address %s ", full_address);
1080 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1081 err = EMAIL_ERROR_INVALID_ADDRESS;
1085 contact_name_buffer_size = address_length;
1086 contact_name = (char*)em_malloc(contact_name_buffer_size);
1088 if (!contact_name) {
1089 EM_DEBUG_EXCEPTION("Memory allocation error!");
1090 err = EMAIL_ERROR_OUT_OF_MEMORY;
1094 while (addr != NULL) {
1095 if (addr->mailbox && addr->host) {
1096 if (!strncmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS")) || !strncmp(addr->mailbox , "INVALID_ADDRESS", strlen("INVALID_ADDRESS")) || !strncmp(addr->host , ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR.")))
1098 EM_DEBUG_LOG("Invalid address ");
1104 EM_DEBUG_LOG("Error in parsing..! ");
1109 EM_SAFE_FREE(email_address);
1110 email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1112 EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
1113 EM_DEBUG_LOG(" email_address[%s]", email_address);
1115 is_searched = false;
1116 EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
1118 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1119 if (err > CTS_SUCCESS) {
1120 contact_index = err;
1121 if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1122 /* get contact name */
1123 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) { /* set contact display name name */
1124 contact_info->contact_id = contact_index; /* NOTE : This is valid only if there is only one address. */
1125 emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1127 contact_display_name = contact_display_name_from_contact_info;
1130 EM_DEBUG_LOG(">>> contact_index[%d]", contact_index);
1131 EM_DEBUG_LOG(">>> contact_name[%s]", contact_display_name);
1133 /* Make display name string */
1134 if (contact_display_name != NULL) {
1137 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1138 contact_display_name_len = strlen(contact_display_name);
1139 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1140 char *temp = contact_name;
1141 contact_name_buffer_size += contact_name_buffer_size;
1142 contact_name = (char *)calloc(1, contact_name_buffer_size);
1143 if (contact_name == NULL) {
1144 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1148 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1152 /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1153 if (addr->next == NULL) {
1154 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1157 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1160 contact_display_name_len = strlen(temp_string);
1161 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1162 char *temp = contact_name;
1163 contact_name_buffer_size += contact_name_buffer_size;
1164 contact_name = (char *)calloc(1, contact_name_buffer_size);
1165 if (contact_name == NULL) {
1166 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1168 err = EMAIL_ERROR_OUT_OF_MEMORY;
1171 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1174 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1175 contact_name_len += contact_display_name_len;
1176 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1181 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1185 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1189 EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is 203) or error [%d]", err);
1192 /* if contact doesn't exist, use alias or email address as display name */
1193 if (addr->personal != NULL) {
1195 tokens = g_strsplit(addr->personal, "%2C", -1);
1197 EM_SAFE_FREE(addr->personal);
1199 addr->personal = g_strjoinv(", ", tokens);
1202 /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
1203 alias = addr->personal;
1206 /* alias = addr->mailbox ? addr->mailbox : ""; */
1207 alias = email_address;
1209 contact_info->alias = EM_SAFE_STRDUP(alias);
1212 contact_display_name = alias;
1213 contact_info->contact_id = -1; /* NOTE : This is valid only if there is only one address. */
1214 contact_info->storage_type = -1;
1216 /* Make display name string */
1217 EM_DEBUG_LOG("contact_display_name : [%s]", contact_display_name);
1218 EM_DEBUG_LOG("email_address : [%s]", email_address);
1220 /* if mail_id is 0, return only contact info without saving contact info to DB */
1221 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1222 /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1223 if (addr->next == NULL) {
1224 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1227 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1229 EM_DEBUG_LOG("temp_string[%s]", temp_string);
1231 contact_display_name_len = strlen(temp_string);
1232 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1233 char *temp = contact_name;
1234 contact_name_buffer_size += contact_name_buffer_size;
1235 contact_name = (char *)calloc(1, contact_name_buffer_size);
1236 if (contact_name == NULL) {
1237 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1239 err = EMAIL_ERROR_OUT_OF_MEMORY;
1242 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1246 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1247 contact_name_len += contact_display_name_len;
1248 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1252 if (temp_emailaddr && email_address) {
1253 if (mail_id == 0) { /* if mail_id is 0, return only contact info without saving contact info to DB */
1254 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1255 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1256 if (addr->next != NULL)
1257 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1258 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1260 else { /* save only the first address information - 09-SEP-2010 */
1261 if (is_saved == 0) {
1263 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1264 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1266 if (addr->next != NULL)
1267 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1269 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1274 if (contact != NULL) {
1275 contacts_svc_struct_free(contact);
1278 EM_SAFE_FREE(contact_display_name_from_contact_info);
1281 } /* while (addr != NULL) */
1283 if (temp_emailaddr) {
1284 EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
1285 contact_info->email_address = temp_emailaddr;
1286 temp_emailaddr = NULL;
1289 if (contact_name != NULL) {
1290 contact_info->contact_name = g_strdup(contact_name);
1293 contact_info->contact_name = g_strdup_printf("%c%d%c%s%c", start_text_ascii, 0, start_text_ascii, full_address, end_text_ascii);
1294 contact_info->contact_id = -1;
1301 if (contact != NULL)
1302 contacts_svc_struct_free(contact);
1303 EM_SAFE_FREE(email_address);
1304 EM_SAFE_FREE(address);
1305 EM_SAFE_FREE(temp_emailaddr);
1306 EM_SAFE_FREE(contact_name);
1307 EM_SAFE_FREE(contact_display_name_from_contact_info);
1309 if (err_code != NULL)
1315 int emcore_free_contact_info(email_mail_contact_info_t *contact_info, int *err_code)
1317 EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
1320 int err = EMAIL_ERROR_NONE;
1322 if (!contact_info) {
1323 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1324 err = EMAIL_ERROR_INVALID_PARAM;
1328 EM_SAFE_FREE(contact_info->contact_name);
1329 EM_SAFE_FREE(contact_info->email_address);
1330 EM_SAFE_FREE(contact_info->alias);
1332 contact_info->storage_type = -1;
1333 contact_info->contact_id = -1;
1338 if (err_code != NULL)
1340 EM_DEBUG_FUNC_END();
1344 int emcore_sync_contact_info(int mail_id, int *err_code)
1346 EM_PROFILE_BEGIN(emCoreMailContactSync);
1347 EM_DEBUG_FUNC_BEGIN();
1350 int err = EMAIL_ERROR_NONE;
1352 emstorage_mail_tbl_t *mail = NULL;
1354 email_mail_contact_info_t contact_info_from;
1355 email_mail_contact_info_t contact_info_to;
1356 email_mail_contact_info_t contact_info_cc;
1357 email_mail_contact_info_t contact_info_bcc;
1359 EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
1361 memset(&contact_info_from, 0x00, sizeof(email_mail_contact_info_t));
1362 memset(&contact_info_to, 0x00, sizeof(email_mail_contact_info_t));
1363 memset(&contact_info_cc, 0x00, sizeof(email_mail_contact_info_t));
1364 memset(&contact_info_bcc, 0x00, sizeof(email_mail_contact_info_t));
1366 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1367 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1372 if (mail->full_address_from != NULL) {
1373 if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err)) {
1374 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1378 if (mail->full_address_to != NULL) {
1379 if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err)) {
1380 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1384 if (mail->full_address_cc != NULL) {
1385 if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err)) {
1386 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1390 if (mail->full_address_bcc != NULL) {
1391 if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err)) {
1392 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1396 EM_SAFE_FREE(mail->email_address_sender);
1397 mail->email_address_sender = contact_info_from.email_address;
1398 contact_info_from.contact_name = NULL;
1399 contact_info_from.email_address = NULL;
1400 EM_SAFE_FREE(mail->email_address_recipient);
1401 if (mail->full_address_to != NULL) {
1402 mail->email_address_recipient = contact_info_to.email_address;
1403 contact_info_to.contact_name = NULL;
1404 contact_info_to.email_address = NULL;
1406 else if (mail->full_address_cc != NULL) {
1407 mail->email_address_recipient = contact_info_cc.email_address;
1408 contact_info_cc.contact_name = NULL;
1409 contact_info_cc.email_address = NULL;
1411 else if (mail->full_address_bcc != NULL) {
1412 mail->email_address_recipient = contact_info_bcc.email_address;
1413 contact_info_bcc.contact_name = NULL;
1414 contact_info_bcc.email_address = NULL;
1418 if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err)) {
1419 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1428 emstorage_free_mail(&mail, 1, NULL);
1430 emcore_free_contact_info(&contact_info_from, NULL);
1431 emcore_free_contact_info(&contact_info_to, NULL);
1432 emcore_free_contact_info(&contact_info_cc, NULL);
1433 emcore_free_contact_info(&contact_info_bcc, NULL);
1435 if (err_code != NULL)
1438 EM_PROFILE_END(emCoreMailContactSync);
1442 /* 1. parsing : alias and address */
1443 /* 2. sync with contact */
1444 /* 3. make glist of address info */
1445 static int emcore_sync_address_info(email_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
1447 EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
1450 int error = EMAIL_ERROR_NONE;
1451 int contact_index = -1;
1452 int is_search = false;
1454 char *address = NULL;
1455 char *contact_display_name_from_contact_info = NULL;
1456 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1457 email_address_info_t *p_address_info = NULL;
1458 ADDRESS *addr = NULL;
1459 CTSstruct *contact = NULL;
1460 CTSvalue *contact_name_value = NULL;
1462 if (address_info_list == NULL) {
1463 EM_DEBUG_EXCEPTION("Invalid param : address_info_list is NULL");
1464 error = EMAIL_ERROR_INVALID_PARAM;
1469 address = EM_SAFE_STRDUP(full_address);
1472 gchar **tokens = g_strsplit(address, ", ", -1);
1473 char *p = g_strjoinv("%2C", tokens);
1479 while (p && p[i] != '\0') {
1485 rfc822_parse_adrlist(&addr, p, NULL);
1490 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1491 error = EMAIL_ERROR_INVALID_PARAM;
1495 /* Get a contact name */
1496 while (addr != NULL) {
1497 if (addr->mailbox && addr->host) {
1498 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR."))
1500 EM_DEBUG_LOG("Invalid address ");
1506 EM_DEBUG_LOG("Error in parsing..! ");
1511 /* set display name */
1512 /* 1) contact name */
1513 /* 2) alias (if a alias in an original mail doesn't exist, this field is set with email address */
1514 /* 3) email address */
1516 if (!(p_address_info = (email_address_info_t *)malloc(sizeof(email_address_info_t)))) {
1517 EM_DEBUG_EXCEPTION("malloc failed...");
1518 error = EMAIL_ERROR_OUT_OF_MEMORY;
1521 memset(p_address_info, 0x00, sizeof(email_address_info_t));
1523 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1525 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1529 error = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1530 if (error > CTS_SUCCESS) {
1531 contact_index = error;
1532 if ((error = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1533 /* get contact name */
1534 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) { /* set contact display name name */
1535 emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1536 EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1537 EM_DEBUG_LOG(">>> contact display name[%s]", contact_display_name_from_contact_info);
1542 EM_DEBUG_EXCEPTION("contacts_svc_struct_get_value error[%d]", error);
1545 EM_DEBUG_EXCEPTION("contacts_svc_get_contact error [%d]", error);
1548 EM_DEBUG_EXCEPTION("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", error);
1550 if (is_search == true) {
1551 p_address_info->contact_id = contact_index;
1552 p_address_info->storage_type = -1;
1553 p_address_info->display_name = contact_display_name_from_contact_info;
1554 EM_DEBUG_LOG("display_name from contact[%s]", p_address_info->display_name);
1557 /* if contact doesn't exist, use alias or email address as display name */
1558 if (addr->personal != NULL) {
1560 tokens = g_strsplit(addr->personal, "%2C", -1);
1562 EM_SAFE_FREE(addr->personal);
1564 addr->personal = g_strjoinv(", ", tokens);
1567 alias = addr->personal;
1572 p_address_info->contact_id = -1;
1573 p_address_info->storage_type = -1;
1574 /* Use an alias or an email address as a display name */
1576 p_address_info->display_name = EM_SAFE_STRDUP(email_address);
1578 p_address_info->display_name = EM_SAFE_STRDUP(alias);
1580 EM_DEBUG_LOG("display_name from email [%s]", p_address_info->display_name);
1583 p_address_info->address = EM_SAFE_STRDUP(email_address);
1584 p_address_info->address_type = address_type;
1586 EM_DEBUG_LOG("email address[%s]", p_address_info->address);
1588 *address_info_list = g_list_append(*address_info_list, p_address_info);
1589 p_address_info = NULL;
1591 EM_DEBUG_LOG("after append");
1594 if (contact != NULL) {
1595 contacts_svc_struct_free(contact);
1599 EM_DEBUG_LOG("next address[%p]", addr->next);
1608 if (contact != NULL)
1609 contacts_svc_struct_free(contact);
1611 EM_SAFE_FREE(address);
1613 if (err_code != NULL)
1615 EM_DEBUG_FUNC_END();
1619 static gint address_compare(gconstpointer a, gconstpointer b)
1621 EM_DEBUG_FUNC_BEGIN();
1622 email_sender_list_t *recipients_list1 = (email_sender_list_t *)a;
1623 email_sender_list_t *recipients_list2 = (email_sender_list_t *)b;
1625 EM_DEBUG_FUNC_END();
1626 return strcmp(recipients_list1->address, recipients_list2->address);
1629 INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
1631 EM_DEBUG_FUNC_BEGIN();
1633 int i = 0, err = EMAIL_ERROR_NONE;
1634 int contact_index = -1;
1635 int is_search = false;
1636 char *address = NULL;
1637 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1638 char *display_name = NULL;
1640 ADDRESS *addr = NULL;
1641 CTSstruct *contact = NULL;
1642 CTSvalue *contact_name_value = NULL;
1643 GList *new_recipients_list = old_recipients_list;
1644 GList *recipients_list;
1646 email_sender_list_t *temp_recipients_list = NULL;
1647 email_sender_list_t *old_recipients_list_t = NULL;
1649 if (full_address == NULL || strlen(full_address) == 0) {
1650 EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
1651 err = EMAIL_ERROR_INVALID_PARAM;
1655 address = EM_SAFE_STRDUP(full_address);
1657 gchar **tokens = g_strsplit(address, ", ", -1);
1658 char *p = g_strjoinv("%2C", tokens);
1662 while (p && p[i] != '\0') {
1668 rfc822_parse_adrlist(&addr, p, NULL);
1673 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1674 err = EMAIL_ERROR_INVALID_PARAM;
1678 while (addr != NULL) {
1679 if (addr->mailbox && addr->host) {
1680 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1681 EM_DEBUG_LOG("Invalid address ");
1686 EM_DEBUG_LOG("Error in parsing..! ");
1691 temp_recipients_list = g_new0(email_sender_list_t, 1);
1693 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1695 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1697 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1698 if (err > CTS_SUCCESS) {
1699 contact_index = err;
1700 if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1701 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) {
1702 emcore_get_mail_display_name(contact_name_value, &display_name);
1703 EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1704 EM_DEBUG_LOG(">>> contact display name[%s]", display_name);
1708 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1711 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1714 EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", err);
1718 temp_recipients_list->display_name = display_name;
1719 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1721 if (addr->personal != NULL) {
1722 tokens = g_strsplit(addr->personal, "%2C", -1);
1723 EM_SAFE_FREE(addr->personal);
1724 addr->personal = g_strjoinv(", ", tokens);
1726 alias = addr->personal;
1732 temp_recipients_list->display_name = EM_SAFE_STRDUP(email_address);
1734 temp_recipients_list->display_name = EM_SAFE_STRDUP(alias);
1736 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1739 temp_recipients_list->address = EM_SAFE_STRDUP(email_address);
1740 EM_DEBUG_LOG("email address[%s]", temp_recipients_list->address);
1742 EM_SAFE_FREE(display_name);
1743 if (contact != NULL) {
1744 contacts_svc_struct_free(contact);
1747 EM_DEBUG_LOG("next address[%p]", addr->next);
1749 recipients_list = g_list_first(new_recipients_list);
1750 while (recipients_list != NULL) {
1751 old_recipients_list_t = (email_sender_list_t *)recipients_list->data;
1752 if (!strcmp(old_recipients_list_t->address, temp_recipients_list->address)) {
1753 old_recipients_list_t->total_count = old_recipients_list_t->total_count + 1;
1754 if (temp_recipients_list != NULL)
1755 g_free(temp_recipients_list);
1759 recipients_list = g_list_next(recipients_list);
1762 new_recipients_list = g_list_insert_sorted(new_recipients_list, temp_recipients_list, address_compare);
1764 temp_recipients_list = NULL;
1767 if (contact != NULL) {
1768 contacts_svc_struct_free(contact);
1776 if (contact != NULL)
1777 contacts_svc_struct_free(contact);
1779 EM_SAFE_FREE(address);
1781 if (err_code != NULL)
1784 EM_DEBUG_FUNC_END();
1785 return new_recipients_list;
1788 INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_info_list_t **address_info_list, int *err_code)
1790 EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1792 int ret = false, err = EMAIL_ERROR_NONE;
1796 emstorage_mail_tbl_t *mail = NULL;
1797 email_address_info_list_t *p_address_info_list = NULL;
1799 if (mail_id <= 0 || !address_info_list) {
1800 EM_DEBUG_EXCEPTION("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1801 err = EMAIL_ERROR_INVALID_PARAM;
1805 /* get mail from mail table */
1806 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1807 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1813 if (!(p_address_info_list = (email_address_info_list_t *)malloc(sizeof(email_address_info_list_t)))) {
1814 EM_DEBUG_EXCEPTION("malloc failed...");
1815 err = EMAIL_ERROR_OUT_OF_MEMORY;
1818 memset(p_address_info_list, 0x00, sizeof(email_address_info_list_t));
1820 if ((contact_error = contacts_svc_connect()) == CTS_SUCCESS) {
1821 EM_DEBUG_LOG("Open Contact Service Success");
1824 EM_DEBUG_EXCEPTION("contact_db_service_connect failed [%d]", contact_error);
1825 err = EMAIL_ERROR_DB_FAILURE;
1829 if (mail->full_address_from && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
1831 if (mail->full_address_to && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
1833 if (mail->full_address_cc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
1835 if (mail->full_address_bcc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
1838 if ((contact_error = contacts_svc_disconnect()) == CTS_SUCCESS)
1839 EM_DEBUG_LOG("Close Contact Service Success");
1841 EM_DEBUG_EXCEPTION("Close Contact Service Fail [%d]", contact_error);
1843 if (failed == false)
1848 *address_info_list = p_address_info_list;
1849 else if (p_address_info_list != NULL)
1850 emstorage_free_address_info_list(&p_address_info_list);
1853 emstorage_free_mail(&mail, 1, NULL);
1855 if (err_code != NULL)
1858 EM_DEBUG_FUNC_END();
1865 * input_mail_id : [in] mail id
1866 * output_mail_data : [out] double pointer to hold mail data.
1868 * succeed : EMAIL_ERROR_NONE
1871 INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
1873 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
1875 int error = EMAIL_ERROR_NONE;
1876 int result_mail_count = 0;
1877 char conditional_clause_string[QUERY_SIZE] = { 0, };
1878 emstorage_mail_tbl_t *result_mail_tbl = NULL;
1880 if (input_mail_id == 0 || !output_mail_data) {
1881 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1882 error = EMAIL_ERROR_INVALID_PARAM;
1886 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
1888 if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
1889 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
1893 if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, 1, output_mail_data, &error)) {
1894 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
1899 if (result_mail_tbl)
1900 emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
1902 EM_DEBUG_FUNC_END("error [%d]", error);
1907 /* internal function */
1908 void emcore_free_body_sharep(void **p)
1910 EM_DEBUG_FUNC_BEGIN();
1912 EM_DEBUG_FUNC_END();
1915 int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
1917 EM_DEBUG_FUNC_BEGIN();
1919 #ifdef __FEATURE_DRM__
1920 drm_bool_type_e drm_file = DRM_UNKNOWN;
1921 drm_file_info_s drm_file_info;
1923 if (input_attachment_tb_data == NULL)
1926 ret = drm_is_drm_file(input_attachment_tb_data->attachment_path, &drm_file);
1928 if (ret == DRM_RETURN_SUCCESS && drm_file == DRM_TRUE) {
1929 if (drm_get_file_info (input_attachment_tb_data->attachment_path, &drm_file_info) == DRM_RETURN_SUCCESS) {
1930 input_attachment_tb_data->attachment_drm_type = 0;
1931 EM_DEBUG_LOG ("fileInfo is [%d]", drm_file_info.oma_info.method);
1932 if (drm_file_info.oma_info.method != DRM_METHOD_TYPE_UNDEFINED) {
1933 input_attachment_tb_data->attachment_drm_type = drm_file_info.oma_info.method;
1939 EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
1940 input_attachment_tb_data->attachment_drm_type = 0;
1944 EM_DEBUG_FUNC_END();
1950 * get mail attachment from local mailbox
1952 * mailbox : server mailbox
1953 * mail_id : mai id to own attachment
1954 * attachment : the number string to be downloaded
1955 * callback : function callback. if NULL, ignored.
1960 INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment_data_t **attachment, int *err_code)
1962 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
1964 if (attachment == NULL || attachment_id == 0) {
1965 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
1966 if (err_code != NULL)
1967 *err_code = EMAIL_ERROR_INVALID_PARAM;
1972 int err = EMAIL_ERROR_NONE;
1973 emstorage_attachment_tbl_t *attachment_tbl = NULL;
1975 /* get attachment from attachment tbl */
1976 if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &err) || !attachment_tbl) {
1977 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
1982 *attachment = em_malloc(sizeof(email_attachment_data_t));
1984 EM_DEBUG_EXCEPTION("malloc failed...");
1985 err = EMAIL_ERROR_OUT_OF_MEMORY;
1989 (*attachment)->attachment_id = attachment_id;
1990 (*attachment)->attachment_name = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
1991 (*attachment)->attachment_size = attachment_tbl->attachment_size;
1992 (*attachment)->save_status = attachment_tbl->attachment_save_status;
1993 (*attachment)->attachment_path = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
1994 (*attachment)->drm_status = attachment_tbl->attachment_drm_type;
1995 (*attachment)->inline_content_status = attachment_tbl->attachment_inline_content_status;
2001 emstorage_free_attachment(&attachment_tbl, 1, NULL);
2010 * get mail attachment
2012 * input_mail_id : mail id to own attachment
2013 * output_attachment_data : result attahchment data
2014 * output_attachment_count : result attahchment count
2016 * succeed : EMAIL_ERROR_NONE
2019 INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
2021 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_attachment_data[%p], output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
2023 if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL) {
2024 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2025 return EMAIL_ERROR_INVALID_PARAM;
2029 int err = EMAIL_ERROR_NONE;
2030 int attachment_tbl_count = 0;
2031 emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2032 email_attachment_data_t *temp_attachment_data = NULL;
2034 /* get attachment from attachment tbl */
2035 if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE ){
2036 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2041 if (attachment_tbl_count) {
2042 EM_DEBUG_LOG("attchment count %d", attachment_tbl_count);
2044 *output_attachment_data = em_malloc(sizeof(email_attachment_data_t) * attachment_tbl_count);
2046 if(*output_attachment_data == NULL) {
2047 EM_DEBUG_EXCEPTION("em_malloc failed");
2048 err = EMAIL_ERROR_OUT_OF_MEMORY;
2052 for (i = 0; i < attachment_tbl_count; i++) {
2053 temp_attachment_data = (*output_attachment_data) + i;
2055 temp_attachment_data->attachment_id = attachment_tbl_list[i].attachment_id;
2056 temp_attachment_data->attachment_name = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
2057 temp_attachment_data->attachment_path = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
2058 temp_attachment_data->attachment_size = attachment_tbl_list[i].attachment_size;
2059 temp_attachment_data->mail_id = attachment_tbl_list[i].mail_id;
2060 temp_attachment_data->account_id = attachment_tbl_list[i].account_id;
2061 temp_attachment_data->mailbox_id = attachment_tbl_list[i].mailbox_id;
2062 temp_attachment_data->save_status = attachment_tbl_list[i].attachment_save_status;
2063 temp_attachment_data->drm_status = attachment_tbl_list[i].attachment_drm_type;
2064 temp_attachment_data->inline_content_status = attachment_tbl_list[i].attachment_inline_content_status;
2070 *output_attachment_count = attachment_tbl_count;
2072 if (attachment_tbl_list)
2073 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2079 INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nth, int *err_code)
2081 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2083 int err = EMAIL_ERROR_NONE;
2085 if (mail_id < 1 || !nth) {
2086 EM_DEBUG_EXCEPTION("mail_id[%d], nth[%d]", mail_id, nth);
2087 err = EMAIL_ERROR_INVALID_PARAM;
2089 if (err_code != NULL)
2092 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, nth, err);
2097 int status = EMAIL_DOWNLOAD_FAIL;
2098 MAILSTREAM *stream = NULL;
2100 emstorage_mail_tbl_t *mail = NULL;
2101 emstorage_attachment_tbl_t *attachment = NULL;
2102 struct attachment_info *ai = NULL;
2103 struct _m_content_info *cnt_info = NULL;
2104 void *tmp_stream = NULL;
2105 char *s_uid = NULL, buf[1024];
2107 emstorage_attachment_tbl_t *attachment_list = NULL;
2108 int current_attachment_no = 0;
2109 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2111 int server_mbox_id = 0;
2113 if (!emcore_check_thread_status()) {
2114 err = EMAIL_ERROR_CANCELLED;
2118 only_body_download = false;
2120 /* get mail from mail table. */
2121 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2122 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2127 if (!mail->server_mail_status) {
2128 EM_DEBUG_EXCEPTION("not synchronous mail...");
2129 err = EMAIL_ERROR_INVALID_MAIL;
2133 if (nth == 0) { /* download all attachments, nth starts from 1, not zero */
2134 /* get attachment list from db */
2135 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2136 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE ){
2137 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2142 else { /* download only nth attachment */
2143 attachment_count_to_be_downloaded = 1;
2144 if (!emstorage_get_attachment_nth(mail_id, nth, &attachment_list, true, &err) || !attachment_list) {
2145 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2151 if (!emcore_check_thread_status()) {
2152 err = EMAIL_ERROR_CANCELLED;
2156 account_id = mail->account_id;
2157 s_uid = EM_SAFE_STRDUP(mail->server_mail_id);
2158 server_mbox_id = mail->mailbox_id;
2160 /* open mail server. */
2161 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
2162 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2163 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2167 stream = (MAILSTREAM *)tmp_stream;
2169 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2170 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
2172 attachment = attachment_list + i;
2173 if (nth == 0) /* download all attachments, nth starts from 1, not zero */
2174 current_attachment_no = i + 1; /* attachment no */
2175 else /* download only nth attachment */
2176 current_attachment_no = nth; /* attachment no */
2178 if (!emcore_check_thread_status()) {
2179 err = EMAIL_ERROR_CANCELLED;
2183 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2184 EM_DEBUG_EXCEPTION("malloc failed...");
2185 err = EMAIL_ERROR_OUT_OF_MEMORY;
2189 cnt_info->grab_type = GRAB_TYPE_ATTACHMENT; /* attachment */
2190 cnt_info->file_no = current_attachment_no; /* attachment no */
2192 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2193 /* text/html be changed to attachment, this isn't real attachment in RFC822. */
2195 cnt_info->file_no--;
2198 /* set sparep(member of BODY) memory free function. */
2199 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2201 if (!emcore_check_thread_status()) {
2202 err = EMAIL_ERROR_CANCELLED;
2206 msg_no = atoi(s_uid);
2208 /* get body structure. */
2209 /* don't free mbody because mbody is freed in closing mail_stream. */
2210 if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0) {
2211 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2215 if (!emcore_check_thread_status()) {
2216 err = EMAIL_ERROR_CANCELLED;
2220 /* set body fetch section. */
2221 if (emcore_set_fetch_body_section(mbody, false, NULL, &err) < 0) {
2222 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2226 /* download attachment. */
2227 _imap4_received_body_size = 0;
2228 _imap4_last_notified_body_size = 0;
2229 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2230 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2232 EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
2233 if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0) {
2234 EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
2238 if (!emcore_check_thread_status()) {
2239 err = EMAIL_ERROR_CANCELLED;
2243 /* select target attachment information. */
2244 for (ai = cnt_info->file ; ai; ai = ai->next) {
2246 EM_DEBUG_LOG("[in loop] %s, %d", ai->name, cnt_info->file_no);
2248 if (--cnt_info->file_no == 0)
2252 EM_DEBUG_LOG("cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
2254 if (cnt_info->file_no == 0 && ai) {
2255 /* rename temporary file to real file. */
2256 if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err)) {
2257 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2261 if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err)) {
2262 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2266 if (!emstorage_move_file(ai->save, buf, false, &err)) {
2267 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2272 EM_SAFE_FREE(ai->save);
2274 EM_DEBUG_LOG("ai->size [%d]", ai->size);
2275 attachment->attachment_size = ai->size;
2276 attachment->attachment_path = EM_SAFE_STRDUP(buf);
2278 /* update attachment information. */
2279 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2280 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2281 /* delete created file. */
2287 #ifdef __FEATURE_DRM__
2288 if (emcore_check_drm(attachment)) { /* has drm attachment ? */
2289 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, (void *)attachment->attachment_path, NULL) == DRM_RETURN_SUCCESS)
2290 EM_DEBUG_LOG("drm_svc_register_file success");
2292 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2293 mail->DRM_status = attachment->attachment_drm_type;
2295 #endif /* __FEATURE_DRM__ */
2298 EM_DEBUG_EXCEPTION("invalid attachment sequence...");
2299 err = EMAIL_ERROR_INVALID_ATTACHMENT;
2304 emcore_free_content_info(cnt_info);
2307 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2311 emcore_close_mailbox(0, stream);
2319 emcore_close_mailbox(account_id, stream);
2320 if (attachment_list)
2321 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2323 emcore_free_content_info(cnt_info);
2325 emstorage_free_mail(&mail, 1, NULL);
2327 EM_SAFE_FREE(s_uid);
2330 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
2332 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
2334 if (err_code != NULL)
2337 EM_DEBUG_FUNC_END();
2341 #ifdef __ATTACHMENT_OPTI__
2342 INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, int nth, int *err_code)
2344 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2346 int err = EMAIL_ERROR_NONE; /* Prevent Defect - 25093 */
2348 int status = EMAIL_DOWNLOAD_FAIL;
2349 MAILSTREAM *stream = NULL;
2350 emstorage_mail_tbl_t *mail = NULL;
2351 emstorage_attachment_tbl_t *attachment = NULL;
2352 void *tmp_stream = NULL;
2353 char *s_uid = NULL, *server_mbox = NULL, buf[512];
2354 emstorage_attachment_tbl_t *attachment_list = NULL;
2355 int current_attachment_no = 0;
2356 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2358 char *savefile = NULL;
2361 #ifdef SUPPORT_EXTERNAL_MEMORY
2362 int iActualSize = 0;
2367 int bIsAdd_to_mmc = false;
2368 int is_on_mmc = false;
2369 email_file_list *pFileListMMc = NULL;
2370 email_file_list *pFileList = NULL;
2371 #endif /* SUPPORT_EXTERNAL_MEMORY */
2374 memset(buf, 0x00, 512);
2376 if (mail_id < 1 || !nth) {
2377 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
2379 err = EMAIL_ERROR_INVALID_PARAM;
2381 if (err_code != NULL)
2385 attachment_no = atoi(nth);
2387 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err); /* 090525, kwangryul.baek */
2392 if (!emcore_check_thread_status()) {
2393 err = EMAIL_ERROR_CANCELLED;
2396 only_body_download = false;
2398 attachment_no = atoi(nth);
2400 if (attachment_no == 0) {
2401 /* download all attachments, nth starts from 1, not zero */
2402 /* get attachment list from db */
2403 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2404 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE || !attachment_list){
2405 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2410 else { /* download only nth attachment */
2411 attachment_count_to_be_downloaded = 1;
2412 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2413 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2421 if (!emcore_check_thread_status()) {
2422 err = EMAIL_ERROR_CANCELLED;
2427 /* get mail from mail table. */
2428 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2429 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2435 /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /* faizan.h@samsung.com */
2437 if (!emcore_check_thread_status()) {
2438 err = EMAIL_ERROR_CANCELLED;
2442 account_id = mail->account_id;
2443 s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
2444 server_mbox = EM_SAFE_STRDUP(mail->mailbox); mail->server_mailbox_name = NULL;
2448 /* open mail server. */
2449 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2450 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2452 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2456 stream = (MAILSTREAM *)tmp_stream;
2459 if (!emcore_check_thread_status()) {
2460 err = EMAIL_ERROR_CANCELLED;
2465 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2466 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
2468 attachment = attachment_list + i;
2469 if (attachment_no == 0) {
2470 /* download all attachments, nth starts from 1, not zero */
2471 current_attachment_no = i + 1; /* attachment no */
2474 /* download only nth attachment */
2475 current_attachment_no = attachment_no; /* attachment no */
2478 if (!emcore_check_thread_status()) {
2479 err = EMAIL_ERROR_CANCELLED;
2483 _imap4_received_body_size = 0;
2484 _imap4_last_notified_body_size = 0;
2485 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2486 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2489 EM_SAFE_FREE(savefile);
2490 if (!emcore_get_temp_file_name(&savefile, &err)) {
2491 EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
2493 if (err_code != NULL)
2501 EM_DEBUG_LOG("uid [%d]", uid);
2503 if (!imap_mail_write_body_to_file(stream, account_id, mail_id, attachment_no, savefile, uid , attachment->section, attachment->encoding, &dec_len, NULL, &err)) {
2504 EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
2505 if (err_code != NULL)
2510 #ifdef SUPPORT_EXTERNAL_MEMORY
2511 iActualSize = emcore_get_actual_mail_size (cnt_info->text.plain , cnt_info->text.html, cnt_info->file , &err);
2512 if (!emstorage_mail_check_free_space(iActualSize, &bIs_full, &err)) {
2513 EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
2518 /* If external memory not present, return error */
2519 if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
2520 err = EMAIL_ERROR_MAIL_MEMORY_FULL;
2523 bIsAdd_to_mmc = true;
2525 #endif /* SUPPORT_EXTERNAL_MEMORY */
2527 if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
2528 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2532 if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
2533 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2537 if (!emstorage_move_file(savefile, buf, false, &err)) {
2538 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2543 attachment->attachment = EM_SAFE_STRDUP(buf);
2544 /* update attachment information. */
2545 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2546 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2551 #ifdef __FEATURE_DRM__
2552 if (emcore_check_drm(attachment)) {
2554 if (drm_svc_register_file(attachment->attachment) == DRM_RESULT_SUCCESS)
2555 EM_DEBUG_LOG("drm_svc_register_file success");
2557 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2558 mail->flag3 = attachment->flag2;
2560 #endif /* __FEATURE_DRM__ */
2562 if (!emcore_check_thread_status()) {
2563 err = EMAIL_ERROR_CANCELLED;
2567 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2574 EM_SAFE_FREE(savefile);
2576 emcore_close_mailbox(0, stream);
2579 if (attachment_list)
2580 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2583 emstorage_free_mail(&mail, 1, NULL);
2589 free(server_mbox);server_mbox = NULL;
2592 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2593 else if (err != EMAIL_ERROR_CANCELLED)
2594 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2596 if (err_code != NULL)
2604 INTERNAL_FUNC int emcore_download_body_multi_sections_bulk(void *mail_stream, int account_id, int mail_id, int verbose, int with_attach, int limited_size, int event_handle , int *err_code)
2606 EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d], event_handle [ %d ] ", mail_stream, account_id, mail_id, verbose, with_attach, event_handle);
2609 int err = EMAIL_ERROR_NONE;
2610 int status = EMAIL_DOWNLOAD_FAIL;
2611 int pop3_body_size = 0;
2612 int pop3_downloaded_size = 0;
2613 MAILSTREAM *stream = NULL;
2615 PARTLIST *section_list = NULL;
2616 email_internal_mailbox_t mailbox = { 0 };
2617 emstorage_mail_tbl_t *mail = NULL;
2618 emstorage_attachment_tbl_t attachment = {0, 0, NULL, };
2619 email_account_t *ref_account = NULL;
2620 struct attachment_info *ai = NULL;
2621 struct _m_content_info *cnt_info = NULL;
2622 void *tmp_stream = NULL;
2623 char *s_uid = NULL, *server_mbox = NULL, buf[512];
2624 int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
2625 int iActualSize = 0;
2626 char html_body[MAX_PATH] = {0, };
2627 emcore_uid_list *uid_list = NULL;
2628 char *mailbox_name = NULL;
2629 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2630 int html_changed = 0;
2635 EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d]", mail_stream, account_id, mail_id, verbose, with_attach);
2636 err = EMAIL_ERROR_INVALID_PARAM;
2638 if (err_code != NULL)
2641 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
2645 FINISH_OFF_IF_CANCELED;
2647 only_body_download = true;
2649 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2650 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2655 if (mail->mailbox_name)
2656 mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
2658 if (1 == mail->body_download_status) {
2659 EM_DEBUG_EXCEPTION("not synchronous mail...");
2660 err = EMAIL_ERROR_INVALID_MAIL;
2664 account_id = mail->account_id;
2665 s_uid = mail->server_mail_id;
2666 server_mbox = mail->server_mailbox_name;
2667 mail->server_mail_id = NULL;
2668 mail->server_mailbox_name = NULL;
2670 attachment.account_id = mail->account_id;
2671 attachment.mail_id = mail->mail_id;
2672 attachment.mailbox_id = mail->mailbox_id;
2673 attachment.attachment_save_status = 0;
2674 mailbox_id = mail->mailbox_id;
2675 emstorage_free_mail(&mail, 1, NULL);
2678 if (!(ref_account = emcore_get_account_reference(account_id))) {
2679 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2680 err = EMAIL_ERROR_INVALID_ACCOUNT;
2684 FINISH_OFF_IF_CANCELED;
2686 /* open mail server. */
2688 if (!emcore_connect_to_remote_mailbox(account_id, mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
2689 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2690 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2693 stream = (MAILSTREAM *)tmp_stream;
2696 stream = (MAILSTREAM *)mail_stream;
2701 FINISH_OFF_IF_CANCELED;
2703 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2704 EM_DEBUG_EXCEPTION("em_malloc failed...");
2705 err = EMAIL_ERROR_OUT_OF_MEMORY;
2709 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
2711 /* in POP3 case, both text and attachment are downloaded in this call. */
2712 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2713 attachment.attachment_save_status = 1; /* all attachments should be downloaded in the case of POP3 */
2715 mailbox.account_id = account_id;
2716 mailbox.mail_stream = stream;
2718 /* download all uids from server. */
2719 if (!emcore_download_uid_all(&mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err)) {
2720 EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
2724 /* get mesg number to be related to last download mail from uid list file */
2725 if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err)) {
2726 EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
2727 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2731 free(s_uid); s_uid = NULL;
2733 if (!emcore_check_thread_status()) {
2734 err = EMAIL_ERROR_CANCELLED;
2738 _pop3_received_body_size = 0;
2739 _pop3_total_body_size = 0;
2740 _pop3_last_notified_body_size = 0;
2741 _pop3_receiving_mail_id = mail_id;
2743 /* send read mail commnad. */
2744 if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err)) {
2745 EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
2749 _pop3_total_body_size = pop3_body_size;
2751 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
2752 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
2754 EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
2756 FINISH_OFF_IF_CANCELED;
2758 /* save message into tempfile */
2759 /* parsing mime from stream. */
2761 if (!emcore_parse_mime(stream, 0, cnt_info, &err)) {
2762 EM_DEBUG_EXCEPTION("emcore_parse_mime failed [%d]", err);
2766 FINISH_OFF_IF_CANCELED;
2768 else { /* in IMAP case, both text and attachment list are downloaded in this call. */
2769 /* This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
2770 if (with_attach > 0)
2771 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2773 cnt_info->grab_type = GRAB_TYPE_TEXT;
2775 int uid = atoi(s_uid);
2777 free(s_uid); s_uid = NULL;
2779 /* set sparep(member of BODY) memory free function */
2780 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2782 /* get body strucutre. */
2783 /* don't free mbody because mbody is freed in closing mail_stream. */
2784 if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL)) {
2785 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2786 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2790 FINISH_OFF_IF_CANCELED;
2792 if (mbody->type == TYPEMULTIPART) {
2793 EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
2794 PART *part_child = mbody->nested.part;
2797 char filename[MAX_PATH+1] = {0, };
2798 int is_attachment = 0;
2799 while (part_child) {
2800 BODY *body = &(part_child->body);
2801 if (only_body_download == true) {
2802 if (((body->id) && strlen(body->id) > 1) || (body->location))
2804 else if (body->disposition.type) { /* "attachment" or "inline" or etc... */
2805 PARAMETER *param = body->disposition.parameter;
2808 EM_DEBUG_LOG("param->attribute [%s], param->value [%s]", param->attribute, param->value);
2810 if (!strcasecmp(param->attribute, "filename")) { /* attribute is "filename" */
2811 strncpy(filename, param->value, MAX_PATH);
2812 EM_DEBUG_LOG(">>>>> FILENAME [%s] ", filename);
2815 param = param->next;
2821 /* it may be report msg */
2822 if (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')
2827 if (is_attachment == 0) {
2828 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
2829 multi_part_body_size = multi_part_body_size + body->size.bytes;
2834 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
2835 multi_part_body_size = multi_part_body_size + body->size.bytes;
2837 part_child = part_child->next;
2842 /* set body fetch section. */
2843 if (emcore_set_fetch_body_section(mbody, true, &iActualSize, &err) < 0) {
2844 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2848 EM_DEBUG_LOG("iActualSize [%d]", iActualSize);
2849 multi_part_body_size = iActualSize;
2851 _imap4_received_body_size = 0;
2852 _imap4_last_notified_body_size = 0;
2853 if (multi_part_body_size > 0) { /* download multiparts */
2854 _imap4_total_body_size = multi_part_body_size;
2855 _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
2857 else { /* download only one body part */
2858 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2859 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2862 /* save message into tempfile */
2863 /* download body text and get attachment list. */
2864 if (emcore_get_body_part_list_full(stream, uid, account_id, mail_id, mbody, cnt_info, &err, section_list, event_handle) < 0) {
2865 EM_DEBUG_EXCEPTION("emcore_get_body falied [%d]", err);
2868 FINISH_OFF_IF_CANCELED;
2872 if (false == emstorage_get_mail_by_id(mail_id, &mail, true, &err)) {
2873 EM_DEBUG_EXCEPTION(" emstorage_get_mail_by_id failed [%d]", err);
2877 if (cnt_info->text.plain) {
2878 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
2880 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
2881 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2886 if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err)) {
2887 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2891 if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err)) {
2892 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2896 mail->file_path_plain = EM_SAFE_STRDUP(buf);
2897 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
2900 if (cnt_info->text.html) {
2901 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
2902 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2906 if (cnt_info->text.plain_charset != NULL) {
2907 memcpy(html_body, cnt_info->text.plain_charset, strlen(cnt_info->text.plain_charset));
2908 strcat(html_body, HTML_EXTENSION_STRING);
2911 memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
2913 if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err)) {
2914 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2918 if (!emstorage_move_file(cnt_info->text.html, buf, false, &err)) {
2919 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2922 mail->file_path_html = EM_SAFE_STRDUP(buf);
2925 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && limited_size < pop3_body_size)
2926 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
2928 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
2930 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2931 if (html_changed) mail->flag2 = 1;
2934 FINISH_OFF_IF_CANCELED;
2936 for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++) {
2937 attachment.attachment_id = attachment_num;
2938 attachment.attachment_size = ai->size;
2939 attachment.attachment_path = ai->save;
2940 attachment.attachment_name = ai->name;
2941 attachment.attachment_drm_type = ai->drm;
2942 attachment.attachment_inline_content_status = ai->type == 1;
2943 attachment.attachment_save_status = 0;
2944 attachment.attachment_mime_type = ai->attachment_mime_type;
2945 #ifdef __ATTACHMENT_OPTI__
2946 attachment.encoding = ai->encoding;
2947 attachment.section = ai->section;
2949 EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
2950 EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
2951 EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
2952 EM_DEBUG_LOG("attachment.attachment_name[%s]", attachment.attachment_name);
2953 EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
2954 EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
2957 local_inline_content_count++;
2958 local_attachment_count++;
2961 /* in POP3 case, rename temporary file to real file. */
2962 attachment.attachment_save_status = 1;
2963 if (ai->type == 1) { /* it is inline content */
2964 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
2965 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2968 if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err)) {
2969 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2974 if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
2975 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2979 if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err)) {
2980 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2985 if (!emstorage_move_file(ai->save, buf, false, &err)) {
2986 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2988 /* delete all created files. */
2989 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
2990 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
2991 /* goto FINISH_OFF; */
2994 if (!emstorage_delete_dir(buf, NULL)) {
2995 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
2996 /* goto FINISH_OFF; */
3003 free(ai->save); ai->save = EM_SAFE_STRDUP(buf);
3005 attachment.attachment_path = ai->save;
3007 #ifdef __FEATURE_DRM__
3008 if (emcore_check_drm(&attachment)) /* is drm content ?*/ {
3009 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, attachment.attachment_path, NULL) != DRM_RETURN_SUCCESS)
3010 EM_DEBUG_EXCEPTION("drm_process_request : register file fail");
3011 mail->DRM_status = attachment.attachment_drm_type;
3013 #endif/* __FEATURE_DRM__ */
3016 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
3017 /* Information : Attachment info already saved if partial body is dowloaded. */
3019 emstorage_attachment_tbl_t *attch_info = NULL;
3020 /* Get attachment details */
3021 if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info) {
3022 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
3023 if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) { /* save only attachment file. */
3024 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3025 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3027 emstorage_free_attachment(&attch_info, 1, NULL);
3028 /* delete all created files. */
3029 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3030 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3034 if (!emstorage_delete_dir(buf, &err)) {
3035 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3039 /* ROLLBACK TRANSACTION; */
3040 emstorage_rollback_transaction(NULL, NULL, NULL);
3048 EM_DEBUG_LOG("Attachment info already exists...!");
3049 /* Update attachment size */
3050 EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
3051 attch_info->attachment_size = ai->size;
3052 if (!emstorage_update_attachment(attch_info, true, &err)) {
3053 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3060 emstorage_free_attachment(&attch_info, 1, NULL);
3066 mail->attachment_yn = 1;
3067 /* save only attachment file. */
3068 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3069 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3070 if (bIsAdd_to_mmc) {
3071 if (attachment.attachment) {
3075 /* delete all created files. */
3076 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3077 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3081 if (!emstorage_delete_dir(buf, &err)) {
3082 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3086 /* ROLLBACK TRANSACTION; */
3087 emstorage_rollback_transaction(NULL, NULL, NULL);
3092 #endif /* End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
3096 EM_DEBUG_LOG("Check #1");
3098 mail->attachment_count = local_attachment_count;
3099 mail->inline_content_count = local_inline_content_count;
3101 EM_DEBUG_LOG("Check #2");
3103 EM_DEBUG_LOG("Mailbox Name [%s]", mailbox_name);
3104 mail->mailbox_name = EM_SAFE_STRDUP(mailbox_name); /* fix for mailboox sync fail */
3106 EM_DEBUG_LOG("Check #3");
3108 /* change mail's information. */
3109 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err)) {
3110 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3111 emstorage_rollback_transaction(NULL, NULL, NULL); /* ROLLBACK TRANSACTION; */
3116 EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
3118 /* in pop3 mail case, the mail is deleted from server after being downloaded. */
3119 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3120 #ifdef DELETE_AFTER_DOWNLOADING
3123 SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
3125 if (!ref_account->keep_mails_on_pop_server_after_download) {
3126 if (!emcore_delete_mails_from_pop3_server(&mbox, delmsg, &err))
3127 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server failed [%d]", err);
3132 if (stream != NULL) {
3133 emcore_close_mailbox(0, stream);
3139 FINISH_OFF_IF_CANCELED;
3145 if (g_inline_count) {
3147 EM_SAFE_FREE(g_inline_list);
3150 multi_part_body_size = 0;
3151 _pop3_received_body_size = 0;
3152 _pop3_last_notified_body_size = 0;
3153 _pop3_total_body_size = 0;
3154 _pop3_receiving_mail_id = 0;
3156 _imap4_received_body_size = 0;
3157 _imap4_last_notified_body_size = 0;
3158 _imap4_total_body_size = 0;
3159 _imap4_download_noti_interval_value = 0;
3162 emcore_free_content_info(cnt_info);
3164 emstorage_free_mail(&mail, 1, NULL);
3165 EM_SAFE_FREE(server_mbox);
3166 EM_SAFE_FREE(s_uid);
3167 EM_SAFE_FREE(mailbox_name);
3169 multi_part_body_size = 0;
3172 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
3174 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
3176 if (err_code != NULL)
3184 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
3185 unsigned long uidvalidity, SEARCHSET *sourceset,
3188 EM_DEBUG_FUNC_BEGIN();
3189 char old_server_uid[129];
3191 EM_DEBUG_LOG("mailbox name - %s", mailbox);
3192 EM_DEBUG_LOG("first sequence number source- %ld", sourceset->first);
3193 EM_DEBUG_LOG("last sequence number last- %ld", sourceset->last);
3194 EM_DEBUG_LOG("first sequence number dest - %ld", destset->first);
3195 EM_DEBUG_LOG("last sequence number dest- %ld", sourceset->last);
3197 /* search for server _mail_id with value sourceset->first and update it with destset->first */
3198 /* faizan.h@samsung.com */
3199 memset(old_server_uid, 0x00, 129);
3200 sprintf(old_server_uid, "%ld", sourceset->first);
3201 EM_DEBUG_LOG(">>>>> old_server_uid = %s", old_server_uid);
3203 memset(g_new_server_uid, 0x00, 129);
3204 sprintf(g_new_server_uid, "%ld", destset->first);
3205 EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
3207 if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
3208 EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
3212 static int emcore_delete_mails_from_remote_server(int input_account_id, int input_mail_ids[], int input_mail_id_count, int input_delete_option)
3214 EM_DEBUG_FUNC_BEGIN("input_account_id[%d], input_mail_ids[%p], input_mail_id_count[%d], input_delete_option [%d]", input_account_id, input_mail_ids, input_mail_id_count, input_delete_option);
3216 int err = EMAIL_ERROR_NONE;
3217 email_account_t *account = NULL;
3218 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3219 int bulk_flag = false;
3222 if (!(account = emcore_get_account_reference(input_account_id))) {
3223 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_account_id);
3224 err = EMAIL_ERROR_INVALID_ACCOUNT;
3228 if (!emnetwork_check_network_status(&err)) {
3229 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
3234 FINISH_OFF_IF_CANCELED;
3236 if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
3237 if (!bulk_flag && !emcore_delete_mails_from_imap4_server(input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
3238 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_imap4_server failed [%d]", err);
3239 if (err == EMAIL_ERROR_IMAP4_STORE_FAILURE)
3240 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3246 else if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3247 if (!emcore_delete_mails_from_pop3_server(account, input_mail_ids, input_mail_id_count)) {
3248 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server falied [%d]", err);
3251 #ifdef __FEATURE_LOCAL_ACTIVITY__
3253 /* Remove local activity */
3254 emstorage_activity_tbl_t new_activity;
3255 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
3256 if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
3257 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
3258 EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
3261 new_activity.activity_type = ACTIVITY_DELETEMAIL;
3264 new_activity.mail_id = mail->mail_id;
3265 new_activity.server_mailid = mail->server_mail_id;
3266 new_activity.src_mbox = NULL;
3267 new_activity.dest_mbox = NULL;
3269 if (!emcore_delete_activity(&new_activity, &err)) {
3270 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
3273 /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
3274 if (!emstorage_remove_downloaded_mail(input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err)) {
3275 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3279 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3284 EM_DEBUG_FUNC_END("err [%d]", err);
3288 int emcore_delete_mail(int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
3290 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num[%d], from_server[%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
3293 int err = EMAIL_ERROR_NONE;
3294 email_account_t *account = NULL;
3296 if (!account_id || !mail_ids || !num) {
3297 EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
3298 err = EMAIL_ERROR_INVALID_PARAM;
3302 if (!(account = emcore_get_account_reference(account_id))) {
3303 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
3304 err = EMAIL_ERROR_INVALID_ACCOUNT;
3308 if (account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC)
3309 from_server = EMAIL_DELETE_LOCALLY;
3311 FINISH_OFF_IF_CANCELED;
3313 if (from_server == EMAIL_DELETE_LOCALLY) /* Delete mails from local storage*/ {
3314 emcore_delete_mails_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
3315 emcore_check_unread_mail();
3317 else { /* Delete mails from server*/
3318 emcore_delete_mails_from_remote_server(account_id, mail_ids, num, from_server);
3326 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0 : err);
3328 if (err_code != NULL)
3331 EM_DEBUG_FUNC_END("err [%d]", err);
3336 int emcore_delete_all_mails_of_acount(int input_account_id)
3338 EM_DEBUG_FUNC_BEGIN("input_account_id [%d]");
3340 int err = EMAIL_ERROR_NONE;
3341 char buf[512] = { 0, };
3343 /* emstorage_delete_mail_by_account is available only locally */
3344 if (!emstorage_delete_mail_by_account(input_account_id, false, &err)) {
3345 EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
3349 if (!emstorage_delete_attachment_all_on_db(input_account_id, NULL, false, &err)) {
3350 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3354 /* delete mail contents from filesystem */
3355 if (!emstorage_get_save_name(input_account_id, 0, 0, NULL, buf, &err)) {
3356 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3360 if (!emstorage_delete_dir(buf, &err)) {
3361 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3364 /* delete meeting request */
3365 if (!emstorage_delete_meeting_request(input_account_id, 0, 0, false, &err)) {
3366 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3370 emcore_check_unread_mail();
3373 EM_DEBUG_FUNC_END("err [%d]",err);
3377 int emcore_delete_all_mails_of_mailbox(int input_mailbox_id, int input_from_server, int *err_code)
3379 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_from_server [%d], err_code [%p]", input_mailbox_id, input_from_server, err_code);
3382 int err = EMAIL_ERROR_NONE;
3383 int search_handle = 0;
3384 int *mail_ids = NULL;
3387 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
3389 if (!input_mailbox_id) {
3390 err = EMAIL_ERROR_INVALID_PARAM;
3391 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3395 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
3396 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
3399 /* Delete all mails in specific mailbox */
3400 if (!emstorage_mail_search_start(NULL, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, 0, &search_handle, &total, true, &err)) {
3401 EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
3406 EM_DEBUG_LOG("emstorage_mail_search_start returns [%d]", total);
3409 mail_ids = em_malloc(sizeof(int) * total);
3410 if (mail_ids == NULL) {
3411 EM_DEBUG_EXCEPTION("em_malloc failed...");
3412 err = EMAIL_ERROR_OUT_OF_MEMORY;
3416 for (i = 0; i < total; i++) {
3417 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
3418 EM_DEBUG_EXCEPTION("emstorage_mail_search_result failed [%d]", err);
3424 if (!emcore_delete_mail(mailbox_tbl->account_id, mail_ids, total, input_from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
3425 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
3433 if (search_handle >= 0) {
3434 if (!emstorage_mail_search_end(search_handle, true, &err))
3435 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
3439 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
3441 EM_SAFE_FREE(mail_ids);
3442 if (err_code != NULL)
3444 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
3448 INTERNAL_FUNC int emcore_delete_mails_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
3450 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
3451 int ret = false, err = EMAIL_ERROR_NONE, i;
3452 emstorage_mail_tbl_t *result_mail_list;
3453 char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
3455 /* Getting mail list by using select mail_id [in] */
3456 if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
3457 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
3461 /* Deleting mails by using select mail_id [in] */
3462 if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
3463 EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
3467 /* Sending Notification */
3468 noti_param_string = em_malloc(sizeof(char) * 10 * num);
3470 if(!noti_param_string) {
3471 EM_DEBUG_EXCEPTION("em_malloc failed");
3472 err = EMAIL_ERROR_OUT_OF_MEMORY;
3476 for(i = 0; i < num; i++) {
3477 memset(mail_id_string, 0, 10);
3478 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
3479 strcat(noti_param_string, mail_id_string);
3480 /* can be optimized by appending sub string with directly pointing on string array kyuho.jo 2011-10-07 */
3483 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE, account_id, noti_param_1, noti_param_string, noti_param_2))
3484 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3486 /* Updating Thread informations */
3487 /* Thread information should be updated as soon as possible. */
3488 for(i = 0; i < num; i++) {
3489 if (result_mail_list[i].thread_item_count > 1) {
3490 if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false, &err)) {
3491 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
3497 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
3498 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3500 for(i = 0; i < num; i++) {
3501 /* Deleting attachments */
3502 if (!emstorage_delete_all_attachments_of_mail(result_mail_list[i].mail_id, false, &err)) {
3503 EM_DEBUG_EXCEPTION("emstorage_delete_all_attachments_of_mail failed [%d]", err);
3504 if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
3505 err = EMAIL_ERROR_NONE;
3508 /* Deleting Directories */
3509 /* delete mail contents from filesystem */
3510 if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
3511 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3516 if (!emstorage_delete_dir(buf, &err)) {
3517 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3521 /* Deleting Meeting Request */
3522 if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, 0, false, &err)) {
3523 EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
3524 if (err != EMAIL_ERROR_CONTACT_NOT_FOUND) {
3533 EM_SAFE_FREE(noti_param_string);
3535 if (err_code != NULL)
3538 EM_DEBUG_FUNC_END();
3542 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count)
3544 EM_DEBUG_FUNC_BEGIN("input_account[%p], input_mail_ids[%p], input_mail_id_count[%d]", input_account, input_mail_ids, input_mail_id_count);
3546 int err = EMAIL_ERROR_NONE;
3550 void *stream = NULL;
3551 email_internal_mailbox_t mailbox_data;
3552 emstorage_mail_tbl_t *mail_tbl_data = NULL;
3554 if (!input_account || !input_mail_ids) {
3555 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3556 err = EMAIL_ERROR_INVALID_PARAM;
3560 for (i = 0; i < input_mail_id_count; i++) {
3561 FINISH_OFF_IF_CANCELED;
3563 mail_id = input_mail_ids[i];
3565 if (!emstorage_get_downloaded_mail(mail_id, &mail_tbl_data, false, &err) || !mail_tbl_data) {
3566 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
3568 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
3575 if (stream == NULL) {
3576 if (!emcore_connect_to_remote_mailbox(input_account->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
3577 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
3581 mailbox_data.mail_stream = stream;
3582 mailbox_data.account_id = input_account->account_id;
3583 mailbox_data.mailbox_id = mail_tbl_data->mailbox_id;
3586 if (mailbox_data.user_data != NULL) {
3587 emcore_free_uids(mailbox_data.user_data, NULL);
3588 mailbox_data.user_data = NULL;
3591 if (!emcore_get_mail_msgno_by_uid(input_account, &mailbox_data, mail_tbl_data->server_mail_id, &msgno, &err)) {
3592 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid faild [%d]", err);
3593 if (err == EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
3594 goto NOT_FOUND_ON_SERVER;
3599 if (!pop3_mail_delete(mailbox_data.mail_stream, msgno, &err)) {
3600 EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
3602 if (err == EMAIL_ERROR_POP3_DELE_FAILURE)
3603 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3607 if (!emstorage_remove_downloaded_mail(input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
3608 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3610 NOT_FOUND_ON_SERVER :
3612 if (mail_tbl_data != NULL)
3613 emstorage_free_mail(&mail_tbl_data, 1, NULL);
3618 if (mail_tbl_data != NULL)
3619 emstorage_free_mail(&mail_tbl_data, 1, NULL);
3622 emcore_close_mailbox(0, stream);
3626 if (mailbox_data.user_data != NULL) {
3627 emcore_free_uids(mailbox_data.user_data, NULL);
3628 mailbox_data.user_data = NULL;
3631 EM_DEBUG_FUNC_END("err [%d]", err);
3635 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(email_account_t *account, email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
3637 EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], uid[%s], msgno[%p], err_code[%p]", account, mailbox, uid, msgno, err_code);
3640 int err = EMAIL_ERROR_NONE;
3642 emcore_uid_list *uid_list = NULL;
3643 emcore_uid_list *pTemp_uid_list = NULL;
3645 if (!account || !mailbox || !uid || !msgno) {
3646 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
3647 err = EMAIL_ERROR_INVALID_PARAM;
3651 uid_list = mailbox->user_data;
3653 if (uid_list == NULL) {
3654 if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3655 if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3656 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed [%d]", err);
3660 else { /* EMAIL_SERVER_TYPE_IMAP4 */
3661 if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3662 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
3666 mailbox->user_data = uid_list;
3668 pTemp_uid_list = uid_list;
3670 if (!strcmp(uid_list->uid, uid)) {
3671 *msgno = uid_list->msgno;
3672 EM_DEBUG_LOG("uid_list->msgno[%d]", uid_list->msgno);
3676 EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
3677 uid_list = uid_list->next;
3680 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3683 if (err_code != NULL)
3685 uid_list = pTemp_uid_list ;
3686 if (uid_list != NULL)
3687 emcore_free_uids(uid_list, NULL);
3688 /* mailbox->user_data and uid_list both point to same memory address, So when uid_list is freed then just set */
3689 /* mailbox->user_data to NULL and dont use EM_SAFE_FREE, it will crash : ) */
3691 mailbox->user_data = NULL;
3692 EM_DEBUG_FUNC_END();
3696 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int input_mailbox_id)
3698 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d]", input_mailbox_id);
3699 int err = EMAIL_ERROR_NONE;
3700 char *conditional_clause_string = NULL;
3701 email_list_filter_t *filter_list = NULL;
3702 int filter_count = 0;
3703 int *result_mail_id_list = NULL;
3704 int result_count = 0;
3705 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
3707 if ( input_mailbox_id <= 0) {
3708 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3709 err = EMAIL_ERROR_INVALID_PARAM;
3713 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
3714 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
3720 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3722 if (filter_list == NULL) {
3723 EM_DEBUG_EXCEPTION("em_malloc failed");
3724 err = EMAIL_ERROR_OUT_OF_MEMORY;
3728 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3729 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3730 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3731 filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3733 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3734 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
3736 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3737 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3738 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3739 filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
3741 if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
3742 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
3746 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3748 if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
3749 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
3753 if (!emcore_delete_mails_from_local_storage(mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
3754 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage [%d]", err);
3761 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
3764 emstorage_free_list_filter(&filter_list, filter_count);
3766 EM_SAFE_FREE(result_mail_id_list);
3768 EM_DEBUG_FUNC_END("err [%d]", err);
3772 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int input_account_id, int input_mailbox_id)
3774 int err = EMAIL_ERROR_NONE;
3775 char *conditional_clause_string = NULL;
3776 email_list_filter_t *filter_list = NULL;
3777 int filter_count = 0;
3778 int *result_mail_id_list = NULL;
3779 int result_count = 0;
3781 if ( input_mailbox_id <= 0) {
3782 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3783 err = EMAIL_ERROR_INVALID_PARAM;
3789 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3791 if (filter_list == NULL) {
3792 EM_DEBUG_EXCEPTION("em_malloc failed");
3793 err = EMAIL_ERROR_OUT_OF_MEMORY;
3797 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3798 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3799 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3800 filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3802 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3803 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
3805 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3806 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3807 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3808 filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
3810 if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
3811 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
3815 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3817 if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
3818 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
3822 if (!emcore_delete_mail(input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
3823 EM_DEBUG_EXCEPTION("emcore_delete_mail [%d]", err);
3830 emstorage_free_list_filter(&filter_list, filter_count);
3832 EM_SAFE_FREE(result_mail_id_list);
3834 EM_DEBUG_FUNC_END("err [%d]", err);
3840 * add a attachment to mail.
3842 * mailbox : mail box
3844 * attachment : attachment to be added
3849 INTERNAL_FUNC int emcore_mail_add_attachment(int mail_id, email_attachment_data_t *attachment, int *err_code)
3851 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
3853 if (attachment == NULL) {
3854 EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
3856 *err_code = EMAIL_ERROR_INVALID_PARAM;
3860 int ret = false, err = EMAIL_ERROR_NONE;
3861 emstorage_mail_tbl_t *mail_table_data = NULL;
3862 int attachment_id = 0;
3866 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
3867 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3872 int account_id = mail_table_data->account_id;
3873 emstorage_attachment_tbl_t attachment_tbl;
3875 memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3877 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
3878 attachment_tbl.account_id = mail_table_data->account_id;
3879 attachment_tbl.mailbox_id = mail_table_data->mailbox_id;
3880 attachment_tbl.mail_id = mail_id;
3881 attachment_tbl.attachment_name = attachment->attachment_name;
3882 attachment_tbl.attachment_size = attachment->attachment_size;
3883 attachment_tbl.attachment_save_status = attachment->save_status;
3884 attachment_tbl.attachment_drm_type = attachment->drm_status;
3885 attachment_tbl.attachment_inline_content_status = attachment->inline_content_status;
3887 /* BEGIN TRANSACTION; */
3888 emstorage_begin_transaction(NULL, NULL, NULL);
3890 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
3891 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3896 attachment->attachment_id = attachment_tbl.attachment_id;
3898 if (attachment->attachment_path) {
3901 if (!attachment->inline_content_status) {
3902 if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err)) {
3903 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3906 attachment_id = attachment_tbl.attachment_id;
3909 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->attachment_name, buf, &err)) {
3910 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3913 attachment_tbl.attachment_path = buf;
3915 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
3916 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3921 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
3922 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3927 if (attachment->save_status) {
3928 if (!emstorage_move_file(attachment->attachment_path, buf, false, &err)) {
3929 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3935 /* Here only filename is being updated. Since first add is being done there will not be any old files.
3936 So no need to check for old files in this update case */
3937 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
3938 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3943 EM_SAFE_FREE(attachment->attachment_path);
3944 attachment->attachment_path = EM_SAFE_STRDUP(buf);
3950 if (ret == true) { /* COMMIT TRANSACTION; */
3951 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
3952 err = EMAIL_ERROR_DB_FAILURE;
3956 else { /* ROLLBACK TRANSACTION; */
3957 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
3958 err = EMAIL_ERROR_DB_FAILURE;
3962 if (mail_table_data != NULL)
3963 emstorage_free_mail(&mail_table_data, 1, NULL);
3967 EM_DEBUG_FUNC_END("err [%d]", err);
3972 INTERNAL_FUNC int emcore_mail_add_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
3974 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
3976 int err = EMAIL_ERROR_NONE;
3977 int attachment_id = 0;
3978 char buf[512] = { 0, };
3979 emstorage_mail_tbl_t *mail_table_data = NULL;
3980 emstorage_attachment_tbl_t attachment_tbl = { 0 };
3982 if (input_attachment_data == NULL) {
3983 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3984 return EMAIL_ERROR_INVALID_PARAM;
3987 if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
3988 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3993 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
3995 attachment_tbl.account_id = mail_table_data->account_id;
3996 attachment_tbl.mailbox_id = mail_table_data->mailbox_id;
3997 attachment_tbl.mail_id = input_mail_id;
3998 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
3999 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4000 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4001 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4002 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4004 /* BEGIN TRANSACTION; */
4005 emstorage_begin_transaction(NULL, NULL, NULL);
4007 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
4008 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4013 input_attachment_data->attachment_id = attachment_tbl.attachment_id;
4015 if (input_attachment_data->attachment_path) {
4016 if (!input_attachment_data->inline_content_status) {
4017 if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4018 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4021 attachment_id = attachment_tbl.attachment_id;
4024 if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4025 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4028 attachment_tbl.attachment_path = buf;
4030 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4031 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4036 if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4037 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4042 if (input_attachment_data->save_status) {
4043 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err)) {
4044 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4050 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4051 So no need to check for old files in this update case */
4052 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4053 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4058 EM_SAFE_FREE(input_attachment_data->attachment_path);
4059 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4063 if (err == EMAIL_ERROR_NONE) { /* COMMIT TRANSACTION; */
4064 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4065 err = EMAIL_ERROR_DB_FAILURE;
4068 else { /* ROLLBACK TRANSACTION; */
4069 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4070 err = EMAIL_ERROR_DB_FAILURE;
4074 if (mail_table_data != NULL)
4075 emstorage_free_mail(&mail_table_data, 1, NULL);
4077 EM_DEBUG_FUNC_END("err [%d]", err);
4083 * delete a attachment from mail.
4085 * mailbox : mail box
4087 * attachment_id : number string of attachment-id to be deleted
4088 * (ex : if attachment id is 2, number stirng will be "2")
4093 int emcore_delete_mail_attachment(int attachment_id, int *err_code)
4095 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
4097 if (attachment_id == 0) {
4098 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
4099 if (err_code != NULL)
4100 *err_code = EMAIL_ERROR_INVALID_PARAM;
4105 int error = EMAIL_ERROR_NONE;
4107 /* BEGIN TRANSACTION; */
4108 emstorage_begin_transaction(NULL, NULL, NULL);
4110 if (!emstorage_delete_attachment_on_db(attachment_id, false, &error)) {
4111 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
4118 if (ret == true) { /* COMMIT TRANSACTION; */
4119 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4120 error = EMAIL_ERROR_DB_FAILURE;
4124 else { /* ROLLBACK TRANSACTION; */
4125 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4126 error = EMAIL_ERROR_DB_FAILURE;
4129 if (err_code != NULL)
4131 EM_DEBUG_FUNC_END("ret [%d]", ret);
4135 static int emcore_mail_update_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
4137 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4139 int err = EMAIL_ERROR_NONE;
4140 int attachment_id = 0;
4141 char buf[512] = { 0 , };
4142 emstorage_attachment_tbl_t *existing_attachment_info = NULL;
4143 emstorage_attachment_tbl_t attachment_tbl = { 0 };
4145 if (input_attachment_data == NULL) {
4146 EM_DEBUG_EXCEPTION("Invalid Parameter");
4147 err = EMAIL_ERROR_INVALID_PARAM;
4151 if (!emstorage_get_attachment(input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
4152 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4157 attachment_tbl.mail_id = input_mail_id;
4158 attachment_tbl.account_id = existing_attachment_info->account_id;
4159 attachment_tbl.mailbox_id = existing_attachment_info->mailbox_id;
4160 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
4161 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4162 attachment_tbl.attachment_path = input_attachment_data->attachment_path;
4163 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4164 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4165 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4166 attachment_tbl.attachment_id = input_attachment_data->attachment_id;
4168 if (!input_attachment_data->inline_content_status) {
4169 if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4170 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4173 attachment_id = attachment_tbl.attachment_id;
4176 if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4177 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4180 attachment_tbl.attachment_path = buf;
4182 EM_DEBUG_LOG("downloaded [%d], savename [%s], attachment_path [%s]", input_attachment_data->save_status, input_attachment_data->attachment_path, existing_attachment_info->attachment_path);
4183 if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
4184 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err)) {
4185 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4191 EM_DEBUG_LOG("no need to move");
4193 EM_SAFE_FREE(input_attachment_data->attachment_path);
4194 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4196 emstorage_begin_transaction(NULL, NULL, NULL);
4198 if (!emstorage_update_attachment(&attachment_tbl, false, &err)) {
4199 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4205 if (err == EMAIL_ERROR_NONE) { /* COMMIT TRANSACTION; */
4206 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4207 err = EMAIL_ERROR_DB_FAILURE;
4210 else { /* ROLLBACK TRANSACTION; */
4211 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4212 err = EMAIL_ERROR_DB_FAILURE;
4216 if (existing_attachment_info)
4217 emstorage_free_attachment(&existing_attachment_info, 1, NULL);
4219 EM_DEBUG_FUNC_END("err [%d]", err);
4224 static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, int input_attachment_a_id, email_attachment_data_t *input_attachment_b_data, int *result)
4226 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_a_id[%d], input_attachment_b_data[%p], result[%p]", input_mail_id, input_attachment_a_id, input_attachment_b_data, result);
4228 EM_IF_NULL_RETURN_VALUE(input_attachment_b_data, false);
4229 EM_IF_NULL_RETURN_VALUE(result, false);
4231 int err, err_2, ret = EMAIL_ERROR_NONE;
4232 emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
4234 if (!emstorage_get_attachment(input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
4235 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4240 if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
4241 EM_DEBUG_LOG("attachment_a_tbl->attachment_name [%s], input_attachment_b_data->name [%s]", attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4242 *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4249 if (attachment_a_tbl)
4250 emstorage_free_attachment(&attachment_a_tbl, 1, &err_2);
4251 EM_DEBUG_FUNC_END("*result [%d]", *result);
4257 * copy a mail to mail box
4259 * src_mailbox : source mail box
4260 * msgno : mail sequence
4261 * dst_mailbox : target mail box
4266 INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, int *err_code)
4268 EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
4271 int err = EMAIL_ERROR_NONE;
4273 emstorage_mail_tbl_t *mail = NULL;
4274 emstorage_attachment_tbl_t *atch_list = NULL;
4276 int count = EMAIL_ATTACHMENT_MAX_COUNT;
4277 char *mailbox_name = NULL;
4279 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
4280 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4285 if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMAIL_ERROR_NONE ){
4286 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4291 /* get increased uid. */
4292 if (!emstorage_increase_mail_id(&mail->mail_id, true, &err)) {
4293 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
4298 /* copy mail body(text) file */
4299 if (mail->file_path_plain) {
4300 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4301 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4306 gchar *filename = g_path_get_basename(mail->file_path_plain);
4308 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4309 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4317 if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err)) {
4318 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4323 mail->file_path_plain = EM_SAFE_STRDUP(buf);
4326 /* copy mail body(html) file */
4327 if (mail->file_path_html) {
4328 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4329 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4334 gchar *filename = g_path_get_basename(mail->file_path_html);
4336 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4337 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4345 if (!emstorage_copy_file(mail->file_path_html, buf, false, &err)) {
4346 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4351 mail->file_path_html = EM_SAFE_STRDUP(buf);
4354 /* BEGIN TRANSACTION; */
4355 emstorage_begin_transaction(NULL, NULL, NULL);
4357 /* insert mail data */
4358 EM_SAFE_FREE(mail->mailbox_name);
4360 mail->account_id = dst_mailbox->account_id;
4361 mail->mailbox_id = dst_mailbox->mailbox_id;
4362 mail->mailbox_name = EM_SAFE_STRDUP(dst_mailbox->mailbox_name);
4363 mail->mailbox_type = dst_mailbox->mailbox_type;
4365 if (!emstorage_add_mail(mail, 0, false, &err)) {
4366 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
4368 if (mail->file_path_plain) {
4369 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4370 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4371 emstorage_rollback_transaction(NULL, NULL, NULL);
4375 if (mail->file_path_html) {
4376 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4377 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4378 emstorage_rollback_transaction(NULL, NULL, NULL);
4383 /* ROLLBACK TRANSACTION; */
4384 emstorage_rollback_transaction(NULL, NULL, NULL);
4390 /* copy attachment file */
4391 for (i = 0; i<count; i++) {
4392 if (atch_list[i].attachment_path) {
4393 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err)) {
4394 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4398 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err)) {
4399 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4403 if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err)) {
4404 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4408 EM_SAFE_FREE(atch_list[i].attachment_path);
4410 atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
4413 atch_list[i].account_id = dst_mailbox->account_id;
4414 atch_list[i].mail_id = mail->mail_id;
4415 atch_list[i].mailbox_id = mail->mailbox_id;
4417 if (!emstorage_add_attachment(&atch_list[i], 0, false, &err)) {
4418 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4424 /* in case error happened, delete copied file. */
4425 if (i && i != count) {
4426 for (;i >= 0; i--) {
4427 if (atch_list[i].attachment_path) {
4429 if (!emstorage_delete_file(atch_list[i].attachment_path, &err)) {
4430 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4431 emstorage_rollback_transaction(NULL, NULL, NULL);
4437 if (mail->file_path_plain) {
4438 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4439 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4440 emstorage_rollback_transaction(NULL, NULL, NULL);
4444 if (mail->file_path_html) {
4445 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4446 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4447 emstorage_rollback_transaction(NULL, NULL, NULL);
4452 /* ROLLBACK TRANSACTION; */
4453 emstorage_rollback_transaction(NULL, NULL, NULL);
4457 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4458 err = EMAIL_ERROR_DB_FAILURE;
4459 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4460 err = EMAIL_ERROR_DB_FAILURE;
4464 if (!emstorage_get_mailbox_name_by_mailbox_type(dst_mailbox->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err)) {
4465 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
4469 if (!strcmp(dst_mailbox->mailbox_name, mailbox_name) && !(mail->flags_seen_field))
4470 emcore_check_unread_mail();
4475 if (atch_list != NULL)
4476 emstorage_free_attachment(&atch_list, count, NULL);
4479 emstorage_free_mail(&mail, 1, NULL);
4481 EM_SAFE_FREE(mailbox_name);
4483 if (err_code != NULL)
4485 EM_DEBUG_FUNC_END("err [%d]", err);
4490 * move a mail to mail box
4492 * old_mailbox : previous mail box
4494 * new_mailbox : target mail box
4499 INTERNAL_FUNC int emcore_move_mail(int mail_ids[], int mail_ids_count, int dst_mailbox_id, int noti_param_1, int noti_param_2 ,int *err_code)
4501 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], mail_ids_count[%d], dst_mailbox_id[%d], noti_param [%d], err_code[%p]", mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, err_code);
4504 int err = EMAIL_ERROR_NONE;
4505 emstorage_mail_tbl_t *mail_list = NULL;
4507 int i = 0, parameter_string_length = 0;
4508 char *parameter_string = NULL, mail_id_string[10];
4510 if ( dst_mailbox_id <= 0 && mail_ids_count < 1) {
4511 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4512 err = EMAIL_ERROR_INVALID_PARAM;
4516 if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
4517 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
4522 account_id = mail_list[0].account_id;
4524 if(!emstorage_move_multiple_mails(account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
4525 EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails failed [%d]", err);
4529 /* Sending a notification */
4530 parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
4531 parameter_string = em_malloc(parameter_string_length);
4533 if (mail_list[0].mailbox_id > 0)
4534 SNPRINTF(parameter_string, parameter_string_length, "%d%c%d%c", mail_list[0].mailbox_id, 0x01, dst_mailbox_id , 0x01);
4536 if (parameter_string == NULL) {
4537 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
4538 err = EMAIL_ERROR_OUT_OF_MEMORY;
4542 for (i = 0; i < mail_ids_count; i++) {
4543 memset(mail_id_string, 0, 10);
4544 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
4545 strcat(parameter_string, mail_id_string);
4548 EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
4550 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
4551 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE] >>>> ");
4554 for (i = 0; i < mail_ids_count; i++) {
4555 if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, true, &err))
4556 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
4559 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
4560 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
4562 emcore_check_unread_mail();
4567 emstorage_free_mail(&mail_list, mail_ids_count, NULL);
4568 EM_SAFE_FREE(parameter_string);
4569 if (err_code != NULL)
4571 EM_DEBUG_FUNC_END("err [%d]", err);
4576 INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, int src_mailbox_id, int mail_ids[], int num, char *dest_mailbox, int *error_code)
4578 EM_DEBUG_FUNC_BEGIN();
4579 MAILSTREAM *stream = NULL;
4581 email_account_t *ref_account = NULL;
4582 emstorage_mail_tbl_t *mail = NULL;
4587 mail_id = mail_ids[0];
4589 ref_account = emcore_get_account_reference(account_id);
4591 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : get account reference[%d]", account_id);
4592 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
4597 /* if not imap4 mail, return */
4598 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
4599 *error_code = EMAIL_ERROR_INVALID_PARAM;
4604 for (i = 0; i < num; i++) {
4605 mail_id = mail_ids[i];
4607 if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail) {
4608 EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id : emstorage_get_downloaded_mail failed [%d]", err_code);
4610 if (err_code == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
4611 /* err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
4615 *error_code = err_code;
4622 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) /* faizan.h@samsung.com mail_move_fix_07042009 */ {
4623 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : Mailbox open[%d]", err_code);
4630 /* set callback for COPY_UID */
4631 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
4633 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
4635 if (mail->server_mail_id) {
4636 if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
4637 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server : Mail cannot be moved failed");
4641 /* send EXPUNGE command */
4642 if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
4643 EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
4645 if (err_code == EMAIL_ERROR_IMAP4_STORE_FAILURE)
4646 err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
4650 /* faizan.h@samsung.com duplicate copy while sync mailbox issue fixed */
4651 EM_DEBUG_LOG(">>>mailbox_name[%s]>>>>>>", mail->mailbox_name);
4652 EM_DEBUG_LOG(">>>g_new_server_uid[%s]>>>>>>", g_new_server_uid);
4653 EM_DEBUG_LOG(">>>mail_id[%d]>>>>>>", mail_id);
4655 if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->mailbox_name, &err_code)) {
4656 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
4660 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
4664 EM_DEBUG_EXCEPTION(">>>> Server MAIL ID IS NULL >>>> ");
4667 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
4676 if (stream) emcore_close_mailbox(account_id, stream);
4679 emstorage_free_mail(&mail, 1, NULL);
4680 EM_DEBUG_FUNC_END("ret [%d]", ret);
4684 static int emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code)
4686 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d] , file_name[%p] , full_path[%p] , err_code[%p]", account_id, mail_id, attachment_id, file_name, full_path, err_code);
4688 int ret = false, err = EMAIL_ERROR_NONE;
4690 if (!file_name || !full_path || !src_file_path) {
4691 EM_DEBUG_EXCEPTION("Invalid paramter");
4692 err = EMAIL_ERROR_INVALID_PARAM;
4696 if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err)) {
4697 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4701 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err)) {
4702 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4706 if (strcmp(src_file_path, full_path) != 0) {
4707 if (!emstorage_copy_file(src_file_path, full_path, false, &err)) {
4708 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4723 * update mail information
4725 INTERNAL_FUNC int emcore_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t* input_meeting_request, int input_from_eas)
4727 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
4729 char filename_buf[1024] = {0, };
4731 int err = EMAIL_ERROR_NONE;
4732 int local_inline_content_count = 0;
4733 emstorage_mail_tbl_t *converted_mail_tbl_data = NULL;
4734 email_meeting_request_t *meeting_req = NULL;
4737 if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list)) {
4738 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4739 err = EMAIL_ERROR_INVALID_PARAM;
4743 if(input_from_eas == 0) {
4744 if (input_mail_data->file_path_plain) {
4745 if (stat(input_mail_data->file_path_plain, &st_buf) < 0) {
4746 EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
4747 err = EMAIL_ERROR_INVALID_MAIL;
4752 if (input_mail_data->file_path_html) {
4753 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
4754 EM_DEBUG_EXCEPTION("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
4755 err = EMAIL_ERROR_INVALID_MAIL;
4760 if (input_attachment_count && input_attachment_data_list) {
4761 for (i = 0; i < input_attachment_count; i++) {
4762 if (input_attachment_data_list[i].save_status) {
4763 if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0) {
4764 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
4765 err = EMAIL_ERROR_INVALID_ATTACHMENT;
4773 if(input_mail_data->mail_size == 0) {
4774 emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
4777 if (!input_mail_data->full_address_from) {
4778 email_account_t *ref_account = NULL;
4779 if (!(ref_account = emcore_get_account_reference(input_mail_data->account_id))) {
4780 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_data->account_id);
4781 err = EMAIL_ERROR_INVALID_ACCOUNT;
4784 input_mail_data->full_address_from = ref_account->user_email_address;
4787 if (input_mail_data->file_path_plain) { /* Save plain text body. */
4788 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, "UTF-8", filename_buf, &err)) {
4789 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4792 EM_SAFE_FREE(input_mail_data->file_path_plain);
4793 input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
4796 if (input_mail_data->file_path_html) { /* Save HTML text body. */
4797 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, "UTF-8.htm", filename_buf, &err)) {
4798 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4801 EM_SAFE_FREE(input_mail_data->file_path_html);
4802 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
4805 if (input_mail_data->file_path_mime_entity) { /* Save mime entity. */
4806 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_mime_entity, "mime_entity", filename_buf, &err)) {
4807 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4810 EM_SAFE_FREE(input_mail_data->file_path_html);
4811 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
4814 if (input_attachment_data_list && input_attachment_count) {
4816 int compare_result = 1;
4817 email_attachment_data_t *temp_attachment_data = NULL;
4819 for(i = 0; i < input_attachment_count; i++) {
4820 temp_attachment_data = input_attachment_data_list + i;
4821 if ( (err = emcore_mail_compare_filename_of_attachment_data(input_mail_data->mail_id, temp_attachment_data->attachment_id, temp_attachment_data, &compare_result)) != EMAIL_ERROR_NONE) {
4822 EM_DEBUG_EXCEPTION("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
4826 if (compare_result == 0) {
4827 EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
4828 if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data)) {
4829 EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
4834 EM_DEBUG_LOG("save names are different");
4835 if(temp_attachment_data->attachment_id > 0) {
4836 if (!emcore_delete_mail_attachment(temp_attachment_data->attachment_id, &err)) {
4837 EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
4842 if ( (err = emcore_mail_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMAIL_ERROR_NONE) {
4843 EM_DEBUG_EXCEPTION("emcore_mail_add_attachment failed [%d]", err);
4848 if (temp_attachment_data->inline_content_status)
4849 local_inline_content_count++;
4853 input_mail_data->attachment_count = input_attachment_count;
4854 input_mail_data->inline_content_count = local_inline_content_count;
4856 if (!input_mail_data->date_time) {
4857 /* time isn't set */
4858 input_mail_data->date_time = time(NULL);
4861 EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
4862 if (input_mail_data->preview_text == NULL) {
4863 if ( (err =emcore_get_preview_text_from_file(input_mail_data->file_path_plain, input_mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(input_mail_data->preview_text))) != EMAIL_ERROR_NONE) {
4864 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
4869 if(!em_convert_mail_data_to_mail_tbl(input_mail_data, 1, &converted_mail_tbl_data, &err)) {
4870 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
4874 /* BEGIN TRANSACTION; */
4875 emstorage_begin_transaction(NULL, NULL, NULL);
4877 if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err)) {
4878 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4883 if (input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
4884 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
4885 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
4886 /* check where there is a meeting request in DB */
4887 if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMAIL_ERROR_DATA_NOT_FOUND) {
4888 EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
4891 EM_SAFE_FREE(meeting_req);
4892 if (err == EMAIL_ERROR_DATA_NOT_FOUND) { /* insert */
4893 emstorage_mail_tbl_t *original_mail = NULL;
4895 if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
4896 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4900 if (original_mail) {
4901 if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_id, input_meeting_request, false, &err)) {
4902 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
4906 emstorage_free_mail(&original_mail, 1, NULL);
4910 if (!emstorage_update_meeting_request(input_meeting_request, false, &err)) {
4911 EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
4919 if (err == EMAIL_ERROR_NONE) {
4920 /* COMMIT TRANSACTION; */
4921 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4922 err = EMAIL_ERROR_DB_FAILURE;
4925 if (input_mail_data->meeting_request_status && !emstorage_notify_storage_event(NOTI_MAIL_UPDATE, input_mail_data->account_id, input_mail_data->mail_id, NULL, UPDATE_MEETING))
4926 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_UPDATE : UPDATE_MEETING_RESPONSE ] >>>> ");
4929 /* ROLLBACK TRANSACTION; */
4930 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4931 err = EMAIL_ERROR_DB_FAILURE;
4937 emstorage_free_meeting_request(meeting_req);
4939 if(converted_mail_tbl_data)
4940 emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
4942 EM_DEBUG_FUNC_END("err [%d]", err);
4946 INTERNAL_FUNC int emcore_fetch_flags(int account_id, int mail_id, email_mail_flag_t *mail_flag, int *err_code)
4948 EM_DEBUG_FUNC_BEGIN();
4952 emstorage_mail_tbl_t *mail = NULL;
4954 EM_IF_NULL_RETURN_VALUE(mail_flag, false);
4955 if (account_id < FIRST_ACCOUNT_ID || mail_id < 1) {
4956 EM_DEBUG_EXCEPTION("emcore_fetch_flags : Invalid Param ");
4960 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail) {
4961 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed - %d ", err);
4966 if (!em_convert_mail_tbl_to_mail_flag(mail, mail_flag, &err)) {
4967 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_int failed - %d ", err);
4975 emstorage_free_mail(&mail, 1, &err);
4977 if (err_code != NULL)
4980 EM_DEBUG_FUNC_END("err [%d]", err);
4983 /* description : modify flag of msgno.
4984 return 1(success), 0(failure)
4986 INTERNAL_FUNC int emcore_modify_flag(int mail_id, email_mail_flag_t new_flag, int sticky_flag, int *err_code)
4988 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
4991 int err = EMAIL_ERROR_NONE;
4992 int old_seen, new_seen;
4994 emstorage_mail_tbl_t *mail = NULL;
4995 email_mail_flag_t old_flag;
4997 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail) {
4998 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
5003 if (!em_convert_mail_tbl_to_mail_flag(mail, &old_flag, &err)) {
5004 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_flag failed [%d]", err);
5008 old_seen = mail->flags_seen_field;
5009 new_seen = new_flag.seen;
5011 if (!em_convert_mail_flag_to_mail_tbl(&new_flag, mail, &err)) {
5012 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_mail_tbl failed [%d]", err);
5016 mail->lock_status = sticky_flag;
5018 if (!emstorage_change_mail_field(mail_id, UPDATE_FLAG, mail, true, &err)) {
5019 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5025 if (old_seen != new_seen && new_seen == 0)
5026 emcore_delete_notification_for_read_mail(mail_id);
5028 emcore_check_unread_mail();
5034 emstorage_free_mail(&mail, 1, NULL);
5038 EM_DEBUG_FUNC_END("err [%d]", err);
5042 INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
5044 EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_ids[%p], num [%d], field_type [%d], value[%d], err_code[%p]", account_id, mail_ids, num, field_type, value, err_code);
5047 int err = EMAIL_ERROR_NONE;
5048 char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "flags_seen_field"
5049 , "flags_deleted_field", "flags_flagged_field", "flags_answered_field"
5050 , "flags_recent_field", "flags_draft_field", "flags_forwarded_field" };
5052 if(field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT || mail_ids == NULL || num <= 0 || account_id == 0) {
5053 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5054 err = EMAIL_ERROR_INVALID_PARAM;
5058 if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
5059 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
5063 emcore_check_unread_mail();
5072 EM_DEBUG_FUNC_END("err [%d]", err);
5077 /* description : modify extra flag of msgno.
5078 return 0(success), -1(failure)
5080 INTERNAL_FUNC int emcore_modify_extra_flag(int mail_id, email_extra_flag_t new_flag, int *err_code)
5082 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
5085 int err = EMAIL_ERROR_NONE;
5086 emstorage_mail_tbl_t mail;
5088 memset(&mail, 0x00, sizeof(emstorage_mail_tbl_t));
5090 mail.mail_id = mail_id;
5091 mail.save_status = new_flag.status;
5092 mail.lock_status = new_flag.report;
5093 mail.lock_status = new_flag.lock;
5094 mail.priority = new_flag.priority;
5095 mail.DRM_status = new_flag.drm;
5097 if (!emstorage_change_mail_field(mail_id, UPDATE_EXTRA_FLAG, &mail, false, &err)) {
5098 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5106 if (err_code != NULL)
5108 EM_DEBUG_FUNC_END("err [%d]", err);
5113 int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *result_total_body_size, int *err_code)
5115 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
5118 int err = EMAIL_ERROR_NONE;
5119 int total_body_size = 0;
5121 char *response = NULL;
5122 POP3LOCAL *pop3local;
5124 if (!stream || !result_total_body_size) {
5125 EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
5126 err = EMAIL_ERROR_INVALID_PARAM;
5130 pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
5132 if (!pop3local || !pop3local->netstream) {
5133 err = EMAIL_ERROR_INVALID_STREAM;
5136 memset(command, 0x00, sizeof(command));
5138 SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
5140 #ifdef FEATURE_CORE_DEBUG
5141 EM_DEBUG_LOG("[POP3] >>> [%s]", command);
5145 /* send command : LIST [msgno] - to get the size of the mail */
5146 if (!net_sout(pop3local->netstream, command, (int)strlen(command))) {
5147 EM_DEBUG_EXCEPTION("net_sout failed...");
5149 err = EMAIL_ERROR_INVALID_RESPONSE;
5153 EM_DEBUG_LOG("Sending command success");
5155 while (pop3local->netstream) {
5156 /* receive response */
5157 if (!(response = net_getline(pop3local->netstream)))
5161 EM_DEBUG_LOG("[POP3] <<< %s", response);
5163 if (*response == '.') {
5164 EM_SAFE_FREE(response);
5168 if (*response == '+') { /* "+ OK" */
5171 if (!(p = strchr(response + strlen("+OK "), ' '))) {
5172 err = EMAIL_ERROR_INVALID_RESPONSE;
5176 total_body_size = atoi(p + 1);
5177 EM_DEBUG_LOG("Body size [%d]", total_body_size);
5179 if (result_total_body_size) {
5180 *result_total_body_size = total_body_size;
5184 else if (*response == '-') { /* "- ERR" */
5185 err = EMAIL_ERROR_INVALID_RESPONSE;
5189 err = EMAIL_ERROR_INVALID_RESPONSE;
5193 EM_SAFE_FREE(response);
5196 memset(command, 0x00, sizeof(command));
5198 if (limited_size && total_body_size > limited_size) {
5199 int count_of_line = limited_size / 80;
5200 SNPRINTF(command, sizeof(command), "TOP %d %d", msgno, count_of_line);
5203 SNPRINTF(command, sizeof(command), "RETR %d", msgno);
5205 EM_DEBUG_LOG("[POP3] >>> %s", command);
5207 emcore_set_network_error(EMAIL_ERROR_NONE); /* set current network error as EMAIL_ERROR_NONE before network operation */
5208 /* get mail from mail server */
5209 if (!pop3_send((MAILSTREAM *)stream, command, NULL)) {
5210 EM_DEBUG_EXCEPTION("pop3_send failed...");
5212 email_session_t *session = NULL;
5214 if (!emcore_get_current_session(&session)) {
5215 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
5216 err = EMAIL_ERROR_SESSION_NOT_FOUND;
5220 if (session->network == EMAIL_ERROR_NONE)
5221 err = EMAIL_ERROR_UNKNOWN;
5223 err = session->network;
5231 EM_SAFE_FREE(response);
5233 if (err_code != NULL)
5235 EM_DEBUG_FUNC_END("err [%d]", err);
5240 INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
5242 EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
5245 if (err_code != NULL)
5246 *err_code = EMAIL_ERROR_INVALID_PARAM;
5251 int err = EMAIL_ERROR_NONE;
5252 int status = EMAIL_DOWNLOAD_FAIL;
5253 MAILSTREAM *stream = NULL;
5254 email_internal_mailbox_t mailbox = {0};
5255 emstorage_mail_tbl_t *mail = NULL;
5256 email_account_t *ref_account = NULL;
5259 email_mail_flag_t new_flag = {0};
5260 char set_flags[100] = { 0, };
5261 char clear_flags[100] = { 0, };
5262 char tmp[100] = { 0, };
5264 if (!emcore_check_thread_status()) {
5265 err = EMAIL_ERROR_CANCELLED;
5269 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5270 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5275 account_id = mail->account_id;
5276 em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5278 if (!(ref_account = emcore_get_account_reference(account_id))) {
5279 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5280 err = EMAIL_ERROR_INVALID_ACCOUNT;
5284 if (!emcore_check_thread_status()) {
5285 err = EMAIL_ERROR_CANCELLED;
5289 /* open mail server. */
5290 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
5291 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5292 status = EMAIL_LIST_CONNECTION_FAIL;
5297 if (!emcore_check_thread_status()) {
5298 err = EMAIL_ERROR_CANCELLED;
5302 mailbox.mailbox_name = mail->mailbox_name;
5303 mailbox.account_id = account_id;
5304 mailbox.mail_stream = stream;
5306 /* download all uids from server. */
5307 if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err)) {
5308 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5312 sprintf (tmp, "%d", msgno);
5315 sprintf(set_flags, "\\Seen");
5317 sprintf(clear_flags, "\\Seen");
5319 if (new_flag.answered)
5320 sprintf(set_flags, "%s \\Answered", set_flags);
5322 sprintf(clear_flags, "%s \\Answered", clear_flags);
5324 if (new_flag.flagged)
5325 sprintf(set_flags, "%s \\Flagged", set_flags);
5327 sprintf(clear_flags, "%s \\Flagged", clear_flags);
5329 if (new_flag.forwarded)
5330 sprintf(set_flags, "%s $Forwarded", set_flags);
5332 sprintf(clear_flags, "%s $Forwarded", clear_flags);
5334 if (strlen(set_flags) > 0) {
5335 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s] ", set_flags);
5336 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5337 EM_DEBUG_LOG(">>>> End mail_setflag ");
5340 if (strlen(clear_flags) > 0) {
5341 EM_DEBUG_LOG(">>>> Calling mail_clearflag [%s]", clear_flags);
5342 mail_clearflag(stream, tmp, clear_flags);
5343 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5346 if (mail->lock_status) {
5347 memset(set_flags, 0x00, 100);
5348 sprintf(set_flags, "Sticky");
5349 if (strlen(set_flags) > 0) {
5350 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s]", set_flags);
5351 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5352 EM_DEBUG_LOG(">>>> End mail_setflag ");
5356 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5358 if (!emcore_check_thread_status()) {
5359 err = EMAIL_ERROR_CANCELLED;
5367 if (stream) emcore_close_mailbox(account_id, stream);
5368 if (mail) emstorage_free_mail(&mail, 1, NULL);
5370 if (err_code != NULL)
5372 EM_DEBUG_FUNC_END("err [%d]", err);
5376 INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
5378 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
5380 if (mail_ids[0] < 1) {
5381 if (err_code != NULL)
5382 *err_code = EMAIL_ERROR_INVALID_PARAM;
5387 int err = EMAIL_ERROR_NONE;
5388 int status = EMAIL_DOWNLOAD_FAIL;
5389 MAILSTREAM *stream = NULL;
5390 email_internal_mailbox_t mailbox;
5391 emstorage_mail_tbl_t *mail = NULL;
5392 email_account_t *ref_account = NULL;
5395 email_mail_flag_t new_flag;
5396 char set_flags[100];
5397 char clear_flags[100];
5402 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
5404 mail_id = mail_ids[0];
5406 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5407 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5412 account_id = mail->account_id;
5414 if (!(ref_account = emcore_get_account_reference(account_id))) {
5415 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5416 err = EMAIL_ERROR_INVALID_ACCOUNT;
5420 /* open mail server. */
5421 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
5422 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5423 status = EMAIL_LIST_CONNECTION_FAIL;
5427 mailbox.mailbox_name = mail->mailbox_name;
5428 mailbox.account_id = account_id;
5429 mailbox.mail_stream = stream;
5431 for (i = 0; i < num; i++) {
5432 mail_id = mail_ids[i];
5434 if (!emcore_check_thread_status()) {
5435 err = EMAIL_ERROR_CANCELLED;
5439 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5440 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5445 /* account_id = mail->account_id; */
5446 em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5448 if (!emcore_check_thread_status()) {
5449 err = EMAIL_ERROR_CANCELLED;
5453 /* download message number from server. */
5454 if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err)) {
5455 EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5459 memset(tmp, 0x00, 100);
5460 sprintf (tmp, "%d", msgno);
5462 memset(set_flags, 0x00, 100);
5463 memset(clear_flags, 0x00, 100);
5466 sprintf(set_flags, "\\Seen");
5468 sprintf(clear_flags, "\\Seen");
5469 EM_DEBUG_LOG("new_flag.seen : %s ", set_flags);
5471 if (strlen(set_flags) > 0) {
5472 EM_DEBUG_LOG(">>>> Calling mail_setflag ");
5473 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5474 EM_DEBUG_LOG(">>>> End mail_setflag ");
5477 EM_DEBUG_LOG(">>>> Calling mail_clearflag ");
5478 mail_clearflag(stream, tmp, clear_flags);
5479 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5482 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5484 if (!emcore_check_thread_status()) {
5485 err = EMAIL_ERROR_CANCELLED;
5493 if (stream) emcore_close_mailbox(account_id, stream);
5494 if (mail) emstorage_free_mail(&mail, 1, NULL);
5496 if (err_code != NULL)
5498 EM_DEBUG_FUNC_END("err [%d]", err);
5502 INTERNAL_FUNC void emcore_free_mail_data_list(email_mail_data_t **mail_list, int count)
5504 EM_DEBUG_FUNC_BEGIN("count[%d]", count);
5506 if (count <= 0 || !mail_list || !*mail_list) {
5507 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5511 email_mail_data_t* p = *mail_list;
5514 for (i = 0; i < count; i++)
5515 emcore_free_mail_data( p+i);
5517 EM_SAFE_FREE(*mail_list);
5519 EM_DEBUG_FUNC_END();
5522 INTERNAL_FUNC void emcore_free_mail_data(email_mail_data_t *mail_data)
5524 EM_DEBUG_FUNC_BEGIN();
5526 EM_SAFE_FREE(mail_data->subject);
5527 EM_SAFE_FREE(mail_data->server_mailbox_name);
5528 EM_SAFE_FREE(mail_data->server_mail_id);
5529 EM_SAFE_FREE(mail_data->message_id);
5530 EM_SAFE_FREE(mail_data->full_address_from);
5531 EM_SAFE_FREE(mail_data->full_address_reply);
5532 EM_SAFE_FREE(mail_data->full_address_to);
5533 EM_SAFE_FREE(mail_data->full_address_cc);
5534 EM_SAFE_FREE(mail_data->full_address_bcc);
5535 EM_SAFE_FREE(mail_data->full_address_return);
5536 EM_SAFE_FREE(mail_data->email_address_sender);
5537 EM_SAFE_FREE(mail_data->email_address_recipient);
5538 EM_SAFE_FREE(mail_data->alias_sender);
5539 EM_SAFE_FREE(mail_data->alias_recipient);
5540 EM_SAFE_FREE(mail_data->file_path_plain);
5541 EM_SAFE_FREE(mail_data->file_path_html);
5542 EM_SAFE_FREE(mail_data->preview_text);
5544 EM_DEBUG_FUNC_END();
5548 INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
5550 EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);
5552 if (!attachment_data_list || !*attachment_data_list) {
5553 if (err_code != NULL)
5554 *err_code = EMAIL_ERROR_INVALID_PARAM;
5558 email_attachment_data_t* p = *attachment_data_list;
5561 for (i = 0; i < attachment_data_count; i++) {
5562 EM_SAFE_FREE(p[i].attachment_name);
5563 EM_SAFE_FREE(p[i].attachment_path);
5566 EM_SAFE_FREE(p); *attachment_data_list = NULL;
5569 *err_code = EMAIL_ERROR_NONE;
5571 EM_DEBUG_FUNC_END();
5577 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
5579 INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code)
5581 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5583 if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
5584 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5585 if (err_code != NULL)
5586 *err_code = EMAIL_ERROR_INVALID_PARAM;
5591 int err = EMAIL_ERROR_NONE;
5593 emstorage_begin_transaction(NULL, NULL, NULL);
5595 if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err)) {
5596 EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
5603 if (ret == true) { /* COMMIT TRANSACTION; */
5604 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5605 err = EMAIL_ERROR_DB_FAILURE;
5609 else { /* ROLLBACK TRANSACTION; */
5610 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5611 err = EMAIL_ERROR_DB_FAILURE;
5614 if (err_code != NULL)
5616 EM_DEBUG_FUNC_END();
5620 INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code)
5622 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
5624 if (!local_activity || !activity_id) {
5625 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
5626 if (err_code != NULL)
5627 *err_code = EMAIL_ERROR_INVALID_PARAM;
5632 int err = EMAIL_ERROR_NONE;
5634 emstorage_begin_transaction(NULL, NULL, NULL);
5636 if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err)) {
5637 EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
5646 if (ret == true) { /* COMMIT TRANSACTION; */
5647 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5648 err = EMAIL_ERROR_DB_FAILURE;
5652 else { /* ROLLBACK TRANSACTION; */
5653 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5654 err = EMAIL_ERROR_DB_FAILURE;
5657 if (err_code != NULL)
5659 EM_DEBUG_FUNC_END();
5665 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
5667 /* API to set or unset a field of flags on server in single IMAP request to server */
5669 INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, email_flags_field_type field_type, int value, int *err_code)
5671 EM_DEBUG_FUNC_BEGIN("mail_ids [%p], num [%d], field_type [%d], value [%d], err_code [%p]", mail_ids, num, field_type, value, err_code);
5673 if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT) {
5674 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5675 if (err_code != NULL) {
5676 *err_code = EMAIL_ERROR_INVALID_PARAM;
5681 MAILSTREAM *stream = NULL;
5682 IMAPLOCAL *imaplocal = NULL;
5683 char tag[MAX_TAG_SIZE] = {0, };
5684 char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
5686 char **string_list = NULL;
5688 int err = EMAIL_ERROR_NONE;
5689 int command_success = false;
5693 int id_set_count = 0;
5694 int len_of_each_range = 0;
5695 int string_count = 0;
5696 email_account_t *temp_account;
5697 email_id_set_t *id_set = NULL;
5698 emstorage_mail_tbl_t *mail = NULL;
5699 email_uid_range_set *uid_range_set = NULL;
5700 email_uid_range_set *uid_range_node = NULL;
5701 char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "\\Seen"
5702 , "\\Deleted", "\\Flagged", "\\Answered"
5703 , "\\Recent", "\\Draft", "$Forwarded" };
5705 mail_id = mail_ids[0];
5707 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail) /*To DO : This is a existing bug. on mail deletion before this call it will fail always */ {
5708 EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
5713 account_id = mail[0].account_id;
5715 temp_account = emcore_get_account_reference(account_id);
5717 if (!temp_account) {
5718 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5719 err = EMAIL_ERROR_INVALID_ACCOUNT;
5723 if (temp_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
5724 EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->incoming_server_type);
5725 err = EMAIL_ERROR_NOT_SUPPORTED;
5729 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
5730 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5734 if (false == emstorage_free_mail(&mail, 1, &err)) {
5735 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5739 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
5740 /* Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids */
5741 /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid */
5742 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
5744 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
5745 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
5749 /* Now execute one by one each comma separated string of mail_ids */
5751 for (i = 0; i < string_count; ++i) {
5752 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
5754 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
5755 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
5759 /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
5761 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
5763 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
5764 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
5768 uid_range_node = uid_range_set;
5770 while (uid_range_node != NULL) {
5771 /* Remove comma from end of uid_range */
5773 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
5775 /* Form the IMAP command */
5777 SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
5780 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s +FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
5782 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s -FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
5784 EM_DEBUG_LOG("[IMAP4] command %s", cmd);
5788 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
5789 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
5791 err = EMAIL_ERROR_UNKNOWN;
5795 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
5796 EM_DEBUG_EXCEPTION("net_sout failed...");
5797 err = EMAIL_ERROR_CONNECTION_BROKEN;
5801 /* Receive Response */
5803 command_success = false;
5805 while (imaplocal->netstream) {
5806 if (!(p = net_getline(imaplocal->netstream))) {
5807 EM_DEBUG_EXCEPTION("net_getline failed...");
5809 err = EMAIL_ERROR_INVALID_RESPONSE;
5813 EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
5815 if (!strncmp(p, tag, strlen(tag))) {
5816 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
5817 /*Delete all local activities */
5818 command_success = true;
5825 command_success = false;
5826 err = EMAIL_ERROR_IMAP4_STORE_FAILURE;
5835 uid_range_node = uid_range_node->next;
5838 emcore_free_uid_range_set(&uid_range_set);
5840 EM_SAFE_FREE(id_set);
5849 #ifdef __FEATURE_LOCAL_ACTIVITY__
5851 emstorage_activity_tbl_t new_activity;
5852 for (i = 0; i<num ; i++) {
5853 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
5854 new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
5855 new_activity.account_id = account_id;
5856 new_activity.mail_id = mail_ids[i];
5858 if (!emcore_delete_activity(&new_activity, &err))
5859 EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
5866 if (false == emstorage_free_mail(&mail, 1, &err))
5867 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5870 emcore_free_comma_separated_strings(&string_list, &string_count);
5873 emcore_free_uid_range_set(&uid_range_set);
5875 emcore_close_mailbox(0, stream);
5878 if (err_code != NULL)
5880 EM_DEBUG_FUNC_END();
5886 INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err_code)
5888 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
5890 int ret = false, err = EMAIL_ERROR_NONE;
5891 int account_index, account_count, mail_id_index = 0;
5892 email_account_t *account_ref = NULL, *account_list_ref = NULL;
5893 int filtered_mail_id_count = 0, *filtered_mail_id_list = NULL, parameter_string_length = 0;
5894 char *parameter_string = NULL, mail_id_string[10] = { 0x00, };
5895 emstorage_mailbox_tbl_t *spam_mailbox = NULL;
5898 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
5899 err = EMAIL_ERROR_INVALID_PARAM;
5903 if (!emcore_get_account_reference_list(&account_list_ref, &account_count, &err)) {
5904 EM_DEBUG_EXCEPTION("emcore_get_account_reference_list failed [%d]", err);
5908 for (account_index = 0; account_index < account_count; account_index++) {
5909 account_ref = account_list_ref + account_index;
5911 if (!emstorage_get_mailbox_by_mailbox_type(account_ref->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err))
5912 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed [%d]", account_ref->account_id, err);
5913 else if (spam_mailbox && spam_mailbox->mailbox_id > 0) {
5914 if (!emstorage_filter_mails_by_rule(account_ref->account_id, spam_mailbox->mailbox_id, (emstorage_rule_tbl_t *)filter_info, &filtered_mail_id_list, &filtered_mail_id_count, &err))
5915 EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
5917 if (filtered_mail_id_count) {
5918 parameter_string_length = 10 /*mailbox_id length*/ + 7 + (10 * filtered_mail_id_count);
5919 parameter_string = em_malloc(sizeof(char) * parameter_string_length);
5921 if (parameter_string == NULL) {
5922 err = EMAIL_ERROR_OUT_OF_MEMORY;
5923 EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
5926 SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%d%c", 0x01, spam_mailbox->mailbox_id, 0x01);
5928 for (mail_id_index = 0; mail_id_index < filtered_mail_id_count; mail_id_index++) {
5929 memset(mail_id_string, 0, 10);
5930 SNPRINTF(mail_id_string, 10, "%d", filtered_mail_id_list[mail_id_index]);
5931 strcat(parameter_string, mail_id_string);
5932 strcat(parameter_string, ",");
5935 EM_DEBUG_LOG("filtered_mail_id_count [%d]", filtered_mail_id_count);
5936 EM_DEBUG_LOG("param string [%s]", parameter_string);
5938 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_ref->account_id, 0, parameter_string, 0))
5939 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
5941 EM_SAFE_FREE(filtered_mail_id_list);
5942 EM_SAFE_FREE(parameter_string);
5945 emstorage_free_mailbox(&spam_mailbox, 1, &err);
5946 spam_mailbox = NULL;
5950 emcore_check_unread_mail();
5955 EM_SAFE_FREE(account_list_ref);
5956 EM_SAFE_FREE(filtered_mail_id_list);
5957 EM_SAFE_FREE(parameter_string);
5961 emstorage_free_mailbox(&spam_mailbox, 1, &err);
5965 EM_DEBUG_FUNC_END();