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