4 * Copyright (c) 2000 - 2011 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-core-event.h"
43 #include "email-core-mailbox.h"
44 #include "email-core-imap-mailbox.h"
45 #include "email-core-mailbox-sync.h"
46 #include "email-core-account.h"
49 #include "email-debug-log.h"
51 INTERNAL_FUNC int emcore_get_default_mail_slot_count(int *output_count, int *err_code)
53 EM_DEBUG_FUNC_BEGIN();
54 EM_DEBUG_LOG("output_count[%p], err_code[%p]", output_count, err_code);
56 int err = EMF_ERROR_NONE;
58 int ret = false, ret2;
60 if (output_count == NULL) {
61 err = EMF_ERROR_INVALID_PARAM;
65 ret2 = vconf_get_int(VCONF_KEY_DEFAULT_SLOT_SIZE, &mail_slot_count);
68 EM_DEBUG_EXCEPTION("vconf_get_int() Failed(%d)", ret2);
69 mail_slot_count = 100;
77 *output_count = mail_slot_count;
87 INTERNAL_FUNC int emcore_remove_overflowed_mails(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 = EMF_ERROR_NONE;
94 emf_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 = EMF_ERROR_INVALID_PARAM;
103 account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
105 if (account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
106 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
107 err = EMF_ERROR_NOT_SUPPORTED;
112 if (!emstorage_get_overflowed_mail_id_list(intput_mailbox_tbl->account_id, intput_mailbox_tbl->mailbox_name, intput_mailbox_tbl->mail_slot_size, &mail_id_list, &mail_id_list_count, true, &err)) {
113 if (err == EMF_ERROR_MAIL_NOT_FOUND) {
114 EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
115 err = EMF_ERROR_NONE;
119 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
124 if (!emcore_delete_mail(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, false, EMF_DELETED_BY_OVERFLOW, false, &err)) {
125 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
132 EM_SAFE_FREE(mail_id_list);
137 EM_DEBUG_FUNC_END("ret [%d]", ret);
142 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, char *mailbox_name, int new_slot_size, int *err_code)
144 EM_DEBUG_FUNC_BEGIN();
145 EM_DEBUG_LOG("account_id [%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
147 int ret = false, err = EMF_ERROR_NONE;
148 int i, mailbox_count = 0;
149 emf_account_t *account_ref = NULL;
150 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
152 if (account_id > ALL_ACCOUNT) {
153 account_ref = emcore_get_account_reference(account_id);
154 if (account_ref && account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
155 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
159 else if (!account_ref) {
160 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
165 if (!emstorage_set_mail_slot_size(account_id, mailbox_name, new_slot_size, true, &err)) {
166 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
172 if (new_slot_size > 0) {
173 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
174 if(!mailbox_tbl_list) {
175 EM_DEBUG_EXCEPTION("em_malloc failed");
178 mailbox_tbl_list->account_id = account_id;
179 mailbox_tbl_list->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
180 mailbox_tbl_list->mail_slot_size = new_slot_size;
182 else { /* read information from DB */
183 if (!emstorage_get_mailbox_by_name(account_id, EMF_MAILBOX_ALL, mailbox_name, &mailbox_tbl_list, true, &err)) {
184 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
191 if (!emstorage_get_mailbox(account_id, EMF_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
192 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
197 for (i = 0; i < mailbox_count; i++) {
198 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
199 if (err == EMF_ERROR_MAIL_NOT_FOUND || err == EMF_ERROR_NOT_SUPPORTED)
200 err = EMF_ERROR_NONE;
202 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
211 if (mailbox_tbl_list)
212 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
221 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
223 emf_account_t *ref_account = NULL;
227 ref_account = emcore_get_account_reference(account_id);
229 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
233 path_len = strlen(ref_account->receiving_server_addr) +
234 (mailbox_name ? strlen(mailbox_name) : 0) + 50;
236 *path = em_malloc(path_len);/* strlen(ref_account->receiving_server_addr) + */
237 /* (mailbox_name ? strlen(mailbox_name) : 0) + 20); */
240 memset(*path, 0x00, path_len);
241 /* 1. server address / server type */
243 if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
244 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->receiving_server_addr, ref_account->port_num);
247 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->receiving_server_addr, ref_account->port_num);
250 /* 2. set tls option if security connection */
251 /* if (ref_account->use_security) strncat(*path + 1, "/tls", path_len-(strlen(*path)-1)); */
252 if (ref_account->use_security & 0x01) {
253 strncat(*path + 1, "/ssl", path_len-(strlen(*path)-1));
255 if (ref_account->use_security & 0x02)
256 strncat(*path + 1, "/tls", path_len-(strlen(*path)-1));
258 strncat(*path + 1, "/notls", path_len-(strlen(*path)-1));
260 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
261 strncat(*path + 1, "}", path_len-strlen(*path)-1);
264 if (mailbox_name) strncat(*path, mailbox_name, path_len-strlen(*path)-1);
269 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int *err_code)
271 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
274 int err = EMF_ERROR_NONE;
275 int status = EMF_DOWNLOAD_FAIL;
277 MAILSTREAM *stream = NULL;
278 emf_mailbox_t *mailbox_list = NULL;
279 emf_account_t *ref_account = NULL;
280 void *tmp_stream = NULL;
281 char *mbox_path = NULL;
282 int i = 0, count = 0, counter = 0, mailbox_type_list[EMF_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
283 char *mailbox_name_for_mailbox_type = NULL;
287 *err_code = EMF_ERROR_NONE;
289 if (!emcore_check_thread_status()) {
290 err = EMF_ERROR_CANCELLED;
294 ref_account = emcore_get_account_reference(account_id);
296 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
297 err = EMF_ERROR_INVALID_ACCOUNT;
301 /* if not imap4 mail, return */
302 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE || ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
303 EM_DEBUG_EXCEPTION("unsupported account...");
304 err = EMF_ERROR_INVALID_ACCOUNT;
308 /* get mail server path */
309 /* mbox_path is not used. the below func might be unnecessary */
310 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
311 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
316 if (!emcore_check_thread_status()) {
317 err = EMF_ERROR_CANCELLED;
322 if (!emcore_connect_to_remote_mailbox(account_id, NULL, (void **)&tmp_stream, &err) || !tmp_stream) {
323 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
325 if (err == EMF_ERROR_CONNECTION_BROKEN)
326 err = EMF_ERROR_CANCELLED;
328 err = EMF_ERROR_CONNECTION_FAILURE;
330 status = EMF_DOWNLOAD_CONNECTION_FAIL;
334 EM_SAFE_FREE(mbox_path);
336 stream = (MAILSTREAM *)tmp_stream;
338 if (!emcore_check_thread_status()) {
339 err = EMF_ERROR_CANCELLED;
343 /* download mailbox list */
344 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
345 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
349 if (!emcore_check_thread_status()) {
350 err = EMF_ERROR_CANCELLED;
354 for (i = 0; i < count; i++) {
355 if (!emcore_check_thread_status()) {
356 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
357 err = EMF_ERROR_CANCELLED;
360 if (mailbox_list[i].name) {
361 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].name);
362 emcore_get_default_mail_slot_count(&(mailbox_list[i].mail_slot_size), NULL);
363 emcore_bind_mailbox_type(mailbox_list + i);
365 if (mailbox_list[i].mailbox_type <= EMF_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
366 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
367 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].name);
368 mailbox_list[i].mailbox_type = EMF_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
371 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
374 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
375 if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
376 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
384 for (counter = EMF_MAILBOX_TYPE_INBOX; counter <= EMF_MAILBOX_TYPE_OUTBOX; counter++) {
385 /* if (!emstorage_get_mailboxname_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
386 if (mailbox_type_list[counter] == -1) {
387 /* EM_DEBUG_EXCEPTION("emstorage_get_mailboxname_by_mailbox_type failed - %d", err); */
388 /* if (EMF_ERROR_MAILBOX_NOT_FOUND == err) */
390 emstorage_mailbox_tbl_t mailbox_tbl;
392 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
394 mailbox_tbl.account_id = account_id;
395 mailbox_tbl.mailbox_id = 0;
396 mailbox_tbl.local_yn = 1;
397 mailbox_tbl.mailbox_type = counter;
398 mailbox_tbl.sync_with_server_yn = 1;
399 mailbox_tbl.modifiable_yn = 1;
400 mailbox_tbl.total_mail_count_on_server = 0;
401 emcore_get_default_mail_slot_count(&mailbox_tbl.mail_slot_size, NULL);
404 case EMF_MAILBOX_TYPE_SENTBOX:
405 mailbox_tbl.mailbox_name = EMF_SENTBOX_NAME;
406 mailbox_tbl.alias = EMF_SENTBOX_DISPLAY_NAME;
409 case EMF_MAILBOX_TYPE_TRASH:
410 mailbox_tbl.mailbox_name = EMF_TRASH_NAME;
411 mailbox_tbl.alias = EMF_TRASH_DISPLAY_NAME;
414 case EMF_MAILBOX_TYPE_DRAFT:
415 mailbox_tbl.mailbox_name = EMF_DRAFTBOX_NAME;
416 mailbox_tbl.alias = EMF_DRAFTBOX_DISPLAY_NAME;
419 case EMF_MAILBOX_TYPE_SPAMBOX:
420 mailbox_tbl.mailbox_name = EMF_SPAMBOX_NAME;
421 mailbox_tbl.alias = EMF_SPAMBOX_DISPLAY_NAME;
424 case EMF_MAILBOX_TYPE_OUTBOX:
425 mailbox_tbl.mailbox_name = EMF_OUTBOX_NAME;
426 mailbox_tbl.alias = EMF_OUTBOX_DISPLAY_NAME;
430 mailbox_tbl.mailbox_name = EMF_INBOX_NAME;
431 mailbox_tbl.alias = EMF_INBOX_DISPLAY_NAME;
435 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
436 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
444 /* goto FINISH_OFF; */
448 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
451 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
454 emf_mailbox_t mailbox;
456 if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
457 if (select_num > 0) {
458 for (i = 0; i < select_num; i++) {
459 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
460 mailbox.account_id = local_mailbox_list[i].account_id;
461 mailbox.name = local_mailbox_list[i].mailbox_name;
462 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
463 EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.name);
464 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
465 local_mailbox_list = NULL;
469 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
470 local_mailbox_list = NULL;
474 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
475 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
479 if (!emcore_check_thread_status()) {
480 err = EMF_ERROR_CANCELLED;
484 for (i = 0; i < count; i++)
485 mailbox_list[i].account_id = account_id;
491 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
492 EM_SAFE_FREE(mbox_path);
495 emcore_close_mailbox(account_id, stream);
498 emcore_free_mailbox(&mailbox_list, count, NULL);
500 if (err_code != NULL)
502 EM_DEBUG_FUNC_END("ret [%d]", ret);
506 int emcore_download_mailbox_list(void *mail_stream,
508 emf_mailbox_t **mailbox_list,
512 MAILSTREAM *stream = mail_stream;
513 emf_callback_holder_t holder;
514 char *pattern = NULL;
515 char *reference = NULL;
516 int err = EMF_ERROR_NONE;
519 EM_DEBUG_FUNC_BEGIN();
522 *err_code = EMF_ERROR_NONE;
525 if (!stream || !mailbox_list || !count) {
526 err = EMF_ERROR_INVALID_PARAM;
530 memset(&holder, 0x00, sizeof(holder));
532 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
535 reference = em_malloc(strlen(stream->original_mailbox) + strlen(mailbox) + 1);
537 strncpy(reference, stream->original_mailbox, (size_t)strlen(stream->original_mailbox));
538 if ((s = strchr(reference, '}')))
540 strcat(reference, mailbox);
544 reference = EM_SAFE_STRDUP(stream->original_mailbox);
547 stream->sparep = &holder;
549 /* imap command : tag LIST reference * */
550 mail_list(stream, reference, pattern);
552 stream->sparep = NULL;
554 EM_SAFE_FREE(reference);
557 *mailbox_list = (emf_mailbox_t *)holder.data;
562 if (err_code) *err_code = err;
568 * check whether this imap mailbox is synchronous mailbox
570 * mailbox : imap mailbox to be checked
571 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
576 int emcore_check_sync_imap_mailbox(emf_mailbox_t *mailbox, int *synchronous, int *err_code)
578 EM_DEBUG_FUNC_BEGIN();
580 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
583 *err_code = EMF_ERROR_NONE;
586 if (!mailbox || !synchronous) {
587 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
589 if (err_code != NULL)
590 *err_code = EMF_ERROR_INVALID_PARAM;
595 int err = EMF_ERROR_NONE;
596 emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
598 if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl, true, &err)) {
599 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
603 *synchronous = imap_mailbox_tbl ? 1 : 0;
608 if (imap_mailbox_tbl != NULL)
609 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
611 if (err_code != NULL)
619 * set sync imap mailbox
621 * mailbox_list : imap mailbox to be synced
622 * syncronous : 0-sync 1 : non-sync
628 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(emf_mailbox_t *mailbox, int synchronous, int *err_code)
630 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
633 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
634 if (err_code != NULL)
635 *err_code = EMF_ERROR_INVALID_PARAM;
640 int err = EMF_ERROR_NONE;
641 emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
642 emcore_uid_list *uid_list = NULL;
643 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
644 MAILSTREAM *stream = mailbox->mail_stream;
645 int mailbox_renamed = 0;
649 IMAPLOCAL *imap_local = NULL;
650 char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
653 /* if synchcronous, insert imap mailbox to db */
654 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl_item, true, &err)) {
655 /* mailbox already exists */
656 /* mailbox Found, Do set the modifiable_yn = 1 */
657 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
658 if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->name, 1, true, &err)) {
659 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
664 if (err != EMF_ERROR_MAILBOX_NOT_FOUND) {
665 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
666 /* This is error scenario so finish the job */
670 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
671 /* Now check if mailbox is renamed */
672 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
674 imap_local = ((MAILSTREAM *)stream)->local;
676 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
678 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->name);
683 /* select the mailbox and get its UID */
684 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)strlen(cmd))) {
685 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->name);
687 err = EMF_ERROR_CONNECTION_BROKEN;
689 imap_local->netstream = NULL;
690 mailbox->mail_stream = NULL;
695 EM_DEBUG_LOG("Get response for select call");
696 while (imap_local->netstream) {
697 p = net_getline(imap_local->netstream);
698 EM_DEBUG_LOG("p =[%s]", p);
699 if (!strncmp(p, "+", 1)) {
703 else if (!strncmp(p, "*", 1)) {
714 /* check if OK or BAD response comes. */
715 /* if response is OK the try getting UID list. */
716 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
717 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->name);
718 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
719 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
720 EM_SAFE_FREE(uid_list);
723 if (!emstorage_get_downloaded_list(mailbox->account_id, NULL, &downloaded_uids, &j, true, &err)) {
724 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
726 downloaded_uids = NULL;
728 else /* Prevent Defect - 28497 */ {
729 emcore_uid_list *uid_elem = uid_list;
730 emcore_uid_list *next_uid_elem = NULL;
732 for (i = j; (i > 0 && !mailbox_renamed); i--) {
734 next_uid_elem = uid_elem->next;
735 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
740 EM_SAFE_FREE(uid_elem->uid);
741 uid_elem = next_uid_elem;
747 } /* mailbox selected */
750 if (mailbox_renamed) /* renamed mailbox */ {
751 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
752 /* Do a mailbox rename in the DB */
753 if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->name, true, &err))
754 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
758 emstorage_mailbox_tbl_t mailbox_tbl;
759 mailbox_tbl.account_id = mailbox->account_id;
760 mailbox_tbl.local_yn = 0;
761 mailbox_tbl.mailbox_name = mailbox->name;
762 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
764 /* Get the Alias Name after parsing the Full mailbox Path */
765 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->name);
768 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
770 mailbox_tbl.alias = mailbox->alias;
771 mailbox_tbl.sync_with_server_yn = 0;
772 mailbox_tbl.modifiable_yn = 1;
773 mailbox_tbl.total_mail_count_on_server = 0;
775 /* if non synchronous, delete imap mailbox from db */
776 if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_name, &mailbox_tbl, true, &err)) {
777 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
782 else /* Its a Fresh Mailbox */ {
783 emstorage_mailbox_tbl_t mailbox_tbl;
784 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
785 mailbox_tbl.account_id = mailbox->account_id;
786 mailbox_tbl.local_yn = 0;
787 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
788 mailbox_tbl.mailbox_name = mailbox->name;
789 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
791 /* Get the Alias Name after Parsing the Full mailbox Path */
792 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->name);
794 if (mailbox->alias) {
795 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
797 mailbox_tbl.alias = mailbox->alias;
798 mailbox_tbl.sync_with_server_yn = 1;
799 mailbox_tbl.modifiable_yn = 1;
800 mailbox_tbl.total_mail_count_on_server = 0;
802 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
804 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
805 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
814 /* set sync db mailbox */
815 mailbox->synchronous = synchronous;
822 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
824 if (imap_mailbox_tbl_item)
825 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
834 * create a new imap mailbox
836 * new_mailbox : imap mailbox to be created
841 INTERNAL_FUNC int emcore_create_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
843 MAILSTREAM *stream = NULL;
844 char *long_enc_path = NULL;
845 void *tmp_stream = NULL;
847 int err = EMF_ERROR_NONE;
849 EM_DEBUG_FUNC_BEGIN();
852 *err_code = EMF_ERROR_NONE;
857 err = EMF_ERROR_INVALID_PARAM;
861 /* connect mail server */
863 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, NULL, (void **)&tmp_stream, NULL))
865 err = EMF_ERROR_CONNECTION_FAILURE;
869 stream = (MAILSTREAM *)tmp_stream;
871 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
872 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
874 err = EMF_ERROR_UNKNOWN;
879 if (!mail_create(stream, long_enc_path))
881 err = EMF_ERROR_UNKNOWN;
885 emcore_close_mailbox(0, stream);
888 EM_SAFE_FREE(long_enc_path);
895 emcore_close_mailbox(0, stream);
899 EM_SAFE_FREE(long_enc_path);
909 * delete a imap mailbox
911 * mailbox : mailbox to be deleted
916 INTERNAL_FUNC int emcore_delete_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
918 MAILSTREAM *stream = NULL;
919 char *long_enc_path = NULL;
920 emf_account_t *ref_account = NULL;
921 void *tmp_stream = NULL;
923 int err = EMF_ERROR_NONE;
925 EM_DEBUG_FUNC_BEGIN();
928 *err_code = EMF_ERROR_NONE;
933 err = EMF_ERROR_INVALID_PARAM;
937 ref_account = emcore_get_account_reference(mailbox->account_id);
940 err = EMF_ERROR_INVALID_PARAM;
944 /* if not imap4 mail, return */
945 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
946 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
948 err = EMF_ERROR_INVALID_PARAM;
952 /* connect mail server */
954 if (!emcore_connect_to_remote_mailbox(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
956 err = EMF_ERROR_CONNECTION_FAILURE;
960 stream = (MAILSTREAM *)tmp_stream;
962 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
963 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
965 err = EMF_ERROR_UNKNOWN;
970 if (!mail_delete(stream, long_enc_path))
972 err = EMF_ERROR_UNKNOWN;
976 emcore_close_mailbox(0, stream);
979 EM_SAFE_FREE(long_enc_path);
981 /* if deleted imap mailbox is synchronous mailbox, delete db imap mailbox from db */
982 if (!emstorage_delete_mailbox(ref_account->account_id, 0, mailbox->name, true, &err)) {
983 EM_DEBUG_EXCEPTION("\t emstorage_delete_mailbox failed - %d", err);
992 emcore_close_mailbox(0, stream);
996 EM_SAFE_FREE(long_enc_path);
1006 * change imap mailbox name
1008 * old_mailbox : previous mailbox
1009 * new_mailbox : renamed mailbox
1014 INTERNAL_FUNC int emcore_modify_imap_mailbox(emf_mailbox_t *old_mailbox, emf_mailbox_t *new_mailbox, int *err_code)
1016 MAILSTREAM *stream = NULL;
1017 char *long_enc_path = NULL;
1018 char *long_enc_path_new = NULL;
1019 emstorage_mailbox_tbl_t imap_mailbox_tbl_item;
1020 emf_account_t *ref_account = NULL;
1021 void *tmp_stream = NULL;
1023 int err = EMF_ERROR_NONE;
1025 EM_DEBUG_FUNC_BEGIN();
1028 *err_code = EMF_ERROR_NONE;
1031 if (!old_mailbox || !new_mailbox)
1033 err = EMF_ERROR_INVALID_PARAM;
1037 ref_account = emcore_get_account_reference(old_mailbox->account_id);
1040 err = EMF_ERROR_INVALID_PARAM;
1044 if (!emcore_get_account_reference(new_mailbox->account_id))
1046 err = EMF_ERROR_INVALID_PARAM;
1050 /* if not imap4 mail, return */
1051 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
1052 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
1054 err = EMF_ERROR_INVALID_PARAM;
1058 /* connect mail server */
1060 if (!emcore_connect_to_remote_mailbox(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
1062 err = EMF_ERROR_CONNECTION_FAILURE;
1066 stream = (MAILSTREAM *)tmp_stream;
1068 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1069 if (!emcore_get_long_encoded_path(old_mailbox->account_id, old_mailbox->name, '/', &long_enc_path, err_code))
1071 err = EMF_ERROR_UNKNOWN;
1075 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1076 if (!emcore_get_long_encoded_path(new_mailbox->account_id, new_mailbox->name, '/', &long_enc_path_new, err_code))
1078 err = EMF_ERROR_UNKNOWN;
1082 /* rename mailbox */
1083 if (!mail_rename(stream, long_enc_path, long_enc_path_new))
1085 err = EMF_ERROR_UNKNOWN;
1089 EM_SAFE_FREE(long_enc_path);
1090 EM_SAFE_FREE(long_enc_path_new);
1092 emcore_close_mailbox(0, stream);
1095 imap_mailbox_tbl_item.alias = NULL;
1096 imap_mailbox_tbl_item.mailbox_name = new_mailbox->name;
1097 imap_mailbox_tbl_item.modifiable_yn = 1;
1099 /* if modified imap mailbox is synchronous mailbox, update mailbox name from imap mailbox table */
1100 if (!emstorage_update_mailbox(ref_account->account_id, 0, old_mailbox->name, &imap_mailbox_tbl_item, true, &err))
1102 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
1109 EM_SAFE_FREE(long_enc_path);
1110 EM_SAFE_FREE(long_enc_path_new);
1114 emcore_close_mailbox(0, stream);