4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 /******************************************************************************
25 * File: email-daemon-mail.c
26 * Desc: email-daemon Mail Operation
31 * 2006.08.16 : created
32 *****************************************************************************/
36 #include <sys/types.h>
43 #include "email-internal-types.h"
44 #include "email-daemon.h"
45 #include "email-core-event.h"
46 #include "email-daemon-account.h"
47 #include "email-debug-log.h"
48 #include "email-storage.h"
49 #include "email-utilities.h"
50 #include "email-core-account.h"
51 #include "email-core-mail.h"
52 #include "email-core-mailbox.h"
53 #include "email-core-utils.h"
54 #include "email-core-smtp.h"
55 #include "email-core-timer.h"
56 #include "email-core-signal.h"
58 #ifdef __FEATURE_LOCAL_ACTIVITY__
59 extern int g_local_activity_run;
60 extern int g_save_local_activity_run;
62 static int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code);
64 INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, int mail_id, int *handle, int* err_code)
66 EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p], err_code[%p]", mail_id, handle, err_code);
68 int ret = false, err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
69 int result_handle = 0, account_id = 0;
70 email_event_t *event_data = NULL;
71 emstorage_mail_tbl_t* mail_table_data = NULL;
72 emstorage_mailbox_tbl_t* local_mailbox = NULL;
73 email_account_t* ref_account = NULL;
74 int dst_mailbox_id = 0;
77 EM_DEBUG_EXCEPTION("mail_id is not valid");
78 err= EMAIL_ERROR_INVALID_PARAM;
82 if(!emstorage_get_mail_by_id(multi_user_name, mail_id, &mail_table_data, true, &err)) {
83 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
87 if (!mail_table_data || mail_table_data->mailbox_id <= 0 || mail_table_data->account_id <= 0) {
88 if (mail_table_data != NULL)
89 EM_DEBUG_EXCEPTION(" mail_table_data->mailbox_id[%d], mail_table_data->account_id[%d]", mail_table_data->mailbox_id, mail_table_data->account_id);
91 *err_code = EMAIL_ERROR_INVALID_MAILBOX;
93 emstorage_free_mail(&mail_table_data, 1, &err);
97 account_id = mail_table_data->account_id;
99 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
101 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
102 err = EMAIL_ERROR_INVALID_ACCOUNT;
106 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
107 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err)) {
108 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
111 dst_mailbox_id = local_mailbox->mailbox_id;
112 if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
113 /* mail is moved to 'OUTBOX' first of all. */
114 if (!emcore_move_mail(multi_user_name, &mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
115 EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
119 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
121 if(!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
122 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
124 /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
125 if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
126 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
130 event_data = em_malloc(sizeof(email_event_t));
131 if (event_data == NULL) {
132 EM_DEBUG_EXCEPTION("em_malloc failed");
133 err = EMAIL_ERROR_OUT_OF_MEMORY;
137 event_data->type = EMAIL_EVENT_SEND_MAIL;
138 event_data->account_id = account_id;
139 event_data->event_param_data_4 = mail_id;
140 event_data->event_param_data_5 = mail_table_data->mailbox_id;
141 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
143 if (!emcore_insert_event_for_sending_mails(event_data, &result_handle, &err)) {
144 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
148 #ifdef __FEATURE_LOCAL_ACTIVITY__
149 EM_DEBUG_LOG("Setting g_save_local_activity_run ");
150 g_save_local_activity_run = 1;
154 *handle = result_handle;
160 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
162 if(!emstorage_set_field_of_mails_with_integer_value(multi_user_name, account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
163 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
166 emcore_free_event(event_data);
167 EM_SAFE_FREE(event_data);
172 emcore_free_account(ref_account);
173 EM_SAFE_FREE(ref_account);
176 if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
177 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
180 emstorage_free_mailbox(&local_mailbox, 1, NULL);
182 if (err_code != NULL)
186 emstorage_free_mail(&mail_table_data, 1, &err);
192 INTERNAL_FUNC int emdaemon_send_mail_saved(char *multi_user_name, int account_id, int *handle, int* err_code)
194 EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
197 int err = EMAIL_ERROR_NONE;
198 email_event_t *event_data = NULL;
199 email_account_t *ref_account = NULL;
200 char *mailbox_name = NULL;
202 if (account_id <= 0) {
203 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
204 err = EMAIL_ERROR_INVALID_PARAM;
208 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
210 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
211 err = EMAIL_ERROR_INVALID_ACCOUNT;
215 if (!emstorage_get_mailbox_name_by_mailbox_type(multi_user_name, account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err)) {
216 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
220 event_data = em_malloc(sizeof(email_event_t));
221 if (event_data == NULL) {
222 EM_DEBUG_EXCEPTION("em_malloc failed");
223 err = EMAIL_ERROR_OUT_OF_MEMORY;
227 event_data->type = EMAIL_EVENT_SEND_MAIL_SAVED;
228 event_data->account_id = account_id;
229 event_data->event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
230 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
232 if (!emcore_insert_event_for_sending_mails(event_data, (int *)handle, &err)) {
233 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
241 if (ret == false && event_data) {
242 emcore_free_event(event_data);
243 EM_SAFE_FREE(event_data);
247 emcore_free_account(ref_account);
248 EM_SAFE_FREE(ref_account);
251 EM_SAFE_FREE(mailbox_name);
260 INTERNAL_FUNC int emdaemon_add_mail(char *multi_user_name,
261 email_mail_data_t *input_mail_data,
262 email_attachment_data_t *input_attachment_data_list,
263 int input_attachment_count,
264 email_meeting_request_t *input_meeting_request,
267 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
270 int err = EMAIL_ERROR_NONE;
272 email_event_t *event_data = NULL;
273 email_account_t *ref_account = NULL;
275 if (!input_mail_data || input_mail_data->account_id <= 0 ||
276 ( ((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to)) {
277 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
278 err = EMAIL_ERROR_INVALID_PARAM;
282 ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
284 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
285 err = EMAIL_ERROR_INVALID_ACCOUNT;
289 if ((err = emcore_add_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas, false)) != EMAIL_ERROR_NONE) {
290 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
294 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
295 if ( input_from_eas == 0 && ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
296 event_data = em_malloc(sizeof(email_event_t));
297 if (event_data == NULL) {
298 EM_DEBUG_EXCEPTION("em_malloc failed");
299 err = EMAIL_ERROR_OUT_OF_MEMORY;
303 event_data->type = EMAIL_EVENT_SAVE_MAIL;
304 event_data->account_id = input_mail_data->account_id;
305 event_data->event_param_data_4 = input_mail_data->mail_id;
306 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
308 if (!emcore_insert_event(event_data, &handle, &err)) {
309 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
319 if (ret == false && event_data) {
320 emcore_free_event(event_data);
321 EM_SAFE_FREE(event_data);
325 emcore_free_account(ref_account);
326 EM_SAFE_FREE(ref_account);
329 EM_DEBUG_FUNC_END("err [%d]", err);
335 INTERNAL_FUNC int emdaemon_add_meeting_request(char *multi_user_name, int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
337 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], meeting_req[%p], err_code[%p]", account_id, input_mailbox_id, meeting_req, err_code);
339 /* default variable */
341 int err = EMAIL_ERROR_NONE;
343 if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 ) {
345 EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
347 err = EMAIL_ERROR_INVALID_PARAM;
351 if (!emcore_add_meeting_request(multi_user_name, account_id, input_mailbox_id, meeting_req, &err)) {
352 EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
365 INTERNAL_FUNC int emdaemon_download_body(char *multi_user_name, int account_id, int mail_id, int verbose, int with_attachment, int *handle, int* err_code)
367 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], verbose[%d], with_attachment[%d], handle[%p], err_code[%p]", account_id, mail_id, verbose, with_attachment, handle, err_code);
370 int err = EMAIL_ERROR_NONE;
371 email_event_t *event_data = NULL;
373 if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
374 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
378 event_data = em_malloc(sizeof(email_event_t));
379 if (event_data == NULL) {
380 EM_DEBUG_EXCEPTION("em_malloc failed");
381 err = EMAIL_ERROR_OUT_OF_MEMORY;
385 event_data->type = EMAIL_EVENT_DOWNLOAD_BODY;
386 event_data->account_id = account_id;
387 event_data->event_param_data_4 = mail_id;
388 event_data->event_param_data_5 = (verbose << 1 | with_attachment);
389 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
391 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
392 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
393 err = EMAIL_ERROR_DB_FAILURE;
397 #ifdef __FEATURE_LOCAL_ACTIVITY__
398 EM_DEBUG_LOG("Setting g_local_activity_run ");
399 g_local_activity_run = 1;
405 if (ret == false && event_data) {
406 emcore_free_event(event_data);
407 EM_SAFE_FREE(event_data);
418 int emdaemon_get_attachment(char *multi_user_name, int attachment_id, email_attachment_data_t** attachment, int* err_code)
420 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
422 /* default variable */
424 int err = EMAIL_ERROR_NONE;
426 if (!attachment_id || !attachment) {
427 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%d], attachment[%p]\n", attachment_id, attachment);
428 err = EMAIL_ERROR_INVALID_PARAM;
432 if (!emcore_get_attachment_info(multi_user_name, attachment_id, attachment, &err) || !attachment) {
433 EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
449 int emdaemon_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t* attachment, int* err_code)
451 EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
453 /* default variable */
455 int err = EMAIL_ERROR_NONE;
458 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
459 err = EMAIL_ERROR_INVALID_PARAM;
463 if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
464 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
468 if (!emcore_add_attachment(multi_user_name, mail_id, attachment, &err)) {
469 EM_DEBUG_EXCEPTION(" emcore_add_attachment failed [%d]", err);
485 int emdaemon_delete_mail_attachment(char *multi_user_name, int attachment_id, int* err_code)
487 EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
489 /* default variable */
491 int err = EMAIL_ERROR_NONE;
493 if (!attachment_id) {
494 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
495 err = EMAIL_ERROR_INVALID_PARAM;
499 if (!emcore_delete_mail_attachment(multi_user_name, attachment_id, &err)) {
500 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
516 INTERNAL_FUNC int emdaemon_download_attachment(char *multi_user_name, int account_id, int mail_id, int nth, int *handle, int* err_code)
518 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%d], handle[%p], err_code[%p]", account_id, mail_id, nth, handle, err_code);
521 int err = EMAIL_ERROR_NONE;
522 email_event_t *event_data = NULL;
525 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
526 err = EMAIL_ERROR_INVALID_PARAM;
530 if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
531 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
535 event_data = em_malloc(sizeof(email_event_t));
536 if (event_data == NULL) {
537 EM_DEBUG_EXCEPTION("em_malloc failed");
538 err = EMAIL_ERROR_OUT_OF_MEMORY;
542 event_data->type = EMAIL_EVENT_DOWNLOAD_ATTACHMENT;
543 event_data->account_id = account_id;
544 event_data->event_param_data_4 = mail_id;
545 event_data->event_param_data_5 = nth;
546 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
548 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
549 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
553 #ifdef __FEATURE_LOCAL_ACTIVITY__
554 EM_DEBUG_LOG("Setting g_local_activity_run ");
555 g_local_activity_run = 1;
562 if (ret == false && event_data) {
563 emcore_free_event(event_data);
564 EM_SAFE_FREE(event_data);
575 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
577 EM_DEBUG_FUNC_BEGIN();
578 return emcore_free_attachment_data(atch_data, 1, err_code);
581 void* thread_func_to_delete_mail(void *thread_argument)
583 EM_DEBUG_FUNC_BEGIN();
584 int err = EMAIL_ERROR_NONE;
585 int *mail_id_list = NULL;
586 int mail_id_count = 0;
589 int noti_param_2 = 0;
591 char *multi_user_name = NULL;
592 email_event_t *event_data = (email_event_t*)thread_argument;
594 account_id = event_data->account_id;
595 mail_id_list = (int*)event_data->event_param_data_3;
596 mail_id_count = event_data->event_param_data_4;
597 from_server = event_data->event_param_data_5;
598 multi_user_name = event_data->multi_user_name;
600 if (!emcore_delete_mail(multi_user_name, account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
601 EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
602 emcore_free_event(event_data); /* prevent 17922 */
603 EM_SAFE_FREE(event_data);
607 if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
608 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
609 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
610 /* if (from_server != EMAIL_DELETE_LOCAL_AND_SERVER && from_server != EMAIL_DELETE_FROM_SERVER) {
611 EM_SAFE_FREE(event_data->event_param_data_3);
613 emcore_free_event(event_data); /* prevent 17922 */
614 EM_SAFE_FREE(event_data);
619 emcore_free_event(event_data); /* prevent 17922 */
620 EM_SAFE_FREE(event_data);
624 /* Don't free event_data as if the data is destined to be passed to event handler through emcore_insert_event.*/
630 INTERNAL_FUNC int emdaemon_delete_mail(char *multi_user_name, int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, int *handle, int* err_code)
632 EM_DEBUG_FUNC_BEGIN("mailbox_id[%d], mail_ids[%p], mail_ids_count[%d], from_server[%d], handle[%p], err_code[%p]", mailbox_id, mail_ids, mail_ids_count, from_server, handle, err_code);
635 int err = EMAIL_ERROR_NONE;
637 int thread_error = 0;
638 email_account_t *ref_account = NULL;
639 email_event_t *event_data = NULL;
640 emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
641 thread_t delete_thread;
643 /* mailbox can be NULL for deleting thread mail. */
644 if (mail_ids_count <= 0) {
645 EM_DEBUG_EXCEPTION("mail_ids_count [%d]", mail_ids_count);
646 err = EMAIL_ERROR_INVALID_PARAM;
650 if ( (err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
651 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
655 if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
656 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
657 err = EMAIL_ERROR_OUT_OF_MEMORY;
661 memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
663 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
664 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
665 err = EMAIL_ERROR_OUT_OF_MEMORY;
669 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl_data->account_id, false);
671 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
672 err = EMAIL_ERROR_INVALID_ACCOUNT;
676 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
677 from_server = EMAIL_DELETE_LOCALLY;
680 event_data->type = EMAIL_EVENT_DELETE_MAIL;
681 event_data->account_id = mailbox_tbl_data->account_id;
682 event_data->event_param_data_3 = (char*)p;
683 event_data->event_param_data_4 = mail_ids_count;
684 event_data->event_param_data_5 = from_server;
685 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
687 THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
688 THREAD_DETACH(delete_thread); /* free resources used for new thread */
693 if (mailbox_tbl_data)
694 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
697 emcore_free_account(ref_account);
698 EM_SAFE_FREE(ref_account);
701 if (err != EMAIL_ERROR_NONE || thread_error != 0) {
702 emcore_free_event(event_data);
703 EM_SAFE_FREE(event_data);
713 int emdaemon_delete_mail_all(char *multi_user_name, int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
715 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], input_from_server[%d], handle[%p], err_code[%p]", input_mailbox_id, input_from_server, output_handle, output_err_code);
718 int err = EMAIL_ERROR_NONE;
719 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
720 email_event_t *event_data = NULL;
722 if (!input_mailbox_id) {
723 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
724 err = EMAIL_ERROR_INVALID_PARAM;
728 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
729 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
733 if(!emcore_delete_all_mails_of_mailbox(multi_user_name, mailbox_tbl->account_id, input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
734 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
738 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
739 if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
740 event_data = em_malloc(sizeof(email_event_t));
741 if (event_data == NULL) {
742 EM_DEBUG_EXCEPTION("em_malloc failed");
743 err = EMAIL_ERROR_OUT_OF_MEMORY;
747 event_data->type = EMAIL_EVENT_DELETE_MAIL_ALL;
748 event_data->account_id = mailbox_tbl->account_id;
749 event_data->event_param_data_4 = input_mailbox_id;
750 event_data->event_param_data_5 = input_from_server;
751 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
753 if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
754 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
758 #ifdef __FEATURE_LOCAL_ACTIVITY__
759 int i, total = 0 , search_handle = 0;
760 int *mail_ids = NULL;
761 emstorage_activity_tbl_t new_activity;
764 if (false == emcore_get_next_activity_id(&activityid,&err)) {
765 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
768 if (!emstorage_mail_search_start(multi_user_name, NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
769 EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
773 mail_ids = em_malloc(sizeof(int) * total);
774 if (mail_ids == NULL) {
775 EM_DEBUG_EXCEPTION(" mailloc failed...");
776 err = EMAIL_ERROR_OUT_OF_MEMORY;
780 for (i = 0; i < total; i++) {
781 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
782 EM_DEBUG_EXCEPTION(" emstorage_mail_search_result failed [%d]", err);
785 EM_SAFE_FREE(mail_ids);
789 new_activity.activity_id = activityid;
790 new_activity.activity_type = ACTIVITY_DELETEMAIL;
791 new_activity.mail_id = mail_ids[i];
792 new_activity.server_mailid = NULL;
793 new_activity.src_mbox = mailbox->mailbox_name;
794 new_activity.dest_mbox = NULL;
795 new_activity.account_id = mailbox->account_id;
797 if (! emcore_add_activity(&new_activity, &err))
798 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
802 EM_SAFE_FREE(mail_ids);
804 EM_DEBUG_LOG("Setting g_local_activity_run ");
805 g_local_activity_run = 1;
808 #endif /* __FEATURE_SYNC_CLIENT_TO_SERVER__ */
814 if (ret == false && event_data) {
815 emcore_free_event(event_data);
816 EM_SAFE_FREE(event_data);
820 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
823 *output_err_code = err;
828 void* thread_func_to_move_mail(void *thread_argument)
830 EM_DEBUG_FUNC_BEGIN();
833 int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
835 email_event_t *event_data = (email_event_t*)thread_argument;
836 int dst_mailbox_id = 0;
837 char *multi_user_name = NULL;
839 if(!event_data) { /*prevent 53096*/
840 EM_DEBUG_EXCEPTION("INVALID_PARMAETER");
845 /* dst_mailbox_name = (char*)event_data->event_param_data_1; */ /*prevent 33693*/
846 mail_ids = (int*)event_data->event_param_data_3;
847 mail_ids_count = event_data->event_param_data_4;
848 dst_mailbox_id = event_data->event_param_data_5;
849 noti_param_1 = event_data->event_param_data_6;
850 noti_param_2 = event_data->event_param_data_7;
851 multi_user_name = event_data->multi_user_name;
853 if (!emcore_move_mail(multi_user_name, mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
854 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
858 if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
859 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
867 if (ret == false && event_data) {
868 emcore_free_event(event_data);
869 EM_SAFE_FREE(event_data);
876 INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_mailbox_id, int dst_mailbox_id, int* err_code)
878 EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
881 int err = EMAIL_ERROR_NONE;
887 int thread_error = 0;
888 email_account_t *ref_account = NULL;
889 email_mail_list_item_t *mail_list = NULL;
890 email_event_t *event_data = NULL;
891 thread_t move_thread;
892 emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
893 emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
895 if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0) {
896 EM_DEBUG_EXCEPTION("Invalid Parameter");
897 err = EMAIL_ERROR_INVALID_PARAM;
901 if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
902 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
906 if ((err = emstorage_get_mailbox_by_id(multi_user_name, src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
907 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
911 ref_account = emcore_get_account_reference(multi_user_name, dst_mailbox_tbl->account_id, false);
913 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
914 err = EMAIL_ERROR_INVALID_ACCOUNT;
918 if(!emstorage_get_mail_list(multi_user_name, src_mailbox_tbl->account_id, src_mailbox_id, NULL, EMAIL_LIST_TYPE_NORMAL, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, false, &mail_list, &select_num, &err)) {
919 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
923 mails = malloc(sizeof(int) * select_num);
926 EM_DEBUG_EXCEPTION("Malloc failed...!");
927 err = EMAIL_ERROR_OUT_OF_MEMORY;
931 memset(mails, 0x00, sizeof(int) * select_num);
933 for(i = 0 ; i < select_num ; i++) {
934 if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
935 mails[num] = mail_list[i].mail_id;
941 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
942 err = EMAIL_ERROR_MAIL_NOT_FOUND;
946 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
947 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
948 err = EMAIL_ERROR_OUT_OF_MEMORY;
952 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
953 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
954 err = EMAIL_ERROR_OUT_OF_MEMORY;
958 memcpy(p, mails, sizeof(int) * num);
960 event_data->account_id = dst_mailbox_tbl->account_id;
961 event_data->type = EMAIL_EVENT_MOVE_MAIL;
962 event_data->event_param_data_3 = (char*)p;
963 event_data->event_param_data_4 = num;
964 event_data->event_param_data_5 = dst_mailbox_id;
965 event_data->event_param_data_8 = src_mailbox_id;
966 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
968 #ifdef __FEATURE_LOCAL_ACTIVITY__
969 int i = 0, activityid = 0;
971 if (false == emcore_get_next_activity_id(&activityid,&err))
972 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
974 for (i =0; i < event_data.event_param_data_4; i++) {
975 emstorage_activity_tbl_t new_activity;
976 new_activity.activity_id = activityid;
977 new_activity.activity_type = ACTIVITY_MOVEMAIL;
978 new_activity.account_id = event_data.account_id;
979 new_activity.mail_id = mail_ids[i];
980 new_activity.dest_mbox = event_data.event_param_data_1;
981 new_activity.server_mailid = NULL;
982 new_activity.src_mbox = event_data.event_param_data_2;
984 if (!emcore_add_activity(&new_activity, &err))
985 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
987 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
988 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
989 THREAD_DETACH(move_thread); /* free resources used for new thread */
990 EM_DEBUG_LOG("thread_error [%d]", thread_error);
995 #ifdef __FEATURE_LOCAL_ACTIVITY__
996 EM_DEBUG_LOG("Setting g_local_activity_run ");
997 g_local_activity_run = 1;
998 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1004 emcore_free_account(ref_account);
1005 EM_SAFE_FREE(ref_account);
1008 if (dst_mailbox_tbl)
1009 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
1011 if (src_mailbox_tbl)
1012 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
1015 EM_SAFE_FREE(mail_list);
1018 EM_SAFE_FREE(mails);
1020 if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1021 emcore_free_event(event_data);
1022 EM_SAFE_FREE(event_data);
1026 EM_DEBUG_FUNC_END("ret [%d]", ret);
1030 INTERNAL_FUNC int emdaemon_move_mail(char *multi_user_name, int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
1032 EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
1034 /* default variable */
1035 int mail_id = 0, *p = NULL, thread_error = 0;
1036 int ret = false, err = EMAIL_ERROR_NONE;
1037 emstorage_mail_tbl_t* mail_table_data = NULL;
1038 email_account_t* ref_account = NULL;
1039 email_event_t *event_data = NULL;
1040 thread_t move_thread;
1041 emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
1042 int src_mailbox_id = 0;
1044 if (num <= 0 || dst_mailbox_id <= 0) {
1045 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1046 err = EMAIL_ERROR_INVALID_PARAM;
1050 if ((err = emstorage_get_mailbox_by_id(multi_user_name, dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
1051 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1055 ref_account = emcore_get_account_reference(multi_user_name, dest_mailbox_tbl->account_id, false);
1058 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
1059 err = EMAIL_ERROR_INVALID_ACCOUNT;
1063 /* Getting source mailbox name */
1064 mail_id = mail_ids[0];
1066 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
1067 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1071 if (src_mailbox_id == 0)
1072 src_mailbox_id = mail_table_data->mailbox_id;
1074 emstorage_free_mail(&mail_table_data, 1, NULL);
1076 if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
1077 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
1078 err = EMAIL_ERROR_OUT_OF_MEMORY;
1082 if ((p = em_malloc(sizeof(int) * num)) == NULL) {
1083 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
1084 err = EMAIL_ERROR_OUT_OF_MEMORY;
1088 memcpy(p, mail_ids, sizeof(int) * num);
1090 event_data->account_id = dest_mailbox_tbl->account_id;
1091 event_data->type = EMAIL_EVENT_MOVE_MAIL;
1092 event_data->event_param_data_3 = (char*)p;
1093 event_data->event_param_data_4 = num;
1094 event_data->event_param_data_5 = dst_mailbox_id;
1095 event_data->event_param_data_8 = src_mailbox_id;
1096 event_data->event_param_data_6 = noti_param_1;
1097 event_data->event_param_data_7 = noti_param_2;
1098 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1100 #ifdef __FEATURE_LOCAL_ACTIVITY__
1101 int i = 0, activityid = 0;
1103 if (false == emcore_get_next_activity_id(&activityid,&err))
1104 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
1106 for (i =0; i < event_data.event_param_data_4; i++) {
1107 emstorage_activity_tbl_t new_activity;
1108 new_activity.activity_id = activityid;
1109 new_activity.activity_type = ACTIVITY_MOVEMAIL;
1110 new_activity.account_id = event_data.account_id;
1111 new_activity.mail_id = mail_ids[i];
1112 new_activity.dest_mbox = event_data.event_param_data_1;
1113 new_activity.server_mailid = NULL;
1114 new_activity.src_mbox = event_data.event_param_data_2;
1116 if (!emcore_add_activity(&new_activity, &err))
1117 EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
1119 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1120 THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
1121 THREAD_DETACH(move_thread); /* free resources used for new thread */
1122 EM_DEBUG_LOG("thread_error [%d]", thread_error);
1127 #ifdef __FEATURE_LOCAL_ACTIVITY__
1128 EM_DEBUG_LOG("Setting g_local_activity_run ");
1129 g_local_activity_run = 1;
1130 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1136 emcore_free_account(ref_account);
1137 EM_SAFE_FREE(ref_account);
1140 if (dest_mailbox_tbl)
1141 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1143 if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1144 emcore_free_event(event_data);
1145 EM_SAFE_FREE(event_data);
1149 EM_DEBUG_FUNC_END("ret [%d]", ret);
1153 INTERNAL_FUNC int emdaemon_set_flags_field(char *multi_user_name, int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int onserver, int* err_code)
1155 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); /*prevent 27460*/
1157 int ret = false, err = EMAIL_ERROR_NONE;
1158 emstorage_account_tbl_t *account_tbl = NULL;
1159 int *mail_id_array = NULL;
1160 email_event_t *event_data = NULL;
1163 if(account_id <= 0 || !mail_ids || num <= 0) {
1164 err = EMAIL_ERROR_INVALID_PARAM;
1165 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1169 if (!emstorage_get_account_by_id(multi_user_name, account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
1170 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
1174 if (!emcore_set_flags_field(multi_user_name, account_id, mail_ids, num, field_type, value, &err)) {
1175 EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
1179 if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
1180 mail_id_array = em_malloc(sizeof(int) * num);
1182 if (mail_id_array == NULL) {
1183 EM_DEBUG_EXCEPTION("em_malloc failed...");
1184 err = EMAIL_ERROR_OUT_OF_MEMORY;
1188 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1190 event_data = em_malloc(sizeof(email_event_t));
1191 if (event_data == NULL) {
1192 EM_DEBUG_EXCEPTION("em_malloc failed");
1193 err = EMAIL_ERROR_OUT_OF_MEMORY;
1197 event_data->type = EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER;
1198 event_data->account_id = account_id;
1199 event_data->event_param_data_1 = NULL;
1200 event_data->event_param_data_3 = (char*)mail_id_array;
1201 event_data->event_param_data_4 = num;
1202 event_data->event_param_data_5 = field_type;
1203 event_data->event_param_data_6 = value;
1204 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1206 if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
1207 EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1216 if (ret == false && event_data) {
1217 emcore_free_event(event_data);
1218 EM_SAFE_FREE(event_data);
1222 emstorage_free_account(&account_tbl, 1, NULL);
1226 EM_DEBUG_FUNC_END();
1231 INTERNAL_FUNC int emdaemon_update_mail(char *multi_user_name, email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
1233 EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count [%d], input_meeting_req [%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
1234 int err = EMAIL_ERROR_NONE;
1235 /*email_event_t *event_data = NULL;*/
1236 email_account_t *ref_account = NULL;
1238 if (!input_mail_data || input_mail_data->account_id <= 0 || input_mail_data->mail_id == 0 ||
1239 (((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to)) {
1240 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1241 err = EMAIL_ERROR_INVALID_PARAM;
1245 ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
1247 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
1248 err = EMAIL_ERROR_INVALID_ACCOUNT;
1252 if ( (err = emcore_update_mail(multi_user_name, input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
1253 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1257 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1258 /* if ( input_from_eas == 0) {
1259 event_data = em_malloc(sizeof(email_event_t));
1260 if (event_data == NULL) {
1261 EM_DEBUG_EXCEPTION("em_malloc failed");
1262 err = EMAIL_ERROR_OUT_OF_MEMORY;
1266 event_data->type = EMAIL_EVENT_UPDATE_MAIL;
1267 event_data->account_id = input_mail_data->account_id;
1268 event_data->event_param_data_1 = (char*)input_mail_data; // need to be duplicated, it is double freed
1269 event_data->event_param_data_2 = (char*)input_attachment_data_list; // need to be duplicated, it is double freed
1270 event_data->event_param_data_3 = (char*)input_meeting_request; // need to be duplicated, it is double freed
1271 event_data->event_param_data_4 = input_attachment_count;
1272 event_data->event_param_data_5 = input_from_eas;
1273 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1275 if (!emcore_insert_event(event_data, &handle, &err)) {
1276 EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
1277 err = EMAIL_ERROR_NONE;
1287 emcore_free_account(ref_account);
1288 EM_SAFE_FREE(ref_account);
1291 EM_DEBUG_FUNC_END("err [%d]", err);
1296 int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code)
1298 EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1301 int err = EMAIL_ERROR_NONE;
1303 emstorage_mail_tbl_t* mail = NULL;
1305 if (!emstorage_get_mail_field_by_id(multi_user_name, mail_id, RETRIEVE_SUMMARY, &mail, true, &err)) {
1306 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1314 emstorage_free_mail(&mail, 1, NULL);
1316 if (err_code != NULL)
1318 EM_DEBUG_FUNC_END();
1324 char *multi_user_name;
1327 INTERNAL_FUNC int emdaemon_send_mail_retry(char *multi_user_name, int mail_id, int timeout_in_sec, int* err_code)
1330 int err = EMAIL_ERROR_NONE;
1331 long nTimerValue = 0;
1332 email_retry_info *retry_info = NULL;
1334 if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
1335 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1339 if (timeout_in_sec == 0) {
1340 if(!emdaemon_send_mail(multi_user_name, mail_id, NULL, &err)) {
1341 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1345 else if (timeout_in_sec > 0) {
1346 retry_info = em_malloc(sizeof(email_retry_info));
1347 if (retry_info == NULL) {
1348 EM_DEBUG_EXCEPTION("em_malloc failed");
1349 err = EMAIL_ERROR_OUT_OF_MEMORY;
1352 retry_info->mail_id = mail_id;
1353 retry_info->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1354 nTimerValue = timeout_in_sec * 1000;
1356 if (emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)retry_info) <= 0) {
1357 EM_DEBUG_EXCEPTION("Failed to start timer");
1371 INTERNAL_FUNC void _OnMailSendRetryTimerCB(void* data)
1373 EM_DEBUG_FUNC_BEGIN();
1374 int err = EMAIL_ERROR_NONE;
1375 email_retry_info *retry_info = NULL;
1378 EM_DEBUG_LOG("Invalid param");
1382 retry_info = (email_retry_info *)data;
1384 if (!_emdaemon_check_mail_id(retry_info->multi_user_name, retry_info->mail_id, &err)) {
1385 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1389 if(!emdaemon_send_mail(retry_info->multi_user_name, retry_info->mail_id, NULL, &err)) {
1390 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1396 free(retry_info->multi_user_name);
1399 EM_DEBUG_FUNC_END();
1403 INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(char *multi_user_name, int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
1405 EM_DEBUG_FUNC_BEGIN("thread_id [%d], target_mailbox_id [%d], move_always_flag [%d], err_code [%p]", thread_id, target_mailbox_id, move_always_flag, err_code);
1407 int err = EMAIL_ERROR_NONE;
1408 int *mail_id_list = NULL, result_count = 0, i, mailbox_count = 0, account_id;
1409 email_mail_list_item_t *mail_list = NULL;
1410 email_mailbox_t *target_mailbox_list = NULL, *target_mailbox = NULL;
1411 char mailbox_id_param_string[10] = {0,};
1413 if (target_mailbox_id == 0) {
1414 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1415 err = EMAIL_ERROR_INVALID_PARAM;
1419 if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1420 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1425 mail_id_list = em_malloc(sizeof(int) * result_count);
1427 if (mail_id_list == NULL) {
1428 EM_DEBUG_EXCEPTION("em_malloc failed...");
1429 err = EMAIL_ERROR_OUT_OF_MEMORY;
1433 for(i = 0; i < result_count; i++) {
1434 mail_id_list[i] = mail_list[i].mail_id;
1436 account_id = mail_list[0].account_id;
1439 if (!emcore_get_mailbox_list(multi_user_name, account_id, &target_mailbox_list, &mailbox_count, &err)) {
1440 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
1444 for(i = 0; i < mailbox_count; i++) {
1445 EM_DEBUG_LOG_SEC("%s %d", target_mailbox_list[i].mailbox_name, target_mailbox_id);
1446 if(target_mailbox_list[i].mailbox_id == target_mailbox_id) {
1447 target_mailbox = (target_mailbox_list + i);
1452 if(!target_mailbox) {
1453 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1458 if (!emdaemon_move_mail(multi_user_name, mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
1459 EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
1464 SNPRINTF(mailbox_id_param_string, 10, "%d", target_mailbox->mailbox_id);
1465 if (!emcore_notify_storage_event(NOTI_THREAD_MOVE, account_id, thread_id, mailbox_id_param_string, move_always_flag))
1466 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1471 emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1472 EM_SAFE_FREE(mail_list);
1473 EM_SAFE_FREE(mail_id_list);
1477 EM_DEBUG_FUNC_END();
1481 INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_id, int delete_always_flag, int *handle, int *err_code)
1483 EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1485 int err = EMAIL_ERROR_NONE;
1487 int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1488 email_mail_list_item_t *mail_list = NULL;
1490 if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_ID_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1491 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1495 mail_id_list = em_malloc(sizeof(int) * result_count);
1497 if (mail_id_list == NULL) {
1498 EM_DEBUG_EXCEPTION("em_malloc failed...");
1499 err = EMAIL_ERROR_OUT_OF_MEMORY;
1503 for(i = 0; i < result_count; i++) {
1504 mail_id_list[i] = mail_list[i].mail_id;
1507 account_id = mail_list[0].account_id;
1508 mailbox_id = mail_list[0].mailbox_id;
1510 // should remove requiring of mailbox information from this function.
1511 // email-service should find mailboxes itself by its mail id.
1512 if (!emdaemon_delete_mail(multi_user_name, mailbox_id, mail_id_list, result_count, false, handle, &err)) {
1513 EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
1517 if (!emcore_notify_storage_event(NOTI_THREAD_DELETE, account_id, thread_id, NULL, delete_always_flag))
1518 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_THREAD_DELETE] >>>> ");
1523 EM_SAFE_FREE(mail_list);
1524 EM_SAFE_FREE(mail_id_list);
1528 EM_DEBUG_FUNC_END();
1532 INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(char *multi_user_name, int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
1534 EM_DEBUG_FUNC_BEGIN("thread_id [%d], seen_flag [%d], on_server [%d], handle [%p], err_code [%p]", thread_id, seen_flag, on_server, handle, err_code);
1536 int err = EMAIL_ERROR_NONE;
1537 int account_id, *mail_id_list = NULL, result_count = 0, i;
1538 email_mail_list_item_t *mail_list = NULL;
1540 if (!emstorage_get_mail_list(multi_user_name, 0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_ID_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1541 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1545 mail_id_list = em_malloc(sizeof(int) * result_count);
1547 if (mail_id_list == NULL) {
1548 EM_DEBUG_EXCEPTION("em_malloc failed...");
1549 err = EMAIL_ERROR_OUT_OF_MEMORY;
1553 for(i = 0; i < result_count; i++) {
1554 mail_id_list[i] = mail_list[i].mail_id;
1557 account_id = mail_list[0].account_id;
1559 if (!emdaemon_set_flags_field(multi_user_name, account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
1560 EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
1564 if (!emcore_notify_storage_event(NOTI_THREAD_MODIFY_SEEN_FLAG, account_id, thread_id, NULL, seen_flag))
1565 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1570 EM_SAFE_FREE(mail_list);
1571 EM_SAFE_FREE(mail_id_list);
1575 EM_DEBUG_FUNC_END();
1579 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name, int input_mailbox_id, int input_on_server, int *output_handle)
1581 EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
1582 int err = EMAIL_ERROR_NONE;
1584 int event_insert = false;
1585 email_event_t *event_data = NULL;
1586 email_account_t *ref_account = NULL;
1587 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1589 if (input_mailbox_id <= 0) {
1590 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1591 err = EMAIL_ERROR_INVALID_PARAM;
1595 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
1596 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1600 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1602 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
1603 err = EMAIL_ERROR_INVALID_ACCOUNT;
1607 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1608 if(input_on_server) {
1609 event_data = em_malloc(sizeof(email_event_t));
1610 if (event_data == NULL) {
1611 EM_DEBUG_EXCEPTION("em_malloc failed");
1612 err = EMAIL_ERROR_OUT_OF_MEMORY;
1616 event_data->type = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
1617 event_data->account_id = mailbox_tbl->account_id;
1618 event_data->event_param_data_4 = input_mailbox_id;
1619 event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
1621 if (!emcore_insert_event(event_data, &handle, &err)) {
1622 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1625 event_insert = true;
1629 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(multi_user_name, input_mailbox_id)) != EMAIL_ERROR_NONE) {
1630 EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1636 if (event_insert == false && event_data) {
1637 EM_SAFE_FREE(event_data->multi_user_name);
1638 emcore_free_event(event_data);
1639 EM_SAFE_FREE(event_data);
1643 emcore_free_account(ref_account);
1644 EM_SAFE_FREE(ref_account);
1648 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1650 EM_DEBUG_FUNC_END("err [%d]", err);