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 = EMAIL_ERROR_NONE;
58 int ret = false, ret2;
60 if (output_count == NULL) {
61 err = EMAIL_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 = 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(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(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("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(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);
137 EM_DEBUG_FUNC_END("ret [%d]", ret);
142 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
144 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
146 int ret = false, err = EMAIL_ERROR_NONE;
148 int account_count = 100;
149 int mailbox_count = 0;
150 email_account_t *account_ref = NULL;
151 emstorage_account_tbl_t *account_tbl_list = NULL;
152 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
154 if (account_id > ALL_ACCOUNT) {
155 account_ref = emcore_get_account_reference(account_id);
156 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
157 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
161 else if (!account_ref) {
162 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
165 if (mailbox_id == 0) {
166 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
167 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
173 if (mailbox_id == 0) {
174 if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
175 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
178 for ( i = 0; i < account_count; i++) {
179 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) {
180 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
188 if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
189 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
195 if (new_slot_size > 0) {
196 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
197 if(!mailbox_tbl_list) {
198 EM_DEBUG_EXCEPTION("em_malloc failed");
201 mailbox_tbl_list->account_id = account_id;
202 mailbox_tbl_list->mailbox_id = mailbox_id;
203 mailbox_tbl_list->mail_slot_size = new_slot_size;
205 else { /* read information from DB */
206 if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
207 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
214 if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
215 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
220 for (i = 0; i < mailbox_count; i++) {
221 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
222 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
223 err = EMAIL_ERROR_NONE;
225 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
233 if (account_tbl_list)
234 emstorage_free_account(&account_tbl_list, account_count, NULL);
236 if (mailbox_tbl_list)
237 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
245 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
247 email_account_t *ref_account = NULL;
251 ref_account = emcore_get_account_reference(account_id);
253 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
257 path_len = strlen(ref_account->incoming_server_address) +
258 (mailbox_name ? strlen(mailbox_name) : 0) + 50;
260 *path = em_malloc(path_len);/* strlen(ref_account->incoming_server_address) + */
261 /* (mailbox_name ? strlen(mailbox_name) : 0) + 20); */
264 memset(*path, 0x00, path_len);
265 /* 1. server address / server type */
267 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
268 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
271 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
274 /* 2. set tls option if security connection */
275 /* if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(strlen(*path)-1)); */
276 if (ref_account->incoming_server_secure_connection & 0x01) {
277 strncat(*path + 1, "/ssl", path_len-(strlen(*path)-1));
279 if (ref_account->incoming_server_secure_connection & 0x02)
280 strncat(*path + 1, "/tls", path_len-(strlen(*path)-1));
282 strncat(*path + 1, "/notls", path_len-(strlen(*path)-1));
284 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
285 strncat(*path + 1, "}", path_len-strlen(*path)-1);
288 if (mailbox_name) strncat(*path, mailbox_name, path_len-strlen(*path)-1);
293 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int *err_code)
295 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
298 int err = EMAIL_ERROR_NONE;
299 int status = EMAIL_DOWNLOAD_FAIL;
301 MAILSTREAM *stream = NULL;
302 email_internal_mailbox_t *mailbox_list = NULL;
303 email_account_t *ref_account = NULL;
304 void *tmp_stream = NULL;
305 char *mbox_path = NULL;
306 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
307 char *mailbox_name_for_mailbox_type = NULL;
311 *err_code = EMAIL_ERROR_NONE;
313 if (!emcore_check_thread_status()) {
314 err = EMAIL_ERROR_CANCELLED;
318 ref_account = emcore_get_account_reference(account_id);
320 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
321 err = EMAIL_ERROR_INVALID_ACCOUNT;
325 /* if not imap4 mail, return */
326 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
327 EM_DEBUG_EXCEPTION("unsupported account...");
328 err = EMAIL_ERROR_INVALID_ACCOUNT;
332 /* get mail server path */
333 /* mbox_path is not used. the below func might be unnecessary */
334 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
335 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
340 if (!emcore_check_thread_status()) {
341 err = EMAIL_ERROR_CANCELLED;
346 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
347 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
349 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
350 err = EMAIL_ERROR_CANCELLED;
352 err = EMAIL_ERROR_CONNECTION_FAILURE;
354 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
358 EM_SAFE_FREE(mbox_path);
360 stream = (MAILSTREAM *)tmp_stream;
362 if (!emcore_check_thread_status()) {
363 err = EMAIL_ERROR_CANCELLED;
367 /* download mailbox list */
368 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
369 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
373 if (!emcore_check_thread_status()) {
374 err = EMAIL_ERROR_CANCELLED;
378 for (i = 0; i < count; i++) {
379 if (!emcore_check_thread_status()) {
380 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
381 err = EMAIL_ERROR_CANCELLED;
384 if (mailbox_list[i].mailbox_name) {
385 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].mailbox_name);
386 emcore_get_default_mail_slot_count(&(mailbox_list[i].mail_slot_size), NULL);
388 if(mailbox_list[i].mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
389 emcore_bind_mailbox_type(mailbox_list + i);
391 if (mailbox_list[i].mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
392 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
393 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].mailbox_name);
394 mailbox_list[i].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
397 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
400 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
401 if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
402 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
410 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
411 /* if (!emstorage_get_mailbox_name_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
412 if (mailbox_type_list[counter] == -1) {
413 /* EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed - %d", err); */
414 /* if (EMAIL_ERROR_MAILBOX_NOT_FOUND == err) */
416 emstorage_mailbox_tbl_t mailbox_tbl;
418 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
420 mailbox_tbl.account_id = account_id;
421 mailbox_tbl.mailbox_id = 0;
422 mailbox_tbl.local_yn = 1;
423 mailbox_tbl.mailbox_type = counter;
424 mailbox_tbl.sync_with_server_yn = 0;
425 mailbox_tbl.modifiable_yn = 1;
426 mailbox_tbl.total_mail_count_on_server = 0;
427 emcore_get_default_mail_slot_count(&mailbox_tbl.mail_slot_size, NULL);
430 case EMAIL_MAILBOX_TYPE_SENTBOX:
431 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
432 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
435 case EMAIL_MAILBOX_TYPE_TRASH:
436 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
437 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
440 case EMAIL_MAILBOX_TYPE_DRAFT:
441 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
442 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
445 case EMAIL_MAILBOX_TYPE_SPAMBOX:
446 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
447 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
450 case EMAIL_MAILBOX_TYPE_OUTBOX:
451 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
452 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
456 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
457 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
461 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
462 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
470 /* goto FINISH_OFF; */
474 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
477 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
480 email_mailbox_t mailbox;
482 if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
483 if (select_num > 0) {
484 for (i = 0; i < select_num; i++) {
485 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
486 mailbox.account_id = local_mailbox_list[i].account_id;
487 mailbox.mailbox_name = local_mailbox_list[i].mailbox_name;
488 mailbox.mailbox_id = local_mailbox_list[i].mailbox_id;
489 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
490 EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.mailbox_name);
491 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
492 local_mailbox_list = NULL;
496 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
497 local_mailbox_list = NULL;
501 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
502 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
506 if (!emcore_check_thread_status()) {
507 err = EMAIL_ERROR_CANCELLED;
511 for (i = 0; i < count; i++)
512 mailbox_list[i].account_id = account_id;
518 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
519 EM_SAFE_FREE(mbox_path);
522 emcore_close_mailbox(account_id, stream);
525 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
527 if (err_code != NULL)
529 EM_DEBUG_FUNC_END("ret [%d]", ret);
533 int emcore_download_mailbox_list(void *mail_stream,
535 email_internal_mailbox_t **mailbox_list,
539 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);
541 MAILSTREAM *stream = mail_stream;
542 email_callback_holder_t holder;
543 char *pattern = NULL;
544 char *reference = NULL;
545 int err = EMAIL_ERROR_NONE;
548 if (!stream || !mailbox_list || !count) {
549 err = EMAIL_ERROR_INVALID_PARAM;
553 memset(&holder, 0x00, sizeof(holder));
555 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
558 reference = em_malloc(strlen(stream->original_mailbox) + strlen(mailbox_name) + 1);
560 strncpy(reference, stream->original_mailbox, (size_t)strlen(stream->original_mailbox));
561 if ((s = strchr(reference, '}')))
563 strcat(reference, mailbox_name);
567 reference = EM_SAFE_STRDUP(stream->original_mailbox);
570 stream->sparep = &holder;
572 /* imap command : tag LIST reference * */
573 /* see callback function mm_list */
574 mail_list(stream, reference, pattern);
576 stream->sparep = NULL;
578 EM_SAFE_FREE(reference);
581 *mailbox_list = (email_internal_mailbox_t*)holder.data;
593 * check whether this imap mailbox is synchronous mailbox
595 * mailbox : imap mailbox to be checked
596 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
601 int emcore_check_sync_imap_mailbox(email_mailbox_t *mailbox, int *synchronous, int *err_code)
603 EM_DEBUG_FUNC_BEGIN();
605 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
608 *err_code = EMAIL_ERROR_NONE;
611 if (!mailbox || !synchronous) {
612 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
614 if (err_code != NULL)
615 *err_code = EMAIL_ERROR_INVALID_PARAM;
620 int err = EMAIL_ERROR_NONE;
621 emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
623 if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl, true, &err)) {
624 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
628 *synchronous = imap_mailbox_tbl ? 1 : 0;
633 if (imap_mailbox_tbl != NULL)
634 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
636 if (err_code != NULL)
644 * set sync imap mailbox
646 * mailbox_list : imap mailbox to be synced
647 * syncronous : 0-sync 1 : non-sync
653 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(email_internal_mailbox_t *mailbox, int synchronous, int *err_code)
655 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
658 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
659 if (err_code != NULL)
660 *err_code = EMAIL_ERROR_INVALID_PARAM;
665 int err = EMAIL_ERROR_NONE;
666 emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
667 emcore_uid_list *uid_list = NULL;
668 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
669 MAILSTREAM *stream = mailbox->mail_stream;
670 int mailbox_renamed = 0;
674 IMAPLOCAL *imap_local = NULL;
675 char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
678 /* if synchcronous, insert imap mailbox to db */
679 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl_item, true, &err)) {
680 /* mailbox already exists */
681 /* mailbox Found, Do set the modifiable_yn = 1 */
682 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
683 if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->mailbox_name, 1, true, &err)) {
684 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
689 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
690 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
691 /* This is error scenario so finish the job */
695 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
696 /* Now check if mailbox is renamed */
697 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
699 imap_local = ((MAILSTREAM *)stream)->local;
701 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
703 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->mailbox_name);
708 /* select the mailbox and get its UID */
709 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)strlen(cmd))) {
710 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->mailbox_name);
712 err = EMAIL_ERROR_CONNECTION_BROKEN;
714 imap_local->netstream = NULL;
715 mailbox->mail_stream = NULL;
720 EM_DEBUG_LOG("Get response for select call");
721 while (imap_local->netstream) {
722 p = net_getline(imap_local->netstream);
723 EM_DEBUG_LOG("p =[%s]", p);
724 if (!strncmp(p, "+", 1)) {
728 else if (!strncmp(p, "*", 1)) {
739 /* check if OK or BAD response comes. */
740 /* if response is OK the try getting UID list. */
741 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
742 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->mailbox_name);
743 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
744 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
745 EM_SAFE_FREE(uid_list);
748 if (!emstorage_get_downloaded_list(mailbox->account_id, 0, &downloaded_uids, &j, true, &err)) {
749 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
751 downloaded_uids = NULL;
753 else /* Prevent Defect - 28497 */ {
754 emcore_uid_list *uid_elem = uid_list;
755 emcore_uid_list *next_uid_elem = NULL;
757 for (i = j; (i > 0 && !mailbox_renamed); i--) {
759 next_uid_elem = uid_elem->next;
760 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
765 EM_SAFE_FREE(uid_elem->uid);
766 uid_elem = next_uid_elem;
772 } /* mailbox selected */
775 if (mailbox_renamed) /* renamed mailbox */ {
776 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
777 /* Do a mailbox rename in the DB */
778 if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->mailbox_name, true, &err))
779 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
783 emstorage_mailbox_tbl_t mailbox_tbl;
784 mailbox_tbl.account_id = mailbox->account_id;
785 mailbox_tbl.local_yn = 0;
786 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
787 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
789 /* Get the Alias Name after parsing the Full mailbox Path */
790 if(mailbox->alias == NULL)
791 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
793 mailbox_tbl.alias = mailbox->alias;
794 mailbox_tbl.sync_with_server_yn = 1;
795 mailbox_tbl.modifiable_yn = 1;
796 mailbox_tbl.total_mail_count_on_server = 0;
798 /* if non synchronous, delete imap mailbox from db */
799 if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_id, &mailbox_tbl, true, &err)) {
800 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
805 else /* Its a Fresh Mailbox */ {
806 emstorage_mailbox_tbl_t mailbox_tbl;
807 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
808 mailbox_tbl.account_id = mailbox->account_id;
809 mailbox_tbl.local_yn = 0;
810 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
811 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
812 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
814 /* Get the Alias Name after Parsing the Full mailbox Path */
815 if(mailbox->alias == NULL)
816 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
818 if (mailbox->alias) {
819 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
821 mailbox_tbl.alias = mailbox->alias;
822 mailbox_tbl.sync_with_server_yn = 1;
823 mailbox_tbl.modifiable_yn = 1;
824 mailbox_tbl.total_mail_count_on_server = 0;
826 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
828 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
829 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
838 /* set sync db mailbox */
839 mailbox->synchronous = synchronous;
846 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
848 if (imap_mailbox_tbl_item)
849 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
858 * create a new imap mailbox
860 * new_mailbox : imap mailbox to be created
865 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
867 MAILSTREAM *stream = NULL;
868 char *long_enc_path = NULL;
869 void *tmp_stream = NULL;
871 int err = EMAIL_ERROR_NONE;
873 EM_DEBUG_FUNC_BEGIN();
876 *err_code = EMAIL_ERROR_NONE;
881 err = EMAIL_ERROR_INVALID_PARAM;
885 /* connect mail server */
887 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, NULL))
889 err = EMAIL_ERROR_CONNECTION_FAILURE;
893 stream = (MAILSTREAM *)tmp_stream;
895 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
896 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, err_code))
898 err = EMAIL_ERROR_UNKNOWN;
903 if (!mail_create(stream, long_enc_path))
905 err = EMAIL_ERROR_UNKNOWN;
909 emcore_close_mailbox(0, stream);
912 EM_SAFE_FREE(long_enc_path);
919 emcore_close_mailbox(0, stream);
923 EM_SAFE_FREE(long_enc_path);
933 * delete a imap mailbox
935 * input_mailbox_id : mailbox ID to be deleted
940 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
942 MAILSTREAM *stream = NULL;
943 char *long_enc_path = NULL;
944 email_account_t *ref_account = NULL;
945 void *tmp_stream = NULL;
947 int err = EMAIL_ERROR_NONE;
948 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
950 EM_DEBUG_FUNC_BEGIN();
953 *err_code = EMAIL_ERROR_NONE;
956 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
957 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
961 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
964 err = EMAIL_ERROR_INVALID_PARAM;
968 /* if not imap4 mail, return */
969 if (ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
970 err = EMAIL_ERROR_INVALID_PARAM;
974 /* connect mail server */
976 if (!emcore_connect_to_remote_mailbox(ref_account->account_id, 0, (void **)&tmp_stream, NULL))
978 err = EMAIL_ERROR_CONNECTION_FAILURE;
982 stream = (MAILSTREAM *)tmp_stream;
984 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
985 if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, err_code))
987 err = EMAIL_ERROR_UNKNOWN;
992 if (!mail_delete(stream, long_enc_path))
994 err = EMAIL_ERROR_UNKNOWN;
998 emcore_close_mailbox(0, stream);
1001 EM_SAFE_FREE(long_enc_path);
1003 /* if deleted imap mailbox is synchronous mailbox, delete db imap mailbox from db */
1004 if (!emstorage_delete_mailbox(ref_account->account_id, 0, input_mailbox_id, true, &err)) {
1005 EM_DEBUG_EXCEPTION("\t emstorage_delete_mailbox failed - %d", err);
1014 emcore_close_mailbox(0, stream);
1018 EM_SAFE_FREE(long_enc_path);
1021 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1030 INTERNAL_FUNC int emcore_move_mailbox_on_imap_server(int input_account_id, char *input_old_mailbox_path, char *input_new_mailbox_path)
1032 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);
1033 MAILSTREAM *stream = NULL;
1034 char *long_enc_path_old = NULL;
1035 char *long_enc_path_new = NULL;
1036 email_account_t *ref_account = NULL;
1037 void *tmp_stream = NULL;
1038 int err = EMAIL_ERROR_NONE;
1040 if (!input_old_mailbox_path || !input_new_mailbox_path) {
1041 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1042 err = EMAIL_ERROR_INVALID_PARAM;
1046 ref_account = emcore_get_account_reference(input_account_id);
1048 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1049 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
1050 err = EMAIL_ERROR_INVALID_ACCOUNT;
1054 /* connect mail server */
1056 if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
1057 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
1061 stream = (MAILSTREAM *)tmp_stream;
1063 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1064 if (!emcore_get_long_encoded_path(input_account_id, input_old_mailbox_path, '/', &long_enc_path_old, &err)) {
1065 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1069 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1070 if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
1071 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1075 /* rename mailbox */
1076 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1077 err = EMAIL_ERROR_UNKNOWN;
1082 EM_SAFE_FREE(long_enc_path_old);
1083 EM_SAFE_FREE(long_enc_path_new);
1086 emcore_close_mailbox(0, stream);
1090 EM_DEBUG_FUNC_END("err [%d]", err);