Boiler plate changed.
[platform/core/messaging/email-service.git] / email-core / email-core-imap-mailbox.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-core-imap_folder.c
26  * Desc :  Mail IMAP mailbox
27  *
28  * Auth : 
29  *
30  * History : 
31  *    2006.08.01  :  created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <vconf.h>
37 #include "email-core-global.h"
38 #include "email-core-utils.h"
39 #include "c-client.h"
40 #include "email-storage.h"
41 #include "email-utilities.h"
42 #include "email-network.h"
43 #include "email-core-event.h"
44 #include "email-core-mailbox.h"
45 #include "email-core-imap-mailbox.h"
46 #include "email-core-mailbox-sync.h"
47 #include "email-core-account.h" 
48 #include "email-core-signal.h"
49 #include "lnx_inc.h"
50
51 #include "email-debug-log.h"
52
53 INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *output_count)
54 {       
55         EM_DEBUG_FUNC_BEGIN("input_account_id [%d] output_count[%p]", input_account_id, output_count);
56
57         int err = EMAIL_ERROR_NONE;
58         int default_mail_slot_count = 25;
59         email_account_t *account_ref = NULL;
60
61         if (output_count == NULL) {
62                 err = EMAIL_ERROR_INVALID_PARAM;
63                 goto FINISH_OFF;
64         }
65
66         account_ref = emcore_get_account_reference(input_account_id);
67         if (account_ref)
68                 default_mail_slot_count = account_ref->default_mail_slot_size;
69
70 FINISH_OFF: 
71         
72         if (output_count)
73                 *output_count = default_mail_slot_count;
74
75         EM_DEBUG_FUNC_END("err[%d]", err);
76         return err;
77 }
78
79
80 INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
81 {
82         EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
83
84         int ret = false; 
85         int *mail_id_list = NULL, mail_id_list_count = 0;
86         int err = EMAIL_ERROR_NONE;
87         email_account_t *account_ref = NULL;
88         
89         if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
90                 if (intput_mailbox_tbl)
91                 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
92                 err = EMAIL_ERROR_INVALID_PARAM;
93                 goto FINISH_OFF;
94         }
95
96         account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
97         if (account_ref) {
98                 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
99                         EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
100                         err = EMAIL_ERROR_NOT_SUPPORTED;
101                         goto FINISH_OFF;
102                 }
103         }
104         
105         if (!emstorage_get_overflowed_mail_id_list(intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_id, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
106                 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
107                         EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
108                         err = EMAIL_ERROR_NONE;
109                         ret = true;
110                 }
111                 else
112                         EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
113                 goto FINISH_OFF;
114         }
115
116         if (mail_id_list) {
117                 if (!emcore_delete_mail(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
118                         EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
119                         goto FINISH_OFF;
120                 }
121         }
122         
123         ret = true;
124 FINISH_OFF: 
125         EM_SAFE_FREE(mail_id_list);
126
127         if (err_code)
128                 *err_code = err;
129
130         EM_DEBUG_FUNC_END("ret [%d]", ret);
131         return ret;
132 }
133
134
135 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
136 {
137         EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
138
139         int ret = false, err = EMAIL_ERROR_NONE; 
140         int i = 0;
141         int account_count = 100;
142         int mailbox_count = 0;
143         email_account_t *account_ref = NULL;
144         emstorage_account_tbl_t *account_tbl_list = NULL;
145         emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
146
147         if (account_id > ALL_ACCOUNT) {
148                 account_ref = emcore_get_account_reference(account_id);
149                 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
150                         EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
151                         ret = true;
152                         goto FINISH_OFF;
153                 }
154                 else if (!account_ref) {
155                         EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
156                         goto FINISH_OFF;
157                 }
158                 if (mailbox_id == 0) {
159                         if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
160                                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
161                                 goto FINISH_OFF;
162                         }
163                 }
164         }
165         else {
166                 if (mailbox_id == 0) {
167                         if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
168                                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
169                                 goto FINISH_OFF;
170                         }
171                         for ( i = 0; i < account_count; i++) {
172                                 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
173                                         EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
174                                         goto FINISH_OFF;
175                                 }
176                         }
177                 }
178         }
179
180
181         if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
182                 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
183                 goto FINISH_OFF;
184         }
185
186         if (mailbox_id) {
187                 mailbox_count = 1;
188                 if (new_slot_size > 0) {
189                         mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
190                         if(!mailbox_tbl_list) {
191                                 EM_DEBUG_EXCEPTION("em_malloc failed");
192                                 goto FINISH_OFF;
193                         }
194                         mailbox_tbl_list->account_id = account_id;
195                         mailbox_tbl_list->mailbox_id = mailbox_id;
196                         mailbox_tbl_list->mail_slot_size = new_slot_size;     
197                 }
198                 else   {        /*  read information from DB */
199                         if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
200                                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
201                                 goto FINISH_OFF;
202                         }
203                         
204                 }
205         }
206         else {
207                 if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
208                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
209                         goto FINISH_OFF;
210                 }
211         }
212
213         for (i = 0; i < mailbox_count; i++) {
214                 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
215                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
216                                 err = EMAIL_ERROR_NONE;
217                         else
218                                 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
219                 }
220         }
221
222         ret = true;
223         
224 FINISH_OFF: 
225
226         if (account_tbl_list)
227                 emstorage_free_account(&account_tbl_list, account_count, NULL);
228
229         if (mailbox_tbl_list)
230                 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
231
232
233         if (err_code)
234                 *err_code = err;
235         return ret;
236 }
237
238 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
239 {
240     email_account_t *ref_account = NULL;
241         size_t path_len = 0;
242
243
244         ref_account = emcore_get_account_reference(account_id);
245         if (!ref_account)        {
246                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
247                 return 0;
248         }
249
250         path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
251                           (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
252
253     *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
254                           /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
255     if (!*path)
256         return 0;
257         memset(*path, 0x00, path_len);
258     /* 1. server address / server type */
259
260     if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
261         SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
262     }
263     else {
264         SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
265     }
266
267     /* 2. set tls option if security connection */
268 /*     if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1)); */
269         if (ref_account->incoming_server_secure_connection & 0x01) {
270                 strncat(*path + 1, "/ssl", path_len-(EM_SAFE_STRLEN(*path)-1));
271         }
272         if (ref_account->incoming_server_secure_connection & 0x02)
273                 strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));
274         else
275                 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
276
277     /*  3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
278     strncat(*path + 1, "}", path_len-EM_SAFE_STRLEN(*path)-1);
279     **path = '{';
280
281     if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
282
283     return 1;
284 }
285
286 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
287 {
288         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
289         
290         int ret = false;
291         int err = EMAIL_ERROR_NONE;
292         int status = EMAIL_DOWNLOAD_FAIL;
293         MAILSTREAM *stream = NULL;
294         email_internal_mailbox_t *mailbox_list = NULL;
295         email_account_t *ref_account = NULL;
296         void *tmp_stream = NULL;
297         char *mbox_path = NULL;
298         char *mailbox_name_for_mailbox_type = NULL;
299         int   i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
300         
301         if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
302                 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
303
304         if (!emcore_check_thread_status())  {
305                 err = EMAIL_ERROR_CANCELLED;
306                 goto FINISH_OFF;
307         }
308         if (!emnetwork_check_network_status(&err)) {
309                 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
310                 goto FINISH_OFF;
311         }
312         
313         ref_account = emcore_get_account_reference(account_id);
314         if (!ref_account)  {
315                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
316                 err = EMAIL_ERROR_INVALID_ACCOUNT;
317                 goto FINISH_OFF;
318         }
319         
320         /* if not imap4 mail, return */
321         if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4)  {
322                 EM_DEBUG_EXCEPTION("unsupported account...");
323                 err = EMAIL_ERROR_INVALID_ACCOUNT;
324                 goto FINISH_OFF;
325         }
326         
327         /*  get mail server path */
328         /*  mbox_path is not used. the below func might be unnecessary */
329         if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path)  {
330                 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
331                 goto FINISH_OFF;
332         }
333         
334         
335         if (!emcore_check_thread_status())  {
336                 err = EMAIL_ERROR_CANCELLED;
337                 goto FINISH_OFF;
338         }
339
340         stream = NULL;
341         if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream)  {
342                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
343                 
344                 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
345                         err = EMAIL_ERROR_CANCELLED;
346                 else
347                         err = EMAIL_ERROR_CONNECTION_FAILURE;
348                 
349                 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
350                 goto FINISH_OFF;
351         }
352         
353         EM_SAFE_FREE(mbox_path);
354         
355         stream = (MAILSTREAM *)tmp_stream;
356         
357         if (!emcore_check_thread_status())  {
358                 err = EMAIL_ERROR_CANCELLED;
359                 goto FINISH_OFF;
360         }
361         
362         /*  download mailbox list */
363         if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err))  {
364                 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
365                 goto FINISH_OFF;
366         }
367
368         if (!emcore_check_thread_status())  {
369                 err = EMAIL_ERROR_CANCELLED;
370                 goto FINISH_OFF;
371         }
372         
373         for (i = 0; i < count; i++) {
374                 if (!emcore_check_thread_status())  {
375                         EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
376                         err = EMAIL_ERROR_CANCELLED;
377                         goto FINISH_OFF;
378                 }
379                 if (mailbox_list[i].mailbox_name) {
380                         EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].mailbox_name);
381                         mailbox_list[i].mail_slot_size = ref_account->default_mail_slot_size;
382
383                         if(mailbox_list[i].mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
384                                 emcore_bind_mailbox_type(mailbox_list + i);
385
386                         if (mailbox_list[i].mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) {    /* if result mailbox type is duplicated,  */
387                                 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
388                                         EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].mailbox_name);
389                                         mailbox_list[i].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one  */
390                                 }
391                                 else
392                                         mailbox_type_list[mailbox_list[i].mailbox_type] = i;
393                         }
394
395                         EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
396                         if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
397                                 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
398                                 goto FINISH_OFF;
399                         }
400
401                 }
402         }
403
404
405         for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
406                 /* if (!emstorage_get_mailbox_name_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err))  */
407                 if (mailbox_type_list[counter] == -1) {
408                         /* EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed - %d", err); */
409                         /* if (EMAIL_ERROR_MAILBOX_NOT_FOUND == err)     */
410                         /* { */
411                                 emstorage_mailbox_tbl_t mailbox_tbl;
412                                 
413                                 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
414                                 
415                                 mailbox_tbl.account_id = account_id;
416                                 mailbox_tbl.mailbox_id = 0;
417                                 mailbox_tbl.local_yn = 1; 
418                                 mailbox_tbl.mailbox_type = counter;
419                                 mailbox_tbl.deleted_flag =  0;
420                                 mailbox_tbl.modifiable_yn = 1; 
421                                 mailbox_tbl.total_mail_count_on_server = 0;
422                                 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
423                                 
424                                 switch (counter) {
425                                         case EMAIL_MAILBOX_TYPE_SENTBOX:
426                                                 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
427                                                 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
428                                                 break;
429                                                 
430                                         case EMAIL_MAILBOX_TYPE_TRASH:
431                                                 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
432                                                 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
433                                                 break;
434
435                                     case EMAIL_MAILBOX_TYPE_DRAFT:
436                                                 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
437                                                 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
438                                                 break;
439                                                 
440                                         case EMAIL_MAILBOX_TYPE_SPAMBOX:
441                                                 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
442                                                 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
443                                                 break;
444                                                 
445                                         case EMAIL_MAILBOX_TYPE_OUTBOX:
446                                                 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
447                                                 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
448                                                 break;
449
450                                         default: 
451                                                 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
452                                                 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
453                                                 break;
454                                 }
455
456                                 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
457                                         EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
458                                         goto FINISH_OFF;
459                                 }
460                                 
461                         /* }     */
462                         /* else */
463                         /* { */
464                         /*  */
465                         /*      goto FINISH_OFF; */
466                         /* } */
467                         
468                 }
469                 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
470         }
471
472         emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
473         int select_num = 0;
474         i = 0;
475         email_mailbox_t mailbox;
476
477         if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
478                 if (local_mailbox_list) {
479                         for (i = 0; i < select_num; i++) {
480                                 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
481                                 mailbox.account_id = local_mailbox_list[i].account_id;
482                                 mailbox.mailbox_name = local_mailbox_list[i].mailbox_name;
483                                 mailbox.mailbox_id = local_mailbox_list[i].mailbox_id;
484                                 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
485                                         EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.mailbox_name);
486                                         emstorage_free_mailbox(&local_mailbox_list, select_num, NULL); 
487                                         local_mailbox_list = NULL;
488                                         goto FINISH_OFF;
489                                 }
490                         }
491                         emstorage_free_mailbox(&local_mailbox_list, select_num, NULL); 
492                         local_mailbox_list = NULL;
493                 }
494         }
495
496         if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
497                         EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
498                         goto FINISH_OFF;
499         }
500         
501         if (!emcore_check_thread_status())  {
502                 err = EMAIL_ERROR_CANCELLED;
503                 goto FINISH_OFF;
504         }
505         
506         for (i = 0; i < count; i++)
507                 mailbox_list[i].account_id = account_id;
508         
509         
510         ret = true;
511         
512 FINISH_OFF: 
513
514         if (err == EMAIL_ERROR_NONE) {
515                 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
516                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
517         }
518         else {
519                 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
520                         EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
521         }
522         EM_SAFE_FREE(mailbox_name_for_mailbox_type);
523         EM_SAFE_FREE(mbox_path);
524
525         if (stream) 
526                 emcore_close_mailbox(account_id, stream);
527         
528         if (mailbox_list) 
529                 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
530
531         if (err_code != NULL)
532                 *err_code = err;
533         EM_DEBUG_FUNC_END("ret [%d]", ret);
534         return ret;
535 }
536
537 int emcore_download_mailbox_list(void *mail_stream, 
538                                                                                 char *mailbox_name,
539                                                                                 email_internal_mailbox_t **mailbox_list,
540                                                                                 int *count, 
541                                                                                 int *err_code)
542 {
543         EM_DEBUG_FUNC_BEGIN("mail_stream [%p], mailbox_name [%p], mailbox_list [%p], count [%p], err_code [%p]", mail_stream, mailbox_name, mailbox_list, count, err_code);
544
545     MAILSTREAM *stream = mail_stream;
546     email_callback_holder_t holder;
547     char *pattern = NULL;
548     char *reference = NULL;
549     int   err = EMAIL_ERROR_NONE;
550     int   ret = false;
551
552         if (!stream || !mailbox_list || !count) {
553         err = EMAIL_ERROR_INVALID_PARAM;
554         goto FINISH_OFF;
555         }
556         
557         memset(&holder, 0x00, sizeof(holder));
558
559     /*  reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
560         if (mailbox_name)  {
561                 char *s = NULL;
562                 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
563                 if (reference) {
564                         strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
565                         if ((s = strchr(reference, '}')))
566                                 *(++s) = '\0';
567                         strcat(reference, mailbox_name);
568                 }
569         }
570         else
571                 reference = EM_SAFE_STRDUP(stream->original_mailbox);
572
573         pattern        = "*";
574     stream->sparep = &holder;
575
576         /*  imap command : tag LIST reference * */
577     /*  see callback function mm_list */
578         mail_list(stream, reference, pattern);
579
580     stream->sparep = NULL;
581
582         EM_SAFE_FREE(reference);
583
584     *count        = holder.num;
585     *mailbox_list = (email_internal_mailbox_t*)holder.data;
586
587         ret = true;
588
589 FINISH_OFF: 
590         if (err_code)
591                 *err_code = err;
592
593         return ret;
594 }
595
596 /* description
597  *    check whether this imap mailbox is synchronous mailbox
598  * arguments
599  *    mailbox  :  imap mailbox to be checked
600  *    synchronous  :   boolean variable to be synchronous (1 : sync 0 : non-sync)
601  * return
602  *    succeed  :  1
603  *    fail  :  0
604  */
605 int emcore_check_sync_imap_mailbox(email_mailbox_t *mailbox, int *synchronous, int *err_code)
606 {
607         EM_DEBUG_FUNC_BEGIN();
608         
609         EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
610         
611         if (err_code) {
612                 *err_code = EMAIL_ERROR_NONE;
613         }
614
615         if (!mailbox || !synchronous) {
616                 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
617                 
618                 if (err_code != NULL)
619                         *err_code = EMAIL_ERROR_INVALID_PARAM;
620                 return false;
621         }
622         
623         int ret = false;
624         int err = EMAIL_ERROR_NONE;
625         emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
626
627         if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl, true, &err))  {
628                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
629                 goto FINISH_OFF;
630         }
631         
632         *synchronous = imap_mailbox_tbl ? 1  :  0;
633         
634         ret = true;
635         
636 FINISH_OFF: 
637         if (imap_mailbox_tbl != NULL)
638                 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
639         
640         if (err_code != NULL)
641                 *err_code = err;
642         
643         return ret;
644 }
645
646
647 /* description
648  *    set sync imap mailbox
649  * arguments
650  *    mailbox_list  :  imap mailbox to be synced
651  *    syncronous  :  0-sync 1 : non-sync
652  * return
653  *    succeed  :  1
654  *    fail  :  0
655  */
656
657 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(email_internal_mailbox_t *mailbox, int synchronous, int *err_code)
658 {
659         EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
660         
661         if (!mailbox)  {
662                 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
663                 if (err_code != NULL)
664                         *err_code = EMAIL_ERROR_INVALID_PARAM;
665                 return false;
666         }
667         
668         int ret = false;
669         int err = EMAIL_ERROR_NONE;
670         emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
671         emstorage_mailbox_tbl_t mailbox_tbl = { 0, };
672         emcore_uid_list *uid_list = NULL;
673         emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
674         MAILSTREAM *stream = mailbox->mail_stream;
675         int mailbox_renamed = 0;
676         int j = 0;
677         int i = 0;
678         int temp = 0;
679         IMAPLOCAL *imap_local = NULL;
680         char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
681                 
682         if (synchronous) {              
683                 /* if synchcronous, insert imap mailbox to db */
684                 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl_item, true, &err))  {        
685                         /* mailbox already exists */
686                         /* mailbox Found, Do set the modifiable_yn = 1 */
687                         EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
688                         if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->mailbox_name, 1, true, &err)) {
689                                 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
690                                 goto JOB_ERROR;
691                         }
692                 }
693                 else {
694                         if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
695                                 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
696                                 /* This is error scenario so finish the job */
697                                 goto JOB_ERROR;
698                         }
699                         else {
700                                 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
701                                 /* Now check if mailbox is renamed */
702                                 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
703                                 if (stream) {
704                                         imap_local = ((MAILSTREAM *)stream)->local;
705                                         EM_DEBUG_LINE;
706                                         sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
707                                         EM_DEBUG_LINE;
708                                         sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->mailbox_name);
709                                         EM_DEBUG_LINE;
710
711                                 }
712                                 
713                                 /* select the mailbox and get its UID */
714                                 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
715                                         EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->mailbox_name);
716                                         /*
717                                         err = EMAIL_ERROR_CONNECTION_BROKEN;
718                                         if(imap_local)
719                                                 imap_local->netstream = NULL;
720                                         mailbox->mail_stream = NULL;
721                                         goto JOB_ERROR;
722                                         */
723                                 }
724                                 else {
725                                         EM_DEBUG_LOG("Get response for select call");
726                                         while (imap_local->netstream) {
727                                         p = net_getline(imap_local->netstream);
728                                                 EM_DEBUG_LOG("p =[%s]", p);
729                                                 if (!strncmp(p, "+", 1)) {
730                                                         ret = 1;
731                                                         break;
732                                                 }
733                                                 else if (!strncmp(p, "*", 1)) {
734                                                 EM_SAFE_FREE(p); 
735                                                 continue;
736                                                 }
737                                                 else {
738                                                         ret = 0;
739                                                         break;
740                                                 }
741                                 }
742                                         EM_SAFE_FREE(p); 
743                                         EM_DEBUG_LINE;
744                                         /* check if OK or BAD response comes. */
745                                         /* if response is OK the try getting UID list. */
746                                         if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK")))  {
747                                                 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->mailbox_name);
748                                                 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
749                                                         EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
750                                                         EM_SAFE_FREE(uid_list);
751                                                 }
752                                                 else {
753                                                         if (!emstorage_get_downloaded_list(mailbox->account_id, 0, &downloaded_uids, &j, true, &err)) {
754                                                                 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
755                                                 
756                                                                 downloaded_uids = NULL;
757                                                         }
758                                                         else /* Prevent Defect - 28497 */ {
759                                                                 emcore_uid_list *uid_elem = uid_list;
760                                                                 emcore_uid_list *next_uid_elem = NULL;
761                                                                 if (uid_elem) {
762                                                                         for (i = j; (i > 0 && !mailbox_renamed); i--)  {
763                                                                                 if (uid_elem) {
764                                                                                         next_uid_elem = uid_elem->next;
765                                                                                         if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
766                                                                                                 temp = i-1;
767                                                                                                 mailbox_renamed = 1;
768                                                                                                 break;
769                                                                                         }
770                                                                                         EM_SAFE_FREE(uid_elem->uid);
771                                                                                         uid_elem = next_uid_elem;
772                                                                                 }
773                                                                         }
774                                                                 }
775                                                         }
776                                                 }
777                                         } /* mailbox selected */
778                                 }       
779
780                                 if (mailbox_renamed) /* renamed mailbox */ {
781                                         EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
782                                         /* Do a mailbox rename in the DB */
783                                         if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->mailbox_name, true, &err))
784                                                 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
785
786                                         mailbox_renamed = 0;
787
788                                         memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
789
790                                         mailbox_tbl.account_id = mailbox->account_id;
791                                         mailbox_tbl.local_yn = 0;
792                                         mailbox_tbl.mailbox_name = mailbox->mailbox_name;
793                                         mailbox_tbl.mailbox_type = mailbox->mailbox_type;
794
795                                         /* Get the Alias Name after parsing the Full mailbox Path */
796                                         if(mailbox->alias == NULL)
797                                                 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
798                                         
799                                         mailbox_tbl.alias = mailbox->alias;
800                                         mailbox_tbl.deleted_flag  = 1;
801                                         mailbox_tbl.modifiable_yn = 1;
802                                         mailbox_tbl.total_mail_count_on_server = 0;
803                                         
804                                         /* if non synchronous, delete imap mailbox from db */
805                                         if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_id, &mailbox_tbl, true, &err)) {
806                                                 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
807                                                 goto JOB_ERROR;
808                                         }
809                                         
810                                 }
811                                 else /* Its a Fresh Mailbox */ {
812                                         memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
813
814                                         mailbox_tbl.mailbox_id     = mailbox->mailbox_id;
815                                         mailbox_tbl.account_id     = mailbox->account_id;
816                                         mailbox_tbl.local_yn       = 0;
817                                         mailbox_tbl.deleted_flag   = 0;
818                                         mailbox_tbl.mailbox_type   = mailbox->mailbox_type;
819                                         mailbox_tbl.mailbox_name   = mailbox->mailbox_name;
820                                         mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
821                                         mailbox_tbl.no_select      = mailbox->no_select;
822
823                                         /* Get the Alias Name after Parsing the Full mailbox Path */
824                                         if(mailbox->alias == NULL)
825                                         mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
826
827                                         if (mailbox->alias) {
828                                                 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
829
830                                                 mailbox_tbl.alias = mailbox->alias;
831                                                 mailbox_tbl.modifiable_yn = 1; 
832                                                 mailbox_tbl.total_mail_count_on_server = 0;
833                                                         
834                                                 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
835
836                                                 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
837                                                         EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
838                                                         goto JOB_ERROR;
839                                                 }
840                                         }
841                                 }
842                         }
843                 }
844         }
845
846         /* set sync db mailbox */
847         mailbox->synchronous = synchronous;
848         
849         ret = true;
850         
851 JOB_ERROR:
852
853         if (downloaded_uids) 
854                 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
855
856         if (imap_mailbox_tbl_item)
857                 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
858
859         if (err_code)
860                 *err_code = err;
861         
862         return ret;
863 }
864
865 /* description
866  *    create a new imap mailbox
867  * arguments
868  *    new_mailbox  :  imap mailbox to be created
869  * return
870  *    succeed  :  1
871  *    fail  :  0
872  */
873 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
874 {
875     MAILSTREAM *stream = NULL;
876     char *long_enc_path = NULL;
877     void *tmp_stream = NULL;
878     int ret = false;
879     int err = EMAIL_ERROR_NONE;
880
881     EM_DEBUG_FUNC_BEGIN();
882
883     if (!mailbox) {
884         err = EMAIL_ERROR_INVALID_PARAM;
885         goto FINISH_OFF;
886     }
887
888     /* connect mail server */
889     stream = NULL;
890     if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
891         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
892         goto FINISH_OFF;
893     }
894
895     stream = (MAILSTREAM *)tmp_stream;
896
897     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
898     if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
899         EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
900         goto FINISH_OFF;
901     }
902
903     /* create mailbox */
904     if (!mail_create(stream, long_enc_path)) {
905         EM_DEBUG_EXCEPTION("mail_create failed");
906         err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
907         goto FINISH_OFF;
908     }
909
910         emcore_close_mailbox(0, stream);                                
911         stream = NULL;
912
913     EM_SAFE_FREE(long_enc_path);
914
915     ret = true;
916
917 FINISH_OFF:
918     if (stream){
919                 emcore_close_mailbox(0, stream);                                
920                 stream = NULL;
921     }
922
923     EM_SAFE_FREE(long_enc_path);
924
925     if (mailbox) {
926                 if (err == EMAIL_ERROR_NONE) {
927                         if(!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
928                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
929                 }
930                 else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
931                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
932     }
933
934     if (err_code)
935         *err_code = err;
936
937     return ret;
938 }
939
940
941 /* description
942  *    delete a imap mailbox
943  * arguments
944  *    input_mailbox_id  :  mailbox ID to be deleted
945  * return
946  *    succeed  :  1
947  *    fail  :  0
948  */
949 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
950 {
951         EM_DEBUG_FUNC_BEGIN();
952
953     MAILSTREAM *stream = NULL;
954     char *long_enc_path = NULL;
955     email_account_t *ref_account = NULL;
956     void *tmp_stream = NULL;
957     int ret = false;
958     int err = EMAIL_ERROR_NONE;
959         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
960
961         if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
962                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
963                 goto FINISH_OFF;
964         }
965
966     ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
967
968     if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
969         EM_DEBUG_EXCEPTION("Invalid account information");
970         err = EMAIL_ERROR_INVALID_ACCOUNT;
971         goto FINISH_OFF;
972     }
973
974     /* connect mail server */
975     if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
976         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
977         goto FINISH_OFF;
978     }
979
980     stream = (MAILSTREAM *)tmp_stream;
981
982     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
983     if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
984         EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
985         goto FINISH_OFF;
986     }
987
988     /* delete mailbox */
989     if (!mail_delete(stream, long_enc_path)) {
990         EM_DEBUG_EXCEPTION("mail_delete failed");
991         err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
992         goto FINISH_OFF;
993     }
994
995         emcore_close_mailbox(0, stream);                                
996         stream = NULL;
997
998     EM_SAFE_FREE(long_enc_path);
999
1000     ret = true;
1001
1002 FINISH_OFF:
1003     if (stream) {
1004                 emcore_close_mailbox(0, stream);                                
1005                 stream = NULL;
1006     }
1007
1008     EM_SAFE_FREE(long_enc_path);
1009
1010     if (mailbox_tbl)
1011                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1012
1013         if (err == EMAIL_ERROR_NONE) {
1014                 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
1015                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
1016         }
1017         else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
1018                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
1019
1020     if (err_code)
1021         *err_code = err;
1022
1023     return ret;
1024 }
1025
1026
1027 INTERNAL_FUNC int emcore_move_mailbox_on_imap_server(int input_account_id, char *input_old_mailbox_path, char *input_new_mailbox_path)
1028 {
1029         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p]", input_account_id, input_old_mailbox_path, input_new_mailbox_path);
1030     MAILSTREAM *stream = NULL;
1031     char *long_enc_path_old = NULL;
1032     char *long_enc_path_new = NULL;
1033     email_account_t *ref_account = NULL;
1034     void *tmp_stream = NULL;
1035     int err = EMAIL_ERROR_NONE;
1036
1037     if (!input_old_mailbox_path || !input_new_mailbox_path) {
1038         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1039         err = EMAIL_ERROR_INVALID_PARAM;
1040         goto FINISH_OFF;
1041     }
1042
1043     ref_account = emcore_get_account_reference(input_account_id);
1044
1045     if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1046         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
1047         err = EMAIL_ERROR_INVALID_ACCOUNT;
1048         goto FINISH_OFF;
1049     }
1050
1051     /* connect mail server */
1052     stream = NULL;
1053     if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
1054         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
1055         goto FINISH_OFF;
1056     }
1057
1058     stream = (MAILSTREAM *)tmp_stream;
1059
1060     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1061     if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
1062         EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1063         goto FINISH_OFF;
1064     }
1065
1066     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1067     if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
1068         EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1069         goto FINISH_OFF;
1070     }
1071
1072     /* rename mailbox */
1073     if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1074         err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
1075         goto FINISH_OFF;
1076     }
1077
1078 FINISH_OFF:
1079     EM_SAFE_FREE(long_enc_path_old);
1080     EM_SAFE_FREE(long_enc_path_new);
1081
1082     if (stream) {
1083                 emcore_close_mailbox(0, stream);                                
1084                 stream = NULL;
1085     }
1086
1087     EM_DEBUG_FUNC_END("err [%d]", err);
1088     return err;
1089 }