tizen beta release
[framework/messaging/email-service.git] / email-core / em-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 :  em-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 "em-core-global.h"
38 #include "em-core-utils.h"
39 #include "c-client.h"
40 #include "em-storage.h"
41 #include "em-core-event.h"
42 #include "em-core-mailbox.h"
43 #include "em-core-imap-mailbox.h"
44 #include "em-core-mailbox-sync.h"
45 #include "em-core-account.h" 
46 #include "lnx_inc.h"
47
48 #include "emf-dbglog.h"
49
50 EXPORT_API int em_core_mailbox_get_default_mail_slot_count(int *output_count, int *err_code)
51 {       
52         EM_DEBUG_FUNC_BEGIN();
53         EM_DEBUG_LOG("output_count[%p], err_code[%p]", output_count, err_code);
54
55         int err = EMF_ERROR_NONE;
56         int mail_slot_count;
57         int ret = false, ret2;
58
59         if (output_count == NULL) {
60                 err = EMF_ERROR_INVALID_PARAM;
61                 goto FINISH_OFF;
62         }
63
64         ret2 = vconf_get_int(VCONF_KEY_DEFAULT_SLOT_SIZE, &mail_slot_count);
65
66         if (ret2 < 0) {
67                 EM_DEBUG_EXCEPTION("vconf_get_int() Failed(%d)", ret2);
68         mail_slot_count = 100;
69         }
70
71         ret = true;
72
73 FINISH_OFF: 
74         
75         if (output_count)
76                 *output_count = mail_slot_count;
77
78         if (err_code)
79                 *err_code = err;
80
81         return ret;
82         
83 }
84
85
86 EXPORT_API int em_core_mailbox_remove_overflowed_mails(emf_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
87 {
88         EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
89
90         int ret = false; 
91         int *mail_id_list = NULL, mail_id_list_count = 0;
92         int err = EMF_ERROR_NONE;
93         emf_account_t *account_ref = NULL;
94         
95         if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
96                 if (intput_mailbox_tbl)
97                 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
98                 err = EMF_ERROR_INVALID_PARAM;
99                 goto FINISH_OFF;
100         }
101
102         account_ref = em_core_get_account_reference(intput_mailbox_tbl->account_id);
103         if (account_ref) {
104                 if (account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
105                         EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
106                         err = EMF_ERROR_NOT_SUPPORTED;
107                         goto FINISH_OFF;
108                 }
109         }
110         
111         if (!em_storage_get_overflowed_mail_id_list(intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_name, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
112                 if (err == EM_STORAGE_ERROR_MAIL_NOT_FOUND) {
113                         EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
114                         err = EMF_ERROR_NONE;
115                         ret = true;
116                 }
117                 else
118                         EM_DEBUG_EXCEPTION("em_storage_get_overflowed_mail_id_list failed [%d]", err);
119                 goto FINISH_OFF;
120         }
121
122         if (mail_id_list) {
123                 if (!em_core_mail_delete(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, false, EMF_DELETED_BY_OVERFLOW, false, &err)) {
124                         EM_DEBUG_EXCEPTION("em_core_mail_delete failed [%d]", err);
125                         goto FINISH_OFF;
126                 }
127         }
128         
129         ret = true;
130 FINISH_OFF: 
131         EM_SAFE_FREE(mail_id_list);
132
133         if (err_code)
134                 *err_code = err;
135
136         EM_DEBUG_FUNC_END("ret [%d]", ret);
137         return ret;
138 }
139
140
141 EXPORT_API int em_core_mailbox_set_mail_slot_size(int account_id, char *mailbox_name, int new_slot_size, int *err_code)
142 {
143         EM_DEBUG_FUNC_BEGIN();
144         EM_DEBUG_LOG("account_id [%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
145
146         int ret = false, err = EMF_ERROR_NONE; 
147         int i, mailbox_count = 0;
148         emf_account_t *account_ref = NULL;
149         emf_mailbox_tbl_t *mailbox_tbl_list = NULL;
150
151         if (account_id > ALL_ACCOUNT) {
152                 account_ref = em_core_get_account_reference(account_id);
153                 if (account_ref && account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
154                         EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
155                         ret = true;
156                         goto FINISH_OFF;
157                 }
158                 else if (!account_ref) {
159                         EM_DEBUG_EXCEPTION("em_core_get_account_reference failed");
160                         goto FINISH_OFF;
161                 }
162         }
163
164         if (!em_storage_set_mail_slot_size(account_id, mailbox_name, new_slot_size, true, &err)) {
165                 EM_DEBUG_EXCEPTION("em_storage_set_mail_slot_size failed [%d]", err);
166                 goto FINISH_OFF;
167         }
168
169         if (mailbox_name) {
170                 mailbox_count = 1;
171                 if (new_slot_size > 0) {
172                         mailbox_tbl_list = em_core_malloc(sizeof(emf_mailbox_tbl_t) * mailbox_count);
173                         if(!mailbox_tbl_list) {
174                                 EM_DEBUG_EXCEPTION("em_core_malloc failed");
175                                 goto FINISH_OFF;
176                         }
177                         mailbox_tbl_list->account_id = account_id;
178                         mailbox_tbl_list->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
179                         mailbox_tbl_list->mail_slot_size = new_slot_size;     
180                 }
181                 else   {        /*  read information from DB */
182                         if (!em_storage_get_mailbox_by_name(account_id, EMF_MAILBOX_ALL, mailbox_name, &mailbox_tbl_list, true, &err)) {
183                                 EM_DEBUG_EXCEPTION("em_storage_get_mailbox failed [%d]", err);
184                                 goto FINISH_OFF;
185                         }
186                         
187                 }
188         }
189         else {
190                 if (!em_storage_get_mailbox(account_id, EMF_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
191                         EM_DEBUG_EXCEPTION("em_storage_get_mailbox failed [%d]", err);
192                         goto FINISH_OFF;
193                 }
194         }
195
196         for (i = 0; i < mailbox_count; i++) {
197                 if (!em_core_mailbox_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
198                         if (err == EM_STORAGE_ERROR_MAIL_NOT_FOUND || err == EMF_ERROR_NOT_SUPPORTED)
199                                 err = EMF_ERROR_NONE;
200                         else
201                                 EM_DEBUG_EXCEPTION("em_core_mailbox_remove_overflowed_mails failed [%d]", err);
202                 }
203         }
204
205         
206
207         ret = true;
208 FINISH_OFF: 
209
210         if (mailbox_tbl_list)
211                 em_storage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
212
213
214         if (err_code)
215                 *err_code = err;
216         return ret;
217 }
218
219
220 static int em_core_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
221 {
222     emf_account_t *ref_account = NULL;
223         size_t path_len = 0;
224
225
226         ref_account = em_core_get_account_reference(account_id);
227         if (!ref_account)        {
228                 EM_DEBUG_EXCEPTION("em_core_get_account_reference failed");
229                 return 0;
230         }
231
232         path_len = strlen(ref_account->receiving_server_addr) +
233                           (mailbox_name ? strlen(mailbox_name) : 0) + 50;
234
235     *path = em_core_malloc(path_len);/* strlen(ref_account->receiving_server_addr) + */
236                           /* (mailbox_name ? strlen(mailbox_name) : 0) + 20); */
237     if (!*path)
238         return 0;
239         memset(*path, 0x00, path_len);
240     /* 1. server address / server type */
241
242     if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
243         SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->receiving_server_addr, ref_account->port_num);
244     }
245     else {
246         SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->receiving_server_addr, ref_account->port_num);
247     }
248
249     /* 2. set tls option if security connection */
250 /*     if (ref_account->use_security) strncat(*path + 1, "/tls", path_len-(strlen(*path)-1)); */
251         if (ref_account->use_security & 0x01) {
252                 strncat(*path + 1, "/ssl", path_len-(strlen(*path)-1));
253         }
254         if (ref_account->use_security & 0x02)
255                 strncat(*path + 1, "/tls", path_len-(strlen(*path)-1));
256         else
257                 strncat(*path + 1, "/notls", path_len-(strlen(*path)-1));
258
259     /*  3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
260     strncat(*path + 1, "}", path_len-strlen(*path)-1);
261     **path = '{';
262
263     if (mailbox_name) strncat(*path, mailbox_name, path_len-strlen(*path)-1);
264
265     return 1;
266 }
267
268 EXPORT_API int em_core_mailbox_sync_mailbox_list(int account_id, char *mailbox_name, int *err_code)
269 {
270         EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
271         
272         int ret = false;
273         int err = EMF_ERROR_NONE;
274         int status = EMF_DOWNLOAD_FAIL;
275         
276         MAILSTREAM *stream = NULL;
277         emf_mailbox_t *mailbox_list = NULL;
278         emf_account_t *ref_account = NULL;
279         void *tmp_stream = NULL;
280         char *mbox_path = NULL;
281         int i = 0, count = 0, counter = 0, mailbox_type_list[EMF_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
282         char *mailbox_name_for_mailbox_type = NULL;
283
284         
285         if (err_code) 
286                 *err_code = EMF_ERROR_NONE;
287         
288         if (!em_core_check_thread_status())  {
289                 err = EMF_ERROR_CANCELLED;
290                 goto FINISH_OFF;
291         }
292         
293         ref_account = em_core_get_account_reference(account_id);
294         if (!ref_account)  {
295                 EM_DEBUG_EXCEPTION("em_core_get_account_reference failed - %d", account_id);
296                 err = EMF_ERROR_INVALID_ACCOUNT;
297                 goto FINISH_OFF;
298         }
299         
300         /* if not imap4 mail, return */
301         if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE || ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)  {
302                 EM_DEBUG_EXCEPTION("unsupported account...");
303                 err = EMF_ERROR_INVALID_ACCOUNT;
304                 goto FINISH_OFF;
305         }
306         
307         /*  get mail server path */
308         /*  mbox_path is not used. the below func might be unnecessary */
309         if (!em_core_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path)  {
310                 EM_DEBUG_EXCEPTION("em_core_get_mailbox_connection_path - %d", err);
311                 goto FINISH_OFF;
312         }
313         
314         
315         if (!em_core_check_thread_status())  {
316                 err = EMF_ERROR_CANCELLED;
317                 goto FINISH_OFF;
318         }
319
320         stream = NULL;
321         if (!em_core_mailbox_open(account_id, NULL, (void **)&tmp_stream, &err) || !tmp_stream)  {
322                 EM_DEBUG_EXCEPTION("em_core_mailbox_open failed - %d", err);
323                 
324                 if (err == EMF_ERROR_CONNECTION_BROKEN)
325                         err = EMF_ERROR_CANCELLED;
326                 else
327                         err = EMF_ERROR_CONNECTION_FAILURE;
328                 
329                 status = EMF_DOWNLOAD_CONNECTION_FAIL;
330                 goto FINISH_OFF;
331         }
332         
333         EM_SAFE_FREE(mbox_path);
334         
335         stream = (MAILSTREAM *)tmp_stream;
336         
337         if (!em_core_check_thread_status())  {
338                 err = EMF_ERROR_CANCELLED;
339                 goto FINISH_OFF;
340         }
341         
342         /*  download mailbox list */
343         if (!em_core_mailbox_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err))  {
344                 EM_DEBUG_EXCEPTION("em_core_mailbox_download_mailbox_list failed - %d", err);
345                 goto FINISH_OFF;
346         }
347
348         if (!em_core_check_thread_status())  {
349                 err = EMF_ERROR_CANCELLED;
350                 goto FINISH_OFF;
351         }
352         
353         for (i = 0; i < count; i++) {
354                 if (!em_core_check_thread_status())  {
355                         EM_DEBUG_LOG("em_core_check_thread_status - cancelled");
356                         err = EMF_ERROR_CANCELLED;
357                         goto FINISH_OFF;
358                 }
359                 if (mailbox_list[i].name) {
360                         EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].name);
361                         em_core_mailbox_get_default_mail_slot_count(&(mailbox_list[i].mail_slot_size), NULL);
362                         em_core_bind_mailbox_type(mailbox_list + i);
363
364                         if (mailbox_list[i].mailbox_type <= EMF_MAILBOX_TYPE_ALL_EMAILS) {      /*  if result mailbox type is duplicated,  */
365                                 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
366                                         EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].name);
367                                         mailbox_list[i].mailbox_type = EMF_MAILBOX_TYPE_USER_DEFINED; /*  ignore latest one  */
368                                 }
369                                 else
370                                         mailbox_type_list[mailbox_list[i].mailbox_type] = i;
371                         }
372
373                         EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
374                         if(!em_core_mailbox_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
375                                 EM_DEBUG_EXCEPTION("em_core_mailbox_set_sync_imap_mailbox failed [%d]", err);
376                                 goto FINISH_OFF;
377                         }
378
379                 }
380         }
381
382
383         for (counter = EMF_MAILBOX_TYPE_INBOX; counter <= EMF_MAILBOX_TYPE_OUTBOX; counter++) {
384                 /* if (!em_storage_get_mailboxname_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err))  */
385                 if (mailbox_type_list[counter] == -1) {
386                         /* EM_DEBUG_EXCEPTION("em_storage_get_mailboxname_by_mailbox_type failed - %d", err); */
387                         /* if (EM_STORAGE_ERROR_MAILBOX_NOT_FOUND == err)        */
388                         /* { */
389                                 emf_mailbox_tbl_t mailbox_tbl;
390                                 
391                                 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
392                                 
393                                 mailbox_tbl.account_id = account_id;
394                                 mailbox_tbl.mailbox_id = 0;
395                                 mailbox_tbl.local_yn = 1; 
396                                 mailbox_tbl.mailbox_type = counter;
397                                 mailbox_tbl.sync_with_server_yn =  1;
398                                 mailbox_tbl.modifiable_yn = 1; 
399                                 mailbox_tbl.total_mail_count_on_server = 0;
400                                 em_core_mailbox_get_default_mail_slot_count(&mailbox_tbl.mail_slot_size, NULL);
401                                 
402                                 switch (counter) {
403                                         case EMF_MAILBOX_TYPE_SENTBOX:
404                                                 mailbox_tbl.mailbox_name = EMF_SENTBOX_NAME;
405                                                 mailbox_tbl.alias = EMF_SENTBOX_DISPLAY_NAME;
406                                                 break;
407                                                 
408                                         case EMF_MAILBOX_TYPE_TRASH:
409                                                 mailbox_tbl.mailbox_name = EMF_TRASH_NAME;
410                                                 mailbox_tbl.alias = EMF_TRASH_DISPLAY_NAME;
411                                                 break;
412
413                                     case EMF_MAILBOX_TYPE_DRAFT:
414                                                 mailbox_tbl.mailbox_name = EMF_DRAFTBOX_NAME;
415                                                 mailbox_tbl.alias = EMF_DRAFTBOX_DISPLAY_NAME;
416                                                 break;
417                                                 
418                                         case EMF_MAILBOX_TYPE_SPAMBOX:
419                                                 mailbox_tbl.mailbox_name = EMF_SPAMBOX_NAME;
420                                                 mailbox_tbl.alias = EMF_SPAMBOX_DISPLAY_NAME;
421                                                 break;
422                                                 
423                                         case EMF_MAILBOX_TYPE_OUTBOX:
424                                                 mailbox_tbl.mailbox_name = EMF_OUTBOX_NAME;
425                                                 mailbox_tbl.alias = EMF_OUTBOX_DISPLAY_NAME;
426                                                 break;
427
428                                         default: 
429                                                 mailbox_tbl.mailbox_name = EMF_INBOX_NAME;
430                                                 mailbox_tbl.alias = EMF_INBOX_DISPLAY_NAME;
431                                                 break;
432                                 }
433
434                                 if (!em_storage_add_mailbox(&mailbox_tbl, true, &err)) {
435                                         EM_DEBUG_EXCEPTION("em_storage_add_mailbox failed - %d", err);
436                                         
437                                         if (err == EM_STORAGE_ERROR_DB_IS_FULL)
438                                                 err = EMF_ERROR_MAIL_MEMORY_FULL;
439                                         else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
440                                                 err = EMF_ERROR_INVALID_PARAM;
441                                         else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
442                                                 err = EMF_ERROR_OUT_OF_MEMORY;
443                                         else if (err == EM_STORAGE_ERROR_DB_FAILURE)
444                                                 err = EMF_ERROR_DB_FAILURE;
445                                         else
446                                                 err = EMF_ERROR_UNKNOWN;
447                                         goto FINISH_OFF;
448                                 }
449                                 
450                         /* }     */
451                         /* else */
452                         /* { */
453                         /*      err = em_storage_get_emf_error_from_em_storage_error(err); */
454                         /*      goto FINISH_OFF; */
455                         /* } */
456                         
457                 }
458                 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
459         }
460
461         emf_mailbox_tbl_t *local_mailbox_list = NULL;
462         int select_num = 0;
463         i = 0;
464         emf_mailbox_t mailbox;
465
466         if (em_storage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
467                 if (select_num > 0) {
468                         for (i = 0; i < select_num; i++) {
469                                 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
470                                 mailbox.account_id = local_mailbox_list[i].account_id;
471                                 mailbox.name       = local_mailbox_list[i].mailbox_name;
472                                 if (!em_core_mailbox_delete_all(&mailbox, &err)) {
473                                         EM_DEBUG_EXCEPTION(" em_core_mailbox_delete_all of Mailbox [%s] Failed ", mailbox.name);
474                                         em_storage_free_mailbox(&local_mailbox_list, select_num, NULL); 
475                                         local_mailbox_list = NULL;
476                                         goto FINISH_OFF;
477                                 }
478                         }
479                         em_storage_free_mailbox(&local_mailbox_list, select_num, NULL); 
480                         local_mailbox_list = NULL;
481                 }
482         }
483
484         if (!em_storage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
485                         EM_DEBUG_EXCEPTION(" >>>> em_storage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
486                         goto FINISH_OFF;
487         }
488         
489         if (!em_core_check_thread_status())  {
490                 err = EMF_ERROR_CANCELLED;
491                 goto FINISH_OFF;
492         }
493         
494         for (i = 0; i < count; i++)
495                 mailbox_list[i].account_id = account_id;
496         
497         
498         ret = true;
499         
500 FINISH_OFF: 
501         EM_SAFE_FREE(mailbox_name_for_mailbox_type);
502         EM_SAFE_FREE(mbox_path);
503
504         if (stream) 
505                 em_core_mailbox_close(account_id, stream);
506         
507         if (mailbox_list) 
508                 em_core_mailbox_free(&mailbox_list, count, NULL);
509
510         if (err_code != NULL)
511                 *err_code = err;
512         EM_DEBUG_FUNC_END("ret [%d]", ret);
513         return ret;
514 }
515
516 int em_core_mailbox_download_mailbox_list(void *mail_stream, 
517                                                                                 char *mailbox, 
518                                                                                 emf_mailbox_t **mailbox_list, 
519                                                                                 int *count, 
520                                                                                 int *err_code)
521 {
522     MAILSTREAM *stream = mail_stream;
523     emf_callback_holder_t holder;
524     char *pattern = NULL;
525     char *reference = NULL;
526     int         err = EMF_ERROR_NONE;
527     int         ret = false;
528
529     EM_DEBUG_FUNC_BEGIN();
530
531         if (err_code) {
532                 *err_code = EMF_ERROR_NONE;
533         }
534
535         if (!stream || !mailbox_list || !count) {
536         err = EMF_ERROR_INVALID_PARAM;
537         goto FINISH_OFF;
538         }
539         
540         memset(&holder, 0x00, sizeof(holder));
541
542     /*  reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
543         if (mailbox)  {
544                 char *s = NULL;
545                 reference = em_core_malloc(strlen(stream->original_mailbox) + strlen(mailbox) + 1);
546                 if (reference) {
547                         strncpy(reference, stream->original_mailbox, (size_t)strlen(stream->original_mailbox));
548                         if ((s = strchr(reference, '}')))
549                                 *(++s) = '\0';
550                         strcat(reference, mailbox);
551                 }
552         }
553         else
554                 reference = EM_SAFE_STRDUP(stream->original_mailbox);
555
556         pattern = "*";
557     stream->sparep = &holder;
558
559         /*  imap command  :  tag LIST reference * */
560         mail_list(stream, reference, pattern);
561
562     stream->sparep = NULL;
563
564         EM_SAFE_FREE(reference);
565
566     *count = holder.num;
567     *mailbox_list = (emf_mailbox_t *)holder.data;
568
569         ret = true;
570
571 FINISH_OFF: 
572         if (err_code) *err_code = err;
573
574         return ret;
575 }
576
577 /* description
578  *    check whether this imap mailbox is synchronous mailbox
579  * arguments
580  *    mailbox  :  imap mailbox to be checked
581  *    synchronous  :   boolean variable to be synchronous (1 : sync 0 : non-sync)
582  * return
583  *    succeed  :  1
584  *    fail  :  0
585  */
586 int em_core_mailbox_check_sync_imap_mailbox(emf_mailbox_t *mailbox, int *synchronous, int *err_code)
587 {
588         EM_DEBUG_FUNC_BEGIN();
589         
590         EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
591         
592         if (err_code) {
593                 *err_code = EMF_ERROR_NONE;
594         }
595
596         if (!mailbox || !synchronous) {
597                 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
598                 
599                 if (err_code != NULL)
600                         *err_code = EMF_ERROR_INVALID_PARAM;
601                 return false;
602         }
603         
604         int ret = false;
605         int err = EMF_ERROR_NONE;
606         emf_mailbox_tbl_t *imap_mailbox_tbl = NULL;
607
608         if (!em_storage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl, true, &err))  {
609                 EM_DEBUG_EXCEPTION("em_storage_get_mailbox_by_name failed - %d", err);
610                 
611                 if (err == EM_STORAGE_ERROR_MAILBOX_NOT_FOUND)
612                         err = EMF_ERROR_MAILBOX_NOT_FOUND;
613                 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
614                         err = EMF_ERROR_INVALID_PARAM;
615                 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
616                         err = EMF_ERROR_OUT_OF_MEMORY;
617                 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
618                         err = EMF_ERROR_DB_FAILURE;
619                 else
620                         err = EMF_ERROR_UNKNOWN;
621                 goto FINISH_OFF;
622         }
623         
624         *synchronous = imap_mailbox_tbl ? 1  :  0;
625         
626         ret = true;
627         
628 FINISH_OFF: 
629         if (imap_mailbox_tbl != NULL)
630                 em_storage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
631         
632         if (err_code != NULL)
633                 *err_code = err;
634         
635         return ret;
636 }
637
638
639 /* description
640  *    set sync imap mailbox
641  * arguments
642  *    mailbox_list  :  imap mailbox to be synced
643  *    syncronous  :  0-sync 1 : non-sync
644  * return
645  *    succeed  :  1
646  *    fail  :  0
647  */
648
649 int em_core_mailbox_set_sync_imap_mailbox(emf_mailbox_t *mailbox, int synchronous, int *err_code)
650 {
651         EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
652         
653         if (!mailbox)  {
654                 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
655                 if (err_code != NULL)
656                         *err_code = EMF_ERROR_INVALID_PARAM;
657                 return false;
658         }
659         
660         int ret = false;
661         int err = EMF_ERROR_NONE;
662         emf_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
663         em_core_uid_list *uid_list = NULL;
664         emf_mail_read_mail_uid_tbl_t *downloaded_uids = NULL;
665         MAILSTREAM *stream = mailbox->mail_stream;
666         int mailbox_renamed = 0;
667         int j = 0;
668         int i = 0;
669         int temp = 0;
670         IMAPLOCAL *imap_local = NULL;
671         char cmd[128], tag[32], *p;     
672                 
673         if (synchronous) {              
674                 /* if synchcronous, insert imap mailbox to db */
675                 if (em_storage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl_item, true, &err))  {       
676                         /* mailbox already exists */
677                         /* mailbox Found, Do set the modifiable_yn = 1 */
678                         EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
679                         if (!em_storage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->name, 1, true, &err)) {
680                                 EM_DEBUG_EXCEPTION(" em_storage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
681                                 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
682                                         err = EMF_ERROR_INVALID_PARAM;
683                                 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
684                                         err = EMF_ERROR_OUT_OF_MEMORY;
685                                 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
686                                         err = EMF_ERROR_DB_FAILURE;
687                                 else
688                                         err = EMF_ERROR_UNKNOWN;
689                                 goto JOB_ERROR;
690                         }
691                 }
692                 else {
693                         if (err != EM_STORAGE_ERROR_MAILBOX_NOT_FOUND) {
694                                 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
695                                 /* This is error scenario so finish the job */
696                                 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
697                                         err = EMF_ERROR_INVALID_PARAM;
698                                 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
699                                         err = EMF_ERROR_OUT_OF_MEMORY;
700                                 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
701                                         err = EMF_ERROR_DB_FAILURE;
702                                 else
703                                         err = EMF_ERROR_UNKNOWN;
704                                 goto JOB_ERROR;
705                         }
706                         else {
707                                 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
708                                 /* Now check if mailbox is renamed */
709                                 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
710                                 if (stream) {
711                                         imap_local = ((MAILSTREAM *)stream)->local;
712                                         EM_DEBUG_LINE;
713                                         sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
714                                         EM_DEBUG_LINE;
715                                         sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->name);
716                                         EM_DEBUG_LINE;
717
718                                 }
719                                 
720                                 /* select the mailbox and get its UID */
721                                 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)strlen(cmd))) {
722                                         EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->name);
723                                         /*
724                                         err = EMF_ERROR_CONNECTION_BROKEN;
725                                         if(imap_local)
726                                                 imap_local->netstream = NULL;
727                                         mailbox->mail_stream = NULL;
728                                         goto JOB_ERROR;
729                                         */
730                                 }
731                                 else {
732                                         EM_DEBUG_LOG("Get response for select call");
733                                         while (imap_local->netstream) {
734                                         p = net_getline(imap_local->netstream);
735                                                 EM_DEBUG_LOG("p =[%s]", p);
736                                                 if (!strncmp(p, "+", 1)) {
737                                                         ret = 1;
738                                                         break;
739                                                 }
740                                                 else if (!strncmp(p, "*", 1)) {
741                                                 EM_SAFE_FREE(p); 
742                                                 continue;
743                                                 }
744                                                 else {
745                                                         ret = 0;
746                                                         break;
747                                                 }
748                                 }
749                                         EM_SAFE_FREE(p); 
750                                         EM_DEBUG_LINE;
751                                         /* check if OK or BAD response comes. */
752                                         /* if response is OK the try getting UID list. */
753                                         if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK")))  {
754                                                 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->name);
755                                                 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
756                                                         EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
757                                                         EM_SAFE_FREE(uid_list);
758                                                 }
759                                                 else {
760                                                         if (!em_storage_get_downloaded_list(mailbox->account_id, NULL, &downloaded_uids, &j, true, &err)) {
761                                                                 EM_DEBUG_EXCEPTION("em_storage_get_downloaded_list failed [%d]", err);
762                                                                 err = em_storage_get_emf_error_from_em_storage_error(err);
763                                                                 downloaded_uids = NULL;
764                                                         }
765                                                         else /* Prevent Defect - 28497 */ {
766                                                                 em_core_uid_list *uid_elem = uid_list;
767                                                                 em_core_uid_list *next_uid_elem = NULL;
768                                                                 if (uid_elem) {
769                                                                         for (i = j; (i > 0 && !mailbox_renamed); i--)  {
770                                                                                 if (uid_elem) {
771                                                                                         next_uid_elem = uid_elem->next;
772                                                                                         if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
773                                                                                                 temp = i-1;
774                                                                                                 mailbox_renamed = 1;
775                                                                                                 break;
776                                                                                         }
777                                                                                         EM_SAFE_FREE(uid_elem->uid);
778                                                                                         uid_elem = next_uid_elem;
779                                                                                 }
780                                                                         }
781                                                                 }
782                                                         }
783                                                 }
784                                         } /* mailbox selected */
785                                 }       
786
787                                 if (mailbox_renamed) /* renamed mailbox */ {
788                                         EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
789                                         /* Do a mailbox rename in the DB */
790                                         if (!em_storage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->name, true, &err))
791                                                 EM_DEBUG_EXCEPTION(" em_storage_modify_mailbox_of_mails Failed [%d]", err);
792
793                                         mailbox_renamed = 0;
794
795                                         emf_mailbox_tbl_t mailbox_tbl;
796                                         mailbox_tbl.account_id = mailbox->account_id;
797                                         mailbox_tbl.local_yn = 0;
798                                         mailbox_tbl.mailbox_name = mailbox->name;
799                                         mailbox_tbl.mailbox_type = mailbox->mailbox_type;
800
801                                         /* Get the Alias Name after parsing the Full mailbox Path */
802                                         mailbox->alias = em_core_get_alias_of_mailbox((const char *)mailbox->name);
803                                         
804                                         if (mailbox->alias)
805                                                 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
806
807                                         mailbox_tbl.alias = mailbox->alias;
808                                         mailbox_tbl.sync_with_server_yn  = 0; 
809                                         mailbox_tbl.modifiable_yn = 1;
810                                         mailbox_tbl.total_mail_count_on_server = 0;
811                                         
812                                         /* if non synchronous, delete imap mailbox from db */
813                                         if (!em_storage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_name, &mailbox_tbl, true, &err)) {
814                                                 EM_DEBUG_EXCEPTION(" em_storage_update_mailbox Failed [ %d ] ", err);
815                                                 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
816                                                         err = EMF_ERROR_INVALID_PARAM;
817                                                 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
818                                                         err = EMF_ERROR_OUT_OF_MEMORY;
819                                                 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
820                                                         err = EMF_ERROR_DB_FAILURE;
821                                                 else
822                                                         err = EMF_ERROR_UNKNOWN;
823                                                 goto JOB_ERROR;
824                                         }
825                                         
826                                 }
827                                 else /* Its a Fresh Mailbox */ {
828                                         emf_mailbox_tbl_t mailbox_tbl;
829                                         mailbox_tbl.mailbox_id = mailbox->mailbox_id;
830                                         mailbox_tbl.account_id = mailbox->account_id;
831                                         mailbox_tbl.local_yn = 0; 
832                                         mailbox_tbl.mailbox_type = mailbox->mailbox_type;
833                                         mailbox_tbl.mailbox_name = mailbox->name;
834                                         mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
835
836                                         /* Get the Alias Name after Parsing the Full mailbox Path */
837                                         mailbox->alias = em_core_get_alias_of_mailbox((const char *)mailbox->name);
838
839                                         if (mailbox->alias) {
840                                                 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
841
842                                                 mailbox_tbl.alias = mailbox->alias;
843                                                 mailbox_tbl.sync_with_server_yn = 1;
844                                                 mailbox_tbl.modifiable_yn = 1; 
845                                                 mailbox_tbl.total_mail_count_on_server = 0;
846                                                         
847                                                 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
848
849                                                 if (!em_storage_add_mailbox(&mailbox_tbl, true, &err)) {
850                                                         EM_DEBUG_EXCEPTION("em_storage_add_mailbox failed - %d", err);
851                                                         
852                                                         if (err == EM_STORAGE_ERROR_DB_IS_FULL)
853                                                                 err = EMF_ERROR_MAIL_MEMORY_FULL;
854                                                         else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
855                                                                 err = EMF_ERROR_INVALID_PARAM;
856                                                         else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
857                                                                 err = EMF_ERROR_OUT_OF_MEMORY;
858                                                         else if (err == EM_STORAGE_ERROR_DB_FAILURE)
859                                                                 err = EMF_ERROR_DB_FAILURE;
860                                                         else
861                                                                 err = EMF_ERROR_UNKNOWN;
862                                                         goto JOB_ERROR;
863                                                 }
864                                         }
865                                 }
866                         }
867                 }
868         }
869
870         /* set sync db mailbox */
871         mailbox->synchronous = synchronous;
872         
873         ret = true;
874         
875 JOB_ERROR:
876
877         if (downloaded_uids) 
878                 em_storage_free_read_mail_uid(&downloaded_uids, j, NULL);
879
880         if (imap_mailbox_tbl_item)
881                 em_storage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
882
883         if (err_code)
884                 *err_code = err;
885         
886         return ret;
887 }
888
889 /* description
890  *    create a new imap mailbox
891  * arguments
892  *    new_mailbox  :  imap mailbox to be created
893  * return
894  *    succeed  :  1
895  *    fail  :  0
896  */
897 EXPORT_API int em_core_mailbox_create_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
898 {
899     MAILSTREAM *stream = NULL;
900     char *long_enc_path = NULL;
901     void *tmp_stream = NULL;
902     int ret = false;
903     int err = EMF_ERROR_NONE;
904
905     EM_DEBUG_FUNC_BEGIN();
906
907         if (err_code) {
908                 *err_code = EMF_ERROR_NONE;
909         }
910
911     if (!mailbox)
912     {
913         err = EMF_ERROR_INVALID_PARAM;
914         goto JOB_ERROR;
915     }
916
917     /* connect mail server */
918     stream = NULL;
919     if (!em_core_mailbox_open(mailbox->account_id, NULL, (void **)&tmp_stream, NULL))
920     {
921         err = EMF_ERROR_CONNECTION_FAILURE;
922         goto JOB_ERROR;
923     }
924
925     stream = (MAILSTREAM *)tmp_stream;
926
927     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
928     if (!em_core_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
929     {
930         err = EMF_ERROR_UNKNOWN;
931         goto JOB_ERROR;
932     }
933
934     /* create mailbox */
935     if (!mail_create(stream, long_enc_path))
936     {
937         err = EMF_ERROR_UNKNOWN;
938         goto JOB_ERROR;
939     }
940
941         em_core_mailbox_close(0, stream);                               
942         stream = NULL;
943
944     EM_SAFE_FREE(long_enc_path);
945
946     ret = true;
947
948 JOB_ERROR:
949     if (stream)
950     {
951                 em_core_mailbox_close(0, stream);                               
952                 stream = NULL;
953     }
954
955     EM_SAFE_FREE(long_enc_path);
956
957     if (err_code)
958         *err_code = err;
959
960     return ret;
961 }
962
963
964 /* description
965  *    delete a imap mailbox
966  * arguments
967  *    mailbox  :  mailbox to be deleted
968  * return
969  *    succeed  :  1
970  *    fail  :  0
971  */
972 EXPORT_API int em_core_mailbox_delete_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
973 {
974     MAILSTREAM *stream = NULL;
975     char *long_enc_path = NULL;
976     emf_account_t *ref_account = NULL;
977     void *tmp_stream = NULL;
978     int ret = false;
979     int err = EMF_ERROR_NONE;
980
981     EM_DEBUG_FUNC_BEGIN();
982
983         if (err_code) {
984                 *err_code = EMF_ERROR_NONE;
985         }
986
987     if (!mailbox)
988     {
989         err = EMF_ERROR_INVALID_PARAM;
990         goto JOB_ERROR;
991     }
992
993     ref_account = em_core_get_account_reference(mailbox->account_id);
994     if (!ref_account)
995     {
996         err = EMF_ERROR_INVALID_PARAM;
997         goto JOB_ERROR;
998     }
999
1000     /* if not imap4 mail, return */
1001     if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE || 
1002             ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
1003     {
1004         err = EMF_ERROR_INVALID_PARAM;
1005         goto JOB_ERROR;
1006     }
1007
1008     /* connect mail server */
1009     stream = NULL;
1010     if (!em_core_mailbox_open(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
1011     {
1012         err = EMF_ERROR_CONNECTION_FAILURE;
1013         goto JOB_ERROR;
1014     }
1015
1016     stream = (MAILSTREAM *)tmp_stream;
1017
1018     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1019     if (!em_core_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
1020     {
1021         err = EMF_ERROR_UNKNOWN;
1022         goto JOB_ERROR;
1023     }
1024
1025     /* delete mailbox */
1026     if (!mail_delete(stream, long_enc_path))
1027     {
1028         err = EMF_ERROR_UNKNOWN;
1029         goto JOB_ERROR;
1030     }
1031
1032         em_core_mailbox_close(0, stream);                               
1033         stream = NULL;
1034
1035     EM_SAFE_FREE(long_enc_path);
1036
1037     /* if deleted imap mailbox is synchronous mailbox, delete db imap mailbox from db */
1038     if (!em_storage_delete_mailbox(ref_account->account_id, 0, mailbox->name, true, &err)) {
1039                 EM_DEBUG_EXCEPTION("\t em_storage_delete_mailbox failed - %d", err);
1040                 
1041                 if (err == EM_STORAGE_ERROR_MAILBOX_NOT_FOUND)
1042                         err = EMF_ERROR_MAILBOX_NOT_FOUND;
1043                 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
1044                         err = EMF_ERROR_INVALID_PARAM;
1045                 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
1046                         err = EMF_ERROR_OUT_OF_MEMORY;
1047                 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
1048                         err = EMF_ERROR_DB_FAILURE;
1049                 else
1050                         err = EMF_ERROR_UNKNOWN;
1051         goto JOB_ERROR;
1052     }
1053
1054     ret = true;
1055
1056 JOB_ERROR:
1057     if (stream)
1058     {
1059                 em_core_mailbox_close(0, stream);                               
1060                 stream = NULL;
1061     }
1062
1063     EM_SAFE_FREE(long_enc_path);
1064
1065     if (err_code)
1066         *err_code = err;
1067
1068     return ret;
1069 }
1070
1071
1072 /* description
1073  *    change imap mailbox name
1074  * arguments
1075  *    old_mailbox  :  previous mailbox
1076  *    new_mailbox  :  renamed mailbox
1077  * return
1078  *    succeed  :  1
1079  *    fail  :  0
1080  */
1081 EXPORT_API int em_core_mailbox_modify_imap_mailbox(emf_mailbox_t *old_mailbox,  emf_mailbox_t *new_mailbox, int *err_code)
1082 {
1083     MAILSTREAM *stream = NULL;
1084     char *long_enc_path = NULL;
1085     char *long_enc_path_new = NULL;
1086     emf_mailbox_tbl_t imap_mailbox_tbl_item;
1087     emf_account_t *ref_account = NULL;
1088     void *tmp_stream = NULL;
1089     int ret = false;
1090     int err = EMF_ERROR_NONE;
1091
1092     EM_DEBUG_FUNC_BEGIN();
1093
1094         if (err_code) {
1095                 *err_code = EMF_ERROR_NONE;
1096         }
1097
1098     if (!old_mailbox || !new_mailbox)
1099     {
1100         err = EMF_ERROR_INVALID_PARAM;
1101         goto JOB_ERROR;
1102     }
1103
1104     ref_account = em_core_get_account_reference(old_mailbox->account_id);
1105     if (!ref_account)
1106     {
1107         err = EMF_ERROR_INVALID_PARAM;
1108         goto JOB_ERROR;
1109     }
1110
1111     if (!em_core_get_account_reference(new_mailbox->account_id))
1112     {
1113         err = EMF_ERROR_INVALID_PARAM;
1114         goto JOB_ERROR;
1115     }
1116
1117     /* if not imap4 mail, return */
1118     if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE || 
1119             ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
1120     {
1121         err = EMF_ERROR_INVALID_PARAM;
1122         goto JOB_ERROR;
1123     }
1124
1125     /* connect mail server */
1126     stream = NULL;
1127     if (!em_core_mailbox_open(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
1128     {
1129         err = EMF_ERROR_CONNECTION_FAILURE;
1130         goto JOB_ERROR;
1131     }
1132
1133     stream = (MAILSTREAM *)tmp_stream;
1134
1135     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1136     if (!em_core_get_long_encoded_path(old_mailbox->account_id, old_mailbox->name, '/', &long_enc_path, err_code))
1137     {
1138         err = EMF_ERROR_UNKNOWN;
1139         goto JOB_ERROR;
1140     }
1141
1142     /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1143     if (!em_core_get_long_encoded_path(new_mailbox->account_id, new_mailbox->name, '/', &long_enc_path_new, err_code))
1144     {
1145         err = EMF_ERROR_UNKNOWN;
1146         goto JOB_ERROR;
1147     }
1148
1149     /* rename mailbox */
1150     if (!mail_rename(stream, long_enc_path, long_enc_path_new))
1151     {
1152         err = EMF_ERROR_UNKNOWN;
1153         goto JOB_ERROR;
1154     }
1155
1156     EM_SAFE_FREE(long_enc_path);
1157     EM_SAFE_FREE(long_enc_path_new);
1158
1159         em_core_mailbox_close(0, stream);                               
1160         stream = NULL;
1161
1162     imap_mailbox_tbl_item.alias = NULL;
1163     imap_mailbox_tbl_item.mailbox_name = new_mailbox->name;
1164     imap_mailbox_tbl_item.modifiable_yn = 1;
1165
1166     /* if modified imap mailbox is synchronous mailbox, update mailbox name from imap mailbox table */
1167     if (!em_storage_update_mailbox(ref_account->account_id, 0, old_mailbox->name, &imap_mailbox_tbl_item, true, &err))
1168     {
1169         EM_DEBUG_EXCEPTION("em_storage_update_mailbox failed - %d", err);
1170         if (err == EM_STORAGE_ERROR_DB_IS_FULL)
1171                         err = EMF_ERROR_MAIL_MEMORY_FULL;
1172                 else
1173                 err = EMF_ERROR_DB_FAILURE;
1174         goto JOB_ERROR;
1175     }
1176
1177     ret = true;
1178
1179 JOB_ERROR:
1180     EM_SAFE_FREE(long_enc_path);
1181     EM_SAFE_FREE(long_enc_path_new);
1182
1183     if (stream)
1184     {
1185                 em_core_mailbox_close(0, stream);                               
1186                 stream = NULL;
1187     }
1188
1189     if (err_code)
1190         *err_code = err;
1191
1192     return ret;
1193 }