Fixed the bug for tizen 3.0
[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(char *multi_user_name, int mail_id, int* err_code);
63
64 INTERNAL_FUNC int emdaemon_send_mail(char *multi_user_name, 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 = 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;
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(multi_user_name, 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(multi_user_name, account_id, false);
100         if (!ref_account) {
101                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
102                 err = EMAIL_ERROR_INVALID_ACCOUNT;
103                 goto FINISH_OFF;
104         }
105
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);
109                 goto FINISH_OFF;
110         }
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);
116                         goto FINISH_OFF;
117                 }
118         }
119 #endif /* __FEATURE_MOVE_TO_OUTBOX_FIRST__ */
120
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 >>>> ");
123
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);
127                 goto FINISH_OFF;
128         }
129
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;
134                 goto FINISH_OFF;
135         }
136
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);
142
143         if (!emcore_insert_event_for_sending_mails(event_data, &result_handle, &err)) {
144                 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
145                 goto FINISH_OFF;
146         }
147
148 #ifdef __FEATURE_LOCAL_ACTIVITY__
149                         EM_DEBUG_LOG("Setting g_save_local_activity_run ");
150                         g_save_local_activity_run = 1;
151 #endif
152
153         if ( handle )
154                 *handle = result_handle;
155
156         ret = true;
157
158 FINISH_OFF:
159         if (ret == false) {
160                 EM_DEBUG_EXCEPTION("emdaemon_send_mail failed [%d]", err);
161
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);
164
165                 if (event_data) {
166                         emcore_free_event(event_data);
167                         EM_SAFE_FREE(event_data);
168                 }
169         }
170
171         if (ref_account) {
172                 emcore_free_account(ref_account);
173                 EM_SAFE_FREE(ref_account);
174         }
175
176         if(!emcore_add_transaction_info(mail_id , result_handle , &err_2))
177                 EM_DEBUG_EXCEPTION("emcore_add_transaction_info failed [%d]", err_2);
178
179         if(local_mailbox)
180                 emstorage_free_mailbox(&local_mailbox, 1, NULL);
181
182         if (err_code != NULL)
183                 *err_code = err;
184
185         if(mail_table_data)
186                 emstorage_free_mail(&mail_table_data, 1, &err);
187
188         EM_DEBUG_FUNC_END();
189         return ret;
190 }
191
192 INTERNAL_FUNC int emdaemon_send_mail_saved(char *multi_user_name, int account_id, int *handle, int* err_code)
193 {
194         EM_DEBUG_FUNC_BEGIN("account_id[%d],handle[%p], err_code[%p]", account_id, handle, err_code);
195
196         int ret = false;
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;
201
202         if (account_id <= 0) {
203                 EM_DEBUG_EXCEPTION("account_id = %d", account_id);
204                 err = EMAIL_ERROR_INVALID_PARAM;
205                 goto FINISH_OFF;
206         }
207
208         ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
209         if (!ref_account) {
210                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
211                 err = EMAIL_ERROR_INVALID_ACCOUNT;
212                 goto FINISH_OFF;
213         }
214
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);
217                 goto FINISH_OFF;
218         }
219
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;
224                 goto FINISH_OFF;
225         }
226
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);
231
232         if (!emcore_insert_event_for_sending_mails(event_data, (int *)handle, &err)) {
233                 EM_DEBUG_EXCEPTION(" emcore_insert_event failed [%d]", err);
234                 goto FINISH_OFF;
235         }
236
237         ret = true;
238
239 FINISH_OFF:
240
241         if (ret == false && event_data) {
242                 emcore_free_event(event_data);
243                 EM_SAFE_FREE(event_data);
244         }
245
246         if (ref_account) {
247                 emcore_free_account(ref_account);
248                 EM_SAFE_FREE(ref_account);
249         }
250
251         EM_SAFE_FREE(mailbox_name);
252
253         if (err_code)
254                 *err_code = err;
255
256         EM_DEBUG_FUNC_END();
257         return ret;
258 }
259
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, 
265                                         int input_from_eas)
266 {
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);
268
269         int ret = false;
270         int err = EMAIL_ERROR_NONE;
271         int handle = 0;
272         email_event_t *event_data = NULL;
273         email_account_t *ref_account = NULL;
274
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;
279                 goto FINISH_OFF;
280         }
281
282         ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
283         if (!ref_account)  {
284                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
285                 err = EMAIL_ERROR_INVALID_ACCOUNT;
286                 goto FINISH_OFF;
287         }
288
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);
291                 goto FINISH_OFF;
292         }
293
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;
300                         goto FINISH_OFF;
301                 }
302
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);
307
308                 if (!emcore_insert_event(event_data, &handle, &err)) {
309                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
310                         goto FINISH_OFF;
311                 }
312         }
313 #endif
314
315         ret = true;
316
317 FINISH_OFF:
318
319         if (ret == false && event_data) {
320                 emcore_free_event(event_data);
321                 EM_SAFE_FREE(event_data);
322         }
323
324         if (ref_account) {
325                 emcore_free_account(ref_account);
326                 EM_SAFE_FREE(ref_account);
327         }
328
329         EM_DEBUG_FUNC_END("err [%d]", err);
330
331         return err;
332 }
333
334
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)
336 {
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);
338
339         /*  default variable */
340         int ret = false;
341         int err = EMAIL_ERROR_NONE;
342
343         if ( account_id <= 0 || !meeting_req || meeting_req->mail_id <= 0 )  {
344                 if(meeting_req)
345                         EM_DEBUG_EXCEPTION("mail_id[%d]", meeting_req->mail_id);
346
347                 err = EMAIL_ERROR_INVALID_PARAM;
348                 goto FINISH_OFF;
349         }
350
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);
353                 goto FINISH_OFF;
354         }
355
356         ret = true;
357
358 FINISH_OFF:
359         if (err_code)
360                 *err_code = err;
361         EM_DEBUG_FUNC_END();
362         return ret;
363 }
364
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)
366 {
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);
368
369         int ret = false;
370         int err = EMAIL_ERROR_NONE;
371         email_event_t *event_data = NULL;
372
373         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
374                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
375                 goto FINISH_OFF;
376         }
377
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;
382                 goto FINISH_OFF;
383         }
384
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);
390
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;
394                 goto FINISH_OFF;
395         }
396
397 #ifdef __FEATURE_LOCAL_ACTIVITY__
398         EM_DEBUG_LOG("Setting g_local_activity_run ");
399         g_local_activity_run = 1;
400 #endif
401         ret = true;
402
403 FINISH_OFF:
404
405         if (ret == false && event_data) {
406                 emcore_free_event(event_data);
407                 EM_SAFE_FREE(event_data);
408         }
409
410         if (err_code)
411                 *err_code = err;
412
413         EM_DEBUG_FUNC_END();
414
415         return ret;
416 }
417
418 int emdaemon_get_attachment(char *multi_user_name, int attachment_id, email_attachment_data_t** attachment, int* err_code)
419 {
420         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], attachment[%p], err_code[%p]", attachment_id, attachment, err_code);
421
422         /*  default variable */
423         int ret = false;
424         int err = EMAIL_ERROR_NONE;
425
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;
429                 goto FINISH_OFF;
430         }
431
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);
434                 goto FINISH_OFF;
435         }
436
437         ret = true;
438
439 FINISH_OFF:
440
441         if (err_code)
442                 *err_code = err;
443
444         EM_DEBUG_FUNC_END();
445
446         return ret;
447 }
448
449 int emdaemon_add_attachment(char *multi_user_name, int mail_id, email_attachment_data_t* attachment, int* err_code)
450 {
451         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
452
453         /*  default variable */
454         int ret = false;
455         int err = EMAIL_ERROR_NONE;
456
457         if (!attachment)  {
458                 EM_DEBUG_EXCEPTION(" mailbox[%p], mail_id[%d], attachment[%p]", mail_id, attachment);
459                 err = EMAIL_ERROR_INVALID_PARAM;
460                 goto FINISH_OFF;
461         }
462
463         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
464                 EM_DEBUG_EXCEPTION(" _emdaemon_check_mail_id failed [%d]", err);
465                 goto FINISH_OFF;
466         }
467
468         if (!emcore_add_attachment(multi_user_name, mail_id, attachment, &err))  {
469                 EM_DEBUG_EXCEPTION(" emcore_add_attachment failed [%d]", err);
470                 goto FINISH_OFF;
471         }
472
473         ret = true;
474
475 FINISH_OFF:
476
477         if (err_code)
478                 *err_code = err;
479
480         EM_DEBUG_FUNC_END();
481
482         return ret;
483 }
484
485 int emdaemon_delete_mail_attachment(char *multi_user_name, int attachment_id, int* err_code)
486 {
487         EM_DEBUG_FUNC_BEGIN("attachment_id[%d], err_code[%p]", attachment_id, err_code);
488
489         /*  default variable */
490         int ret = false;
491         int err = EMAIL_ERROR_NONE;
492
493         if (!attachment_id)  {
494                 EM_DEBUG_EXCEPTION("attachment_id[%d]", attachment_id);
495                 err = EMAIL_ERROR_INVALID_PARAM;
496                 goto FINISH_OFF;
497         }
498
499         if (!emcore_delete_mail_attachment(multi_user_name, attachment_id, &err))  {
500                 EM_DEBUG_EXCEPTION(" emcore_delete_mail_attachment failed [%d]", err);
501                 goto FINISH_OFF;
502         }
503
504         ret = true;
505
506 FINISH_OFF:
507
508         if (err_code)
509                 *err_code = err;
510
511         EM_DEBUG_FUNC_END();
512
513         return ret;
514 }
515
516 INTERNAL_FUNC int emdaemon_download_attachment(char *multi_user_name, int account_id, int mail_id, int nth, int *handle, int* err_code)
517 {
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);
519
520         int ret = false;
521         int err = EMAIL_ERROR_NONE;
522         email_event_t *event_data = NULL;
523
524         if (!nth) {
525                 EM_DEBUG_EXCEPTION("nth[%p] is invalid", nth);
526                 err = EMAIL_ERROR_INVALID_PARAM;
527                 goto FINISH_OFF;
528         }
529
530         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err)) {
531                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
532                 goto FINISH_OFF;
533         }
534
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;
539                 goto FINISH_OFF;
540         }
541
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);
547
548         if (!emcore_insert_event(event_data, (int*)handle, &err))  {
549                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
550                 goto FINISH_OFF;
551         }
552
553 #ifdef __FEATURE_LOCAL_ACTIVITY__
554         EM_DEBUG_LOG("Setting g_local_activity_run ");
555         g_local_activity_run = 1;
556 #endif
557
558         ret = true;
559
560 FINISH_OFF:
561
562         if (ret == false && event_data) {
563                 emcore_free_event(event_data);
564                 EM_SAFE_FREE(event_data);
565         }
566
567         if (err_code)
568                 *err_code = err;
569
570         EM_DEBUG_FUNC_END();
571
572         return ret;
573 }
574
575 INTERNAL_FUNC int emdaemon_free_attachment_data(email_attachment_data_t** atch_data, int* err_code)
576 {
577         EM_DEBUG_FUNC_BEGIN();
578         return emcore_free_attachment_data(atch_data, 1, err_code);
579 }
580
581 void* thread_func_to_delete_mail(void *thread_argument)
582 {
583         EM_DEBUG_FUNC_BEGIN();
584         int  err = EMAIL_ERROR_NONE;
585         int *mail_id_list = NULL;
586         int  mail_id_count = 0;
587         int  account_id = 0;
588         int  from_server = 0;
589         int  noti_param_2 = 0;
590         int handle = 0;
591         char *multi_user_name = NULL;
592         email_event_t *event_data = (email_event_t*)thread_argument;
593
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;
599
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);
604                 goto FINISH_OFF;
605         }
606
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);
612                         } */
613                         emcore_free_event(event_data); /* prevent 17922 */
614                         EM_SAFE_FREE(event_data);
615                         goto FINISH_OFF;
616                 }
617         }
618         else {
619                 emcore_free_event(event_data); /* prevent 17922 */
620                 EM_SAFE_FREE(event_data);
621         }
622
623 FINISH_OFF:
624         /* Don't free event_data as if the data is destined to be passed to event handler through emcore_insert_event.*/
625
626         EM_DEBUG_FUNC_END();
627         return SUCCESS;
628 }
629
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)
631 {
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);
633
634         int ret = false;
635         int err = EMAIL_ERROR_NONE;
636         int *p = NULL;
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;
642
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;
647                 goto FINISH_OFF;
648         }
649
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);
652                 goto FINISH_OFF;
653         }
654
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;
658                 goto FINISH_OFF;
659         }
660
661         memcpy(p, mail_ids, sizeof(int) * mail_ids_count);
662
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;
666                 goto FINISH_OFF;
667         }
668
669         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl_data->account_id, false);
670         if (!ref_account) {
671                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed.");
672                 err = EMAIL_ERROR_INVALID_ACCOUNT;
673                 goto FINISH_OFF;
674         }
675
676         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
677                 from_server = EMAIL_DELETE_LOCALLY;
678         }
679
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);
686
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 */
689         ret = true;
690
691 FINISH_OFF:
692
693         if (mailbox_tbl_data)
694                 emstorage_free_mailbox(&mailbox_tbl_data, 1, NULL);
695
696         if (ref_account) {
697                 emcore_free_account(ref_account);
698                 EM_SAFE_FREE(ref_account);
699         }
700
701         if (err != EMAIL_ERROR_NONE || thread_error != 0) {
702             emcore_free_event(event_data);
703                 EM_SAFE_FREE(event_data);
704                 EM_SAFE_FREE(p);
705         }
706
707         if (err_code)
708                 *err_code = err;
709
710         return ret;
711 }
712
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)
714 {
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);
716
717         int ret = false;
718         int err = EMAIL_ERROR_NONE;
719         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
720         email_event_t *event_data = NULL;
721
722         if (!input_mailbox_id) {
723                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
724                 err = EMAIL_ERROR_INVALID_PARAM;
725                 goto FINISH_OFF;
726         }
727
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);
730                 goto FINISH_OFF;
731         }
732
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);
735                 goto FINISH_OFF;
736         }
737
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;
744                         goto FINISH_OFF;
745                 }
746
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);
752
753                 if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
754                         EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
755                         goto FINISH_OFF;
756                 }
757
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;
762                 int activityid = 0;
763
764                 if (false == emcore_get_next_activity_id(&activityid,&err)) {
765                         EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
766                 }
767
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);
770                         goto FINISH_OFF;
771                 }
772
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;
777                         goto FINISH_OFF;
778                 }
779
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);
783
784
785                                 EM_SAFE_FREE(mail_ids);
786                                 goto FINISH_OFF;
787                         }
788
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;
796
797                         if (! emcore_add_activity(&new_activity, &err))
798                                 EM_DEBUG_EXCEPTION(" emcore_add_activity  Failed  - %d ", err);
799
800                 }
801
802                 EM_SAFE_FREE(mail_ids);
803
804                 EM_DEBUG_LOG("Setting g_local_activity_run ");
805                 g_local_activity_run = 1;
806 #endif
807         }
808 #endif  /*  __FEATURE_SYNC_CLIENT_TO_SERVER__ */
809
810         ret = true;
811
812 FINISH_OFF:
813
814         if (ret == false && event_data) {
815                 emcore_free_event(event_data);
816                 EM_SAFE_FREE(event_data);
817         }
818
819         if (mailbox_tbl)
820                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
821
822         if (output_err_code)
823                 *output_err_code = err;
824
825         return ret;
826 }
827
828 void* thread_func_to_move_mail(void *thread_argument)
829 {
830         EM_DEBUG_FUNC_BEGIN();
831
832         int ret = false;
833         int *mail_ids = NULL, mail_ids_count, noti_param_1, noti_param_2, err;
834         int handle = 0;
835         email_event_t *event_data = (email_event_t*)thread_argument;
836         int dst_mailbox_id = 0;
837         char *multi_user_name = NULL;
838
839         if(!event_data) { /*prevent 53096*/
840                 EM_DEBUG_EXCEPTION("INVALID_PARMAETER");
841                 return NULL;
842         }
843
844
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;
852
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);
855                 goto FINISH_OFF;
856         }
857
858         if (!emcore_insert_event(event_data, (int*)&handle, &err)) {
859                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
860                 goto FINISH_OFF;
861         }
862
863         ret = true;
864
865 FINISH_OFF:
866
867         if (ret == false && event_data) {
868                 emcore_free_event(event_data);
869                 EM_SAFE_FREE(event_data);
870         }
871
872         EM_DEBUG_FUNC_END();
873         return SUCCESS;
874 }
875
876 INTERNAL_FUNC int emdaemon_move_mail_all_mails(char *multi_user_name, int src_mailbox_id, int dst_mailbox_id, int* err_code)
877 {
878         EM_DEBUG_FUNC_BEGIN("src_mailbox_id[%d], dst_mailbox_id[%d], err_code[%p]", src_mailbox_id, dst_mailbox_id, err_code);
879
880         int ret = false;
881         int err = EMAIL_ERROR_NONE;
882         int select_num = 0;
883         int *mails = NULL;
884         int *p = NULL;
885         int i = 0;
886         int num = 0;
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;
894
895         if ( dst_mailbox_id <= 0|| src_mailbox_id <= 0)  {
896                 EM_DEBUG_EXCEPTION("Invalid Parameter");
897                 err = EMAIL_ERROR_INVALID_PARAM;
898                 goto FINISH_OFF;
899         }
900
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);
903                 goto FINISH_OFF;
904         }
905
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);
908                 goto FINISH_OFF;
909         }
910
911         ref_account = emcore_get_account_reference(multi_user_name, dst_mailbox_tbl->account_id, false);
912         if (!ref_account)  {
913                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dst_mailbox_tbl->account_id);
914                 err = EMAIL_ERROR_INVALID_ACCOUNT;
915                 goto FINISH_OFF;
916         }
917
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");
920                 goto FINISH_OFF;
921         }
922
923         mails = malloc(sizeof(int) * select_num);
924
925         if( !mails ) {
926                 EM_DEBUG_EXCEPTION("Malloc failed...!");
927                 err = EMAIL_ERROR_OUT_OF_MEMORY;
928                 goto FINISH_OFF;
929         }
930
931         memset(mails, 0x00, sizeof(int) * select_num);
932
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;
936                         num++;
937                 }
938         }
939
940         if( num <= 0) {
941                 EM_DEBUG_EXCEPTION("can't find avalable mails. num = %d", num);
942                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
943                 goto FINISH_OFF;
944         }
945
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;
949                 goto FINISH_OFF;
950         }
951
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;
955                 goto FINISH_OFF;
956         }
957
958         memcpy(p, mails, sizeof(int) * num);
959
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);
967
968 #ifdef __FEATURE_LOCAL_ACTIVITY__
969         int i = 0, activityid = 0;
970
971         if (false == emcore_get_next_activity_id(&activityid,&err))
972                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
973
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;
983
984                 if (!emcore_add_activity(&new_activity, &err))
985                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
986         }
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);
991         ret = true;
992
993 FINISH_OFF:
994
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__ */
999
1000         if (err_code)
1001                 *err_code = err;
1002
1003         if (ref_account) {
1004                 emcore_free_account(ref_account);
1005                 EM_SAFE_FREE(ref_account);
1006         }
1007
1008         if (dst_mailbox_tbl)
1009                 emstorage_free_mailbox(&dst_mailbox_tbl, 1, NULL);
1010
1011         if (src_mailbox_tbl)
1012                 emstorage_free_mailbox(&src_mailbox_tbl, 1, NULL);
1013
1014         if(mail_list)
1015                 EM_SAFE_FREE(mail_list);
1016
1017         if(mails != NULL )
1018                 EM_SAFE_FREE(mails);
1019
1020     if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1021         emcore_free_event(event_data);
1022         EM_SAFE_FREE(event_data);
1023         EM_SAFE_FREE(p);
1024     }
1025
1026         EM_DEBUG_FUNC_END("ret [%d]", ret);
1027         return ret;
1028 }
1029
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)
1031 {
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);
1033
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;
1043
1044         if (num <= 0 || dst_mailbox_id <= 0) {
1045                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1046                 err = EMAIL_ERROR_INVALID_PARAM;
1047                 goto FINISH_OFF;
1048         }
1049
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);
1052                 goto FINISH_OFF;
1053         }
1054
1055         ref_account = emcore_get_account_reference(multi_user_name, dest_mailbox_tbl->account_id, false);
1056
1057         if (!ref_account) {
1058                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", dest_mailbox_tbl->account_id);
1059                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1060                 goto FINISH_OFF;
1061         }
1062
1063         /* Getting source mailbox name */
1064         mail_id = mail_ids[0];
1065
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);
1068                 goto FINISH_OFF;
1069         }
1070
1071         if (src_mailbox_id == 0)
1072                 src_mailbox_id = mail_table_data->mailbox_id;
1073
1074         emstorage_free_mail(&mail_table_data, 1, NULL);
1075
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;
1079                 goto FINISH_OFF;
1080         }
1081
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;
1085                 goto FINISH_OFF;
1086         }
1087
1088         memcpy(p, mail_ids, sizeof(int) * num);
1089
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);
1099
1100 #ifdef __FEATURE_LOCAL_ACTIVITY__
1101         int i = 0, activityid = 0;
1102
1103         if (false == emcore_get_next_activity_id(&activityid,&err))
1104                 EM_DEBUG_EXCEPTION(" emcore_get_next_activity_id Failed - %d ", err);
1105
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;
1115
1116                 if (!emcore_add_activity(&new_activity, &err))
1117                         EM_DEBUG_EXCEPTION(" emcore_add_activity Failed - %d ", err);
1118         }
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);
1123         ret = true;
1124
1125 FINISH_OFF:
1126
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__ */
1131
1132         if (err_code)
1133                 *err_code = err;
1134
1135         if (ref_account) {
1136                 emcore_free_account(ref_account);
1137                 EM_SAFE_FREE(ref_account);
1138         }
1139
1140         if (dest_mailbox_tbl)
1141                 emstorage_free_mailbox(&dest_mailbox_tbl, 1, NULL);
1142
1143     if (err != EMAIL_ERROR_NONE || thread_error != 0) {
1144         emcore_free_event(event_data);
1145         EM_SAFE_FREE(event_data);
1146         EM_SAFE_FREE(p);
1147     }
1148
1149         EM_DEBUG_FUNC_END("ret [%d]", ret);
1150         return ret;
1151 }
1152
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)
1154 {
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*/
1156
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;
1161         int handle = 0;
1162
1163         if(account_id <= 0 || !mail_ids || num <= 0) {
1164                 err = EMAIL_ERROR_INVALID_PARAM;
1165                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1166                 goto FINISH_OFF;
1167         }
1168
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);
1171                 goto FINISH_OFF;
1172         }
1173
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);
1176                 goto FINISH_OFF;
1177         }
1178
1179         if( onserver && account_tbl->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 ) {
1180                 mail_id_array = em_malloc(sizeof(int) * num);
1181
1182                 if (mail_id_array == NULL)  {
1183                         EM_DEBUG_EXCEPTION("em_malloc failed...");
1184                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1185                         goto FINISH_OFF;
1186                 }
1187
1188                 memcpy(mail_id_array, mail_ids, sizeof(int) * num);
1189
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;
1194                         goto FINISH_OFF;
1195                 }
1196
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);
1205
1206                 if (!emcore_insert_event(event_data, (int*)&handle, &err))  {
1207                         EM_DEBUG_LOG("emcore_insert_event failed [%d]", err);
1208                         goto FINISH_OFF;
1209                 }
1210         }
1211
1212         ret = true;
1213
1214 FINISH_OFF:
1215
1216         if (ret == false && event_data) {
1217                 emcore_free_event(event_data);
1218                 EM_SAFE_FREE(event_data);
1219         }
1220
1221         if (account_tbl)
1222                 emstorage_free_account(&account_tbl, 1, NULL);
1223
1224         if (err_code)
1225                 *err_code = err;
1226         EM_DEBUG_FUNC_END();
1227         return ret;
1228 }
1229
1230
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)
1232 {
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;
1237
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;
1242                 goto FINISH_OFF;
1243         }
1244
1245         ref_account = emcore_get_account_reference(multi_user_name, input_mail_data->account_id, false);
1246         if (!ref_account)  {
1247                 EM_DEBUG_LOG(" emcore_get_account_reference failed [%d]", input_mail_data->account_id);
1248                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1249                 goto FINISH_OFF;
1250         }
1251
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);
1254                 goto FINISH_OFF;
1255         }
1256
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;
1263                         goto FINISH_OFF;
1264                 }
1265
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);
1274
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;
1278                         goto FINISH_OFF;
1279                 }
1280         }
1281 */
1282 #endif
1283
1284 FINISH_OFF:
1285
1286         if (ref_account) {
1287                 emcore_free_account(ref_account);
1288                 EM_SAFE_FREE(ref_account);
1289         }
1290
1291         EM_DEBUG_FUNC_END("err [%d]", err);
1292         return err;
1293 }
1294
1295
1296 int _emdaemon_check_mail_id(char *multi_user_name, int mail_id, int* err_code)
1297 {
1298         EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
1299
1300         int ret = false;
1301         int err = EMAIL_ERROR_NONE;
1302
1303         emstorage_mail_tbl_t* mail = NULL;
1304
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);
1307                 goto FINISH_OFF;
1308         }
1309
1310         ret = true;
1311
1312 FINISH_OFF:
1313         if (mail != NULL)
1314                 emstorage_free_mail(&mail, 1, NULL);
1315
1316         if (err_code != NULL)
1317                 *err_code = err;
1318         EM_DEBUG_FUNC_END();
1319         return ret;
1320 }
1321
1322 typedef struct {
1323         int   mail_id;
1324         char *multi_user_name;
1325 } email_retry_info;
1326
1327 INTERNAL_FUNC int emdaemon_send_mail_retry(char *multi_user_name, int mail_id, int timeout_in_sec, int* err_code)
1328 {
1329         int ret = false;
1330         int err = EMAIL_ERROR_NONE;
1331         long nTimerValue = 0;
1332         email_retry_info *retry_info = NULL;
1333
1334         if (!_emdaemon_check_mail_id(multi_user_name, mail_id, &err))  {
1335                 EM_DEBUG_EXCEPTION("_emdaemon_check_mail_id failed [%d]", err);
1336                 goto FINISH_OFF;
1337         }
1338
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);
1342                         goto FINISH_OFF;
1343                 }
1344         }
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;
1350                 }
1351
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;
1355
1356                 if (emcore_set_timer_ex(nTimerValue, (EMAIL_TIMER_CALLBACK) _OnMailSendRetryTimerCB, (void*)retry_info) <= 0) {
1357                         EM_DEBUG_EXCEPTION("Failed to start timer");
1358                         goto FINISH_OFF;
1359                 }
1360         }
1361
1362         ret = true;
1363
1364 FINISH_OFF:
1365         if (err_code)
1366                 *err_code = err;
1367
1368         return ret;
1369 }
1370
1371 INTERNAL_FUNC void _OnMailSendRetryTimerCB(void* data)
1372 {
1373         EM_DEBUG_FUNC_BEGIN();
1374         int err = EMAIL_ERROR_NONE;
1375         email_retry_info *retry_info = NULL;
1376
1377         if (!data) {
1378                 EM_DEBUG_LOG("Invalid param");
1379                 return;
1380         }
1381
1382         retry_info = (email_retry_info *)data;
1383
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);
1386                 goto FINISH_OFF;
1387         }
1388
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);
1391                 goto FINISH_OFF;
1392         }
1393
1394 FINISH_OFF:
1395
1396         free(retry_info->multi_user_name);
1397         free(retry_info);
1398
1399         EM_DEBUG_FUNC_END();
1400         return;
1401 }
1402
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)
1404 {
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);
1406         int ret = false;
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,};
1412
1413         if (target_mailbox_id == 0) {
1414                 EM_DEBUG_EXCEPTION("target_mailbox_id [%d]", target_mailbox_id);
1415                 err = EMAIL_ERROR_INVALID_PARAM;
1416                 goto FINISH_OFF;
1417         }
1418
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);
1421
1422                 goto FINISH_OFF;
1423         }
1424
1425         mail_id_list = em_malloc(sizeof(int) * result_count);
1426
1427         if (mail_id_list == NULL) {
1428                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1429                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1430                 goto FINISH_OFF;
1431         }
1432
1433         for(i = 0; i < result_count; i++) {
1434                 mail_id_list[i] = mail_list[i].mail_id;
1435         }
1436         account_id = mail_list[0].account_id;
1437
1438
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);
1441                 goto FINISH_OFF;
1442         }
1443
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);
1448                         break;
1449                 }
1450         }
1451
1452         if(!target_mailbox) {
1453                 EM_DEBUG_EXCEPTION("couldn't find proper target mailbox.");
1454                 goto FINISH_OFF;
1455         }
1456
1457 /*
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);
1460                 goto FINISH_OFF;
1461         }
1462 */
1463
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] >>>> ");
1467
1468         ret = true;
1469
1470 FINISH_OFF:
1471         emcore_free_mailbox_list(&target_mailbox_list, mailbox_count);
1472         EM_SAFE_FREE(mail_list);
1473         EM_SAFE_FREE(mail_id_list);
1474
1475         if (err_code)
1476                 *err_code = err;
1477         EM_DEBUG_FUNC_END();
1478         return ret;
1479 }
1480
1481 INTERNAL_FUNC int emdaemon_delete_mail_thread(char *multi_user_name, int thread_id, int delete_always_flag, int *handle, int *err_code)
1482 {
1483         EM_DEBUG_FUNC_BEGIN("thread_id [%d], delete_always_flag [%d], err_code [%p]", thread_id, delete_always_flag, err_code);
1484         int ret = false;
1485         int err = EMAIL_ERROR_NONE;
1486         int account_id = 0;
1487         int mailbox_id, *mail_id_list = NULL, result_count = 0, i;
1488         email_mail_list_item_t *mail_list = NULL;
1489
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);
1492                 goto FINISH_OFF;
1493         }
1494
1495         mail_id_list = em_malloc(sizeof(int) * result_count);
1496
1497         if (mail_id_list == NULL) {
1498                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1499                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1500                 goto FINISH_OFF;
1501         }
1502
1503         for(i = 0; i < result_count; i++) {
1504                 mail_id_list[i] = mail_list[i].mail_id;
1505         }
1506
1507         account_id = mail_list[0].account_id;
1508         mailbox_id = mail_list[0].mailbox_id;
1509
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);
1514                 goto FINISH_OFF;
1515         }
1516
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] >>>> ");
1519
1520         ret = true;
1521
1522 FINISH_OFF:
1523         EM_SAFE_FREE(mail_list);
1524         EM_SAFE_FREE(mail_id_list);
1525
1526         if (err_code)
1527                 *err_code = err;
1528         EM_DEBUG_FUNC_END();
1529         return ret;
1530 }
1531
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)
1533 {
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);
1535         int ret = false;
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;
1539
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);
1542                 goto FINISH_OFF;
1543         }
1544
1545         mail_id_list = em_malloc(sizeof(int) * result_count);
1546
1547         if (mail_id_list == NULL) {
1548                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1549                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1550                 goto FINISH_OFF;
1551         }
1552
1553         for(i = 0; i < result_count; i++) {
1554                 mail_id_list[i] = mail_list[i].mail_id;
1555         }
1556
1557         account_id = mail_list[0].account_id;
1558
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);
1561                 goto FINISH_OFF;
1562         }
1563
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] >>>> ");
1566
1567         ret = true;
1568
1569 FINISH_OFF:
1570         EM_SAFE_FREE(mail_list);
1571         EM_SAFE_FREE(mail_id_list);
1572
1573         if (err_code)
1574                 *err_code = err;
1575         EM_DEBUG_FUNC_END();
1576         return ret;
1577 }
1578
1579 INTERNAL_FUNC int emdaemon_expunge_mails_deleted_flagged(char *multi_user_name, int input_mailbox_id, int input_on_server, int *output_handle)
1580 {
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;
1583         int handle = 0;
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;
1588
1589         if (input_mailbox_id <= 0)  {
1590                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1591                 err = EMAIL_ERROR_INVALID_PARAM;
1592                 goto FINISH_OFF;
1593         }
1594
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);
1597                 goto FINISH_OFF;
1598         }
1599
1600         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1601         if (!ref_account)  {
1602                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox_tbl->account_id);
1603                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1604                 goto FINISH_OFF;
1605         }
1606
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;
1613                         goto FINISH_OFF;
1614                 }
1615
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);
1620
1621                 if (!emcore_insert_event(event_data, &handle, &err)) {
1622                         EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", err);
1623                         goto FINISH_OFF;
1624                 }
1625                 event_insert = true;
1626         }
1627         else
1628 #endif
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);
1631                         goto FINISH_OFF;
1632                 }
1633
1634 FINISH_OFF:
1635
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);
1640         }
1641
1642         if (ref_account) {
1643                 emcore_free_account(ref_account);
1644                 EM_SAFE_FREE(ref_account);
1645         }
1646
1647         if(mailbox_tbl)
1648                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1649
1650         EM_DEBUG_FUNC_END("err [%d]", err);
1651         return err;
1652 }