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 : em-core-imap_folder.c
26 * Desc : Mail IMAP mailbox
31 * 2006.08.01 : created
32 *****************************************************************************/
37 #include "em-core-global.h"
38 #include "em-core-utils.h"
40 #include "em-storage.h"
41 #include "em-core-event.h"
42 #include "em-core-mailbox.h"
43 #include "em-core-imap-mailbox.h"
44 #include "em-core-mailbox-sync.h"
45 #include "em-core-account.h"
48 #include "emf-dbglog.h"
50 EXPORT_API int em_core_mailbox_get_default_mail_slot_count(int *output_count, int *err_code)
52 EM_DEBUG_FUNC_BEGIN();
53 EM_DEBUG_LOG("output_count[%p], err_code[%p]", output_count, err_code);
55 int err = EMF_ERROR_NONE;
57 int ret = false, ret2;
59 if (output_count == NULL) {
60 err = EMF_ERROR_INVALID_PARAM;
64 ret2 = vconf_get_int(VCONF_KEY_DEFAULT_SLOT_SIZE, &mail_slot_count);
67 EM_DEBUG_EXCEPTION("vconf_get_int() Failed(%d)", ret2);
68 mail_slot_count = 100;
76 *output_count = mail_slot_count;
86 EXPORT_API int em_core_mailbox_remove_overflowed_mails(emf_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
88 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
91 int *mail_id_list = NULL, mail_id_list_count = 0;
92 int err = EMF_ERROR_NONE;
93 emf_account_t *account_ref = NULL;
95 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
96 if (intput_mailbox_tbl)
97 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
98 err = EMF_ERROR_INVALID_PARAM;
102 account_ref = em_core_get_account_reference(intput_mailbox_tbl->account_id);
104 if (account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
105 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
106 err = EMF_ERROR_NOT_SUPPORTED;
111 if (!em_storage_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)) {
112 if (err == EM_STORAGE_ERROR_MAIL_NOT_FOUND) {
113 EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
114 err = EMF_ERROR_NONE;
118 EM_DEBUG_EXCEPTION("em_storage_get_overflowed_mail_id_list failed [%d]", err);
123 if (!em_core_mail_delete(intput_mailbox_tbl->account_id, mail_id_list, mail_id_list_count, false, EMF_DELETED_BY_OVERFLOW, false, &err)) {
124 EM_DEBUG_EXCEPTION("em_core_mail_delete failed [%d]", err);
131 EM_SAFE_FREE(mail_id_list);
136 EM_DEBUG_FUNC_END("ret [%d]", ret);
141 EXPORT_API int em_core_mailbox_set_mail_slot_size(int account_id, char *mailbox_name, int new_slot_size, int *err_code)
143 EM_DEBUG_FUNC_BEGIN();
144 EM_DEBUG_LOG("account_id [%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
146 int ret = false, err = EMF_ERROR_NONE;
147 int i, mailbox_count = 0;
148 emf_account_t *account_ref = NULL;
149 emf_mailbox_tbl_t *mailbox_tbl_list = NULL;
151 if (account_id > ALL_ACCOUNT) {
152 account_ref = em_core_get_account_reference(account_id);
153 if (account_ref && account_ref->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC) {
154 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
158 else if (!account_ref) {
159 EM_DEBUG_EXCEPTION("em_core_get_account_reference failed");
164 if (!em_storage_set_mail_slot_size(account_id, mailbox_name, new_slot_size, true, &err)) {
165 EM_DEBUG_EXCEPTION("em_storage_set_mail_slot_size failed [%d]", err);
171 if (new_slot_size > 0) {
172 mailbox_tbl_list = em_core_malloc(sizeof(emf_mailbox_tbl_t) * mailbox_count);
173 if(!mailbox_tbl_list) {
174 EM_DEBUG_EXCEPTION("em_core_malloc failed");
177 mailbox_tbl_list->account_id = account_id;
178 mailbox_tbl_list->mailbox_name = EM_SAFE_STRDUP(mailbox_name);
179 mailbox_tbl_list->mail_slot_size = new_slot_size;
181 else { /* read information from DB */
182 if (!em_storage_get_mailbox_by_name(account_id, EMF_MAILBOX_ALL, mailbox_name, &mailbox_tbl_list, true, &err)) {
183 EM_DEBUG_EXCEPTION("em_storage_get_mailbox failed [%d]", err);
190 if (!em_storage_get_mailbox(account_id, EMF_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
191 EM_DEBUG_EXCEPTION("em_storage_get_mailbox failed [%d]", err);
196 for (i = 0; i < mailbox_count; i++) {
197 if (!em_core_mailbox_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
198 if (err == EM_STORAGE_ERROR_MAIL_NOT_FOUND || err == EMF_ERROR_NOT_SUPPORTED)
199 err = EMF_ERROR_NONE;
201 EM_DEBUG_EXCEPTION("em_core_mailbox_remove_overflowed_mails failed [%d]", err);
210 if (mailbox_tbl_list)
211 em_storage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
220 static int em_core_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
222 emf_account_t *ref_account = NULL;
226 ref_account = em_core_get_account_reference(account_id);
228 EM_DEBUG_EXCEPTION("em_core_get_account_reference failed");
232 path_len = strlen(ref_account->receiving_server_addr) +
233 (mailbox_name ? strlen(mailbox_name) : 0) + 50;
235 *path = em_core_malloc(path_len);/* strlen(ref_account->receiving_server_addr) + */
236 /* (mailbox_name ? strlen(mailbox_name) : 0) + 20); */
239 memset(*path, 0x00, path_len);
240 /* 1. server address / server type */
242 if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
243 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->receiving_server_addr, ref_account->port_num);
246 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->receiving_server_addr, ref_account->port_num);
249 /* 2. set tls option if security connection */
250 /* if (ref_account->use_security) strncat(*path + 1, "/tls", path_len-(strlen(*path)-1)); */
251 if (ref_account->use_security & 0x01) {
252 strncat(*path + 1, "/ssl", path_len-(strlen(*path)-1));
254 if (ref_account->use_security & 0x02)
255 strncat(*path + 1, "/tls", path_len-(strlen(*path)-1));
257 strncat(*path + 1, "/notls", path_len-(strlen(*path)-1));
259 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
260 strncat(*path + 1, "}", path_len-strlen(*path)-1);
263 if (mailbox_name) strncat(*path, mailbox_name, path_len-strlen(*path)-1);
268 EXPORT_API int em_core_mailbox_sync_mailbox_list(int account_id, char *mailbox_name, int *err_code)
270 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], err_code[%p]", account_id, mailbox_name, err_code);
273 int err = EMF_ERROR_NONE;
274 int status = EMF_DOWNLOAD_FAIL;
276 MAILSTREAM *stream = NULL;
277 emf_mailbox_t *mailbox_list = NULL;
278 emf_account_t *ref_account = NULL;
279 void *tmp_stream = NULL;
280 char *mbox_path = NULL;
281 int i = 0, count = 0, counter = 0, mailbox_type_list[EMF_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
282 char *mailbox_name_for_mailbox_type = NULL;
286 *err_code = EMF_ERROR_NONE;
288 if (!em_core_check_thread_status()) {
289 err = EMF_ERROR_CANCELLED;
293 ref_account = em_core_get_account_reference(account_id);
295 EM_DEBUG_EXCEPTION("em_core_get_account_reference failed - %d", account_id);
296 err = EMF_ERROR_INVALID_ACCOUNT;
300 /* if not imap4 mail, return */
301 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE || ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
302 EM_DEBUG_EXCEPTION("unsupported account...");
303 err = EMF_ERROR_INVALID_ACCOUNT;
307 /* get mail server path */
308 /* mbox_path is not used. the below func might be unnecessary */
309 if (!em_core_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
310 EM_DEBUG_EXCEPTION("em_core_get_mailbox_connection_path - %d", err);
315 if (!em_core_check_thread_status()) {
316 err = EMF_ERROR_CANCELLED;
321 if (!em_core_mailbox_open(account_id, NULL, (void **)&tmp_stream, &err) || !tmp_stream) {
322 EM_DEBUG_EXCEPTION("em_core_mailbox_open failed - %d", err);
324 if (err == EMF_ERROR_CONNECTION_BROKEN)
325 err = EMF_ERROR_CANCELLED;
327 err = EMF_ERROR_CONNECTION_FAILURE;
329 status = EMF_DOWNLOAD_CONNECTION_FAIL;
333 EM_SAFE_FREE(mbox_path);
335 stream = (MAILSTREAM *)tmp_stream;
337 if (!em_core_check_thread_status()) {
338 err = EMF_ERROR_CANCELLED;
342 /* download mailbox list */
343 if (!em_core_mailbox_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
344 EM_DEBUG_EXCEPTION("em_core_mailbox_download_mailbox_list failed - %d", err);
348 if (!em_core_check_thread_status()) {
349 err = EMF_ERROR_CANCELLED;
353 for (i = 0; i < count; i++) {
354 if (!em_core_check_thread_status()) {
355 EM_DEBUG_LOG("em_core_check_thread_status - cancelled");
356 err = EMF_ERROR_CANCELLED;
359 if (mailbox_list[i].name) {
360 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].name);
361 em_core_mailbox_get_default_mail_slot_count(&(mailbox_list[i].mail_slot_size), NULL);
362 em_core_bind_mailbox_type(mailbox_list + i);
364 if (mailbox_list[i].mailbox_type <= EMF_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
365 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
366 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].name);
367 mailbox_list[i].mailbox_type = EMF_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
370 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
373 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
374 if(!em_core_mailbox_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
375 EM_DEBUG_EXCEPTION("em_core_mailbox_set_sync_imap_mailbox failed [%d]", err);
383 for (counter = EMF_MAILBOX_TYPE_INBOX; counter <= EMF_MAILBOX_TYPE_OUTBOX; counter++) {
384 /* if (!em_storage_get_mailboxname_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
385 if (mailbox_type_list[counter] == -1) {
386 /* EM_DEBUG_EXCEPTION("em_storage_get_mailboxname_by_mailbox_type failed - %d", err); */
387 /* if (EM_STORAGE_ERROR_MAILBOX_NOT_FOUND == err) */
389 emf_mailbox_tbl_t mailbox_tbl;
391 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
393 mailbox_tbl.account_id = account_id;
394 mailbox_tbl.mailbox_id = 0;
395 mailbox_tbl.local_yn = 1;
396 mailbox_tbl.mailbox_type = counter;
397 mailbox_tbl.sync_with_server_yn = 1;
398 mailbox_tbl.modifiable_yn = 1;
399 mailbox_tbl.total_mail_count_on_server = 0;
400 em_core_mailbox_get_default_mail_slot_count(&mailbox_tbl.mail_slot_size, NULL);
403 case EMF_MAILBOX_TYPE_SENTBOX:
404 mailbox_tbl.mailbox_name = EMF_SENTBOX_NAME;
405 mailbox_tbl.alias = EMF_SENTBOX_DISPLAY_NAME;
408 case EMF_MAILBOX_TYPE_TRASH:
409 mailbox_tbl.mailbox_name = EMF_TRASH_NAME;
410 mailbox_tbl.alias = EMF_TRASH_DISPLAY_NAME;
413 case EMF_MAILBOX_TYPE_DRAFT:
414 mailbox_tbl.mailbox_name = EMF_DRAFTBOX_NAME;
415 mailbox_tbl.alias = EMF_DRAFTBOX_DISPLAY_NAME;
418 case EMF_MAILBOX_TYPE_SPAMBOX:
419 mailbox_tbl.mailbox_name = EMF_SPAMBOX_NAME;
420 mailbox_tbl.alias = EMF_SPAMBOX_DISPLAY_NAME;
423 case EMF_MAILBOX_TYPE_OUTBOX:
424 mailbox_tbl.mailbox_name = EMF_OUTBOX_NAME;
425 mailbox_tbl.alias = EMF_OUTBOX_DISPLAY_NAME;
429 mailbox_tbl.mailbox_name = EMF_INBOX_NAME;
430 mailbox_tbl.alias = EMF_INBOX_DISPLAY_NAME;
434 if (!em_storage_add_mailbox(&mailbox_tbl, true, &err)) {
435 EM_DEBUG_EXCEPTION("em_storage_add_mailbox failed - %d", err);
437 if (err == EM_STORAGE_ERROR_DB_IS_FULL)
438 err = EMF_ERROR_MAIL_MEMORY_FULL;
439 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
440 err = EMF_ERROR_INVALID_PARAM;
441 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
442 err = EMF_ERROR_OUT_OF_MEMORY;
443 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
444 err = EMF_ERROR_DB_FAILURE;
446 err = EMF_ERROR_UNKNOWN;
453 /* err = em_storage_get_emf_error_from_em_storage_error(err); */
454 /* goto FINISH_OFF; */
458 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
461 emf_mailbox_tbl_t *local_mailbox_list = NULL;
464 emf_mailbox_t mailbox;
466 if (em_storage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
467 if (select_num > 0) {
468 for (i = 0; i < select_num; i++) {
469 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
470 mailbox.account_id = local_mailbox_list[i].account_id;
471 mailbox.name = local_mailbox_list[i].mailbox_name;
472 if (!em_core_mailbox_delete_all(&mailbox, &err)) {
473 EM_DEBUG_EXCEPTION(" em_core_mailbox_delete_all of Mailbox [%s] Failed ", mailbox.name);
474 em_storage_free_mailbox(&local_mailbox_list, select_num, NULL);
475 local_mailbox_list = NULL;
479 em_storage_free_mailbox(&local_mailbox_list, select_num, NULL);
480 local_mailbox_list = NULL;
484 if (!em_storage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
485 EM_DEBUG_EXCEPTION(" >>>> em_storage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
489 if (!em_core_check_thread_status()) {
490 err = EMF_ERROR_CANCELLED;
494 for (i = 0; i < count; i++)
495 mailbox_list[i].account_id = account_id;
501 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
502 EM_SAFE_FREE(mbox_path);
505 em_core_mailbox_close(account_id, stream);
508 em_core_mailbox_free(&mailbox_list, count, NULL);
510 if (err_code != NULL)
512 EM_DEBUG_FUNC_END("ret [%d]", ret);
516 int em_core_mailbox_download_mailbox_list(void *mail_stream,
518 emf_mailbox_t **mailbox_list,
522 MAILSTREAM *stream = mail_stream;
523 emf_callback_holder_t holder;
524 char *pattern = NULL;
525 char *reference = NULL;
526 int err = EMF_ERROR_NONE;
529 EM_DEBUG_FUNC_BEGIN();
532 *err_code = EMF_ERROR_NONE;
535 if (!stream || !mailbox_list || !count) {
536 err = EMF_ERROR_INVALID_PARAM;
540 memset(&holder, 0x00, sizeof(holder));
542 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
545 reference = em_core_malloc(strlen(stream->original_mailbox) + strlen(mailbox) + 1);
547 strncpy(reference, stream->original_mailbox, (size_t)strlen(stream->original_mailbox));
548 if ((s = strchr(reference, '}')))
550 strcat(reference, mailbox);
554 reference = EM_SAFE_STRDUP(stream->original_mailbox);
557 stream->sparep = &holder;
559 /* imap command : tag LIST reference * */
560 mail_list(stream, reference, pattern);
562 stream->sparep = NULL;
564 EM_SAFE_FREE(reference);
567 *mailbox_list = (emf_mailbox_t *)holder.data;
572 if (err_code) *err_code = err;
578 * check whether this imap mailbox is synchronous mailbox
580 * mailbox : imap mailbox to be checked
581 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
586 int em_core_mailbox_check_sync_imap_mailbox(emf_mailbox_t *mailbox, int *synchronous, int *err_code)
588 EM_DEBUG_FUNC_BEGIN();
590 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
593 *err_code = EMF_ERROR_NONE;
596 if (!mailbox || !synchronous) {
597 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
599 if (err_code != NULL)
600 *err_code = EMF_ERROR_INVALID_PARAM;
605 int err = EMF_ERROR_NONE;
606 emf_mailbox_tbl_t *imap_mailbox_tbl = NULL;
608 if (!em_storage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl, true, &err)) {
609 EM_DEBUG_EXCEPTION("em_storage_get_mailbox_by_name failed - %d", err);
611 if (err == EM_STORAGE_ERROR_MAILBOX_NOT_FOUND)
612 err = EMF_ERROR_MAILBOX_NOT_FOUND;
613 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
614 err = EMF_ERROR_INVALID_PARAM;
615 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
616 err = EMF_ERROR_OUT_OF_MEMORY;
617 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
618 err = EMF_ERROR_DB_FAILURE;
620 err = EMF_ERROR_UNKNOWN;
624 *synchronous = imap_mailbox_tbl ? 1 : 0;
629 if (imap_mailbox_tbl != NULL)
630 em_storage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
632 if (err_code != NULL)
640 * set sync imap mailbox
642 * mailbox_list : imap mailbox to be synced
643 * syncronous : 0-sync 1 : non-sync
649 int em_core_mailbox_set_sync_imap_mailbox(emf_mailbox_t *mailbox, int synchronous, int *err_code)
651 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
654 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
655 if (err_code != NULL)
656 *err_code = EMF_ERROR_INVALID_PARAM;
661 int err = EMF_ERROR_NONE;
662 emf_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
663 em_core_uid_list *uid_list = NULL;
664 emf_mail_read_mail_uid_tbl_t *downloaded_uids = NULL;
665 MAILSTREAM *stream = mailbox->mail_stream;
666 int mailbox_renamed = 0;
670 IMAPLOCAL *imap_local = NULL;
671 char cmd[128], tag[32], *p;
674 /* if synchcronous, insert imap mailbox to db */
675 if (em_storage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->name, &imap_mailbox_tbl_item, true, &err)) {
676 /* mailbox already exists */
677 /* mailbox Found, Do set the modifiable_yn = 1 */
678 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
679 if (!em_storage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->name, 1, true, &err)) {
680 EM_DEBUG_EXCEPTION(" em_storage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
681 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
682 err = EMF_ERROR_INVALID_PARAM;
683 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
684 err = EMF_ERROR_OUT_OF_MEMORY;
685 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
686 err = EMF_ERROR_DB_FAILURE;
688 err = EMF_ERROR_UNKNOWN;
693 if (err != EM_STORAGE_ERROR_MAILBOX_NOT_FOUND) {
694 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
695 /* This is error scenario so finish the job */
696 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
697 err = EMF_ERROR_INVALID_PARAM;
698 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
699 err = EMF_ERROR_OUT_OF_MEMORY;
700 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
701 err = EMF_ERROR_DB_FAILURE;
703 err = EMF_ERROR_UNKNOWN;
707 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
708 /* Now check if mailbox is renamed */
709 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
711 imap_local = ((MAILSTREAM *)stream)->local;
713 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
715 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->name);
720 /* select the mailbox and get its UID */
721 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)strlen(cmd))) {
722 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->name);
724 err = EMF_ERROR_CONNECTION_BROKEN;
726 imap_local->netstream = NULL;
727 mailbox->mail_stream = NULL;
732 EM_DEBUG_LOG("Get response for select call");
733 while (imap_local->netstream) {
734 p = net_getline(imap_local->netstream);
735 EM_DEBUG_LOG("p =[%s]", p);
736 if (!strncmp(p, "+", 1)) {
740 else if (!strncmp(p, "*", 1)) {
751 /* check if OK or BAD response comes. */
752 /* if response is OK the try getting UID list. */
753 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
754 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->name);
755 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
756 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
757 EM_SAFE_FREE(uid_list);
760 if (!em_storage_get_downloaded_list(mailbox->account_id, NULL, &downloaded_uids, &j, true, &err)) {
761 EM_DEBUG_EXCEPTION("em_storage_get_downloaded_list failed [%d]", err);
762 err = em_storage_get_emf_error_from_em_storage_error(err);
763 downloaded_uids = NULL;
765 else /* Prevent Defect - 28497 */ {
766 em_core_uid_list *uid_elem = uid_list;
767 em_core_uid_list *next_uid_elem = NULL;
769 for (i = j; (i > 0 && !mailbox_renamed); i--) {
771 next_uid_elem = uid_elem->next;
772 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
777 EM_SAFE_FREE(uid_elem->uid);
778 uid_elem = next_uid_elem;
784 } /* mailbox selected */
787 if (mailbox_renamed) /* renamed mailbox */ {
788 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
789 /* Do a mailbox rename in the DB */
790 if (!em_storage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->name, true, &err))
791 EM_DEBUG_EXCEPTION(" em_storage_modify_mailbox_of_mails Failed [%d]", err);
795 emf_mailbox_tbl_t mailbox_tbl;
796 mailbox_tbl.account_id = mailbox->account_id;
797 mailbox_tbl.local_yn = 0;
798 mailbox_tbl.mailbox_name = mailbox->name;
799 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
801 /* Get the Alias Name after parsing the Full mailbox Path */
802 mailbox->alias = em_core_get_alias_of_mailbox((const char *)mailbox->name);
805 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
807 mailbox_tbl.alias = mailbox->alias;
808 mailbox_tbl.sync_with_server_yn = 0;
809 mailbox_tbl.modifiable_yn = 1;
810 mailbox_tbl.total_mail_count_on_server = 0;
812 /* if non synchronous, delete imap mailbox from db */
813 if (!em_storage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_name, &mailbox_tbl, true, &err)) {
814 EM_DEBUG_EXCEPTION(" em_storage_update_mailbox Failed [ %d ] ", err);
815 if (err == EM_STORAGE_ERROR_INVALID_PARAM)
816 err = EMF_ERROR_INVALID_PARAM;
817 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
818 err = EMF_ERROR_OUT_OF_MEMORY;
819 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
820 err = EMF_ERROR_DB_FAILURE;
822 err = EMF_ERROR_UNKNOWN;
827 else /* Its a Fresh Mailbox */ {
828 emf_mailbox_tbl_t mailbox_tbl;
829 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
830 mailbox_tbl.account_id = mailbox->account_id;
831 mailbox_tbl.local_yn = 0;
832 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
833 mailbox_tbl.mailbox_name = mailbox->name;
834 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
836 /* Get the Alias Name after Parsing the Full mailbox Path */
837 mailbox->alias = em_core_get_alias_of_mailbox((const char *)mailbox->name);
839 if (mailbox->alias) {
840 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
842 mailbox_tbl.alias = mailbox->alias;
843 mailbox_tbl.sync_with_server_yn = 1;
844 mailbox_tbl.modifiable_yn = 1;
845 mailbox_tbl.total_mail_count_on_server = 0;
847 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
849 if (!em_storage_add_mailbox(&mailbox_tbl, true, &err)) {
850 EM_DEBUG_EXCEPTION("em_storage_add_mailbox failed - %d", err);
852 if (err == EM_STORAGE_ERROR_DB_IS_FULL)
853 err = EMF_ERROR_MAIL_MEMORY_FULL;
854 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
855 err = EMF_ERROR_INVALID_PARAM;
856 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
857 err = EMF_ERROR_OUT_OF_MEMORY;
858 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
859 err = EMF_ERROR_DB_FAILURE;
861 err = EMF_ERROR_UNKNOWN;
870 /* set sync db mailbox */
871 mailbox->synchronous = synchronous;
878 em_storage_free_read_mail_uid(&downloaded_uids, j, NULL);
880 if (imap_mailbox_tbl_item)
881 em_storage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
890 * create a new imap mailbox
892 * new_mailbox : imap mailbox to be created
897 EXPORT_API int em_core_mailbox_create_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
899 MAILSTREAM *stream = NULL;
900 char *long_enc_path = NULL;
901 void *tmp_stream = NULL;
903 int err = EMF_ERROR_NONE;
905 EM_DEBUG_FUNC_BEGIN();
908 *err_code = EMF_ERROR_NONE;
913 err = EMF_ERROR_INVALID_PARAM;
917 /* connect mail server */
919 if (!em_core_mailbox_open(mailbox->account_id, NULL, (void **)&tmp_stream, NULL))
921 err = EMF_ERROR_CONNECTION_FAILURE;
925 stream = (MAILSTREAM *)tmp_stream;
927 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
928 if (!em_core_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
930 err = EMF_ERROR_UNKNOWN;
935 if (!mail_create(stream, long_enc_path))
937 err = EMF_ERROR_UNKNOWN;
941 em_core_mailbox_close(0, stream);
944 EM_SAFE_FREE(long_enc_path);
951 em_core_mailbox_close(0, stream);
955 EM_SAFE_FREE(long_enc_path);
965 * delete a imap mailbox
967 * mailbox : mailbox to be deleted
972 EXPORT_API int em_core_mailbox_delete_imap_mailbox(emf_mailbox_t *mailbox, int *err_code)
974 MAILSTREAM *stream = NULL;
975 char *long_enc_path = NULL;
976 emf_account_t *ref_account = NULL;
977 void *tmp_stream = NULL;
979 int err = EMF_ERROR_NONE;
981 EM_DEBUG_FUNC_BEGIN();
984 *err_code = EMF_ERROR_NONE;
989 err = EMF_ERROR_INVALID_PARAM;
993 ref_account = em_core_get_account_reference(mailbox->account_id);
996 err = EMF_ERROR_INVALID_PARAM;
1000 /* if not imap4 mail, return */
1001 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
1002 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
1004 err = EMF_ERROR_INVALID_PARAM;
1008 /* connect mail server */
1010 if (!em_core_mailbox_open(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
1012 err = EMF_ERROR_CONNECTION_FAILURE;
1016 stream = (MAILSTREAM *)tmp_stream;
1018 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1019 if (!em_core_get_long_encoded_path(mailbox->account_id, mailbox->name, '/', &long_enc_path, err_code))
1021 err = EMF_ERROR_UNKNOWN;
1025 /* delete mailbox */
1026 if (!mail_delete(stream, long_enc_path))
1028 err = EMF_ERROR_UNKNOWN;
1032 em_core_mailbox_close(0, stream);
1035 EM_SAFE_FREE(long_enc_path);
1037 /* if deleted imap mailbox is synchronous mailbox, delete db imap mailbox from db */
1038 if (!em_storage_delete_mailbox(ref_account->account_id, 0, mailbox->name, true, &err)) {
1039 EM_DEBUG_EXCEPTION("\t em_storage_delete_mailbox failed - %d", err);
1041 if (err == EM_STORAGE_ERROR_MAILBOX_NOT_FOUND)
1042 err = EMF_ERROR_MAILBOX_NOT_FOUND;
1043 else if (err == EM_STORAGE_ERROR_INVALID_PARAM)
1044 err = EMF_ERROR_INVALID_PARAM;
1045 else if (err == EM_STORAGE_ERROR_OUT_OF_MEMORY)
1046 err = EMF_ERROR_OUT_OF_MEMORY;
1047 else if (err == EM_STORAGE_ERROR_DB_FAILURE)
1048 err = EMF_ERROR_DB_FAILURE;
1050 err = EMF_ERROR_UNKNOWN;
1059 em_core_mailbox_close(0, stream);
1063 EM_SAFE_FREE(long_enc_path);
1073 * change imap mailbox name
1075 * old_mailbox : previous mailbox
1076 * new_mailbox : renamed mailbox
1081 EXPORT_API int em_core_mailbox_modify_imap_mailbox(emf_mailbox_t *old_mailbox, emf_mailbox_t *new_mailbox, int *err_code)
1083 MAILSTREAM *stream = NULL;
1084 char *long_enc_path = NULL;
1085 char *long_enc_path_new = NULL;
1086 emf_mailbox_tbl_t imap_mailbox_tbl_item;
1087 emf_account_t *ref_account = NULL;
1088 void *tmp_stream = NULL;
1090 int err = EMF_ERROR_NONE;
1092 EM_DEBUG_FUNC_BEGIN();
1095 *err_code = EMF_ERROR_NONE;
1098 if (!old_mailbox || !new_mailbox)
1100 err = EMF_ERROR_INVALID_PARAM;
1104 ref_account = em_core_get_account_reference(old_mailbox->account_id);
1107 err = EMF_ERROR_INVALID_PARAM;
1111 if (!em_core_get_account_reference(new_mailbox->account_id))
1113 err = EMF_ERROR_INVALID_PARAM;
1117 /* if not imap4 mail, return */
1118 if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
1119 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4)
1121 err = EMF_ERROR_INVALID_PARAM;
1125 /* connect mail server */
1127 if (!em_core_mailbox_open(ref_account->account_id, NULL, (void **)&tmp_stream, NULL))
1129 err = EMF_ERROR_CONNECTION_FAILURE;
1133 stream = (MAILSTREAM *)tmp_stream;
1135 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1136 if (!em_core_get_long_encoded_path(old_mailbox->account_id, old_mailbox->name, '/', &long_enc_path, err_code))
1138 err = EMF_ERROR_UNKNOWN;
1142 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1143 if (!em_core_get_long_encoded_path(new_mailbox->account_id, new_mailbox->name, '/', &long_enc_path_new, err_code))
1145 err = EMF_ERROR_UNKNOWN;
1149 /* rename mailbox */
1150 if (!mail_rename(stream, long_enc_path, long_enc_path_new))
1152 err = EMF_ERROR_UNKNOWN;
1156 EM_SAFE_FREE(long_enc_path);
1157 EM_SAFE_FREE(long_enc_path_new);
1159 em_core_mailbox_close(0, stream);
1162 imap_mailbox_tbl_item.alias = NULL;
1163 imap_mailbox_tbl_item.mailbox_name = new_mailbox->name;
1164 imap_mailbox_tbl_item.modifiable_yn = 1;
1166 /* if modified imap mailbox is synchronous mailbox, update mailbox name from imap mailbox table */
1167 if (!em_storage_update_mailbox(ref_account->account_id, 0, old_mailbox->name, &imap_mailbox_tbl_item, true, &err))
1169 EM_DEBUG_EXCEPTION("em_storage_update_mailbox failed - %d", err);
1170 if (err == EM_STORAGE_ERROR_DB_IS_FULL)
1171 err = EMF_ERROR_MAIL_MEMORY_FULL;
1173 err = EMF_ERROR_DB_FAILURE;
1180 EM_SAFE_FREE(long_enc_path);
1181 EM_SAFE_FREE(long_enc_path_new);
1185 em_core_mailbox_close(0, stream);