4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File : email-core-mail.c
26 * Desc : Mail Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
42 #include <sys/types.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"
63 #include "email-core-signal.h"
64 #include "email-core-smtp.h"
66 #include "email-convert.h"
67 #include "email-debug-log.h"
69 #ifdef __FEATURE_DRM__
70 #include <drm_client.h>
71 #endif /* __FEATURE_DRM__ */
73 #define ST_SILENT (long) 0x2 /* don't return results */
74 #define ST_SET (long) 0x4 /* set vs. clear */
76 static char g_new_server_uid[129];
78 bool only_body_download = false;
80 int multi_part_body_size = 0;
81 int is_multi_part_body_download_all = 0;
82 int _pop3_receiving_mail_id = 0;
83 int _pop3_received_body_size = 0;
84 int _pop3_last_notified_body_size = 0;
85 int _pop3_total_body_size = 0;
87 int _imap4_received_body_size = 0;
88 int _imap4_last_notified_body_size = 0;
89 int _imap4_total_body_size = 0;
90 int _imap4_download_noti_interval_value = 0;
92 BODY **g_inline_list = NULL ;
93 int g_inline_count = 0;
96 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count);
97 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);
99 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
102 #ifdef FEATURE_CORE_DEBUG
103 static char *_getType(int type)
106 case 0: return "TYPETEXT";
107 case 1: return "TYPEMULTIPART";
108 case 2: return "TYPEMESSAGE";
109 case 3: return "TYPEAPPLICATION";
110 case 4: return "TYPEAUDIO";
111 case 5: return "TYPEVIDEO";
112 case 6: return "TYPEMODEL";
113 case 7: return "TYPEOTHER";
115 return g_strdup_printf("%d", type);
118 static char *_getEncoding(int encoding)
121 case 0: return "ENC7BIT";
122 case 1: return "ENC8BIT";
123 case 2: return "ENCBINARY";
124 case 3: return "ENCBASE64";
125 case 4: return "ENCQUOTEDPRINTABLE";
126 case 5: return "ENCOTHER";
128 return g_strdup_printf("%d", encoding);
131 static void _print_parameter(PARAMETER *param)
133 while (param != NULL) {
134 EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
135 EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
141 static void _print_stringlist(STRINGLIST *stringlist)
143 while (stringlist != NULL) {
144 EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
145 EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
147 stringlist = stringlist->next;
151 static void _print_body(BODY *body, int recursive)
153 EM_DEBUG_LOG(" ========================================================== ");
156 EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
157 EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
158 EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
160 EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
162 _print_parameter(body->parameter);
164 EM_DEBUG_LOG("body->id[%s]", body->id);
165 EM_DEBUG_LOG("body->description[%s]", body->description);
167 EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
168 EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
170 _print_parameter(body->disposition.parameter);
172 EM_DEBUG_LOG("body->language[%p]", body->language);
174 _print_stringlist(body->language);
176 EM_DEBUG_LOG("body->location[%s]", body->location);
178 EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
179 EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
180 EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
182 EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
183 EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
184 EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
186 EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
188 EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
189 EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
191 EM_DEBUG_LOG("body->md5[%s]", body->md5);
192 EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
195 PART *part = body->nested.part;
197 while (part != NULL) {
198 _print_body(&(part->body), recursive);
204 EM_DEBUG_LOG(" ========================================================== ");
206 #endif /* FEATURE_CORE_DEBUG */
208 static int convert_contact_err_to_email_err(int contact_err)
210 int err = EMAIL_ERROR_NONE;
212 switch (contact_err) {
213 case CONTACTS_ERROR_NONE :
214 err = EMAIL_ERROR_NONE;
216 case CONTACTS_ERROR_OUT_OF_MEMORY :
217 err = EMAIL_ERROR_OUT_OF_MEMORY;
219 case CONTACTS_ERROR_INVALID_PARAMETER :
220 err = EMAIL_ERROR_INVALID_PARAM;
222 case CONTACTS_ERROR_NO_DATA :
223 err = EMAIL_ERROR_DATA_NOT_FOUND;
225 case CONTACTS_ERROR_DB :
226 err = EMAIL_ERROR_DB_FAILURE;
228 case CONTACTS_ERROR_IPC :
229 err = EMAIL_ERROR_IPC_CONNECTION_FAILURE;
231 case CONTACTS_ERROR_SYSTEM:
232 err = EMAIL_ERROR_SYSTEM_FAILURE;
234 case CONTACTS_ERROR_INTERNAL:
236 err = EMAIL_ERROR_UNKNOWN;
242 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
244 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
247 int err = EMAIL_ERROR_NONE;
249 POP3LOCAL *pop3local = NULL;
254 EM_DEBUG_EXCEPTION("stream[%p]", stream);
255 err = EMAIL_ERROR_INVALID_PARAM;
259 if (!(pop3local = stream->local) || !pop3local->netstream) {
260 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
261 err = EMAIL_ERROR_UNKNOWN;
265 memset(cmd, 0x00, sizeof(cmd));
267 SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
269 #ifdef FEATURE_CORE_DEBUG
270 EM_DEBUG_LOG("[POP3] >>> %s", cmd);
273 /* send command : delete specified mail */
274 if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
275 EM_DEBUG_EXCEPTION("net_sout failed...");
276 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN; */
280 /* receive response */
281 if (!(p = net_getline(pop3local->netstream))) {
282 EM_DEBUG_EXCEPTION("net_getline failed...");
283 err = EMAIL_ERROR_INVALID_RESPONSE;
287 #ifdef FEATURE_CORE_DEBUG
288 EM_DEBUG_LOG("[POP3] <<< %s", p);
291 if (*p == '-') { /* '-ERR' */
292 err = EMAIL_ERROR_POP3_DELE_FAILURE; /* EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
296 if (*p != '+') { /* '+OK' ... */
297 err = EMAIL_ERROR_INVALID_RESPONSE;
313 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
315 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
318 EM_DEBUG_FUNC_BEGIN();
322 int err = EMAIL_ERROR_NONE;
324 unsigned long first_uid = 0;
325 unsigned long last_uid = 0;
327 unsigned long *old_server_uid = NULL;
328 unsigned long *new_server_uid = NULL;
330 SEARCHSET *temp = sourceset;
332 char old_server_uid_char[129];
333 char new_server_uid_char[129];
335 if (NULL == sourceset || NULL == destset) {
337 sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids
338 if sourceset is NULL then corresponding dest set will be NULL
341 EM_DEBUG_LOG("emcore_mail_copyuid_ex failed : Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
345 /* To get count of mails actually moved */
348 if (temp->first > 0) {
349 first_uid = temp->first;
353 if (temp->last > 0) {
354 last_uid = temp->last;
356 while (first_uid < last_uid) {
369 EM_DEBUG_LOG("Count of mails copied [%d]", count);
370 old_server_uid = em_malloc(count * sizeof(unsigned long));
371 new_server_uid = em_malloc(count * sizeof(unsigned long));
373 /* While loop below will collect all old server uid from sourceset into old_server_uid array */
376 if (sourceset->first > 0) {
377 first_uid = sourceset->first;
380 old_server_uid[index] = first_uid;
382 if (sourceset->last > 0) {
383 last_uid = sourceset->last;
385 while (first_uid < last_uid) {
389 old_server_uid[index] = first_uid;
394 sourceset = sourceset->next;
397 /* While loop below will collect all new server uid from destset into new_server_uid array */
400 first_uid = last_uid = 0;
403 if (destset->first > 0) {
404 first_uid = destset->first;
407 new_server_uid[index] = first_uid;
409 if (destset->last > 0) {
410 last_uid = destset->last;
412 while (first_uid < last_uid) {
416 new_server_uid[index] = first_uid;
421 destset = destset->next;
424 /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
426 for (i = 0; i <= index; ++i) {
428 memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
429 sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
431 EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
433 memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
434 sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
436 EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
438 if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
439 EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
442 if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
443 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
446 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
448 if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
449 EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
455 EM_SAFE_FREE(old_server_uid);
456 EM_SAFE_FREE(new_server_uid);
460 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)
462 EM_DEBUG_FUNC_BEGIN();
463 MAILSTREAM *stream = NULL;
464 int err_code = EMAIL_ERROR_NONE;
465 email_account_t *ref_account = NULL;
468 email_id_set_t *id_set = NULL;
469 int id_set_count = 0;
471 email_uid_range_set *uid_range_set = NULL;
472 int len_of_each_range = 0;
474 email_uid_range_set *uid_range_node = NULL;
476 char **string_list = NULL;
477 int string_count = 0;
478 emstorage_mailbox_tbl_t* dest_mailbox = NULL;
480 if (num <= 0 || account_id <= 0 || src_mailbox_id <= 0 || dest_mailbox_id <= 0 || NULL == mail_ids) {
481 if (error_code != NULL) {
482 *error_code = EMAIL_ERROR_INVALID_PARAM;
484 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);
488 ref_account = emcore_get_account_reference(account_id);
490 if (NULL == ref_account) {
491 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
493 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
497 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
498 *error_code = EMAIL_ERROR_INVALID_PARAM;
503 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) {
504 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
509 if (NULL != stream) {
510 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
511 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
512 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
513 /* 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 */
514 /* 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 */
515 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
517 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code)) {
518 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
522 if ( (err_code = emstorage_get_mailbox_by_id(dest_mailbox_id, &dest_mailbox)) != EMAIL_ERROR_NONE || !dest_mailbox) {
523 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err_code);
527 /* Now execute one by one each comma separated string of mail_ids */
529 for (i = 0; i < string_count; ++i) {
530 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
532 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
533 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
537 /* 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 */
539 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
541 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
542 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
546 uid_range_node = uid_range_set;
548 while (uid_range_node != NULL) {
549 /* Remove comma from end of uid_range */
550 uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
551 EM_DEBUG_LOG("uid_range_node->uid_range - %s", uid_range_node->uid_range);
552 if (!mail_copy_full(stream, uid_range_node->uid_range, dest_mailbox->mailbox_name, CP_UID | CP_MOVE)) {
553 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be moved failed");
554 EM_DEBUG_EXCEPTION("Mail MOVE failed ");
557 else if (!mail_expunge_full(stream, uid_range_node->uid_range, EX_UID)) {
558 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be expunged after move. failed!");
559 EM_DEBUG_EXCEPTION("Mail Expunge after move failed ");
563 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
566 uid_range_node = uid_range_node->next;
569 emcore_free_uid_range_set(&uid_range_set);
571 EM_SAFE_FREE(id_set);
578 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
586 emcore_close_mailbox(0, stream);
590 emcore_free_account(ref_account);
591 EM_SAFE_FREE(ref_account);
594 #ifdef __FEATURE_LOCAL_ACTIVITY__
595 if (ret || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
596 emstorage_activity_tbl_t new_activity;
597 for (i = 0; i<num ; i++) {
598 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
599 new_activity.activity_type = ACTIVITY_MOVEMAIL;
600 new_activity.account_id = account_id;
601 new_activity.mail_id = mail_ids[i];
602 new_activity.src_mbox = src_mailbox;
603 new_activity.dest_mbox = dest_mailbox;
605 if (!emcore_delete_activity(&new_activity, &err_code)) {
606 EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
613 emstorage_free_mailbox(&dest_mailbox, 1, &err_code);
616 if (error_code != NULL) {
617 *error_code = err_code;
623 int emcore_delete_mails_from_imap4_server(int mail_ids[], int num, int from_server, int *err_code)
625 EM_DEBUG_FUNC_BEGIN();
628 int err = EMAIL_ERROR_NONE;
629 IMAPLOCAL *imaplocal = NULL;
631 MAILSTREAM *stream = NULL;
632 char tag[MAX_TAG_SIZE];
633 char cmd[MAX_IMAP_COMMAND_LENGTH];
634 email_id_set_t *id_set = NULL;
635 int id_set_count = 0;
637 email_uid_range_set *uid_range_set = NULL;
638 int len_of_each_range = 0;
639 email_uid_range_set *uid_range_node = NULL;
640 char **string_list = NULL;
641 int string_count = 0;
642 int delete_success = false;
643 emstorage_mail_tbl_t *mail_tbl_data = NULL;
645 if (num <= 0 || !mail_ids) {
646 EM_DEBUG_EXCEPTION(" Invalid parameter ");
647 err = EMAIL_ERROR_INVALID_PARAM;
651 for(i = 0; i < num; i++) {
652 if (!emstorage_get_downloaded_mail(mail_ids[i], &mail_tbl_data, false, &err)) {
653 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
655 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
663 if (!mail_tbl_data || mail_tbl_data->account_id <= 0 || mail_tbl_data->mailbox_id <= 0) {
664 EM_DEBUG_EXCEPTION("mail_tbl_data [%p]", mail_tbl_data);
668 if (!emcore_connect_to_remote_mailbox(mail_tbl_data->account_id, mail_tbl_data->mailbox_id , (void **)&stream, &err)) {
669 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
673 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
674 /* 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 */
675 /* 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 */
676 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
678 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
679 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
683 /* Now execute one by one each comma separated string of mail_ids */
685 for (i = 0; i < string_count; ++i) {
686 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
688 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
689 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
693 /* 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 */
695 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
697 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
698 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
702 uid_range_node = uid_range_set;
704 while (uid_range_node != NULL) {
705 /* Remove comma from end of uid_range */
707 uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
709 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
710 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
712 err = EMAIL_ERROR_CONNECTION_BROKEN;
716 memset(tag, 0x00, sizeof(tag));
717 memset(cmd, 0x00, sizeof(cmd));
719 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
720 SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
722 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
725 /* send command : set deleted flag */
726 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
727 EM_DEBUG_EXCEPTION("net_sout failed...");
729 err = EMAIL_ERROR_CONNECTION_BROKEN; /* EMAIL_ERROR_UNKNOWN */
734 while (imaplocal->netstream) {
735 /* receive response */
736 if (!(p = net_getline(imaplocal->netstream))) {
737 EM_DEBUG_EXCEPTION("net_getline failed...");
739 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
744 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
746 /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
747 /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl
748 if (strstr(p, "FETCH") != NULL) {
749 EM_DEBUG_LOG(" FETCH Response recieved ");
750 delete_success = true;
757 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
758 if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
759 /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
760 EM_DEBUG_LOG(" OK Response recieved ");
761 delete_success = true;
767 delete_success = false;
768 err = EMAIL_ERROR_IMAP4_STORE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
776 memset(tag, 0x00, sizeof(tag));
777 memset(cmd, 0x00, sizeof(cmd));
779 EM_DEBUG_LOG("Calling Expunge");
781 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
782 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
784 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
786 /* send command : EXPUNGE */
787 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
788 EM_DEBUG_EXCEPTION("net_sout failed...");
790 err = EMAIL_ERROR_CONNECTION_BROKEN;
794 while (imaplocal->netstream) {
795 /* receive response */
796 if (!(p = net_getline(imaplocal->netstream))) {
797 EM_DEBUG_EXCEPTION("net_getline failed...");
799 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
803 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
805 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
806 if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
807 #ifdef __FEATURE_LOCAL_ACTIVITY__
809 emstorage_mail_tbl_t **mail = NULL;
811 mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
813 EM_DEBUG_EXCEPTION("em_malloc failed");
814 err = EMAIL_ERROR_UNKNOWN;
818 if (delete_success) {
819 for (index = 0 ; index < num; index++) {
820 if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err)) {
821 EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
823 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
824 EM_DEBUG_LOG("EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER : ");
830 /* Clear entry from mail_read_mail_uid_tbl */
831 if (mail[index]->server_mail_id != NULL) {
832 if (!emstorage_remove_downloaded_mail(mail[index]->account_id, mail[index]->mailbox_name, mail[index]->server_mail_id, true, &err)) {
833 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
837 /* Delete local activity */
838 emstorage_activity_tbl_t new_activity;
839 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
840 if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
841 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
842 EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
845 new_activity.activity_type = ACTIVITY_DELETEMAIL;
848 new_activity.mail_id = mail[index]->mail_id;
849 new_activity.server_mailid = NULL;
850 new_activity.src_mbox = NULL;
851 new_activity.dest_mbox = NULL;
853 if (!emcore_delete_activity(&new_activity, &err)) {
854 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
858 /* 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.
859 * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted.
861 /* Delete local activity */
862 emstorage_activity_tbl_t new_activity;
863 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
864 new_activity.activity_type = ACTIVITY_DELETEMAIL;
865 new_activity.mail_id = mail_ids[index]; /* valid mail id passed for outbox mails*/
866 new_activity.server_mailid = NULL;
867 new_activity.src_mbox = NULL;
868 new_activity.dest_mbox = NULL;
870 if (!emcore_delete_activity(&new_activity, &err)) {
871 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
876 for (index = 0; index < num; index++) {
877 if (!emstorage_free_mail(&mail[index], 1, &err)) {
878 EM_DEBUG_EXCEPTION(" emstorage_free_mail [%d]", err);
889 else { /* 'NO' or 'BAD' */
890 err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
897 uid_range_node = uid_range_node->next;
900 emcore_free_uid_range_set(&uid_range_set);
902 EM_SAFE_FREE(id_set);
912 emcore_free_comma_separated_strings(&string_list, &string_count);
915 emcore_free_uid_range_set(&uid_range_set);
932 static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
934 EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
937 int err = EMAIL_ERROR_NONE;
939 IMAPLOCAL *imaplocal = NULL;
940 char tag[16], cmd[64];
943 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
944 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
946 err = EMAIL_ERROR_INVALID_PARAM; /* EMAIL_ERROR_UNKNOWN */
950 memset(tag, 0x00, sizeof(tag));
951 memset(cmd, 0x00, sizeof(cmd));
953 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
954 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
956 #ifdef FEATURE_CORE_DEBUG
957 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
960 /* send command : delete flaged mail */
961 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
962 EM_DEBUG_EXCEPTION("net_sout failed...");
964 err = EMAIL_ERROR_CONNECTION_BROKEN;
968 while (imaplocal->netstream) {
969 /* receive response */
970 if (!(p = net_getline(imaplocal->netstream))) {
971 EM_DEBUG_EXCEPTION("net_getline failed...");
973 err = EMAIL_ERROR_INVALID_RESPONSE; /* EMAIL_ERROR_UNKNOWN; */
977 #ifdef FEATURE_CORE_DEBUG
978 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
981 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
982 if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
986 else { /* 'NO' or 'BAD' */
987 err = EMAIL_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMAIL_ERROR_INVALID_RESPONSE; */
1006 int emcore_get_mail_contact_info(email_mail_contact_info_t *contact_info, char *full_address, int *err_code)
1008 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
1011 int err = EMAIL_ERROR_NONE;
1013 if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
1014 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
1018 if (err_code != NULL)
1024 int emcore_search_contact_info_by_address(const char *contact_uri, int property_id, char *address, int limit, contacts_record_h *contacts_record)
1026 EM_DEBUG_FUNC_BEGIN();
1027 int contact_err = CONTACTS_ERROR_NONE;
1029 contacts_query_h query = NULL;
1030 contacts_filter_h filter = NULL;
1031 contacts_list_h list = NULL;
1033 if ((contact_err = contacts_query_create(contact_uri, &query)) != CONTACTS_ERROR_NONE) {
1034 EM_DEBUG_EXCEPTION("contacts_query_create failed");
1038 if ((contact_err = contacts_filter_create(contact_uri, &filter)) != CONTACTS_ERROR_NONE) {
1039 EM_DEBUG_EXCEPTION("contacts_filter_create failed");
1043 if ((contact_err = contacts_filter_add_str(filter, property_id, CONTACTS_MATCH_EXACTLY, address)) != CONTACTS_ERROR_NONE) {
1044 EM_DEBUG_EXCEPTION("contacts_filter_add_str failed");
1048 if ((contact_err = contacts_query_set_filter(query, filter)) != CONTACTS_ERROR_NONE) {
1049 EM_DEBUG_EXCEPTION("contacts_query_set_filter failed");
1053 if ((contact_err = contacts_db_get_records_with_query(query, 0, limit, &list)) != CONTACTS_ERROR_NONE) {
1054 EM_DEBUG_EXCEPTION("contacts_db_get_record_with_query failed");
1058 if ((contact_err = contacts_list_get_current_record_p(list, contacts_record)) != CONTACTS_ERROR_NONE) {
1059 EM_DEBUG_EXCEPTION("contacts_list_get_current_record_p failed");
1066 contacts_query_destroy(query);
1069 contacts_filter_destroy(filter);
1072 contacts_list_destroy(list, false);
1077 int emcore_set_contacts_log(int account_id, char *email_address, char *subject, time_t date_time, email_action_t action)
1079 EM_DEBUG_FUNC_BEGIN("account_id : [%d], address : [%p], subject : [%s], action : [%d], date_time : [%d]", account_id, email_address, subject, action, (int)date_time);
1081 int err = EMAIL_ERROR_NONE;
1082 int contacts_error = CONTACTS_ERROR_NONE;
1084 int action_type = 0;
1086 contacts_record_h phone_record = NULL;
1087 contacts_record_h person_record = NULL;
1089 if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
1090 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1094 if ((contacts_error = contacts_record_create(_contacts_phone_log._uri, &phone_record)) != CONTACTS_ERROR_NONE) {
1095 EM_DEBUG_EXCEPTION("contacts_query_create failed [%d]", contacts_error);
1099 /* Set email address */
1100 if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.address, email_address)) != CONTACTS_ERROR_NONE) {
1101 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
1105 /* Search contact person info */
1106 if ((contacts_error = emcore_search_contact_info_by_address(_contacts_person_email._uri, _contacts_person_email.email, email_address, 1, &person_record)) != CONTACTS_ERROR_NONE) {
1107 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed [%d]", contacts_error);
1108 EM_DEBUG_LOG("Not match person");
1110 /* Get person_id in contacts_person_email record */
1111 if (person_record && (contacts_error = contacts_record_get_int(person_record, _contacts_person_email.person_id, &person_id)) != CONTACTS_ERROR_NONE) {
1112 EM_DEBUG_EXCEPTION("contacts_record_get_str failed [%d]", contacts_error);
1116 /* Set the person id */
1117 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.person_id, person_id)) != CONTACTS_ERROR_NONE) {
1118 EM_DEBUG_EXCEPTION("contacts_record_set_int [person id] failed [%d]", contacts_error);
1124 case EMAIL_ACTION_SEND_MAIL :
1125 action_type = CONTACTS_PLOG_TYPE_EMAIL_SENT;
1127 case EMAIL_ACTION_SYNC_HEADER :
1128 action_type = CONTACTS_PLOG_TYPE_EMAIL_RECEIVED;
1131 EM_DEBUG_EXCEPTION("Unknown action type");
1136 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_type, action_type)) != CONTACTS_ERROR_NONE) {
1137 EM_DEBUG_EXCEPTION("contacts_record_set_int [log_type] failed [%d]", contacts_error);
1142 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.log_time, (int)date_time)) != CONTACTS_ERROR_NONE) {
1143 EM_DEBUG_EXCEPTION("contacts_record_set_str [address] failed [%d]", contacts_error);
1148 if ((contacts_error = contacts_record_set_str(phone_record, _contacts_phone_log.extra_data2, subject)) != CONTACTS_ERROR_NONE) {
1149 EM_DEBUG_EXCEPTION("contacts_record_set_str [subject] failed [%d]", contacts_error);
1154 if ((contacts_error = contacts_record_set_int(phone_record, _contacts_phone_log.extra_data1, account_id)) != CONTACTS_ERROR_NONE) {
1155 EM_DEBUG_EXCEPTION("contacts_record_set_int [mail id] failed [%d]", contacts_error);
1159 /* Insert the record in DB */
1160 if ((contacts_error = contacts_db_insert_record(phone_record, NULL)) != CONTACTS_ERROR_NONE) {
1161 EM_DEBUG_EXCEPTION("contacts_db_insert_record failed [%d]",contacts_error );
1167 if (phone_record != NULL)
1168 contacts_record_destroy(phone_record, false);
1170 if (person_record != NULL)
1171 contacts_record_destroy(person_record, false);
1173 err = convert_contact_err_to_email_err(contacts_error);
1175 if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
1176 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1177 err = convert_contact_err_to_email_err(contacts_error);
1180 EM_DEBUG_FUNC_END("err [%d]", err);
1184 INTERNAL_FUNC int emcore_set_sent_contacts_log(emstorage_mail_tbl_t *input_mail_data)
1186 EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1189 int err = EMAIL_ERROR_NONE;
1190 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1191 char *address_array[3] = {input_mail_data->full_address_to, input_mail_data->full_address_cc, input_mail_data->full_address_bcc};
1192 ADDRESS *addr = NULL;
1193 ADDRESS *p_addr = NULL;
1195 for (i = 0; i < 3; i++) {
1196 if (address_array[i] && address_array[i][0] != 0) {
1197 rfc822_parse_adrlist(&addr, address_array[i], NULL);
1198 for (p_addr = addr ; p_addr ;p_addr = p_addr->next) {
1199 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox, addr->host);
1200 if ((err = emcore_set_contacts_log(input_mail_data->account_id, email_address, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SEND_MAIL)) != EMAIL_ERROR_NONE) {
1201 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed : [%d]", err);
1207 mail_free_address(&addr);
1216 mail_free_address(&addr);
1218 EM_DEBUG_FUNC_END("err [%d]", err);
1222 INTERNAL_FUNC int emcore_set_received_contacts_log(emstorage_mail_tbl_t *input_mail_data)
1224 EM_DEBUG_FUNC_BEGIN("input_mail_data : [%p]", input_mail_data);
1225 int err = EMAIL_ERROR_NONE;
1227 if ((err = emcore_set_contacts_log(input_mail_data->account_id, input_mail_data->email_address_sender, input_mail_data->subject, input_mail_data->date_time, EMAIL_ACTION_SYNC_HEADER)) != EMAIL_ERROR_NONE) {
1228 EM_DEBUG_EXCEPTION("emcore_set_contacts_log failed [%d]", err);
1231 EM_DEBUG_FUNC_END("err [%d]", err);
1235 INTERNAL_FUNC int emcore_delete_contacts_log(int account_id)
1237 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
1239 int err = EMAIL_ERROR_NONE;
1240 int contacts_error = CONTACTS_ERROR_NONE;
1242 if ((contacts_error = contacts_connect2()) != CONTACTS_ERROR_NONE) {
1243 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1247 /* Delete record of the account id */
1248 if ((contacts_error = contacts_phone_log_delete(CONTACTS_PHONE_LOG_DELETE_BY_EMAIL_EXTRA_DATA1, account_id)) != CONTACTS_ERROR_NONE) {
1249 EM_DEBUG_EXCEPTION("contacts_phone_log_delete failed [%d]", contacts_error);
1254 err = convert_contact_err_to_email_err(contacts_error);
1256 if ((contacts_error = contacts_disconnect2()) != CONTACTS_ERROR_NONE) {
1257 EM_DEBUG_EXCEPTION("Open connect service failed [%d]", contacts_error);
1258 err = convert_contact_err_to_email_err(contacts_error);
1261 EM_DEBUG_FUNC_END("err [%d]", err);
1265 INTERNAL_FUNC int emcore_get_mail_display_name(char *email_address, char **contact_display_name, int *err_code)
1267 EM_DEBUG_FUNC_BEGIN("contact_name_value[%s], contact_display_name[%p]", email_address, contact_display_name);
1269 int contact_err = 0;
1271 char *display = NULL;
1272 /* Contact variable */
1273 contacts_record_h record = NULL;
1275 if ((contact_err = emcore_search_contact_info_by_address(_contacts_contact_email._uri, _contacts_contact_email.email, email_address, 1, &record)) != CONTACTS_ERROR_NONE) {
1276 EM_DEBUG_EXCEPTION("emcore_search_contact_info_by_address failed");
1280 if ((contact_err = contacts_list_get_count(list, &list_count)) != CONTACTS_ERROR_NONE) {
1281 EM_DEBUG_EXCEPTION("contacts_list_get_count failed");
1285 if (list_count > 1) {
1286 EM_DEBUG_EXCEPTION("Duplicated contacts info");
1287 contact_err = CONTACTS_ERROR_INTERNAL;
1289 } else if (list_count == 0) {
1290 EM_DEBUG_EXCEPTION("Not found contact info");
1291 contact_err = CONTACTS_ERROR_NO_DATA;
1296 if (contacts_record_get_str(record, _contacts_contact_email.display_name, &display) != CONTACTS_ERROR_NONE) {
1297 EM_DEBUG_EXCEPTION("contacts_record_get_str failed");
1306 contacts_record_destroy(record, false);
1309 *contact_display_name = display;
1311 *contact_display_name = NULL;
1312 EM_SAFE_FREE(display);
1315 if (err_code != NULL)
1316 *err_code = convert_contact_err_to_email_err(contact_err);
1321 int emcore_get_mail_contact_info_with_update(email_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
1323 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
1326 int err = EMAIL_ERROR_NONE;
1327 ADDRESS *addr = NULL;
1328 char *address = NULL;
1329 char *temp_emailaddr = NULL;
1331 int is_searched = false;
1332 int address_length = 0;
1333 char *email_address = NULL;
1334 int contact_name_len = 0;
1335 char temp_string[1024] = { 0 , };
1337 char *contact_display_name = NULL;
1338 char *contact_display_name_from_contact_info = NULL;
1339 int contact_display_name_len = 0;
1341 int contact_name_buffer_size = 0;
1342 char *contact_name = NULL;
1344 if (!contact_info) {
1345 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1346 err = EMAIL_ERROR_INVALID_PARAM;
1350 if (!full_address) {
1353 temp_emailaddr = NULL;
1356 address_length = 2 * EM_SAFE_STRLEN(full_address);
1357 temp_emailaddr = (char *)calloc(1, address_length);
1360 em_skip_whitespace(full_address , &address);
1361 EM_DEBUG_LOG("address[address][%s]", address);
1365 gchar **tokens = g_strsplit(address, ", ", -1);
1366 char *p = g_strjoinv("%2C", tokens);
1371 while (p && p[i] != '\0')
1377 EM_DEBUG_LOG(" 2 converted address %s ", p);
1379 rfc822_parse_adrlist(&addr, p, NULL);
1382 EM_DEBUG_LOG(" 3 full_address %s ", full_address);
1385 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1386 err = EMAIL_ERROR_INVALID_ADDRESS;
1390 contact_name_buffer_size = address_length;
1391 contact_name = (char*)em_malloc(contact_name_buffer_size);
1393 if (!contact_name) {
1394 EM_DEBUG_EXCEPTION("Memory allocation error!");
1395 err = EMAIL_ERROR_OUT_OF_MEMORY;
1399 while (addr != NULL) {
1400 if (addr->mailbox && addr->host) {
1401 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.")))
1403 EM_DEBUG_LOG("Invalid address ");
1409 EM_DEBUG_LOG("Error in parsing..! ");
1414 EM_SAFE_FREE(email_address);
1415 email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1417 EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
1418 EM_DEBUG_LOG(" email_address[%s]", email_address);
1420 is_searched = false;
1421 EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
1423 if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &err) && err == EMAIL_ERROR_NONE) {
1424 contact_display_name = contact_display_name_from_contact_info;
1426 EM_DEBUG_LOG(">>> contact_name[%s]", contact_display_name);
1427 /* Make display name string */
1430 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1431 contact_display_name_len = EM_SAFE_STRLEN(contact_display_name);
1432 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1433 char *temp = contact_name;
1434 contact_name_buffer_size += contact_name_buffer_size;
1435 contact_name = (char *)calloc(1, contact_name_buffer_size);
1436 if (contact_name == NULL) {
1437 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1441 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1445 /* 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); */
1446 if (addr->next == NULL) {
1447 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1450 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1453 contact_display_name_len = EM_SAFE_STRLEN(temp_string);
1454 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1455 char *temp = contact_name;
1456 contact_name_buffer_size += contact_name_buffer_size;
1457 contact_name = (char *)calloc(1, contact_name_buffer_size);
1458 if (contact_name == NULL) {
1459 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1461 err = EMAIL_ERROR_OUT_OF_MEMORY;
1464 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1467 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1468 contact_name_len += contact_display_name_len;
1469 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1473 EM_DEBUG_LOG("emcore_get_mail_display_name - Not found contact record(if err is 203) or error [%d]", err);
1476 /* if contact doesn't exist, use alias or email address as display name */
1477 if (addr->personal != NULL) {
1479 tokens = g_strsplit(addr->personal, "%2C", -1);
1481 EM_SAFE_FREE(addr->personal);
1483 addr->personal = g_strjoinv(", ", tokens);
1486 /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
1487 alias = addr->personal;
1490 /* alias = addr->mailbox ? addr->mailbox : ""; */
1491 alias = email_address;
1493 contact_info->alias = EM_SAFE_STRDUP(alias);
1496 contact_display_name = alias;
1497 contact_info->contact_id = -1; /* NOTE : This is valid only if there is only one address. */
1498 contact_info->storage_type = -1;
1500 /* Make display name string */
1501 EM_DEBUG_LOG("contact_display_name : [%s]", contact_display_name);
1502 EM_DEBUG_LOG("email_address : [%s]", email_address);
1504 /* if mail_id is 0, return only contact info without saving contact info to DB */
1505 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1506 /* 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); */
1507 if (addr->next == NULL) {
1508 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1511 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1513 EM_DEBUG_LOG("temp_string[%s]", temp_string);
1515 contact_display_name_len = EM_SAFE_STRLEN(temp_string);
1516 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1517 char *temp = contact_name;
1518 contact_name_buffer_size += contact_name_buffer_size;
1519 contact_name = (char *)calloc(1, contact_name_buffer_size);
1520 if (contact_name == NULL) {
1521 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1523 err = EMAIL_ERROR_OUT_OF_MEMORY;
1526 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1530 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1531 contact_name_len += contact_display_name_len;
1532 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1536 if (temp_emailaddr && email_address) {
1537 if (mail_id == 0) { /* if mail_id is 0, return only contact info without saving contact info to DB */
1538 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1539 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1540 if (addr->next != NULL)
1541 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1542 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1544 else { /* save only the first address information - 09-SEP-2010 */
1545 if (is_saved == 0) {
1547 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1548 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1550 if (addr->next != NULL)
1551 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1553 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1558 EM_SAFE_FREE(contact_display_name_from_contact_info);
1561 } /* while (addr != NULL) */
1563 if (temp_emailaddr) {
1564 EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
1565 contact_info->email_address = temp_emailaddr;
1566 temp_emailaddr = NULL;
1570 contact_info->contact_name = g_strdup(contact_name); /*prevent 40020*/
1576 EM_SAFE_FREE(email_address);
1577 EM_SAFE_FREE(address);
1578 EM_SAFE_FREE(temp_emailaddr);
1579 EM_SAFE_FREE(contact_name);
1580 EM_SAFE_FREE(contact_display_name_from_contact_info);
1582 if (err_code != NULL)
1588 int emcore_free_contact_info(email_mail_contact_info_t *contact_info, int *err_code)
1590 EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
1593 int err = EMAIL_ERROR_NONE;
1595 if (!contact_info) {
1596 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1597 err = EMAIL_ERROR_INVALID_PARAM;
1601 EM_SAFE_FREE(contact_info->contact_name);
1602 EM_SAFE_FREE(contact_info->email_address);
1603 EM_SAFE_FREE(contact_info->alias);
1605 contact_info->storage_type = -1;
1606 contact_info->contact_id = -1;
1611 if (err_code != NULL)
1613 EM_DEBUG_FUNC_END();
1617 int emcore_sync_contact_info(int mail_id, int *err_code)
1619 EM_PROFILE_BEGIN(emCoreMailContactSync);
1620 EM_DEBUG_FUNC_BEGIN();
1623 int err = EMAIL_ERROR_NONE;
1625 emstorage_mail_tbl_t *mail = NULL;
1627 email_mail_contact_info_t contact_info_from;
1628 email_mail_contact_info_t contact_info_to;
1629 email_mail_contact_info_t contact_info_cc;
1630 email_mail_contact_info_t contact_info_bcc;
1632 EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
1634 memset(&contact_info_from, 0x00, sizeof(email_mail_contact_info_t));
1635 memset(&contact_info_to, 0x00, sizeof(email_mail_contact_info_t));
1636 memset(&contact_info_cc, 0x00, sizeof(email_mail_contact_info_t));
1637 memset(&contact_info_bcc, 0x00, sizeof(email_mail_contact_info_t));
1639 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1640 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1645 if (mail->full_address_from != NULL) {
1646 if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err)) {
1647 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1651 if (mail->full_address_to != NULL) {
1652 if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err)) {
1653 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1657 if (mail->full_address_cc != NULL) {
1658 if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err)) {
1659 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1663 if (mail->full_address_bcc != NULL) {
1664 if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err)) {
1665 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1669 EM_SAFE_FREE(mail->email_address_sender);
1670 mail->email_address_sender = contact_info_from.email_address;
1671 contact_info_from.contact_name = NULL;
1672 contact_info_from.email_address = NULL;
1673 EM_SAFE_FREE(mail->email_address_recipient);
1674 if (mail->full_address_to != NULL) {
1675 mail->email_address_recipient = contact_info_to.email_address;
1676 contact_info_to.contact_name = NULL;
1677 contact_info_to.email_address = NULL;
1679 else if (mail->full_address_cc != NULL) {
1680 mail->email_address_recipient = contact_info_cc.email_address;
1681 contact_info_cc.contact_name = NULL;
1682 contact_info_cc.email_address = NULL;
1684 else if (mail->full_address_bcc != NULL) {
1685 mail->email_address_recipient = contact_info_bcc.email_address;
1686 contact_info_bcc.contact_name = NULL;
1687 contact_info_bcc.email_address = NULL;
1691 if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err)) {
1692 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1701 emstorage_free_mail(&mail, 1, NULL);
1703 emcore_free_contact_info(&contact_info_from, NULL);
1704 emcore_free_contact_info(&contact_info_to, NULL);
1705 emcore_free_contact_info(&contact_info_cc, NULL);
1706 emcore_free_contact_info(&contact_info_bcc, NULL);
1708 if (err_code != NULL)
1711 EM_PROFILE_END(emCoreMailContactSync);
1715 /* 1. parsing : alias and address */
1716 /* 2. sync with contact */
1717 /* 3. make glist of address info */
1718 static int emcore_sync_address_info(email_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
1720 EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
1723 int error = EMAIL_ERROR_NONE;
1724 int contact_index = -1;
1725 int is_search = false;
1727 char *address = NULL;
1728 char *contact_display_name_from_contact_info = NULL;
1729 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1730 email_address_info_t *p_address_info = NULL;
1731 ADDRESS *addr = NULL;
1733 if (address_info_list == NULL) {
1734 EM_DEBUG_EXCEPTION("Invalid param : address_info_list is NULL");
1735 error = EMAIL_ERROR_INVALID_PARAM;
1740 address = EM_SAFE_STRDUP(full_address);
1743 gchar **tokens = g_strsplit(address, ", ", -1);
1744 char *p = g_strjoinv("%2C", tokens);
1750 while (p && p[i] != '\0') {
1756 rfc822_parse_adrlist(&addr, p, NULL);
1761 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1762 error = EMAIL_ERROR_INVALID_PARAM;
1766 /* Get a contact name */
1767 while (addr != NULL) {
1768 if (addr->mailbox && addr->host) {
1769 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1770 EM_DEBUG_LOG("Invalid address ");
1776 EM_DEBUG_LOG("Error in parsing..! ");
1781 /* set display name */
1782 /* 1) contact name */
1783 /* 2) alias (if a alias in an original mail doesn't exist, this field is set with email address */
1784 /* 3) email address */
1786 if (!(p_address_info = (email_address_info_t *)malloc(sizeof(email_address_info_t)))) {
1787 EM_DEBUG_EXCEPTION("malloc failed...");
1788 error = EMAIL_ERROR_OUT_OF_MEMORY;
1791 memset(p_address_info, 0x00, sizeof(email_address_info_t));
1793 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1795 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1799 if (emcore_get_mail_display_name(email_address, &contact_display_name_from_contact_info, &error) && error == EMAIL_ERROR_NONE) {
1800 EM_DEBUG_LOG(">>> contact display name[%s]", contact_display_name_from_contact_info);
1805 EM_DEBUG_EXCEPTION("emcore_get_mail_display_name - Not found contact record(if err is -203) or error [%d]", error);
1807 if (is_search == true) {
1808 p_address_info->contact_id = contact_index;
1809 p_address_info->storage_type = -1;
1810 p_address_info->display_name = contact_display_name_from_contact_info;
1811 EM_DEBUG_LOG("display_name from contact[%s]", p_address_info->display_name);
1814 /* if contact doesn't exist, use alias or email address as display name */
1815 if (addr->personal != NULL) {
1817 tokens = g_strsplit(addr->personal, "%2C", -1);
1819 EM_SAFE_FREE(addr->personal);
1821 addr->personal = g_strjoinv(", ", tokens);
1824 alias = addr->personal;
1829 p_address_info->contact_id = -1;
1830 p_address_info->storage_type = -1;
1831 /* Use an alias or an email address as a display name */
1833 p_address_info->display_name = EM_SAFE_STRDUP(email_address);
1835 p_address_info->display_name = EM_SAFE_STRDUP(alias);
1837 EM_DEBUG_LOG("display_name from email [%s]", p_address_info->display_name);
1840 p_address_info->address = EM_SAFE_STRDUP(email_address);
1841 p_address_info->address_type = address_type;
1843 EM_DEBUG_LOG("email address[%s]", p_address_info->address);
1845 *address_info_list = g_list_append(*address_info_list, p_address_info);
1846 p_address_info = NULL;
1848 EM_DEBUG_LOG("after append");
1852 EM_DEBUG_LOG("next address[%p]", addr->next);
1862 EM_SAFE_FREE(address);
1864 if (err_code != NULL)
1866 EM_DEBUG_FUNC_END();
1870 static gint address_compare(gconstpointer a, gconstpointer b)
1872 EM_DEBUG_FUNC_BEGIN();
1873 email_sender_list_t *recipients_list1 = (email_sender_list_t *)a;
1874 email_sender_list_t *recipients_list2 = (email_sender_list_t *)b;
1876 EM_DEBUG_FUNC_END();
1877 return strcmp(recipients_list1->address, recipients_list2->address);
1880 INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
1882 EM_DEBUG_FUNC_BEGIN();
1884 int i = 0, err = EMAIL_ERROR_NONE;
1885 int is_search = false;
1886 char *address = NULL;
1887 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1888 char *display_name = NULL;
1890 ADDRESS *addr = NULL;
1891 GList *new_recipients_list = old_recipients_list;
1892 GList *recipients_list;
1894 email_sender_list_t *temp_recipients_list = NULL;
1895 email_sender_list_t *old_recipients_list_t = NULL;
1897 if (full_address == NULL || EM_SAFE_STRLEN(full_address) == 0) {
1898 EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
1899 err = EMAIL_ERROR_INVALID_PARAM;
1903 address = EM_SAFE_STRDUP(full_address);
1905 gchar **tokens = g_strsplit(address, ", ", -1);
1906 char *p = g_strjoinv("%2C", tokens);
1910 while (p && p[i] != '\0') {
1916 rfc822_parse_adrlist(&addr, p, NULL);
1921 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1922 err = EMAIL_ERROR_INVALID_PARAM;
1926 while (addr != NULL) {
1927 if (addr->mailbox && addr->host) {
1928 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1929 EM_DEBUG_LOG("Invalid address ");
1934 EM_DEBUG_LOG("Error in parsing..! ");
1939 if ((temp_recipients_list = g_new0(email_sender_list_t, 1)) == NULL) {
1940 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
1941 err = EMAIL_ERROR_OUT_OF_MEMORY;
1946 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1948 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1950 if (emcore_get_mail_display_name(email_address, &display_name, &err) && err == EMAIL_ERROR_NONE) {
1951 EM_DEBUG_LOG(">>> contact display name[%s]", display_name);
1954 EM_DEBUG_LOG("emcore_get_mail_display_name - Not found contact record(if err is -203) or error [%d]", err);
1958 temp_recipients_list->display_name = display_name;
1959 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1961 if (addr->personal != NULL) {
1962 tokens = g_strsplit(addr->personal, "%2C", -1);
1963 EM_SAFE_FREE(addr->personal);
1964 addr->personal = g_strjoinv(", ", tokens);
1966 alias = addr->personal;
1972 temp_recipients_list->display_name = EM_SAFE_STRDUP(email_address);
1974 temp_recipients_list->display_name = EM_SAFE_STRDUP(alias);
1976 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1979 temp_recipients_list->address = EM_SAFE_STRDUP(email_address);
1980 EM_DEBUG_LOG("email address[%s]", temp_recipients_list->address);
1982 EM_SAFE_FREE(display_name);
1983 EM_DEBUG_LOG("next address[%p]", addr->next);
1985 recipients_list = g_list_first(new_recipients_list);
1986 while (recipients_list != NULL) {
1987 old_recipients_list_t = (email_sender_list_t *)recipients_list->data;
1988 if (!strcmp(old_recipients_list_t->address, temp_recipients_list->address)) {
1989 old_recipients_list_t->total_count = old_recipients_list_t->total_count + 1;
1990 g_free(temp_recipients_list);
1993 recipients_list = g_list_next(recipients_list);
1996 new_recipients_list = g_list_insert_sorted(new_recipients_list, temp_recipients_list, address_compare);
1998 g_free(temp_recipients_list);
1999 temp_recipients_list = NULL;
2007 EM_SAFE_FREE(address);
2009 if (err_code != NULL)
2012 EM_DEBUG_FUNC_END();
2013 return new_recipients_list;
2016 INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, email_address_info_list_t **address_info_list, int *err_code)
2018 EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
2020 int ret = false, err = EMAIL_ERROR_NONE;
2024 emstorage_mail_tbl_t *mail = NULL;
2025 email_address_info_list_t *p_address_info_list = NULL;
2027 if (mail_id <= 0 || !address_info_list) {
2028 EM_DEBUG_EXCEPTION("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
2029 err = EMAIL_ERROR_INVALID_PARAM;
2033 /* get mail from mail table */
2034 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2035 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2041 if (!(p_address_info_list = (email_address_info_list_t *)malloc(sizeof(email_address_info_list_t)))) {
2042 EM_DEBUG_EXCEPTION("malloc failed...");
2043 err = EMAIL_ERROR_OUT_OF_MEMORY;
2046 memset(p_address_info_list, 0x00, sizeof(email_address_info_list_t));
2048 if ((contact_error = contacts_connect2()) == CONTACTS_ERROR_NONE) {
2049 EM_DEBUG_LOG("Open Contact Service Success");
2052 EM_DEBUG_EXCEPTION("contact_db_service_connect failed [%d]", contact_error);
2053 err = EMAIL_ERROR_DB_FAILURE;
2057 if (mail->full_address_from && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
2059 if (mail->full_address_to && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
2061 if (mail->full_address_cc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
2063 if (mail->full_address_bcc && emcore_sync_address_info(EMAIL_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
2066 if ((contact_error = contacts_disconnect2()) == CONTACTS_ERROR_NONE)
2067 EM_DEBUG_LOG("Close Contact Service Success");
2069 EM_DEBUG_EXCEPTION("Close Contact Service Fail [%d]", contact_error);
2071 if (failed == false)
2076 *address_info_list = p_address_info_list;
2077 else if (p_address_info_list != NULL)
2078 emstorage_free_address_info_list(&p_address_info_list);
2080 emstorage_free_mail(&mail, 1, NULL);
2082 if (err_code != NULL)
2085 EM_DEBUG_FUNC_END();
2092 * input_mail_id : [in] mail id
2093 * output_mail_data : [out] double pointer to hold mail data.
2095 * succeed : EMAIL_ERROR_NONE
2098 INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, email_mail_data_t **output_mail_data)
2100 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
2102 int error = EMAIL_ERROR_NONE;
2103 int result_mail_count = 0;
2104 char conditional_clause_string[QUERY_SIZE] = { 0, };
2105 emstorage_mail_tbl_t *result_mail_tbl = NULL;
2107 if (input_mail_id == 0 || !output_mail_data) {
2108 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2109 error = EMAIL_ERROR_INVALID_PARAM;
2113 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
2115 if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
2116 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
2120 if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, 1, output_mail_data, &error)) {
2121 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
2126 if (result_mail_tbl)
2127 emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
2129 EM_DEBUG_FUNC_END("error [%d]", error);
2134 /* internal function */
2135 void emcore_free_body_sharep(void **p)
2137 EM_DEBUG_FUNC_BEGIN();
2139 EM_DEBUG_FUNC_END();
2142 int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
2144 EM_DEBUG_FUNC_BEGIN();
2146 #ifdef __FEATURE_DRM__
2147 drm_bool_type_e drm_file = DRM_UNKNOWN;
2148 drm_file_info_s drm_file_info;
2150 if (input_attachment_tb_data == NULL)
2153 ret = drm_is_drm_file(input_attachment_tb_data->attachment_path, &drm_file);
2155 if (ret == DRM_RETURN_SUCCESS && drm_file == DRM_TRUE) {
2156 if (drm_get_file_info (input_attachment_tb_data->attachment_path, &drm_file_info) == DRM_RETURN_SUCCESS) {
2157 input_attachment_tb_data->attachment_drm_type = 0;
2158 EM_DEBUG_LOG ("fileInfo is [%d]", drm_file_info.oma_info.method);
2159 if (drm_file_info.oma_info.method != DRM_METHOD_TYPE_UNDEFINED) {
2160 input_attachment_tb_data->attachment_drm_type = drm_file_info.oma_info.method;
2166 EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
2167 input_attachment_tb_data->attachment_drm_type = 0;
2171 EM_DEBUG_FUNC_END();
2177 * get mail attachment from local mailbox
2179 * mailbox : server mailbox
2180 * mail_id : mai id to own attachment
2181 * attachment : the number string to be downloaded
2182 * callback : function callback. if NULL, ignored.
2187 INTERNAL_FUNC int emcore_get_attachment_info(int attachment_id, email_attachment_data_t **attachment, int *err_code)
2189 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
2191 if (attachment == NULL || attachment_id == 0) {
2192 EM_DEBUG_EXCEPTION("attachment_id[%d], attachment[%p]", attachment_id, attachment);
2193 if (err_code != NULL)
2194 *err_code = EMAIL_ERROR_INVALID_PARAM;
2199 int err = EMAIL_ERROR_NONE;
2200 emstorage_attachment_tbl_t *attachment_tbl = NULL;
2202 /* get attachment from attachment tbl */
2203 if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &err) || !attachment_tbl) {
2204 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
2209 *attachment = em_malloc(sizeof(email_attachment_data_t));
2211 EM_DEBUG_EXCEPTION("malloc failed...");
2212 err = EMAIL_ERROR_OUT_OF_MEMORY;
2216 (*attachment)->attachment_id = attachment_id;
2217 (*attachment)->attachment_name = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
2218 (*attachment)->attachment_size = attachment_tbl->attachment_size;
2219 (*attachment)->save_status = attachment_tbl->attachment_save_status;
2220 (*attachment)->attachment_path = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
2221 (*attachment)->drm_status = attachment_tbl->attachment_drm_type;
2222 (*attachment)->inline_content_status = attachment_tbl->attachment_inline_content_status;
2228 emstorage_free_attachment(&attachment_tbl, 1, NULL);
2237 * get mail attachment
2239 * input_mail_id : mail id to own attachment
2240 * output_attachment_data : result attahchment data
2241 * output_attachment_count : result attahchment count
2243 * succeed : EMAIL_ERROR_NONE
2246 INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, email_attachment_data_t **output_attachment_data, int *output_attachment_count)
2248 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);
2250 if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL) {
2251 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2252 return EMAIL_ERROR_INVALID_PARAM;
2256 int err = EMAIL_ERROR_NONE;
2257 int attachment_tbl_count = 0;
2258 emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2259 email_attachment_data_t *temp_attachment_data = NULL;
2261 /* get attachment from attachment tbl */
2262 if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE ){
2263 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2268 if (attachment_tbl_count) {
2269 EM_DEBUG_LOG("attchment count %d", attachment_tbl_count);
2271 *output_attachment_data = em_malloc(sizeof(email_attachment_data_t) * attachment_tbl_count);
2273 if(*output_attachment_data == NULL) {
2274 EM_DEBUG_EXCEPTION("em_malloc failed");
2275 err = EMAIL_ERROR_OUT_OF_MEMORY;
2279 for (i = 0; i < attachment_tbl_count; i++) {
2280 temp_attachment_data = (*output_attachment_data) + i;
2282 temp_attachment_data->attachment_id = attachment_tbl_list[i].attachment_id;
2283 temp_attachment_data->attachment_name = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
2284 temp_attachment_data->attachment_path = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
2285 temp_attachment_data->attachment_size = attachment_tbl_list[i].attachment_size;
2286 temp_attachment_data->mail_id = attachment_tbl_list[i].mail_id;
2287 temp_attachment_data->account_id = attachment_tbl_list[i].account_id;
2288 temp_attachment_data->mailbox_id = attachment_tbl_list[i].mailbox_id;
2289 temp_attachment_data->save_status = attachment_tbl_list[i].attachment_save_status;
2290 temp_attachment_data->drm_status = attachment_tbl_list[i].attachment_drm_type;
2291 temp_attachment_data->inline_content_status = attachment_tbl_list[i].attachment_inline_content_status;
2297 *output_attachment_count = attachment_tbl_count;
2299 if (attachment_tbl_list)
2300 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2306 INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, int nth, int *err_code)
2308 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2310 int err = EMAIL_ERROR_NONE;
2313 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2314 err = EMAIL_ERROR_INVALID_PARAM;
2316 if (err_code != NULL)
2319 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, nth, err);
2324 int status = EMAIL_DOWNLOAD_FAIL;
2325 MAILSTREAM *stream = NULL;
2327 emstorage_mail_tbl_t *mail = NULL;
2328 emstorage_attachment_tbl_t *attachment = NULL;
2329 struct attachment_info *ai = NULL;
2330 struct _m_content_info *cnt_info = NULL;
2331 void *tmp_stream = NULL;
2332 char *s_uid = NULL, buf[1024];
2334 emstorage_attachment_tbl_t *attachment_list = NULL;
2335 int current_attachment_no = 0;
2336 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2338 int server_mbox_id = 0;
2340 if (!emcore_check_thread_status()) {
2341 err = EMAIL_ERROR_CANCELLED;
2345 only_body_download = false;
2347 /* get mail from mail table. */
2348 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2349 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2354 if (!mail->server_mail_status) {
2355 EM_DEBUG_EXCEPTION("not synchronous mail...");
2356 err = EMAIL_ERROR_INVALID_MAIL;
2360 if (nth == 0) { /* download all attachments, nth starts from 1, not zero */
2361 /* get attachment list from db */
2362 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2363 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE ){
2364 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2369 else { /* download only nth attachment */
2370 attachment_count_to_be_downloaded = 1;
2371 if (!emstorage_get_attachment_nth(mail_id, nth, &attachment_list, true, &err) || !attachment_list) {
2372 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2378 if (!emcore_check_thread_status()) {
2379 err = EMAIL_ERROR_CANCELLED;
2383 account_id = mail->account_id;
2384 s_uid = EM_SAFE_STRDUP(mail->server_mail_id);
2385 server_mbox_id = mail->mailbox_id;
2387 /* open mail server. */
2388 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
2389 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2390 if(err == EMAIL_ERROR_NO_SUCH_HOST)
2391 err = EMAIL_ERROR_CONNECTION_FAILURE;
2392 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2396 stream = (MAILSTREAM *)tmp_stream;
2398 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2399 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
2401 attachment = attachment_list + i;
2402 if (nth == 0) /* download all attachments, nth starts from 1, not zero */
2403 current_attachment_no = i + 1; /* attachment no */
2404 else /* download only nth attachment */
2405 current_attachment_no = nth; /* attachment no */
2407 if (!emcore_check_thread_status()) {
2408 err = EMAIL_ERROR_CANCELLED;
2412 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2413 EM_DEBUG_EXCEPTION("malloc failed...");
2414 err = EMAIL_ERROR_OUT_OF_MEMORY;
2418 cnt_info->grab_type = GRAB_TYPE_ATTACHMENT; /* attachment */
2419 cnt_info->file_no = current_attachment_no; /* attachment no */
2421 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2422 /* text/html be changed to attachment, this isn't real attachment in RFC822. */
2424 cnt_info->file_no--;
2427 /* set sparep(member of BODY) memory free function. */
2428 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2430 if (!emcore_check_thread_status()) {
2431 err = EMAIL_ERROR_CANCELLED;
2435 msg_no = s_uid? atoi(s_uid): 0;
2437 /* get body structure. */
2438 /* don't free mbody because mbody is freed in closing mail_stream. */
2439 if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0) {
2440 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2444 if (!emcore_check_thread_status()) {
2445 err = EMAIL_ERROR_CANCELLED;
2449 /* set body fetch section. */
2450 if (emcore_set_fetch_body_section(mbody, false, NULL, &err) < 0) {
2451 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2455 /* download attachment. */
2456 _imap4_received_body_size = 0;
2457 _imap4_last_notified_body_size = 0;
2458 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2459 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2461 EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
2462 if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0) {
2463 EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
2467 if (!emcore_check_thread_status()) {
2468 err = EMAIL_ERROR_CANCELLED;
2472 /* select target attachment information. */
2473 for (ai = cnt_info->file ; ai; ai = ai->next) {
2474 EM_DEBUG_LOG("[in loop] name[%s] save[%s] no[%d]", ai->save, ai->name, cnt_info->file_no);
2475 if (--cnt_info->file_no == 0)
2479 EM_DEBUG_LOG("selected cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
2481 if (cnt_info->file_no == 0 && ai) {
2482 /* rename temporary file to real file. */
2483 if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err)) {
2484 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2488 if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err)) {
2489 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2493 if (!emstorage_move_file(ai->save, buf, false, &err)) {
2494 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2495 err = EMAIL_ERROR_INVALID_ATTACHMENT_SAVE_NAME;
2499 EM_SAFE_FREE(ai->save);
2501 EM_DEBUG_LOG("ai->size [%d]", ai->size);
2502 attachment->attachment_size = ai->size;
2503 attachment->attachment_path = EM_SAFE_STRDUP(buf);
2505 /* update attachment information. */
2506 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2507 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2508 /* delete created file. */
2514 #ifdef __FEATURE_DRM__
2515 if (emcore_check_drm(attachment)) { /* has drm attachment ? */
2516 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, (void *)attachment->attachment_path, NULL) == DRM_RETURN_SUCCESS)
2517 EM_DEBUG_LOG("drm_svc_register_file success");
2519 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2520 mail->DRM_status = attachment->attachment_drm_type;
2522 #endif /* __FEATURE_DRM__ */
2525 EM_DEBUG_EXCEPTION("invalid attachment sequence...");
2526 err = EMAIL_ERROR_INVALID_ATTACHMENT;
2531 emcore_free_content_info(cnt_info);
2534 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2538 emcore_close_mailbox(0, stream);
2546 emcore_close_mailbox(account_id, stream);
2547 if (attachment_list)
2548 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2550 emcore_free_content_info(cnt_info);
2552 emstorage_free_mail(&mail, 1, NULL);
2554 EM_SAFE_FREE(s_uid);
2557 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, nth, 0);
2559 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, nth, err);
2561 if (err_code != NULL)
2564 EM_DEBUG_FUNC_END();
2568 #ifdef __ATTACHMENT_OPTI__
2569 INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, int nth, int *err_code)
2571 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], err_code[%p]", account_id, mail_id, nth, err_code);
2573 int err = EMAIL_ERROR_NONE; /* Prevent Defect - 25093 */
2575 int status = EMAIL_DOWNLOAD_FAIL;
2576 MAILSTREAM *stream = NULL;
2577 emstorage_mail_tbl_t *mail = NULL;
2578 emstorage_attachment_tbl_t *attachment = NULL;
2579 void *tmp_stream = NULL;
2580 char *s_uid = NULL, *server_mbox = NULL, buf[512];
2581 emstorage_attachment_tbl_t *attachment_list = NULL;
2582 int current_attachment_no = 0;
2583 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2585 char *savefile = NULL;
2588 #ifdef SUPPORT_EXTERNAL_MEMORY
2589 int iActualSize = 0;
2594 int bIsAdd_to_mmc = false;
2595 int is_on_mmc = false;
2596 email_file_list *pFileListMMc = NULL;
2597 email_file_list *pFileList = NULL;
2598 #endif /* SUPPORT_EXTERNAL_MEMORY */
2601 memset(buf, 0x00, 512);
2603 if (mail_id < 1 || !nth) {
2604 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
2606 err = EMAIL_ERROR_INVALID_PARAM;
2608 if (err_code != NULL)
2612 attachment_no = atoi(nth);
2614 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err); /* 090525, kwangryul.baek */
2619 if (!emcore_check_thread_status()) {
2620 err = EMAIL_ERROR_CANCELLED;
2623 only_body_download = false;
2625 attachment_no = atoi(nth);
2627 if (attachment_no == 0) {
2628 /* download all attachments, nth starts from 1, not zero */
2629 /* get attachment list from db */
2630 attachment_count_to_be_downloaded = EMAIL_ATTACHMENT_MAX_COUNT;
2631 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMAIL_ERROR_NONE || !attachment_list){
2632 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2637 else { /* download only nth attachment */
2638 attachment_count_to_be_downloaded = 1;
2639 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2640 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2648 if (!emcore_check_thread_status()) {
2649 err = EMAIL_ERROR_CANCELLED;
2654 /* get mail from mail table. */
2655 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2656 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2662 /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /* faizan.h@samsung.com */
2664 if (!emcore_check_thread_status()) {
2665 err = EMAIL_ERROR_CANCELLED;
2669 account_id = mail->account_id;
2670 s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
2671 server_mbox = EM_SAFE_STRDUP(mail->mailbox); mail->server_mailbox_name = NULL;
2675 /* open mail server. */
2676 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2677 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2679 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2683 stream = (MAILSTREAM *)tmp_stream;
2686 if (!emcore_check_thread_status()) {
2687 err = EMAIL_ERROR_CANCELLED;
2692 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2693 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
2695 attachment = attachment_list + i;
2696 if (attachment_no == 0) {
2697 /* download all attachments, nth starts from 1, not zero */
2698 current_attachment_no = i + 1; /* attachment no */
2701 /* download only nth attachment */
2702 current_attachment_no = attachment_no; /* attachment no */
2705 if (!emcore_check_thread_status()) {
2706 err = EMAIL_ERROR_CANCELLED;
2710 _imap4_received_body_size = 0;
2711 _imap4_last_notified_body_size = 0;
2712 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2713 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2716 EM_SAFE_FREE(savefile);
2717 if (!emcore_get_temp_file_name(&savefile, &err)) {
2718 EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
2720 if (err_code != NULL)
2728 EM_DEBUG_LOG("uid [%d]", uid);
2730 if (!imap_mail_write_body_to_file(stream, account_id, mail_id, attachment_no, savefile, uid , attachment->section, attachment->encoding, &dec_len, NULL, &err)) {
2731 EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
2732 if (err_code != NULL)
2737 #ifdef SUPPORT_EXTERNAL_MEMORY
2738 iActualSize = emcore_get_actual_mail_size (cnt_info->text.plain , cnt_info->text.html, cnt_info->file , &err);
2739 if (!emstorage_mail_check_free_space(iActualSize, &bIs_full, &err)) {
2740 EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
2745 /* If external memory not present, return error */
2746 if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
2747 err = EMAIL_ERROR_MAIL_MEMORY_FULL;
2750 bIsAdd_to_mmc = true;
2752 #endif /* SUPPORT_EXTERNAL_MEMORY */
2754 if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
2755 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2759 if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
2760 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2764 if (!emstorage_move_file(savefile, buf, false, &err)) {
2765 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2770 attachment->attachment = EM_SAFE_STRDUP(buf);
2771 /* update attachment information. */
2772 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2773 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2778 #ifdef __FEATURE_DRM__
2779 if (emcore_check_drm(attachment)) {
2781 if (drm_svc_register_file(attachment->attachment) == DRM_RESULT_SUCCESS)
2782 EM_DEBUG_LOG("drm_svc_register_file success");
2784 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2785 mail->flag3 = attachment->flag2;
2787 #endif /* __FEATURE_DRM__ */
2789 if (!emcore_check_thread_status()) {
2790 err = EMAIL_ERROR_CANCELLED;
2794 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2801 EM_SAFE_FREE(savefile);
2803 emcore_close_mailbox(0, stream);
2806 if (attachment_list)
2807 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2810 emstorage_free_mail(&mail, 1, NULL);
2816 free(server_mbox);server_mbox = NULL;
2819 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2820 else if (err != EMAIL_ERROR_CANCELLED)
2821 emcore_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2823 if (err_code != NULL)
2830 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)
2832 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);
2835 int err = EMAIL_ERROR_NONE;
2836 int status = EMAIL_DOWNLOAD_FAIL;
2837 int pop3_body_size = 0;
2838 int pop3_downloaded_size = 0;
2839 MAILSTREAM *stream = NULL;
2841 PARTLIST *section_list = NULL;
2842 email_internal_mailbox_t mailbox = { 0 };
2843 emstorage_mail_tbl_t *mail = NULL;
2844 emstorage_attachment_tbl_t attachment = {0, 0, NULL, };
2845 email_account_t *ref_account = NULL;
2846 struct attachment_info *ai = NULL;
2847 struct _m_content_info *cnt_info = NULL;
2848 void *tmp_stream = NULL;
2849 char *s_uid = NULL, buf[512];
2850 int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
2851 int iActualSize = 0;
2852 char html_body[MAX_PATH] = {0, };
2853 emcore_uid_list *uid_list = NULL;
2854 char *mailbox_name = NULL;
2855 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2856 int html_changed = 0;
2860 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);
2861 err = EMAIL_ERROR_INVALID_PARAM;
2863 if (err_code != NULL)
2866 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
2870 FINISH_OFF_IF_CANCELED;
2872 only_body_download = true;
2874 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2875 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2880 if (mail->mailbox_name)
2881 mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
2883 if (1 == mail->body_download_status) {
2884 EM_DEBUG_EXCEPTION("not synchronous mail...");
2885 err = EMAIL_ERROR_INVALID_MAIL;
2889 s_uid = EM_SAFE_STRDUP(mail->server_mail_id);
2891 attachment.account_id = mail->account_id;
2892 attachment.mail_id = mail->mail_id;
2893 attachment.mailbox_id = mail->mailbox_id;
2894 attachment.attachment_save_status = 0;
2896 if (!(ref_account = emcore_get_account_reference(account_id))) {
2897 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2898 err = EMAIL_ERROR_INVALID_ACCOUNT;
2902 FINISH_OFF_IF_CANCELED;
2904 /* open mail server. */
2906 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&tmp_stream, &err) || !tmp_stream) {
2907 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2908 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
2911 stream = (MAILSTREAM *)tmp_stream;
2914 stream = (MAILSTREAM *)mail_stream;
2916 FINISH_OFF_IF_CANCELED;
2918 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2919 EM_DEBUG_EXCEPTION("em_malloc failed...");
2920 err = EMAIL_ERROR_OUT_OF_MEMORY;
2924 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
2926 /* in POP3 case, both text and attachment are downloaded in this call. */
2927 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2928 attachment.attachment_save_status = 1; /* all attachments should be downloaded in the case of POP3 */
2930 mailbox.account_id = account_id;
2931 mailbox.mail_stream = stream;
2933 /* download all uids from server. */
2934 if (!emcore_download_uid_all(&mailbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err)) {
2935 EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
2939 /* get mesg number to be related to last download mail from uid list file */
2940 if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err)) {
2941 EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
2942 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2946 if (!emcore_check_thread_status()) {
2947 err = EMAIL_ERROR_CANCELLED;
2951 _pop3_received_body_size = 0;
2952 _pop3_total_body_size = 0;
2953 _pop3_last_notified_body_size = 0;
2954 _pop3_receiving_mail_id = mail_id;
2956 /* send read mail commnad. */
2957 if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err)) {
2958 EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
2962 _pop3_total_body_size = pop3_body_size;
2964 if (!emcore_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
2965 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
2967 EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
2969 FINISH_OFF_IF_CANCELED;
2971 /* save message into tempfile */
2972 /* parsing mime from stream. */
2974 if (!emcore_parse_mime(stream, 0, cnt_info, &err)) {
2975 EM_DEBUG_EXCEPTION("emcore_parse_mime failed [%d]", err);
2979 FINISH_OFF_IF_CANCELED;
2981 else { /* in IMAP case, both text and attachment list are downloaded in this call. */
2982 /* This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
2983 if (with_attach > 0)
2984 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2986 cnt_info->grab_type = GRAB_TYPE_TEXT;
2988 int uid = s_uid? atoi(s_uid):0; /*prevent 39118*/
2990 /* set sparep(member of BODY) memory free function */
2991 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2993 /* get body strucutre. */
2994 /* don't free mbody because mbody is freed in closing mail_stream. */
2995 if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL)) {
2996 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2997 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3001 FINISH_OFF_IF_CANCELED;
3003 if (mbody->type == TYPEMULTIPART) {
3004 EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
3005 PART *part_child = mbody->nested.part;
3008 char filename[MAX_PATH+1] = {0, };
3009 int is_attachment = 0;
3010 while (part_child) {
3011 BODY *body = &(part_child->body);
3012 if (only_body_download == true) {
3013 if (((body->id) && EM_SAFE_STRLEN(body->id) > 1) || (body->location))
3015 else if (body->disposition.type) { /* "attachment" or "inline" or etc... */
3016 PARAMETER *param = body->disposition.parameter;
3019 EM_DEBUG_LOG("param->attribute [%s], param->value [%s]", param->attribute, param->value);
3021 if (!strcasecmp(param->attribute, "filename")) { /* attribute is "filename" */
3022 strncpy(filename, param->value, MAX_PATH);
3023 EM_DEBUG_LOG(">>>>> FILENAME [%s] ", filename);
3026 param = param->next;
3032 /* it may be report msg */
3033 if (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')
3038 if (is_attachment == 0) {
3039 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
3040 multi_part_body_size = multi_part_body_size + body->size.bytes;
3045 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
3046 multi_part_body_size = multi_part_body_size + body->size.bytes;
3048 part_child = part_child->next;
3053 /* set body fetch section. */
3054 if (emcore_set_fetch_body_section(mbody, true, &iActualSize, &err) < 0) {
3055 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
3059 EM_DEBUG_LOG("iActualSize [%d]", iActualSize);
3060 multi_part_body_size = iActualSize;
3062 _imap4_received_body_size = 0;
3063 _imap4_last_notified_body_size = 0;
3064 if (multi_part_body_size > 0) { /* download multiparts */
3065 _imap4_total_body_size = multi_part_body_size;
3066 _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
3068 else { /* download only one body part */
3069 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
3070 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
3073 /* save message into tempfile */
3074 /* download body text and get attachment list. */
3075 if (emcore_get_body_part_list_full(stream, uid, account_id, mail_id, mbody, cnt_info, &err, section_list, event_handle) < 0) {
3076 EM_DEBUG_EXCEPTION("emcore_get_body_part_list_full failed [%d]", err);
3079 FINISH_OFF_IF_CANCELED;
3082 if (cnt_info->text.plain) {
3083 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
3085 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
3086 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3091 if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err)) {
3092 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3096 if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err)) {
3097 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3101 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3102 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3105 if (cnt_info->text.html) {
3106 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
3107 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3111 if (cnt_info->text.plain_charset != NULL) {
3112 memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
3113 strcat(html_body, HTML_EXTENSION_STRING);
3116 memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3118 if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err)) {
3119 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3123 if (!emstorage_move_file(cnt_info->text.html, buf, false, &err)) {
3124 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3127 mail->file_path_html = EM_SAFE_STRDUP(buf);
3130 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && limited_size < pop3_body_size)
3131 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
3133 mail->body_download_status = EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
3135 /* Update local_preview_text */
3136 if ((err = emcore_get_preview_text_from_file(mail->file_path_plain, mail->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail->preview_text))) != EMAIL_ERROR_NONE) {
3137 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failedi : [%d]", err);
3140 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
3141 if (html_changed) mail->flag2 = 1;
3144 FINISH_OFF_IF_CANCELED;
3146 for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++) {
3147 attachment.attachment_id = attachment_num;
3148 attachment.attachment_size = ai->size;
3149 attachment.attachment_path = ai->save;
3150 attachment.attachment_name = ai->name;
3151 attachment.attachment_drm_type = ai->drm;
3152 attachment.attachment_inline_content_status = ai->type == 1;
3153 attachment.attachment_save_status = 0;
3154 attachment.attachment_mime_type = ai->attachment_mime_type;
3155 #ifdef __ATTACHMENT_OPTI__
3156 attachment.encoding = ai->encoding;
3157 attachment.section = ai->section;
3159 EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
3160 EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
3161 EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
3162 EM_DEBUG_LOG("attachment.attachment_name[%s]", attachment.attachment_name);
3163 EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
3164 EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
3167 local_inline_content_count++;
3168 local_attachment_count++;
3171 /* in POP3 case, rename temporary file to real file. */
3172 attachment.attachment_save_status = 1;
3173 if (ai->type == 1) { /* it is inline content */
3174 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
3175 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3178 if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err)) {
3179 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3184 if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
3185 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3189 if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err)) {
3190 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3195 if (!emstorage_move_file(ai->save, buf, false, &err)) {
3196 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3198 /* delete all created files. */
3199 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
3200 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
3201 /* goto FINISH_OFF; */
3204 if (!emstorage_delete_dir(buf, NULL)) {
3205 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
3206 /* goto FINISH_OFF; */
3213 free(ai->save); ai->save = EM_SAFE_STRDUP(buf);
3215 attachment.attachment_path = ai->save;
3217 #ifdef __FEATURE_DRM__
3218 if (emcore_check_drm(&attachment)) /* is drm content ?*/ {
3219 if (drm_process_request(DRM_REQUEST_TYPE_REGISTER_FILE, attachment.attachment_path, NULL) != DRM_RETURN_SUCCESS)
3220 EM_DEBUG_EXCEPTION("drm_process_request : register file fail");
3221 mail->DRM_status = attachment.attachment_drm_type;
3223 #endif/* __FEATURE_DRM__ */
3226 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
3227 /* Information : Attachment info already saved if partial body is dowloaded. */
3229 emstorage_attachment_tbl_t *attch_info = NULL;
3230 /* Get attachment details */
3231 if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info) {
3232 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
3233 if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND) { /* save only attachment file. */
3234 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3235 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3237 emstorage_free_attachment(&attch_info, 1, NULL);
3238 /* delete all created files. */
3239 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3240 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3244 if (!emstorage_delete_dir(buf, &err)) {
3245 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3249 /* ROLLBACK TRANSACTION; */
3250 emstorage_rollback_transaction(NULL, NULL, NULL);
3258 EM_DEBUG_LOG("Attachment info already exists...!");
3259 /* Update attachment size */
3260 EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
3261 attch_info->attachment_size = ai->size;
3262 if (!emstorage_update_attachment(attch_info, true, &err)) {
3263 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3270 emstorage_free_attachment(&attch_info, 1, NULL);
3276 mail->attachment_yn = 1;
3277 /* save only attachment file. */
3278 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3279 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3280 if (bIsAdd_to_mmc) {
3281 if (attachment.attachment) {
3285 /* delete all created files. */
3286 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3287 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3291 if (!emstorage_delete_dir(buf, &err)) {
3292 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3296 /* ROLLBACK TRANSACTION; */
3297 emstorage_rollback_transaction(NULL, NULL, NULL);
3302 #endif /* End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
3306 EM_DEBUG_LOG("Check #1");
3308 mail->attachment_count = local_attachment_count;
3309 mail->inline_content_count = local_inline_content_count;
3311 EM_DEBUG_LOG("Check #2");
3313 EM_DEBUG_LOG("Mailbox Name [%s]", mailbox_name);
3314 mail->mailbox_name = EM_SAFE_STRDUP(mailbox_name); /* fix for mailboox sync fail */
3316 EM_DEBUG_LOG("Check #3");
3318 /* change mail's information. */
3319 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err)) {
3320 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3321 emstorage_rollback_transaction(NULL, NULL, NULL); /* ROLLBACK TRANSACTION; */
3326 EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
3328 /* in pop3 mail case, the mail is deleted from server after being downloaded. */
3329 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3330 #ifdef DELETE_AFTER_DOWNLOADING
3333 SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
3335 if (!ref_account->keep_mails_on_pop_server_after_download) {
3336 if (!emcore_delete_mails_from_pop3_server(&mbox, delmsg, &err))
3337 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server failed [%d]", err);
3342 if (stream != NULL) {
3343 emcore_close_mailbox(0, stream);
3349 FINISH_OFF_IF_CANCELED;
3355 if (g_inline_count) {
3357 EM_SAFE_FREE(g_inline_list);
3361 emcore_free_account(ref_account);
3362 EM_SAFE_FREE(ref_account);
3365 multi_part_body_size = 0;
3366 _pop3_received_body_size = 0;
3367 _pop3_last_notified_body_size = 0;
3368 _pop3_total_body_size = 0;
3369 _pop3_receiving_mail_id = 0;
3371 _imap4_received_body_size = 0;
3372 _imap4_last_notified_body_size = 0;
3373 _imap4_total_body_size = 0;
3374 _imap4_download_noti_interval_value = 0;
3377 emcore_free_content_info(cnt_info);
3379 emstorage_free_mail(&mail, 1, NULL);
3380 EM_SAFE_FREE(s_uid);
3381 EM_SAFE_FREE(mailbox_name);
3383 multi_part_body_size = 0;
3386 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
3388 emcore_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
3390 if (err_code != NULL)
3398 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
3399 unsigned long uidvalidity, SEARCHSET *sourceset,
3402 EM_DEBUG_FUNC_BEGIN();
3403 char old_server_uid[129];
3405 EM_DEBUG_LOG("mailbox name - %s", mailbox);
3406 EM_DEBUG_LOG("first sequence number source- %ld", sourceset->first);
3407 EM_DEBUG_LOG("last sequence number last- %ld", sourceset->last);
3408 EM_DEBUG_LOG("first sequence number dest - %ld", destset->first);
3409 EM_DEBUG_LOG("last sequence number dest- %ld", sourceset->last);
3411 /* search for server _mail_id with value sourceset->first and update it with destset->first */
3412 /* faizan.h@samsung.com */
3413 memset(old_server_uid, 0x00, 129);
3414 sprintf(old_server_uid, "%ld", sourceset->first);
3415 EM_DEBUG_LOG(">>>>> old_server_uid = %s", old_server_uid);
3417 memset(g_new_server_uid, 0x00, 129);
3418 sprintf(g_new_server_uid, "%ld", destset->first);
3419 EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
3421 if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
3422 EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
3426 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)
3428 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);
3430 int err = EMAIL_ERROR_NONE;
3431 email_account_t *account = NULL;
3432 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3433 int bulk_flag = false;
3436 if (!(account = emcore_get_account_reference(input_account_id))) {
3437 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_account_id);
3438 err = EMAIL_ERROR_INVALID_ACCOUNT;
3442 if (!emnetwork_check_network_status(&err)) {
3443 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
3447 FINISH_OFF_IF_CANCELED;
3449 if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
3450 if (!bulk_flag && !emcore_delete_mails_from_imap4_server(input_mail_ids, input_mail_id_count, input_delete_option, &err)) {
3451 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_imap4_server failed [%d]", err);
3452 if (err == EMAIL_ERROR_IMAP4_STORE_FAILURE)
3453 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3459 else if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3460 if (!emcore_delete_mails_from_pop3_server(account, input_mail_ids, input_mail_id_count)) {
3461 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_pop3_server falied [%d]", err);
3464 #ifdef __FEATURE_LOCAL_ACTIVITY__
3466 /* Remove local activity */
3467 emstorage_activity_tbl_t new_activity;
3468 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
3469 if (from_server == EMAIL_DELETE_FOR_SEND_THREAD) {
3470 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
3471 EM_DEBUG_LOG("from_server == EMAIL_DELETE_FOR_SEND_THREAD ");
3474 new_activity.activity_type = ACTIVITY_DELETEMAIL;
3477 new_activity.mail_id = mail->mail_id;
3478 new_activity.server_mailid = mail->server_mail_id;
3479 new_activity.src_mbox = NULL;
3480 new_activity.dest_mbox = NULL;
3482 if (!emcore_delete_activity(&new_activity, &err)) {
3483 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
3486 /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
3487 if (!emstorage_remove_downloaded_mail(input_account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err)) {
3488 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3492 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3498 emcore_free_account(account);
3499 EM_SAFE_FREE(account);
3502 EM_DEBUG_FUNC_END("err [%d]", err);
3506 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)
3508 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);
3511 int err = EMAIL_ERROR_NONE;
3512 email_account_t *account = NULL;
3514 if (!account_id || !mail_ids || !num) {
3515 EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
3516 err = EMAIL_ERROR_INVALID_PARAM;
3520 if (!(account = emcore_get_account_reference(account_id))) {
3521 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
3522 err = EMAIL_ERROR_INVALID_ACCOUNT;
3526 FINISH_OFF_IF_CANCELED;
3528 if (from_server == EMAIL_DELETE_LOCALLY) /* Delete mails from local storage*/ {
3529 emcore_delete_mails_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
3530 emcore_display_unread_in_badge();
3532 else { /* Delete mails from server*/
3533 emcore_delete_mails_from_remote_server(account_id, mail_ids, num, from_server);
3541 emcore_show_user_message(account_id, EMAIL_ACTION_DELETE_MAIL, ret == true ? 0 : err);
3544 emcore_free_account(account);
3545 EM_SAFE_FREE(account);
3548 if (err_code != NULL)
3551 EM_DEBUG_FUNC_END("err [%d]", err);
3556 int emcore_delete_all_mails_of_acount(int input_account_id)
3558 EM_DEBUG_FUNC_BEGIN("input_account_id [%d]");
3560 int err = EMAIL_ERROR_NONE;
3561 char buf[512] = { 0, };
3563 /* emstorage_delete_mail_by_account is available only locally */
3564 if (!emstorage_delete_mail_by_account(input_account_id, false, &err)) {
3565 EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
3569 if (!emstorage_delete_attachment_all_on_db(input_account_id, NULL, false, &err)) {
3570 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3574 /* delete mail contents from filesystem */
3575 if (!emstorage_get_save_name(input_account_id, 0, 0, NULL, buf, &err)) {
3576 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3580 if (!emstorage_delete_dir(buf, &err)) {
3581 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3584 /* delete meeting request */
3585 if (!emstorage_delete_meeting_request(input_account_id, 0, 0, false, &err)) {
3586 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3591 EM_DEBUG_FUNC_END("err [%d]",err);
3595 INTERNAL_FUNC int emcore_delete_all_mails_of_mailbox(int input_account_id, int input_mailbox_id, int input_from_server, int *err_code)
3597 EM_DEBUG_FUNC_BEGIN("input_account_id[%d] input_mailbox_id[%d] input_from_server[%d] err_code[%p]", input_account_id, input_mailbox_id, input_from_server, err_code);
3600 int err = EMAIL_ERROR_NONE;
3601 int *mail_id_array = NULL;
3602 int mail_id_count = 0;
3603 char conditional_clause[QUERY_SIZE] = { 0, };
3605 if (!input_mailbox_id) {
3606 err = EMAIL_ERROR_INVALID_PARAM;
3607 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3611 /* Delete all mails in specific mailbox */
3613 SNPRINTF(conditional_clause, QUERY_SIZE, " where mailbox_id = %d ", input_mailbox_id);
3615 emstorage_query_mail_id_list(conditional_clause, false, &mail_id_array, &mail_id_count);
3617 EM_DEBUG_LOG("emstorage_query_mail_id_list returns [%d]", mail_id_count);
3619 if (mail_id_count > 0) {
3620 if (!emcore_delete_mail(input_account_id, mail_id_array, mail_id_count, input_from_server, EMAIL_DELETED_BY_COMMAND, false, &err)) {
3621 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
3629 EM_SAFE_FREE(mail_id_array);
3631 if (err_code != NULL)
3634 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
3638 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)
3640 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);
3641 int ret = false, err = EMAIL_ERROR_NONE, i;
3642 emstorage_mail_tbl_t *result_mail_list = NULL;
3643 char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
3645 /* Getting mail list by using select mail_id [in] */
3646 if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
3647 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
3651 /* Deleting mails by using select mail_id [in] */
3652 if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
3653 EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
3657 /* Sending Notification */
3658 noti_param_string = em_malloc(sizeof(char) * 10 * num);
3660 if(!noti_param_string) {
3661 EM_DEBUG_EXCEPTION("em_malloc failed");
3662 err = EMAIL_ERROR_OUT_OF_MEMORY;
3666 for(i = 0; i < num; i++) {
3667 memset(mail_id_string, 0, sizeof(mail_id_string));
3668 SNPRINTF(mail_id_string, sizeof(mail_id_string), "%d,", mail_ids[i]);
3669 strcat(noti_param_string, mail_id_string);
3670 /* can be optimized by appending sub string with directly pointing on string array kyuho.jo 2011-10-07 */
3673 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE, account_id, noti_param_1, noti_param_string, noti_param_2))
3674 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3676 /* Updating Thread informations */
3677 /* Thread information should be updated as soon as possible. */
3678 for(i = 0; i < num; i++) {
3679 if (result_mail_list[i].thread_item_count > 1) {
3680 if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false, &err)) {
3681 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
3687 if (!emcore_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
3688 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3690 for(i = 0; i < num; i++) {
3691 /* Deleting attachments */
3692 if (!emstorage_delete_all_attachments_of_mail(result_mail_list[i].mail_id, false, &err)) {
3693 EM_DEBUG_EXCEPTION("emstorage_delete_all_attachments_of_mail failed [%d]", err);
3694 if (err == EMAIL_ERROR_ATTACHMENT_NOT_FOUND)
3695 err = EMAIL_ERROR_NONE;
3698 /* Deleting Directories */
3699 /* delete mail contents from filesystem */
3700 if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
3701 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3705 if (!emstorage_delete_dir(buf, &err)) {
3706 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3709 /* Deleting Meeting Request */
3710 if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, 0, false, &err)) {
3711 EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
3712 if (err != EMAIL_ERROR_CONTACT_NOT_FOUND) {
3721 EM_SAFE_FREE(noti_param_string);
3723 if (result_mail_list)
3724 emstorage_free_mail(&result_mail_list, num, NULL);
3726 if (err_code != NULL)
3729 EM_DEBUG_FUNC_END();
3733 static int emcore_delete_mails_from_pop3_server(email_account_t *input_account, int input_mail_ids[], int input_mail_id_count)
3735 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);
3737 int err = EMAIL_ERROR_NONE;
3741 void *stream = NULL;
3742 email_internal_mailbox_t mailbox_data = { 0, };
3743 emstorage_mail_tbl_t *mail_tbl_data = NULL;
3745 if (!input_account || !input_mail_ids) {
3746 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3747 err = EMAIL_ERROR_INVALID_PARAM;
3751 for (i = 0; i < input_mail_id_count; i++) {
3752 FINISH_OFF_IF_CANCELED;
3754 mail_id = input_mail_ids[i];
3756 if (!emstorage_get_downloaded_mail(mail_id, &mail_tbl_data, false, &err) || !mail_tbl_data) {
3757 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_mail failed [%d]", err);
3759 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
3766 if (stream == NULL) {
3767 if (!emcore_connect_to_remote_mailbox(input_account->account_id, 0, (void **)&stream, &err)) {
3768 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
3772 mailbox_data.mail_stream = stream;
3773 mailbox_data.account_id = input_account->account_id;
3774 mailbox_data.mailbox_id = mail_tbl_data->mailbox_id;
3777 if (mailbox_data.user_data != NULL) {
3778 emcore_free_uids(mailbox_data.user_data, NULL);
3779 mailbox_data.user_data = NULL;
3782 if (!emcore_get_mail_msgno_by_uid(input_account, &mailbox_data, mail_tbl_data->server_mail_id, &msgno, &err)) {
3783 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid faild [%d]", err);
3784 if (err == EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
3785 goto NOT_FOUND_ON_SERVER;
3790 if (!pop3_mail_delete(mailbox_data.mail_stream, msgno, &err)) {
3791 EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
3793 if (err == EMAIL_ERROR_POP3_DELE_FAILURE)
3794 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3798 if (!emstorage_remove_downloaded_mail(input_account->account_id, mail_tbl_data->server_mailbox_name, mail_tbl_data->server_mail_id, true, &err))
3799 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3801 NOT_FOUND_ON_SERVER :
3803 if (mail_tbl_data != NULL)
3804 emstorage_free_mail(&mail_tbl_data, 1, NULL);
3809 if (mail_tbl_data != NULL)
3810 emstorage_free_mail(&mail_tbl_data, 1, NULL);
3813 emcore_close_mailbox(0, stream);
3817 if (mailbox_data.user_data != NULL) {
3818 emcore_free_uids(mailbox_data.user_data, NULL);
3819 mailbox_data.user_data = NULL;
3822 EM_DEBUG_FUNC_END("err [%d]", err);
3826 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)
3828 EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], uid[%s], msgno[%p], err_code[%p]", account, mailbox, uid, msgno, err_code);
3831 int err = EMAIL_ERROR_NONE;
3833 emcore_uid_list *uid_list = NULL;
3835 if (!account || !mailbox || !uid || !msgno) {
3836 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
3837 err = EMAIL_ERROR_INVALID_PARAM;
3841 uid_list = mailbox->user_data;
3843 if (uid_list == NULL) {
3844 if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
3845 if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3846 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed [%d]", err);
3850 else { /* EMAIL_SERVER_TYPE_IMAP4 */
3851 if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3852 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
3856 mailbox->user_data = uid_list;
3860 if (!strcmp(uid_list->uid, uid)) {
3861 *msgno = uid_list->msgno;
3862 EM_DEBUG_LOG("uid_list->msgno[%d]", uid_list->msgno);
3866 EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
3867 uid_list = uid_list->next;
3870 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3873 if (err_code != NULL)
3876 if (uid_list != NULL)
3877 emcore_free_uids(uid_list, NULL);
3878 /* mailbox->user_data and uid_list both point to same memory address, So when uid_list is freed then just set */
3879 /* mailbox->user_data to NULL and dont use EM_SAFE_FREE, it will crash : ) */
3881 mailbox->user_data = NULL;
3882 EM_DEBUG_FUNC_END();
3886 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_local_storage(int input_mailbox_id)
3888 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d]", input_mailbox_id);
3889 int err = EMAIL_ERROR_NONE;
3890 char *conditional_clause_string = NULL;
3891 email_list_filter_t *filter_list = NULL;
3892 int filter_count = 0;
3893 int *result_mail_id_list = NULL;
3894 int result_count = 0;
3895 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
3897 if (input_mailbox_id <= 0) {
3898 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3899 err = EMAIL_ERROR_INVALID_PARAM;
3903 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
3904 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
3910 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3912 if (filter_list == NULL) {
3913 EM_DEBUG_EXCEPTION("em_malloc failed");
3914 err = EMAIL_ERROR_OUT_OF_MEMORY;
3918 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3919 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3920 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3921 filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3923 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3924 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
3926 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3927 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3928 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3929 filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
3931 if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
3932 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
3936 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
3938 if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
3939 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
3943 if (!emcore_delete_mails_from_local_storage(mailbox_tbl->account_id, result_mail_id_list, result_count, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
3944 EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage [%d]", err);
3951 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
3954 emstorage_free_list_filter(&filter_list, filter_count);
3956 EM_SAFE_FREE(conditional_clause_string);
3957 EM_SAFE_FREE(result_mail_id_list);
3959 EM_DEBUG_FUNC_END("err [%d]", err);
3963 INTERNAL_FUNC int emcore_expunge_mails_deleted_flagged_from_remote_server(int input_account_id, int input_mailbox_id)
3965 int err = EMAIL_ERROR_NONE;
3966 char *conditional_clause_string = NULL;
3967 email_list_filter_t *filter_list = NULL;
3968 int filter_count = 0;
3969 int *result_mail_id_list = NULL;
3970 int result_count = 0;
3972 if ( input_mailbox_id <= 0) {
3973 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3974 err = EMAIL_ERROR_INVALID_PARAM;
3980 filter_list = em_malloc(sizeof(email_list_filter_t) * filter_count);
3982 if (filter_list == NULL) {
3983 EM_DEBUG_EXCEPTION("em_malloc failed");
3984 err = EMAIL_ERROR_OUT_OF_MEMORY;
3988 filter_list[0].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3989 filter_list[0].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3990 filter_list[0].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_MAILBOX_ID;
3991 filter_list[0].list_filter_item.rule.key_value.integer_type_value = input_mailbox_id;
3993 filter_list[1].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_OPERATOR;
3994 filter_list[1].list_filter_item.operator_type = EMAIL_LIST_FILTER_OPERATOR_AND;
3996 filter_list[2].list_filter_item_type = EMAIL_LIST_FILTER_ITEM_RULE;
3997 filter_list[2].list_filter_item.rule.rule_type = EMAIL_LIST_FILTER_RULE_EQUAL;
3998 filter_list[2].list_filter_item.rule.target_attribute = EMAIL_MAIL_ATTRIBUTE_FLAGS_DELETED_FIELD;
3999 filter_list[2].list_filter_item.rule.key_value.integer_type_value = 1;
4001 if ( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
4002 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
4006 EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
4008 if ((err = emstorage_query_mail_id_list(conditional_clause_string, true, &result_mail_id_list, &result_count)) != EMAIL_ERROR_NONE) {
4009 EM_DEBUG_EXCEPTION("emstorage_query_mail_id_list [%d]", err);
4013 if (!emcore_delete_mail(input_account_id, result_mail_id_list, result_count, EMAIL_DELETE_FROM_SERVER, 1, EMAIL_DELETED_BY_COMMAND, &err)) {
4014 EM_DEBUG_EXCEPTION("emcore_delete_mail [%d]", err);
4021 emstorage_free_list_filter(&filter_list, filter_count);
4023 EM_SAFE_FREE(result_mail_id_list);
4024 EM_SAFE_FREE(conditional_clause_string);
4026 EM_DEBUG_FUNC_END("err [%d]", err);
4032 * add a attachment to mail.
4034 * mailbox : mail box
4036 * attachment : attachment to be added
4041 INTERNAL_FUNC int emcore_mail_add_attachment(int mail_id, email_attachment_data_t *attachment, int *err_code)
4043 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
4045 if (attachment == NULL) {
4046 EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
4048 *err_code = EMAIL_ERROR_INVALID_PARAM;
4052 int ret = false, err = EMAIL_ERROR_NONE;
4053 emstorage_mail_tbl_t *mail_table_data = NULL;
4054 int attachment_id = 0;
4058 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
4059 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
4064 int account_id = mail_table_data->account_id;
4065 emstorage_attachment_tbl_t attachment_tbl;
4067 memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
4069 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
4070 attachment_tbl.account_id = mail_table_data->account_id;
4071 attachment_tbl.mailbox_id = mail_table_data->mailbox_id;
4072 attachment_tbl.mail_id = mail_id;
4073 attachment_tbl.attachment_name = attachment->attachment_name;
4074 attachment_tbl.attachment_size = attachment->attachment_size;
4075 attachment_tbl.attachment_save_status = attachment->save_status;
4076 attachment_tbl.attachment_drm_type = attachment->drm_status;
4077 attachment_tbl.attachment_inline_content_status = attachment->inline_content_status;
4079 /* BEGIN TRANSACTION; */
4080 emstorage_begin_transaction(NULL, NULL, NULL);
4082 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
4083 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4088 attachment->attachment_id = attachment_tbl.attachment_id;
4090 if (attachment->attachment_path) {
4093 if (!attachment->inline_content_status) {
4094 if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err)) {
4095 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4098 attachment_id = attachment_tbl.attachment_id;
4101 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->attachment_name, buf, &err)) {
4102 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4105 attachment_tbl.attachment_path = buf;
4107 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4108 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4113 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4114 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4119 if (attachment->save_status) {
4120 if (!emstorage_move_file(attachment->attachment_path, buf, false, &err)) {
4121 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4127 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4128 So no need to check for old files in this update case */
4129 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4130 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4135 EM_SAFE_FREE(attachment->attachment_path);
4136 attachment->attachment_path = EM_SAFE_STRDUP(buf);
4142 if (ret == true) { /* COMMIT TRANSACTION; */
4143 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4144 err = EMAIL_ERROR_DB_FAILURE;
4148 else { /* ROLLBACK TRANSACTION; */
4149 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4150 err = EMAIL_ERROR_DB_FAILURE;
4154 if (mail_table_data != NULL)
4155 emstorage_free_mail(&mail_table_data, 1, NULL);
4159 EM_DEBUG_FUNC_END("err [%d]", err);
4164 INTERNAL_FUNC int emcore_mail_add_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
4166 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4168 int err = EMAIL_ERROR_NONE;
4169 int attachment_id = 0;
4170 char buf[512] = { 0, };
4171 emstorage_mail_tbl_t *mail_table_data = NULL;
4172 emstorage_attachment_tbl_t attachment_tbl = { 0 };
4174 if (input_attachment_data == NULL) {
4175 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4176 return EMAIL_ERROR_INVALID_PARAM;
4179 if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
4180 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
4185 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
4187 attachment_tbl.account_id = mail_table_data->account_id;
4188 attachment_tbl.mailbox_id = mail_table_data->mailbox_id;
4189 attachment_tbl.mail_id = input_mail_id;
4190 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
4191 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4192 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4193 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4194 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4196 /* BEGIN TRANSACTION; */
4197 emstorage_begin_transaction(NULL, NULL, NULL);
4199 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
4200 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4205 input_attachment_data->attachment_id = attachment_tbl.attachment_id;
4207 if (input_attachment_data->attachment_path) {
4208 if (!input_attachment_data->inline_content_status) {
4209 if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4210 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4213 attachment_id = attachment_tbl.attachment_id;
4216 if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4217 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4220 attachment_tbl.attachment_path = buf;
4222 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4223 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4228 if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4229 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4234 if (input_attachment_data->save_status) {
4235 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err)) {
4236 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4242 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4243 So no need to check for old files in this update case */
4244 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4245 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4250 EM_SAFE_FREE(input_attachment_data->attachment_path);
4251 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4255 if (err == EMAIL_ERROR_NONE) { /* COMMIT TRANSACTION; */
4256 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4257 err = EMAIL_ERROR_DB_FAILURE;
4260 else { /* ROLLBACK TRANSACTION; */
4261 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4262 err = EMAIL_ERROR_DB_FAILURE;
4266 if (mail_table_data != NULL)
4267 emstorage_free_mail(&mail_table_data, 1, NULL);
4269 EM_DEBUG_FUNC_END("err [%d]", err);
4275 * delete a attachment from mail.
4277 * mailbox : mail box
4279 * attachment_id : number string of attachment-id to be deleted
4280 * (ex : if attachment id is 2, number stirng will be "2")
4285 int emcore_delete_mail_attachment(int attachment_id, int *err_code)
4287 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
4289 if (attachment_id == 0) {
4290 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
4291 if (err_code != NULL)
4292 *err_code = EMAIL_ERROR_INVALID_PARAM;
4297 int error = EMAIL_ERROR_NONE;
4298 char attachment_folder_path[MAX_PATH] = {0, };
4299 emstorage_attachment_tbl_t *attachment_tbl = NULL;
4301 if (!emstorage_get_attachment(attachment_id, &attachment_tbl, true, &error)) {
4302 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed");
4306 /* BEGIN TRANSACTION; */
4307 emstorage_begin_transaction(NULL, NULL, NULL);
4309 if (!emstorage_delete_attachment_on_db(attachment_id, false, &error)) {
4310 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
4312 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4313 error = EMAIL_ERROR_DB_FAILURE;
4319 SNPRINTF(attachment_folder_path, sizeof(attachment_folder_path), "%s/%d/%d/%d", MAILHOME, attachment_tbl->account_id, attachment_tbl->mail_id, attachment_id);
4321 if (!emstorage_delete_dir(attachment_folder_path, NULL)) {
4322 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed");
4325 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4326 error = EMAIL_ERROR_DB_FAILURE;
4332 emstorage_free_attachment(&attachment_tbl, 1, NULL);
4334 if (err_code != NULL)
4337 EM_DEBUG_FUNC_END("ret [%d]", ret);
4341 static int emcore_mail_update_attachment_data(int input_mail_id, email_attachment_data_t *input_attachment_data)
4343 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4345 int err = EMAIL_ERROR_NONE;
4346 int attachment_id = 0;
4347 char buf[512] = { 0 , };
4348 emstorage_attachment_tbl_t *existing_attachment_info = NULL;
4349 emstorage_attachment_tbl_t attachment_tbl = { 0 };
4351 if (input_attachment_data == NULL) {
4352 EM_DEBUG_EXCEPTION("Invalid Parameter");
4353 err = EMAIL_ERROR_INVALID_PARAM;
4357 if (!emstorage_get_attachment(input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
4358 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4363 attachment_tbl.mail_id = input_mail_id;
4364 attachment_tbl.account_id = existing_attachment_info->account_id;
4365 attachment_tbl.mailbox_id = existing_attachment_info->mailbox_id;
4366 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
4367 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4368 attachment_tbl.attachment_path = input_attachment_data->attachment_path;
4369 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4370 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4371 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4372 attachment_tbl.attachment_id = input_attachment_data->attachment_id;
4374 if (!input_attachment_data->inline_content_status) {
4375 if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4376 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4379 attachment_id = attachment_tbl.attachment_id;
4382 if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4383 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4386 attachment_tbl.attachment_path = buf;
4388 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);
4389 if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
4390 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err)) {
4391 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4397 EM_DEBUG_LOG("no need to move");
4399 EM_SAFE_FREE(input_attachment_data->attachment_path);
4400 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4402 emstorage_begin_transaction(NULL, NULL, NULL);
4404 if (!emstorage_update_attachment(&attachment_tbl, false, &err)) {
4405 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4411 if (err == EMAIL_ERROR_NONE) { /* COMMIT TRANSACTION; */
4412 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4413 err = EMAIL_ERROR_DB_FAILURE;
4416 else { /* ROLLBACK TRANSACTION; */
4417 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4418 err = EMAIL_ERROR_DB_FAILURE;
4422 if (existing_attachment_info)
4423 emstorage_free_attachment(&existing_attachment_info, 1, NULL);
4425 EM_DEBUG_FUNC_END("err [%d]", err);
4430 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)
4432 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);
4434 EM_IF_NULL_RETURN_VALUE(input_attachment_b_data, false);
4435 EM_IF_NULL_RETURN_VALUE(result, false);
4437 int err, err_2, ret = EMAIL_ERROR_NONE;
4438 emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
4440 if (!emstorage_get_attachment(input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
4441 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4446 if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
4447 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);
4448 *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4455 if (attachment_a_tbl)
4456 emstorage_free_attachment(&attachment_a_tbl, 1, &err_2);
4457 EM_DEBUG_FUNC_END("*result [%d]", *result);
4463 * copy a mail to mail box
4465 * src_mailbox : source mail box
4466 * msgno : mail sequence
4467 * dst_mailbox : target mail box
4472 INTERNAL_FUNC int emcore_mail_copy(int mail_id, email_mailbox_t *dst_mailbox, int *err_code)
4474 EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
4477 int err = EMAIL_ERROR_NONE;
4479 emstorage_mail_tbl_t *mail = NULL;
4480 emstorage_attachment_tbl_t *atch_list = NULL;
4482 int count = EMAIL_ATTACHMENT_MAX_COUNT;
4483 char *mailbox_name = NULL;
4485 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
4486 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4491 if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMAIL_ERROR_NONE ){
4492 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4497 /* get increased uid. */
4498 if (!emstorage_increase_mail_id(&mail->mail_id, true, &err)) {
4499 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
4504 /* copy mail body(text) file */
4505 if (mail->file_path_plain) {
4506 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4507 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4512 gchar *filename = g_path_get_basename(mail->file_path_plain);
4514 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4515 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4523 if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err)) {
4524 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4529 mail->file_path_plain = EM_SAFE_STRDUP(buf);
4532 /* copy mail body(html) file */
4533 if (mail->file_path_html) {
4534 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4535 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4540 gchar *filename = g_path_get_basename(mail->file_path_html);
4542 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4543 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4551 if (!emstorage_copy_file(mail->file_path_html, buf, false, &err)) {
4552 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4557 mail->file_path_html = EM_SAFE_STRDUP(buf);
4560 /* BEGIN TRANSACTION; */
4561 emstorage_begin_transaction(NULL, NULL, NULL);
4563 /* insert mail data */
4564 EM_SAFE_FREE(mail->mailbox_name);
4566 mail->account_id = dst_mailbox->account_id;
4567 mail->mailbox_id = dst_mailbox->mailbox_id;
4568 mail->mailbox_name = EM_SAFE_STRDUP(dst_mailbox->mailbox_name);
4569 mail->mailbox_type = dst_mailbox->mailbox_type;
4571 if (!emstorage_add_mail(mail, 0, false, &err)) {
4572 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
4574 if (mail->file_path_plain) {
4575 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4576 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4577 emstorage_rollback_transaction(NULL, NULL, NULL);
4581 if (mail->file_path_html) {
4582 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4583 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4584 emstorage_rollback_transaction(NULL, NULL, NULL);
4589 /* ROLLBACK TRANSACTION; */
4590 emstorage_rollback_transaction(NULL, NULL, NULL);
4596 /* copy attachment file */
4597 for (i = 0; i<count; i++) {
4598 if (atch_list[i].attachment_path) {
4599 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err)) {
4600 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4604 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err)) {
4605 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4609 if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err)) {
4610 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4614 EM_SAFE_FREE(atch_list[i].attachment_path);
4616 atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
4619 atch_list[i].account_id = dst_mailbox->account_id;
4620 atch_list[i].mail_id = mail->mail_id;
4621 atch_list[i].mailbox_id = mail->mailbox_id;
4623 if (!emstorage_add_attachment(&atch_list[i], 0, false, &err)) {
4624 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4630 /* in case error happened, delete copied file. */
4631 if (i && i != count) {
4632 for (;i >= 0; i--) {
4633 if (atch_list[i].attachment_path) {
4635 if (!emstorage_delete_file(atch_list[i].attachment_path, &err)) {
4636 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4637 emstorage_rollback_transaction(NULL, NULL, NULL);
4643 if (mail->file_path_plain) {
4644 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4645 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4646 emstorage_rollback_transaction(NULL, NULL, NULL);
4650 if (mail->file_path_html) {
4651 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4652 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4653 emstorage_rollback_transaction(NULL, NULL, NULL);
4658 /* ROLLBACK TRANSACTION; */
4659 emstorage_rollback_transaction(NULL, NULL, NULL);
4663 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4664 err = EMAIL_ERROR_DB_FAILURE;
4665 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4666 err = EMAIL_ERROR_DB_FAILURE;
4670 if (!emstorage_get_mailbox_name_by_mailbox_type(dst_mailbox->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err)) {
4671 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
4675 if (!strcmp(dst_mailbox->mailbox_name, mailbox_name) && !(mail->flags_seen_field))
4676 emcore_display_unread_in_badge();
4681 if (atch_list != NULL)
4682 emstorage_free_attachment(&atch_list, count, NULL);
4685 emstorage_free_mail(&mail, 1, NULL);
4687 EM_SAFE_FREE(mailbox_name);
4689 if (err_code != NULL)
4691 EM_DEBUG_FUNC_END("err [%d]", err);
4696 * move a mail to mail box
4698 * old_mailbox : previous mail box
4700 * new_mailbox : target mail box
4705 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)
4707 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);
4710 int err = EMAIL_ERROR_NONE;
4711 emstorage_mail_tbl_t *mail_list = NULL;
4713 int i = 0, parameter_string_length = 0;
4714 char *parameter_string = NULL, mail_id_string[10];
4716 if ( dst_mailbox_id <= 0 && mail_ids_count < 1) {
4717 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4718 err = EMAIL_ERROR_INVALID_PARAM;
4722 if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
4723 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
4727 account_id = mail_list[0].account_id;
4729 if(!emstorage_move_multiple_mails_on_db(account_id, dst_mailbox_id, mail_ids, mail_ids_count, true, &err)) {
4730 EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails_on_db failed [%d]", err);
4734 /* Sending a notification */
4735 parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
4736 parameter_string = em_malloc(parameter_string_length);
4738 if (parameter_string == NULL) {
4739 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
4740 err = EMAIL_ERROR_OUT_OF_MEMORY;
4744 if (mail_list[0].mailbox_id > 0)
4745 SNPRINTF(parameter_string, parameter_string_length, "%d%c%d%c", mail_list[0].mailbox_id, 0x01, dst_mailbox_id , 0x01);
4747 for (i = 0; i < mail_ids_count; i++) {
4748 memset(mail_id_string, 0, 10);
4749 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
4750 strcat(parameter_string, mail_id_string);
4753 EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
4755 if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
4756 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_MOVE] >>>> ");
4759 for (i = 0; i < mail_ids_count; i++) {
4760 if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, true, &err))
4761 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
4764 if (!emcore_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
4765 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
4767 emcore_display_unread_in_badge();
4773 if (!emcore_notify_storage_event(NOTI_MAIL_MOVE_FAIL, account_id, noti_param_1, parameter_string, noti_param_2))
4774 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_MOVE_FAIL ] >>>> ");
4777 emstorage_free_mail(&mail_list, mail_ids_count, NULL);
4779 EM_SAFE_FREE(parameter_string);
4781 if (err_code != NULL)
4783 EM_DEBUG_FUNC_END("err [%d]", err);
4788 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)
4790 EM_DEBUG_FUNC_BEGIN();
4791 MAILSTREAM *stream = NULL;
4793 email_account_t *ref_account = NULL;
4794 emstorage_mail_tbl_t *mail = NULL;
4799 mail_id = mail_ids[0];
4801 ref_account = emcore_get_account_reference(account_id);
4803 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : get account reference[%d]", account_id);
4804 *error_code = EMAIL_ERROR_INVALID_ACCOUNT;
4809 /* if not imap4 mail, return */
4810 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
4811 *error_code = EMAIL_ERROR_INVALID_PARAM;
4816 for (i = 0; i < num; i++) {
4817 mail_id = mail_ids[i];
4819 if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail) {
4820 EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id : emstorage_get_downloaded_mail failed [%d]", err_code);
4822 if (err_code == EMAIL_ERROR_MAIL_NOT_FOUND) { /* not server mail */
4823 /* err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
4827 *error_code = err_code;
4834 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox_id, (void **)&stream, &err_code)) /* faizan.h@samsung.com mail_move_fix_07042009 */ {
4835 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : Mailbox open[%d]", err_code);
4842 /* set callback for COPY_UID */
4843 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
4845 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
4847 if (mail->server_mail_id) {
4848 if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
4849 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server : Mail cannot be moved failed");
4853 /* send EXPUNGE command */
4854 if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
4855 EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
4857 if (err_code == EMAIL_ERROR_IMAP4_STORE_FAILURE)
4858 err_code = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
4862 /* faizan.h@samsung.com duplicate copy while sync mailbox issue fixed */
4863 EM_DEBUG_LOG(">>>mailbox_name[%s]>>>>>>", mail->mailbox_name);
4864 EM_DEBUG_LOG(">>>g_new_server_uid[%s]>>>>>>", g_new_server_uid);
4865 EM_DEBUG_LOG(">>>mail_id[%d]>>>>>>", mail_id);
4867 if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->mailbox_name, &err_code)) {
4868 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
4872 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
4876 EM_DEBUG_EXCEPTION(">>>> Server MAIL ID IS NULL >>>> ");
4882 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
4891 if (stream) emcore_close_mailbox(account_id, stream);
4894 emcore_free_account(ref_account);
4895 EM_SAFE_FREE(ref_account);
4899 emstorage_free_mail(&mail, 1, NULL);
4900 EM_DEBUG_FUNC_END("ret [%d]", ret);
4905 static int emcore_copy_mail_to_another_account_on_local_storeage(int input_mail_id, emstorage_mailbox_tbl_t *input_source_mailbox, emstorage_mailbox_tbl_t *input_target_mailbox, int input_task_id, int *output_mail_id)
4907 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox[%p] input_target_mailbox[%p] input_task_id [%d] output_mail_id[%p]", input_mail_id, input_source_mailbox, input_target_mailbox, input_task_id, output_mail_id);
4909 int err = EMAIL_ERROR_NONE;
4910 int attachment_count = 0;
4911 email_mail_data_t *mail_data = NULL;
4912 email_attachment_data_t *attachment_data = NULL;
4914 if ( input_source_mailbox == NULL || input_target_mailbox == NULL || input_mail_id <= 0 || output_mail_id == NULL) {
4915 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4916 err = EMAIL_ERROR_INVALID_PARAM;
4920 if((err = emcore_get_mail_data(input_mail_id, &mail_data)) != EMAIL_ERROR_NONE) {
4921 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed [%d]", err);
4922 err = EMAIL_ERROR_INVALID_MAIL;
4926 /* Check download status */
4927 if(mail_data->body_download_status != 1) {
4928 /* If not downloaded, download fully */
4929 if (!emcore_download_body_multi_sections_bulk(NULL,
4930 input_source_mailbox->account_id,
4933 (mail_data->attachment_count > 0)?1:0,
4937 EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed [%d]", err);
4942 /* Get attachments */
4943 if((err = emcore_get_attachment_data_list(input_mail_id, &attachment_data, &attachment_count)) != EMAIL_ERROR_NONE) {
4944 EM_DEBUG_EXCEPTION("emcore_get_attachment_data_list failed [%d]", err);
4948 mail_data->account_id = input_target_mailbox->account_id;
4949 mail_data->mail_id = 0;
4950 mail_data->mailbox_id = input_target_mailbox->mailbox_id;
4951 mail_data->mailbox_type = input_target_mailbox->mailbox_type;
4952 mail_data->thread_id = 0;
4953 mail_data->thread_item_count = 0;
4955 if((err = emcore_add_mail(mail_data, attachment_data, attachment_count, NULL, false)) != EMAIL_ERROR_NONE) {
4956 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
4960 *output_mail_id = mail_data->mail_id;
4964 emcore_free_mail_data(mail_data);
4965 free(mail_data); /* prevent 34648 */
4968 if (attachment_data)
4969 emcore_free_attachment_data(&attachment_data, attachment_count, NULL);
4971 EM_DEBUG_FUNC_END("err [%d]", err);
4975 INTERNAL_FUNC int emcore_move_mail_to_another_account(int input_mail_id, int input_source_mailbox_id, int input_target_mailbox_id, int input_task_id)
4977 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d] input_source_mailbox_id[%d] input_target_mailbox_id[%d] result_mail_id[%p] input_task_id [%d]", input_mail_id, input_source_mailbox_id, input_target_mailbox_id, input_task_id);
4978 int err = EMAIL_ERROR_NONE;
4979 int err_for_delete_mail = EMAIL_ERROR_NONE;
4980 int moved_mail_id = 0;
4981 emstorage_mailbox_tbl_t *source_mailbox = NULL;
4982 emstorage_mailbox_tbl_t *target_mailbox = NULL;
4983 email_account_t *source_account_ref = NULL;
4984 email_account_t *target_account_ref = NULL;
4986 if((err = emstorage_get_mailbox_by_id(input_source_mailbox_id, &source_mailbox)) != EMAIL_ERROR_NONE) {
4987 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for source_mailbox [%d]", err);
4991 if((err = emstorage_get_mailbox_by_id(input_target_mailbox_id, &target_mailbox)) != EMAIL_ERROR_NONE) {
4992 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed for target_mailbox [%d]", err);
4996 /* Check account type */
4997 /* POP -> IMAP possible */
4998 /* IMAP -> POP possible, but the mail would not be on server */
4999 /* EAS -> X impossible */
5000 /* X -> EAS impossible */
5002 source_account_ref = emcore_get_account_reference(source_mailbox->account_id);
5004 if(source_account_ref == NULL || source_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
5005 EM_DEBUG_EXCEPTION("Invalid account");
5006 err = EMAIL_ERROR_INVALID_ACCOUNT;
5010 target_account_ref = emcore_get_account_reference(target_mailbox->account_id);
5012 if(target_account_ref == NULL || target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
5013 EM_DEBUG_EXCEPTION("Invalid account");
5014 err = EMAIL_ERROR_INVALID_ACCOUNT;
5019 if((err = emcore_copy_mail_to_another_account_on_local_storeage(input_mail_id, source_mailbox, target_mailbox, input_task_id, &moved_mail_id)) != EMAIL_ERROR_NONE) {
5020 EM_DEBUG_EXCEPTION("emcore_copy_mail_to_another_account_on_local_storeage failed [%d]", err);
5024 if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 1 , &err)) {
5025 EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
5029 if(target_account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
5030 if((err = emcore_sync_mail_from_client_to_server(moved_mail_id)) != EMAIL_ERROR_NONE) {
5031 EM_DEBUG_EXCEPTION("emcore_sync_mail_from_client_to_server failed [%d]", err);
5033 /* if append is failed, restore source mail and delete copied mail. */
5034 if(!emcore_set_flags_field(source_mailbox->account_id, &input_mail_id, 1, EMAIL_FLAGS_DELETED_FIELD, 0 , &err)) {
5035 EM_DEBUG_EXCEPTION("emcore_set_flags_field failed [%d]", err);
5039 if(!emcore_delete_mail(target_mailbox->account_id, &moved_mail_id, 1, false, 0, 0, &err_for_delete_mail))
5040 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err_for_delete_mail);
5045 if(!emcore_delete_mail(source_mailbox->account_id, &input_mail_id, 1, true, 0, 0, &err)) {
5046 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
5053 emstorage_free_mailbox(&source_mailbox, 1, NULL);
5056 emstorage_free_mailbox(&target_mailbox, 1, NULL);
5058 if (source_account_ref) {
5059 emcore_free_account(source_account_ref);
5060 EM_SAFE_FREE(source_account_ref);
5063 if (target_account_ref) {
5064 emcore_free_account(target_account_ref);
5065 EM_SAFE_FREE(target_account_ref);
5068 EM_DEBUG_FUNC_END("err [%d]", err);
5072 INTERNAL_FUNC 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)
5074 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);
5076 int ret = false, err = EMAIL_ERROR_NONE;
5078 if (!file_name || !full_path || !src_file_path) {
5079 EM_DEBUG_EXCEPTION("Invalid paramter");
5080 err = EMAIL_ERROR_INVALID_PARAM;
5084 if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err)) {
5085 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
5089 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err)) {
5090 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
5094 if (strcmp(src_file_path, full_path) != 0) {
5095 if (!emstorage_copy_file(src_file_path, full_path, false, &err)) {
5096 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
5109 /* description : update mail information */
5110 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)
5112 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);
5114 char filename_buf[1024] = {0, };
5115 char *body_text_file_name = NULL;
5117 int err = EMAIL_ERROR_NONE;
5118 int local_inline_content_count = 0;
5119 emstorage_mail_tbl_t *converted_mail_tbl_data = NULL;
5120 email_meeting_request_t *meeting_req = NULL;
5123 if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list)) {
5124 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5125 err = EMAIL_ERROR_INVALID_PARAM;
5129 if(input_from_eas == 0) {
5130 if (input_mail_data->file_path_plain) {
5131 if (stat(input_mail_data->file_path_plain, &st_buf) < 0) {
5132 EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
5133 err = EMAIL_ERROR_INVALID_MAIL;
5138 if (input_mail_data->file_path_html) {
5139 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
5140 EM_DEBUG_EXCEPTION("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
5141 err = EMAIL_ERROR_INVALID_MAIL;
5146 if (input_attachment_count && input_attachment_data_list) {
5147 for (i = 0; i < input_attachment_count; i++) {
5148 if (input_attachment_data_list[i].save_status) {
5149 if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0) {
5150 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
5151 err = EMAIL_ERROR_INVALID_ATTACHMENT;
5159 if(input_mail_data->mail_size == 0) {
5160 emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
5163 if (input_mail_data->file_path_plain) { /* Save plain text body. */
5164 if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_plain, &body_text_file_name)) != EMAIL_ERROR_NONE) {
5165 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
5166 err = EMAIL_ERROR_INVALID_FILE_PATH;
5170 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, body_text_file_name, filename_buf, &err)) {
5171 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5174 EM_SAFE_FREE(input_mail_data->file_path_plain);
5175 input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
5178 if (input_mail_data->file_path_html) { /* Save HTML text body. */
5179 EM_SAFE_FREE(body_text_file_name);
5180 if ( (err = em_get_file_name_from_file_path(input_mail_data->file_path_html, &body_text_file_name)) != EMAIL_ERROR_NONE) {
5181 EM_DEBUG_EXCEPTION("em_get_file_name_from_file_path failed [%d]", err);
5182 err = EMAIL_ERROR_INVALID_FILE_PATH;
5186 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, body_text_file_name, filename_buf, &err)) {
5187 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5190 EM_SAFE_FREE(input_mail_data->file_path_html);
5191 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
5194 if (input_mail_data->file_path_mime_entity) { /* Save mime entity. */
5195 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)) {
5196 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
5199 EM_SAFE_FREE(input_mail_data->file_path_mime_entity);
5200 input_mail_data->file_path_mime_entity = EM_SAFE_STRDUP(filename_buf);
5203 if (input_attachment_data_list && input_attachment_count) {
5205 int compare_result = 1;
5206 email_attachment_data_t *temp_attachment_data = NULL;
5208 for(i = 0; i < input_attachment_count; i++) {
5209 temp_attachment_data = input_attachment_data_list + i;
5210 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) {
5211 EM_DEBUG_EXCEPTION("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
5215 if (compare_result == 0) {
5216 EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
5217 if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data)) {
5218 EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
5223 EM_DEBUG_LOG("save names are different");
5224 if(temp_attachment_data->attachment_id > 0) {
5225 if (!emcore_delete_mail_attachment(temp_attachment_data->attachment_id, &err)) {
5226 EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
5231 if ( (err = emcore_mail_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMAIL_ERROR_NONE) {
5232 EM_DEBUG_EXCEPTION("emcore_mail_add_attachment failed [%d]", err);
5237 if (temp_attachment_data->inline_content_status)
5238 local_inline_content_count++;
5242 input_mail_data->attachment_count = input_attachment_count;
5243 input_mail_data->inline_content_count = local_inline_content_count;
5245 if (!input_mail_data->date_time) {
5246 /* time isn't set */
5247 input_mail_data->date_time = time(NULL);
5250 EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
5251 if (input_mail_data->preview_text == NULL) {
5252 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) {
5253 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
5254 if (err != EMAIL_ERROR_EMPTY_FILE)
5259 if(!em_convert_mail_data_to_mail_tbl(input_mail_data, 1, &converted_mail_tbl_data, &err)) {
5260 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
5264 /* BEGIN TRANSACTION; */
5265 emstorage_begin_transaction(NULL, NULL, NULL);
5267 if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err)) {
5268 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5273 if (input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
5274 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
5275 || input_mail_data->meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
5276 /* check where there is a meeting request in DB */
5277 if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMAIL_ERROR_DATA_NOT_FOUND) {
5278 EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
5281 EM_SAFE_FREE(meeting_req);
5282 if (err == EMAIL_ERROR_DATA_NOT_FOUND) { /* insert */
5283 emstorage_mail_tbl_t *original_mail = NULL;
5285 if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
5286 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5290 if (original_mail) {
5291 if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_id, input_meeting_request, false, &err)) {
5292 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
5296 emstorage_free_mail(&original_mail, 1, NULL);
5300 if (!emstorage_update_meeting_request(input_meeting_request, false, &err)) {
5301 EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
5309 if (err == EMAIL_ERROR_NONE) {
5310 /* COMMIT TRANSACTION; */
5311 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5312 err = EMAIL_ERROR_DB_FAILURE;
5315 if (input_mail_data->meeting_request_status && !emcore_notify_storage_event(NOTI_MAIL_UPDATE, input_mail_data->account_id, input_mail_data->mail_id, NULL, UPDATE_MEETING))
5316 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event failed [ NOTI_MAIL_UPDATE : UPDATE_MEETING_RESPONSE ] >>>> ");
5319 /* ROLLBACK TRANSACTION; */
5320 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5321 err = EMAIL_ERROR_DB_FAILURE;
5326 EM_SAFE_FREE(body_text_file_name);
5329 emstorage_free_meeting_request(meeting_req);
5331 if(converted_mail_tbl_data)
5332 emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
5334 EM_DEBUG_FUNC_END("err [%d]", err);
5339 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)
5341 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);
5344 int err = EMAIL_ERROR_NONE;
5345 char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "flags_seen_field"
5346 , "flags_deleted_field", "flags_flagged_field", "flags_answered_field"
5347 , "flags_recent_field", "flags_draft_field", "flags_forwarded_field" };
5349 if(field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT || mail_ids == NULL || num <= 0 || account_id == 0) {
5350 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5351 err = EMAIL_ERROR_INVALID_PARAM;
5355 if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
5356 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
5360 if(field_type == EMAIL_FLAGS_SEEN_FIELD)
5361 emcore_display_unread_in_badge();
5370 EM_DEBUG_FUNC_END("err [%d]", err);
5374 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)
5376 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
5379 int err = EMAIL_ERROR_NONE;
5380 int total_body_size = 0;
5382 char *response = NULL;
5383 POP3LOCAL *pop3local;
5385 if (!stream || !result_total_body_size) {
5386 EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
5387 err = EMAIL_ERROR_INVALID_PARAM;
5391 pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
5393 if (!pop3local || !pop3local->netstream) {
5394 err = EMAIL_ERROR_INVALID_STREAM;
5397 memset(command, 0x00, sizeof(command));
5399 SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
5401 #ifdef FEATURE_CORE_DEBUG
5402 EM_DEBUG_LOG("[POP3] >>> [%s]", command);
5406 /* send command : LIST [msgno] - to get the size of the mail */
5407 if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command))) {
5408 EM_DEBUG_EXCEPTION("net_sout failed...");
5410 err = EMAIL_ERROR_INVALID_RESPONSE;
5414 EM_DEBUG_LOG("Sending command success");
5416 while (pop3local->netstream) {
5417 /* receive response */
5418 if (!(response = net_getline(pop3local->netstream)))
5422 EM_DEBUG_LOG("[POP3] <<< %s", response);
5424 if (*response == '.') {
5425 EM_SAFE_FREE(response);
5429 if (*response == '+') { /* "+ OK" */
5432 if (!(p = strchr(response + strlen("+OK "), ' '))) {
5433 err = EMAIL_ERROR_INVALID_RESPONSE;
5437 total_body_size = atoi(p + 1);
5438 EM_DEBUG_LOG("Body size [%d]", total_body_size);
5440 if (result_total_body_size) {
5441 *result_total_body_size = total_body_size;
5445 else if (*response == '-') { /* "- ERR" */
5446 err = EMAIL_ERROR_INVALID_RESPONSE;
5450 err = EMAIL_ERROR_INVALID_RESPONSE;
5454 EM_SAFE_FREE(response);
5457 memset(command, 0x00, sizeof(command));
5459 if (limited_size && total_body_size > limited_size) {
5460 int count_of_line = limited_size / 80;
5461 SNPRINTF(command, sizeof(command), "TOP %d %d", msgno, count_of_line);
5464 SNPRINTF(command, sizeof(command), "RETR %d", msgno);
5466 EM_DEBUG_LOG("[POP3] >>> %s", command);
5468 emcore_set_network_error(EMAIL_ERROR_NONE); /* set current network error as EMAIL_ERROR_NONE before network operation */
5469 /* get mail from mail server */
5470 if (!pop3_send((MAILSTREAM *)stream, command, NULL)) {
5471 EM_DEBUG_EXCEPTION("pop3_send failed...");
5473 email_session_t *session = NULL;
5475 if (!emcore_get_current_session(&session)) {
5476 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
5477 err = EMAIL_ERROR_SESSION_NOT_FOUND;
5481 if (session->network == EMAIL_ERROR_NONE)
5482 err = EMAIL_ERROR_UNKNOWN;
5484 err = session->network;
5492 EM_SAFE_FREE(response);
5494 if (err_code != NULL)
5496 EM_DEBUG_FUNC_END("err [%d]", err);
5501 INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
5503 EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
5506 if (err_code != NULL)
5507 *err_code = EMAIL_ERROR_INVALID_PARAM;
5512 int err = EMAIL_ERROR_NONE;
5513 int status = EMAIL_DOWNLOAD_FAIL;
5514 MAILSTREAM *stream = NULL;
5515 email_internal_mailbox_t mailbox = {0};
5516 emstorage_mail_tbl_t *mail = NULL;
5517 email_account_t *ref_account = NULL;
5520 char set_flags[100] = { 0, };
5521 char clear_flags[100] = { 0, };
5522 char tmp[100] = { 0, };
5524 if (!emcore_check_thread_status()) {
5525 err = EMAIL_ERROR_CANCELLED;
5529 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5530 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5535 account_id = mail->account_id;
5537 if (!(ref_account = emcore_get_account_reference(account_id))) {
5538 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5539 err = EMAIL_ERROR_INVALID_ACCOUNT;
5543 if (!emcore_check_thread_status()) {
5544 err = EMAIL_ERROR_CANCELLED;
5548 /* open mail server. */
5549 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
5550 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5551 status = EMAIL_LIST_CONNECTION_FAIL;
5556 if (!emcore_check_thread_status()) {
5557 err = EMAIL_ERROR_CANCELLED;
5561 mailbox.mailbox_name = mail->mailbox_name;
5562 mailbox.account_id = account_id;
5563 mailbox.mail_stream = stream;
5565 /* download all uids from server. */
5566 if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err)) {
5567 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5571 sprintf (tmp, "%d", msgno);
5573 if (mail->flags_seen_field)
5574 sprintf(set_flags, "\\Seen");
5576 sprintf(clear_flags, "\\Seen");
5578 if (mail->flags_answered_field)
5579 sprintf(set_flags, "%s \\Answered", set_flags);
5581 sprintf(clear_flags, "%s \\Answered", clear_flags);
5583 if (mail->flags_flagged_field)
5584 sprintf(set_flags, "%s \\Flagged", set_flags);
5586 sprintf(clear_flags, "%s \\Flagged", clear_flags);
5588 if (mail->flags_forwarded_field)
5589 sprintf(set_flags, "%s $Forwarded", set_flags);
5591 sprintf(clear_flags, "%s $Forwarded", clear_flags);
5593 if (EM_SAFE_STRLEN(set_flags) > 0) {
5594 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s] ", set_flags);
5595 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5596 EM_DEBUG_LOG(">>>> End mail_setflag ");
5599 if (EM_SAFE_STRLEN(clear_flags) > 0) {
5600 EM_DEBUG_LOG(">>>> Calling mail_clearflag [%s]", clear_flags);
5601 mail_clearflag(stream, tmp, clear_flags);
5602 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5605 if (mail->lock_status) {
5606 memset(set_flags, 0x00, 100);
5607 sprintf(set_flags, "Sticky");
5608 if (EM_SAFE_STRLEN(set_flags) > 0) {
5609 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s]", set_flags);
5610 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5611 EM_DEBUG_LOG(">>>> End mail_setflag ");
5615 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5617 if (!emcore_check_thread_status()) {
5618 err = EMAIL_ERROR_CANCELLED;
5627 emcore_close_mailbox(account_id, stream);
5630 emstorage_free_mail(&mail, 1, NULL);
5633 emcore_free_account(ref_account);
5634 EM_SAFE_FREE(ref_account);
5637 if (err_code != NULL)
5639 EM_DEBUG_FUNC_END("err [%d]", err);
5643 INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
5645 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
5647 if (mail_ids[0] < 1) {
5648 if (err_code != NULL)
5649 *err_code = EMAIL_ERROR_INVALID_PARAM;
5654 int err = EMAIL_ERROR_NONE;
5655 int status = EMAIL_DOWNLOAD_FAIL;
5656 MAILSTREAM *stream = NULL;
5657 email_internal_mailbox_t mailbox;
5658 emstorage_mail_tbl_t *mail = NULL;
5659 email_account_t *ref_account = NULL;
5662 char set_flags[100];
5663 char clear_flags[100];
5668 memset(&mailbox, 0x00, sizeof(email_mailbox_t));
5670 mail_id = mail_ids[0];
5672 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5673 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5678 account_id = mail->account_id;
5680 if (!(ref_account = emcore_get_account_reference(account_id))) {
5681 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5682 err = EMAIL_ERROR_INVALID_ACCOUNT;
5686 /* open mail server. */
5687 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
5688 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5689 status = EMAIL_LIST_CONNECTION_FAIL;
5693 mailbox.mailbox_name = mail->mailbox_name;
5694 mailbox.account_id = account_id;
5695 mailbox.mail_stream = stream;
5697 for (i = 0; i < num; i++) {
5698 mail_id = mail_ids[i];
5700 if (!emcore_check_thread_status()) {
5701 err = EMAIL_ERROR_CANCELLED;
5705 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5706 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5711 if (!emcore_check_thread_status()) {
5712 err = EMAIL_ERROR_CANCELLED;
5716 /* download message number from server. */
5717 if (!emcore_get_mail_msgno_by_uid(ref_account, &mailbox, mail->server_mail_id, &msgno, &err)) {
5718 EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5722 memset(tmp, 0x00, 100);
5723 sprintf (tmp, "%d", msgno);
5725 memset(set_flags, 0x00, 100);
5726 memset(clear_flags, 0x00, 100);
5728 if (mail->flags_seen_field)
5729 sprintf(set_flags, "\\Seen");
5731 sprintf(clear_flags, "\\Seen");
5732 EM_DEBUG_LOG("new_flag.seen : %s ", set_flags);
5734 if (EM_SAFE_STRLEN(set_flags) > 0) {
5735 EM_DEBUG_LOG(">>>> Calling mail_setflag ");
5736 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5737 EM_DEBUG_LOG(">>>> End mail_setflag ");
5740 EM_DEBUG_LOG(">>>> Calling mail_clearflag ");
5741 mail_clearflag(stream, tmp, clear_flags);
5742 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5745 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5747 if (!emcore_check_thread_status()) {
5748 err = EMAIL_ERROR_CANCELLED;
5756 if (stream) emcore_close_mailbox(account_id, stream);
5757 if (mail) emstorage_free_mail(&mail, 1, NULL);
5760 emcore_free_account(ref_account);
5761 EM_SAFE_FREE(ref_account);
5764 if (err_code != NULL)
5766 EM_DEBUG_FUNC_END("err [%d]", err);
5770 INTERNAL_FUNC void emcore_free_mail_data_list(email_mail_data_t **mail_list, int count)
5772 EM_DEBUG_FUNC_BEGIN("count[%d]", count);
5774 if (count <= 0 || !mail_list || !*mail_list) {
5775 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5779 email_mail_data_t* p = *mail_list;
5782 for (i = 0; i < count; i++)
5783 emcore_free_mail_data( p+i);
5785 EM_SAFE_FREE(*mail_list);
5787 EM_DEBUG_FUNC_END();
5790 INTERNAL_FUNC void emcore_free_mail_data(email_mail_data_t *mail_data)
5792 EM_DEBUG_FUNC_BEGIN();
5795 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
5799 EM_SAFE_FREE(mail_data->subject);
5800 EM_SAFE_FREE(mail_data->server_mailbox_name);
5801 EM_SAFE_FREE(mail_data->server_mail_id);
5802 EM_SAFE_FREE(mail_data->message_id);
5803 EM_SAFE_FREE(mail_data->full_address_from);
5804 EM_SAFE_FREE(mail_data->full_address_reply);
5805 EM_SAFE_FREE(mail_data->full_address_to);
5806 EM_SAFE_FREE(mail_data->full_address_cc);
5807 EM_SAFE_FREE(mail_data->full_address_bcc);
5808 EM_SAFE_FREE(mail_data->full_address_return);
5809 EM_SAFE_FREE(mail_data->email_address_sender);
5810 EM_SAFE_FREE(mail_data->email_address_recipient);
5811 EM_SAFE_FREE(mail_data->alias_sender);
5812 EM_SAFE_FREE(mail_data->alias_recipient);
5813 EM_SAFE_FREE(mail_data->file_path_plain);
5814 EM_SAFE_FREE(mail_data->file_path_html);
5815 EM_SAFE_FREE(mail_data->preview_text);
5817 EM_DEBUG_FUNC_END();
5821 INTERNAL_FUNC int emcore_free_attachment_data(email_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
5823 EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);
5825 if (!attachment_data_list || !*attachment_data_list) {
5826 if (err_code != NULL)
5827 *err_code = EMAIL_ERROR_INVALID_PARAM;
5831 email_attachment_data_t* p = *attachment_data_list;
5834 for (i = 0; i < attachment_data_count; i++) {
5835 EM_SAFE_FREE(p[i].attachment_name);
5836 EM_SAFE_FREE(p[i].attachment_path);
5837 EM_SAFE_FREE(p[i].attachment_mime_type);
5840 EM_SAFE_FREE(p); *attachment_data_list = NULL;
5843 *err_code = EMAIL_ERROR_NONE;
5845 EM_DEBUG_FUNC_END();
5851 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
5853 INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code)
5855 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5857 if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
5858 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5859 if (err_code != NULL)
5860 *err_code = EMAIL_ERROR_INVALID_PARAM;
5865 int err = EMAIL_ERROR_NONE;
5867 emstorage_begin_transaction(NULL, NULL, NULL);
5869 if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err)) {
5870 EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
5877 if (ret == true) { /* COMMIT TRANSACTION; */
5878 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5879 err = EMAIL_ERROR_DB_FAILURE;
5883 else { /* ROLLBACK TRANSACTION; */
5884 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5885 err = EMAIL_ERROR_DB_FAILURE;
5888 if (err_code != NULL)
5890 EM_DEBUG_FUNC_END();
5894 INTERNAL_FUNC int emcore_insert_pbd_activity(email_event_partial_body_thd *local_activity, int *activity_id, int *err_code)
5896 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
5898 if (!local_activity || !activity_id) {
5899 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
5900 if (err_code != NULL)
5901 *err_code = EMAIL_ERROR_INVALID_PARAM;
5906 int err = EMAIL_ERROR_NONE;
5908 emstorage_begin_transaction(NULL, NULL, NULL);
5910 if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err)) {
5911 EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
5920 if (ret == true) { /* COMMIT TRANSACTION; */
5921 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5922 err = EMAIL_ERROR_DB_FAILURE;
5926 else { /* ROLLBACK TRANSACTION; */
5927 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5928 err = EMAIL_ERROR_DB_FAILURE;
5931 if (err_code != NULL)
5933 EM_DEBUG_FUNC_END();
5939 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
5941 /* API to set or unset a field of flags on server in single IMAP request to server */
5943 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)
5945 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);
5947 if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMAIL_FLAGS_FIELD_COUNT) {
5948 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
5949 if (err_code != NULL) {
5950 *err_code = EMAIL_ERROR_INVALID_PARAM;
5955 MAILSTREAM *stream = NULL;
5956 IMAPLOCAL *imaplocal = NULL;
5957 char tag[MAX_TAG_SIZE] = {0, };
5958 char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
5960 char **string_list = NULL;
5962 int err = EMAIL_ERROR_NONE;
5963 int command_success = false;
5967 int id_set_count = 0;
5968 int len_of_each_range = 0;
5969 int string_count = 0;
5970 email_account_t *temp_account = NULL;
5971 email_id_set_t *id_set = NULL;
5972 emstorage_mail_tbl_t *mail = NULL;
5973 email_uid_range_set *uid_range_set = NULL;
5974 email_uid_range_set *uid_range_node = NULL;
5975 char *field_type_name[EMAIL_FLAGS_FIELD_COUNT] = { "\\Seen"
5976 , "\\Deleted", "\\Flagged", "\\Answered"
5977 , "\\Recent", "\\Draft", "$Forwarded" };
5979 mail_id = mail_ids[0];
5981 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 */ {
5982 EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
5987 account_id = mail[0].account_id;
5989 temp_account = emcore_get_account_reference(account_id);
5991 if (!temp_account) {
5992 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5993 err = EMAIL_ERROR_INVALID_ACCOUNT;
5997 if (temp_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
5998 EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->incoming_server_type);
5999 err = EMAIL_ERROR_NOT_SUPPORTED;
6003 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_id, (void **)&stream, &err) || !stream) {
6004 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
6008 if (false == emstorage_free_mail(&mail, 1, &err)) {
6009 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
6013 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
6014 /* 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 */
6015 /* 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 */
6016 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
6018 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
6019 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
6023 /* Now execute one by one each comma separated string of mail_ids */
6025 for (i = 0; i < string_count; ++i) {
6026 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
6028 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
6029 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
6033 /* 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 */
6035 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
6037 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
6038 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
6042 uid_range_node = uid_range_set;
6044 while (uid_range_node != NULL) {
6045 /* Remove comma from end of uid_range */
6047 uid_range_node->uid_range[EM_SAFE_STRLEN(uid_range_node->uid_range) - 1] = '\0';
6049 /* Form the IMAP command */
6051 SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
6054 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]);
6056 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]);
6058 EM_DEBUG_LOG("[IMAP4] command %s", cmd);
6062 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
6063 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
6065 err = EMAIL_ERROR_UNKNOWN;
6069 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
6070 EM_DEBUG_EXCEPTION("net_sout failed...");
6071 err = EMAIL_ERROR_CONNECTION_BROKEN;
6075 /* Receive Response */
6077 command_success = false;
6079 while (imaplocal->netstream) {
6080 if (!(p = net_getline(imaplocal->netstream))) {
6081 EM_DEBUG_EXCEPTION("net_getline failed...");
6083 err = EMAIL_ERROR_INVALID_RESPONSE;
6087 EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
6089 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag))) {
6090 if (!strncmp(p + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
6091 /*Delete all local activities */
6092 command_success = true;
6099 command_success = false;
6100 err = EMAIL_ERROR_IMAP4_STORE_FAILURE;
6109 uid_range_node = uid_range_node->next;
6112 emcore_free_uid_range_set(&uid_range_set);
6114 EM_SAFE_FREE(id_set);
6123 #ifdef __FEATURE_LOCAL_ACTIVITY__
6125 emstorage_activity_tbl_t new_activity;
6126 for (i = 0; i<num ; i++) {
6127 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
6128 new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
6129 new_activity.account_id = account_id;
6130 new_activity.mail_id = mail_ids[i];
6132 if (!emcore_delete_activity(&new_activity, &err))
6133 EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
6140 if (false == emstorage_free_mail(&mail, 1, &err))
6141 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
6144 emcore_free_comma_separated_strings(&string_list, &string_count);
6147 emcore_free_uid_range_set(&uid_range_set);
6149 emcore_close_mailbox(0, stream);
6153 emcore_free_account(temp_account);
6154 EM_SAFE_FREE(temp_account);
6157 if (err_code != NULL)
6159 EM_DEBUG_FUNC_END();
6164 INTERNAL_FUNC int emcore_mail_filter_by_rule(email_rule_t *filter_info, int *err_code)
6166 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
6168 int ret = false, err = EMAIL_ERROR_NONE;
6169 int account_index, account_count, mail_id_index = 0;
6170 email_account_t *account_ref = NULL, *account_list_ref = NULL;
6171 int filtered_mail_id_count = 0, *filtered_mail_id_list = NULL, parameter_string_length = 0;
6172 char *parameter_string = NULL, mail_id_string[10] = { 0x00, };
6173 emstorage_mailbox_tbl_t *spam_mailbox = NULL;
6176 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
6177 err = EMAIL_ERROR_INVALID_PARAM;
6181 if (!emcore_get_account_reference_list(&account_list_ref, &account_count, &err)) {
6182 EM_DEBUG_EXCEPTION("emcore_get_account_reference_list failed [%d]", err);
6186 for (account_index = 0; account_index < account_count; account_index++) {
6187 account_ref = account_list_ref + account_index;
6189 if (!emstorage_get_mailbox_by_mailbox_type(account_ref->account_id, EMAIL_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err))
6190 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed [%d]", account_ref->account_id, err);
6191 else if (spam_mailbox && spam_mailbox->mailbox_id > 0) {
6192 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))
6193 EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
6195 if (filtered_mail_id_count) {
6196 parameter_string_length = 10 /*mailbox_id length*/ + 7 + (10 * filtered_mail_id_count);
6197 parameter_string = em_malloc(sizeof(char) * parameter_string_length);
6199 if (parameter_string == NULL) {
6200 err = EMAIL_ERROR_OUT_OF_MEMORY;
6201 EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
6204 SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%d%c", 0x01, spam_mailbox->mailbox_id, 0x01);
6206 for (mail_id_index = 0; mail_id_index < filtered_mail_id_count; mail_id_index++) {
6207 memset(mail_id_string, 0, 10);
6208 SNPRINTF(mail_id_string, 10, "%d", filtered_mail_id_list[mail_id_index]);
6209 strcat(parameter_string, mail_id_string);
6210 strcat(parameter_string, ",");
6213 EM_DEBUG_LOG("filtered_mail_id_count [%d]", filtered_mail_id_count);
6214 EM_DEBUG_LOG("param string [%s]", parameter_string);
6216 if (!emcore_notify_storage_event(NOTI_MAIL_MOVE, account_ref->account_id, 0, parameter_string, 0))
6217 EM_DEBUG_EXCEPTION("emcore_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
6219 EM_SAFE_FREE(filtered_mail_id_list);
6220 EM_SAFE_FREE(parameter_string);
6223 emstorage_free_mailbox(&spam_mailbox, 1, &err);
6224 spam_mailbox = NULL;
6228 emcore_display_unread_in_badge();
6233 EM_SAFE_FREE(account_list_ref);
6234 EM_SAFE_FREE(filtered_mail_id_list);
6235 EM_SAFE_FREE(parameter_string);
6239 emstorage_free_mailbox(&spam_mailbox, 1, &err);
6243 EM_DEBUG_FUNC_END();