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);
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, emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
89 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
92 int *mail_id_list = NULL, mail_id_list_count = 0;
93 int err = EMAIL_ERROR_NONE;
94 email_account_t *account_ref = NULL;
96 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
97 if (intput_mailbox_tbl)
98 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
99 err = EMAIL_ERROR_INVALID_PARAM;
103 account_ref = emcore_get_account_reference(multi_user_name, intput_mailbox_tbl->account_id);
105 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
106 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
107 err = EMAIL_ERROR_NOT_SUPPORTED;
112 if (!emstorage_get_overflowed_mail_id_list(multi_user_name, intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_id, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
113 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
114 EM_DEBUG_LOG_SEC("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
115 err = EMAIL_ERROR_NONE;
119 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
124 if (!emcore_delete_mail(multi_user_name, intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, EMAIL_DELETE_LOCALLY, EMAIL_DELETED_BY_OVERFLOW, false, &err)) {
125 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
132 EM_SAFE_FREE(mail_id_list);
135 emcore_free_account(account_ref);
136 EM_SAFE_FREE(account_ref);
142 EM_DEBUG_FUNC_END("ret [%d]", ret);
147 INTERNAL_FUNC int emcore_set_mail_slot_size(char *multi_user_name, int account_id, int mailbox_id, int new_slot_size, int *err_code)
149 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
151 int ret = false, err = EMAIL_ERROR_NONE;
153 int account_count = 100;
154 int mailbox_count = 0;
155 email_account_t *account_ref = NULL;
156 emstorage_account_tbl_t *account_tbl_list = NULL;
157 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
159 if (account_id > ALL_ACCOUNT) {
160 account_ref = emcore_get_account_reference(multi_user_name, account_id);
161 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
162 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
166 else if (!account_ref) {
167 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
170 if (mailbox_id == 0) {
171 if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
172 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
178 if (mailbox_id == 0) {
179 if ( !emstorage_get_account_list(multi_user_name, &account_count, &account_tbl_list, false, false, &err)) {
180 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
183 for ( i = 0; i < account_count; i++) {
184 if ( (err = emstorage_set_field_of_accounts_with_integer_value(multi_user_name, account_tbl_list[i].account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
185 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
192 if (!emstorage_set_mail_slot_size(multi_user_name, account_id, mailbox_id, new_slot_size, true, &err)) {
193 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
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(multi_user_name, 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(multi_user_name, account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
219 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
224 for (i = 0; i < mailbox_count; i++) {
225 if (!emcore_remove_overflowed_mails(multi_user_name, mailbox_tbl_list + i, &err)) {
226 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
227 err = EMAIL_ERROR_NONE;
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(char *multi_user_name, 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(multi_user_name, 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);
322 static void emcore_find_mailbox_diff_between_local_and_remote (email_internal_mailbox_t *remote_box_list,
323 int remote_box_count, emstorage_mailbox_tbl_t *local_box_list, int local_box_count,
324 GList** remote_box_only, GList** local_box_only)
326 if ( !remote_box_only || !local_box_only ) {
330 GList *remote_head = NULL;
331 GList *local_head = NULL;
332 GList *remote_p = NULL;
333 GList *local_p = NULL;
335 EM_DEBUG_LOG ("remote_box_count[%d] local_box_count[%d]", remote_box_count, local_box_count);
337 if (local_box_count == 0) {
338 for (i=0 ; i<remote_box_count ; i++) {
339 *remote_box_only = g_list_prepend (*remote_box_only, remote_box_list+i);
340 *local_box_only = NULL;
345 for (i=0; i<remote_box_count; i++)
346 remote_head = g_list_prepend (remote_head, remote_box_list+i);
348 for (i=0; i<local_box_count; i++)
349 local_head = g_list_prepend (local_head, local_box_list+i);
352 for (remote_p = remote_head; remote_p ; remote_p = g_list_next (remote_p)) {
353 matched = false ; /* initialized not matched for each iteration */
354 email_internal_mailbox_t *remote_box = (email_internal_mailbox_t *) g_list_nth_data (remote_p, 0);
355 EM_DEBUG_LOG_DEV ("remote [%s]", remote_box->mailbox_name );
356 /* find matching mailbox in local box */
357 for (local_p = local_head; local_p ; local_p = g_list_next(local_p)) {
358 emstorage_mailbox_tbl_t *local_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (local_p, 0);
360 EM_DEBUG_LOG_DEV ("vs local [%s]", local_box->mailbox_name);
361 if (!EM_SAFE_STRCMP (remote_box->mailbox_name, local_box->mailbox_name)) {
362 /* It is unnecessary to compare the matched box in the next iteration, so remove it from local_box*/
363 local_head = g_list_delete_link (local_head, local_p);
368 /* if matching not found, add it to remote box */
369 if (matched == false) {
370 EM_DEBUG_LOG ("New box: name[%s] alias[%s]", remote_box->mailbox_name, remote_box->alias);
371 *remote_box_only = g_list_prepend (*remote_box_only, remote_box);
375 /* local_head contains unmatched local box */
376 *local_box_only = local_head;
378 if (remote_head) g_list_free(remote_head);
381 INTERNAL_FUNC int emcore_sync_mailbox_list(char *multi_user_name, int account_id, char *mailbox_name, int event_handle, int *err_code)
383 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, event_handle, err_code);
386 int err = EMAIL_ERROR_NONE;
387 MAILSTREAM *stream = NULL;
388 email_internal_mailbox_t *mailbox_list = NULL; /* mailbox list from imap server */
389 /* mailbox list from DB */
390 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
391 int local_mailbox_count = 0;
392 GList *remote_box_only = NULL;
393 GList *local_box_only = NULL;
394 email_account_t *ref_account = NULL;
395 void *tmp_stream = NULL;
396 char *mbox_path = NULL;
397 char *mailbox_name_for_mailbox_type = NULL;
398 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
401 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, event_handle, 0))
402 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
404 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
406 if (!emnetwork_check_network_status(&err)) {
407 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
411 ref_account = emcore_get_account_reference(multi_user_name, account_id);
413 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
414 err = EMAIL_ERROR_INVALID_ACCOUNT;
418 /* if not imap4 mail, return */
419 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
420 EM_DEBUG_EXCEPTION("unsupported account...");
421 err = EMAIL_ERROR_INVALID_ACCOUNT;
425 /* get mail server path */
426 /* mbox_path is not used. the below func might be unnecessary */
427 if (!emcore_get_mailbox_connection_path(multi_user_name, account_id, NULL, &mbox_path, &err) || !mbox_path) {
428 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
433 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
436 if (!emcore_connect_to_remote_mailbox(multi_user_name, account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
437 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
439 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
440 err = EMAIL_ERROR_CANCELLED;
442 err = EMAIL_ERROR_CONNECTION_FAILURE;
446 EM_SAFE_FREE(mbox_path);
448 stream = (MAILSTREAM *)tmp_stream;
450 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
452 /* download mailbox list */
453 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
454 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed [%d]", err);
458 /* get all mailboxes which is previously synced */
459 if (!emstorage_get_mailbox_list (multi_user_name, account_id, EMAIL_MAILBOX_FROM_SERVER, EMAIL_MAILBOX_SORT_BY_NAME_ASC,\
460 &local_mailbox_count, &local_mailbox_list, 1, &err)) {
461 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
462 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_list error [%d]", err);
465 EM_DEBUG_LOG ("mailbox not found");
468 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
470 emcore_find_mailbox_diff_between_local_and_remote (mailbox_list, count, local_mailbox_list, local_mailbox_count,
471 &remote_box_only, &local_box_only);
473 /* for remote_box_only, add new mailbox to DB */
474 GList *p = remote_box_only;
475 for (; p; p = g_list_next (p)) {
476 email_internal_mailbox_t *new_mailbox = (email_internal_mailbox_t *) g_list_nth_data (p, 0);
478 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
480 if (!new_mailbox->mailbox_name) {
484 /* EM_DEBUG_LOG_SEC ("mailbox name [%s]", new_mailbox->mailbox_name); */
485 new_mailbox->mail_slot_size = ref_account->default_mail_slot_size;
487 if (new_mailbox->mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
488 emcore_bind_mailbox_type (new_mailbox);
490 if (new_mailbox->mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
491 if (mailbox_type_list[new_mailbox->mailbox_type] != -1) {
492 EM_DEBUG_LOG_SEC ("Mailbox type [%d] of [%s] is duplicated", new_mailbox->mailbox_type, new_mailbox->mailbox_name);
493 new_mailbox->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
496 mailbox_type_list[new_mailbox->mailbox_type] = 1;
499 /* make box variable to be added in DB */
500 emstorage_mailbox_tbl_t mailbox_tbl = {0};
501 mailbox_tbl.mailbox_id = new_mailbox->mailbox_id;
502 mailbox_tbl.account_id = new_mailbox->account_id;
503 mailbox_tbl.local_yn = 0;
504 mailbox_tbl.deleted_flag = 0;
505 mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
506 mailbox_tbl.mailbox_name = new_mailbox->mailbox_name;
507 mailbox_tbl.mail_slot_size = new_mailbox->mail_slot_size;
508 mailbox_tbl.no_select = new_mailbox->no_select;
510 /* Get the Alias Name after Parsing the Full mailbox Path */
511 if (new_mailbox->alias == NULL)
512 new_mailbox->alias = emcore_get_alias_of_mailbox ((const char *)new_mailbox->mailbox_name);
514 if (new_mailbox->alias) {
515 mailbox_tbl.alias = new_mailbox->alias;
516 mailbox_tbl.modifiable_yn = 1;
517 mailbox_tbl.total_mail_count_on_server = 0;
519 if (!emstorage_add_mailbox (multi_user_name, &mailbox_tbl, true, &err)) {
520 EM_DEBUG_EXCEPTION ("emstorage_add_mailbox error [%d]", err);
524 if (mailbox_tbl.mailbox_type == EMAIL_MAILBOX_TYPE_INBOX)
526 EM_DEBUG_LOG_SEC ("MAILBOX ADDED: mailbox_name [%s] alias [%s] mailbox_type [%d]", new_mailbox->mailbox_name,\
527 new_mailbox->alias, mailbox_tbl.mailbox_type);
531 /* delete all local boxes and mails */
533 for (; p; p = g_list_next (p)) {
534 emstorage_mailbox_tbl_t *del_box = (emstorage_mailbox_tbl_t *) g_list_nth_data (p, 0);
536 if (!emstorage_delete_mail_by_mailbox (multi_user_name, del_box, 1, &err)) {
537 EM_DEBUG_EXCEPTION ("emstorage_delete_mail_by_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
538 err, del_box->account_id, del_box->mailbox_name);
541 if (!emstorage_delete_mailbox (multi_user_name, del_box->account_id, EMAIL_MAILBOX_FROM_SERVER, del_box->mailbox_id, 1, &err)) {
542 EM_DEBUG_EXCEPTION ("emstorage_delete_mailbox error [%d] account_id [%d] mailbox_name [%s]",\
543 err, del_box->account_id, del_box->mailbox_name);
546 #ifdef __FEATURE_WIFI_AUTO_DOWNLOAD__
547 if (!emstorage_delete_auto_download_activity_by_mailbox(multi_user_name, del_box->account_id, del_box->mailbox_id, 1, &err))
548 EM_DEBUG_EXCEPTION("emstorage_delete_auto_download_activity_by_mailbox failed");
551 EM_DEBUG_LOG_SEC ("MAILBOX REMOVED: mailbox_name[%s] mailbox_id[%d]", del_box->mailbox_name, del_box->mailbox_id);
554 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
555 if (mailbox_type_list[counter] == -1) {
556 int err2 = EMAIL_ERROR_NONE;
557 emstorage_mailbox_tbl_t mailbox_tbl;
558 emstorage_mailbox_tbl_t *result_mailbox_tbl = NULL;
560 if(emstorage_get_mailbox_by_mailbox_type(multi_user_name, account_id, counter, &result_mailbox_tbl, true, &err2)) {
561 if(result_mailbox_tbl) {
562 emstorage_free_mailbox(&result_mailbox_tbl, 1, NULL);
567 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
569 mailbox_tbl.account_id = account_id;
570 mailbox_tbl.mailbox_id = 0;
571 mailbox_tbl.local_yn = 1;
572 mailbox_tbl.mailbox_type = counter;
573 mailbox_tbl.deleted_flag = 0;
574 mailbox_tbl.modifiable_yn = 1;
575 mailbox_tbl.total_mail_count_on_server = 0;
576 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
579 case EMAIL_MAILBOX_TYPE_SENTBOX:
580 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
581 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
584 case EMAIL_MAILBOX_TYPE_TRASH:
585 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
586 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
589 case EMAIL_MAILBOX_TYPE_DRAFT:
590 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
591 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
594 case EMAIL_MAILBOX_TYPE_SPAMBOX:
595 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
596 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
599 case EMAIL_MAILBOX_TYPE_OUTBOX:
600 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
601 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
605 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
606 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
610 if (!emstorage_add_mailbox(multi_user_name, &mailbox_tbl, true, &err)) {
611 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
615 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
618 if (!emstorage_set_all_mailbox_modifiable_yn(multi_user_name, account_id, 0, true, &err)) {
619 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
624 emcore_refresh_imap_idle_thread();
626 FINISH_OFF_IF_EVENT_CANCELED (err, event_handle);
628 for (i = 0; i < count; i++)
629 mailbox_list[i].account_id = account_id;
635 if (err == EMAIL_ERROR_NONE) {
636 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, event_handle, err))
637 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
640 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, event_handle, err))
641 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
643 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
644 EM_SAFE_FREE(mbox_path);
647 emcore_free_account(ref_account);
648 EM_SAFE_FREE(ref_account);
652 stream = mail_close (stream);
655 emcore_free_internal_mailbox (&mailbox_list, count, NULL);
657 if (local_mailbox_list)
658 emstorage_free_mailbox (&local_mailbox_list, local_mailbox_count, NULL);
661 g_list_free (local_box_only);
664 g_list_free (remote_box_only);
666 if (err_code != NULL)
668 EM_DEBUG_FUNC_END("ret [%d]", ret);
672 int emcore_download_mailbox_list(void *mail_stream,
674 email_internal_mailbox_t **mailbox_list,
678 EM_DEBUG_FUNC_BEGIN("mail_stream [%p], mailbox_name [%p], mailbox_list [%p], count [%p], err_code [%p]", mail_stream, mailbox_name, mailbox_list, count, err_code);
680 MAILSTREAM *stream = mail_stream;
681 email_callback_holder_t holder;
682 char *pattern = NULL;
683 char *reference = NULL;
684 int err = EMAIL_ERROR_NONE;
687 if (!stream || !mailbox_list || !count) {
688 err = EMAIL_ERROR_INVALID_PARAM;
692 memset(&holder, 0x00, sizeof(holder));
694 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
697 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
699 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
700 if ((s = strchr(reference, '}')))
702 strcat(reference, mailbox_name);
706 reference = EM_SAFE_STRDUP(stream->original_mailbox);
709 stream->sparep = &holder;
711 /* imap command : tag LIST reference * */
712 /* see callback function mm_list */
713 mail_list (stream, reference, pattern);
715 stream->sparep = NULL;
717 EM_SAFE_FREE(reference);
720 *mailbox_list = (email_internal_mailbox_t*) holder.data;
732 * create a new imap mailbox
734 * new_mailbox : imap mailbox to be created
739 INTERNAL_FUNC int emcore_create_imap_mailbox(char *multi_user_name, email_mailbox_t *mailbox, int *err_code)
741 MAILSTREAM *stream = NULL;
742 char *long_enc_path = NULL;
743 void *tmp_stream = NULL;
745 int err = EMAIL_ERROR_NONE;
746 email_session_t *session = NULL;
748 EM_DEBUG_FUNC_BEGIN();
751 err = EMAIL_ERROR_INVALID_PARAM;
755 if (!emcore_get_empty_session(&session))
756 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
758 /* connect mail server */
760 if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
761 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
765 stream = (MAILSTREAM *) tmp_stream;
767 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
768 if (!emcore_get_long_encoded_path(multi_user_name, mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
769 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
774 if (!mail_create(stream, long_enc_path)) {
775 EM_DEBUG_EXCEPTION("mail_create failed");
777 if (!emcore_get_current_session(&session)) {
778 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
779 err = EMAIL_ERROR_SESSION_NOT_FOUND;
783 if (session->error == EMAIL_ERROR_ALREADY_EXISTS)
784 err = session->error;
786 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
791 EM_SAFE_FREE(long_enc_path);
797 stream = mail_close (stream);
800 EM_SAFE_FREE(long_enc_path);
803 if (err == EMAIL_ERROR_NONE) {
804 if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
805 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
807 else if (!emcore_notify_network_event (NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
808 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
811 emcore_clear_session(session);
821 * delete a imap mailbox
823 * input_mailbox_id : mailbox ID to be deleted
828 INTERNAL_FUNC int emcore_delete_imap_mailbox(char *multi_user_name, int input_mailbox_id, int *err_code)
830 EM_DEBUG_FUNC_BEGIN();
832 MAILSTREAM *stream = NULL;
833 char *long_enc_path = NULL;
834 email_account_t *ref_account = NULL;
835 void *tmp_stream = NULL;
837 int err = EMAIL_ERROR_NONE;
838 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
840 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_START, input_mailbox_id, 0, 0, 0))
841 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_DELETE_MAILBOX_START] failed");
843 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
844 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
848 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
850 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
851 EM_DEBUG_EXCEPTION("Invalid account information");
852 err = EMAIL_ERROR_INVALID_ACCOUNT;
856 /* connect mail server */
857 if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
858 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
862 stream = (MAILSTREAM *)tmp_stream;
864 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
865 if (!emcore_get_long_encoded_path(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
866 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
871 if (!mail_delete(stream, long_enc_path)) {
872 EM_DEBUG_EXCEPTION("mail_delete failed");
873 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
877 EM_SAFE_FREE(long_enc_path);
883 stream = mail_close (stream);
887 emcore_free_account(ref_account);
888 EM_SAFE_FREE(ref_account);
891 EM_SAFE_FREE(long_enc_path);
894 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
896 if (err == EMAIL_ERROR_NONE) {
897 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
898 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
900 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
901 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
910 INTERNAL_FUNC int emcore_rename_mailbox_on_imap_server(char *multi_user_name, int input_account_id, int input_mailbox_id, char *input_old_mailbox_path, char *input_new_mailbox_path, int handle_to_be_published)
912 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_mailbox_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p] handle_to_be_published[%d]", input_account_id, input_mailbox_id, input_old_mailbox_path, input_new_mailbox_path, handle_to_be_published);
913 MAILSTREAM *stream = NULL;
914 char *long_enc_path_old = NULL;
915 char *long_enc_path_new = NULL;
916 email_account_t *ref_account = NULL;
917 void *tmp_stream = NULL;
918 int err = EMAIL_ERROR_NONE;
920 if (!input_old_mailbox_path || !input_new_mailbox_path) {
921 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
922 err = EMAIL_ERROR_INVALID_PARAM;
926 ref_account = emcore_get_account_reference(multi_user_name, input_account_id);
928 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
929 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
930 err = EMAIL_ERROR_INVALID_ACCOUNT;
934 /* connect mail server */
936 if (!emcore_connect_to_remote_mailbox(multi_user_name, input_account_id, 0, (void **)&tmp_stream, &err)) {
937 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
941 stream = (MAILSTREAM *)tmp_stream;
943 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
944 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
945 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
949 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
950 if (!emcore_get_long_encoded_path(multi_user_name, input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
951 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
956 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
957 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
963 if (err == EMAIL_ERROR_NONE) {
964 if(!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FINISH, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
965 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FINISH] failed");
968 if (!emcore_notify_network_event(NOTI_RENAME_MAILBOX_FAIL, input_mailbox_id, input_new_mailbox_path, handle_to_be_published, 0))
969 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_RENAME_MAILBOX_FAIL] failed");
971 EM_SAFE_FREE(long_enc_path_old);
972 EM_SAFE_FREE(long_enc_path_new);
975 emcore_free_account(ref_account);
976 EM_SAFE_FREE(ref_account);
980 stream = mail_close (stream);
983 EM_DEBUG_FUNC_END("err [%d]", err);
988 #ifdef __FEATURE_IMAP_QUOTA__
990 quota_t callback_for_get_quota_root(MAILSTREAM *stream, unsigned char *mailbox, STRINGLIST *quota_root_list)
992 EM_DEBUG_FUNC_BEGIN();
998 quota_t callback_for_get_quota(MAILSTREAM *stream, unsigned char *quota_root, QUOTALIST *quota_list)
1000 EM_DEBUG_FUNC_BEGIN();
1002 EM_DEBUG_FUNC_END();
1007 INTERNAL_FUNC int emcore_register_quota_callback()
1009 EM_DEBUG_FUNC_BEGIN();
1010 int err = EMAIL_ERROR_NONE;
1012 mail_parameters(NULL, SET_QUOTAROOT, callback_for_get_quota_root); /* set callback function for handling quota root message */
1013 mail_parameters(NULL, SET_QUOTA, callback_for_get_quota); /* set callback function for handling quota message */
1015 EM_DEBUG_FUNC_END("err [%d]", err);
1019 INTERNAL_FUNC int emcore_get_quota_root(int input_mailbox_id, email_quota_resource_t *output_list_of_resource_limits)
1021 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], output_list_of_resource_limits[%p]", input_mailbox_id, output_list_of_resource_limits);
1022 int err = EMAIL_ERROR_NONE;
1023 MAILSTREAM *stream = NULL;
1024 email_account_t *ref_account = NULL;
1025 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1027 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1028 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1032 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
1034 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1035 EM_DEBUG_EXCEPTION("Invalid account information");
1036 err = EMAIL_ERROR_INVALID_ACCOUNT;
1040 /* connect mail server */
1041 if (!emcore_connect_to_remote_mailbox(multi_user_name, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
1042 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1046 imap_getquotaroot(stream, mailbox_tbl->mailbox_name);
1050 EM_DEBUG_FUNC_END("err [%d]", err);
1054 INTERNAL_FUNC int emcore_get_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *output_list_of_resource_limits)
1056 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, output_list_of_resource_limits);
1057 int err = EMAIL_ERROR_NONE;
1058 MAILSTREAM *stream = NULL;
1059 email_account_t *ref_account = NULL;
1060 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1062 if ((err = emstorage_get_mailbox_by_id(multi_user_name, input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
1063 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1067 ref_account = emcore_get_account_reference(multi_user_name, mailbox_tbl->account_id);
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, mailbox_tbl->account_id, 0, (void **)&stream, &err)) {
1077 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1081 imap_getquota(stream, input_quota_root);
1084 EM_DEBUG_FUNC_END("err [%d]", err);
1088 INTERNAL_FUNC int emcore_set_quota(int input_mailbox_id, char *input_quota_root, email_quota_resource_t *input_list_of_resource_limits)
1090 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d] input_quota_root[%p] output_list_of_resource_limits[%p]", input_mailbox_id, input_quota_root, input_list_of_resource_limits);
1091 int err = EMAIL_ERROR_NONE;
1092 /* TODO : set quota using the function 'imap_setquota' */
1094 EM_DEBUG_FUNC_END("err [%d]", err);
1098 #endif /* __FEATURE_IMAP_QUOTA__ */