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;
127 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
133 if (!emcore_delete_mails_from_local_storage(multi_user_name,
134 input_mailbox_tbl->account_id,
137 EMAIL_DELETE_LOCALLY,
138 EMAIL_DELETED_BY_COMMAND,
140 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
149 EM_SAFE_FREE(mail_id_list);
152 emcore_free_account(account_ref);
153 EM_SAFE_FREE(account_ref);
159 EM_DEBUG_FUNC_END("ret [%d]", ret);
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)
166 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
168 int ret = false, err = EMAIL_ERROR_NONE;
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;
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");
182 } else if (!account_ref) {
183 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
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);
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);
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);
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);
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");
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);
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);
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;
243 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
252 emcore_free_account(account_ref);
253 EM_SAFE_FREE(account_ref);
256 if (account_tbl_list)
257 emstorage_free_account(&account_tbl_list, account_count, NULL);
259 if (mailbox_tbl_list)
260 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
268 static int emcore_get_mailbox_connection_path(char *multi_user_name, int account_id, char *mailbox_name, char **path, int *err_code)
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;
274 int err = EMAIL_ERROR_NONE;
276 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
278 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
282 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
283 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
285 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
286 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
288 EM_DEBUG_EXCEPTION("em_mallocfailed");
289 err = EMAIL_ERROR_OUT_OF_MEMORY;
293 memset(*path, 0x00, path_len);
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);
299 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
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));
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));
310 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
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);
316 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
323 emcore_free_account(ref_account);
324 EM_SAFE_FREE(ref_account);
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)
340 if (!remote_box_only || !local_box_only) {
344 GList *remote_head = NULL;
345 GList *local_head = NULL;
346 GList *remote_p = NULL;
347 GList *local_p = NULL;
349 EM_DEBUG_LOG("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
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;
359 for (i = 0; i < remote_box_count; i++)
360 remote_head = g_list_prepend(remote_head, remote_box_list+i);
362 for (i = 0; i < local_box_count; i++)
363 local_head = g_list_prepend(local_head, local_box_list+i);
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);
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);
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);
389 /* local_head contains unmatched local box */
390 *local_box_only = local_head;
392 if (remote_head) g_list_free(remote_head);
395 INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id, char *mailbox_name, int event_handle, int *err_code)
397 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, event_handle, err_code);
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};
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 >>>> ");
418 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
420 if (!emnetwork_check_network_status(&err)) {
421 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
425 ref_account = emcore_get_account_reference(multi_user_name, account_id, false);
427 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
428 err = EMAIL_ERROR_INVALID_ACCOUNT;
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;
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);
447 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
450 if (!emcore_connect_to_remote_mailbox(multi_user_name,
454 (void **)&tmp_stream,
455 &err) || !tmp_stream) {
456 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
458 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
459 err = EMAIL_ERROR_CANCELLED;
461 err = EMAIL_ERROR_CONNECTION_FAILURE;
465 EM_SAFE_FREE(mbox_path);
467 stream = (MAILSTREAM *)tmp_stream;
469 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
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);
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);
483 EM_DEBUG_LOG("mailbox not found");
486 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
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);
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);
496 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
498 if (!new_mailbox->mailbox_name) {
502 /* EM_DEBUG_LOG_SEC("mailbox name [%s]", new_mailbox->mailbox_name); */
503 new_mailbox->mail_slot_size = ref_account->default_mail_slot_size;
505 if (new_mailbox->mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
506 emcore_bind_mailbox_type(new_mailbox);
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 */
513 mailbox_type_list[new_mailbox->mailbox_type] = 1;
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;
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);
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;
536 if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
537 EM_DEBUG_EXCEPTION("emstorage_add_mailbox error [%d]", err);
541 if (mailbox_tbl.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
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);
548 /* delete all local boxes and mails */
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);
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);
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);
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");
568 EM_DEBUG_LOG_SEC("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
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;
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);
584 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
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;
596 case EMAIL_MAILBOX_TYPE_SENTBOX:
597 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
598 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
601 case EMAIL_MAILBOX_TYPE_TRASH:
602 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
603 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
606 case EMAIL_MAILBOX_TYPE_DRAFT:
607 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
608 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
611 case EMAIL_MAILBOX_TYPE_SPAMBOX:
612 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
613 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
616 case EMAIL_MAILBOX_TYPE_OUTBOX:
617 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
618 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
622 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
623 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
627 if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
628 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
632 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
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);
641 emcore_refresh_imap_idle_thread();
643 FINISH_OFF_IF_EVENT_CANCELED(err, event_handle);
645 for (i = 0; i < count; i++)
646 mailbox_list[i].account_id = account_id;
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 >>>> ");
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 >>>> ");
659 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
660 EM_SAFE_FREE(mbox_path);
663 emcore_free_account(ref_account);
664 EM_SAFE_FREE(ref_account);
668 stream = mail_close(stream);
671 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
673 if (local_mailbox_list)
674 emstorage_free_mailbox(&local_mailbox_list, local_mailbox_count, NULL);
677 g_list_free(local_box_only);
680 g_list_free(remote_box_only);
682 if (err_code != NULL)
684 EM_DEBUG_FUNC_END("ret [%d]", ret);
688 int emcore_download_mailbox_list(void *mail_stream,
690 email_internal_mailbox_t **mailbox_list,
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);
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;
703 if (!stream || !mailbox_list || !count) {
704 err = EMAIL_ERROR_INVALID_PARAM;
708 memset(&holder, 0x00, sizeof(holder));
710 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
713 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
715 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
716 if ((s = strchr(reference, '}')))
718 EM_SAFE_STRNCAT(reference, mailbox_name, (EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1) - EM_SAFE_STRLEN(reference) - 1);
721 reference = EM_SAFE_STRDUP(stream->original_mailbox);
724 stream->sparep = &holder;
726 /* imap command : tag LIST reference * */
727 /* see callback function mm_list */
728 mail_list(stream, reference, pattern);
730 stream->sparep = NULL;
732 EM_SAFE_FREE(reference);
735 *mailbox_list = (email_internal_mailbox_t*) holder.data;
747 * create a new imap mailbox
749 * new_mailbox : imap mailbox to be created
754 INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
756 MAILSTREAM *stream = NULL;
757 char *long_enc_path = NULL;
758 void *tmp_stream = NULL;
760 int err = EMAIL_ERROR_NONE;
761 email_session_t *session = NULL;
763 EM_DEBUG_FUNC_BEGIN();
766 err = EMAIL_ERROR_INVALID_PARAM;
770 if (!emcore_get_empty_session(&session))
771 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
773 /* connect mail server */
775 if (!emcore_connect_to_remote_mailbox(multi_user_name,
779 (void **)&tmp_stream,
781 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
785 stream = (MAILSTREAM *) tmp_stream;
787 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
788 if (!emcore_get_long_encoded_path(multi_user_name, mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
789 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
794 if (!mail_create(stream, long_enc_path)) {
795 EM_DEBUG_EXCEPTION("mail_create failed");
797 if (!emcore_get_current_session(&session)) {
798 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
799 err = EMAIL_ERROR_SESSION_NOT_FOUND;
803 if (session->error == EMAIL_ERROR_ALREADY_EXISTS)
804 err = session->error;
806 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
811 EM_SAFE_FREE(long_enc_path);
817 stream = mail_close(stream);
820 EM_SAFE_FREE(long_enc_path);
823 if (err == EMAIL_ERROR_NONE) {
824 if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
825 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
826 } else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
827 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
830 emcore_clear_session(session);
840 * delete a imap mailbox
842 * input_mailbox_id : mailbox ID to be deleted
847 INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code)
849 EM_DEBUG_FUNC_BEGIN();
851 MAILSTREAM *stream = NULL;
852 char *long_enc_path = NULL;
853 email_account_t *ref_account = NULL;
854 void *tmp_stream = NULL;
856 int err = EMAIL_ERROR_NONE;
857 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
859 if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
860 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
862 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
863 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
867 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
868 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
869 EM_DEBUG_EXCEPTION("Invalid account information");
870 err = EMAIL_ERROR_INVALID_ACCOUNT;
874 /* connect mail server */
875 if (!emcore_connect_to_remote_mailbox(multi_user_name,
876 mailbox_tbl->account_id,
879 (void **)&tmp_stream,
881 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
885 stream = (MAILSTREAM *)tmp_stream;
887 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
888 if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
889 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
894 if (!mail_delete(stream, long_enc_path)) {
895 EM_DEBUG_EXCEPTION("mail_delete failed");
896 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
900 EM_SAFE_FREE(long_enc_path);
906 stream = mail_close(stream);
910 emcore_free_account(ref_account);
911 EM_SAFE_FREE(ref_account);
914 EM_SAFE_FREE(long_enc_path);
917 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
919 if (err == EMAIL_ERROR_NONE) {
920 if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
921 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
922 } else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
923 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
932 INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
934 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p] handle_to_be_published[%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, handle_to_be_published);
935 MAILSTREAM *stream = NULL;
936 char *long_enc_path_old = NULL;
937 char *long_enc_path_new = NULL;
938 email_account_t *ref_account = NULL;
939 void *tmp_stream = NULL;
940 int err = EMAIL_ERROR_NONE;
942 if (!input_old_mailbox_path || !input_new_mailbox_path) {
943 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
944 err = EMAIL_ERROR_INVALID_PARAM;
948 ref_account = emcore_get_account_reference(multi_user_name, input_account_id, false);
949 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
950 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
951 err = EMAIL_ERROR_INVALID_ACCOUNT;
955 /* connect mail server */
957 if (!emcore_connect_to_remote_mailbox(multi_user_name,
961 (void **)&tmp_stream,
963 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
967 stream = (MAILSTREAM *)tmp_stream;
969 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
970 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
971 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
975 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
976 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
977 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
982 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
983 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
989 if (err == EMAIL_ERROR_NONE) {
990 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
991 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
993 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
994 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
996 EM_SAFE_FREE(long_enc_path_old);
997 EM_SAFE_FREE(long_enc_path_new);
1000 emcore_free_account(ref_account);
1001 EM_SAFE_FREE(ref_account);
1005 stream = mail_close(stream);
1008 EM_DEBUG_FUNC_END("err [%d]", err);
1013 #ifdef __FEATURE_IMAP_QUOTA__
1015 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
1017 EM_DEBUG_FUNC_BEGIN();
1019 EM_DEBUG_FUNC_END();
1023 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
1025 EM_DEBUG_FUNC_BEGIN();
1027 EM_DEBUG_FUNC_END();
1032 INTERNAL_FUNC int emcore_register_quota_callback()
1034 EM_DEBUG_FUNC_BEGIN();
1035 int err = EMAIL_ERROR_NONE;
1037 mail_parameters(NULL, SET_QUOTAROOT, callback_for_get_quota_root); /* set callback function for handling quota root message */
1038 mail_parameters(NULL, SET_QUOTA, callback_for_get_quota); /* set callback function for handling quota message */
1040 EM_DEBUG_FUNC_END("err [%d]", err);
1044 INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resource_t *output_list_of_resource_limits)
1046 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_list_of_resource_limits[%p]", input_mailbox_id, output_list_of_resource_limits);
1047 int err = EMAIL_ERROR_NONE;
1048 MAILSTREAM *stream = NULL;
1049 email_account_t *ref_account = NULL;
1050 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1052 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1053 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1057 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1058 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1059 EM_DEBUG_EXCEPTION("Invalid account information");
1060 err = EMAIL_ERROR_INVALID_ACCOUNT;
1064 /* connect mail server */
1065 if (!emcore_connect_to_remote_mailbox(multi_user_name,
1066 mailbox_tbl->account_id,
1071 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1075 imap_getquotaroot(stream, mailbox_tbl->mailbox_name);
1079 EM_DEBUG_FUNC_END("err [%d]", err);
1083 INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *output_list_of_resource_limits)
1085 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, output_list_of_resource_limits);
1086 int err = EMAIL_ERROR_NONE;
1087 MAILSTREAM *stream = NULL;
1088 email_account_t *ref_account = NULL;
1089 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1091 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1092 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1096 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id, false);
1098 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1099 EM_DEBUG_EXCEPTION("Invalid account information");
1100 err = EMAIL_ERROR_INVALID_ACCOUNT;
1104 /* connect mail server */
1105 if (!emcore_connect_to_remote_mailbox(multi_user_name,
1106 mailbox_tbl->account_id,
1111 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1115 imap_getquota(stream, input_quota_root);
1118 EM_DEBUG_FUNC_END("err [%d]", err);
1122 INTERNAL_FUNC int emcore_set_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *input_list_of_resource_limits)
1124 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, input_list_of_resource_limits);
1125 int err = EMAIL_ERROR_NONE;
1126 /* TODO : set quota using the function 'imap_setquota' */
1128 EM_DEBUG_FUNC_END("err [%d]", err);
1132 #endif /* __FEATURE_IMAP_QUOTA__ */