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 *output_count = default_mail_slot_count;
75 EM_DEBUG_FUNC_END("err[%d]", err);
80 INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
82 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
85 int *mail_id_list = NULL, mail_id_list_count = 0;
86 int err = EMAIL_ERROR_NONE;
87 email_account_t *account_ref = NULL;
89 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
90 if (intput_mailbox_tbl)
91 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
92 err = EMAIL_ERROR_INVALID_PARAM;
96 account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
98 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
99 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
100 err = EMAIL_ERROR_NOT_SUPPORTED;
105 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)) {
106 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
107 EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
108 err = EMAIL_ERROR_NONE;
112 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
117 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)) {
118 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
125 EM_SAFE_FREE(mail_id_list);
130 EM_DEBUG_FUNC_END("ret [%d]", ret);
135 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
137 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
139 int ret = false, err = EMAIL_ERROR_NONE;
141 int account_count = 100;
142 int mailbox_count = 0;
143 email_account_t *account_ref = NULL;
144 emstorage_account_tbl_t *account_tbl_list = NULL;
145 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
147 if (account_id > ALL_ACCOUNT) {
148 account_ref = emcore_get_account_reference(account_id);
149 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
150 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
154 else if (!account_ref) {
155 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
158 if (mailbox_id == 0) {
159 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
160 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
166 if (mailbox_id == 0) {
167 if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
168 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
171 for ( i = 0; i < account_count; i++) {
172 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) {
173 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
181 if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
182 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
188 if (new_slot_size > 0) {
189 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
190 if(!mailbox_tbl_list) {
191 EM_DEBUG_EXCEPTION("em_malloc failed");
194 mailbox_tbl_list->account_id = account_id;
195 mailbox_tbl_list->mailbox_id = mailbox_id;
196 mailbox_tbl_list->mail_slot_size = new_slot_size;
198 else { /* read information from DB */
199 if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
200 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
207 if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
208 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
213 for (i = 0; i < mailbox_count; i++) {
214 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
215 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
216 err = EMAIL_ERROR_NONE;
218 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
226 if (account_tbl_list)
227 emstorage_free_account(&account_tbl_list, account_count, NULL);
229 if (mailbox_tbl_list)
230 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
238 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
240 email_account_t *ref_account = NULL;
244 ref_account = emcore_get_account_reference(account_id);
246 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
250 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
251 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
253 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
254 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
257 memset(*path, 0x00, path_len);
258 /* 1. server address / server type */
260 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
261 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
264 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
267 /* 2. set tls option if security connection */
268 /* if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1)); */
269 if (ref_account->incoming_server_secure_connection & 0x01) {
270 strncat(*path + 1, "/ssl", path_len-(EM_SAFE_STRLEN(*path)-1));
272 if (ref_account->incoming_server_secure_connection & 0x02)
273 strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));
275 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
277 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
278 strncat(*path + 1, "}", path_len-EM_SAFE_STRLEN(*path)-1);
281 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
286 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
288 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
291 int err = EMAIL_ERROR_NONE;
292 int status = EMAIL_DOWNLOAD_FAIL;
293 MAILSTREAM *stream = NULL;
294 email_internal_mailbox_t *mailbox_list = NULL;
295 email_account_t *ref_account = NULL;
296 void *tmp_stream = NULL;
297 char *mbox_path = NULL;
298 char *mailbox_name_for_mailbox_type = NULL;
299 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
301 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
302 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
304 if (!emcore_check_thread_status()) {
305 err = EMAIL_ERROR_CANCELLED;
308 if (!emnetwork_check_network_status(&err)) {
309 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
313 ref_account = emcore_get_account_reference(account_id);
315 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
316 err = EMAIL_ERROR_INVALID_ACCOUNT;
320 /* if not imap4 mail, return */
321 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
322 EM_DEBUG_EXCEPTION("unsupported account...");
323 err = EMAIL_ERROR_INVALID_ACCOUNT;
327 /* get mail server path */
328 /* mbox_path is not used. the below func might be unnecessary */
329 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
330 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
335 if (!emcore_check_thread_status()) {
336 err = EMAIL_ERROR_CANCELLED;
341 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
342 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
344 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
345 err = EMAIL_ERROR_CANCELLED;
347 err = EMAIL_ERROR_CONNECTION_FAILURE;
349 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
353 EM_SAFE_FREE(mbox_path);
355 stream = (MAILSTREAM *)tmp_stream;
357 if (!emcore_check_thread_status()) {
358 err = EMAIL_ERROR_CANCELLED;
362 /* download mailbox list */
363 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
364 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
368 if (!emcore_check_thread_status()) {
369 err = EMAIL_ERROR_CANCELLED;
373 for (i = 0; i < count; i++) {
374 if (!emcore_check_thread_status()) {
375 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
376 err = EMAIL_ERROR_CANCELLED;
379 if (mailbox_list[i].mailbox_name) {
380 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].mailbox_name);
381 mailbox_list[i].mail_slot_size = ref_account->default_mail_slot_size;
383 if(mailbox_list[i].mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
384 emcore_bind_mailbox_type(mailbox_list + i);
386 if (mailbox_list[i].mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
387 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
388 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].mailbox_name);
389 mailbox_list[i].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
392 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
395 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
396 if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
397 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
405 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
406 /* if (!emstorage_get_mailbox_name_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
407 if (mailbox_type_list[counter] == -1) {
408 /* EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed - %d", err); */
409 /* if (EMAIL_ERROR_MAILBOX_NOT_FOUND == err) */
411 emstorage_mailbox_tbl_t mailbox_tbl;
413 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
415 mailbox_tbl.account_id = account_id;
416 mailbox_tbl.mailbox_id = 0;
417 mailbox_tbl.local_yn = 1;
418 mailbox_tbl.mailbox_type = counter;
419 mailbox_tbl.deleted_flag = 0;
420 mailbox_tbl.modifiable_yn = 1;
421 mailbox_tbl.total_mail_count_on_server = 0;
422 mailbox_tbl.mail_slot_size = ref_account->default_mail_slot_size;
425 case EMAIL_MAILBOX_TYPE_SENTBOX:
426 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
427 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
430 case EMAIL_MAILBOX_TYPE_TRASH:
431 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
432 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
435 case EMAIL_MAILBOX_TYPE_DRAFT:
436 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
437 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
440 case EMAIL_MAILBOX_TYPE_SPAMBOX:
441 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
442 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
445 case EMAIL_MAILBOX_TYPE_OUTBOX:
446 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
447 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
451 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
452 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
456 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
457 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
465 /* goto FINISH_OFF; */
469 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
472 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
475 email_mailbox_t mailbox;
477 if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
478 if (local_mailbox_list) {
479 for (i = 0; i < select_num; i++) {
480 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
481 mailbox.account_id = local_mailbox_list[i].account_id;
482 mailbox.mailbox_name = local_mailbox_list[i].mailbox_name;
483 mailbox.mailbox_id = local_mailbox_list[i].mailbox_id;
484 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
485 EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.mailbox_name);
486 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
487 local_mailbox_list = NULL;
491 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
492 local_mailbox_list = NULL;
496 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
497 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
501 if (!emcore_check_thread_status()) {
502 err = EMAIL_ERROR_CANCELLED;
506 for (i = 0; i < count; i++)
507 mailbox_list[i].account_id = account_id;
514 if (err == EMAIL_ERROR_NONE) {
515 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
516 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
519 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
520 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
522 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
523 EM_SAFE_FREE(mbox_path);
526 emcore_close_mailbox(account_id, stream);
529 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
531 if (err_code != NULL)
533 EM_DEBUG_FUNC_END("ret [%d]", ret);
537 int emcore_download_mailbox_list(void *mail_stream,
539 email_internal_mailbox_t **mailbox_list,
543 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);
545 MAILSTREAM *stream = mail_stream;
546 email_callback_holder_t holder;
547 char *pattern = NULL;
548 char *reference = NULL;
549 int err = EMAIL_ERROR_NONE;
552 if (!stream || !mailbox_list || !count) {
553 err = EMAIL_ERROR_INVALID_PARAM;
557 memset(&holder, 0x00, sizeof(holder));
559 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
562 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
564 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
565 if ((s = strchr(reference, '}')))
567 strcat(reference, mailbox_name);
571 reference = EM_SAFE_STRDUP(stream->original_mailbox);
574 stream->sparep = &holder;
576 /* imap command : tag LIST reference * */
577 /* see callback function mm_list */
578 mail_list(stream, reference, pattern);
580 stream->sparep = NULL;
582 EM_SAFE_FREE(reference);
585 *mailbox_list = (email_internal_mailbox_t*)holder.data;
597 * check whether this imap mailbox is synchronous mailbox
599 * mailbox : imap mailbox to be checked
600 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
605 int emcore_check_sync_imap_mailbox(email_mailbox_t *mailbox, int *synchronous, int *err_code)
607 EM_DEBUG_FUNC_BEGIN();
609 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
612 *err_code = EMAIL_ERROR_NONE;
615 if (!mailbox || !synchronous) {
616 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
618 if (err_code != NULL)
619 *err_code = EMAIL_ERROR_INVALID_PARAM;
624 int err = EMAIL_ERROR_NONE;
625 emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
627 if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl, true, &err)) {
628 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
632 *synchronous = imap_mailbox_tbl ? 1 : 0;
637 if (imap_mailbox_tbl != NULL)
638 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
640 if (err_code != NULL)
648 * set sync imap mailbox
650 * mailbox_list : imap mailbox to be synced
651 * syncronous : 0-sync 1 : non-sync
657 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(email_internal_mailbox_t *mailbox, int synchronous, int *err_code)
659 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
662 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
663 if (err_code != NULL)
664 *err_code = EMAIL_ERROR_INVALID_PARAM;
669 int err = EMAIL_ERROR_NONE;
670 emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
671 emstorage_mailbox_tbl_t mailbox_tbl = { 0, };
672 emcore_uid_list *uid_list = NULL;
673 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
674 MAILSTREAM *stream = mailbox->mail_stream;
675 int mailbox_renamed = 0;
679 IMAPLOCAL *imap_local = NULL;
680 char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
683 /* if synchcronous, insert imap mailbox to db */
684 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl_item, true, &err)) {
685 /* mailbox already exists */
686 /* mailbox Found, Do set the modifiable_yn = 1 */
687 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
688 if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->mailbox_name, 1, true, &err)) {
689 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
694 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
695 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
696 /* This is error scenario so finish the job */
700 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
701 /* Now check if mailbox is renamed */
702 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
704 imap_local = ((MAILSTREAM *)stream)->local;
706 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
708 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->mailbox_name);
713 /* select the mailbox and get its UID */
714 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
715 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->mailbox_name);
717 err = EMAIL_ERROR_CONNECTION_BROKEN;
719 imap_local->netstream = NULL;
720 mailbox->mail_stream = NULL;
725 EM_DEBUG_LOG("Get response for select call");
726 while (imap_local->netstream) {
727 p = net_getline(imap_local->netstream);
728 EM_DEBUG_LOG("p =[%s]", p);
729 if (!strncmp(p, "+", 1)) {
733 else if (!strncmp(p, "*", 1)) {
744 /* check if OK or BAD response comes. */
745 /* if response is OK the try getting UID list. */
746 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
747 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->mailbox_name);
748 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
749 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
750 EM_SAFE_FREE(uid_list);
753 if (!emstorage_get_downloaded_list(mailbox->account_id, 0, &downloaded_uids, &j, true, &err)) {
754 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
756 downloaded_uids = NULL;
758 else /* Prevent Defect - 28497 */ {
759 emcore_uid_list *uid_elem = uid_list;
760 emcore_uid_list *next_uid_elem = NULL;
762 for (i = j; (i > 0 && !mailbox_renamed); i--) {
764 next_uid_elem = uid_elem->next;
765 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
770 EM_SAFE_FREE(uid_elem->uid);
771 uid_elem = next_uid_elem;
777 } /* mailbox selected */
780 if (mailbox_renamed) /* renamed mailbox */ {
781 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
782 /* Do a mailbox rename in the DB */
783 if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->mailbox_name, true, &err))
784 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
788 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
790 mailbox_tbl.account_id = mailbox->account_id;
791 mailbox_tbl.local_yn = 0;
792 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
793 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
795 /* Get the Alias Name after parsing the Full mailbox Path */
796 if(mailbox->alias == NULL)
797 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
799 mailbox_tbl.alias = mailbox->alias;
800 mailbox_tbl.deleted_flag = 1;
801 mailbox_tbl.modifiable_yn = 1;
802 mailbox_tbl.total_mail_count_on_server = 0;
804 /* if non synchronous, delete imap mailbox from db */
805 if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_id, &mailbox_tbl, true, &err)) {
806 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
811 else /* Its a Fresh Mailbox */ {
812 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
814 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
815 mailbox_tbl.account_id = mailbox->account_id;
816 mailbox_tbl.local_yn = 0;
817 mailbox_tbl.deleted_flag = 0;
818 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
819 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
820 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
821 mailbox_tbl.no_select = mailbox->no_select;
823 /* Get the Alias Name after Parsing the Full mailbox Path */
824 if(mailbox->alias == NULL)
825 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
827 if (mailbox->alias) {
828 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
830 mailbox_tbl.alias = mailbox->alias;
831 mailbox_tbl.modifiable_yn = 1;
832 mailbox_tbl.total_mail_count_on_server = 0;
834 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
836 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
837 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
846 /* set sync db mailbox */
847 mailbox->synchronous = synchronous;
854 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
856 if (imap_mailbox_tbl_item)
857 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
866 * create a new imap mailbox
868 * new_mailbox : imap mailbox to be created
873 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
875 MAILSTREAM *stream = NULL;
876 char *long_enc_path = NULL;
877 void *tmp_stream = NULL;
879 int err = EMAIL_ERROR_NONE;
881 EM_DEBUG_FUNC_BEGIN();
884 err = EMAIL_ERROR_INVALID_PARAM;
888 /* connect mail server */
890 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
891 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
895 stream = (MAILSTREAM *)tmp_stream;
897 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
898 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
899 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
904 if (!mail_create(stream, long_enc_path)) {
905 EM_DEBUG_EXCEPTION("mail_create failed");
906 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
910 emcore_close_mailbox(0, stream);
913 EM_SAFE_FREE(long_enc_path);
919 emcore_close_mailbox(0, stream);
923 EM_SAFE_FREE(long_enc_path);
926 if (err == EMAIL_ERROR_NONE) {
927 if(!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
928 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
930 else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
931 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
942 * delete a imap mailbox
944 * input_mailbox_id : mailbox ID to be deleted
949 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
951 EM_DEBUG_FUNC_BEGIN();
953 MAILSTREAM *stream = NULL;
954 char *long_enc_path = NULL;
955 email_account_t *ref_account = NULL;
956 void *tmp_stream = NULL;
958 int err = EMAIL_ERROR_NONE;
959 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
961 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
962 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
966 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
968 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
969 EM_DEBUG_EXCEPTION("Invalid account information");
970 err = EMAIL_ERROR_INVALID_ACCOUNT;
974 /* connect mail server */
975 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
976 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
980 stream = (MAILSTREAM *)tmp_stream;
982 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
983 if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
984 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
989 if (!mail_delete(stream, long_enc_path)) {
990 EM_DEBUG_EXCEPTION("mail_delete failed");
991 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
995 emcore_close_mailbox(0, stream);
998 EM_SAFE_FREE(long_enc_path);
1004 emcore_close_mailbox(0, stream);
1008 EM_SAFE_FREE(long_enc_path);
1011 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1013 if (err == EMAIL_ERROR_NONE) {
1014 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
1015 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
1017 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
1018 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
1027 INTERNAL_FUNC int emcore_move_mailbox_on_imap_server(int input_account_id, char *input_old_mailbox_path, char *input_new_mailbox_path)
1029 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);
1030 MAILSTREAM *stream = NULL;
1031 char *long_enc_path_old = NULL;
1032 char *long_enc_path_new = NULL;
1033 email_account_t *ref_account = NULL;
1034 void *tmp_stream = NULL;
1035 int err = EMAIL_ERROR_NONE;
1037 if (!input_old_mailbox_path || !input_new_mailbox_path) {
1038 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1039 err = EMAIL_ERROR_INVALID_PARAM;
1043 ref_account = emcore_get_account_reference(input_account_id);
1045 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1046 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
1047 err = EMAIL_ERROR_INVALID_ACCOUNT;
1051 /* connect mail server */
1053 if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
1054 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
1058 stream = (MAILSTREAM *)tmp_stream;
1060 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1061 if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
1062 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1066 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1067 if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
1068 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1072 /* rename mailbox */
1073 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1074 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
1079 EM_SAFE_FREE(long_enc_path_old);
1080 EM_SAFE_FREE(long_enc_path_new);
1083 emcore_close_mailbox(0, stream);
1087 EM_DEBUG_FUNC_END("err [%d]", err);