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-mailbox-sync.h"
46 #include "email-core-account.h"
47 #include "email-core-signal.h"
52 #include "email-debug-log.h"
54 INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *output_count)
56 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] output_count[%p]", input_account_id, output_count);
58 int err = EMAIL_ERROR_NONE;
59 int default_mail_slot_count = 25;
60 email_account_t *account_ref = NULL;
62 if (output_count == NULL) {
63 err = EMAIL_ERROR_INVALID_PARAM;
67 account_ref = emcore_get_account_reference(input_account_id);
69 default_mail_slot_count = account_ref->default_mail_slot_size;
74 emcore_free_account(account_ref);
75 EM_SAFE_FREE(account_ref);
79 *output_count = default_mail_slot_count;
81 EM_DEBUG_FUNC_END("err[%d]", err);
86 INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
88 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
91 int *mail_id_list = NULL, mail_id_list_count = 0;
92 int err = EMAIL_ERROR_NONE;
93 email_account_t *account_ref = NULL;
95 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
96 if (intput_mailbox_tbl)
97 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
98 err = EMAIL_ERROR_INVALID_PARAM;
102 account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
104 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
105 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
106 err = EMAIL_ERROR_NOT_SUPPORTED;
111 if (!emstorage_get_overflowed_mail_id_list(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)) {
112 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
113 EM_DEBUG_LOG_SEC("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
114 err = EMAIL_ERROR_NONE;
118 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
123 if (!emcore_delete_mail(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
124 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
131 EM_SAFE_FREE(mail_id_list);
134 emcore_free_account(account_ref);
135 EM_SAFE_FREE(account_ref);
141 EM_DEBUG_FUNC_END("ret [%d]", ret);
146 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
148 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
150 int ret = false, err = EMAIL_ERROR_NONE;
152 int account_count = 100;
153 int mailbox_count = 0;
154 email_account_t *account_ref = NULL;
155 emstorage_account_tbl_t *account_tbl_list = NULL;
156 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
158 if (account_id > ALL_ACCOUNT) {
159 account_ref = emcore_get_account_reference(account_id);
160 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
161 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
165 else if (!account_ref) {
166 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
169 if (mailbox_id == 0) {
170 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
171 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
177 if (mailbox_id == 0) {
178 if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
179 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
182 for ( i = 0; i < account_count; i++) {
183 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
184 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
192 if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
193 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
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");
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;
209 else { /* read information from DB */
210 if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
211 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
218 if (!emstorage_get_mailbox_list(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);
224 for (i = 0; i < mailbox_count; i++) {
225 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
226 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
227 err = EMAIL_ERROR_NONE;
229 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
238 emcore_free_account(account_ref);
239 EM_SAFE_FREE(account_ref);
242 if (account_tbl_list)
243 emstorage_free_account(&account_tbl_list, account_count, NULL);
245 if (mailbox_tbl_list)
246 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
254 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
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;
260 int err = EMAIL_ERROR_NONE;
262 ref_account = emcore_get_account_reference(account_id);
264 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
268 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
269 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
271 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
272 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
274 EM_DEBUG_EXCEPTION("em_malloc failed");
275 err = EMAIL_ERROR_OUT_OF_MEMORY;
279 memset(*path, 0x00, path_len);
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);
285 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
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));
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));
296 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
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);
302 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
309 emcore_free_account(ref_account);
310 EM_SAFE_FREE(ref_account);
319 static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_mailbox_t *remote_box_list,
320 int remote_box_count, emstorage_mailbox_tbl_t *local_box_list, int local_box_count,
321 GList** remote_box_only, GList** local_box_only)
323 if ( !remote_box_only || !local_box_only ) {
327 GList *remote_head = NULL;
328 GList *local_head = NULL;
329 GList *remote_p = NULL;
330 GList *local_p = NULL;
332 EM_DEBUG_LOG ("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
334 if (local_box_count == 0) {
335 for (i=0 ; i<remote_box_count ; i++) {
336 *remote_box_only = g_list_prepend (*remote_box_only, remote_box_list+i);
337 *local_box_only = NULL;
342 for (i=0; i<remote_box_count; i++)
343 remote_head = g_list_prepend (remote_head, remote_box_list+i);
345 for (i=0; i<local_box_count; i++)
346 local_head = g_list_prepend (local_head, local_box_list+i);
349 for (remote_p = remote_head; remote_p ; remote_p = g_list_next (remote_p)) {
350 matched = false ; /* initialized not matched for each iteration */
351 email_internal_mailbox_t *remote_box = (email_internal_mailbox_t *) g_list_nth_data (remote_p, 0);
352 EM_DEBUG_LOG_DEV ("remote [%s]", remote_box->mailbox_name );
353 /* find matching mailbox in local box */
354 for (local_p = local_head; local_p ; local_p = g_list_next(local_p)) {
355 emstorage_mailbox_tbl_t *local_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (local_p, 0);
357 EM_DEBUG_LOG_DEV ("vs local [%s]", local_box->mailbox_name);
358 if (!EM_SAFE_STRCMP (remote_box->mailbox_name, local_box->mailbox_name)) {
359 /* It is unnecessary to compare the matched box in the next iteration, so remove it from local_box*/
360 local_head = g_list_delete_link (local_head, local_p);
365 /* if matching not found, add it to remote box */
366 if (matched == false) {
367 EM_DEBUG_LOG ("New box: name[%s] alias[%s]", remote_box->mailbox_name, remote_box->alias);
368 *remote_box_only = g_list_prepend (*remote_box_only, remote_box);
372 /* local_head contains unmatched local box */
373 *local_box_only = local_head;
376 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
378 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
381 int err = EMAIL_ERROR_NONE;
382 int status = EMAIL_DOWNLOAD_FAIL;
383 MAILSTREAM *stream = NULL;
384 email_internal_mailbox_t *mailbox_list = NULL; /* mailbox list from imap server */
385 /* mailbox list from DB */
386 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
387 int local_mailbox_count = 0;
388 GList *remote_box_only = NULL;
389 GList *local_box_only = NULL;
390 email_account_t *ref_account = NULL;
391 void *tmp_stream = NULL;
392 char *mbox_path = NULL;
393 char *mailbox_name_for_mailbox_type = NULL;
394 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
396 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
397 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
399 if (!emcore_check_thread_status()) {
400 err = EMAIL_ERROR_CANCELLED;
403 if (!emnetwork_check_network_status(&err)) {
404 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
408 ref_account = emcore_get_account_reference(account_id);
410 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
411 err = EMAIL_ERROR_INVALID_ACCOUNT;
415 /* if not imap4 mail, return */
416 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
417 EM_DEBUG_EXCEPTION("unsupported account...");
418 err = EMAIL_ERROR_INVALID_ACCOUNT;
422 /* get mail server path */
423 /* mbox_path is not used. the below func might be unnecessary */
424 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
425 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
430 if (!emcore_check_thread_status()) {
431 err = EMAIL_ERROR_CANCELLED;
436 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
437 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
439 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
440 err = EMAIL_ERROR_CANCELLED;
442 err = EMAIL_ERROR_CONNECTION_FAILURE;
444 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
448 EM_SAFE_FREE(mbox_path);
450 stream = (MAILSTREAM *)tmp_stream;
452 if (!emcore_check_thread_status()) {
453 err = EMAIL_ERROR_CANCELLED;
457 /* download mailbox list */
458 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
459 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed [%d]", err);
463 /* get all mailboxes which is previously synced */
464 if (!emstorage_get_mailbox_list (account_id, EMAIL_MAILBOX_FROM_SERVER, EMAIL_MAILBOX_SORT_BY_NAME_ASC,\
465 &local_mailbox_count, &local_mailbox_list, 1, &err)) {
466 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
467 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list error [%d]", err);
470 EM_DEBUG_LOG ("mailbox not found");
473 if (!emcore_check_thread_status()) {
474 err = EMAIL_ERROR_CANCELLED;
478 emcore_find_mailbox_diff_between_local_and_remote (mailbox_list, count, local_mailbox_list, local_mailbox_count,
479 &remote_box_only, &local_box_only);
481 /* for remote_box_only, add new mailbox to DB */
482 GList *p = remote_box_only;
483 for (; p; p = g_list_next (p)) {
484 email_internal_mailbox_t *new_mailbox = (email_internal_mailbox_t *) g_list_nth_data (p, 0);
486 if (!emcore_check_thread_status()) {
487 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
488 err = EMAIL_ERROR_CANCELLED;
492 if (!new_mailbox->mailbox_name) {
496 /* EM_DEBUG_LOG_SEC ("mailbox name [%s]", new_mailbox->mailbox_name); */
497 new_mailbox->mail_slot_size = ref_account->default_mail_slot_size;
499 if (new_mailbox->mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
500 emcore_bind_mailbox_type (new_mailbox);
502 if (new_mailbox->mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
503 if (mailbox_type_list[new_mailbox->mailbox_type] != -1) {
504 EM_DEBUG_LOG_SEC ("Mailbox type [%d] of [%s] is duplicated", new_mailbox->mailbox_type, new_mailbox->mailbox_name);
505 new_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
508 mailbox_type_list[new_mailbox->mailbox_type] = 1;
511 /* make box variable to be added in DB */
512 emstorage_mailbox_tbl_t mailbox_tbl = {0};
513 mailbox_tbl.mailbox_id = new_mailbox->mailbox_id;
514 mailbox_tbl.account_id = new_mailbox->account_id;
515 mailbox_tbl.local_yn = 0;
516 mailbox_tbl.deleted_flag = 0;
517 mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
518 mailbox_tbl.mailbox_name = new_mailbox->mailbox_name;
519 mailbox_tbl.mail_slot_size = new_mailbox->mail_slot_size;
520 mailbox_tbl.no_select = new_mailbox->no_select;
522 /* Get the Alias Name after Parsing the Full mailbox Path */
523 if (new_mailbox->alias == NULL)
524 new_mailbox->alias = emcore_get_alias_of_mailbox ((const char *)new_mailbox->mailbox_name);
526 if (new_mailbox->alias) {
527 mailbox_tbl.alias = new_mailbox->alias;
528 mailbox_tbl.modifiable_yn = 1;
529 mailbox_tbl.total_mail_count_on_server = 0;
531 if (!emstorage_add_mailbox (&mailbox_tbl, true, &err)) {
532 EM_DEBUG_EXCEPTION ("emstorage_add_mailbox error [%d]", err);
535 EM_DEBUG_LOG_SEC ("MAILBOX ADDED: mailbox_name [%s] alias [%s] mailbox_type [%d]", new_mailbox->mailbox_name,\
536 new_mailbox->alias, mailbox_tbl.mailbox_type);
540 /* delete all local boxes and mails */
542 for (; p; p = g_list_next (p)) {
543 emstorage_mailbox_tbl_t *del_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (p, 0);
544 if (!emstorage_delete_mail_by_mailbox (del_box->account_id, del_box->mailbox_id, 1, &err)) {
545 EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
546 err, del_box->account_id, del_box->mailbox_name);
548 if (!emstorage_delete_mailbox (del_box->account_id, EMAIL_MAILBOX_FROM_SERVER, del_box->mailbox_id, 1, &err)) {
549 EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
550 err, del_box->account_id, del_box->mailbox_name);
552 EM_DEBUG_LOG_SEC ("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
555 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
556 if (mailbox_type_list[counter] == -1) {
557 int err2 = EMAIL_ERROR_NONE;
558 emstorage_mailbox_tbl_t mailbox_tbl;
559 emstorage_mailbox_tbl_t *result_mailbox_tbl = NULL;
561 if(emstorage_get_mailbox_by_mailbox_type(account_id, counter, &result_mailbox_tbl, true, &err2)) {
562 if(result_mailbox_tbl) {
563 emstorage_free_mailbox(&result_mailbox_tbl, 1, NULL);
568 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
570 mailbox_tbl.account_id = account_id;
571 mailbox_tbl.mailbox_id = 0;
572 mailbox_tbl.local_yn = 1;
573 mailbox_tbl.mailbox_type = counter;
574 mailbox_tbl.deleted_flag = 0;
575 mailbox_tbl.modifiable_yn = 1;
576 mailbox_tbl.total_mail_count_on_server = 0;
577 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
580 case EMAIL_MAILBOX_TYPE_SENTBOX:
581 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
582 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
585 case EMAIL_MAILBOX_TYPE_TRASH:
586 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
587 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
590 case EMAIL_MAILBOX_TYPE_DRAFT:
591 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
592 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
595 case EMAIL_MAILBOX_TYPE_SPAMBOX:
596 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
597 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
600 case EMAIL_MAILBOX_TYPE_OUTBOX:
601 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
602 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
606 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
607 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
611 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
612 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
616 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
618 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
619 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
623 if (!emcore_check_thread_status()) {
624 err = EMAIL_ERROR_CANCELLED;
628 for (i = 0; i < count; i++)
629 mailbox_list[i].account_id = account_id;
636 if (err == EMAIL_ERROR_NONE) {
637 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
638 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
641 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
642 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
644 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
645 EM_SAFE_FREE(mbox_path);
648 emcore_free_account(ref_account);
649 EM_SAFE_FREE(ref_account);
653 stream = mail_close (stream);
656 emcore_free_internal_mailbox (&mailbox_list, count, NULL);
658 if (local_mailbox_list)
659 emstorage_free_mailbox (&local_mailbox_list, local_mailbox_count, NULL);
662 g_list_free (local_box_only);
665 g_list_free (remote_box_only);
667 if (err_code != NULL)
669 EM_DEBUG_FUNC_END("ret [%d]", ret);
673 int emcore_download_mailbox_list(void *mail_stream,
675 email_internal_mailbox_t **mailbox_list,
679 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);
681 MAILSTREAM *stream = mail_stream;
682 email_callback_holder_t holder;
683 char *pattern = NULL;
684 char *reference = NULL;
685 int err = EMAIL_ERROR_NONE;
688 if (!stream || !mailbox_list || !count) {
689 err = EMAIL_ERROR_INVALID_PARAM;
693 memset(&holder, 0x00, sizeof(holder));
695 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
698 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
700 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
701 if ((s = strchr(reference, '}')))
703 strcat(reference, mailbox_name);
707 reference = EM_SAFE_STRDUP(stream->original_mailbox);
710 stream->sparep = &holder;
712 /* imap command : tag LIST reference * */
713 /* see callback function mm_list */
714 mail_list (stream, reference, pattern);
716 stream->sparep = NULL;
718 EM_SAFE_FREE(reference);
721 *mailbox_list = (email_internal_mailbox_t*) holder.data;
733 * create a new imap mailbox
735 * new_mailbox : imap mailbox to be created
740 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
742 MAILSTREAM *stream = NULL;
743 char *long_enc_path = NULL;
744 void *tmp_stream = NULL;
746 int err = EMAIL_ERROR_NONE;
748 EM_DEBUG_FUNC_BEGIN();
751 err = EMAIL_ERROR_INVALID_PARAM;
755 /* connect mail server */
757 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
758 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
762 stream = (MAILSTREAM *) tmp_stream;
764 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
765 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
766 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
771 if (!mail_create(stream, long_enc_path)) {
772 EM_DEBUG_EXCEPTION("mail_create failed");
773 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
777 EM_SAFE_FREE(long_enc_path);
783 stream = mail_close (stream);
786 EM_SAFE_FREE(long_enc_path);
789 if (err == EMAIL_ERROR_NONE) {
790 if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
791 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
793 else if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
794 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
805 * delete a imap mailbox
807 * input_mailbox_id : mailbox ID to be deleted
812 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
814 EM_DEBUG_FUNC_BEGIN();
816 MAILSTREAM *stream = NULL;
817 char *long_enc_path = NULL;
818 email_account_t *ref_account = NULL;
819 void *tmp_stream = NULL;
821 int err = EMAIL_ERROR_NONE;
822 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
824 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
825 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
827 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
828 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
832 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
834 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
835 EM_DEBUG_EXCEPTION("Invalid account information");
836 err = EMAIL_ERROR_INVALID_ACCOUNT;
840 /* connect mail server */
841 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
842 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
846 stream = (MAILSTREAM *)tmp_stream;
848 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
849 if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
850 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
855 if (!mail_delete(stream, long_enc_path)) {
856 EM_DEBUG_EXCEPTION("mail_delete failed");
857 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
861 EM_SAFE_FREE(long_enc_path);
867 stream = mail_close (stream);
871 emcore_free_account(ref_account);
872 EM_SAFE_FREE(ref_account);
875 EM_SAFE_FREE(long_enc_path);
878 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
880 if (err == EMAIL_ERROR_NONE) {
881 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
882 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
884 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
885 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
894 INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
896 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);
897 MAILSTREAM *stream = NULL;
898 char *long_enc_path_old = NULL;
899 char *long_enc_path_new = NULL;
900 email_account_t *ref_account = NULL;
901 void *tmp_stream = NULL;
902 int err = EMAIL_ERROR_NONE;
904 if (!input_old_mailbox_path || !input_new_mailbox_path) {
905 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
906 err = EMAIL_ERROR_INVALID_PARAM;
910 ref_account = emcore_get_account_reference(input_account_id);
912 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
913 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
914 err = EMAIL_ERROR_INVALID_ACCOUNT;
918 /* connect mail server */
920 if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
921 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
925 stream = (MAILSTREAM *)tmp_stream;
927 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
928 if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
929 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
933 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
934 if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
935 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
940 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
941 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
947 if (err == EMAIL_ERROR_NONE) {
948 if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
949 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
952 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
953 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
955 EM_SAFE_FREE(long_enc_path_old);
956 EM_SAFE_FREE(long_enc_path_new);
959 emcore_free_account(ref_account);
960 EM_SAFE_FREE(ref_account);
964 stream = mail_close (stream);
967 EM_DEBUG_FUNC_END("err [%d]", err);
972 #ifdef __FEATURE_IMAP_QUOTA__
974 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
976 EM_DEBUG_FUNC_BEGIN();
982 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
984 EM_DEBUG_FUNC_BEGIN();
991 INTERNAL_FUNC int emcore_register_quota_callback()
993 EM_DEBUG_FUNC_BEGIN();
994 int err = EMAIL_ERROR_NONE;
996 mail_parameters(NULL, SET_QUOTAROOT, callback_for_get_quota_root); /* set callback function for handling quota root message */
997 mail_parameters(NULL, SET_QUOTA, callback_for_get_quota); /* set callback function for handling quota message */
999 EM_DEBUG_FUNC_END("err [%d]", err);
1003 INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resource_t *output_list_of_resource_limits)
1005 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_list_of_resource_limits[%p]", input_mailbox_id, output_list_of_resource_limits);
1006 int err = EMAIL_ERROR_NONE;
1007 MAILSTREAM *stream = NULL;
1008 email_account_t *ref_account = NULL;
1009 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1011 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1012 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1016 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
1018 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1019 EM_DEBUG_EXCEPTION("Invalid account information");
1020 err = EMAIL_ERROR_INVALID_ACCOUNT;
1024 /* connect mail server */
1025 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
1026 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1030 imap_getquotaroot(stream, mailbox_tbl->mailbox_name);
1034 EM_DEBUG_FUNC_END("err [%d]", err);
1038 INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *output_list_of_resource_limits)
1040 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);
1041 int err = EMAIL_ERROR_NONE;
1042 MAILSTREAM *stream = NULL;
1043 email_account_t *ref_account = NULL;
1044 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1046 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1047 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1051 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
1053 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1054 EM_DEBUG_EXCEPTION("Invalid account information");
1055 err = EMAIL_ERROR_INVALID_ACCOUNT;
1059 /* connect mail server */
1060 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
1061 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1065 imap_getquota(stream, input_quota_root);
1068 EM_DEBUG_FUNC_END("err [%d]", err);
1072 INTERNAL_FUNC int emcore_set_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *input_list_of_resource_limits)
1074 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);
1075 int err = EMAIL_ERROR_NONE;
1076 /* TODO : set quota using the function 'imap_setquota' */
1078 EM_DEBUG_FUNC_END("err [%d]", err);
1082 #endif /* __FEATURE_IMAP_QUOTA__ */