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