4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 /******************************************************************************
25 * File : email-core-imap_folder.c
26 * Desc : Mail IMAP mailbox
31 * 2006.08.01 : created
32 *****************************************************************************/
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"
53 #include "email-debug-log.h"
55 INTERNAL_FUNC int emcore_get_default_mail_slot_count(char *multi_user_name, int input_account_id, int *output_count)
57 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] output_count[%p]", input_account_id, output_count);
59 int err = EMAIL_ERROR_NONE;
60 int default_mail_slot_count = 25;
61 email_account_t *account_ref = NULL;
63 if (output_count == NULL) {
64 err = EMAIL_ERROR_INVALID_PARAM;
68 account_ref = emcore_get_account_reference(multi_user_name, input_account_id, false);
70 default_mail_slot_count = account_ref->default_mail_slot_size;
75 emcore_free_account(account_ref);
76 EM_SAFE_FREE(account_ref);
80 *output_count = default_mail_slot_count;
82 EM_DEBUG_FUNC_END("err[%d]", err);
87 INTERNAL_FUNC int emcore_remove_overflowed_mails(char *multi_user_name,
88 emstorage_mailbox_tbl_t *input_mailbox_tbl,
91 EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], err_code[%p]", input_mailbox_tbl, err_code);
94 int *mail_id_list = NULL, mail_id_list_count = 0;
95 int err = EMAIL_ERROR_NONE;
96 email_account_t *account_ref = NULL;
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;
105 account_ref = emcore_get_account_reference(multi_user_name, input_mailbox_tbl->account_id, false);
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;
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,
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;
128 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
134 if (!emcore_delete_mails_from_local_storage(multi_user_name,
135 input_mailbox_tbl->account_id,
138 EMAIL_DELETE_LOCALLY,
139 EMAIL_DELETED_BY_COMMAND,
141 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
150 EM_SAFE_FREE(mail_id_list);
153 emcore_free_account(account_ref);
154 EM_SAFE_FREE(account_ref);
160 EM_DEBUG_FUNC_END("ret [%d]", ret);
165 INTERNAL_FUNC int emcore_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *err_code)
167 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
169 int ret = false, err = EMAIL_ERROR_NONE;
171 int account_count = 100;
172 int mailbox_count = 0;
173 email_account_t *account_ref = NULL;
174 emstorage_account_tbl_t *account_tbl_list = NULL;
175 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
177 if (account_id > ALL_ACCOUNT) {
178 account_ref = emcore_get_account_reference(multi_user_name, account_id, false);
179 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
180 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
184 else if (!account_ref) {
185 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
188 if (mailbox_id == 0) {
189 if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
190 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
196 if (mailbox_id == 0) {
197 if ( !emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_list, false, false, &err)) {
198 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
201 for ( i = 0; i < account_count; i++) {
202 if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
203 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
210 if (!emstorage_set_mail_slot_size(multi_user_name, account_id, mailbox_id, new_slot_size, true, &err)) {
211 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
217 if (new_slot_size > 0) {
218 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
219 if(!mailbox_tbl_list) {
220 EM_DEBUG_EXCEPTION("em_malloc failed");
223 mailbox_tbl_list->account_id = account_id;
224 mailbox_tbl_list->mailbox_id = mailbox_id;
225 mailbox_tbl_list->mail_slot_size = new_slot_size;
227 else { /* read information from DB */
228 if ((err = emstorage_get_mailbox_by_id(multi_user_name, mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
229 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
236 if (!emstorage_get_mailbox_list(multi_user_name, account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
237 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
242 for (i = 0; i < mailbox_count; i++) {
243 if (!emcore_remove_overflowed_mails(multi_user_name, mailbox_tbl_list + i, &err)) {
244 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
245 err = EMAIL_ERROR_NONE;
247 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
256 emcore_free_account(account_ref);
257 EM_SAFE_FREE(account_ref);
260 if (account_tbl_list)
261 emstorage_free_account(&account_tbl_list, account_count, NULL);
263 if (mailbox_tbl_list)
264 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
272 static int emcore_get_mailbox_connection_path(char *multi_user_name, int account_id, char *mailbox_name, char **path, int *err_code)
274 EM_DEBUG_FUNC_BEGIN_SEC("account_id [%d], mailbox_name[%s], err_code[%p]", account_id, mailbox_name, err_code);
275 email_account_t *ref_account = NULL;
278 int err = EMAIL_ERROR_NONE;
280 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
282 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
286 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
287 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
289 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
290 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
292 EM_DEBUG_EXCEPTION("em_malloc failed");
293 err = EMAIL_ERROR_OUT_OF_MEMORY;
297 memset(*path, 0x00, path_len);
299 /* 1. server address / server type */
300 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
301 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
303 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
306 /* 2. set tls option if security connection */
307 /*if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));*/
308 if (ref_account->incoming_server_secure_connection & 0x01) {
309 strncat(*path + 1, "/ssl", path_len-(EM_SAFE_STRLEN(*path)-1));
311 if (ref_account->incoming_server_secure_connection & 0x02)
312 strncat(*path + 1, "/tls/force_tls_v1_0", path_len-(EM_SAFE_STRLEN(*path)-1));
314 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
316 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
317 strncat(*path + 1, "}", path_len-EM_SAFE_STRLEN(*path)-1);
320 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
327 emcore_free_account(ref_account);
328 EM_SAFE_FREE(ref_account);
340 static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_mailbox_t *remote_box_list,
341 int remote_box_count, emstorage_mailbox_tbl_t *local_box_list, int local_box_count,
342 GList** remote_box_only, GList** local_box_only)
344 if ( !remote_box_only || !local_box_only ) {
348 GList *remote_head = NULL;
349 GList *local_head = NULL;
350 GList *remote_p = NULL;
351 GList *local_p = NULL;
353 EM_DEBUG_LOG ("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
355 if (local_box_count == 0) {
356 for (i=0 ; i<remote_box_count ; i++) {
357 *remote_box_only = g_list_prepend (*remote_box_only, remote_box_list+i);
358 *local_box_only = NULL;
363 for (i=0; i<remote_box_count; i++)
364 remote_head = g_list_prepend (remote_head, remote_box_list+i);
366 for (i=0; i<local_box_count; i++)
367 local_head = g_list_prepend (local_head, local_box_list+i);
370 for (remote_p = remote_head; remote_p ; remote_p = g_list_next (remote_p)) {
371 matched = false ; /* initialized not matched for each iteration */
372 email_internal_mailbox_t *remote_box = (email_internal_mailbox_t *) g_list_nth_data (remote_p, 0);
373 EM_DEBUG_LOG_DEV ("remote [%s]", remote_box->mailbox_name );
374 /* find matching mailbox in local box */
375 for (local_p = local_head; local_p ; local_p = g_list_next(local_p)) {
376 emstorage_mailbox_tbl_t *local_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (local_p, 0);
378 EM_DEBUG_LOG_DEV ("vs local [%s]", local_box->mailbox_name);
379 if (!EM_SAFE_STRCMP (remote_box->mailbox_name, local_box->mailbox_name)) {
380 /* It is unnecessary to compare the matched box in the next iteration, so remove it from local_box*/
381 local_head = g_list_delete_link (local_head, local_p);
386 /* if matching not found, add it to remote box */
387 if (matched == false) {
388 EM_DEBUG_LOG ("New box: name[%s] alias[%s]", remote_box->mailbox_name, remote_box->alias);
389 *remote_box_only = g_list_prepend (*remote_box_only, remote_box);
393 /* local_head contains unmatched local box */
394 *local_box_only = local_head;
396 if (remote_head) g_list_free(remote_head);
399 INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id, char *mailbox_name, int event_handle, int *err_code)
401 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, event_handle, err_code);
404 int err = EMAIL_ERROR_NONE;
405 MAILSTREAM *stream = NULL;
406 email_internal_mailbox_t *mailbox_list = NULL; /* mailbox list from imap server */
407 /* mailbox list from DB */
408 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
409 int local_mailbox_count = 0;
410 GList *remote_box_only = NULL;
411 GList *local_box_only = NULL;
412 email_account_t *ref_account = NULL;
413 void *tmp_stream = NULL;
414 char *mbox_path = NULL;
415 char *mailbox_name_for_mailbox_type = NULL;
416 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
419 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, event_handle, 0))
420 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
422 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
424 if (!emnetwork_check_network_status(&err)) {
425 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
429 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
431 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
432 err = EMAIL_ERROR_INVALID_ACCOUNT;
436 /* if not imap4 mail, return */
437 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
438 EM_DEBUG_EXCEPTION("unsupported account...");
439 err = EMAIL_ERROR_INVALID_ACCOUNT;
443 /* get mail server path */
444 /* mbox_path is not used. the below func might be unnecessary */
445 if (!emcore_get_mailbox_connection_path(multi_user_name, account_id, NULL, &mbox_path, &err) || !mbox_path) {
446 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
451 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
454 if (!emcore_connect_to_remote_mailbox(multi_user_name,
458 (void **)&tmp_stream,
459 &err) || !tmp_stream) {
460 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
462 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
463 err = EMAIL_ERROR_CANCELLED;
465 err = EMAIL_ERROR_CONNECTION_FAILURE;
469 EM_SAFE_FREE(mbox_path);
471 stream = (MAILSTREAM *)tmp_stream;
473 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
475 /* download mailbox list */
476 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
477 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed [%d]", err);
481 /* get all mailboxes which is previously synced */
482 if (!emstorage_get_mailbox_list (multi_user_name, account_id, EMAIL_MAILBOX_FROM_SERVER, EMAIL_MAILBOX_SORT_BY_NAME_ASC,\
483 &local_mailbox_count, &local_mailbox_list, 1, &err)) {
484 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
485 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list error [%d]", err);
488 EM_DEBUG_LOG ("mailbox not found");
491 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
493 emcore_find_mailbox_diff_between_local_and_remote (mailbox_list, count, local_mailbox_list, local_mailbox_count,
494 &remote_box_only, &local_box_only);
496 /* for remote_box_only, add new mailbox to DB */
497 GList *p = remote_box_only;
498 for (; p; p = g_list_next (p)) {
499 email_internal_mailbox_t *new_mailbox = (email_internal_mailbox_t *) g_list_nth_data (p, 0);
501 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
503 if (!new_mailbox->mailbox_name) {
507 /* EM_DEBUG_LOG_SEC ("mailbox name [%s]", new_mailbox->mailbox_name); */
508 new_mailbox->mail_slot_size = ref_account->default_mail_slot_size;
510 if (new_mailbox->mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
511 emcore_bind_mailbox_type (new_mailbox);
513 if (new_mailbox->mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
514 if (mailbox_type_list[new_mailbox->mailbox_type] != -1) {
515 EM_DEBUG_LOG_SEC ("Mailbox type [%d] of [%s] is duplicated", new_mailbox->mailbox_type, new_mailbox->mailbox_name);
516 new_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
519 mailbox_type_list[new_mailbox->mailbox_type] = 1;
522 /* make box variable to be added in DB */
523 emstorage_mailbox_tbl_t mailbox_tbl = {0};
524 mailbox_tbl.mailbox_id = new_mailbox->mailbox_id;
525 mailbox_tbl.account_id = new_mailbox->account_id;
526 mailbox_tbl.local_yn = 0;
527 mailbox_tbl.deleted_flag = 0;
528 mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
529 mailbox_tbl.mailbox_name = new_mailbox->mailbox_name;
530 mailbox_tbl.mail_slot_size = new_mailbox->mail_slot_size;
531 mailbox_tbl.no_select = new_mailbox->no_select;
533 /* Get the Alias Name after Parsing the Full mailbox Path */
534 if (new_mailbox->alias == NULL)
535 new_mailbox->alias = emcore_get_alias_of_mailbox ((const char *)new_mailbox->mailbox_name);
537 if (new_mailbox->alias) {
538 mailbox_tbl.alias = new_mailbox->alias;
539 mailbox_tbl.modifiable_yn = 1;
540 mailbox_tbl.total_mail_count_on_server = 0;
542 if (!emstorage_add_mailbox (multi_user_name, &mailbox_tbl, true, &err)) {
543 EM_DEBUG_EXCEPTION ("emstorage_add_mailbox error [%d]", err);
547 if (mailbox_tbl.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
549 EM_DEBUG_LOG_SEC ("MAILBOX ADDED: mailbox_name [%s] alias [%s] mailbox_type [%d]", new_mailbox->mailbox_name,\
550 new_mailbox->alias, mailbox_tbl.mailbox_type);
554 /* delete all local boxes and mails */
556 for (; p; p = g_list_next (p)) {
557 emstorage_mailbox_tbl_t *del_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (p, 0);
559 if (!emstorage_delete_mail_by_mailbox (multi_user_name, del_box, 1, &err)) {
560 EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
561 err, del_box->account_id, del_box->mailbox_name);
564 if (!emstorage_delete_mailbox (multi_user_name, del_box->account_id, EMAIL_MAILBOX_FROM_SERVER, del_box->mailbox_id, 1, &err)) {
565 EM_DEBUG_EXCEPTION ("emstorage_delete_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
566 err, del_box->account_id, del_box->mailbox_name);
569 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
570 if (!emstorage_delete_auto_download_activity_by_mailbox(multi_user_name, del_box->account_id, del_box->mailbox_id, 1, &err))
571 EM_DEBUG_EXCEPTION("emstorage_delete_auto_download_activity_by_mailbox failed");
574 EM_DEBUG_LOG_SEC ("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
577 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
578 if (mailbox_type_list[counter] == -1) {
579 int err2 = EMAIL_ERROR_NONE;
580 emstorage_mailbox_tbl_t mailbox_tbl;
581 emstorage_mailbox_tbl_t *result_mailbox_tbl = NULL;
583 if(emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, counter, &result_mailbox_tbl, true, &err2)) {
584 if(result_mailbox_tbl) {
585 emstorage_free_mailbox(&result_mailbox_tbl, 1, NULL);
590 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
592 mailbox_tbl.account_id = account_id;
593 mailbox_tbl.mailbox_id = 0;
594 mailbox_tbl.local_yn = 1;
595 mailbox_tbl.mailbox_type = counter;
596 mailbox_tbl.deleted_flag = 0;
597 mailbox_tbl.modifiable_yn = 1;
598 mailbox_tbl.total_mail_count_on_server = 0;
599 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
602 case EMAIL_MAILBOX_TYPE_SENTBOX:
603 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
604 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
607 case EMAIL_MAILBOX_TYPE_TRASH:
608 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
609 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
612 case EMAIL_MAILBOX_TYPE_DRAFT:
613 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
614 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
617 case EMAIL_MAILBOX_TYPE_SPAMBOX:
618 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
619 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
622 case EMAIL_MAILBOX_TYPE_OUTBOX:
623 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
624 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
628 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
629 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
633 if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
634 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
638 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
641 if (!emstorage_set_all_mailbox_modifiable_yn(multi_user_name, account_id, 0, true, &err)) {
642 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
647 emcore_refresh_imap_idle_thread();
649 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
651 for (i = 0; i < count; i++)
652 mailbox_list[i].account_id = account_id;
658 if (err == EMAIL_ERROR_NONE) {
659 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, event_handle, err))
660 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
663 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, event_handle, err))
664 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
666 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
667 EM_SAFE_FREE(mbox_path);
670 emcore_free_account(ref_account);
671 EM_SAFE_FREE(ref_account);
675 stream = mail_close (stream);
678 emcore_free_internal_mailbox (&mailbox_list, count, NULL);
680 if (local_mailbox_list)
681 emstorage_free_mailbox (&local_mailbox_list, local_mailbox_count, NULL);
684 g_list_free (local_box_only);
687 g_list_free (remote_box_only);
689 if (err_code != NULL)
691 EM_DEBUG_FUNC_END("ret [%d]", ret);
695 int emcore_download_mailbox_list(void *mail_stream,
697 email_internal_mailbox_t **mailbox_list,
701 EM_DEBUG_FUNC_BEGIN("mail_stream [%p], mailbox_name [%p], mailbox_list [%p], count [%p], err_code [%p]", mail_stream, mailbox_name, mailbox_list, count, err_code);
703 MAILSTREAM *stream = mail_stream;
704 email_callback_holder_t holder;
705 char *pattern = NULL;
706 char *reference = NULL;
707 int err = EMAIL_ERROR_NONE;
710 if (!stream || !mailbox_list || !count) {
711 err = EMAIL_ERROR_INVALID_PARAM;
715 memset(&holder, 0x00, sizeof(holder));
717 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
720 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
722 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
723 if ((s = strchr(reference, '}')))
725 strcat(reference, mailbox_name);
729 reference = EM_SAFE_STRDUP(stream->original_mailbox);
732 stream->sparep = &holder;
734 /* imap command : tag LIST reference * */
735 /* see callback function mm_list */
736 mail_list (stream, reference, pattern);
738 stream->sparep = NULL;
740 EM_SAFE_FREE(reference);
743 *mailbox_list = (email_internal_mailbox_t*) holder.data;
755 * create a new imap mailbox
757 * new_mailbox : imap mailbox to be created
762 INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
764 MAILSTREAM *stream = NULL;
765 char *long_enc_path = NULL;
766 void *tmp_stream = NULL;
768 int err = EMAIL_ERROR_NONE;
769 email_session_t *session = NULL;
771 EM_DEBUG_FUNC_BEGIN();
774 err = EMAIL_ERROR_INVALID_PARAM;
778 if (!emcore_get_empty_session(&session))
779 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
781 /* connect mail server */
783 if (!emcore_connect_to_remote_mailbox(multi_user_name,
787 (void **)&tmp_stream,
789 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
793 stream = (MAILSTREAM *) tmp_stream;
795 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
796 if (!emcore_get_long_encoded_path(multi_user_name, mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
797 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
802 if (!mail_create(stream, long_enc_path)) {
803 EM_DEBUG_EXCEPTION("mail_create failed");
805 if (!emcore_get_current_session(&session)) {
806 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
807 err = EMAIL_ERROR_SESSION_NOT_FOUND;
811 if (session->error == EMAIL_ERROR_ALREADY_EXISTS)
812 err = session->error;
814 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
819 EM_SAFE_FREE(long_enc_path);
825 stream = mail_close (stream);
828 EM_SAFE_FREE(long_enc_path);
831 if (err == EMAIL_ERROR_NONE) {
832 if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
833 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
835 else if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
836 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
839 emcore_clear_session(session);
849 * delete a imap mailbox
851 * input_mailbox_id : mailbox ID to be deleted
856 INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code)
858 EM_DEBUG_FUNC_BEGIN();
860 MAILSTREAM *stream = NULL;
861 char *long_enc_path = NULL;
862 email_account_t *ref_account = NULL;
863 void *tmp_stream = NULL;
865 int err = EMAIL_ERROR_NONE;
866 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
868 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
869 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
871 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
872 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
876 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
877 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
878 EM_DEBUG_EXCEPTION("Invalid account information");
879 err = EMAIL_ERROR_INVALID_ACCOUNT;
883 /* connect mail server */
884 if (!emcore_connect_to_remote_mailbox(multi_user_name,
885 mailbox_tbl->account_id,
888 (void **)&tmp_stream,
890 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
894 stream = (MAILSTREAM *)tmp_stream;
896 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
897 if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
898 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
903 if (!mail_delete(stream, long_enc_path)) {
904 EM_DEBUG_EXCEPTION("mail_delete failed");
905 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
909 EM_SAFE_FREE(long_enc_path);
915 stream = mail_close (stream);
919 emcore_free_account(ref_account);
920 EM_SAFE_FREE(ref_account);
923 EM_SAFE_FREE(long_enc_path);
926 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
928 if (err == EMAIL_ERROR_NONE) {
929 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
930 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
932 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
933 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
942 INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
944 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p] handle_to_be_published[%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, handle_to_be_published);
945 MAILSTREAM *stream = NULL;
946 char *long_enc_path_old = NULL;
947 char *long_enc_path_new = NULL;
948 email_account_t *ref_account = NULL;
949 void *tmp_stream = NULL;
950 int err = EMAIL_ERROR_NONE;
952 if (!input_old_mailbox_path || !input_new_mailbox_path) {
953 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
954 err = EMAIL_ERROR_INVALID_PARAM;
958 ref_account = emcore_get_account_reference(multi_user_name, input_account_id, false);
959 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
960 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
961 err = EMAIL_ERROR_INVALID_ACCOUNT;
965 /* connect mail server */
967 if (!emcore_connect_to_remote_mailbox(multi_user_name,
971 (void **)&tmp_stream,
973 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
977 stream = (MAILSTREAM *)tmp_stream;
979 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
980 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
981 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
985 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
986 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
987 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
992 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
993 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
999 if (err == EMAIL_ERROR_NONE) {
1000 if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
1001 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
1004 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
1005 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
1007 EM_SAFE_FREE(long_enc_path_old);
1008 EM_SAFE_FREE(long_enc_path_new);
1011 emcore_free_account(ref_account);
1012 EM_SAFE_FREE(ref_account);
1016 stream = mail_close (stream);
1019 EM_DEBUG_FUNC_END("err [%d]", err);
1024 #ifdef __FEATURE_IMAP_QUOTA__
1026 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
1028 EM_DEBUG_FUNC_BEGIN();
1030 EM_DEBUG_FUNC_END();
1034 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
1036 EM_DEBUG_FUNC_BEGIN();
1038 EM_DEBUG_FUNC_END();
1043 INTERNAL_FUNC int emcore_register_quota_callback()
1045 EM_DEBUG_FUNC_BEGIN();
1046 int err = EMAIL_ERROR_NONE;
1048 mail_parameters(NULL, SET_QUOTAROOT, callback_for_get_quota_root); /* set callback function for handling quota root message */
1049 mail_parameters(NULL, SET_QUOTA, callback_for_get_quota); /* set callback function for handling quota message */
1051 EM_DEBUG_FUNC_END("err [%d]", err);
1055 INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resource_t *output_list_of_resource_limits)
1057 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_list_of_resource_limits[%p]", input_mailbox_id, output_list_of_resource_limits);
1058 int err = EMAIL_ERROR_NONE;
1059 MAILSTREAM *stream = NULL;
1060 email_account_t *ref_account = NULL;
1061 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1063 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1064 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1068 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1069 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1070 EM_DEBUG_EXCEPTION("Invalid account information");
1071 err = EMAIL_ERROR_INVALID_ACCOUNT;
1075 /* connect mail server */
1076 if (!emcore_connect_to_remote_mailbox(multi_user_name,
1077 mailbox_tbl->account_id,
1082 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1086 imap_getquotaroot(stream, mailbox_tbl->mailbox_name);
1090 EM_DEBUG_FUNC_END("err [%d]", err);
1094 INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *output_list_of_resource_limits)
1096 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, output_list_of_resource_limits);
1097 int err = EMAIL_ERROR_NONE;
1098 MAILSTREAM *stream = NULL;
1099 email_account_t *ref_account = NULL;
1100 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1102 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1103 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1107 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1109 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1110 EM_DEBUG_EXCEPTION("Invalid account information");
1111 err = EMAIL_ERROR_INVALID_ACCOUNT;
1115 /* connect mail server */
1116 if (!emcore_connect_to_remote_mailbox(multi_user_name,
1117 mailbox_tbl->account_id,
1122 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1126 imap_getquota(stream, input_quota_root);
1129 EM_DEBUG_FUNC_END("err [%d]", err);
1133 INTERNAL_FUNC int emcore_set_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *input_list_of_resource_limits)
1135 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, input_list_of_resource_limits);
1136 int err = EMAIL_ERROR_NONE;
1137 /* TODO : set quota using the function 'imap_setquota' */
1139 EM_DEBUG_FUNC_END("err [%d]", err);
1143 #endif /* __FEATURE_IMAP_QUOTA__ */