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