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