4a96d7725d1d31efa5bacc2b655bd8bdd3d18bdf
[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
738         EM_DEBUG_LOG("count: [%d], mailbox_list[%p]", *count, *mailbox_list);
739         if (*count <= 0 || *mailbox_list == NULL) {
740                 err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
741                 ret = false;
742         } else {
743                 ret = true;
744
745                 /* checking once more if there is a INBOX or not */
746                 int i = 0;
747                 bool is_found_inbox = false;
748                 for (i = 0; i < *count; i++) {
749                         if ((*mailbox_list)[i].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX || !g_ascii_strcasecmp((*mailbox_list)[i].mailbox_name, "INBOX")) {
750                                 is_found_inbox = true;
751                                 break;
752                         }
753                 }
754                 if (!is_found_inbox) {
755                         EM_DEBUG_CRITICAL_EXCEPTION("There is no INBOX!");
756                         err = EMAIL_ERROR_MAILBOX_NOT_FOUND;
757                         ret = false;
758                 }
759         }
760
761 FINISH_OFF:
762         if (err_code)
763                 *err_code = err;
764
765         return ret;
766 }
767
768 /* description
769  *    create a new imap mailbox
770  * arguments
771  *    new_mailbox  :  imap mailbox to be created
772  * return
773  *    succeed  :  1
774  *    fail  :  0
775  */
776 INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
777 {
778         MAILSTREAM *stream = NULL;
779         char *long_enc_path = NULL;
780         void *tmp_stream = NULL;
781         int ret = false;
782         int err = EMAIL_ERROR_NONE;
783         email_session_t *session = NULL;
784
785         EM_DEBUG_FUNC_BEGIN();
786
787         if (!mailbox) {
788                 err = EMAIL_ERROR_INVALID_PARAM;
789                 goto FINISH_OFF;
790         }
791
792         if (!emcore_get_empty_session(&session))
793                 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
794
795         /* connect mail server */
796         stream = NULL;
797         if (!emcore_connect_to_remote_mailbox(multi_user_name,
798                                                                                         mailbox->account_id,
799                                                                                         0,
800                                                                                         true,
801                                                                                         (void **)&tmp_stream,
802                                                                                         &err)) {
803                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
804                 goto FINISH_OFF;
805         }
806
807         stream = (MAILSTREAM *) tmp_stream;
808
809         /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
810         if (!emcore_get_long_encoded_path(multi_user_name, mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
811                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
812                 goto FINISH_OFF;
813         }
814
815         /* create mailbox */
816         if (!mail_create(stream, long_enc_path)) {
817                 EM_DEBUG_EXCEPTION("mail_create failed");
818
819                 if (!emcore_get_current_session(&session)) {
820                         EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
821                         err = EMAIL_ERROR_SESSION_NOT_FOUND;
822                         goto FINISH_OFF;
823                 }
824
825                 if (session->error == EMAIL_ERROR_ALREADY_EXISTS)
826                         err = session->error;
827                 else
828                         err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
829
830                 goto FINISH_OFF;
831         }
832
833         EM_SAFE_FREE(long_enc_path);
834
835         ret = true;
836
837 FINISH_OFF:
838         if (stream) {
839                 stream = mail_close(stream);
840         }
841
842         EM_SAFE_FREE(long_enc_path);
843
844         if (mailbox) {
845                 if (err == EMAIL_ERROR_NONE) {
846                         if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
847                                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
848                 } else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
849                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
850         }
851
852         emcore_clear_session(session);
853
854         if (err_code)
855                 *err_code = err;
856
857         return ret;
858 }
859
860
861 /* description
862  *    delete a imap mailbox
863  * arguments
864  *    input_mailbox_id  :  mailbox ID to be deleted
865  * return
866  *    succeed  :  1
867  *    fail  :  0
868  */
869 INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code)
870 {
871         EM_DEBUG_FUNC_BEGIN();
872
873         MAILSTREAM *stream = NULL;
874         char *long_enc_path = NULL;
875         email_account_t *ref_account = NULL;
876         void *tmp_stream = NULL;
877         int ret = false;
878         int err = EMAIL_ERROR_NONE;
879         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
880
881         if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
882                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
883
884         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
885                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
886                 goto FINISH_OFF;
887         }
888
889         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
890         if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
891                 EM_DEBUG_EXCEPTION("Invalid account information");
892                 err = EMAIL_ERROR_INVALID_ACCOUNT;
893                 goto FINISH_OFF;
894         }
895
896         /* connect mail server */
897         if (!emcore_connect_to_remote_mailbox(multi_user_name,
898                                                                                         mailbox_tbl->account_id,
899                                                                                         0,
900                                                                                         true,
901                                                                                         (void **)&tmp_stream,
902                                                                                         &err)) {
903                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
904                 goto FINISH_OFF;
905         }
906
907         stream = (MAILSTREAM *)tmp_stream;
908
909         /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
910         if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
911                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
912                 goto FINISH_OFF;
913         }
914
915         /* delete mailbox */
916         if (!mail_delete(stream, long_enc_path)) {
917                 EM_DEBUG_EXCEPTION("mail_delete failed");
918                 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
919                 goto FINISH_OFF;
920         }
921
922         EM_SAFE_FREE(long_enc_path);
923
924         ret = true;
925
926 FINISH_OFF:
927         if (stream) {
928                 stream = mail_close(stream);
929         }
930
931         if (ref_account) {
932                 emcore_free_account(ref_account);
933                 EM_SAFE_FREE(ref_account);
934         }
935
936         EM_SAFE_FREE(long_enc_path);
937
938         if (mailbox_tbl)
939                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
940
941         if (err == EMAIL_ERROR_NONE) {
942                 if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
943                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
944         } else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
945                 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
946
947         if (err_code)
948                 *err_code = err;
949
950         return ret;
951 }
952
953
954 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)
955 {
956         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);
957         MAILSTREAM *stream = NULL;
958         char *long_enc_path_old = NULL;
959         char *long_enc_path_new = NULL;
960         email_account_t *ref_account = NULL;
961         void *tmp_stream = NULL;
962         int err = EMAIL_ERROR_NONE;
963
964         if (!input_old_mailbox_path || !input_new_mailbox_path) {
965                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
966                 err = EMAIL_ERROR_INVALID_PARAM;
967                 goto FINISH_OFF;
968         }
969
970         ref_account = emcore_get_account_reference(multi_user_name, input_account_id, false);
971         if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
972                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
973                 err = EMAIL_ERROR_INVALID_ACCOUNT;
974                 goto FINISH_OFF;
975         }
976
977         /* connect mail server */
978         stream = NULL;
979         if (!emcore_connect_to_remote_mailbox(multi_user_name,
980                                                                                         input_account_id,
981                                                                                         0,
982                                                                                         true,
983                                                                                         (void **)&tmp_stream,
984                                                                                         &err)) {
985                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
986                 goto FINISH_OFF;
987         }
988
989         stream = (MAILSTREAM *)tmp_stream;
990
991         /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
992         if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
993                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
994                 goto FINISH_OFF;
995         }
996
997         /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
998         if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
999                 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1000                 goto FINISH_OFF;
1001         }
1002
1003         /* rename mailbox */
1004         if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1005                 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
1006                 goto FINISH_OFF;
1007         }
1008
1009 FINISH_OFF:
1010
1011         if (err == EMAIL_ERROR_NONE) {
1012                 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
1013                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
1014         } else {
1015                 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
1016                         EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
1017         }
1018         EM_SAFE_FREE(long_enc_path_old);
1019         EM_SAFE_FREE(long_enc_path_new);
1020
1021         if (ref_account) {
1022                 emcore_free_account(ref_account);
1023                 EM_SAFE_FREE(ref_account);
1024         }
1025
1026         if (stream) {
1027                 stream = mail_close(stream);
1028         }
1029
1030         EM_DEBUG_FUNC_END("err [%d]", err);
1031         return err;
1032 }
1033
1034
1035 #ifdef __FEATURE_IMAP_QUOTA__
1036
1037 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
1038 {
1039         EM_DEBUG_FUNC_BEGIN();
1040         quota_t ret_quota = NIL;
1041         EM_DEBUG_FUNC_END();
1042         return ret_quota;
1043 }
1044
1045 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
1046 {
1047         EM_DEBUG_FUNC_BEGIN();
1048         quota_t ret_quota = NIL;
1049         EM_DEBUG_FUNC_END();
1050         return ret_quota;
1051 }
1052
1053
1054 INTERNAL_FUNC int emcore_register_quota_callback()
1055 {
1056         EM_DEBUG_FUNC_BEGIN();
1057         int err = EMAIL_ERROR_NONE;
1058
1059         mail_parameters(NULL, SET_QUOTAROOT, callback_for_get_quota_root); /* set callback function for handling quota root message */
1060         mail_parameters(NULL, SET_QUOTA,     callback_for_get_quota);      /* set callback function for handling quota message */
1061
1062         EM_DEBUG_FUNC_END("err [%d]", err);
1063         return err;
1064 }
1065
1066 INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resource_t *output_list_of_resource_limits)
1067 {
1068         EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_list_of_resource_limits[%p]", input_mailbox_id, output_list_of_resource_limits);
1069         int err = EMAIL_ERROR_NONE;
1070         MAILSTREAM *stream = NULL;
1071         email_account_t *ref_account = NULL;
1072         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1073
1074         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1075                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1076                 goto FINISH_OFF;
1077         }
1078
1079         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1080         if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1081                 EM_DEBUG_EXCEPTION("Invalid account information");
1082                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1083                 goto FINISH_OFF;
1084         }
1085
1086         /* connect mail server */
1087         if (!emcore_connect_to_remote_mailbox(multi_user_name,
1088                                                                                         mailbox_tbl->account_id,
1089                                                                                         0,
1090                                                                                         true,
1091                                                                                         (void **)&stream,
1092                                                                                         &err)) {
1093                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1094                 goto FINISH_OFF;
1095         }
1096
1097         imap_getquotaroot(stream, mailbox_tbl->mailbox_name);
1098
1099 FINISH_OFF:
1100
1101         EM_DEBUG_FUNC_END("err [%d]", err);
1102         return err;
1103 }
1104
1105 INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *output_list_of_resource_limits)
1106 {
1107         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);
1108         int err = EMAIL_ERROR_NONE;
1109         MAILSTREAM *stream = NULL;
1110         email_account_t *ref_account = NULL;
1111         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1112
1113         if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1114                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1115                 goto FINISH_OFF;
1116         }
1117
1118         ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1119
1120         if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1121                 EM_DEBUG_EXCEPTION("Invalid account information");
1122                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1123                 goto FINISH_OFF;
1124         }
1125
1126         /* connect mail server */
1127         if (!emcore_connect_to_remote_mailbox(multi_user_name,
1128                                                                                         mailbox_tbl->account_id,
1129                                                                                         0,
1130                                                                                         true,
1131                                                                                         (void **)&stream,
1132                                                                                         &err)) {
1133                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1134                 goto FINISH_OFF;
1135         }
1136
1137         imap_getquota(stream, input_quota_root);
1138
1139 FINISH_OFF:
1140         EM_DEBUG_FUNC_END("err [%d]", err);
1141         return err;
1142 }
1143
1144 INTERNAL_FUNC int emcore_set_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *input_list_of_resource_limits)
1145 {
1146         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);
1147         int err = EMAIL_ERROR_NONE;
1148         /* TODO : set quota using the function 'imap_setquota' */
1149
1150         EM_DEBUG_FUNC_END("err [%d]", err);
1151         return err;
1152 }
1153
1154 #endif /* __FEATURE_IMAP_QUOTA__ */