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-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 *output_count, int *err_code)
55 EM_DEBUG_FUNC_BEGIN();
56 EM_DEBUG_LOG("output_count[%p], err_code[%p]", output_count, err_code);
58 int err = EMAIL_ERROR_NONE;
60 int ret = false, ret2;
62 if (output_count == NULL) {
63 err = EMAIL_ERROR_INVALID_PARAM;
67 ret2 = vconf_get_int(VCONF_KEY_DEFAULT_SLOT_SIZE, &mail_slot_count);
70 EM_DEBUG_EXCEPTION("vconf_get_int() Failed(%d)", ret2);
71 mail_slot_count = 100;
79 *output_count = mail_slot_count;
89 INTERNAL_FUNC int emcore_remove_overflowed_mails(emstorage_mailbox_tbl_t *intput_mailbox_tbl, int *err_code)
91 EM_DEBUG_FUNC_BEGIN("intput_mailbox_tbl[%p], err_code[%p]", intput_mailbox_tbl, err_code);
94 int *mail_id_list = NULL, mail_id_list_count = 0;
95 int err = EMAIL_ERROR_NONE;
96 email_account_t *account_ref = NULL;
98 if (!intput_mailbox_tbl || intput_mailbox_tbl->account_id < 1) {
99 if (intput_mailbox_tbl)
100 EM_DEBUG_EXCEPTION("Invalid Parameter. intput_mailbox_tbl->account_id [%d]", intput_mailbox_tbl->account_id);
101 err = EMAIL_ERROR_INVALID_PARAM;
105 account_ref = emcore_get_account_reference(intput_mailbox_tbl->account_id);
107 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
108 EM_DEBUG_LOG("ActiveSync Account didn't support mail slot");
109 err = EMAIL_ERROR_NOT_SUPPORTED;
114 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)) {
115 if (err == EMAIL_ERROR_MAIL_NOT_FOUND) {
116 EM_DEBUG_LOG("There are enough slot in intput_mailbox_tbl [%s]", intput_mailbox_tbl->mailbox_name);
117 err = EMAIL_ERROR_NONE;
121 EM_DEBUG_EXCEPTION("emstorage_get_overflowed_mail_id_list failed [%d]", err);
126 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)) {
127 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
134 EM_SAFE_FREE(mail_id_list);
139 EM_DEBUG_FUNC_END("ret [%d]", ret);
144 INTERNAL_FUNC int emcore_set_mail_slot_size(int account_id, int mailbox_id, int new_slot_size, int *err_code)
146 EM_DEBUG_FUNC_BEGIN("account_id [%d], mailbox_id[%d], err_code[%p]", account_id, mailbox_id, err_code);
148 int ret = false, err = EMAIL_ERROR_NONE;
150 int account_count = 100;
151 int mailbox_count = 0;
152 email_account_t *account_ref = NULL;
153 emstorage_account_tbl_t *account_tbl_list = NULL;
154 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
156 if (account_id > ALL_ACCOUNT) {
157 account_ref = emcore_get_account_reference(account_id);
158 if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC) {
159 EM_DEBUG_LOG("ActiveSync account didn't support mail slot");
163 else if (!account_ref) {
164 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
167 if (mailbox_id == 0) {
168 if ( (err = emstorage_set_field_of_accounts_with_integer_value(account_id, "default_mail_slot_size", new_slot_size, true)) != EMAIL_ERROR_NONE) {
169 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
175 if (mailbox_id == 0) {
176 if ( !emstorage_get_account_list(&account_count, &account_tbl_list, false, false, &err)) {
177 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
180 for ( i = 0; i < account_count; i++) {
181 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) {
182 EM_DEBUG_EXCEPTION("emstorage_set_field_of_accounts_with_integer_value failed [%d]", err);
190 if (!emstorage_set_mail_slot_size(account_id, mailbox_id, new_slot_size, true, &err)) {
191 EM_DEBUG_EXCEPTION("emstorage_set_mail_slot_size failed [%d]", err);
197 if (new_slot_size > 0) {
198 mailbox_tbl_list = em_malloc(sizeof(emstorage_mailbox_tbl_t) * mailbox_count);
199 if(!mailbox_tbl_list) {
200 EM_DEBUG_EXCEPTION("em_malloc failed");
203 mailbox_tbl_list->account_id = account_id;
204 mailbox_tbl_list->mailbox_id = mailbox_id;
205 mailbox_tbl_list->mail_slot_size = new_slot_size;
207 else { /* read information from DB */
208 if ((err = emstorage_get_mailbox_by_id(mailbox_id, &mailbox_tbl_list)) != EMAIL_ERROR_NONE) {
209 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
216 if (!emstorage_get_mailbox_list(account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &mailbox_count, &mailbox_tbl_list, true, &err)) {
217 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed [%d]", err);
222 for (i = 0; i < mailbox_count; i++) {
223 if (!emcore_remove_overflowed_mails(mailbox_tbl_list + i, &err)) {
224 if (err == EMAIL_ERROR_MAIL_NOT_FOUND || err == EMAIL_ERROR_NOT_SUPPORTED)
225 err = EMAIL_ERROR_NONE;
227 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed [%d]", err);
235 if (account_tbl_list)
236 emstorage_free_account(&account_tbl_list, account_count, NULL);
238 if (mailbox_tbl_list)
239 emstorage_free_mailbox(&mailbox_tbl_list, mailbox_count, NULL);
247 static int emcore_get_mailbox_connection_path(int account_id, char *mailbox_name, char **path, int *err_code)
249 email_account_t *ref_account = NULL;
253 ref_account = emcore_get_account_reference(account_id);
255 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
259 path_len = EM_SAFE_STRLEN(ref_account->incoming_server_address) +
260 (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 50;
262 *path = em_malloc(path_len);/* EM_SAFE_STRLEN(ref_account->incoming_server_address) + */
263 /* (mailbox_name ? EM_SAFE_STRLEN(mailbox_name) : 0) + 20); */
266 memset(*path, 0x00, path_len);
267 /* 1. server address / server type */
269 if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
270 SNPRINTF(*path + 1, path_len-1, "%s:%d/pop", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
273 SNPRINTF(*path + 1, path_len-1, "%s:%d/imap", ref_account->incoming_server_address, ref_account->incoming_server_port_number);
276 /* 2. set tls option if security connection */
277 /* if (ref_account->incoming_server_secure_connection) strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1)); */
278 if (ref_account->incoming_server_secure_connection & 0x01) {
279 strncat(*path + 1, "/ssl", path_len-(EM_SAFE_STRLEN(*path)-1));
281 if (ref_account->incoming_server_secure_connection & 0x02)
282 strncat(*path + 1, "/tls", path_len-(EM_SAFE_STRLEN(*path)-1));
284 strncat(*path + 1, "/notls", path_len-(EM_SAFE_STRLEN(*path)-1));
286 /* 3. re-format mailbox name (ex:"{mai.test.com:143/imap} or {mai.test.com:143/imap/tls}"} */
287 strncat(*path + 1, "}", path_len-EM_SAFE_STRLEN(*path)-1);
290 if (mailbox_name) strncat(*path, mailbox_name, path_len-EM_SAFE_STRLEN(*path)-1);
295 INTERNAL_FUNC int emcore_sync_mailbox_list(int account_id, char *mailbox_name, int handle, int *err_code)
297 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_name[%p], handle[%d], err_code[%p]", account_id, mailbox_name, handle, err_code);
300 int err = EMAIL_ERROR_NONE;
301 int status = EMAIL_DOWNLOAD_FAIL;
302 MAILSTREAM *stream = NULL;
303 email_internal_mailbox_t *mailbox_list = NULL;
304 email_account_t *ref_account = NULL;
305 void *tmp_stream = NULL;
306 char *mbox_path = NULL;
307 char *mailbox_name_for_mailbox_type = NULL;
308 int i = 0, count = 0, counter = 0, mailbox_type_list[EMAIL_MAILBOX_TYPE_ALL_EMAILS + 1] = {-1, -1, -1, -1, -1, -1, -1, -1};
310 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_START, account_id, 0, handle, 0))
311 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_START] Failed >>>> ");
313 if (!emcore_check_thread_status()) {
314 err = EMAIL_ERROR_CANCELLED;
317 if (!emnetwork_check_network_status(&err)) {
318 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", err);
322 ref_account = emcore_get_account_reference(account_id);
324 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
325 err = EMAIL_ERROR_INVALID_ACCOUNT;
329 /* if not imap4 mail, return */
330 if ( ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
331 EM_DEBUG_EXCEPTION("unsupported account...");
332 err = EMAIL_ERROR_INVALID_ACCOUNT;
336 /* get mail server path */
337 /* mbox_path is not used. the below func might be unnecessary */
338 if (!emcore_get_mailbox_connection_path(account_id, NULL, &mbox_path, &err) || !mbox_path) {
339 EM_DEBUG_EXCEPTION("emcore_get_mailbox_connection_path - %d", err);
344 if (!emcore_check_thread_status()) {
345 err = EMAIL_ERROR_CANCELLED;
350 if (!emcore_connect_to_remote_mailbox(account_id, 0, (void **)&tmp_stream, &err) || !tmp_stream) {
351 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
353 if (err == EMAIL_ERROR_CONNECTION_BROKEN)
354 err = EMAIL_ERROR_CANCELLED;
356 err = EMAIL_ERROR_CONNECTION_FAILURE;
358 status = EMAIL_DOWNLOAD_CONNECTION_FAIL;
362 EM_SAFE_FREE(mbox_path);
364 stream = (MAILSTREAM *)tmp_stream;
366 if (!emcore_check_thread_status()) {
367 err = EMAIL_ERROR_CANCELLED;
371 /* download mailbox list */
372 if (!emcore_download_mailbox_list(stream, mailbox_name, &mailbox_list, &count, &err)) {
373 EM_DEBUG_EXCEPTION("emcore_download_mailbox_list failed - %d", err);
377 if (!emcore_check_thread_status()) {
378 err = EMAIL_ERROR_CANCELLED;
382 for (i = 0; i < count; i++) {
383 if (!emcore_check_thread_status()) {
384 EM_DEBUG_LOG("emcore_check_thread_status - cancelled");
385 err = EMAIL_ERROR_CANCELLED;
388 if (mailbox_list[i].mailbox_name) {
389 EM_DEBUG_LOG("mailbox name - %s", mailbox_list[i].mailbox_name);
390 emcore_get_default_mail_slot_count(&(mailbox_list[i].mail_slot_size), NULL);
392 if(mailbox_list[i].mailbox_type == EMAIL_MAILBOX_TYPE_NONE)
393 emcore_bind_mailbox_type(mailbox_list + i);
395 if (mailbox_list[i].mailbox_type <= EMAIL_MAILBOX_TYPE_ALL_EMAILS) { /* if result mailbox type is duplicated, */
396 if (mailbox_type_list[mailbox_list[i].mailbox_type] != -1) {
397 EM_DEBUG_LOG("Mailbox type [%d] of [%s] is duplicated", mailbox_list[i].mailbox_type, mailbox_list[i].mailbox_name);
398 mailbox_list[i].mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED; /* ignore latest one */
401 mailbox_type_list[mailbox_list[i].mailbox_type] = i;
404 EM_DEBUG_LOG("mailbox type [%d]", mailbox_list[i].mailbox_type);
405 if(!emcore_set_sync_imap_mailbox(mailbox_list + i, 1, &err)) {
406 EM_DEBUG_EXCEPTION("emcore_set_sync_imap_mailbox failed [%d]", err);
414 for (counter = EMAIL_MAILBOX_TYPE_INBOX; counter <= EMAIL_MAILBOX_TYPE_OUTBOX; counter++) {
415 /* if (!emstorage_get_mailbox_name_by_mailbox_type(account_id, counter, &mailbox_name_for_mailbox_type, false, &err)) */
416 if (mailbox_type_list[counter] == -1) {
417 /* EM_DEBUG_EXCEPTION("emstorage_get_mailbox_name_by_mailbox_type failed - %d", err); */
418 /* if (EMAIL_ERROR_MAILBOX_NOT_FOUND == err) */
420 emstorage_mailbox_tbl_t mailbox_tbl;
422 memset(&mailbox_tbl, 0x00, sizeof(mailbox_tbl));
424 mailbox_tbl.account_id = account_id;
425 mailbox_tbl.mailbox_id = 0;
426 mailbox_tbl.local_yn = 1;
427 mailbox_tbl.mailbox_type = counter;
428 mailbox_tbl.deleted_flag = 0;
429 mailbox_tbl.modifiable_yn = 1;
430 mailbox_tbl.total_mail_count_on_server = 0;
431 emcore_get_default_mail_slot_count(&mailbox_tbl.mail_slot_size, NULL);
434 case EMAIL_MAILBOX_TYPE_SENTBOX:
435 mailbox_tbl.mailbox_name = EMAIL_SENTBOX_NAME;
436 mailbox_tbl.alias = EMAIL_SENTBOX_DISPLAY_NAME;
439 case EMAIL_MAILBOX_TYPE_TRASH:
440 mailbox_tbl.mailbox_name = EMAIL_TRASH_NAME;
441 mailbox_tbl.alias = EMAIL_TRASH_DISPLAY_NAME;
444 case EMAIL_MAILBOX_TYPE_DRAFT:
445 mailbox_tbl.mailbox_name = EMAIL_DRAFTBOX_NAME;
446 mailbox_tbl.alias = EMAIL_DRAFTBOX_DISPLAY_NAME;
449 case EMAIL_MAILBOX_TYPE_SPAMBOX:
450 mailbox_tbl.mailbox_name = EMAIL_SPAMBOX_NAME;
451 mailbox_tbl.alias = EMAIL_SPAMBOX_DISPLAY_NAME;
454 case EMAIL_MAILBOX_TYPE_OUTBOX:
455 mailbox_tbl.mailbox_name = EMAIL_OUTBOX_NAME;
456 mailbox_tbl.alias = EMAIL_OUTBOX_DISPLAY_NAME;
460 mailbox_tbl.mailbox_name = EMAIL_INBOX_NAME;
461 mailbox_tbl.alias = EMAIL_INBOX_DISPLAY_NAME;
465 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
466 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
474 /* goto FINISH_OFF; */
478 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
481 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
484 email_mailbox_t mailbox;
486 if (emstorage_get_mailbox_by_modifiable_yn(account_id, 0 /* modifiable_yn */, &select_num, &local_mailbox_list, true, &err)) {
487 if (local_mailbox_list) {
488 for (i = 0; i < select_num; i++) {
489 EM_DEBUG_LOG(">>> MailBox needs to be Deleted[ %s ] ", local_mailbox_list[i].mailbox_name);
490 mailbox.account_id = local_mailbox_list[i].account_id;
491 mailbox.mailbox_name = local_mailbox_list[i].mailbox_name;
492 mailbox.mailbox_id = local_mailbox_list[i].mailbox_id;
493 if (!emcore_delete_mailbox_all(&mailbox, &err)) {
494 EM_DEBUG_EXCEPTION(" emcore_delete_all of Mailbox [%s] Failed ", mailbox.mailbox_name);
495 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
496 local_mailbox_list = NULL;
500 emstorage_free_mailbox(&local_mailbox_list, select_num, NULL);
501 local_mailbox_list = NULL;
505 if (!emstorage_set_all_mailbox_modifiable_yn(account_id, 0, true, &err)) {
506 EM_DEBUG_EXCEPTION(" >>>> emstorage_set_all_mailbox_modifiable_yn Failed [ %d ]", err);
510 if (!emcore_check_thread_status()) {
511 err = EMAIL_ERROR_CANCELLED;
515 for (i = 0; i < count; i++)
516 mailbox_list[i].account_id = account_id;
523 if (err == EMAIL_ERROR_NONE) {
524 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH, account_id, 0, handle, err))
525 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FINISH] Failed >>>> ");
528 if (!emcore_notify_network_event(NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL, account_id, 0, handle, err))
529 EM_DEBUG_EXCEPTION("emcore_notify_network_event [ NOTI_SYNC_IMAP_MAILBOX_LIST_FAIL] Failed >>>> ");
531 EM_SAFE_FREE(mailbox_name_for_mailbox_type);
532 EM_SAFE_FREE(mbox_path);
535 emcore_close_mailbox(account_id, stream);
538 emcore_free_internal_mailbox(&mailbox_list, count, NULL);
540 if (err_code != NULL)
542 EM_DEBUG_FUNC_END("ret [%d]", ret);
546 int emcore_download_mailbox_list(void *mail_stream,
548 email_internal_mailbox_t **mailbox_list,
552 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);
554 MAILSTREAM *stream = mail_stream;
555 email_callback_holder_t holder;
556 char *pattern = NULL;
557 char *reference = NULL;
558 int err = EMAIL_ERROR_NONE;
561 if (!stream || !mailbox_list || !count) {
562 err = EMAIL_ERROR_INVALID_PARAM;
566 memset(&holder, 0x00, sizeof(holder));
568 /* reference (ex : "{mail.test.com}", "{mail.test.com}inbox") */
571 reference = em_malloc(EM_SAFE_STRLEN(stream->original_mailbox) + strlen(mailbox_name) + 1); /*prevent 34352*/
573 strncpy(reference, stream->original_mailbox, (size_t)EM_SAFE_STRLEN(stream->original_mailbox));
574 if ((s = strchr(reference, '}')))
576 strcat(reference, mailbox_name);
580 reference = EM_SAFE_STRDUP(stream->original_mailbox);
583 stream->sparep = &holder;
585 /* imap command : tag LIST reference * */
586 /* see callback function mm_list */
587 mail_list(stream, reference, pattern);
589 stream->sparep = NULL;
591 EM_SAFE_FREE(reference);
594 *mailbox_list = (email_internal_mailbox_t*)holder.data;
606 * check whether this imap mailbox is synchronous mailbox
608 * mailbox : imap mailbox to be checked
609 * synchronous : boolean variable to be synchronous (1 : sync 0 : non-sync)
614 int emcore_check_sync_imap_mailbox(email_mailbox_t *mailbox, int *synchronous, int *err_code)
616 EM_DEBUG_FUNC_BEGIN();
618 EM_DEBUG_LOG("\t mailbox[%p], synchronous[%p], err_code[%p]", mailbox, synchronous, err_code);
621 *err_code = EMAIL_ERROR_NONE;
624 if (!mailbox || !synchronous) {
625 EM_DEBUG_EXCEPTION("\t mailbox[%p], synchronous[%p]", mailbox, synchronous);
627 if (err_code != NULL)
628 *err_code = EMAIL_ERROR_INVALID_PARAM;
633 int err = EMAIL_ERROR_NONE;
634 emstorage_mailbox_tbl_t *imap_mailbox_tbl = NULL;
636 if (!emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl, true, &err)) {
637 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed - %d", err);
641 *synchronous = imap_mailbox_tbl ? 1 : 0;
646 if (imap_mailbox_tbl != NULL)
647 emstorage_free_mailbox(&imap_mailbox_tbl, 1, NULL);
649 if (err_code != NULL)
657 * set sync imap mailbox
659 * mailbox_list : imap mailbox to be synced
660 * syncronous : 0-sync 1 : non-sync
666 INTERNAL_FUNC int emcore_set_sync_imap_mailbox(email_internal_mailbox_t *mailbox, int synchronous, int *err_code)
668 EM_DEBUG_FUNC_BEGIN("mailbox[%p], synchronous[%d], err_code[%p]", mailbox, synchronous, err_code);
671 EM_DEBUG_EXCEPTION("mailbox[%p], synchronous[%d]", mailbox, synchronous);
672 if (err_code != NULL)
673 *err_code = EMAIL_ERROR_INVALID_PARAM;
678 int err = EMAIL_ERROR_NONE;
679 emstorage_mailbox_tbl_t *imap_mailbox_tbl_item = NULL;
680 emstorage_mailbox_tbl_t mailbox_tbl = { 0, };
681 emcore_uid_list *uid_list = NULL;
682 emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
683 MAILSTREAM *stream = mailbox->mail_stream;
684 int mailbox_renamed = 0;
688 IMAPLOCAL *imap_local = NULL;
689 char cmd[128] = { 0 , }, tag[32] = { 0 , }, *p = NULL;
692 /* if synchcronous, insert imap mailbox to db */
693 if (emstorage_get_mailbox_by_name(mailbox->account_id, 0, mailbox->mailbox_name, &imap_mailbox_tbl_item, true, &err)) {
694 /* mailbox already exists */
695 /* mailbox Found, Do set the modifiable_yn = 1 */
696 EM_DEBUG_LOG("mailbox already exists and setting modifiable_yn to 1");
697 if (!emstorage_update_mailbox_modifiable_yn(mailbox->account_id, 0, mailbox->mailbox_name, 1, true, &err)) {
698 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox_modifiable_yn Failed [ %d ] ", err);
703 if (err != EMAIL_ERROR_MAILBOX_NOT_FOUND) {
704 EM_DEBUG_EXCEPTION(">>>>.>>>>>Getting mailbox failed>>>>>>>>>>>>>>");
705 /* This is error scenario so finish the job */
709 /* This is not error scenario - mailbox is either new/renamed mailbox and needs to be added/modfied in DB */
710 /* Now check if mailbox is renamed */
711 EM_DEBUG_LOG(">>>>>>>>>>>>>>>>>>>>>>>MAILBOX NEW OR RENAMED");
713 imap_local = ((MAILSTREAM *)stream)->local;
715 sprintf(tag, "%08lx", 0xffffffff & (((MAILSTREAM *)stream)->gensym++));
717 sprintf(cmd, "%s SELECT %s\015\012", tag, mailbox->mailbox_name);
722 /* select the mailbox and get its UID */
723 if (!imap_local || !imap_local->netstream || !net_sout(imap_local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
724 EM_DEBUG_EXCEPTION("network error - failed to IDLE on Mailbox [%s]", mailbox->mailbox_name);
726 err = EMAIL_ERROR_CONNECTION_BROKEN;
728 imap_local->netstream = NULL;
729 mailbox->mail_stream = NULL;
734 EM_DEBUG_LOG("Get response for select call");
735 while (imap_local->netstream) {
736 p = net_getline(imap_local->netstream);
737 EM_DEBUG_LOG("p =[%s]", p);
738 if (!strncmp(p, "+", 1)) {
742 else if (!strncmp(p, "*", 1)) {
753 /* check if OK or BAD response comes. */
754 /* if response is OK the try getting UID list. */
755 if (!strncmp((char *)imap_local->reply.key, "OK", strlen("OK"))) {
756 EM_DEBUG_LOG(">>>>>>>>>>Select success on %s mailbox", mailbox->mailbox_name);
757 if (!imap4_mailbox_get_uids(stream, &uid_list, &err)) {
758 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed - %d", err);
759 EM_SAFE_FREE(uid_list);
762 if (!emstorage_get_downloaded_list(mailbox->account_id, 0, &downloaded_uids, &j, true, &err)) {
763 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
765 downloaded_uids = NULL;
767 else /* Prevent Defect - 28497 */ {
768 emcore_uid_list *uid_elem = uid_list;
769 emcore_uid_list *next_uid_elem = NULL;
771 for (i = j; (i > 0 && !mailbox_renamed); i--) {
773 next_uid_elem = uid_elem->next;
774 if (uid_elem->uid && downloaded_uids[i - 1].s_uid && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
779 EM_SAFE_FREE(uid_elem->uid);
780 uid_elem = next_uid_elem;
786 } /* mailbox selected */
789 if (mailbox_renamed) /* renamed mailbox */ {
790 EM_DEBUG_LOG("downloaded_uids[temp].mailbox_name [%s]", downloaded_uids[temp].mailbox_name);
791 /* Do a mailbox rename in the DB */
792 if (!emstorage_modify_mailbox_of_mails(downloaded_uids[temp].mailbox_name, mailbox->mailbox_name, true, &err))
793 EM_DEBUG_EXCEPTION(" emstorage_modify_mailbox_of_mails Failed [%d]", err);
797 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
799 mailbox_tbl.account_id = mailbox->account_id;
800 mailbox_tbl.local_yn = 0;
801 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
802 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
804 /* Get the Alias Name after parsing the Full mailbox Path */
805 if(mailbox->alias == NULL)
806 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
808 mailbox_tbl.alias = mailbox->alias;
809 mailbox_tbl.deleted_flag = 1;
810 mailbox_tbl.modifiable_yn = 1;
811 mailbox_tbl.total_mail_count_on_server = 0;
813 /* if non synchronous, delete imap mailbox from db */
814 if (!emstorage_update_mailbox(mailbox->account_id, 0, downloaded_uids[temp].mailbox_id, &mailbox_tbl, true, &err)) {
815 EM_DEBUG_EXCEPTION(" emstorage_update_mailbox Failed [ %d ] ", err);
820 else /* Its a Fresh Mailbox */ {
821 memset(&mailbox_tbl, 0, sizeof(emstorage_mailbox_tbl_t));
823 mailbox_tbl.mailbox_id = mailbox->mailbox_id;
824 mailbox_tbl.account_id = mailbox->account_id;
825 mailbox_tbl.local_yn = 0;
826 mailbox_tbl.deleted_flag = 0;
827 mailbox_tbl.mailbox_type = mailbox->mailbox_type;
828 mailbox_tbl.mailbox_name = mailbox->mailbox_name;
829 mailbox_tbl.mail_slot_size = mailbox->mail_slot_size;
830 mailbox_tbl.no_select = mailbox->no_select;
832 /* Get the Alias Name after Parsing the Full mailbox Path */
833 if(mailbox->alias == NULL)
834 mailbox->alias = emcore_get_alias_of_mailbox((const char *)mailbox->mailbox_name);
836 if (mailbox->alias) {
837 EM_DEBUG_LOG("mailbox->alias [%s] ", mailbox->alias);
839 mailbox_tbl.alias = mailbox->alias;
840 mailbox_tbl.modifiable_yn = 1;
841 mailbox_tbl.total_mail_count_on_server = 0;
843 EM_DEBUG_LOG("mailbox_tbl.mailbox_type - %d", mailbox_tbl.mailbox_type);
845 if (!emstorage_add_mailbox(&mailbox_tbl, true, &err)) {
846 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed - %d", err);
855 /* set sync db mailbox */
856 mailbox->synchronous = synchronous;
863 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
865 if (imap_mailbox_tbl_item)
866 emstorage_free_mailbox(&imap_mailbox_tbl_item, 1, NULL);
875 * create a new imap mailbox
877 * new_mailbox : imap mailbox to be created
882 INTERNAL_FUNC int emcore_create_imap_mailbox(email_mailbox_t *mailbox, int *err_code)
884 MAILSTREAM *stream = NULL;
885 char *long_enc_path = NULL;
886 void *tmp_stream = NULL;
888 int err = EMAIL_ERROR_NONE;
890 EM_DEBUG_FUNC_BEGIN();
893 err = EMAIL_ERROR_INVALID_PARAM;
897 /* connect mail server */
899 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, 0, (void **)&tmp_stream, &err)) {
900 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
904 stream = (MAILSTREAM *)tmp_stream;
906 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
907 if (!emcore_get_long_encoded_path(mailbox->account_id, mailbox->mailbox_name, '/', &long_enc_path, &err)) {
908 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
913 if (!mail_create(stream, long_enc_path)) {
914 EM_DEBUG_EXCEPTION("mail_create failed");
915 err = EMAIL_ERROR_IMAP4_CREATE_FAILURE;
919 emcore_close_mailbox(0, stream);
922 EM_SAFE_FREE(long_enc_path);
928 emcore_close_mailbox(0, stream);
932 EM_SAFE_FREE(long_enc_path);
935 if (err == EMAIL_ERROR_NONE) {
936 if(!emcore_notify_network_event(NOTI_ADD_MAILBOX_FINISH, mailbox->account_id, mailbox->mailbox_name, 0, 0))
937 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
939 else if (!emcore_notify_network_event(NOTI_ADD_MAILBOX_FAIL, mailbox->account_id, mailbox->mailbox_name, 0, err))
940 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
951 * delete a imap mailbox
953 * input_mailbox_id : mailbox ID to be deleted
958 INTERNAL_FUNC int emcore_delete_imap_mailbox(int input_mailbox_id, int *err_code)
960 EM_DEBUG_FUNC_BEGIN();
962 MAILSTREAM *stream = NULL;
963 char *long_enc_path = NULL;
964 email_account_t *ref_account = NULL;
965 void *tmp_stream = NULL;
967 int err = EMAIL_ERROR_NONE;
968 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
970 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
971 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
975 ref_account = emcore_get_account_reference(mailbox_tbl->account_id);
977 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
978 EM_DEBUG_EXCEPTION("Invalid account information");
979 err = EMAIL_ERROR_INVALID_ACCOUNT;
983 /* connect mail server */
984 if (!emcore_connect_to_remote_mailbox(mailbox_tbl->account_id, 0, (void **)&tmp_stream, &err)) {
985 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
989 stream = (MAILSTREAM *)tmp_stream;
991 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
992 if (!emcore_get_long_encoded_path(mailbox_tbl->account_id, mailbox_tbl->mailbox_name, '/', &long_enc_path, &err)) {
993 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed [%d]", err);
998 if (!mail_delete(stream, long_enc_path)) {
999 EM_DEBUG_EXCEPTION("mail_delete failed");
1000 err = EMAIL_ERROR_IMAP4_DELETE_FAILURE;
1004 emcore_close_mailbox(0, stream);
1007 EM_SAFE_FREE(long_enc_path);
1013 emcore_close_mailbox(0, stream);
1017 EM_SAFE_FREE(long_enc_path);
1020 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1022 if (err == EMAIL_ERROR_NONE) {
1023 if(!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FINISH, input_mailbox_id, 0, 0, 0))
1024 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FINISH] failed");
1026 else if (!emcore_notify_network_event(NOTI_DELETE_MAILBOX_FAIL, input_mailbox_id, 0, 0, err))
1027 EM_DEBUG_EXCEPTION("emcore_notify_network_event[NOTI_ADD_MAILBOX_FAIL] failed");
1036 INTERNAL_FUNC int emcore_move_mailbox_on_imap_server(int input_account_id, char *input_old_mailbox_path, char *input_new_mailbox_path)
1038 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);
1039 MAILSTREAM *stream = NULL;
1040 char *long_enc_path_old = NULL;
1041 char *long_enc_path_new = NULL;
1042 email_account_t *ref_account = NULL;
1043 void *tmp_stream = NULL;
1044 int err = EMAIL_ERROR_NONE;
1046 if (!input_old_mailbox_path || !input_new_mailbox_path) {
1047 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1048 err = EMAIL_ERROR_INVALID_PARAM;
1052 ref_account = emcore_get_account_reference(input_account_id);
1054 if (!ref_account || ref_account->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
1055 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ACCOUNT");
1056 err = EMAIL_ERROR_INVALID_ACCOUNT;
1060 /* connect mail server */
1062 if (!emcore_connect_to_remote_mailbox(input_account_id, 0, (void **)&tmp_stream, &err)) {
1063 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed. [%d]", err);
1067 stream = (MAILSTREAM *)tmp_stream;
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_old_mailbox_path, '/', &long_enc_path_old, &err)) {
1071 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1075 /* encode mailbox name by UTF7, and rename to full path (ex : {mail.test.com}child_mailbox) */
1076 if (!emcore_get_long_encoded_path(input_account_id, input_new_mailbox_path, '/', &long_enc_path_new, &err)) {
1077 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed. [%d]", err);
1081 /* rename mailbox */
1082 if (!mail_rename(stream, long_enc_path_old, long_enc_path_new)) {
1083 err = EMAIL_ERROR_IMAP4_RENAME_FAILURE;
1088 EM_SAFE_FREE(long_enc_path_old);
1089 EM_SAFE_FREE(long_enc_path_new);
1092 emcore_close_mailbox(0, stream);
1096 EM_DEBUG_FUNC_END("err [%d]", err);