Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-mail.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23
24 /******************************************************************************
25  * File: email-daemon-mail.c
26  * Desc: email-daemon Mail Operation
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <glib.h>
39 #include <unistd.h>
40 #include <malloc.h>
41 #include <pthread.h>
42
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"
57
58 #ifdef __FEATURE_LOCAL_ACTIVITY__
59 extern int g_local_activity_run;
60 extern int g_save_local_activity_run;
61 #endif
62 static int _emdaemon_check_mail_id(int mail_id, int* err_code);
63
64 INTERNAL_FUNC int emdaemon_send_mail(int mail_id, int *handle, int* err_code)
65 {
66         EM_DEBUG_FUNC_BEGIN("mail_id[%d], handle[%p], err_code[%p]", mail_id, handle, err_code);
67
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 = { 0, };
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;
75
76         if(mail_id <= 0) {
77                 EM_DEBUG_EXCEPTION("mail_id is not valid");
78                 err= EMAIL_ERROR_INVALID_PARAM;
79                 goto FINISH_OFF;
80         }
81
82         if(!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
83                 EM_DEBUG_EXCEPTION("Failed to get mail by mail_id [%d]", err);
84                 goto FINISH_OFF;
85         }
86
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);
90                 if (err_code)
91                         *err_code = EMAIL_ERROR_INVALID_MAILBOX;
92                 if(mail_table_data)
93                         emstorage_free_mail(&mail_table_data, 1, &err);
94                 return false;
95         }
96
97         account_id = mail_table_data->account_id;
98
99         ref_account = emcore_get_account_reference(account_id);
100
101         if (!ref_account) {
102                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
103                 err = EMAIL_ERROR_INVALID_ACCOUNT;
104                 goto FINISH_OFF;
105         }
106
107 #ifdef __FEATURE_MOVE_TO_OUTBOX_FIRST__
108         if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_OUTBOX, &local_mailbox, true, &err))  {
109                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
110                 goto FINISH_OFF;
111         }
112         dst_mailbox_id = local_mailbox->mailbox_id;
113         if ( mail_table_data->mailbox_id != dst_mailbox_id ) {
114                         /*  mail is moved to 'OUTBOX' first of all. */
115                 if (!emcore_move_mail(&mail_id, 1, dst_mailbox_id, EMAIL_MOVED_AFTER_SENDING, 0, &err)) {
116                         EM_DEBUG_EXCEPTION("emcore_mail_move falied [%d]", err);
117                         goto FINISH_OFF;
118                 }
119         }
120 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
121
122         if(!emcore_notify_network_event(NOTI_SEND_START, account_id, NULL, mail_id, 0))
123                 EM_DEBUG_EXCEPTION(" emcore_notify_network_event [ NOTI_SEND_START] Failed >>>> ");
124
125         /* set EMAIL_MAIL_STATUS_SEND_WAIT status */
126
127         if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_WAIT, true, &err)) {
128                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
129
130                 goto FINISH_OFF;
131         }
132
133         event_data.type               = EMAIL_EVENT_SEND_MAIL;
134         event_data.account_id         = account_id;
135         event_data.event_param_data_4 = mail_id;
136         event_data.event_param_data_5 = mail_table_data->mailbox_id;
137
138         if (!emcore_insert_event_for_sending_mails(&event_data, &result_handle, &err)) {
139                 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
140                 goto FINISH_OFF;
141         }
142
143 #ifdef __FEATURE_LOCAL_ACTIVITY__
144                         EM_DEBUG_LOG("Setting g_save_local_activity_run ");
145                         g_save_local_activity_run = 1;
146 #endif
147
148         if ( handle )
149                 *handle = result_handle;
150
151         ret = true;
152
153 FINISH_OFF:
154         if (ret == false) {
155                 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
156
157                 if(!emstorage_set_field_of_mails_with_integer_value(account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SAVED, true, &err))
158                         EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value [%d]",err);
159
160                 EM_SAFE_FREE(event_data.event_param_data_3);
161         }
162
163         if (ref_account) {
164                 emcore_free_account(ref_account);
165                 EM_SAFE_FREE(ref_account);
166         }
167
168         if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
169                 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
170
171         if(local_mailbox)
172                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
173
174         if (err_code != NULL)
175                 *err_code = err;
176
177         if(mail_table_data)
178                 emstorage_free_mail(&mail_table_data, 1, &err);
179
180         EM_DEBUG_FUNC_END();
181         return ret;
182 }
183
184 INTERNAL_FUNC int emdaemon_send_mail_saved(int account_id, int *handle, int* err_code)
185 {
186         EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
187
188         int ret = false;
189         int err = EMAIL_ERROR_NONE;
190         email_event_t event_data = { 0 , };
191         email_account_t* ref_account = NULL;
192         char *mailbox_name = NULL;
193
194         if (account_id <= 0)  {
195                 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
196                 err = EMAIL_ERROR_INVALID_PARAM;
197                 goto FINISH_OFF;
198         }
199
200         ref_account = emcore_get_account_reference(account_id);
201
202         if (!ref_account)  {
203                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
204                 err = EMAIL_ERROR_INVALID_ACCOUNT;
205                 goto FINISH_OFF;
206         }
207
208         if (!emstorage_get_mailbox_name_by_mailbox_type(account_id,EMAIL_MAILBOX_TYPE_OUTBOX,&mailbox_name, false, &err))  {
209                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed [%d]", err);
210                 goto FINISH_OFF;
211         }
212
213         event_data.type               = EMAIL_EVENT_SEND_MAIL_SAVED;
214         event_data.account_id         = account_id;
215         event_data.event_param_data_3 = EM_SAFE_STRDUP(mailbox_name);
216
217         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
218                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
219                 goto FINISH_OFF;
220         }
221
222         ret = true;
223
224 FINISH_OFF:
225         if (ret == false)  {
226                 EM_SAFE_FREE(event_data.event_param_data_3);
227         }
228
229         if (ref_account) {
230                 emcore_free_account(ref_account);
231                 EM_SAFE_FREE(ref_account);
232         }
233
234         EM_SAFE_FREE(mailbox_name);
235
236         if (err_code)
237                 *err_code = err;
238
239         EM_DEBUG_FUNC_END();
240         return ret;
241 }
242
243 INTERNAL_FUNC int emdaemon_add_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
244 {
245         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);
246
247         int         err = EMAIL_ERROR_NONE;
248         int         handle = 0;
249         email_event_t event_data = { 0 };
250         email_account_t* ref_account = NULL;
251
252         if (!input_mail_data || input_mail_data->account_id <= 0 ||
253                 ( ((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to))  {
254                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
255                 err = EMAIL_ERROR_INVALID_PARAM;
256                 goto FINISH_OFF;
257         }
258
259         ref_account = emcore_get_account_reference(input_mail_data->account_id);
260         if (!ref_account)  {
261                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
262                 err = EMAIL_ERROR_INVALID_ACCOUNT;
263                 goto FINISH_OFF;
264         }
265
266         if ((err = emcore_add_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
267                 EM_DEBUG_EXCEPTION("emcore_add_mail failed [%d]", err);
268                 goto FINISH_OFF;
269         }
270
271 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
272         if ( input_from_eas == 0 && ref_account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
273                 event_data.type               = EMAIL_EVENT_SAVE_MAIL;
274                 event_data.account_id         = input_mail_data->account_id;
275                 event_data.event_param_data_4 = input_mail_data->mail_id;
276
277                 if (!emcore_insert_event(&event_data, &handle, &err))  {
278                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
279                         goto FINISH_OFF;
280                 }
281         }
282 #endif
283
284 FINISH_OFF:
285
286         if (ref_account) {
287                 emcore_free_account(ref_account);
288                 EM_SAFE_FREE(ref_account);
289         }
290
291         EM_DEBUG_FUNC_END("err [%d]", err);
292         return err;
293 }
294
295
296 INTERNAL_FUNC int emdaemon_add_meeting_request(int account_id, int input_mailbox_id, email_meeting_request_t *meeting_req, int* err_code)
297 {
298         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);
299
300         /*  default variable */
301         int ret = false;
302         int err = EMAIL_ERROR_NONE;
303
304         if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 )  {
305                 if(meeting_req)
306                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
307
308                 err = EMAIL_ERROR_INVALID_PARAM;
309                 goto FINISH_OFF;
310         }
311
312         if (!emcore_add_meeting_request(account_id, input_mailbox_id, meeting_req, &err))  {
313                 EM_DEBUG_EXCEPTION(" emcore_save_mail_meeting_request failed [%d]", err);
314                 goto FINISH_OFF;
315         }
316
317         ret = true;
318
319 FINISH_OFF:
320         if (err_code)
321                 *err_code = err;
322         EM_DEBUG_FUNC_END();
323         return ret;
324 }
325
326 INTERNAL_FUNC int emdaemon_download_body(int account_id, int mail_id, int verbose, int with_attachment,  int *handle, int* err_code)
327 {
328         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);
329
330         /*  default variable */
331         int ret = false;
332         int err = EMAIL_ERROR_NONE;
333 /*      mpNewMail_StopAlertSound(); */
334
335         if (!_emdaemon_check_mail_id(mail_id, &err))  {
336                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
337                 goto FINISH_OFF;
338         }
339
340         email_event_t event_data;
341
342         memset(&event_data, 0x00, sizeof(email_event_t));
343
344         event_data.type = EMAIL_EVENT_DOWNLOAD_BODY;
345         event_data.event_param_data_4 = mail_id;
346         event_data.account_id = account_id;
347         event_data.event_param_data_3 = GINT_TO_POINTER(verbose << 1 | with_attachment);
348
349         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
350                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
351                 err = EMAIL_ERROR_DB_FAILURE;
352                 goto FINISH_OFF;
353         }
354 #ifdef __FEATURE_LOCAL_ACTIVITY__
355         EM_DEBUG_LOG("Setting g_local_activity_run ");
356         g_local_activity_run = 1;
357 #endif
358         ret = true;
359
360 FINISH_OFF:
361         if (err_code)
362                 *err_code = err;
363         EM_DEBUG_FUNC_END();
364         return ret;
365 }
366
367 int emdaemon_get_attachment(int attachment_id, email_attachment_data_t** attachment, int* err_code)
368 {
369         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
370
371         /*  default variable */
372         int ret = false;
373         int err = EMAIL_ERROR_NONE;
374
375         if (!attachment_id || !attachment)  {
376                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%d], attachment[%p]\n", attachment_id, attachment);
377                 err = EMAIL_ERROR_INVALID_PARAM;
378                 goto FINISH_OFF;
379         }
380
381         if (!emcore_get_attachment_info(attachment_id, attachment, &err) || !attachment)  {
382                 EM_DEBUG_EXCEPTION("emcore_get_attachment_info failed [%d]", err);
383                 goto FINISH_OFF;
384         }
385
386         ret = true;
387
388 FINISH_OFF:
389         if (err_code)
390                 *err_code = err;
391         EM_DEBUG_FUNC_END();
392         return ret;
393 }
394
395 int emdaemon_add_attachment(int mail_id, email_attachment_data_t* attachment, int* err_code)
396 {
397         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
398
399         /*  default variable */
400         int ret = false;
401         int err = EMAIL_ERROR_NONE;
402
403         if (!attachment)  {
404                 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
405                 err = EMAIL_ERROR_INVALID_PARAM;
406                 goto FINISH_OFF;
407         }
408
409         if (!_emdaemon_check_mail_id(mail_id, &err))  {
410                 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
411                 goto FINISH_OFF;
412         }
413
414         if (!emcore_mail_add_attachment(mail_id, attachment, &err))  {
415                 EM_DEBUG_EXCEPTION(" emcore_mail_add_attachment failed [%d]", err);
416                 goto FINISH_OFF;
417         }
418
419         ret = true;
420
421 FINISH_OFF:
422         if (err_code)
423                 *err_code = err;
424         EM_DEBUG_FUNC_END();
425         return ret;
426 }
427
428 int emdaemon_delete_mail_attachment(int attachment_id, int* err_code)
429 {
430         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
431
432         /*  default variable */
433         int ret = false;
434         int err = EMAIL_ERROR_NONE;
435
436         if (!attachment_id)  {
437                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
438                 err = EMAIL_ERROR_INVALID_PARAM;
439                 goto FINISH_OFF;
440         }
441
442         if (!emcore_delete_mail_attachment(attachment_id, &err))  {
443                 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
444                 goto FINISH_OFF;
445         }
446
447         ret = true;
448
449 FINISH_OFF:
450         if (err_code)
451                 *err_code = err;
452         EM_DEBUG_FUNC_END();
453         return ret;
454 }
455
456 INTERNAL_FUNC int emdaemon_download_attachment(int account_id, int mail_id, int nth, int *handle, int* err_code)
457 {
458         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);
459
460         int ret = false;
461         int err = EMAIL_ERROR_NONE;
462
463         if (!nth)  {
464                 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
465                 err = EMAIL_ERROR_INVALID_PARAM;
466                 goto FINISH_OFF;
467         }
468
469         if (!_emdaemon_check_mail_id(mail_id, &err))  {
470                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
471                 goto FINISH_OFF;
472         }
473
474         email_event_t event_data;
475
476         memset(&event_data, 0x00, sizeof(email_event_t));
477
478         event_data.type = EMAIL_EVENT_DOWNLOAD_ATTACHMENT;
479         event_data.account_id         = account_id;
480         event_data.event_param_data_4 = mail_id;
481         event_data.event_param_data_5 = nth;
482
483         if (!emcore_insert_event(&event_data, (int*)handle, &err))  {
484                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
485                 goto FINISH_OFF;
486         }
487
488 #ifdef __FEATURE_LOCAL_ACTIVITY__
489         EM_DEBUG_LOG("Setting g_local_activity_run ");
490         g_local_activity_run = 1;
491 #endif
492
493         ret = true;
494
495 FINISH_OFF:
496         if (err_code)
497                 *err_code = err;
498
499         return ret;
500 }
501
502 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
503 {
504         EM_DEBUG_FUNC_BEGIN();
505         return emcore_free_attachment_data(atch_data, 1, err_code);
506 }
507
508 void* thread_func_to_delete_mail(void *thread_argument)
509 {
510         EM_DEBUG_FUNC_BEGIN();
511         int  err = EMAIL_ERROR_NONE;
512         int *mail_id_list = NULL;
513         int  mail_id_count = 0;
514         int  account_id = 0;
515         int  from_server = 0;
516         int  noti_param_2 = 0;
517         int handle = 0;
518         email_event_t *event_data = (email_event_t*)thread_argument;
519
520         account_id         = event_data->account_id;
521         mail_id_list       = (int*)event_data->event_param_data_3;
522         mail_id_count      = event_data->event_param_data_4;
523         from_server        = event_data->event_param_data_5;
524
525         if (!emcore_delete_mail(account_id, mail_id_list, mail_id_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_COMMAND, noti_param_2, &err)) {
526                 EM_DEBUG_EXCEPTION(" emcore_delete_mail failed [%d]", err);
527                 goto FINISH_OFF;
528         }
529
530         if (from_server == EMAIL_DELETE_LOCAL_AND_SERVER || from_server == EMAIL_DELETE_FROM_SERVER) {
531                 if (!emcore_insert_event(event_data, (int*)handle, &err)) {
532                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
533                         goto FINISH_OFF;
534                 }
535         }
536
537 FINISH_OFF:
538         /* all of members will be freed after using in each event handler */
539
540         if (from_server != EMAIL_DELETE_LOCAL_AND_SERVER && from_server != EMAIL_DELETE_FROM_SERVER) {
541                 EM_SAFE_FREE(event_data->event_param_data_3);
542         }
543         EM_SAFE_FREE(event_data);
544
545         EM_DEBUG_FUNC_END();
546         return SUCCESS;
547 }
548
549 INTERNAL_FUNC int emdaemon_delete_mail(int mailbox_id, int mail_ids[], int mail_ids_count, int from_server, int *handle, int* err_code)
550 {
551         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);
552
553         int ret = false;
554         int err = EMAIL_ERROR_NONE;
555         int *p = NULL;
556         int thread_error = 0;
557         email_account_t *ref_account = NULL;
558         email_event_t *event_data = NULL;
559         emstorage_mailbox_tbl_t *mailbox_tbl_data = NULL;
560         thread_t delete_thread;
561
562         /* mailbox can be NULL for deleting thread mail. */
563         if (mail_ids_count <= 0) {
564                 EM_DEBUG_EXCEPTION("mail_ids_count [%d]", mail_ids_count);
565                 err = EMAIL_ERROR_INVALID_PARAM;
566                 goto FINISH_OFF;
567         }
568
569         if ( (err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_data)) != EMAIL_ERROR_NONE) {
570                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%err]", err);
571                 goto FINISH_OFF;
572         }
573
574         if ((p = em_malloc(sizeof(int) * mail_ids_count)) == NULL) {
575                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
576                 err = EMAIL_ERROR_OUT_OF_MEMORY;
577                 goto FINISH_OFF;
578         }
579
580         memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
581
582         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
583                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
584                 err = EMAIL_ERROR_OUT_OF_MEMORY;
585                 goto FINISH_OFF;
586         }
587
588         ref_account = emcore_get_account_reference(mailbox_tbl_data->account_id);
589
590         if (!ref_account) {
591                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
592                 err = EMAIL_ERROR_INVALID_ACCOUNT;
593                 goto FINISH_OFF;
594         }
595
596         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
597                 from_server = EMAIL_DELETE_LOCALLY;
598         }
599
600         event_data->type                   = EMAIL_EVENT_DELETE_MAIL;
601         event_data->account_id             = mailbox_tbl_data->account_id;
602         event_data->event_param_data_3     = (char*)p;
603         event_data->event_param_data_4     = mail_ids_count;
604         event_data->event_param_data_5     = from_server;
605
606         THREAD_CREATE(delete_thread, thread_func_to_delete_mail, (void*)event_data, thread_error);
607         THREAD_DETACH(delete_thread); /* free resources used for new thread */
608         ret = true;
609
610 FINISH_OFF:
611
612         if (mailbox_tbl_data)
613                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
614
615         if (ref_account) {
616                 emcore_free_account(ref_account);
617                 EM_SAFE_FREE(ref_account);
618         }
619
620         if (thread_error != 0) {
621                 EM_SAFE_FREE(event_data);
622                 EM_SAFE_FREE(p);
623         }
624
625         if (err_code)
626                 *err_code = err;
627
628         return ret;
629 }
630
631 int emdaemon_delete_mail_all(int input_mailbox_id, int input_from_server, int *output_handle, int *output_err_code)
632 {
633         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);
634
635         int                      ret = false;
636         int                      err = EMAIL_ERROR_NONE;
637         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
638         email_event_t              event_data = { 0 };
639
640         if (!input_mailbox_id) {
641                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
642                 err = EMAIL_ERROR_INVALID_PARAM;
643                 goto FINISH_OFF;
644         }
645
646         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl) != EMAIL_ERROR_NONE)) {
647                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
648                 goto FINISH_OFF;
649         }
650
651         if(!emcore_delete_all_mails_of_mailbox(mailbox_tbl->account_id, input_mailbox_id, EMAIL_DELETE_LOCALLY, &err)) {
652                 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
653                 goto FINISH_OFF;
654         }
655
656 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
657         if(input_from_server == EMAIL_DELETE_LOCAL_AND_SERVER || input_from_server == EMAIL_DELETE_FROM_SERVER) {
658                 event_data.type               = EMAIL_EVENT_DELETE_MAIL_ALL;
659                 event_data.account_id         = mailbox_tbl->account_id;
660                 event_data.event_param_data_4 = input_mailbox_id;
661                 event_data.event_param_data_5 = input_from_server;
662
663                 if (!emcore_insert_event(&event_data, (int*)output_handle, &err))  {
664                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
665                         goto FINISH_OFF;
666                 }
667
668 #ifdef __FEATURE_LOCAL_ACTIVITY__
669                 int i, total = 0 , search_handle = 0;
670                 int *mail_ids = NULL;
671                 emstorage_activity_tbl_t new_activity;
672                 int activityid = 0;
673
674                 if (false == emcore_get_next_activity_id(&activityid,&err)) {
675                         EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
676                 }
677
678                 if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->mailbox_name, 0, &search_handle, &total, true, &err)) {
679                         EM_DEBUG_EXCEPTION(" emstorage_mail_search_start failed [%d]", err);
680
681
682                         goto FINISH_OFF;
683                 }
684
685                 mail_ids = em_malloc(sizeof(int) * total);
686                 if (mail_ids == NULL)  {
687                         EM_DEBUG_EXCEPTION(" mailloc failed...");
688
689                         err = EMAIL_ERROR_OUT_OF_MEMORY;
690                         goto FINISH_OFF;
691                 }
692
693                 for (i = 0; i < total; i++)  {
694                         if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err))  {
695                                 EM_DEBUG_EXCEPTION(" emstorage_mail_search_result failed [%d]", err);
696
697
698                                 EM_SAFE_FREE(mail_ids);
699                                 goto FINISH_OFF;
700                         }
701
702                         new_activity.activity_id        = activityid;
703                         new_activity.activity_type      = ACTIVITY_DELETEMAIL;
704                         new_activity.mail_id            = mail_ids[i];
705                         new_activity.server_mailid      = NULL;
706                         new_activity.src_mbox           = mailbox->mailbox_name;
707                         new_activity.dest_mbox          = NULL;
708                         new_activity.account_id         = mailbox->account_id;
709
710                         if (! emcore_add_activity(&new_activity, &err))
711                                 EM_DEBUG_EXCEPTION(" emcore_add_activity  Failed  - %d ", err);
712
713                 }
714
715                 EM_SAFE_FREE(mail_ids);
716
717                 EM_DEBUG_LOG("Setting g_local_activity_run ");
718                 g_local_activity_run = 1;
719 #endif
720         }
721 #endif  /*  __FEATURE_SYNC_CLIENT_TO_SERVER__ */
722
723         ret = true;
724
725 FINISH_OFF:
726
727         if (mailbox_tbl)
728                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
729
730         if (output_err_code)
731                 *output_err_code = err;
732
733         return ret;
734 }
735
736 void* thread_func_to_move_mail(void *thread_argument)
737 {
738         EM_DEBUG_FUNC_BEGIN();
739         int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
740         int handle = 0;
741         email_event_t *event_data = (email_event_t*)thread_argument;
742         int dst_mailbox_id = 0;
743
744 /*      dst_mailbox_name   = (char*)event_data->event_param_data_1; */ /*prevent 33693*/
745         mail_ids           = (int*)event_data->event_param_data_3;
746         mail_ids_count     = event_data->event_param_data_4;
747         dst_mailbox_id     = event_data->event_param_data_5;
748         noti_param_1       = event_data->event_param_data_6;
749         noti_param_2       = event_data->event_param_data_7;
750
751
752         if (!emcore_move_mail(mail_ids, mail_ids_count, dst_mailbox_id, noti_param_1, noti_param_2, &err)) {
753                 EM_DEBUG_EXCEPTION("emcore_mail_move failed [%d]", err);
754                 goto FINISH_OFF;
755         }
756
757         if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
758                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
759                 goto FINISH_OFF;
760         }
761
762 FINISH_OFF:
763         /* emcore_free_event(event_data); */ /* all of members will be freed after using in each event handler */
764         EM_SAFE_FREE(event_data);
765
766         EM_DEBUG_FUNC_END();
767         return SUCCESS;
768 }
769
770 INTERNAL_FUNC int emdaemon_move_mail_all_mails(int src_mailbox_id, int dst_mailbox_id, int* err_code)
771 {
772         EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
773
774         int ret = false;
775         int err = EMAIL_ERROR_NONE;
776         int select_num = 0;
777         int *mails = NULL;
778         int *p = NULL;
779         int i = 0;
780         int num = 0;
781         int thread_error;
782         email_account_t *ref_account = NULL;
783         email_mail_list_item_t *mail_list = NULL;
784         email_event_t *event_data = NULL;
785         thread_t move_thread;
786         emstorage_mailbox_tbl_t *dst_mailbox_tbl = NULL;
787         emstorage_mailbox_tbl_t *src_mailbox_tbl = NULL;
788
789         if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0)  {
790                 EM_DEBUG_EXCEPTION("Invalid Parameter");
791                 err = EMAIL_ERROR_INVALID_PARAM;
792                 goto FINISH_OFF;
793         }
794
795         if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dst_mailbox_tbl)) != EMAIL_ERROR_NONE || !dst_mailbox_tbl) {
796                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
797                 goto FINISH_OFF;
798         }
799
800         if ((err = emstorage_get_mailbox_by_id(src_mailbox_id, &src_mailbox_tbl)) != EMAIL_ERROR_NONE || !src_mailbox_tbl) {
801                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
802                 goto FINISH_OFF;
803         }
804
805         ref_account = emcore_get_account_reference(dst_mailbox_tbl->account_id);
806
807         if (!ref_account)  {
808                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
809                 err = EMAIL_ERROR_INVALID_ACCOUNT;
810                 goto FINISH_OFF;
811         }
812
813         if(!emstorage_get_mail_list(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)) {
814                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed");
815                 goto FINISH_OFF;
816         }
817
818         mails = malloc(sizeof(int) * select_num);
819
820         if( !mails ) {
821                 EM_DEBUG_EXCEPTION("Malloc failed...!");
822                 err = EMAIL_ERROR_OUT_OF_MEMORY;
823                 goto FINISH_OFF;
824         }
825
826         memset(mails, 0x00, sizeof(int) * select_num);
827
828         for(i = 0 ; i < select_num ; i++) {
829                 if( mail_list[i].save_status != EMAIL_MAIL_STATUS_SENDING ) {
830                         mails[num] = mail_list[i].mail_id;
831                         num++;
832                 }
833         }
834
835         if( num <= 0) {
836                 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
837                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
838                 goto FINISH_OFF;
839         }
840
841         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
842                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
843                 err = EMAIL_ERROR_OUT_OF_MEMORY;
844                 goto FINISH_OFF;
845         }
846
847         if ((p = em_malloc(sizeof(int) * num)) == NULL) {
848                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
849                 err = EMAIL_ERROR_OUT_OF_MEMORY;
850                 goto FINISH_OFF;
851         }
852
853         memcpy(p, mails, sizeof(int) * num);
854
855         event_data->account_id         = dst_mailbox_tbl->account_id;
856         event_data->type               = EMAIL_EVENT_MOVE_MAIL;
857         event_data->event_param_data_3 = (char*)p;
858         event_data->event_param_data_4 = num;
859         event_data->event_param_data_5 = dst_mailbox_id;
860         event_data->event_param_data_8 = src_mailbox_id;
861
862 #ifdef __FEATURE_LOCAL_ACTIVITY__
863         int i = 0, activityid = 0;
864
865         if (false == emcore_get_next_activity_id(&activityid,&err))
866                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
867
868         for (i =0; i < event_data.event_param_data_4; i++) {
869                 emstorage_activity_tbl_t        new_activity;
870                 new_activity.activity_id = activityid;
871                 new_activity.activity_type = ACTIVITY_MOVEMAIL;
872                 new_activity.account_id    = event_data.account_id;
873                 new_activity.mail_id       = mail_ids[i];
874                 new_activity.dest_mbox     = event_data.event_param_data_1;
875                 new_activity.server_mailid = NULL;
876                 new_activity.src_mbox      = event_data.event_param_data_2;
877
878                 if (!emcore_add_activity(&new_activity, &err))
879                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
880         }
881 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
882         THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
883         THREAD_DETACH(move_thread); /* free resources used for new thread */
884         ret = true;
885
886 FINISH_OFF:
887
888 #ifdef __FEATURE_LOCAL_ACTIVITY__
889         EM_DEBUG_LOG("Setting g_local_activity_run ");
890         g_local_activity_run = 1;
891 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
892
893         if (err_code)
894                 *err_code = err;
895
896         if (ref_account) {
897                 emcore_free_account(ref_account);
898                 EM_SAFE_FREE(ref_account);
899         }
900
901         if (dst_mailbox_tbl)
902                 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
903
904         if (src_mailbox_tbl)
905                 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
906
907         if(mail_list)
908                 EM_SAFE_FREE(mail_list);
909
910         if(mails != NULL )
911                 EM_SAFE_FREE(mails);
912
913
914         EM_DEBUG_FUNC_END("ret [%d]", ret);
915         return ret;
916 }
917
918 INTERNAL_FUNC int emdaemon_move_mail(int mail_ids[], int num, int dst_mailbox_id, int noti_param_1, int noti_param_2, int* err_code)
919 {
920         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], num[%d], dst_mailbox_id[%d], err_code[%p]", mail_ids, num, dst_mailbox_id, err_code);
921
922         /*  default variable */
923         int mail_id = 0, *p = NULL, thread_error;
924         int ret = false, err = EMAIL_ERROR_NONE;
925         emstorage_mail_tbl_t* mail_table_data = NULL;
926         email_account_t* ref_account = NULL;
927         email_event_t *event_data = NULL;
928         thread_t move_thread;
929         emstorage_mailbox_tbl_t *dest_mailbox_tbl = NULL;
930         int src_mailbox_id = 0;
931
932         if (num <= 0 || dst_mailbox_id <= 0) {
933                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
934                 err = EMAIL_ERROR_INVALID_PARAM;
935                 goto FINISH_OFF;
936         }
937
938         if ((err = emstorage_get_mailbox_by_id(dst_mailbox_id, &dest_mailbox_tbl)) != EMAIL_ERROR_NONE || !dest_mailbox_tbl) {
939                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
940                 goto FINISH_OFF;
941         }
942
943         ref_account = emcore_get_account_reference(dest_mailbox_tbl->account_id);
944
945         if (!ref_account) {
946                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
947                 err = EMAIL_ERROR_INVALID_ACCOUNT;
948                 goto FINISH_OFF;
949         }
950
951         /* Getting source mailbox name */
952         mail_id = mail_ids[0];
953
954         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data) {
955                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
956
957                 goto FINISH_OFF;
958         }
959
960         if (src_mailbox_id == 0)
961                 src_mailbox_id = mail_table_data->mailbox_id;
962
963         emstorage_free_mail(&mail_table_data, 1, NULL);
964
965         if ((event_data = em_malloc(sizeof(email_event_t)) ) == NULL) {
966                 EM_DEBUG_EXCEPTION("em_malloc for event_data failed...");
967                 err = EMAIL_ERROR_OUT_OF_MEMORY;
968                 goto FINISH_OFF;
969         }
970
971         if ((p = em_malloc(sizeof(int) * num)) == NULL) {
972                 EM_DEBUG_EXCEPTION("em_malloc for p failed...");
973                 err = EMAIL_ERROR_OUT_OF_MEMORY;
974                 goto FINISH_OFF;
975         }
976
977         memcpy(p, mail_ids, sizeof(int) * num);
978
979         event_data->account_id        = dest_mailbox_tbl->account_id;
980         event_data->type               = EMAIL_EVENT_MOVE_MAIL;
981         event_data->event_param_data_3 = (char*)p;
982         event_data->event_param_data_4 = num;
983         event_data->event_param_data_5 = dst_mailbox_id;
984         event_data->event_param_data_8 = src_mailbox_id;
985         event_data->event_param_data_6 = noti_param_1;
986         event_data->event_param_data_7 = noti_param_2;
987
988 #ifdef __FEATURE_LOCAL_ACTIVITY__
989         int i = 0, activityid = 0;
990
991         if (false == emcore_get_next_activity_id(&activityid,&err))
992                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
993
994         for (i =0; i < event_data.event_param_data_4; i++) {
995                 emstorage_activity_tbl_t        new_activity;
996                 new_activity.activity_id = activityid;
997                 new_activity.activity_type = ACTIVITY_MOVEMAIL;
998                 new_activity.account_id    = event_data.account_id;
999                 new_activity.mail_id       = mail_ids[i];
1000                 new_activity.dest_mbox     = event_data.event_param_data_1;
1001                 new_activity.server_mailid = NULL;
1002                 new_activity.src_mbox      = event_data.event_param_data_2;
1003
1004                 if (!emcore_add_activity(&new_activity, &err))
1005                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
1006         }
1007 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1008         THREAD_CREATE(move_thread, thread_func_to_move_mail, (void*)event_data, thread_error);
1009         THREAD_DETACH(move_thread); /* free resources used for new thread */
1010         ret = true;
1011
1012 FINISH_OFF:
1013
1014 #ifdef __FEATURE_LOCAL_ACTIVITY__
1015         EM_DEBUG_LOG("Setting g_local_activity_run ");
1016         g_local_activity_run = 1;
1017 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
1018
1019         if (err_code)
1020                 *err_code = err;
1021
1022         if (ref_account) {
1023                 emcore_free_account(ref_account);
1024                 EM_SAFE_FREE(ref_account);
1025         }
1026
1027         if (dest_mailbox_tbl)
1028                 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1029
1030         EM_DEBUG_FUNC_END("ret [%d]", ret);
1031         return ret;
1032 }
1033
1034 INTERNAL_FUNC int emdaemon_set_flags_field(int account_id, int mail_ids[], int num, email_flags_field_type field_type, int value, int onserver, int* err_code)
1035 {
1036         EM_DEBUG_FUNC_BEGIN("mail_ids[%d], num[%d], field_type [%d], value[%d], err_code[%p]", mail_ids[0], num, field_type, value, err_code);
1037
1038         int ret = false, err = EMAIL_ERROR_NONE;
1039         emstorage_account_tbl_t *account_tbl = NULL;
1040
1041         if(account_id <= 0 || !mail_ids || num <= 0) {
1042                 err = EMAIL_ERROR_INVALID_PARAM;
1043                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1044                 goto FINISH_OFF;
1045         }
1046
1047         if (!emstorage_get_account_by_id(account_id, EMAIL_ACC_GET_OPT_DEFAULT, &account_tbl, false, &err)) {
1048                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id falled [%d]", err);
1049                 goto FINISH_OFF;
1050         }
1051
1052         if (!emcore_set_flags_field(account_id, mail_ids, num, field_type, value, &err))  {
1053                 EM_DEBUG_EXCEPTION("emcore_set_flags_field falled [%d]", err);
1054                 goto FINISH_OFF;
1055         }
1056
1057         if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 )  {
1058                 int *mail_id_array = NULL;
1059                 email_event_t event_data = {0};
1060                 int handle = 0;
1061
1062                 mail_id_array = em_malloc(sizeof(int) * num);
1063
1064                 if (mail_id_array == NULL)  {
1065                         EM_DEBUG_EXCEPTION("em_malloc failed...");
1066                err = EMAIL_ERROR_OUT_OF_MEMORY;
1067                goto FINISH_OFF;
1068                 }
1069
1070                 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1071
1072                 event_data.type               = EMAIL_EVENT_SYNC_FLAGS_FIELD_TO_SERVER;
1073                 event_data.account_id         = account_id;
1074                 event_data.event_param_data_1 = NULL;
1075                 event_data.event_param_data_3 = (char*)mail_id_array;
1076                 event_data.event_param_data_4 = num;
1077                 event_data.event_param_data_5 = field_type;
1078                 event_data.event_param_data_6 = value;
1079
1080                 if (!emcore_insert_event(&event_data, (int*)&handle, &err))  {
1081                         EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1082                         goto FINISH_OFF;
1083                 }
1084         }
1085
1086         ret = true;
1087
1088 FINISH_OFF:
1089
1090         if (account_tbl)
1091                 emstorage_free_account(&account_tbl, 1, NULL);
1092
1093         if (err_code)
1094                 *err_code = err;
1095         EM_DEBUG_FUNC_END();
1096         return ret;
1097 }
1098
1099
1100 INTERNAL_FUNC int emdaemon_update_mail(email_mail_data_t *input_mail_data, email_attachment_data_t *input_attachment_data_list, int input_attachment_count, email_meeting_request_t *input_meeting_request, int input_from_eas)
1101 {
1102         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);
1103
1104         int            err = EMAIL_ERROR_NONE;
1105         int            handle = 0;
1106         email_event_t    event_data = { 0 };
1107         email_account_t *ref_account = NULL;
1108
1109         if (!input_mail_data || input_mail_data->account_id <= 0 || input_mail_data->mail_id == 0 ||
1110                 (((input_mail_data->report_status & EMAIL_MAIL_REPORT_MDN) != 0) && !input_mail_data->full_address_to))  {
1111                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1112                 err = EMAIL_ERROR_INVALID_PARAM;
1113                 goto FINISH_OFF;
1114         }
1115
1116         ref_account = emcore_get_account_reference(input_mail_data->account_id);
1117         if (!ref_account)  {
1118                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
1119                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1120                 goto FINISH_OFF;
1121         }
1122
1123         if ( (err = emcore_update_mail(input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas)) != EMAIL_ERROR_NONE) {
1124                 EM_DEBUG_EXCEPTION("emcore_update_mail failed [%d]", err);
1125                 goto FINISH_OFF;
1126         }
1127
1128 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1129         if ( input_from_eas == 0) {
1130                 event_data.type               = EMAIL_EVENT_UPDATE_MAIL;
1131                 event_data.account_id         = input_mail_data->account_id;
1132                 event_data.event_param_data_1 = (char*)input_mail_data;
1133                 event_data.event_param_data_2 = (char*)input_attachment_data_list;
1134                 event_data.event_param_data_3 = (char*)input_meeting_request;
1135                 event_data.event_param_data_4 = input_attachment_count;
1136                 event_data.event_param_data_5 = input_from_eas;
1137
1138                 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err))  {
1139                         EM_DEBUG_EXCEPTION("emcore_insert_event_for_sending_mails failed [%d]", err);
1140                         err = EMAIL_ERROR_NONE;
1141                         goto FINISH_OFF;
1142                 }
1143         }
1144 #endif
1145
1146 FINISH_OFF:
1147
1148         if (ref_account) {
1149                 emcore_free_account(ref_account);
1150                 EM_SAFE_FREE(ref_account);
1151         }
1152
1153         EM_DEBUG_FUNC_END("err [%d]", err);
1154         return err;
1155 }
1156
1157
1158 int _emdaemon_check_mail_id(int mail_id, int* err_code)
1159 {
1160         EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1161
1162         int ret = false;
1163         int err = EMAIL_ERROR_NONE;
1164
1165         emstorage_mail_tbl_t* mail = NULL;
1166
1167         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail, true, &err))  {
1168                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
1169                 goto FINISH_OFF;
1170         }
1171
1172         ret = true;
1173
1174 FINISH_OFF:
1175         if (mail != NULL)
1176                 emstorage_free_mail(&mail, 1, NULL);
1177
1178         if (err_code != NULL)
1179                 *err_code = err;
1180         EM_DEBUG_FUNC_END();
1181         return ret;
1182 }
1183
1184
1185 INTERNAL_FUNC int emdaemon_send_mail_retry(int mail_id,  int timeout_in_sec, int* err_code)
1186 {
1187         int ret = false;
1188         int err = EMAIL_ERROR_NONE;
1189         long nTimerValue = 0;
1190         char mail_id_string[10] = { 0, };
1191
1192         if (!_emdaemon_check_mail_id(mail_id, &err))  {
1193                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1194                 goto FINISH_OFF;
1195         }
1196
1197         if ( timeout_in_sec == 0 ) {
1198                 if(!emdaemon_send_mail(mail_id, NULL, &err)) {
1199                         EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1200                         goto FINISH_OFF;
1201                 }
1202         }
1203         else if ( timeout_in_sec > 0 ) {
1204                 sprintf(mail_id_string,"%d",mail_id);
1205                 nTimerValue = timeout_in_sec * 1000;
1206                 if ( emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)mail_id_string) <= 0) {
1207                         EM_DEBUG_EXCEPTION("Failed to start timer");
1208                         goto FINISH_OFF;
1209                 }
1210         }
1211         ret = true;
1212
1213 FINISH_OFF:
1214         if (err_code)
1215                 *err_code = err;
1216
1217         return ret;
1218 }
1219
1220
1221 INTERNAL_FUNC void _OnMailSendRetryTimerCB( void* data )
1222 {
1223         EM_DEBUG_FUNC_BEGIN();
1224         int err = EMAIL_ERROR_NONE;
1225         int mail_id = 0 ;
1226
1227         if( !data ) {
1228                 EM_DEBUG_LOG("Invalid param");
1229                 goto FINISH_OFF;
1230         }
1231
1232         mail_id = atoi((char*)data);
1233
1234         if (!_emdaemon_check_mail_id(mail_id, &err))  {
1235                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1236                 goto FINISH_OFF;
1237         }
1238
1239         if(!emdaemon_send_mail(mail_id, NULL, &err)) {
1240                 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
1241                 goto FINISH_OFF;
1242         }
1243
1244 FINISH_OFF:
1245
1246         EM_DEBUG_FUNC_END();
1247         return;
1248 }
1249
1250 INTERNAL_FUNC int emdaemon_move_mail_thread_to_mailbox(int thread_id, int target_mailbox_id, int move_always_flag, int *err_code)
1251 {
1252         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);
1253         int ret = false;
1254         int err = EMAIL_ERROR_NONE;
1255         int *mail_id_list = NULL, result_count = 0, i, mailbox_count = 0, account_id;
1256         email_mail_list_item_t *mail_list = NULL;
1257         email_mailbox_t *target_mailbox_list = NULL, *target_mailbox = NULL;
1258         char mailbox_id_param_string[10] = {0,};
1259
1260         if (target_mailbox_id == 0) {
1261                 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1262                 err = EMAIL_ERROR_INVALID_PARAM;
1263                 goto FINISH_OFF;
1264         }
1265
1266         if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_DATETIME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1267                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1268
1269                 goto FINISH_OFF;
1270         }
1271
1272         mail_id_list = em_malloc(sizeof(int) * result_count);
1273
1274         if (mail_id_list == NULL) {
1275                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1276                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1277                 goto FINISH_OFF;
1278         }
1279
1280         for(i = 0; i < result_count; i++) {
1281                 mail_id_list[i] = mail_list[i].mail_id;
1282         }
1283         account_id = mail_list[0].account_id;
1284
1285
1286         if (!emcore_get_mailbox_list(account_id, &target_mailbox_list, &mailbox_count, &err)) {
1287                 EM_DEBUG_EXCEPTION("emcore_get_mailbox_list failed [%d]", err);
1288                 goto FINISH_OFF;
1289         }
1290
1291         for(i = 0; i < mailbox_count; i++) {
1292                 EM_DEBUG_LOG("%s %d", target_mailbox_list[i].mailbox_name, target_mailbox_id);
1293                 if(target_mailbox_list[i].mailbox_id == target_mailbox_id) {
1294                         target_mailbox = (target_mailbox_list + i);
1295                         break;
1296                 }
1297         }
1298
1299         if(!target_mailbox) {
1300                 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1301                 goto FINISH_OFF;
1302         }
1303
1304         if (!emdaemon_move_mail(mail_id_list, result_count, target_mailbox->mailbox_id, EMAIL_MOVED_BY_MOVING_THREAD, move_always_flag, &err)) {
1305                 EM_DEBUG_EXCEPTION("emdaemon_move_mail failed [%d]", err);
1306                 goto FINISH_OFF;
1307         }
1308
1309         SNPRINTF(mailbox_id_param_string, 10, "%d", target_mailbox->mailbox_id);
1310         if (!emcore_notify_storage_event(NOTI_THREAD_MOVE, account_id, thread_id, mailbox_id_param_string, move_always_flag))
1311                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1312
1313         ret = true;
1314
1315 FINISH_OFF:
1316         emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1317         EM_SAFE_FREE(mail_list);
1318         EM_SAFE_FREE(mail_id_list);
1319
1320         if (err_code)
1321                 *err_code = err;
1322         EM_DEBUG_FUNC_END();
1323         return ret;
1324 }
1325
1326 INTERNAL_FUNC int emdaemon_delete_mail_thread(int thread_id, int delete_always_flag, int *handle, int *err_code)
1327 {
1328         EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1329         int ret = false;
1330         int err = EMAIL_ERROR_NONE;
1331         int account_id = 0;
1332         int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1333         email_mail_list_item_t *mail_list = NULL;
1334
1335         if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_NAME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1336                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1337
1338                 goto FINISH_OFF;
1339         }
1340
1341         mail_id_list = em_malloc(sizeof(int) * result_count);
1342
1343         if (mail_id_list == NULL) {
1344                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1345                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1346                 goto FINISH_OFF;
1347         }
1348
1349         for(i = 0; i < result_count; i++) {
1350                 mail_id_list[i] = mail_list[i].mail_id;
1351         }
1352
1353         account_id = mail_list[0].account_id;
1354         mailbox_id = mail_list[0].mailbox_id;
1355
1356         // should remove requiring of mailbox information from this function.
1357         // email-service should find mailboxes itself by its mail id.
1358         if (!emdaemon_delete_mail(mailbox_id, mail_id_list, result_count, false, handle, &err)) {
1359                 EM_DEBUG_EXCEPTION("emdaemon_delete_mail failed [%d]", err);
1360                 goto FINISH_OFF;
1361         }
1362
1363         if (!emcore_notify_storage_event(NOTI_THREAD_DELETE, account_id, thread_id, NULL, delete_always_flag))
1364                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_THREAD_DELETE] >>>> ");
1365
1366         ret = true;
1367
1368 FINISH_OFF:
1369         EM_SAFE_FREE(mail_list);
1370         EM_SAFE_FREE(mail_id_list);
1371
1372         if (err_code)
1373                 *err_code = err;
1374         EM_DEBUG_FUNC_END();
1375         return ret;
1376 }
1377
1378 INTERNAL_FUNC int emdaemon_modify_seen_flag_of_thread(int thread_id, int seen_flag, int on_server, int *handle, int *err_code)
1379 {
1380         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);
1381         int ret = false;
1382         int err = EMAIL_ERROR_NONE;
1383         int account_id, *mail_id_list = NULL, result_count = 0, i;
1384         email_mail_list_item_t *mail_list = NULL;
1385
1386         if (!emstorage_get_mail_list(0, 0, NULL, thread_id, -1, -1, 0, NULL, EMAIL_SORT_MAILBOX_NAME_HIGH, true, &mail_list, &result_count, &err) || !mail_list || !result_count) {
1387                 EM_DEBUG_EXCEPTION("emstorage_get_mail_list failed [%d]", err);
1388
1389                 goto FINISH_OFF;
1390         }
1391
1392         mail_id_list = em_malloc(sizeof(int) * result_count);
1393
1394         if (mail_id_list == NULL) {
1395                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1396                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1397                 goto FINISH_OFF;
1398         }
1399
1400         for(i = 0; i < result_count; i++) {
1401                 mail_id_list[i] = mail_list[i].mail_id;
1402         }
1403
1404         account_id = mail_list[0].account_id;
1405
1406         if (!emdaemon_set_flags_field(account_id, mail_id_list, result_count, EMAIL_FLAGS_SEEN_FIELD, seen_flag, on_server, &err)) {
1407                 EM_DEBUG_EXCEPTION("emdaemon_set_flags_field failed [%d]", err);
1408                 goto FINISH_OFF;
1409         }
1410
1411         if (!emcore_notify_storage_event(NOTI_THREAD_MODIFY_SEEN_FLAG, account_id, thread_id, NULL, seen_flag))
1412                 EM_DEBUG_EXCEPTION(" emcore_notify_storage_event Failed [NOTI_MAIL_MOVE] >>>> ");
1413
1414         ret = true;
1415
1416 FINISH_OFF:
1417         EM_SAFE_FREE(mail_list);
1418         EM_SAFE_FREE(mail_id_list);
1419
1420         if (err_code)
1421                 *err_code = err;
1422         EM_DEBUG_FUNC_END();
1423         return ret;
1424 }
1425
1426 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(int input_mailbox_id, int input_on_server, int *output_handle)
1427 {
1428         EM_DEBUG_FUNC_BEGIN("input_mailbox_id [%d], input_on_server [%d], output_handle [%p]", input_mailbox_id, input_on_server, output_handle);
1429         int                      err = EMAIL_ERROR_NONE;
1430         int                      handle = 0;
1431         email_event_t              event_data = { 0 };
1432         email_account_t           *ref_account = NULL;
1433         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1434
1435         if (input_mailbox_id <= 0)  {
1436                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1437                 err = EMAIL_ERROR_INVALID_PARAM;
1438                 goto FINISH_OFF;
1439         }
1440
1441         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE) {
1442                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
1443                 goto FINISH_OFF;
1444         }
1445
1446         ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
1447         if (!ref_account)  {
1448                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
1449                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1450                 goto FINISH_OFF;
1451         }
1452
1453 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
1454         if(input_on_server) {
1455                 event_data.type               = EMAIL_EVENT_EXPUNGE_MAILS_DELETED_FLAGGED;
1456                 event_data.account_id         = mailbox_tbl->account_id;
1457                 event_data.event_param_data_4 = input_mailbox_id;
1458
1459                 if (!emcore_insert_event(&event_data, &handle, &err))  {
1460                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1461                         goto FINISH_OFF;
1462                 }
1463         }
1464         else
1465 #endif
1466                 if ( (err = emcore_expunge_mails_deleted_flagged_from_local_storage(input_mailbox_id)) != EMAIL_ERROR_NONE) {
1467                         EM_DEBUG_EXCEPTION("emcore_expunge_mails_deleted_flagged_from_local_storage failed [%d]", err);
1468                         goto FINISH_OFF;
1469                 }
1470
1471 FINISH_OFF:
1472
1473         if (ref_account) {
1474                 emcore_free_account(ref_account);
1475                 EM_SAFE_FREE(ref_account);
1476         }
1477
1478         if(mailbox_tbl)
1479                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1480
1481         EM_DEBUG_FUNC_END("err [%d]", err);
1482         return err;
1483 }