4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File : email-core-mail.c
26 * Desc : Mail Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
42 #include <sys/types.h>
47 #include <contacts-svc.h>
49 #include "email-internal-types.h"
52 #include "email-utilities.h"
53 #include "email-core-global.h"
54 #include "email-core-utils.h"
55 #include "email-core-mail.h"
56 #include "email-core-mime.h"
57 #include "email-core-mailbox.h"
58 #include "email-storage.h"
59 #include "email-core-mailbox-sync.h"
60 #include "email-core-event.h"
61 #include "email-core-account.h"
63 #include "email-convert.h"
64 #include "email-debug-log.h"
66 #ifdef __FEATURE_DRM__
67 #include <drm-service.h>
68 #endif /* __FEATURE_DRM__ */
70 #define ST_SILENT (long) 0x2 /* don't return results */
71 #define ST_SET (long) 0x4 /* set vs. clear */
73 static char g_new_server_uid[129];
75 bool only_body_download = false;
77 int multi_part_body_size = 0;
78 int is_multi_part_body_download_all = 0;
79 int _pop3_receiving_mail_id = 0;
80 int _pop3_received_body_size = 0;
81 int _pop3_last_notified_body_size = 0;
82 int _pop3_total_body_size = 0;
84 int _imap4_received_body_size = 0;
85 int _imap4_last_notified_body_size = 0;
86 int _imap4_total_body_size = 0;
87 int _imap4_download_noti_interval_value = 0;
89 BODY **g_inline_list = NULL ;
90 int g_inline_count = 0;
92 static int emcore_delete_mail_from_server(emf_account_t *account, emf_mailbox_t *mailbox, int msgno, int *err_code);
93 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);
95 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
98 #ifdef FEATURE_CORE_DEBUG
99 static char *_getType(int type)
102 case 0: return "TYPETEXT";
103 case 1: return "TYPEMULTIPART";
104 case 2: return "TYPEMESSAGE";
105 case 3: return "TYPEAPPLICATION";
106 case 4: return "TYPEAUDIO";
107 case 5: return "TYPEVIDEO";
108 case 6: return "TYPEMODEL";
109 case 7: return "TYPEOTHER";
111 return g_strdup_printf("%d", type);
114 static char *_getEncoding(int encoding)
117 case 0: return "ENC7BIT";
118 case 1: return "ENC8BIT";
119 case 2: return "ENCBINARY";
120 case 3: return "ENCBASE64";
121 case 4: return "ENCQUOTEDPRINTABLE";
122 case 5: return "ENCOTHER";
124 return g_strdup_printf("%d", encoding);
127 static void _print_parameter(PARAMETER *param)
129 while (param != NULL) {
130 EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
131 EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
137 static void _print_stringlist(STRINGLIST *stringlist)
139 while (stringlist != NULL) {
140 EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
141 EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
143 stringlist = stringlist->next;
147 static void _print_body(BODY *body, int recursive)
149 EM_DEBUG_LOG(" ========================================================== ");
152 EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
153 EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
154 EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
156 EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
158 _print_parameter(body->parameter);
160 EM_DEBUG_LOG("body->id[%s]", body->id);
161 EM_DEBUG_LOG("body->description[%s]", body->description);
163 EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
164 EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
166 _print_parameter(body->disposition.parameter);
168 EM_DEBUG_LOG("body->language[%p]", body->language);
170 _print_stringlist(body->language);
172 EM_DEBUG_LOG("body->location[%s]", body->location);
174 EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
175 EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
176 EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
178 EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
179 EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
180 EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
182 EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
184 EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
185 EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
187 EM_DEBUG_LOG("body->md5[%s]", body->md5);
188 EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
191 PART *part = body->nested.part;
193 while (part != NULL) {
194 _print_body(&(part->body), recursive);
200 EM_DEBUG_LOG(" ========================================================== ");
202 #endif /* FEATURE_CORE_DEBUG */
205 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
207 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
210 int err = EMF_ERROR_NONE;
212 POP3LOCAL *pop3local = NULL;
217 EM_DEBUG_EXCEPTION("stream[%p]", stream);
218 err = EMF_ERROR_INVALID_PARAM;
222 if (!(pop3local = stream->local) || !pop3local->netstream) {
223 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
224 err = EMF_ERROR_UNKNOWN;
228 memset(cmd, 0x00, sizeof(cmd));
230 SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
232 #ifdef FEATURE_CORE_DEBUG
233 EM_DEBUG_LOG("[POP3] >>> %s", cmd);
236 /* send command : delete specified mail */
237 if (!net_sout(pop3local->netstream, cmd, (int)strlen(cmd))) {
238 EM_DEBUG_EXCEPTION("net_sout failed...");
239 err = EMF_ERROR_CONNECTION_BROKEN; /* EMF_ERROR_UNKNOWN; */
243 /* receive response */
244 if (!(p = net_getline(pop3local->netstream))) {
245 EM_DEBUG_EXCEPTION("net_getline failed...");
246 err = EMF_ERROR_INVALID_RESPONSE;
250 #ifdef FEATURE_CORE_DEBUG
251 EM_DEBUG_LOG("[POP3] <<< %s", p);
254 if (*p == '-') { /* '-ERR' */
255 err = EMF_ERROR_POP3_DELE_FAILURE; /* EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
259 if (*p != '+') { /* '+OK' ... */
260 err = EMF_ERROR_INVALID_RESPONSE;
276 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
278 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
281 EM_DEBUG_FUNC_BEGIN();
285 int err = EMF_ERROR_NONE;
287 unsigned long first_uid = 0;
288 unsigned long last_uid = 0;
290 unsigned long *old_server_uid = NULL;
291 unsigned long *new_server_uid = NULL;
293 SEARCHSET *temp = sourceset;
295 char old_server_uid_char[129];
296 char new_server_uid_char[129];
298 if (NULL == sourceset || NULL == destset) {
300 sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids
301 if sourceset is NULL then corresponding dest set will be NULL
304 EM_DEBUG_LOG("emcore_mail_copyuid_ex failed : Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
308 /* To get count of mails actually moved */
311 if (temp->first > 0) {
312 first_uid = temp->first;
316 if (temp->last > 0) {
317 last_uid = temp->last;
319 while (first_uid < last_uid) {
332 EM_DEBUG_LOG("Count of mails copied [%d]", count);
333 old_server_uid = em_malloc(count * sizeof(unsigned long));
334 new_server_uid = em_malloc(count * sizeof(unsigned long));
336 /* While loop below will collect all old server uid from sourceset into old_server_uid array */
339 if (sourceset->first > 0) {
340 first_uid = sourceset->first;
343 old_server_uid[index] = first_uid;
345 if (sourceset->last > 0) {
346 last_uid = sourceset->last;
348 while (first_uid < last_uid) {
352 old_server_uid[index] = first_uid;
357 sourceset = sourceset->next;
360 /* While loop below will collect all new server uid from destset into new_server_uid array */
363 first_uid = last_uid = 0;
366 if (destset->first > 0) {
367 first_uid = destset->first;
370 new_server_uid[index] = first_uid;
372 if (destset->last > 0) {
373 last_uid = destset->last;
375 while (first_uid < last_uid) {
379 new_server_uid[index] = first_uid;
384 destset = destset->next;
387 /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
389 for (i = 0; i <= index; ++i) {
391 memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
392 sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
394 EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
396 memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
397 sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
399 EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
401 if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
402 EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
405 if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
406 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
409 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
411 if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
412 EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
418 EM_SAFE_FREE(old_server_uid);
419 EM_SAFE_FREE(new_server_uid);
423 INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, char *src_mailbox, int mail_ids[], int num, char *dest_mailbox, int *error_code)
425 EM_DEBUG_FUNC_BEGIN();
426 MAILSTREAM *stream = NULL;
427 int err_code = EMF_ERROR_NONE;
428 emf_account_t *ref_account = NULL;
431 emf_id_set_t *id_set = NULL;
432 int id_set_count = 0;
434 emf_uid_range_set *uid_range_set = NULL;
435 int len_of_each_range = 0;
437 emf_uid_range_set *uid_range_node = NULL;
439 char **string_list = NULL;
440 int string_count = 0;
442 if (num <= 0 || account_id <= 0 || NULL == src_mailbox || NULL == dest_mailbox || NULL == mail_ids) {
443 if (error_code != NULL) {
444 *error_code = EMF_ERROR_INVALID_PARAM;
446 EM_DEBUG_LOG("Invalid Parameters- num[%d], account_id[%d], src_mailbox[%p], dest_mailbox[%p], mail_ids[%p]", num, account_id, src_mailbox, dest_mailbox, mail_ids);
450 ref_account = emcore_get_account_reference(account_id);
452 if (NULL == ref_account) {
453 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
455 *error_code = EMF_ERROR_INVALID_ACCOUNT;
460 if (ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
461 *error_code = EMF_ERROR_INVALID_PARAM;
466 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox, (void **)&stream, &err_code)) {
467 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
472 if (NULL != stream) {
473 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
474 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
475 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
476 /* 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 */
477 /* 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 */
478 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
480 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code)) {
481 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
485 /* Now execute one by one each comma separated string of mail_ids */
487 for (i = 0; i < string_count; ++i) {
488 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
490 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
491 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
495 /* 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 */
497 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
499 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
500 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
504 uid_range_node = uid_range_set;
506 while (uid_range_node != NULL) {
507 /* Remove comma from end of uid_range */
508 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
509 EM_DEBUG_LOG("uid_range_node->uid_range - %s", uid_range_node->uid_range);
510 if (!mail_copy_full(stream, uid_range_node->uid_range, dest_mailbox, CP_UID | CP_MOVE)) {
511 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be moved failed");
512 EM_DEBUG_EXCEPTION("Mail MOVE failed ");
515 else if (!mail_expunge_full(stream, uid_range_node->uid_range, EX_UID)) {
516 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex : Mail cannot be expunged after move. failed!");
517 EM_DEBUG_EXCEPTION("Mail Expunge after move failed ");
521 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
524 uid_range_node = uid_range_node->next;
527 emcore_free_uid_range_set(&uid_range_set);
529 EM_SAFE_FREE(id_set);
536 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
544 emcore_close_mailbox(0, stream);
547 #ifdef __FEATURE_LOCAL_ACTIVITY__
548 if (ret || ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
549 emstorage_activity_tbl_t new_activity;
550 for (i = 0; i<num ; i++) {
551 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
552 new_activity.activity_type = ACTIVITY_MOVEMAIL;
553 new_activity.account_id = account_id;
554 new_activity.mail_id = mail_ids[i];
555 new_activity.src_mbox = src_mailbox;
556 new_activity.dest_mbox = dest_mailbox;
558 if (!emcore_delete_activity(&new_activity, &err_code)) {
559 EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
566 if (error_code != NULL) {
567 *error_code = err_code;
573 #ifdef __FEATURE_LOCAL_ACTIVITY__
574 int imap4_mail_delete_ex(emf_mailbox_t *mailbox, int mail_ids[], int num, int from_server, int *err_code)
576 int imap4_mail_delete_ex(emf_mailbox_t *mailbox, int mail_ids[], int num, int *err_code)
579 EM_DEBUG_FUNC_BEGIN();
582 int err = EMF_ERROR_NONE;
584 IMAPLOCAL *imaplocal = NULL;
586 MAILSTREAM *stream = NULL;
587 char tag[MAX_TAG_SIZE];
588 char cmd[MAX_IMAP_COMMAND_LENGTH];
589 emf_id_set_t *id_set = NULL;
590 int id_set_count = 0;
592 emf_uid_range_set *uid_range_set = NULL;
593 int len_of_each_range = 0;
594 emf_uid_range_set *uid_range_node = NULL;
595 char **string_list = NULL;
596 int string_count = 0;
597 int delete_success = false;
599 if (num <= 0 || !mail_ids || !mailbox) {
600 EM_DEBUG_EXCEPTION(" Invalid parameter ");
601 err = EMF_ERROR_INVALID_PARAM;
605 stream = mailbox->mail_stream;
607 EM_DEBUG_EXCEPTION(" Stream is NULL ");
608 err = EMF_ERROR_INVALID_PARAM;
613 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
614 /* 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 */
615 /* 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 */
616 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
618 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
619 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
623 /* Now execute one by one each comma separated string of mail_ids */
625 for (i = 0; i < string_count; ++i) {
626 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
628 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
629 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
633 /* 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 */
635 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
637 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
638 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
642 uid_range_node = uid_range_set;
644 while (uid_range_node != NULL) {
645 /* Remove comma from end of uid_range */
647 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
649 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
650 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
652 err = EMF_ERROR_CONNECTION_BROKEN;
656 memset(tag, 0x00, sizeof(tag));
657 memset(cmd, 0x00, sizeof(cmd));
659 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
660 SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
662 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
665 /* send command : set deleted flag */
666 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
667 EM_DEBUG_EXCEPTION("net_sout failed...");
669 err = EMF_ERROR_CONNECTION_BROKEN; /* EMF_ERROR_UNKNOWN */
674 while (imaplocal->netstream) {
675 /* receive response */
676 if (!(p = net_getline(imaplocal->netstream))) {
677 EM_DEBUG_EXCEPTION("net_getline failed...");
679 err = EMF_ERROR_INVALID_RESPONSE; /* EMF_ERROR_UNKNOWN; */
684 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
686 /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
687 /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl
688 if (strstr(p, "FETCH") != NULL) {
689 EM_DEBUG_LOG(" FETCH Response recieved ");
690 delete_success = true;
697 if (!strncmp(p, tag, strlen(tag))) {
698 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
699 /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
700 EM_DEBUG_LOG(" OK Response recieved ");
701 delete_success = true;
707 delete_success = false;
708 err = EMF_ERROR_IMAP4_STORE_FAILURE; /* EMF_ERROR_INVALID_RESPONSE; */
716 memset(tag, 0x00, sizeof(tag));
717 memset(cmd, 0x00, sizeof(cmd));
719 EM_DEBUG_LOG("Calling Expunge");
721 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
722 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
724 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
726 /* send command : EXPUNGE */
727 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
728 EM_DEBUG_EXCEPTION("net_sout failed...");
730 err = EMF_ERROR_CONNECTION_BROKEN;
734 while (imaplocal->netstream) {
735 /* receive response */
736 if (!(p = net_getline(imaplocal->netstream))) {
737 EM_DEBUG_EXCEPTION("net_getline failed...");
739 err = EMF_ERROR_INVALID_RESPONSE; /* EMF_ERROR_UNKNOWN; */
743 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
745 if (!strncmp(p, tag, strlen(tag))) {
746 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
747 #ifdef __FEATURE_LOCAL_ACTIVITY__
749 emstorage_mail_tbl_t **mail = NULL;
751 mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
753 EM_DEBUG_EXCEPTION("em_malloc failed");
754 err = EMF_ERROR_UNKNOWN;
758 if (delete_success) {
759 for (index = 0 ; index < num; index++) {
760 if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err)) {
761 EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
763 if (err == EMF_ERROR_MAIL_NOT_FOUND) {
764 EM_DEBUG_LOG("EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER : ");
770 /* Clear entry from mail_read_mail_uid_tbl */
771 if (mail[index]->server_mail_id != NULL) {
772 if (!emstorage_remove_downloaded_mail(mailbox->account_id, mailbox->name, mail[index]->server_mail_id, true, &err)) {
773 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
777 /* Delete local activity */
778 emstorage_activity_tbl_t new_activity;
779 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
780 if (from_server == EMF_DELETE_FOR_SEND_THREAD) {
781 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
782 EM_DEBUG_LOG("from_server == EMF_DELETE_FOR_SEND_THREAD ");
785 new_activity.activity_type = ACTIVITY_DELETEMAIL;
788 new_activity.mail_id = mail[index]->mail_id;
789 new_activity.server_mailid = NULL;
790 new_activity.src_mbox = NULL;
791 new_activity.dest_mbox = NULL;
793 if (!emcore_delete_activity(&new_activity, &err)) {
794 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
798 /* 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.
799 * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted.
801 /* Delete local activity */
802 emstorage_activity_tbl_t new_activity;
803 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
804 new_activity.activity_type = ACTIVITY_DELETEMAIL;
805 new_activity.mail_id = mail_ids[index]; /* valid mail id passed for outbox mails*/
806 new_activity.server_mailid = NULL;
807 new_activity.src_mbox = NULL;
808 new_activity.dest_mbox = NULL;
810 if (!emcore_delete_activity(&new_activity, &err)) {
811 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
816 for (index = 0; index < num; index++) {
817 if (!emstorage_free_mail(&mail[index], 1, &err)) {
818 EM_DEBUG_EXCEPTION(" emstorage_free_mail [%d]", err);
829 else { /* 'NO' or 'BAD' */
830 err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMF_ERROR_INVALID_RESPONSE; */
837 uid_range_node = uid_range_node->next;
840 emcore_free_uid_range_set(&uid_range_set);
842 EM_SAFE_FREE(id_set);
852 emcore_free_comma_separated_strings(&string_list, &string_count);
855 emcore_free_uid_range_set(&uid_range_set);
868 static int imap4_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
870 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
873 int err = EMF_ERROR_NONE;
875 IMAPLOCAL *imaplocal = NULL;
876 char tag[16], cmd[64];
879 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
880 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
882 err = EMF_ERROR_INVALID_PARAM; /* EMF_ERROR_UNKNOWN */
886 memset(tag, 0x00, sizeof(tag));
887 memset(cmd, 0x00, sizeof(cmd));
889 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
890 SNPRINTF(cmd, sizeof(cmd), "%s STORE %d +FLAGS.SILENT (\\Deleted)\015\012", tag, msgno);
892 #ifdef FEATURE_CORE_DEBUG
893 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
896 /* send command : set deleted flag */
897 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
898 EM_DEBUG_EXCEPTION("net_sout failed...");
900 err = EMF_ERROR_CONNECTION_BROKEN; /* EMF_ERROR_UNKNOWN */
904 while (imaplocal->netstream) {
905 /* receive response */
906 if (!(p = net_getline(imaplocal->netstream))) {
907 EM_DEBUG_EXCEPTION("net_getline failed...");
909 err = EMF_ERROR_INVALID_RESPONSE; /* EMF_ERROR_UNKNOWN; */
913 #ifdef FEATURE_CORE_DEBUG
914 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
916 if (!strncmp(p, tag, strlen(tag))) {
917 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
921 else { /* 'NO' or 'BAD' */
922 err = EMF_ERROR_IMAP4_STORE_FAILURE; /* EMF_ERROR_INVALID_RESPONSE; */
930 memset(tag, 0x00, sizeof(tag));
931 memset(cmd, 0x00, sizeof(cmd));
933 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
934 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
936 #ifdef FEATURE_CORE_DEBUG
937 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
940 /* send command : delete flaged mail */
941 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
942 EM_DEBUG_EXCEPTION("net_sout failed...");
944 err = EMF_ERROR_CONNECTION_BROKEN;
948 while (imaplocal->netstream) {
949 /* receive response */
950 if (!(p = net_getline(imaplocal->netstream))) {
951 EM_DEBUG_EXCEPTION("net_getline failed...");
953 err = EMF_ERROR_INVALID_RESPONSE; /* EMF_ERROR_UNKNOWN; */
957 #ifdef FEATURE_CORE_DEBUG
958 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
961 if (!strncmp(p, tag, strlen(tag))) {
962 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
966 else { /* 'NO' or 'BAD' */
967 err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMF_ERROR_INVALID_RESPONSE; */
990 static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
992 EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
995 int err = EMF_ERROR_NONE;
997 IMAPLOCAL *imaplocal = NULL;
998 char tag[16], cmd[64];
1001 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
1002 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
1004 err = EMF_ERROR_INVALID_PARAM; /* EMF_ERROR_UNKNOWN */
1008 memset(tag, 0x00, sizeof(tag));
1009 memset(cmd, 0x00, sizeof(cmd));
1011 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
1012 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
1014 #ifdef FEATURE_CORE_DEBUG
1015 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
1018 /* send command : delete flaged mail */
1019 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
1020 EM_DEBUG_EXCEPTION("net_sout failed...");
1022 err = EMF_ERROR_CONNECTION_BROKEN;
1026 while (imaplocal->netstream) {
1027 /* receive response */
1028 if (!(p = net_getline(imaplocal->netstream))) {
1029 EM_DEBUG_EXCEPTION("net_getline failed...");
1031 err = EMF_ERROR_INVALID_RESPONSE; /* EMF_ERROR_UNKNOWN; */
1035 #ifdef FEATURE_CORE_DEBUG
1036 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
1039 if (!strncmp(p, tag, strlen(tag))) {
1040 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
1044 else { /* 'NO' or 'BAD' */
1045 err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE; /* EMF_ERROR_INVALID_RESPONSE; */
1064 int emcore_get_mail_contact_info(emf_mail_contact_info_t *contact_info, char *full_address, int *err_code)
1066 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
1069 int err = EMF_ERROR_NONE;
1071 if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
1072 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
1076 if (err_code != NULL)
1082 INTERNAL_FUNC int emcore_get_mail_display_name(CTSvalue *contact_name_value, char **contact_display_name)
1084 EM_DEBUG_FUNC_BEGIN("contact_name_value[%p], contact_display_name[%p]", contact_name_value, contact_display_name);
1085 char *display = NULL;
1086 const char *first = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_FIRST_STR);
1087 const char *last = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_LAST_STR);
1089 EM_DEBUG_LOG(">>>>>> first[%s] last[%s]", first, last);
1090 if (first != NULL && last != NULL) {
1091 /* if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_name_order()) */
1092 if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_order(CTS_ORDER_OF_DISPLAY))
1093 display = g_strconcat(first, " ", last, NULL);
1095 display = g_strconcat(last, " ", first, NULL);
1098 else if (first != NULL || last != NULL) {
1100 display = (char *)EM_SAFE_STRDUP(first);
1102 display = (char *)EM_SAFE_STRDUP(last);
1105 display = g_strdup(contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_DISPLAY_STR));
1107 if (contact_display_name != NULL)
1108 *contact_display_name = display;
1113 int emcore_get_mail_contact_info_with_update(emf_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
1115 EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
1118 int err = EMF_ERROR_NONE;
1119 ADDRESS *addr = NULL;
1120 char *address = NULL;
1121 char *temp_emailaddr = NULL;
1122 int start_text_ascii = 2;
1123 int end_text_ascii = 3;
1125 int is_searched = false;
1126 int address_length = 0;
1127 char *email_address = NULL;
1128 int contact_name_len = 0;
1129 char temp_string[1024] = { 0 , };
1131 CTSstruct *contact = NULL;
1132 CTSvalue *contact_name_value = NULL;
1133 int contact_index = -1;
1134 char *contact_display_name = NULL;
1135 char *contact_display_name_from_contact_info = NULL;
1136 int contact_display_name_len = 0;
1138 int contact_name_buffer_size = 0;
1139 char *contact_name = NULL;
1141 if (!contact_info) {
1142 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1143 err = EMF_ERROR_INVALID_PARAM;
1147 if (!full_address) {
1150 temp_emailaddr = NULL;
1153 address_length = 2 * strlen(full_address);
1154 temp_emailaddr = (char *)calloc(1, address_length);
1157 em_skip_whitespace(full_address , &address);
1158 EM_DEBUG_LOG("address[address][%s]", address);
1162 gchar **tokens = g_strsplit(address, ", ", -1);
1163 char *p = g_strjoinv("%2C", tokens);
1168 while (p && p[i] != '\0')
1174 EM_DEBUG_LOG(" 2 converted address %s ", p);
1176 rfc822_parse_adrlist(&addr, p, NULL);
1179 EM_DEBUG_LOG(" 3 full_address %s ", full_address);
1182 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1183 err = EMF_ERROR_INVALID_ADDRESS;
1187 contact_name_buffer_size = address_length;
1188 contact_name = (char*)em_malloc(contact_name_buffer_size);
1190 if (!contact_name) {
1191 EM_DEBUG_EXCEPTION("Memory allocation error!");
1192 err = EMF_ERROR_OUT_OF_MEMORY;
1196 while (addr != NULL) {
1197 if (addr->mailbox && addr->host) {
1198 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.")))
1200 EM_DEBUG_LOG("Invalid address ");
1206 EM_DEBUG_LOG("Error in parsing..! ");
1211 EM_SAFE_FREE(email_address);
1212 email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1214 EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
1215 EM_DEBUG_LOG(" email_address[%s]", email_address);
1217 is_searched = false;
1218 EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
1220 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1221 if (err > CTS_SUCCESS) {
1222 contact_index = err;
1223 if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1224 /* get contact name */
1225 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) { /* set contact display name name */
1226 contact_info->contact_id = contact_index; /* NOTE : This is valid only if there is only one address. */
1227 emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1229 contact_display_name = contact_display_name_from_contact_info;
1232 EM_DEBUG_LOG(">>> contact_index[%d]", contact_index);
1233 EM_DEBUG_LOG(">>> contact_name[%s]", contact_display_name);
1235 /* Make display name string */
1236 if (contact_display_name != NULL) {
1239 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1240 contact_display_name_len = strlen(contact_display_name);
1241 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1242 char *temp = contact_name;
1243 contact_name_buffer_size += contact_name_buffer_size;
1244 contact_name = (char *)calloc(1, contact_name_buffer_size);
1245 if (contact_name == NULL) {
1246 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1250 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1254 /* 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); */
1255 if (addr->next == NULL) {
1256 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1259 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1262 contact_display_name_len = strlen(temp_string);
1263 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1264 char *temp = contact_name;
1265 contact_name_buffer_size += contact_name_buffer_size;
1266 contact_name = (char *)calloc(1, contact_name_buffer_size);
1267 if (contact_name == NULL) {
1268 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1270 err = EMF_ERROR_OUT_OF_MEMORY;
1273 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1276 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1277 contact_name_len += contact_display_name_len;
1278 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1283 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1287 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1291 EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is 203) or error [%d]", err);
1294 /* if contact doesn't exist, use alias or email address as display name */
1295 if (addr->personal != NULL) {
1297 tokens = g_strsplit(addr->personal, "%2C", -1);
1299 EM_SAFE_FREE(addr->personal);
1301 addr->personal = g_strjoinv(", ", tokens);
1304 /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
1305 alias = addr->personal;
1308 /* alias = addr->mailbox ? addr->mailbox : ""; */
1309 alias = email_address;
1311 contact_info->alias = EM_SAFE_STRDUP(alias);
1314 contact_display_name = alias;
1315 contact_info->contact_id = -1; /* NOTE : This is valid only if there is only one address. */
1316 contact_info->storage_type = -1;
1318 /* Make display name string */
1319 EM_DEBUG_LOG("contact_display_name : [%s]", contact_display_name);
1320 EM_DEBUG_LOG("email_address : [%s]", email_address);
1322 /* if mail_id is 0, return only contact info without saving contact info to DB */
1323 if (mail_id == 0 || (contact_name_len == 0)) { /* save only the first address information - 09-SEP-2010 */
1324 /* 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); */
1325 if (addr->next == NULL) {
1326 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1329 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1331 EM_DEBUG_LOG("temp_string[%s]", temp_string);
1333 contact_display_name_len = strlen(temp_string);
1334 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) { /* re-alloc memory */
1335 char *temp = contact_name;
1336 contact_name_buffer_size += contact_name_buffer_size;
1337 contact_name = (char *)calloc(1, contact_name_buffer_size);
1338 if (contact_name == NULL) {
1339 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1341 err = EMF_ERROR_OUT_OF_MEMORY;
1344 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1348 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1349 contact_name_len += contact_display_name_len;
1350 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1354 if (temp_emailaddr && email_address) {
1355 if (mail_id == 0) { /* if mail_id is 0, return only contact info without saving contact info to DB */
1356 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1357 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1358 if (addr->next != NULL)
1359 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1360 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1362 else { /* save only the first address information - 09-SEP-2010 */
1363 if (is_saved == 0) {
1365 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1366 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1368 if (addr->next != NULL)
1369 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1371 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1376 if (contact != NULL) {
1377 contacts_svc_struct_free(contact);
1380 EM_SAFE_FREE(contact_display_name_from_contact_info);
1383 } /* while (addr != NULL) */
1385 if (temp_emailaddr) {
1386 EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
1387 contact_info->email_address = temp_emailaddr;
1388 temp_emailaddr = NULL;
1391 if (contact_name != NULL) {
1392 contact_info->contact_name = g_strdup(contact_name);
1395 contact_info->contact_name = g_strdup_printf("%c%d%c%s%c", start_text_ascii, 0, start_text_ascii, full_address, end_text_ascii);
1396 contact_info->contact_id = -1;
1403 if (contact != NULL)
1404 contacts_svc_struct_free(contact);
1405 EM_SAFE_FREE(email_address);
1406 EM_SAFE_FREE(address);
1407 EM_SAFE_FREE(temp_emailaddr);
1408 EM_SAFE_FREE(contact_name);
1409 EM_SAFE_FREE(contact_display_name_from_contact_info);
1411 if (err_code != NULL)
1417 int emcore_free_contact_info(emf_mail_contact_info_t *contact_info, int *err_code)
1419 EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
1422 int err = EMF_ERROR_NONE;
1424 if (!contact_info) {
1425 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1426 err = EMF_ERROR_INVALID_PARAM;
1430 EM_SAFE_FREE(contact_info->contact_name);
1431 EM_SAFE_FREE(contact_info->email_address);
1432 EM_SAFE_FREE(contact_info->alias);
1434 contact_info->storage_type = -1;
1435 contact_info->contact_id = -1;
1440 if (err_code != NULL)
1442 EM_DEBUG_FUNC_END();
1446 int emcore_sync_contact_info(int mail_id, int *err_code)
1448 EM_PROFILE_BEGIN(emCoreMailContactSync);
1449 EM_DEBUG_FUNC_BEGIN();
1452 int err = EMF_ERROR_NONE;
1454 emstorage_mail_tbl_t *mail = NULL;
1456 emf_mail_contact_info_t contact_info_from;
1457 emf_mail_contact_info_t contact_info_to;
1458 emf_mail_contact_info_t contact_info_cc;
1459 emf_mail_contact_info_t contact_info_bcc;
1461 EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
1463 memset(&contact_info_from, 0x00, sizeof(emf_mail_contact_info_t));
1464 memset(&contact_info_to, 0x00, sizeof(emf_mail_contact_info_t));
1465 memset(&contact_info_cc, 0x00, sizeof(emf_mail_contact_info_t));
1466 memset(&contact_info_bcc, 0x00, sizeof(emf_mail_contact_info_t));
1468 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1469 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1474 if (mail->full_address_from != NULL) {
1475 if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err)) {
1476 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1480 if (mail->full_address_to != NULL) {
1481 if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err)) {
1482 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1486 if (mail->full_address_cc != NULL) {
1487 if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err)) {
1488 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1492 if (mail->full_address_bcc != NULL) {
1493 if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err)) {
1494 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1498 EM_SAFE_FREE(mail->email_address_sender);
1499 mail->email_address_sender = contact_info_from.email_address;
1500 contact_info_from.contact_name = NULL;
1501 contact_info_from.email_address = NULL;
1502 EM_SAFE_FREE(mail->email_address_recipient);
1503 if (mail->full_address_to != NULL) {
1504 mail->email_address_recipient = contact_info_to.email_address;
1505 contact_info_to.contact_name = NULL;
1506 contact_info_to.email_address = NULL;
1508 else if (mail->full_address_cc != NULL) {
1509 mail->email_address_recipient = contact_info_cc.email_address;
1510 contact_info_cc.contact_name = NULL;
1511 contact_info_cc.email_address = NULL;
1513 else if (mail->full_address_bcc != NULL) {
1514 mail->email_address_recipient = contact_info_bcc.email_address;
1515 contact_info_bcc.contact_name = NULL;
1516 contact_info_bcc.email_address = NULL;
1520 if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err)) {
1521 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1530 emstorage_free_mail(&mail, 1, NULL);
1532 emcore_free_contact_info(&contact_info_from, NULL);
1533 emcore_free_contact_info(&contact_info_to, NULL);
1534 emcore_free_contact_info(&contact_info_cc, NULL);
1535 emcore_free_contact_info(&contact_info_bcc, NULL);
1537 if (err_code != NULL)
1540 EM_PROFILE_END(emCoreMailContactSync);
1544 /* 1. parsing : alias and address */
1545 /* 2. sync with contact */
1546 /* 3. make glist of address info */
1547 static int emcore_sync_address_info(emf_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
1549 EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
1552 int error = EMF_ERROR_NONE;
1553 int contact_index = -1;
1554 int is_search = false;
1556 char *address = NULL;
1557 char *contact_display_name_from_contact_info = NULL;
1558 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1559 emf_address_info_t *p_address_info = NULL;
1560 ADDRESS *addr = NULL;
1561 CTSstruct *contact = NULL;
1562 CTSvalue *contact_name_value = NULL;
1564 if (full_address == NULL || address_info_list == NULL) {
1565 EM_DEBUG_EXCEPTION("Invalid param : full_address or address_info_list is NULL");
1566 error = EMF_ERROR_INVALID_PARAM;
1571 address = EM_SAFE_STRDUP(full_address);
1574 gchar **tokens = g_strsplit(address, ", ", -1);
1575 char *p = g_strjoinv("%2C", tokens);
1581 while (p && p[i] != '\0') {
1587 rfc822_parse_adrlist(&addr, p, NULL);
1592 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1593 error = EMF_ERROR_INVALID_PARAM;
1597 /* Get a contact name */
1598 while (addr != NULL) {
1599 if (addr->mailbox && addr->host) {
1600 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR."))
1602 EM_DEBUG_LOG("Invalid address ");
1608 EM_DEBUG_LOG("Error in parsing..! ");
1613 /* set display name */
1614 /* 1) contact name */
1615 /* 2) alias (if a alias in an original mail doesn't exist, this field is set with email address */
1616 /* 3) email address */
1618 if (!(p_address_info = (emf_address_info_t *)malloc(sizeof(emf_address_info_t)))) {
1619 EM_DEBUG_EXCEPTION("malloc failed...");
1620 error = EMF_ERROR_OUT_OF_MEMORY;
1623 memset(p_address_info, 0x00, sizeof(emf_address_info_t));
1625 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1627 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1631 error = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1632 if (error > CTS_SUCCESS) {
1633 contact_index = error;
1634 if ((error = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1635 /* get contact name */
1636 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) { /* set contact display name name */
1637 emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1638 EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1639 EM_DEBUG_LOG(">>> contact display name[%s]", contact_display_name_from_contact_info);
1644 EM_DEBUG_EXCEPTION("contacts_svc_struct_get_value error[%d]", error);
1647 EM_DEBUG_EXCEPTION("contacts_svc_get_contact error [%d]", error);
1650 EM_DEBUG_EXCEPTION("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", error);
1652 if (is_search == true) {
1653 p_address_info->contact_id = contact_index;
1654 p_address_info->storage_type = -1;
1655 p_address_info->display_name = contact_display_name_from_contact_info;
1656 EM_DEBUG_LOG("display_name from contact[%s]", p_address_info->display_name);
1659 /* if contact doesn't exist, use alias or email address as display name */
1660 if (addr->personal != NULL) {
1662 tokens = g_strsplit(addr->personal, "%2C", -1);
1664 EM_SAFE_FREE(addr->personal);
1666 addr->personal = g_strjoinv(", ", tokens);
1669 alias = addr->personal;
1674 p_address_info->contact_id = -1;
1675 p_address_info->storage_type = -1;
1676 /* Use an alias or an email address as a display name */
1678 p_address_info->display_name = EM_SAFE_STRDUP(email_address);
1680 p_address_info->display_name = EM_SAFE_STRDUP(alias);
1682 EM_DEBUG_LOG("display_name from email [%s]", p_address_info->display_name);
1685 p_address_info->address = EM_SAFE_STRDUP(email_address);
1686 p_address_info->address_type = address_type;
1688 EM_DEBUG_LOG("email address[%s]", p_address_info->address);
1690 *address_info_list = g_list_append(*address_info_list, p_address_info);
1691 p_address_info = NULL;
1693 EM_DEBUG_LOG("after append");
1696 if (contact != NULL) {
1697 contacts_svc_struct_free(contact);
1701 EM_DEBUG_LOG("next address[%p]", addr->next);
1710 if (contact != NULL)
1711 contacts_svc_struct_free(contact);
1713 EM_SAFE_FREE(address);
1715 if (err_code != NULL)
1717 EM_DEBUG_FUNC_END();
1721 static gint address_compare(gconstpointer a, gconstpointer b)
1723 EM_DEBUG_FUNC_BEGIN();
1724 emf_sender_list_t *recipients_list1 = (emf_sender_list_t *)a;
1725 emf_sender_list_t *recipients_list2 = (emf_sender_list_t *)b;
1727 EM_DEBUG_FUNC_END();
1728 return strcmp(recipients_list1->address, recipients_list2->address);
1731 INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
1733 EM_DEBUG_FUNC_BEGIN();
1735 int i = 0, err = EMF_ERROR_NONE;
1736 int contact_index = -1;
1737 int is_search = false;
1738 char *address = NULL;
1739 char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1740 char *display_name = NULL;
1742 ADDRESS *addr = NULL;
1743 CTSstruct *contact = NULL;
1744 CTSvalue *contact_name_value = NULL;
1745 GList *new_recipients_list = old_recipients_list;
1746 GList *recipients_list;
1748 emf_sender_list_t *temp_recipients_list = NULL;
1749 emf_sender_list_t *old_recipients_list_t = NULL;
1751 if (full_address == NULL || strlen(full_address) == 0) {
1752 EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
1753 err = EMF_ERROR_INVALID_PARAM;
1757 address = EM_SAFE_STRDUP(full_address);
1759 gchar **tokens = g_strsplit(address, ", ", -1);
1760 char *p = g_strjoinv("%2C", tokens);
1764 while (p && p[i] != '\0') {
1770 rfc822_parse_adrlist(&addr, p, NULL);
1775 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1776 err = EMF_ERROR_INVALID_PARAM;
1780 while (addr != NULL) {
1781 if (addr->mailbox && addr->host) {
1782 if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1783 EM_DEBUG_LOG("Invalid address ");
1788 EM_DEBUG_LOG("Error in parsing..! ");
1793 temp_recipients_list = g_new0(emf_sender_list_t, 1);
1795 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1797 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1799 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1800 if (err > CTS_SUCCESS) {
1801 contact_index = err;
1802 if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1803 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) {
1804 emcore_get_mail_display_name(contact_name_value, &display_name);
1805 EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1806 EM_DEBUG_LOG(">>> contact display name[%s]", display_name);
1810 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1813 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1816 EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", err);
1820 temp_recipients_list->display_name = display_name;
1821 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1823 if (addr->personal != NULL) {
1824 tokens = g_strsplit(addr->personal, "%2C", -1);
1825 EM_SAFE_FREE(addr->personal);
1826 addr->personal = g_strjoinv(", ", tokens);
1828 alias = addr->personal;
1834 temp_recipients_list->display_name = EM_SAFE_STRDUP(email_address);
1836 temp_recipients_list->display_name = EM_SAFE_STRDUP(alias);
1838 EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1841 temp_recipients_list->address = EM_SAFE_STRDUP(email_address);
1842 EM_DEBUG_LOG("email address[%s]", temp_recipients_list->address);
1844 EM_SAFE_FREE(display_name);
1845 if (contact != NULL) {
1846 contacts_svc_struct_free(contact);
1849 EM_DEBUG_LOG("next address[%p]", addr->next);
1851 recipients_list = g_list_first(new_recipients_list);
1852 while (recipients_list != NULL) {
1853 old_recipients_list_t = (emf_sender_list_t *)recipients_list->data;
1854 if (!strcmp(old_recipients_list_t->address, temp_recipients_list->address)) {
1855 old_recipients_list_t->total_count = old_recipients_list_t->total_count + 1;
1856 if (temp_recipients_list != NULL)
1857 g_free(temp_recipients_list);
1861 recipients_list = g_list_next(recipients_list);
1864 new_recipients_list = g_list_insert_sorted(new_recipients_list, temp_recipients_list, address_compare);
1866 temp_recipients_list = NULL;
1869 if (contact != NULL) {
1870 contacts_svc_struct_free(contact);
1878 if (contact != NULL)
1879 contacts_svc_struct_free(contact);
1881 EM_SAFE_FREE(address);
1883 if (err_code != NULL)
1886 EM_DEBUG_FUNC_END();
1887 return new_recipients_list;
1890 INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, emf_address_info_list_t **address_info_list, int *err_code)
1892 EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1894 int ret = false, err = EMF_ERROR_NONE;
1898 emstorage_mail_tbl_t *mail = NULL;
1899 emf_address_info_list_t *p_address_info_list = NULL;
1901 if (mail_id <= 0 || !address_info_list) {
1902 EM_DEBUG_EXCEPTION("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1903 err = EMF_ERROR_INVALID_PARAM;
1907 /* get mail from mail table */
1908 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1909 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1915 if (!(p_address_info_list = (emf_address_info_list_t *)malloc(sizeof(emf_address_info_list_t)))) {
1916 EM_DEBUG_EXCEPTION("malloc failed...");
1917 err = EMF_ERROR_OUT_OF_MEMORY;
1920 memset(p_address_info_list, 0x00, sizeof(emf_address_info_list_t));
1922 if ((contact_error = contacts_svc_connect()) == CTS_SUCCESS) {
1923 EM_DEBUG_LOG("Open Contact Service Success");
1926 EM_DEBUG_EXCEPTION("contact_db_service_connect failed [%d]", contact_error);
1927 err = EMF_ERROR_DB_FAILURE;
1931 if (emcore_sync_address_info(EMF_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
1933 if (emcore_sync_address_info(EMF_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
1935 if (emcore_sync_address_info(EMF_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
1937 if (emcore_sync_address_info(EMF_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
1940 if ((contact_error = contacts_svc_disconnect()) == CTS_SUCCESS)
1941 EM_DEBUG_LOG("Close Contact Service Success");
1943 EM_DEBUG_EXCEPTION("Close Contact Service Fail [%d]", contact_error);
1945 if (failed == false)
1950 *address_info_list = p_address_info_list;
1951 else if (p_address_info_list != NULL)
1952 emstorage_free_address_info_list(&p_address_info_list);
1955 emstorage_free_mail(&mail, 1, NULL);
1957 if (err_code != NULL)
1960 EM_DEBUG_FUNC_END();
1967 * input_mail_id : [in] mail id
1968 * output_mail_data : [out] double pointer to hold mail data.
1970 * succeed : EMF_ERROR_NONE
1973 INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, emf_mail_data_t **output_mail_data)
1975 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
1977 int error = EMF_ERROR_NONE;
1978 int result_mail_count = 0;
1979 char conditional_clause_string[QUERY_SIZE] = { 0, };
1980 emstorage_mail_tbl_t *result_mail_tbl = NULL;
1982 if (input_mail_id == 0 || !output_mail_data) {
1983 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
1984 error = EMF_ERROR_INVALID_PARAM;
1988 SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
1990 if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
1991 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
1995 if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, 1, output_mail_data, &error)) {
1996 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
2001 if (result_mail_tbl)
2002 emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
2004 EM_DEBUG_FUNC_END("error [%d]", error);
2009 /* internal function */
2010 void emcore_free_body_sharep(void **p)
2012 EM_DEBUG_FUNC_BEGIN();
2014 EM_DEBUG_FUNC_END();
2017 int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
2019 EM_DEBUG_FUNC_BEGIN();
2021 #ifdef __FEATURE_DRM__
2022 drm_dcf_info_t fileInfo;
2024 if (input_attachment_tb_data == NULL)
2027 if (drm_svc_is_drm_file (input_attachment_tb_data->attachment_path)) {
2028 if (drm_svc_get_dcf_file_info (input_attachment_tb_data->attachment_path, &fileInfo) == DRM_RESULT_SUCCESS) {
2029 input_attachment_tb_data->attachment_drm_type = 0;
2030 EM_DEBUG_LOG ("fileInfo is [%d]", fileInfo.method);
2031 if (fileInfo.method != DRM_METHOD_UNDEFINED) {
2032 input_attachment_tb_data->attachment_drm_type = fileInfo.method;
2038 EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
2039 input_attachment_tb_data->attachment_drm_type = 0;
2043 EM_DEBUG_FUNC_END();
2049 * get mail attachment from local mailbox
2051 * mailbox : server mailbox
2052 * mail_id : mai id to own attachment
2053 * attachment : the number string to be downloaded
2054 * callback : function callback. if NULL, ignored.
2059 INTERNAL_FUNC int emcore_get_attachment_info(int mail_id, char *attachment_id_string, emf_attachment_info_t **attachment, int *err_code)
2061 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment_id_string[%p], attachment[%p], err_code[%p]", mail_id, attachment_id_string, attachment, err_code);
2063 if (attachment == NULL || attachment_id_string == NULL) {
2064 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id_string[%p], attachment[%p]", mail_id, attachment_id_string, attachment);
2065 if (err_code != NULL)
2066 *err_code = EMF_ERROR_INVALID_PARAM;
2071 int err = EMF_ERROR_NONE;
2072 emstorage_attachment_tbl_t *attachment_tbl = NULL;
2073 int attachment_id = atoi(attachment_id_string);
2075 /* get attachment from attachment tbl */
2076 if (!emstorage_get_attachment(mail_id, attachment_id, &attachment_tbl, true, &err) || !attachment_tbl) {
2077 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
2082 *attachment = em_malloc(sizeof(emf_attachment_info_t));
2084 EM_DEBUG_EXCEPTION("malloc failed...");
2085 err = EMF_ERROR_OUT_OF_MEMORY;
2089 (*attachment)->attachment_id = attachment_id;
2090 (*attachment)->name = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
2091 (*attachment)->size = attachment_tbl->attachment_size;
2092 (*attachment)->downloaded = attachment_tbl->attachment_save_status;
2093 (*attachment)->savename = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
2094 (*attachment)->drm = attachment_tbl->attachment_drm_type;
2095 (*attachment)->inline_content = attachment_tbl->attachment_inline_content_status;
2096 (*attachment)->next = NULL;
2102 emstorage_free_attachment(&attachment_tbl, 1, NULL);
2111 * get mail attachment
2113 * input_mail_id : mail id to own attachment
2114 * output_attachment_data : result attahchment data
2115 * output_attachment_count : result attahchment count
2117 * succeed : EMF_ERROR_NONE
2120 INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, emf_attachment_data_t **output_attachment_data, int *output_attachment_count)
2122 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);
2124 if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL) {
2125 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
2126 return EMF_ERROR_INVALID_PARAM;
2130 int err = EMF_ERROR_NONE;
2131 int attachment_tbl_count = 0;
2132 emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2133 emf_attachment_data_t *temp_attachment_data = NULL;
2135 /* get attachment from attachment tbl */
2136 if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMF_ERROR_NONE ){
2137 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2142 if (attachment_tbl_count) {
2143 EM_DEBUG_LOG("attchment count %d", attachment_tbl_count);
2145 *output_attachment_data = em_malloc(sizeof(emf_attachment_data_t) * attachment_tbl_count);
2147 if(*output_attachment_data == NULL) {
2148 EM_DEBUG_EXCEPTION("em_malloc failed");
2149 err = EMF_ERROR_OUT_OF_MEMORY;
2153 for (i = 0; i < attachment_tbl_count; i++) {
2154 temp_attachment_data = (*output_attachment_data) + i;
2156 temp_attachment_data->attachment_id = attachment_tbl_list[i].attachment_id;
2157 temp_attachment_data->attachment_name = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
2158 temp_attachment_data->attachment_path = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
2159 temp_attachment_data->attachment_size = attachment_tbl_list[i].attachment_size;
2160 temp_attachment_data->mail_id = attachment_tbl_list[i].mail_id;
2161 temp_attachment_data->account_id = attachment_tbl_list[i].account_id;
2162 temp_attachment_data->mailbox_name = attachment_tbl_list[i].mailbox_name; attachment_tbl_list[i].mailbox_name = NULL;
2163 temp_attachment_data->save_status = attachment_tbl_list[i].attachment_save_status;
2164 temp_attachment_data->drm_status = attachment_tbl_list[i].attachment_drm_type;
2165 temp_attachment_data->inline_content_status = attachment_tbl_list[i].attachment_inline_content_status;
2171 *output_attachment_count = attachment_tbl_count;
2173 if (attachment_tbl_list)
2174 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2180 INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, char *nth, int *err_code)
2182 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%p], err_code[%p]", account_id, mail_id, nth, err_code);
2184 int attachment_no = 0;
2185 int err = EMF_ERROR_NONE;
2187 if (mail_id < 1 || !nth) {
2188 EM_DEBUG_EXCEPTION("mail_id[%d], nth[%p]", mail_id, nth);
2189 err = EMF_ERROR_INVALID_PARAM;
2191 if (err_code != NULL)
2195 attachment_no = atoi(nth);
2197 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err);
2202 int status = EMF_DOWNLOAD_FAIL;
2203 MAILSTREAM *stream = NULL;
2205 emstorage_mail_tbl_t *mail = NULL;
2206 emstorage_attachment_tbl_t *attachment = NULL;
2207 struct attachment_info *ai = NULL;
2208 struct _m_content_info *cnt_info = NULL;
2209 void *tmp_stream = NULL;
2210 char *s_uid = NULL, *server_mbox = NULL, buf[1024];
2212 emstorage_attachment_tbl_t *attachment_list = NULL;
2213 int current_attachment_no = 0;
2214 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2217 if (!emcore_check_thread_status()) {
2218 err = EMF_ERROR_CANCELLED;
2222 only_body_download = false;
2224 /* get mail from mail table. */
2225 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2226 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2231 if (!mail->server_mail_status) {
2232 EM_DEBUG_EXCEPTION("not synchronous mail...");
2233 err = EMF_ERROR_INVALID_MAIL;
2237 attachment_no = atoi(nth);
2238 if (attachment_no == 0) { /* download all attachments, nth starts from 1, not zero */
2239 /* get attachment list from db */
2240 attachment_count_to_be_downloaded = EMF_ATTACHMENT_MAX_COUNT;
2241 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMF_ERROR_NONE ){
2242 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2247 else { /* download only nth attachment */
2248 attachment_count_to_be_downloaded = 1;
2249 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2250 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2256 if (!emcore_check_thread_status()) {
2257 err = EMF_ERROR_CANCELLED;
2261 account_id = mail->account_id;
2262 s_uid = EM_SAFE_STRDUP(mail->server_mail_id);
2263 server_mbox = EM_SAFE_STRDUP(mail->mailbox_name);
2265 /* open mail server. */
2266 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2267 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2268 status = EMF_DOWNLOAD_CONNECTION_FAIL;
2272 stream = (MAILSTREAM *)tmp_stream;
2274 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2275 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
2277 attachment = attachment_list + i;
2278 if (attachment_no == 0) /* download all attachments, nth starts from 1, not zero */
2279 current_attachment_no = i + 1; /* attachment no */
2280 else /* download only nth attachment */
2281 current_attachment_no = attachment_no; /* attachment no */
2283 if (!emcore_check_thread_status()) {
2284 err = EMF_ERROR_CANCELLED;
2288 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2289 EM_DEBUG_EXCEPTION("malloc failed...");
2290 err = EMF_ERROR_OUT_OF_MEMORY;
2294 cnt_info->grab_type = GRAB_TYPE_ATTACHMENT; /* attachment */
2295 cnt_info->file_no = current_attachment_no; /* attachment no */
2297 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2298 /* text/html be changed to attachment, this isn't real attachment in RFC822. */
2300 cnt_info->file_no--;
2303 /* set sparep(member of BODY) memory free function. */
2304 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2306 if (!emcore_check_thread_status()) {
2307 err = EMF_ERROR_CANCELLED;
2311 msg_no = atoi(s_uid);
2313 /* get body structure. */
2314 /* don't free mbody because mbody is freed in closing mail_stream. */
2315 if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0) {
2316 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2320 if (!emcore_check_thread_status()) {
2321 err = EMF_ERROR_CANCELLED;
2325 /* set body fetch section. */
2326 if (emcore_set_fetch_body_section(mbody, false, NULL, &err) < 0) {
2327 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2331 /* download attachment. */
2332 _imap4_received_body_size = 0;
2333 _imap4_last_notified_body_size = 0;
2334 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2335 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2337 EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
2338 if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0) {
2339 EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
2343 if (!emcore_check_thread_status()) {
2344 err = EMF_ERROR_CANCELLED;
2348 /* select target attachment information. */
2349 for (ai = cnt_info->file ; ai; ai = ai->next) {
2351 EM_DEBUG_LOG("[in loop] %s, %d", ai->name, cnt_info->file_no);
2353 if (--cnt_info->file_no == 0)
2357 EM_DEBUG_LOG("cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
2359 if (cnt_info->file_no == 0 && ai) {
2360 /* rename temporary file to real file. */
2361 if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err)) {
2362 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2366 if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err)) {
2367 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2371 if (!emstorage_move_file(ai->save, buf, false, &err)) {
2372 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2377 EM_SAFE_FREE(ai->save);
2379 EM_DEBUG_LOG("ai->size [%d]", ai->size);
2380 attachment->attachment_size = ai->size;
2381 attachment->attachment_path = EM_SAFE_STRDUP(buf);
2383 /* update attachment information. */
2384 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2385 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2386 /* delete created file. */
2392 #ifdef __FEATURE_DRM__
2393 if (emcore_check_drm(attachment)) { /* has drm attachment ? */
2394 if (drm_svc_register_file(attachment->attachment_path) == DRM_RESULT_SUCCESS)
2395 EM_DEBUG_LOG("drm_svc_register_file success");
2397 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2398 mail->DRM_status = attachment->attachment_drm_type;
2400 #endif /* __FEATURE_DRM__ */
2403 EM_DEBUG_EXCEPTION("invalid attachment sequence...");
2404 err = EMF_ERROR_INVALID_ATTACHMENT;
2409 emcore_free_content_info(cnt_info);
2412 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2416 emcore_close_mailbox(0, stream);
2424 emcore_close_mailbox(account_id, stream);
2425 if (attachment_list)
2426 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2428 emcore_free_content_info(cnt_info);
2430 emstorage_free_mail(&mail, 1, NULL);
2432 EM_SAFE_FREE(s_uid);
2433 EM_SAFE_FREE(server_mbox);
2436 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2438 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2440 if (err_code != NULL)
2443 EM_DEBUG_FUNC_END();
2447 #ifdef __ATTACHMENT_OPTI__
2448 INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, char *nth, int *err_code)
2450 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%p], err_code[%p]", account_id, mail_id, nth, err_code);
2452 int attachment_no = 0;
2453 int err = EMF_ERROR_NONE; /* Prevent Defect - 25093 */
2455 int status = EMF_DOWNLOAD_FAIL;
2456 MAILSTREAM *stream = NULL;
2457 emstorage_mail_tbl_t *mail = NULL;
2458 emstorage_attachment_tbl_t *attachment = NULL;
2459 void *tmp_stream = NULL;
2460 char *s_uid = NULL, *server_mbox = NULL, buf[512];
2461 emstorage_attachment_tbl_t *attachment_list = NULL;
2462 int current_attachment_no = 0;
2463 int attachment_count_to_be_downloaded = 0; /* how many attachments should be downloaded */
2465 char *savefile = NULL;
2468 #ifdef SUPPORT_EXTERNAL_MEMORY
2469 int iActualSize = 0;
2474 int bIsAdd_to_mmc = false;
2475 int is_on_mmc = false;
2476 emf_file_list *pFileListMMc = NULL;
2477 emf_file_list *pFileList = NULL;
2478 #endif /* SUPPORT_EXTERNAL_MEMORY */
2481 memset(buf, 0x00, 512);
2483 if (mail_id < 1 || !nth) {
2484 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
2486 err = EMF_ERROR_INVALID_PARAM;
2488 if (err_code != NULL)
2492 attachment_no = atoi(nth);
2494 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err); /* 090525, kwangryul.baek */
2499 if (!emcore_check_thread_status()) {
2500 err = EMF_ERROR_CANCELLED;
2503 only_body_download = false;
2505 attachment_no = atoi(nth);
2507 if (attachment_no == 0) {
2508 /* download all attachments, nth starts from 1, not zero */
2509 /* get attachment list from db */
2510 attachment_count_to_be_downloaded = EMF_ATTACHMENT_MAX_COUNT;
2511 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMF_ERROR_NONE || !attachment_list){
2512 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2517 else { /* download only nth attachment */
2518 attachment_count_to_be_downloaded = 1;
2519 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2520 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2528 if (!emcore_check_thread_status()) {
2529 err = EMF_ERROR_CANCELLED;
2534 /* get mail from mail table. */
2535 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2536 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2542 /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /* faizan.h@samsung.com */
2544 if (!emcore_check_thread_status()) {
2545 err = EMF_ERROR_CANCELLED;
2549 account_id = mail->account_id;
2550 s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
2551 server_mbox = EM_SAFE_STRDUP(mail->mailbox); mail->server_mailbox_name = NULL;
2555 /* open mail server. */
2556 if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2557 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2559 status = EMF_DOWNLOAD_CONNECTION_FAIL;
2563 stream = (MAILSTREAM *)tmp_stream;
2566 if (!emcore_check_thread_status()) {
2567 err = EMF_ERROR_CANCELLED;
2572 for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2573 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
2575 attachment = attachment_list + i;
2576 if (attachment_no == 0) {
2577 /* download all attachments, nth starts from 1, not zero */
2578 current_attachment_no = i + 1; /* attachment no */
2581 /* download only nth attachment */
2582 current_attachment_no = attachment_no; /* attachment no */
2585 if (!emcore_check_thread_status()) {
2586 err = EMF_ERROR_CANCELLED;
2590 _imap4_received_body_size = 0;
2591 _imap4_last_notified_body_size = 0;
2592 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2593 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2596 EM_SAFE_FREE(savefile);
2597 if (!emcore_get_temp_file_name(&savefile, &err)) {
2598 EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
2600 if (err_code != NULL)
2608 EM_DEBUG_LOG("uid [%d]", uid);
2610 if (!imap_mail_write_body_to_file(stream, account_id, mail_id, attachment_no, savefile, uid , attachment->section, attachment->encoding, &dec_len, NULL, &err)) {
2611 EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
2612 if (err_code != NULL)
2617 #ifdef SUPPORT_EXTERNAL_MEMORY
2618 iActualSize = emcore_get_actual_mail_size (cnt_info->text.plain , cnt_info->text.html, cnt_info->file , &err);
2619 if (!emstorage_mail_check_free_space(iActualSize, &bIs_full, &err)) {
2620 EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
2625 /* If external memory not present, return error */
2626 if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
2627 err = EMF_ERROR_MAIL_MEMORY_FULL;
2630 bIsAdd_to_mmc = true;
2632 #endif /* SUPPORT_EXTERNAL_MEMORY */
2634 if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
2635 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2639 if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
2640 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2644 if (!emstorage_move_file(savefile, buf, false, &err)) {
2645 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2650 attachment->attachment = EM_SAFE_STRDUP(buf);
2651 /* update attachment information. */
2652 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2653 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2658 #ifdef __FEATURE_DRM__
2659 if (emcore_check_drm(attachment)) {
2661 if (drm_svc_register_file(attachment->attachment) == DRM_RESULT_SUCCESS)
2662 EM_DEBUG_LOG("drm_svc_register_file success");
2664 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2665 mail->flag3 = attachment->flag2;
2667 #endif /* __FEATURE_DRM__ */
2669 if (!emcore_check_thread_status()) {
2670 err = EMF_ERROR_CANCELLED;
2674 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2681 EM_SAFE_FREE(savefile);
2683 emcore_close_mailbox(0, stream);
2686 if (attachment_list)
2687 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2690 emstorage_free_mail(&mail, 1, NULL);
2696 free(server_mbox);server_mbox = NULL;
2699 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2700 else if (err != EMF_ERROR_CANCELLED)
2701 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2703 if (err_code != NULL)
2711 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)
2713 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);
2716 int err = EMF_ERROR_NONE;
2717 int status = EMF_DOWNLOAD_FAIL;
2718 int pop3_body_size = 0;
2719 int pop3_downloaded_size = 0;
2720 MAILSTREAM *stream = NULL;
2722 PARTLIST *section_list = NULL;
2723 emf_mailbox_t mbox = { 0 };
2724 emstorage_mail_tbl_t *mail = NULL;
2725 emstorage_attachment_tbl_t attachment = {0, 0, NULL, };
2726 emf_account_t *ref_account = NULL;
2727 struct attachment_info *ai = NULL;
2728 struct _m_content_info *cnt_info = NULL;
2729 void *tmp_stream = NULL;
2730 char *s_uid = NULL, *server_mbox = NULL, buf[512];
2731 int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
2732 int iActualSize = 0;
2733 char html_body[MAX_PATH] = {0, };
2734 emcore_uid_list *uid_list = NULL;
2735 char *mailbox_name = NULL;
2736 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2737 int html_changed = 0;
2741 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);
2742 err = EMF_ERROR_INVALID_PARAM;
2744 if (err_code != NULL)
2747 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
2751 FINISH_OFF_IF_CANCELED;
2753 only_body_download = true;
2755 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2756 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2761 if (mail->mailbox_name)
2762 mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
2764 if (1 == mail->body_download_status) {
2765 EM_DEBUG_EXCEPTION("not synchronous mail...");
2766 err = EMF_ERROR_INVALID_MAIL;
2770 account_id = mail->account_id;
2771 s_uid = mail->server_mail_id;
2772 server_mbox = mail->server_mailbox_name;
2773 mail->server_mail_id = NULL;
2774 mail->server_mailbox_name = NULL;
2776 attachment.account_id = mail->account_id;
2777 attachment.mail_id = mail->mail_id;
2778 attachment.mailbox_name = mail->mailbox_name; mail->mailbox_name = NULL;
2779 attachment.attachment_save_status = 0;
2781 emstorage_free_mail(&mail, 1, NULL);
2784 if (!(ref_account = emcore_get_account_reference(account_id))) {
2785 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2786 err = EMF_ERROR_INVALID_ACCOUNT;
2790 FINISH_OFF_IF_CANCELED;
2792 /* open mail server. */
2794 if (!emcore_connect_to_remote_mailbox(account_id, mailbox_name, (void **)&tmp_stream, &err) || !tmp_stream) {
2795 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2796 status = EMF_DOWNLOAD_CONNECTION_FAIL;
2799 stream = (MAILSTREAM *)tmp_stream;
2802 stream = (MAILSTREAM *)mail_stream;
2804 free(server_mbox); server_mbox = NULL;
2807 FINISH_OFF_IF_CANCELED;
2809 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2810 EM_DEBUG_EXCEPTION("em_malloc failed...");
2811 err = EMF_ERROR_OUT_OF_MEMORY;
2815 if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3) { /* POP3 */
2816 /* in POP3 case, both text and attachment are downloaded in this call. */
2817 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2818 attachment.attachment_save_status = 1; /* all attachments should be downloaded in the case of POP3 */
2820 mbox.account_id = account_id;
2821 mbox.mail_stream = stream;
2823 /* download all uids from server. */
2824 if (!emcore_download_uid_all(&mbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err)) {
2825 EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
2829 /* get mesg number to be related to last download mail from uid list file */
2830 if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err)) {
2831 EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
2832 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2836 free(s_uid); s_uid = NULL;
2838 if (!emcore_check_thread_status()) {
2839 err = EMF_ERROR_CANCELLED;
2843 _pop3_received_body_size = 0;
2844 _pop3_total_body_size = 0;
2845 _pop3_last_notified_body_size = 0;
2846 _pop3_receiving_mail_id = mail_id;
2848 /* send read mail commnad. */
2849 if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err))
2850 /* if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, PARTIAL_BODY_SIZE_IN_BYTES, &pop3_downloaded_size, &pop3_body_size, &err)) */ {
2851 EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
2855 _pop3_total_body_size = pop3_body_size;
2857 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
2858 EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
2860 EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
2862 FINISH_OFF_IF_CANCELED;
2864 /* save message into tempfile */
2865 /* parsing mime from stream. */
2867 if (!emcore_parse_mime(stream, 0, cnt_info, &err)) {
2868 EM_DEBUG_EXCEPTION("emcore_parse_mime failed [%d]", err);
2872 FINISH_OFF_IF_CANCELED;
2874 else { /* in IMAP case, both text and attachment list are downloaded in this call. */
2875 /* This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
2876 if (with_attach > 0)
2877 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2879 cnt_info->grab_type = GRAB_TYPE_TEXT;
2881 int uid = atoi(s_uid);
2883 free(s_uid); s_uid = NULL;
2885 /* set sparep(member of BODY) memory free function */
2886 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2888 /* get body strucutre. */
2889 /* don't free mbody because mbody is freed in closing mail_stream. */
2890 if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL)) {
2891 EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2892 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2896 FINISH_OFF_IF_CANCELED;
2898 if (mbody->type == TYPEMULTIPART) {
2899 EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
2900 PART *part_child = mbody->nested.part;
2903 char filename[MAX_PATH+1] = {0, };
2904 int is_attachment = 0;
2905 while (part_child) {
2906 BODY *body = &(part_child->body);
2907 if (only_body_download == true) {
2908 if (((body->id) && strlen(body->id) > 1) || (body->location))
2910 else if (body->disposition.type) { /* "attachment" or "inline" or etc... */
2911 PARAMETER *param = body->disposition.parameter;
2914 EM_DEBUG_LOG("param->attribute [%s], param->value [%s]", param->attribute, param->value);
2916 if (!strcasecmp(param->attribute, "filename")) { /* attribute is "filename" */
2917 strncpy(filename, param->value, MAX_PATH);
2918 EM_DEBUG_LOG(">>>>> FILENAME [%s] ", filename);
2921 param = param->next;
2926 if (!*filename) { /* it may be report msg */
2927 if (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')
2932 if (is_attachment == 0) {
2933 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
2934 multi_part_body_size = multi_part_body_size + body->size.bytes;
2937 else { /* download all */
2938 EM_DEBUG_LOG("%d : body->size.bytes[%ld]", counter+1, body->size.bytes);
2939 multi_part_body_size = multi_part_body_size + body->size.bytes;
2941 part_child = part_child->next;
2946 /* set body fetch section. */
2947 if (emcore_set_fetch_body_section(mbody, true, &iActualSize, &err) < 0) {
2948 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2952 EM_DEBUG_LOG("iActualSize [%d]", iActualSize);
2953 multi_part_body_size = iActualSize;
2955 _imap4_received_body_size = 0;
2956 _imap4_last_notified_body_size = 0;
2957 if (multi_part_body_size > 0) { /* download multiparts */
2958 _imap4_total_body_size = multi_part_body_size;
2959 _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
2961 else { /* download only one body part */
2962 _imap4_total_body_size = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2963 _imap4_download_noti_interval_value = 0; /* This will be assigned in imap_mail_write_body_to_file() */
2966 /* save message into tempfile */
2967 /* download body text and get attachment list. */
2968 if (emcore_get_body_part_list_full(stream, uid, account_id, mail_id, mbody, cnt_info, &err, section_list, event_handle) < 0) {
2969 EM_DEBUG_EXCEPTION("emcore_get_body falied [%d]", err);
2972 FINISH_OFF_IF_CANCELED;
2976 if (false == emstorage_get_mail_by_id(mail_id, &mail, true, &err)) {
2977 EM_DEBUG_EXCEPTION(" emstorage_get_mail_by_id failed [%d]", err);
2981 if (cnt_info->text.plain) {
2982 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
2984 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
2985 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2990 if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err)) {
2991 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2995 if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err)) {
2996 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3000 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3001 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3004 if (cnt_info->text.html) {
3005 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
3006 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3010 if (cnt_info->text.plain_charset != NULL) {
3011 memcpy(html_body, cnt_info->text.plain_charset, strlen(cnt_info->text.plain_charset));
3012 strcat(html_body, HTML_EXTENSION_STRING);
3015 memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3017 if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err)) {
3018 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3022 if (!emstorage_move_file(cnt_info->text.html, buf, false, &err)) {
3023 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3026 mail->file_path_html = EM_SAFE_STRDUP(buf);
3029 if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && limited_size < pop3_body_size)
3030 mail->body_download_status = EMF_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
3032 mail->body_download_status = EMF_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
3034 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
3035 if (html_changed) mail->flag2 = 1;
3038 FINISH_OFF_IF_CANCELED;
3040 for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++) {
3041 attachment.attachment_id = attachment_num;
3042 attachment.attachment_size = ai->size;
3043 attachment.attachment_path = ai->save;
3044 attachment.attachment_name = ai->name;
3045 attachment.attachment_drm_type = ai->drm;
3046 attachment.attachment_inline_content_status = ai->type == 1;
3047 attachment.attachment_save_status = 0;
3048 attachment.attachment_mime_type = ai->attachment_mime_type;
3049 #ifdef __ATTACHMENT_OPTI__
3050 attachment.encoding = ai->encoding;
3051 attachment.section = ai->section;
3053 EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
3054 EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
3055 EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
3056 EM_DEBUG_LOG("attachment.attachment_name[%s]", attachment.attachment_name);
3057 EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
3058 EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
3061 local_inline_content_count++;
3062 local_attachment_count++;
3065 /* in POP3 case, rename temporary file to real file. */
3066 attachment.attachment_save_status = 1;
3067 if (ai->type == 1) { /* it is inline content */
3068 if (!emstorage_create_dir(account_id, mail_id, 0, &err)) {
3069 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3072 if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err)) {
3073 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3078 if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
3079 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3083 if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err)) {
3084 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3089 if (!emstorage_move_file(ai->save, buf, false, &err)) {
3090 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3092 /* delete all created files. */
3093 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
3094 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
3095 /* goto FINISH_OFF; */
3098 if (!emstorage_delete_dir(buf, NULL)) {
3099 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
3100 /* goto FINISH_OFF; */
3107 free(ai->save); ai->save = EM_SAFE_STRDUP(buf);
3109 attachment.attachment_path = ai->save;
3111 #ifdef __FEATURE_DRM__
3112 if (emcore_check_drm(&attachment)) /* is drm content ?*/ {
3113 if (!drm_svc_register_file(attachment.attachment_path))
3114 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
3115 mail->DRM_status = attachment.attachment_drm_type;
3117 #endif/* __FEATURE_DRM__ */
3120 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
3121 /* Information : Attachment info already saved if partial body is dowloaded. */
3123 emstorage_attachment_tbl_t *attch_info = NULL;
3124 /* Get attachment details */
3125 if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info) {
3126 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
3127 if (err == EMF_ERROR_ATTACHMENT_NOT_FOUND) { /* save only attachment file. */
3128 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3129 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3131 emstorage_free_attachment(&attch_info, 1, NULL);
3132 /* delete all created files. */
3133 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3134 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3138 if (!emstorage_delete_dir(buf, &err)) {
3139 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3143 /* ROLLBACK TRANSACTION; */
3144 emstorage_rollback_transaction(NULL, NULL, NULL);
3152 EM_DEBUG_LOG("Attachment info already exists...!");
3153 /* Update attachment size */
3154 EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
3155 attch_info->attachment_size = ai->size;
3156 if (!emstorage_update_attachment(attch_info, true, &err)) {
3157 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3164 emstorage_free_attachment(&attch_info, 1, NULL);
3170 mail->attachment_yn = 1;
3171 /* save only attachment file. */
3172 if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3173 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3174 if (bIsAdd_to_mmc) {
3175 if (attachment.attachment) {
3179 /* delete all created files. */
3180 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err)) {
3181 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3185 if (!emstorage_delete_dir(buf, &err)) {
3186 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3190 /* ROLLBACK TRANSACTION; */
3191 emstorage_rollback_transaction(NULL, NULL, NULL);
3198 #endif /* End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
3202 EM_DEBUG_LOG("Check #1");
3204 mail->attachment_count = local_attachment_count;
3205 mail->inline_content_count = local_inline_content_count;
3207 EM_DEBUG_LOG("Check #2");
3209 EM_DEBUG_LOG("Mailbox Name [%s]", mailbox_name);
3210 mail->mailbox_name = EM_SAFE_STRDUP(mailbox_name); /* fix for mailboox sync fail */
3212 EM_DEBUG_LOG("Check #3");
3214 /* change mail's information. */
3215 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err)) {
3216 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3217 emstorage_rollback_transaction(NULL, NULL, NULL); /* ROLLBACK TRANSACTION; */
3222 EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
3224 /* in pop3 mail case, the mail is deleted from server after being downloaded. */
3225 if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3226 #ifdef DELETE_AFTER_DOWNLOADING
3229 SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
3231 if (!ref_account->keep_on_server) {
3232 if (!emcore_delete_mail_from_server(&mbox, delmsg, &err))
3233 EM_DEBUG_EXCEPTION("emcore_delete_mail_from_server failed [%d]", err);
3238 if (stream != NULL) {
3239 emcore_close_mailbox(0, stream);
3245 FINISH_OFF_IF_CANCELED;
3251 if (g_inline_count) {
3253 EM_SAFE_FREE(g_inline_list);
3256 multi_part_body_size = 0;
3257 _pop3_received_body_size = 0;
3258 _pop3_last_notified_body_size = 0;
3259 _pop3_total_body_size = 0;
3260 _pop3_receiving_mail_id = 0;
3262 _imap4_received_body_size = 0;
3263 _imap4_last_notified_body_size = 0;
3264 _imap4_total_body_size = 0;
3265 _imap4_download_noti_interval_value = 0;
3268 emcore_free_content_info(cnt_info);
3270 emstorage_free_mail(&mail, 1, NULL);
3271 if (attachment.mailbox_name)
3272 free(attachment.mailbox_name);
3278 multi_part_body_size = 0;
3281 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
3283 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
3286 free (mailbox_name);
3288 if (err_code != NULL)
3296 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
3297 unsigned long uidvalidity, SEARCHSET *sourceset,
3300 EM_DEBUG_FUNC_BEGIN();
3301 char old_server_uid[129];
3303 EM_DEBUG_LOG("mailbox name - %s", mailbox);
3304 EM_DEBUG_LOG("first sequence number source- %ld", sourceset->first);
3305 EM_DEBUG_LOG("last sequence number last- %ld", sourceset->last);
3306 EM_DEBUG_LOG("first sequence number dest - %ld", destset->first);
3307 EM_DEBUG_LOG("last sequence number dest- %ld", sourceset->last);
3309 /* search for server _mail_id with value sourceset->first and update it with destset->first */
3310 /* faizan.h@samsung.com */
3311 memset(old_server_uid, 0x00, 129);
3312 sprintf(old_server_uid, "%ld", sourceset->first);
3313 EM_DEBUG_LOG(">>>>> old_server_uid = %s", old_server_uid);
3315 memset(g_new_server_uid, 0x00, 129);
3316 sprintf(g_new_server_uid, "%ld", destset->first);
3317 EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
3319 if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
3320 EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
3324 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)
3326 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);
3329 int err = EMF_ERROR_NONE;
3330 int status = EMF_DELETE_FAIL;
3332 emf_account_t *account = NULL;
3333 emf_mailbox_t mailbox;
3334 emstorage_mail_tbl_t *mail = NULL;
3335 void *stream = NULL;
3338 int msgno = 0, parameter_string_length = 0;
3339 char *parameter_string = NULL, mail_id_string[10];
3340 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3341 int bulk_flag = false;
3344 memset(&mailbox, 0x00, sizeof(emf_mailbox_t));
3346 if (!account_id || !mail_ids || !num) {
3347 EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
3348 err = EMF_ERROR_INVALID_PARAM;
3352 mail_id = mail_ids[0];
3354 if (!(account = emcore_get_account_reference(account_id))) {
3355 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
3356 err = EMF_ERROR_INVALID_ACCOUNT;
3360 if (account->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC)
3361 from_server = EMF_DELETE_LOCALLY;
3363 FINISH_OFF_IF_CANCELED;
3365 parameter_string_length = sizeof(char) * (num * 8 + 128 /* MAILBOX_LEN_IN_MAIL_TBL */ * 2);
3366 parameter_string = malloc(parameter_string_length);
3368 if (parameter_string == NULL) {
3369 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
3370 err = EMF_ERROR_OUT_OF_MEMORY;
3374 memset(parameter_string, 0, parameter_string_length);
3376 if (from_server == EMF_DELETE_LOCAL_AND_SERVER || from_server == EMF_DELETE_FOR_SEND_THREAD) { /* server delete */
3377 for (i = 0; i < num; i++) {
3378 mail_id = mail_ids[i];
3380 if (!emstorage_get_downloaded_mail(mail_id, &mail, false, &err) || !mail) {
3381 EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id failed [%d]", err);
3383 if (err == EMF_ERROR_MAIL_NOT_FOUND) { /* not server mail */
3384 /* err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
3393 EM_SAFE_FREE(mailbox.user_data);
3395 if (stream == NULL) {
3396 if (!emcore_connect_to_remote_mailbox(account_id, mail->server_mailbox_name , (void **)&stream, &err)) {
3397 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
3398 status = EMF_DELETE_CONNECTION_FAIL;
3402 mailbox.account_id = account_id;
3403 mailbox.name = mail->server_mailbox_name;
3404 mailbox.mail_stream = stream;
3406 FINISH_OFF_IF_CANCELED;
3409 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3410 if (account->receiving_server_type == EMF_SERVER_TYPE_IMAP4) {
3411 #ifdef __FEATURE_LOCAL_ACTIVITY__
3412 if (!bulk_flag && !imap4_mail_delete_ex(&mailbox, mail_ids, num, from_server, &err)) {
3413 #else /* __FEATURE_LOCAL_ACTIVITY__ */
3414 if (!bulk_flag && !imap4_mail_delete_ex(&mailbox, mail_ids, num, &err)) {
3415 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3416 EM_DEBUG_EXCEPTION("imap4_mail_delete_ex failed [%d]", err);
3417 if (err == EMF_ERROR_IMAP4_STORE_FAILURE)
3418 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3425 else if (account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3426 if (!emcore_get_mail_msgno_by_uid(account, &mailbox, mail->server_mail_id, &msgno, &err)) {
3427 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid faild [%d]", err);
3428 if (err == EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER)
3429 goto NOT_FOUND_ON_SERVER;
3434 FINISH_OFF_IF_CANCELED;
3436 if (!emcore_delete_mail_from_server(account, &mailbox, msgno, &err)) {
3437 EM_DEBUG_EXCEPTION("emcore_delete_mail_from_server falied [%d]", err);
3440 #ifdef __FEATURE_LOCAL_ACTIVITY__
3442 /* Remove local activity */
3443 emstorage_activity_tbl_t new_activity;
3444 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
3445 if (from_server == EMF_DELETE_FOR_SEND_THREAD) {
3446 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
3447 EM_DEBUG_LOG("from_server == EMF_DELETE_FOR_SEND_THREAD ");
3450 new_activity.activity_type = ACTIVITY_DELETEMAIL;
3453 new_activity.mail_id = mail->mail_id;
3454 new_activity.server_mailid = mail->server_mail_id;
3455 new_activity.src_mbox = NULL;
3456 new_activity.dest_mbox = NULL;
3458 if (!emcore_delete_activity(&new_activity, &err)) {
3459 EM_DEBUG_EXCEPTION(" emcore_delete_activity failed - %d ", err);
3462 /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
3463 if (!emstorage_remove_downloaded_mail(account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err)) {
3464 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3468 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3470 #else /* __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__ */
3471 if (!emcore_get_mail_msgno_by_uid(account, &mailbox, mail->server_mail_id, &msgno, &err)) {
3472 EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid faild [%d]", err);
3474 if (err == EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER) /* Prevent Defect - 9561 */
3475 goto NOT_FOUND_ON_SERVER;
3480 FINISH_OFF_IF_CANCELED;
3482 if (!emcore_delete_mail_from_server(account, &mailbox, msgno, &err)) {
3483 EM_DEBUG_LOG("emcore_delete_mail_from_server falied [%d]", err);
3487 #endif /* __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__ */
3489 if (!emstorage_remove_downloaded_mail(account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))
3490 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3492 /* emcore_delete_notification_for_read_mail(mail_id); */
3494 NOT_FOUND_ON_SERVER:
3495 memset(mail_id_string, 0, 10);
3496 SNPRINTF(mail_id_string, 10, "%d,", mail_id);
3497 strcat(parameter_string, mail_id_string);
3499 FINISH_OFF_IF_CANCELED;
3501 emstorage_free_mail(&mail, 1, NULL);
3504 else if (from_server == EMF_DELETE_LOCALLY) /* Local Delete */ {
3505 emcore_delete_mail_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
3506 for (i = 0; i < num; i++) {
3507 /* emcore_delete_notification_for_read_mail(mail_id); */
3508 SNPRINTF(mail_id_string, 10, "%d,", mail_id);
3509 strcat(parameter_string, mail_id_string);
3512 emcore_check_unread_mail();
3520 emcore_close_mailbox(0, stream);
3524 if (mailbox.user_data != NULL) {
3525 emcore_free_uids(mailbox.user_data, NULL);
3526 mailbox.user_data = NULL;
3529 EM_SAFE_FREE(parameter_string);
3532 emstorage_free_mail(&mail, 1, NULL);
3536 emcore_show_popup(account_id, EMF_ACTION_DELETE_MAIL, ret == true ? 0 : err);
3538 if (err_code != NULL)
3545 int emcore_delete_mail_all(emf_mailbox_t *mailbox, int with_server, int *err_code)
3547 EM_DEBUG_FUNC_BEGIN("mailbox [%p], with_server [%d], err_code [%p]", mailbox, with_server, err_code);
3550 int err = EMF_ERROR_NONE;
3551 int search_handle = 0;
3552 int *mail_ids = NULL;
3555 char buf[512] = { 0, };
3557 if (!mailbox || mailbox->account_id < FIRST_ACCOUNT_ID) {
3558 err = EMF_ERROR_INVALID_PARAM;
3562 if (mailbox->name != NULL) {
3563 /* Delete all mails in specific mailbox */
3564 if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->name, 0, &search_handle, &total, true, &err)) {
3565 EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
3570 EM_DEBUG_LOG("emstorage_mail_search_start returns [%d]", total);
3573 mail_ids = em_malloc(sizeof(int) * total);
3574 if (mail_ids == NULL) {
3575 EM_DEBUG_EXCEPTION("em_malloc failed...");
3576 err = EMF_ERROR_OUT_OF_MEMORY;
3580 for (i = 0; i < total; i++) {
3581 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
3582 EM_DEBUG_EXCEPTION("emstorage_mail_search_result failed [%d]", err);
3588 if (!emcore_delete_mail(mailbox->account_id, mail_ids, total, with_server, EMF_DELETED_BY_COMMAND, false, &err)) {
3589 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
3594 else if (with_server == EMF_DELETE_LOCALLY){
3595 /* emstorage_delete_mail_by_account is available only locally */
3596 if (!emstorage_delete_mail_by_account(mailbox->account_id, false, &err)) {
3597 EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
3602 if (!emstorage_delete_attachment_all_on_db(mailbox->account_id, NULL, false, &err)) {
3603 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3608 /* delete mail contents from filesystem */
3609 if (!emstorage_get_save_name(mailbox->account_id, 0, 0, NULL, buf, &err)) {
3610 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3615 if (!emstorage_delete_dir(buf, &err)) {
3616 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3620 /* delete meeting request */
3621 if (!emstorage_delete_meeting_request(mailbox->account_id, 0, NULL, false, &err)) {
3622 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3627 emcore_check_unread_mail();
3633 if (search_handle >= 0) {
3634 if (!emstorage_mail_search_end(search_handle, true, &err))
3635 EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
3638 EM_SAFE_FREE(mail_ids);
3639 if (err_code != NULL)
3641 EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
3645 INTERNAL_FUNC int emcore_delete_mail_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
3647 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);
3648 int ret = false, err = EMF_ERROR_NONE, i;
3649 emstorage_mail_tbl_t *result_mail_list;
3650 char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
3652 /* Getting mail list by using select mail_id [in] */
3654 if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
3655 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
3659 /* Deleting mails by using select mail_id [in] */
3660 if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
3661 EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
3665 /* Sending Notification */
3666 noti_param_string = em_malloc(sizeof(char) * 10 * num);
3668 if(!noti_param_string) {
3669 EM_DEBUG_EXCEPTION("em_malloc failed");
3670 err = EMF_ERROR_OUT_OF_MEMORY;
3674 for(i = 0; i < num; i++) {
3675 memset(mail_id_string, 0, 10);
3676 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
3677 strcat(noti_param_string, mail_id_string);
3678 /* can be optimized by appending sub string with directly pointing on string array kyuho.jo 2011-10-07 */
3681 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE, account_id, noti_param_1, noti_param_string, noti_param_2))
3682 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3684 /* Updating Thread informations */
3685 /* Thread information should be updated as soon as possible. */
3686 for(i = 0; i < num; i++) {
3687 if (result_mail_list[i].thread_item_count > 1) {
3688 if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false, &err)) {
3689 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
3695 if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
3696 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3698 for(i = 0; i < num; i++) {
3699 /* Deleting attachments */
3700 if (!emstorage_delete_attachment_on_db(result_mail_list[i].mail_id, 0, false, &err)) {
3701 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", err);
3702 if (err == EMF_ERROR_ATTACHMENT_NOT_FOUND)
3703 err = EMF_ERROR_NONE;
3706 /* Deleting Directories */
3707 /* delete mail contents from filesystem */
3708 if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
3709 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3714 if (!emstorage_delete_dir(buf, &err)) {
3715 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3719 /* Deleting Meeting Request */
3720 if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, NULL, false, &err)) {
3721 EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
3722 if (err != EMF_ERROR_CONTACT_NOT_FOUND) {
3731 EM_SAFE_FREE(noti_param_string);
3733 if (err_code != NULL)
3736 EM_DEBUG_FUNC_END();
3740 int emcore_delete_mail_from_server(emf_account_t *account, emf_mailbox_t *mailbox, int msgno, int *err_code)
3742 EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], msgno[%d], err_code[%p]", account, mailbox, msgno, err_code);
3745 int err = EMF_ERROR_NONE;
3747 if (!account || !mailbox) {
3748 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], msgno[%d]", account, mailbox, msgno);
3750 err = EMF_ERROR_INVALID_PARAM;
3754 if (account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3755 if (!pop3_mail_delete(mailbox->mail_stream, msgno, &err)) {
3756 EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
3758 if (err == EMF_ERROR_POP3_DELE_FAILURE)
3759 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3763 else { /* EMF_SERVER_TYPE_IMAP4 */
3764 if (!imap4_mail_delete(mailbox->mail_stream, msgno, &err)) {
3765 EM_DEBUG_EXCEPTION("imap4_mail_delete failed [%d]", err);
3767 if (err == EMF_ERROR_IMAP4_STORE_FAILURE)
3768 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3778 EM_DEBUG_FUNC_END();
3782 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(emf_account_t *account, emf_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
3784 EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], uid[%s], msgno[%p], err_code[%p]", account, mailbox, uid, msgno, err_code);
3787 int err = EMF_ERROR_NONE;
3789 emcore_uid_list *uid_list = NULL;
3790 emcore_uid_list *pTemp_uid_list = NULL;
3792 if (!account || !mailbox || !uid || !msgno) {
3793 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
3794 err = EMF_ERROR_INVALID_PARAM;
3798 uid_list = mailbox->user_data;
3800 if (uid_list == NULL) {
3801 if (account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3802 if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3803 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed [%d]", err);
3807 else { /* EMF_SERVER_TYPE_IMAP4 */
3808 if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err)) {
3809 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
3813 mailbox->user_data = uid_list;
3815 pTemp_uid_list = uid_list;
3817 if (!strcmp(uid_list->uid, uid)) {
3818 *msgno = uid_list->msgno;
3819 EM_DEBUG_LOG("uid_list->msgno[%d]", uid_list->msgno);
3823 EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
3824 uid_list = uid_list->next;
3827 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3830 if (err_code != NULL)
3832 uid_list = pTemp_uid_list ;
3833 if (uid_list != NULL)
3834 emcore_free_uids(uid_list, NULL);
3835 /* mailbox->user_data and uid_list both point to same memory address, So when uid_list is freed then just set */
3836 /* mailbox->user_data to NULL and dont use EM_SAFE_FREE, it will crash : ) */
3838 mailbox->user_data = NULL;
3839 EM_DEBUG_FUNC_END();
3844 * add a attachment to mail.
3846 * mailbox : mail box
3848 * attachment : attachment to be added
3853 INTERNAL_FUNC int emcore_mail_add_attachment(int mail_id, emf_attachment_info_t *attachment, int *err_code)
3855 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
3857 if (attachment == NULL) {
3858 EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
3860 *err_code = EMF_ERROR_INVALID_PARAM;
3864 int ret = false, err = EMF_ERROR_NONE;
3865 emstorage_mail_tbl_t *mail_table_data = NULL;
3866 int attachment_id = 0;
3870 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
3871 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3876 int account_id = mail_table_data->account_id;
3877 emstorage_attachment_tbl_t attachment_tbl;
3879 memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3881 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
3882 attachment_tbl.account_id = mail_table_data->account_id;
3883 attachment_tbl.mailbox_name = mail_table_data->mailbox_name;
3884 attachment_tbl.mail_id = mail_id;
3885 attachment_tbl.attachment_name = attachment->name;
3886 attachment_tbl.attachment_size = attachment->size;
3887 attachment_tbl.attachment_save_status = attachment->downloaded;
3888 attachment_tbl.attachment_drm_type = attachment->drm;
3889 attachment_tbl.attachment_inline_content_status = attachment->inline_content;
3891 /* BEGIN TRANSACTION; */
3892 emstorage_begin_transaction(NULL, NULL, NULL);
3894 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
3895 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3900 attachment->attachment_id = attachment_tbl.attachment_id;
3902 if (attachment->savename) {
3905 if (!attachment->inline_content) {
3906 if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err)) {
3907 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3910 attachment_id = attachment_tbl.attachment_id;
3913 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->name, buf, &err)) {
3914 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3917 attachment_tbl.attachment_path = buf;
3919 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
3920 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3925 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
3926 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3931 if (attachment->downloaded) {
3932 if (!emstorage_move_file(attachment->savename, buf, false, &err)) {
3933 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3939 /* Here only filename is being updated. Since first add is being done there will not be any old files.
3940 So no need to check for old files in this update case */
3941 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
3942 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3947 EM_SAFE_FREE(attachment->savename);
3948 attachment->savename = EM_SAFE_STRDUP(buf);
3954 if (ret == true) { /* COMMIT TRANSACTION; */
3955 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
3956 err = EMF_ERROR_DB_FAILURE;
3960 else { /* ROLLBACK TRANSACTION; */
3961 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
3962 err = EMF_ERROR_DB_FAILURE;
3966 if (mail_table_data != NULL)
3967 emstorage_free_mail(&mail_table_data, 1, NULL);
3971 EM_DEBUG_FUNC_END("err [%d]", err);
3976 INTERNAL_FUNC int emcore_mail_add_attachment_data(int input_mail_id, emf_attachment_data_t *input_attachment_data)
3978 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
3980 int err = EMF_ERROR_NONE;
3981 int attachment_id = 0;
3982 char buf[512] = { 0, };
3983 emstorage_mail_tbl_t *mail_table_data = NULL;
3984 emstorage_attachment_tbl_t attachment_tbl = { 0 };
3986 if (input_attachment_data == NULL) {
3987 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
3988 return EMF_ERROR_INVALID_PARAM;
3991 if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
3992 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3997 mail_table_data->attachment_count = mail_table_data->attachment_count + 1;
3999 attachment_tbl.account_id = mail_table_data->account_id;
4000 attachment_tbl.mailbox_name = mail_table_data->mailbox_name;
4001 attachment_tbl.mail_id = input_mail_id;
4002 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
4003 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4004 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4005 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4006 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4008 /* BEGIN TRANSACTION; */
4009 emstorage_begin_transaction(NULL, NULL, NULL);
4011 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
4012 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4017 input_attachment_data->attachment_id = attachment_tbl.attachment_id;
4019 if (input_attachment_data->attachment_path) {
4020 if (!input_attachment_data->inline_content_status) {
4021 if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4022 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4025 attachment_id = attachment_tbl.attachment_id;
4028 if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4029 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4032 attachment_tbl.attachment_path = buf;
4034 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4035 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4040 if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4041 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4046 if (input_attachment_data->save_status) {
4047 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err)) {
4048 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4054 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4055 So no need to check for old files in this update case */
4056 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err)) {
4057 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4062 EM_SAFE_FREE(input_attachment_data->attachment_path);
4063 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4067 if (err == EMF_ERROR_NONE) { /* COMMIT TRANSACTION; */
4068 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4069 err = EMF_ERROR_DB_FAILURE;
4072 else { /* ROLLBACK TRANSACTION; */
4073 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4074 err = EMF_ERROR_DB_FAILURE;
4078 if (mail_table_data != NULL)
4079 emstorage_free_mail(&mail_table_data, 1, NULL);
4081 EM_DEBUG_FUNC_END("err [%d]", err);
4087 * delete a attachment from mail.
4089 * mailbox : mail box
4091 * attachment_id : number string of attachment-id to be deleted
4092 * (ex : if attachment id is 2, number stirng will be "2")
4097 int emcore_delete_mail_attachment(int mail_id, char *attachment_id_string, int *err_code)
4099 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment_id_string[%s], err_code[%p]", mail_id, attachment_id_string, err_code);
4101 if (attachment_id_string == NULL) {
4102 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%p]", mail_id, attachment_id_string);
4103 if (err_code != NULL)
4104 *err_code = EMF_ERROR_INVALID_PARAM;
4109 int error = EMF_ERROR_NONE;
4110 int attachment_id = atoi(attachment_id_string);
4112 /* BEGIN TRANSACTION; */
4113 emstorage_begin_transaction(NULL, NULL, NULL);
4115 if (!emstorage_delete_attachment_on_db(mail_id, attachment_id, false, &error)) {
4116 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
4123 if (ret == true) { /* COMMIT TRANSACTION; */
4124 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4125 error = EMF_ERROR_DB_FAILURE;
4129 else { /* ROLLBACK TRANSACTION; */
4130 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4131 error = EMF_ERROR_DB_FAILURE;
4134 if (err_code != NULL)
4136 EM_DEBUG_FUNC_END("ret [%d]", ret);
4140 static int emcore_mail_update_attachment_data(int input_mail_id, emf_attachment_data_t *input_attachment_data)
4142 EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4144 int err = EMF_ERROR_NONE;
4145 int attachment_id = 0;
4146 char buf[512] = { 0 , };
4147 emstorage_attachment_tbl_t *existing_attachment_info = NULL;
4148 emstorage_attachment_tbl_t attachment_tbl = { 0 };
4150 if (input_attachment_data == NULL) {
4151 EM_DEBUG_EXCEPTION("Invalid Parameter");
4152 err = EMF_ERROR_INVALID_PARAM;
4156 if (!emstorage_get_attachment(input_mail_id, input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
4157 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4162 attachment_tbl.mail_id = input_mail_id;
4163 attachment_tbl.account_id = existing_attachment_info->account_id;
4164 attachment_tbl.mailbox_name = existing_attachment_info->mailbox_name;
4165 attachment_tbl.attachment_name = input_attachment_data->attachment_name;
4166 attachment_tbl.attachment_size = input_attachment_data->attachment_size;
4167 attachment_tbl.attachment_path = input_attachment_data->attachment_path;
4168 attachment_tbl.attachment_save_status = input_attachment_data->save_status;
4169 attachment_tbl.attachment_drm_type = input_attachment_data->drm_status;
4170 attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4171 attachment_tbl.attachment_id = input_attachment_data->attachment_id;
4173 if (!input_attachment_data->inline_content_status) {
4174 if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err)) {
4175 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4178 attachment_id = attachment_tbl.attachment_id;
4181 if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err)) {
4182 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4185 attachment_tbl.attachment_path = buf;
4187 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);
4188 if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
4189 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err)) {
4190 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4196 EM_DEBUG_LOG("no need to move");
4198 EM_SAFE_FREE(input_attachment_data->attachment_path);
4199 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4201 emstorage_begin_transaction(NULL, NULL, NULL);
4203 if (!emstorage_update_attachment(&attachment_tbl, false, &err)) {
4204 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4210 if (err == EMF_ERROR_NONE) { /* COMMIT TRANSACTION; */
4211 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4212 err = EMF_ERROR_DB_FAILURE;
4215 else { /* ROLLBACK TRANSACTION; */
4216 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4217 err = EMF_ERROR_DB_FAILURE;
4221 if (existing_attachment_info)
4222 emstorage_free_attachment(&existing_attachment_info, 1, NULL);
4224 EM_DEBUG_FUNC_END("err [%d]", err);
4229 static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, int input_attachment_a_id, emf_attachment_data_t *input_attachment_b_data, int *result)
4231 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);
4233 EM_IF_NULL_RETURN_VALUE(input_attachment_b_data, false);
4234 EM_IF_NULL_RETURN_VALUE(result, false);
4236 int err, err_2, ret = EMF_ERROR_NONE;
4237 emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
4239 if (!emstorage_get_attachment(input_mail_id, input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
4240 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4245 if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
4246 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);
4247 *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4254 if (attachment_a_tbl)
4255 emstorage_free_attachment(&attachment_a_tbl, 1, &err_2);
4256 EM_DEBUG_FUNC_END("*result [%d]", *result);
4262 * copy a mail to mail box
4264 * src_mailbox : source mail box
4265 * msgno : mail sequence
4266 * dst_mailbox : target mail box
4271 INTERNAL_FUNC int emcore_mail_copy(int mail_id, emf_mailbox_t *dst_mailbox, int *err_code)
4273 EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
4276 int err = EMF_ERROR_NONE;
4278 emstorage_mail_tbl_t *mail = NULL;
4279 emstorage_attachment_tbl_t *atch_list = NULL;
4281 int count = EMF_ATTACHMENT_MAX_COUNT;
4282 char *mailbox_name = NULL;
4284 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
4285 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4290 if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMF_ERROR_NONE ){
4291 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4296 /* get increased uid. */
4297 if (!emstorage_increase_mail_id(&mail->mail_id, true, &err)) {
4298 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
4303 /* copy mail body(text) file */
4304 if (mail->file_path_plain) {
4305 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4306 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4311 gchar *filename = g_path_get_basename(mail->file_path_plain);
4313 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4314 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4322 if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err)) {
4323 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4328 mail->file_path_plain = EM_SAFE_STRDUP(buf);
4331 /* copy mail body(html) file */
4332 if (mail->file_path_html) {
4333 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err)) {
4334 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4339 gchar *filename = g_path_get_basename(mail->file_path_html);
4341 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err)) {
4342 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4350 if (!emstorage_copy_file(mail->file_path_html, buf, false, &err)) {
4351 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4356 mail->file_path_html = EM_SAFE_STRDUP(buf);
4359 /* BEGIN TRANSACTION; */
4360 emstorage_begin_transaction(NULL, NULL, NULL);
4362 /* insert mail data */
4363 EM_SAFE_FREE(mail->mailbox_name);
4365 mail->account_id = dst_mailbox->account_id;
4366 mail->mailbox_id = dst_mailbox->mailbox_id;
4367 mail->mailbox_name = EM_SAFE_STRDUP(dst_mailbox->name);
4368 mail->mailbox_type = dst_mailbox->mailbox_type;
4370 if (!emstorage_add_mail(mail, 0, false, &err)) {
4371 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
4373 if (mail->file_path_plain) {
4374 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4375 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4376 emstorage_rollback_transaction(NULL, NULL, NULL);
4380 if (mail->file_path_html) {
4381 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4382 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4383 emstorage_rollback_transaction(NULL, NULL, NULL);
4388 /* ROLLBACK TRANSACTION; */
4389 emstorage_rollback_transaction(NULL, NULL, NULL);
4395 /* copy attachment file */
4396 for (i = 0; i<count; i++) {
4397 if (atch_list[i].attachment_path) {
4398 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err)) {
4399 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4403 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err)) {
4404 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4408 if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err)) {
4409 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4413 EM_SAFE_FREE(atch_list[i].attachment_path);
4415 atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
4418 atch_list[i].account_id = dst_mailbox->account_id;
4419 atch_list[i].mail_id = mail->mail_id;
4421 EM_SAFE_FREE(atch_list[i].mailbox_name);
4423 atch_list[i].mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
4425 if (!emstorage_add_attachment(&atch_list[i], 0, false, &err)) {
4426 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4432 /* in case error happened, delete copied file. */
4433 if (i && i != count) {
4434 for (;i >= 0; i--) {
4435 if (atch_list[i].attachment_path) {
4437 if (!emstorage_delete_file(atch_list[i].attachment_path, &err)) {
4438 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4439 emstorage_rollback_transaction(NULL, NULL, NULL);
4445 if (mail->file_path_plain) {
4446 if (!emstorage_delete_file(mail->file_path_plain, &err)) {
4447 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4448 emstorage_rollback_transaction(NULL, NULL, NULL);
4452 if (mail->file_path_html) {
4453 if (!emstorage_delete_file(mail->file_path_html, &err)) {
4454 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4455 emstorage_rollback_transaction(NULL, NULL, NULL);
4460 /* ROLLBACK TRANSACTION; */
4461 emstorage_rollback_transaction(NULL, NULL, NULL);
4465 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4466 err = EMF_ERROR_DB_FAILURE;
4467 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4468 err = EMF_ERROR_DB_FAILURE;
4472 if (!emstorage_get_mailboxname_by_mailbox_type(dst_mailbox->account_id, EMF_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err)) {
4473 EM_DEBUG_EXCEPTION("emstorage_get_mailboxname_by_mailbox_type failed [%d]", err);
4477 if (!strcmp(dst_mailbox->name, mailbox_name) && !(mail->flags_seen_field))
4478 emcore_check_unread_mail();
4483 if (atch_list != NULL)
4484 emstorage_free_attachment(&atch_list, count, NULL);
4487 emstorage_free_mail(&mail, 1, NULL);
4489 EM_SAFE_FREE(mailbox_name);
4491 if (err_code != NULL)
4493 EM_DEBUG_FUNC_END("err [%d]", err);
4498 * move a mail to mail box
4500 * old_mailbox : previous mail box
4502 * new_mailbox : target mail box
4507 INTERNAL_FUNC int emcore_mail_move(int mail_ids[], int mail_ids_count, char *dst_mailbox_name, int noti_param_1, int noti_param_2 ,int *err_code)
4509 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], mail_ids_count[%d], dst_mailbox_name[%s], noti_param [%d], err_code[%p]", mail_ids, mail_ids_count, dst_mailbox_name, noti_param_1, err_code);
4512 int err = EMF_ERROR_NONE;
4513 emstorage_mail_tbl_t *mail_list = NULL;
4515 int i = 0, parameter_string_length = 0;
4516 char *parameter_string = NULL, mail_id_string[10];
4517 emstorage_attachment_tbl_t attachment;
4519 if (!dst_mailbox_name && mail_ids_count < 1) {
4520 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
4521 err = EMF_ERROR_INVALID_PARAM;
4525 if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
4526 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
4531 account_id = mail_list[0].account_id;
4533 if(!emstorage_move_multiple_mails(account_id, dst_mailbox_name, mail_ids, mail_ids_count, true, &err)) {
4534 EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails failed [%d]", err);
4538 /* Sending a notification */
4539 parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
4540 parameter_string = em_malloc(parameter_string_length);
4542 if (mail_list[0].mailbox_name)
4543 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s%c", mail_list[0].mailbox_name, 0x01, dst_mailbox_name , 0x01);
4545 if (parameter_string == NULL) {
4546 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
4547 err = EMF_ERROR_OUT_OF_MEMORY;
4551 for (i = 0; i < mail_ids_count; i++) {
4552 memset(mail_id_string, 0, 10);
4553 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
4554 strcat(parameter_string, mail_id_string);
4557 EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
4559 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
4560 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE] >>>> ");
4562 /* Upating thread mail info should be done as soon as possible */
4563 attachment.mailbox_name = dst_mailbox_name;
4565 for (i = 0; i < mail_ids_count; i++) {
4566 if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, true, &err))
4567 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
4570 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
4571 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
4573 emcore_check_unread_mail();
4578 emstorage_free_mail(&mail_list, mail_ids_count, NULL);
4579 EM_SAFE_FREE(parameter_string);
4580 if (err_code != NULL)
4582 EM_DEBUG_FUNC_END("err [%d]", err);
4587 INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, char *src_mailbox, int mail_ids[], int num, char *dest_mailbox, int *error_code)
4589 EM_DEBUG_FUNC_BEGIN();
4590 MAILSTREAM *stream = NULL;
4592 emf_account_t *ref_account = NULL;
4593 emstorage_mail_tbl_t *mail = NULL;
4598 mail_id = mail_ids[0];
4600 ref_account = emcore_get_account_reference(account_id);
4602 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : get account reference[%d]", account_id);
4603 *error_code = EMF_ERROR_INVALID_ACCOUNT;
4608 /* if not imap4 mail, return */
4609 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
4610 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
4611 *error_code = EMF_ERROR_INVALID_PARAM;
4616 for (i = 0; i < num; i++) {
4617 mail_id = mail_ids[i];
4619 if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail) {
4620 EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id : emstorage_get_downloaded_mail failed [%d]", err_code);
4622 if (err_code == EMF_ERROR_MAIL_NOT_FOUND) { /* not server mail */
4623 /* err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
4627 *error_code = err_code;
4634 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox, (void **)&stream, &err_code)) /* faizan.h@samsung.com mail_move_fix_07042009 */ {
4635 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed : Mailbox open[%d]", err_code);
4642 /* set callback for COPY_UID */
4643 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
4645 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
4647 if (mail->server_mail_id) {
4648 if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
4649 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server : Mail cannot be moved failed");
4653 /* send EXPUNGE command */
4654 if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
4655 EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
4657 if (err_code == EMF_ERROR_IMAP4_STORE_FAILURE)
4658 err_code = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
4662 /* faizan.h@samsung.com duplicate copy while sync mailbox issue fixed */
4663 EM_DEBUG_LOG(">>>mailbox_name[%s]>>>>>>", mail->mailbox_name);
4664 EM_DEBUG_LOG(">>>g_new_server_uid[%s]>>>>>>", g_new_server_uid);
4665 EM_DEBUG_LOG(">>>mail_id[%d]>>>>>>", mail_id);
4667 if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->mailbox_name, &err_code)) {
4668 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
4672 EM_DEBUG_LOG("Mail MOVE SUCCESS ");
4676 EM_DEBUG_EXCEPTION(">>>> Server MAIL ID IS NULL >>>> ");
4679 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
4688 if (stream) emcore_close_mailbox(account_id, stream);
4691 emstorage_free_mail(&mail, 1, NULL);
4692 EM_DEBUG_FUNC_END("ret [%d]", ret);
4696 static int emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code)
4698 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);
4700 int ret = false, err = EMF_ERROR_NONE;
4702 if (!file_name || !full_path || !src_file_path) {
4703 EM_DEBUG_EXCEPTION("Invalid paramter");
4704 err = EMF_ERROR_INVALID_PARAM;
4708 if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err)) {
4709 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4713 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err)) {
4714 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4718 if (strcmp(src_file_path, full_path) != 0) {
4719 if (!emstorage_copy_file(src_file_path, full_path, false, &err)) {
4720 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4735 * update mail information
4737 INTERNAL_FUNC int emcore_update_mail(emf_mail_data_t *input_mail_data, emf_attachment_data_t *input_attachment_data_list, int input_attachment_count, emf_meeting_request_t* input_meeting_request, int input_from_eas)
4739 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);
4741 char filename_buf[1024] = {0, };
4743 int err = EMF_ERROR_NONE;
4744 int local_inline_content_count = 0;
4745 emstorage_mail_tbl_t *converted_mail_tbl_data = NULL;
4746 emf_meeting_request_t *meeting_req = NULL;
4749 if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list)) {
4750 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
4751 err = EMF_ERROR_INVALID_PARAM;
4755 if(input_from_eas == 0) {
4756 if (input_mail_data->file_path_plain) {
4757 if (stat(input_mail_data->file_path_plain, &st_buf) < 0) {
4758 EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
4759 err = EMF_ERROR_INVALID_MAIL;
4764 if (input_mail_data->file_path_html) {
4765 if (stat(input_mail_data->file_path_html, &st_buf) < 0) {
4766 EM_DEBUG_EXCEPTION("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
4767 err = EMF_ERROR_INVALID_MAIL;
4772 if (input_attachment_count && input_attachment_data_list) {
4773 for (i = 0; i < input_attachment_count; i++) {
4774 if (input_attachment_data_list[i].save_status) {
4775 if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0) {
4776 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
4777 err = EMF_ERROR_INVALID_ATTACHMENT;
4785 if(input_mail_data->mail_size == 0) {
4786 emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
4789 if (!input_mail_data->full_address_from) {
4790 emf_account_t *ref_account = NULL;
4791 if (!(ref_account = emcore_get_account_reference(input_mail_data->account_id))) {
4792 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_data->account_id);
4793 err = EMF_ERROR_INVALID_ACCOUNT;
4796 input_mail_data->full_address_from = ref_account->email_addr;
4799 if (input_mail_data->file_path_plain) { /* Save plain text body. */
4800 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, "UTF-8", filename_buf, &err)) {
4801 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4804 EM_SAFE_FREE(input_mail_data->file_path_plain);
4805 input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
4808 if (input_mail_data->file_path_html) { /* Save HTML text body. */
4809 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, "UTF-8.htm", filename_buf, &err)) {
4810 EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4813 EM_SAFE_FREE(input_mail_data->file_path_html);
4814 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
4817 if (input_attachment_data_list && input_attachment_count) {
4819 int compare_result = 1;
4820 char attachment_id[20];
4821 emf_attachment_data_t *temp_attachment_data = NULL;
4823 for(i = 0; i < input_attachment_count; i++) {
4824 temp_attachment_data = input_attachment_data_list + i;
4825 if ( (err = emcore_mail_compare_filename_of_attachment_data(input_mail_data->mail_id, temp_attachment_data->attachment_id, temp_attachment_data, &compare_result)) != EMF_ERROR_NONE) {
4826 EM_DEBUG_EXCEPTION("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
4830 if (compare_result == 0) {
4831 EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
4832 if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data)) {
4833 EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
4838 EM_DEBUG_LOG("save names are different");
4839 SNPRINTF(attachment_id, sizeof(attachment_id), "%d", temp_attachment_data->attachment_id);
4841 if(temp_attachment_data->attachment_id > 0) {
4842 if (!emcore_delete_mail_attachment(input_mail_data->mail_id, attachment_id, &err)) {
4843 EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
4848 if ( (err = emcore_mail_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMF_ERROR_NONE) {
4849 EM_DEBUG_EXCEPTION("emcore_mail_add_attachment failed [%d]", err);
4854 if (temp_attachment_data->inline_content_status)
4855 local_inline_content_count++;
4859 input_mail_data->attachment_count = input_attachment_count;
4860 input_mail_data->inline_content_count = local_inline_content_count;
4862 if (!input_mail_data->date_time) {
4863 /* time isn't set */
4864 input_mail_data->date_time = time(NULL);
4867 EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
4868 if (input_mail_data->preview_text == NULL) {
4869 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))) != EMF_ERROR_NONE) {
4870 EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
4875 if(!em_convert_mail_data_to_mail_tbl(input_mail_data, 1, &converted_mail_tbl_data, &err)) {
4876 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
4880 /* BEGIN TRANSACTION; */
4881 emstorage_begin_transaction(NULL, NULL, NULL);
4883 if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err)) {
4884 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4889 if (input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_REQUEST
4890 || input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_RESPONSE
4891 || input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
4892 /* check where there is a meeting request in DB */
4893 if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMF_ERROR_DATA_NOT_FOUND) {
4894 EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
4897 EM_SAFE_FREE(meeting_req);
4898 if (err == EMF_ERROR_DATA_NOT_FOUND) { /* insert */
4899 emstorage_mail_tbl_t *original_mail = NULL;
4901 if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
4902 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4906 if (original_mail) {
4907 if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_name, input_meeting_request, false, &err)) {
4908 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
4912 emstorage_free_mail(&original_mail, 1, NULL);
4916 if (!emstorage_update_meeting_request(input_meeting_request, false, &err)) {
4917 EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
4925 if (err == EMF_ERROR_NONE) {
4926 /* COMMIT TRANSACTION; */
4927 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4928 err = EMF_ERROR_DB_FAILURE;
4931 if (input_mail_data->meeting_request_status && !emstorage_notify_storage_event(NOTI_MAIL_UPDATE, input_mail_data->account_id, input_mail_data->mail_id, NULL, UPDATE_MEETING))
4932 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_UPDATE : UPDATE_MEETING_RESPONSE ] >>>> ");
4935 /* ROLLBACK TRANSACTION; */
4936 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4937 err = EMF_ERROR_DB_FAILURE;
4943 emstorage_free_meeting_request(&meeting_req, 1, NULL);
4945 if(converted_mail_tbl_data)
4946 emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
4948 EM_DEBUG_FUNC_END("err [%d]", err);
4952 INTERNAL_FUNC int emcore_fetch_flags(int account_id, int mail_id, emf_mail_flag_t *mail_flag, int *err_code)
4954 EM_DEBUG_FUNC_BEGIN();
4958 emstorage_mail_tbl_t *mail = NULL;
4960 EM_IF_NULL_RETURN_VALUE(mail_flag, false);
4961 if (account_id < FIRST_ACCOUNT_ID || mail_id < 1) {
4962 EM_DEBUG_EXCEPTION("emcore_fetch_flags : Invalid Param ");
4966 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail) {
4967 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed - %d ", err);
4972 if (!em_convert_mail_tbl_to_mail_flag(mail, mail_flag, &err)) {
4973 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_int failed - %d ", err);
4981 emstorage_free_mail(&mail, 1, &err);
4983 if (err_code != NULL)
4986 EM_DEBUG_FUNC_END("err [%d]", err);
4989 /* description : modify flag of msgno.
4990 return 1(success), 0(failure)
4992 INTERNAL_FUNC int emcore_modify_flag(int mail_id, emf_mail_flag_t new_flag, int sticky_flag, int *err_code)
4994 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
4997 int err = EMF_ERROR_NONE;
4998 int old_seen, new_seen;
5000 emstorage_mail_tbl_t *mail = NULL;
5001 emf_mail_flag_t old_flag;
5003 if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail) {
5004 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
5009 if (!em_convert_mail_tbl_to_mail_flag(mail, &old_flag, &err)) {
5010 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_flag failed [%d]", err);
5014 old_seen = mail->flags_seen_field;
5015 new_seen = new_flag.seen;
5017 if (!em_convert_mail_flag_to_mail_tbl(&new_flag, mail, &err)) {
5018 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_mail_tbl failed [%d]", err);
5022 mail->lock_status = sticky_flag;
5024 if (!emstorage_change_mail_field(mail_id, UPDATE_FLAG, mail, true, &err)) {
5025 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5031 if (old_seen != new_seen && new_seen == 0)
5032 emcore_delete_notification_for_read_mail(mail_id);
5034 emcore_check_unread_mail();
5040 emstorage_free_mail(&mail, 1, NULL);
5044 EM_DEBUG_FUNC_END("err [%d]", err);
5048 INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num, emf_flags_field_type field_type, int value, int *err_code)
5050 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);
5053 int err = EMF_ERROR_NONE;
5054 char *field_type_name[EMF_FLAGS_FIELD_COUNT] = { "flags_seen_field"
5055 , "flags_deleted_field", "flags_flagged_field", "flags_answered_field"
5056 , "flags_recent_field", "flags_draft_field", "flags_forwarded_field" };
5058 if(field_type < 0 || field_type >= EMF_FLAGS_FIELD_COUNT || mail_ids == NULL || num <= 0 || account_id == 0) {
5059 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
5060 err = EMF_ERROR_INVALID_PARAM;
5064 if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
5065 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
5070 emcore_check_unread_mail();
5078 EM_DEBUG_FUNC_END("err [%d]", err);
5083 /* description : modify extra flag of msgno.
5084 return 0(success), -1(failure)
5086 INTERNAL_FUNC int emcore_modify_extra_flag(int mail_id, emf_extra_flag_t new_flag, int *err_code)
5088 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
5091 int err = EMF_ERROR_NONE;
5092 emstorage_mail_tbl_t mail;
5094 memset(&mail, 0x00, sizeof(emstorage_mail_tbl_t));
5096 mail.mail_id = mail_id;
5097 mail.save_status = new_flag.status;
5098 mail.lock_status = new_flag.report;
5099 mail.lock_status = new_flag.lock;
5100 mail.priority = new_flag.priority;
5101 mail.DRM_status = new_flag.drm;
5103 if (!emstorage_change_mail_field(mail_id, UPDATE_EXTRA_FLAG, &mail, false, &err)) {
5104 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5112 if (err_code != NULL)
5114 EM_DEBUG_FUNC_END("err [%d]", err);
5119 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)
5121 EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
5124 int err = EMF_ERROR_NONE;
5125 int total_body_size = 0;
5127 char *response = NULL;
5128 POP3LOCAL *pop3local;
5130 if (!stream || !result_total_body_size) {
5131 EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
5132 err = EMF_ERROR_INVALID_PARAM;
5136 pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
5138 if (!pop3local || !pop3local->netstream) {
5139 err = EMF_ERROR_INVALID_STREAM;
5142 memset(command, 0x00, sizeof(command));
5144 SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
5146 #ifdef FEATURE_CORE_DEBUG
5147 EM_DEBUG_LOG("[POP3] >>> [%s]", command);
5151 /* send command : LIST [msgno] - to get the size of the mail */
5152 if (!net_sout(pop3local->netstream, command, (int)strlen(command))) {
5153 EM_DEBUG_EXCEPTION("net_sout failed...");
5155 err = EMF_ERROR_INVALID_RESPONSE;
5159 EM_DEBUG_LOG("Sending command success");
5161 while (pop3local->netstream) {
5162 /* receive response */
5163 if (!(response = net_getline(pop3local->netstream)))
5167 EM_DEBUG_LOG("[POP3] <<< %s", response);
5169 if (*response == '.') {
5170 EM_SAFE_FREE(response);
5174 if (*response == '+') { /* "+ OK" */
5177 if (!(p = strchr(response + strlen("+OK "), ' '))) {
5178 err = EMF_ERROR_INVALID_RESPONSE;
5182 total_body_size = atoi(p + 1);
5183 EM_DEBUG_LOG("Body size [%d]", total_body_size);
5185 if (result_total_body_size) {
5186 *result_total_body_size = total_body_size;
5190 else if (*response == '-') { /* "- ERR" */
5191 err = EMF_ERROR_INVALID_RESPONSE;
5195 err = EMF_ERROR_INVALID_RESPONSE;
5199 EM_SAFE_FREE(response);
5202 memset(command, 0x00, sizeof(command));
5204 if (limited_size && total_body_size > limited_size) {
5205 int count_of_line = limited_size / 80;
5206 SNPRINTF(command, sizeof(command), "TOP %d %d", msgno, count_of_line);
5209 SNPRINTF(command, sizeof(command), "RETR %d", msgno);
5211 EM_DEBUG_LOG("[POP3] >>> %s", command);
5213 emcore_set_network_error(EMF_ERROR_NONE); /* set current network error as EMF_ERROR_NONE before network operation */
5214 /* get mail from mail server */
5215 if (!pop3_send((MAILSTREAM *)stream, command, NULL)) {
5216 EM_DEBUG_EXCEPTION("pop3_send failed...");
5218 emf_session_t *session = NULL;
5220 if (!emcore_get_current_session(&session)) {
5221 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
5222 err = EMF_ERROR_SESSION_NOT_FOUND;
5226 if (session->network == EMF_ERROR_NONE)
5227 err = EMF_ERROR_UNKNOWN;
5229 err = session->network;
5237 EM_SAFE_FREE(response);
5239 if (err_code != NULL)
5241 EM_DEBUG_FUNC_END("err [%d]", err);
5246 INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
5248 EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
5251 if (err_code != NULL)
5252 *err_code = EMF_ERROR_INVALID_PARAM;
5257 int err = EMF_ERROR_NONE;
5258 int status = EMF_DOWNLOAD_FAIL;
5259 MAILSTREAM *stream = NULL;
5260 emf_mailbox_t mbox = {0};
5261 emstorage_mail_tbl_t *mail = NULL;
5262 emf_account_t *ref_account = NULL;
5265 emf_mail_flag_t new_flag = {0};
5266 char set_flags[100] = { 0, };
5267 char clear_flags[100] = { 0, };
5268 char tmp[100] = { 0, };
5270 if (!emcore_check_thread_status()) {
5271 err = EMF_ERROR_CANCELLED;
5275 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5276 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5281 account_id = mail->account_id;
5282 em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5284 if (!(ref_account = emcore_get_account_reference(account_id))) {
5285 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5286 err = EMF_ERROR_INVALID_ACCOUNT;
5290 if (!emcore_check_thread_status()) {
5291 err = EMF_ERROR_CANCELLED;
5295 /* open mail server. */
5296 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream) {
5297 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5298 status = EMF_LIST_CONNECTION_FAIL;
5303 if (!emcore_check_thread_status()) {
5304 err = EMF_ERROR_CANCELLED;
5308 mbox.name = mail->mailbox_name;
5309 mbox.account_id = account_id;
5310 mbox.mail_stream = stream;
5312 /* download all uids from server. */
5313 if (!emcore_get_mail_msgno_by_uid(ref_account, &mbox, mail->server_mail_id, &msgno, &err)) {
5314 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5318 sprintf (tmp, "%d", msgno);
5321 sprintf(set_flags, "\\Seen");
5323 sprintf(clear_flags, "\\Seen");
5325 if (new_flag.answered)
5326 sprintf(set_flags, "%s \\Answered", set_flags);
5328 sprintf(clear_flags, "%s \\Answered", clear_flags);
5330 if (new_flag.flagged)
5331 sprintf(set_flags, "%s \\Flagged", set_flags);
5333 sprintf(clear_flags, "%s \\Flagged", clear_flags);
5335 if (new_flag.forwarded)
5336 sprintf(set_flags, "%s $Forwarded", set_flags);
5338 sprintf(clear_flags, "%s $Forwarded", clear_flags);
5340 if (strlen(set_flags) > 0) {
5341 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s] ", set_flags);
5342 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5343 EM_DEBUG_LOG(">>>> End mail_setflag ");
5346 if (strlen(clear_flags) > 0) {
5347 EM_DEBUG_LOG(">>>> Calling mail_clearflag [%s]", clear_flags);
5348 mail_clearflag(stream, tmp, clear_flags);
5349 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5352 if (mail->lock_status) {
5353 memset(set_flags, 0x00, 100);
5354 sprintf(set_flags, "Sticky");
5355 if (strlen(set_flags) > 0) {
5356 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s]", set_flags);
5357 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5358 EM_DEBUG_LOG(">>>> End mail_setflag ");
5362 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5364 if (!emcore_check_thread_status()) {
5365 err = EMF_ERROR_CANCELLED;
5373 if (stream) emcore_close_mailbox(account_id, stream);
5374 if (mail) emstorage_free_mail(&mail, 1, NULL);
5376 if (err_code != NULL)
5378 EM_DEBUG_FUNC_END("err [%d]", err);
5382 INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
5384 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
5386 if (mail_ids[0] < 1) {
5387 if (err_code != NULL)
5388 *err_code = EMF_ERROR_INVALID_PARAM;
5393 int err = EMF_ERROR_NONE;
5394 int status = EMF_DOWNLOAD_FAIL;
5395 MAILSTREAM *stream = NULL;
5397 emstorage_mail_tbl_t *mail = NULL;
5398 emf_account_t *ref_account = NULL;
5401 emf_mail_flag_t new_flag;
5402 char set_flags[100];
5403 char clear_flags[100];
5408 memset(&mbox, 0x00, sizeof(emf_mailbox_t));
5410 mail_id = mail_ids[0];
5412 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5413 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5418 account_id = mail->account_id;
5420 if (!(ref_account = emcore_get_account_reference(account_id))) {
5421 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5422 err = EMF_ERROR_INVALID_ACCOUNT;
5426 /* open mail server. */
5427 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream) {
5428 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5429 status = EMF_LIST_CONNECTION_FAIL;
5433 mbox.name = mail->mailbox_name;
5434 mbox.account_id = account_id;
5435 mbox.mail_stream = stream;
5437 for (i = 0; i < num; i++) {
5438 mail_id = mail_ids[i];
5440 if (!emcore_check_thread_status()) {
5441 err = EMF_ERROR_CANCELLED;
5445 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
5446 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5451 /* account_id = mail->account_id; */
5452 em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5454 if (!emcore_check_thread_status()) {
5455 err = EMF_ERROR_CANCELLED;
5459 /* download message number from server. */
5460 if (!emcore_get_mail_msgno_by_uid(ref_account, &mbox, mail->server_mail_id, &msgno, &err)) {
5461 EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no : %d ", err);
5465 memset(tmp, 0x00, 100);
5466 sprintf (tmp, "%d", msgno);
5468 memset(set_flags, 0x00, 100);
5469 memset(clear_flags, 0x00, 100);
5472 sprintf(set_flags, "\\Seen");
5474 sprintf(clear_flags, "\\Seen");
5475 EM_DEBUG_LOG("new_flag.seen : %s ", set_flags);
5477 if (strlen(set_flags) > 0) {
5478 EM_DEBUG_LOG(">>>> Calling mail_setflag ");
5479 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5480 EM_DEBUG_LOG(">>>> End mail_setflag ");
5483 EM_DEBUG_LOG(">>>> Calling mail_clearflag ");
5484 mail_clearflag(stream, tmp, clear_flags);
5485 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5488 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5490 if (!emcore_check_thread_status()) {
5491 err = EMF_ERROR_CANCELLED;
5499 if (stream) emcore_close_mailbox(account_id, stream);
5500 if (mail) emstorage_free_mail(&mail, 1, NULL);
5502 if (err_code != NULL)
5504 EM_DEBUG_FUNC_END("err [%d]", err);
5508 INTERNAL_FUNC int emcore_free_mail_data(emf_mail_data_t **mail_list, int count, int *err_code)
5510 EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d]", mail_list, count);
5512 if (count <= 0 || !mail_list || !*mail_list) {
5513 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
5515 *err_code = EMF_ERROR_INVALID_PARAM;
5519 emf_mail_data_t* p = *mail_list;
5522 for (i = 0; i < count; i++) {
5523 EM_SAFE_FREE(p[i].mailbox_name);
5524 EM_SAFE_FREE(p[i].subject);
5525 EM_SAFE_FREE(p[i].server_mailbox_name);
5526 EM_SAFE_FREE(p[i].server_mail_id);
5527 EM_SAFE_FREE(p[i].message_id);
5528 EM_SAFE_FREE(p[i].full_address_from);
5529 EM_SAFE_FREE(p[i].full_address_reply);
5530 EM_SAFE_FREE(p[i].full_address_to);
5531 EM_SAFE_FREE(p[i].full_address_cc);
5532 EM_SAFE_FREE(p[i].full_address_bcc);
5533 EM_SAFE_FREE(p[i].full_address_return);
5534 EM_SAFE_FREE(p[i].email_address_sender);
5535 EM_SAFE_FREE(p[i].email_address_recipient);
5536 EM_SAFE_FREE(p[i].alias_sender);
5537 EM_SAFE_FREE(p[i].alias_recipient);
5538 EM_SAFE_FREE(p[i].file_path_plain);
5539 EM_SAFE_FREE(p[i].file_path_html);
5540 EM_SAFE_FREE(p[i].preview_text);
5543 EM_SAFE_FREE(p); *mail_list = NULL;
5546 *err_code = EMF_ERROR_NONE;
5548 EM_DEBUG_FUNC_END();
5553 INTERNAL_FUNC int emcore_free_attachment_info(emf_attachment_info_t **atch_info, int *err_code)
5555 EM_DEBUG_FUNC_BEGIN("atch_info[%p], err_code[%p]", atch_info, err_code);
5557 if (!atch_info || !*atch_info) {
5558 if (err_code != NULL)
5559 *err_code = EMF_ERROR_INVALID_PARAM;
5563 emf_attachment_info_t *p = *atch_info;
5564 emf_attachment_info_t *t;
5567 EM_SAFE_FREE(p->name);
5568 EM_SAFE_FREE(p->savename);
5574 if (err_code != NULL)
5575 *err_code = EMF_ERROR_NONE;
5576 EM_DEBUG_FUNC_END();
5581 INTERNAL_FUNC int emcore_free_attachment_data(emf_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
5583 EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);
5585 if (!attachment_data_list || !*attachment_data_list) {
5586 if (err_code != NULL)
5587 *err_code = EMF_ERROR_INVALID_PARAM;
5591 emf_attachment_data_t* p = *attachment_data_list;
5594 for (i = 0; i < attachment_data_count; i++) {
5595 EM_SAFE_FREE(p[i].mailbox_name);
5596 EM_SAFE_FREE(p[i].attachment_name);
5597 EM_SAFE_FREE(p[i].attachment_path);
5600 EM_SAFE_FREE(p); *attachment_data_list = NULL;
5603 *err_code = EMF_ERROR_NONE;
5605 EM_DEBUG_FUNC_END();
5611 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
5613 INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code)
5615 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5617 if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
5618 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5619 if (err_code != NULL)
5620 *err_code = EMF_ERROR_INVALID_PARAM;
5625 int err = EMF_ERROR_NONE;
5627 emstorage_begin_transaction(NULL, NULL, NULL);
5629 if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err)) {
5630 EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
5639 if (ret == true) { /* COMMIT TRANSACTION; */
5640 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5641 err = EMF_ERROR_DB_FAILURE;
5645 else { /* ROLLBACK TRANSACTION; */
5646 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5647 err = EMF_ERROR_DB_FAILURE;
5650 if (err_code != NULL)
5652 EM_DEBUG_FUNC_END();
5656 INTERNAL_FUNC int emcore_insert_pbd_activity(emf_event_partial_body_thd *local_activity, int *activity_id, int *err_code)
5658 EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
5660 if (!local_activity || !activity_id) {
5661 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
5662 if (err_code != NULL)
5663 *err_code = EMF_ERROR_INVALID_PARAM;
5668 int err = EMF_ERROR_NONE;
5670 emstorage_begin_transaction(NULL, NULL, NULL);
5672 if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err)) {
5673 EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
5682 if (ret == true) { /* COMMIT TRANSACTION; */
5683 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5684 err = EMF_ERROR_DB_FAILURE;
5688 else { /* ROLLBACK TRANSACTION; */
5689 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5690 err = EMF_ERROR_DB_FAILURE;
5693 if (err_code != NULL)
5695 EM_DEBUG_FUNC_END();
5701 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
5703 /* API to set or unset a field of flags on server in single IMAP request to server */
5705 INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, emf_flags_field_type field_type, int value, int *err_code)
5707 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);
5709 if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMF_FLAGS_FIELD_COUNT) {
5710 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
5711 if (err_code != NULL) {
5712 *err_code = EMF_ERROR_INVALID_PARAM;
5717 MAILSTREAM *stream = NULL;
5718 IMAPLOCAL *imaplocal = NULL;
5719 char tag[MAX_TAG_SIZE] = {0, };
5720 char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
5722 char **string_list = NULL;
5724 int err = EMF_ERROR_NONE;
5725 int command_success = false;
5729 int id_set_count = 0;
5730 int len_of_each_range = 0;
5731 int string_count = 0;
5732 emf_account_t *temp_account;
5733 emf_id_set_t *id_set = NULL;
5734 emstorage_mail_tbl_t *mail = NULL;
5735 emf_uid_range_set *uid_range_set = NULL;
5736 emf_uid_range_set *uid_range_node = NULL;
5737 char *field_type_name[EMF_FLAGS_FIELD_COUNT] = { "\\Seen"
5738 , "\\Deleted", "\\Flagged", "\\Answered"
5739 , "\\Recent", "\\Draft", "$Forwarded" };
5741 mail_id = mail_ids[0];
5743 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 */ {
5744 EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
5749 account_id = mail[0].account_id;
5751 temp_account = emcore_get_account_reference(account_id);
5753 if (!temp_account) {
5754 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5755 err = EMF_ERROR_INVALID_ACCOUNT;
5759 if (temp_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
5760 EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->receiving_server_type);
5761 err = EMF_ERROR_NOT_SUPPORTED;
5765 if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream) {
5766 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5770 if (false == emstorage_free_mail(&mail, 1, &err)) {
5771 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5775 /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
5776 /* 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 */
5777 /* 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 */
5778 /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
5780 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err)) {
5781 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
5785 /* Now execute one by one each comma separated string of mail_ids */
5787 for (i = 0; i < string_count; ++i) {
5788 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
5790 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
5791 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
5795 /* 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 */
5797 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /* 1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
5799 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
5800 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
5804 uid_range_node = uid_range_set;
5806 while (uid_range_node != NULL) {
5807 /* Remove comma from end of uid_range */
5809 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
5811 /* Form the IMAP command */
5813 SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
5816 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]);
5818 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]);
5820 EM_DEBUG_LOG("[IMAP4] command %s", cmd);
5824 if (!(imaplocal = stream->local) || !imaplocal->netstream) {
5825 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
5827 err = EMF_ERROR_UNKNOWN;
5831 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
5832 EM_DEBUG_EXCEPTION("net_sout failed...");
5833 err = EMF_ERROR_CONNECTION_BROKEN;
5837 /* Receive Response */
5839 command_success = false;
5841 while (imaplocal->netstream) {
5842 if (!(p = net_getline(imaplocal->netstream))) {
5843 EM_DEBUG_EXCEPTION("net_getline failed...");
5845 err = EMF_ERROR_INVALID_RESPONSE;
5849 EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
5851 if (!strncmp(p, tag, strlen(tag))) {
5852 if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
5853 /*Delete all local activities */
5854 command_success = true;
5861 command_success = false;
5862 err = EMF_ERROR_IMAP4_STORE_FAILURE;
5871 uid_range_node = uid_range_node->next;
5874 emcore_free_uid_range_set(&uid_range_set);
5876 EM_SAFE_FREE(id_set);
5885 #ifdef __FEATURE_LOCAL_ACTIVITY__
5887 emstorage_activity_tbl_t new_activity;
5888 for (i = 0; i<num ; i++) {
5889 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
5890 new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
5891 new_activity.account_id = account_id;
5892 new_activity.mail_id = mail_ids[i];
5894 if (!emcore_delete_activity(&new_activity, &err))
5895 EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
5902 if (false == emstorage_free_mail(&mail, 1, &err))
5903 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5906 emcore_free_comma_separated_strings(&string_list, &string_count);
5909 emcore_free_uid_range_set(&uid_range_set);
5911 emcore_close_mailbox(0, stream);
5914 if (err_code != NULL)
5916 EM_DEBUG_FUNC_END();
5922 INTERNAL_FUNC int emcore_mail_filter_by_rule(emf_rule_t *filter_info, int *err_code)
5924 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
5926 int ret = false, err = EMF_ERROR_NONE;
5927 int account_index, account_count, mail_id_index = 0;
5928 emf_account_t *account_ref = NULL, *account_list_ref = NULL;
5929 int filtered_mail_id_count = 0, *filtered_mail_id_list = NULL, parameter_string_length = 0;
5930 char *parameter_string = NULL, mail_id_string[10] = { 0x00, };
5931 emstorage_mailbox_tbl_t *spam_mailbox = NULL;
5934 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
5935 err = EMF_ERROR_INVALID_PARAM;
5939 if (!emcore_get_account_reference_list(&account_list_ref, &account_count, &err)) {
5940 EM_DEBUG_EXCEPTION("emcore_get_account_reference_list failed [%d]", err);
5944 for (account_index = 0; account_index < account_count; account_index++) {
5945 account_ref = account_list_ref + account_index;
5947 if (!emstorage_get_mailbox_by_mailbox_type(account_ref->account_id, EMF_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err))
5948 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed [%d]", account_ref->account_id, err);
5949 else if (spam_mailbox && spam_mailbox->mailbox_name) {
5950 if (!emstorage_filter_mails_by_rule(account_ref->account_id, spam_mailbox->mailbox_name, (emstorage_rule_tbl_t *)filter_info, &filtered_mail_id_list, &filtered_mail_id_count, &err))
5951 EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
5953 if (filtered_mail_id_count) {
5954 parameter_string_length = strlen(spam_mailbox->mailbox_name) + 7 + (10 * filtered_mail_id_count);
5955 parameter_string = em_malloc(sizeof(char) * parameter_string_length);
5957 if (parameter_string == NULL) {
5958 err = EMF_ERROR_OUT_OF_MEMORY;
5959 EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
5963 SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%s%c", 0x01, spam_mailbox->mailbox_name, 0x01);
5965 for (mail_id_index = 0; mail_id_index < filtered_mail_id_count; mail_id_index++) {
5966 memset(mail_id_string, 0, 10);
5967 SNPRINTF(mail_id_string, 10, "%d", filtered_mail_id_list[mail_id_index]);
5968 strcat(parameter_string, mail_id_string);
5969 strcat(parameter_string, ",");
5972 EM_DEBUG_LOG("filtered_mail_id_count [%d]", filtered_mail_id_count);
5973 EM_DEBUG_LOG("param string [%s]", parameter_string);
5975 if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_ref->account_id, 0, parameter_string, 0))
5976 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
5978 EM_SAFE_FREE(filtered_mail_id_list);
5979 EM_SAFE_FREE(parameter_string);
5982 emstorage_free_mailbox(&spam_mailbox, 1, &err);
5983 spam_mailbox = NULL;
5987 emcore_check_unread_mail();
5992 EM_SAFE_FREE(account_list_ref);
5993 EM_SAFE_FREE(filtered_mail_id_list);
5994 EM_SAFE_FREE(parameter_string);
5998 emstorage_free_mailbox(&spam_mailbox, 1, &err);
6002 EM_DEBUG_FUNC_END();