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"
40 #include "email-storage.h"
41 #include "email-utilities.h"
42 #include "email-network.h"
43 #include "email-core-event.h"
44 #include "email-core-mailbox.h"
45 #include "email-core-imap-mailbox.h"
46 #include "email-core-mailbox-sync.h"
47 #include "email-core-account.h"
48 #include "email-core-signal.h"
51 #include "email-debug-log.h"
53 INTERNAL_FUNC int emcore_get_default_mail_slot_count(int input_account_id, int *output_count)
55 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] output_count[%p]", input_account_id, output_count);
57 int err = EMAIL_ERROR_NONE;
58 int default_mail_slot_count = 25;
59 email_account_t *account_ref = NULL;
61 if (output_count == NULL) {
62 err = EMAIL_ERROR_INVALID_PARAM;
66 account_ref = emcore_get_account_reference(input_account_id);
68 default_mail_slot_count = account_ref->default_mail_slot_size;
73 emcore_free_account(account_ref);
74 EM_SAFE_FREE(account_ref);
78 *output_count = default_mail_slot_count;
80 EM_DEBUG_FUNC_END("err[%d]", err);
85 INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
87 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
90 int *mail_id_list = NULL, mail_id_list_count = 0;
91 int err = EMAIL_ERROR_NONE;
92 email_account_t *account_ref = NULL;
94 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
95 if (intput_mailbox_tbl)
96 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
97 err = EMAIL_ERROR_INVALID_PARAM;
101 account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
103 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
104 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
105 err = EMAIL_ERROR_NOT_SUPPORTED;
110 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)) {
111 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
112 EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
113 err = EMAIL_ERROR_NONE;
117 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
122 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)) {
123 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
130 EM_SAFE_FREE(mail_id_list);
133 emcore_free_account(account_ref);
134 EM_SAFE_FREE(account_ref);
140 EM_DEBUG_FUNC_END("ret [%d]", ret);
145 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
147 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
149 int ret = false, err = EMAIL_ERROR_NONE;
151 int account_count = 100;
152 int mailbox_count = 0;
153 email_account_t *account_ref = NULL;
154 emstorage_account_tbl_t *account_tbl_list = NULL;
155 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
157 if (account_id > ALL_ACCOUNT) {
158 account_ref = emcore_get_account_reference(account_id);
159 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
160 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
164 else if (!account_ref) {
165 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
168 if (mailbox_id == 0) {
169 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
170 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
176 if (mailbox_id == 0) {
177 if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
178 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
181 for ( i = 0; i < account_count; i++) {
182 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) {
183 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
191 if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
192 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
198 if (new_slot_size > 0) {
199 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
200 if(!mailbox_tbl_list) {
201 EM_DEBUG_EXCEPTION("em_malloc failed");
204 mailbox_tbl_list->account_id = account_id;
205 mailbox_tbl_list->mailbox_id = mailbox_id;
206 mailbox_tbl_list->mail_slot_size = new_slot_size;
208 else { /* read information from DB */
209 if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
210 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
217 if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
218 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
223 for (i = 0; i < mailbox_count; i++) {
224 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
225 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
226 err = EMAIL_ERROR_NONE;
228 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
237 emcore_free_account(account_ref);
238 EM_SAFE_FREE(account_ref);
241 if (account_tbl_list)
242 emstorage_free_account(&account_tbl_list, account_count, NULL);
244 if (mailbox_tbl_list)
245 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
253 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
255 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_name[%s], err_code[%p]", account_id, mailbox_name, err_code);
256 email_account_t *ref_account = NULL;
259 int err = EMAIL_ERROR_NONE;
261 ref_account = emcore_get_account_reference(account_id);
263 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
267 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
268 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
270 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
271 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
273 EM_DEBUG_EXCEPTION("em_malloc failed");
274 err = EMAIL_ERROR_OUT_OF_MEMORY;
278 memset(*path, 0x00, path_len);
280 /* 1. server address / server type */
281 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
282 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
284 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
287 /* 2. set tls option if security connection */
288 /*if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));*/
289 if (ref_account->incoming_server_secure_connection & 0x01) {
290 strncat(*path + 1, "/ssl", path_len-(EM_SAFE_STRLEN(*path)-1));
292 if (ref_account->incoming_server_secure_connection & 0x02)
293 strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));
295 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
297 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
298 strncat(*path + 1, "}", path_len-EM_SAFE_STRLEN(*path)-1);
301 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
308 emcore_free_account(ref_account);
309 EM_SAFE_FREE(ref_account);
318 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
320 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
323 int err = EMAIL_ERROR_NONE;
324 int status = EMAIL_DOWNLOAD_FAIL;
325 MAILSTREAM *stream = NULL;
326 email_internal_mailbox_t *mailbox_list = NULL;
327 email_account_t *ref_account = NULL;
328 void *tmp_stream = NULL;
329 char *mbox_path = NULL;
330 char *mailbox_name_for_mailbox_type = NULL;
331 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
333 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
334 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
336 if (!emcore_check_thread_status()) {
337 err = EMAIL_ERROR_CANCELLED;
340 if (!emnetwork_check_network_status(&err)) {
341 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
345 ref_account = emcore_get_account_reference(account_id);
347 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
348 err = EMAIL_ERROR_INVALID_ACCOUNT;
352 /* if not imap4 mail, return */
353 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
354 EM_DEBUG_EXCEPTION("unsupported account...");
355 err = EMAIL_ERROR_INVALID_ACCOUNT;
359 /* get mail server path */
360 /* mbox_path is not used. the below func might be unnecessary */
361 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
362 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
367 if (!emcore_check_thread_status()) {
368 err = EMAIL_ERROR_CANCELLED;
373 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
374 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
376 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
377 err = EMAIL_ERROR_CANCELLED;
379 err = EMAIL_ERROR_CONNECTION_FAILURE;
381 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
385 EM_SAFE_FREE(mbox_path);
387 stream = (MAILSTREAM *)tmp_stream;
389 if (!emcore_check_thread_status()) {
390 err = EMAIL_ERROR_CANCELLED;
394 /* download mailbox list */
395 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
396 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
400 if (!emcore_check_thread_status()) {
401 err = EMAIL_ERROR_CANCELLED;
405 for (i = 0; i < count; i++) {
406 if (!emcore_check_thread_status()) {
407 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
408 err = EMAIL_ERROR_CANCELLED;
411 if (mailbox_list[i].mailbox_name) {
412 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].mailbox_name);
413 mailbox_list[i].mail_slot_size = ref_account->default_mail_slot_size;
415 if(mailbox_list[i].mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
416 emcore_bind_mailbox_type(mailbox_list + i);
418 if (mailbox_list[i].mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
419 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
420 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].mailbox_name);
421 mailbox_list[i].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
424 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
427 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
428 if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
429 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
437 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
438 /* if (!emstorage_get_mailbox_name_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
439 if (mailbox_type_list[counter] == -1) {
440 /* EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed - %d", err); */
441 /* if (EMAIL_ERROR_MAILBOX_NOT_FOUND == err) */
443 emstorage_mailbox_tbl_t mailbox_tbl;
445 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
447 mailbox_tbl.account_id = account_id;
448 mailbox_tbl.mailbox_id = 0;
449 mailbox_tbl.local_yn = 1;
450 mailbox_tbl.mailbox_type = counter;
451 mailbox_tbl.deleted_flag = 0;
452 mailbox_tbl.modifiable_yn = 1;
453 mailbox_tbl.total_mail_count_on_server = 0;
454 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
457 case EMAIL_MAILBOX_TYPE_SENTBOX:
458 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
459 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
462 case EMAIL_MAILBOX_TYPE_TRASH:
463 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
464 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
467 case EMAIL_MAILBOX_TYPE_DRAFT:
468 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
469 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
472 case EMAIL_MAILBOX_TYPE_SPAMBOX:
473 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
474 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
477 case EMAIL_MAILBOX_TYPE_OUTBOX:
478 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
479 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
483 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
484 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
488 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
489 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
497 /* goto FINISH_OFF; */
501 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
504 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
507 email_mailbox_t mailbox;
509 if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
510 if (local_mailbox_list) {
511 for (i = 0; i < select_num; i++) {
512 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
513 mailbox.account_id = local_mailbox_list[i].account_id;
514 mailbox.mailbox_name = local_mailbox_list[i].mailbox_name;
515 mailbox.mailbox_id = local_mailbox_list[i].mailbox_id;
516 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
517 EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.mailbox_name);
518 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
519 local_mailbox_list = NULL;
523 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
524 local_mailbox_list = NULL;
528 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
529 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
533 if (!emcore_check_thread_status()) {
534 err = EMAIL_ERROR_CANCELLED;
538 for (i = 0; i < count; i++)
539 mailbox_list[i].account_id = account_id;
546 if (err == EMAIL_ERROR_NONE) {
547 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
548 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
551 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
552 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
554 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
555 EM_SAFE_FREE(mbox_path);
558 emcore_free_account(ref_account);
559 EM_SAFE_FREE(ref_account);
563 emcore_close_mailbox(account_id, stream);
566 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
568 if (err_code != NULL)
570 EM_DEBUG_FUNC_END("ret [%d]", ret);
574 int emcore_download_mailbox_list(void *mail_stream,
576 email_internal_mailbox_t **mailbox_list,
580 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);
582 MAILSTREAM *stream = mail_stream;
583 email_callback_holder_t holder;
584 char *pattern = NULL;
585 char *reference = NULL;
586 int err = EMAIL_ERROR_NONE;
589 if (!stream || !mailbox_list || !count) {
590 err = EMAIL_ERROR_INVALID_PARAM;
594 memset(&holder, 0x00, sizeof(holder));
596 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
599 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
601 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
602 if ((s = strchr(reference, '}')))
604 strcat(reference, mailbox_name);
608 reference = EM_SAFE_STRDUP(stream->original_mailbox);
611 stream->sparep = &holder;
613 /* imap command : tag LIST reference * */
614 /* see callback function mm_list */
615 mail_list(stream, reference, pattern);
617 stream->sparep = NULL;
619 EM_SAFE_FREE(reference);
622 *mailbox_list = (email_internal_mailbox_t*)holder.data;
634 * check whether this imap mailbox is synchronous mailbox
636 * mailbox : imap mailbox to be checked
637 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
642 int emcore_check_sync_imap_mailbox(email_mailbox_t *mailbox, int *synchronous, int *err_code)
644 EM_DEBUG_FUNC_BEGIN();
646 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
649 *err_code = EMAIL_ERROR_NONE;
652 if (!mailbox || !synchronous) {
653 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
655 if (err_code != NULL)
656 *err_code = EMAIL_ERROR_INVALID_PARAM;
661 int err = EMAIL_ERROR_NONE;
662 emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
664 if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl, true, &err)) {
665 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
669 *synchronous = imap_mailbox_tbl ? 1 : 0;
674 if (imap_mailbox_tbl != NULL)
675 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
677 if (err_code != NULL)
685 * set sync imap mailbox
687 * mailbox_list : imap mailbox to be synced
688 * syncronous : 0-sync 1 : non-sync
694 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(email_internal_mailbox_t *mailbox, int synchronous, int *err_code)
696 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
699 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
700 if (err_code != NULL)
701 *err_code = EMAIL_ERROR_INVALID_PARAM;
706 int err = EMAIL_ERROR_NONE;
707 emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
708 emstorage_mailbox_tbl_t mailbox_tbl = { 0, };
709 emcore_uid_list *uid_list = NULL;
710 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
711 MAILSTREAM *stream = mailbox->mail_stream;
712 int mailbox_renamed = 0;
716 IMAPLOCAL *imap_local = NULL;
717 char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
720 /* if synchcronous, insert imap mailbox to db */
721 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl_item, true, &err)) {
722 /* mailbox already exists */
723 /* mailbox Found, Do set the modifiable_yn = 1 */
724 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
725 if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->mailbox_name, 1, true, &err)) {
726 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
731 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
732 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
733 /* This is error scenario so finish the job */
737 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
738 /* Now check if mailbox is renamed */
739 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
741 imap_local = ((MAILSTREAM *)stream)->local;
743 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
745 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->mailbox_name);
750 /* select the mailbox and get its UID */
751 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
752 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->mailbox_name);
754 err = EMAIL_ERROR_CONNECTION_BROKEN;
756 imap_local->netstream = NULL;
757 mailbox->mail_stream = NULL;
762 EM_DEBUG_LOG("Get response for select call");
763 while (imap_local->netstream) {
764 p = net_getline(imap_local->netstream);
765 EM_DEBUG_LOG("p =[%s]", p);
766 if (!strncmp(p, "+", 1)) {
770 else if (!strncmp(p, "*", 1)) {
781 /* check if OK or BAD response comes. */
782 /* if response is OK the try getting UID list. */
783 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
784 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->mailbox_name);
785 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
786 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
787 EM_SAFE_FREE(uid_list);
790 if (!emstorage_get_downloaded_list(mailbox->account_id, 0, &downloaded_uids, &j, true, &err)) {
791 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
793 downloaded_uids = NULL;
795 else /* Prevent Defect - 28497 */ {
796 emcore_uid_list *uid_elem = uid_list;
797 emcore_uid_list *next_uid_elem = NULL;
799 for (i = j; (i > 0 && !mailbox_renamed); i--) {
801 next_uid_elem = uid_elem->next;
802 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
807 EM_SAFE_FREE(uid_elem->uid);
808 uid_elem = next_uid_elem;
814 } /* mailbox selected */
817 if (mailbox_renamed) /* renamed mailbox */ {
818 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
819 /* Do a mailbox rename in the DB */
820 if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->mailbox_name, true, &err))
821 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
825 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
827 mailbox_tbl.account_id = mailbox->account_id;
828 mailbox_tbl.local_yn = 0;
829 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
830 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
832 /* Get the Alias Name after parsing the Full mailbox Path */
833 if(mailbox->alias == NULL)
834 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
836 mailbox_tbl.alias = mailbox->alias;
837 mailbox_tbl.deleted_flag = 1;
838 mailbox_tbl.modifiable_yn = 1;
839 mailbox_tbl.total_mail_count_on_server = 0;
841 /* if non synchronous, delete imap mailbox from db */
842 if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_id, &mailbox_tbl, true, &err)) {
843 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
848 else /* Its a Fresh Mailbox */ {
849 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
851 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
852 mailbox_tbl.account_id = mailbox->account_id;
853 mailbox_tbl.local_yn = 0;
854 mailbox_tbl.deleted_flag = 0;
855 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
856 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
857 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
858 mailbox_tbl.no_select = mailbox->no_select;
860 /* Get the Alias Name after Parsing the Full mailbox Path */
861 if(mailbox->alias == NULL)
862 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
864 if (mailbox->alias) {
865 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
867 mailbox_tbl.alias = mailbox->alias;
868 mailbox_tbl.modifiable_yn = 1;
869 mailbox_tbl.total_mail_count_on_server = 0;
871 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
873 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
874 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
883 /* set sync db mailbox */
884 mailbox->synchronous = synchronous;
891 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
893 if (imap_mailbox_tbl_item)
894 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
903 * create a new imap mailbox
905 * new_mailbox : imap mailbox to be created
910 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
912 MAILSTREAM *stream = NULL;
913 char *long_enc_path = NULL;
914 void *tmp_stream = NULL;
916 int err = EMAIL_ERROR_NONE;
918 EM_DEBUG_FUNC_BEGIN();
921 err = EMAIL_ERROR_INVALID_PARAM;
925 /* connect mail server */
927 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
928 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
932 stream = (MAILSTREAM *)tmp_stream;
934 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
935 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
936 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
941 if (!mail_create(stream, long_enc_path)) {
942 EM_DEBUG_EXCEPTION("mail_create failed");
943 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
947 emcore_close_mailbox(0, stream);
950 EM_SAFE_FREE(long_enc_path);
956 emcore_close_mailbox(0, stream);
960 EM_SAFE_FREE(long_enc_path);
963 if (err == EMAIL_ERROR_NONE) {
964 if(!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
965 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
967 else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
968 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
979 * delete a imap mailbox
981 * input_mailbox_id : mailbox ID to be deleted
986 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
988 EM_DEBUG_FUNC_BEGIN();
990 MAILSTREAM *stream = NULL;
991 char *long_enc_path = NULL;
992 email_account_t *ref_account = NULL;
993 void *tmp_stream = NULL;
995 int err = EMAIL_ERROR_NONE;
996 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
998 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
999 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
1003 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
1005 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1006 EM_DEBUG_EXCEPTION("Invalid account information");
1007 err = EMAIL_ERROR_INVALID_ACCOUNT;
1011 /* connect mail server */
1012 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
1013 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
1017 stream = (MAILSTREAM *)tmp_stream;
1019 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1020 if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
1021 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
1025 /* delete mailbox */
1026 if (!mail_delete(stream, long_enc_path)) {
1027 EM_DEBUG_EXCEPTION("mail_delete failed");
1028 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
1032 emcore_close_mailbox(0, stream);
1035 EM_SAFE_FREE(long_enc_path);
1041 emcore_close_mailbox(0, stream);
1046 emcore_free_account(ref_account);
1047 EM_SAFE_FREE(ref_account);
1050 EM_SAFE_FREE(long_enc_path);
1053 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1055 if (err == EMAIL_ERROR_NONE) {
1056 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
1057 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
1059 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
1060 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
1069 INTERNAL_FUNC int emcore_move_mailbox_on_imap_server(int input_account_id, char *input_old_mailbox_path, char *input_new_mailbox_path)
1071 EM_DEBUG_FUNC_BEGIN("input_account_id [%d], input_old_mailbox_path [%p], input_new_mailbox_path [%p]", input_account_id, input_old_mailbox_path, input_new_mailbox_path);
1072 MAILSTREAM *stream = NULL;
1073 char *long_enc_path_old = NULL;
1074 char *long_enc_path_new = NULL;
1075 email_account_t *ref_account = NULL;
1076 void *tmp_stream = NULL;
1077 int err = EMAIL_ERROR_NONE;
1079 if (!input_old_mailbox_path || !input_new_mailbox_path) {
1080 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1081 err = EMAIL_ERROR_INVALID_PARAM;
1085 ref_account = emcore_get_account_reference(input_account_id);
1087 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1088 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
1089 err = EMAIL_ERROR_INVALID_ACCOUNT;
1093 /* connect mail server */
1095 if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
1096 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
1100 stream = (MAILSTREAM *)tmp_stream;
1102 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1103 if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
1104 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1108 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1109 if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
1110 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1114 /* rename mailbox */
1115 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1116 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
1121 EM_SAFE_FREE(long_enc_path_old);
1122 EM_SAFE_FREE(long_enc_path_new);
1125 emcore_free_account(ref_account);
1126 EM_SAFE_FREE(ref_account);
1130 emcore_close_mailbox(0, stream);
1134 EM_DEBUG_FUNC_END("err [%d]", err);