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