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.
23 /******************************************************************************
24 * File : email-core-mailbox.c
25 * Desc : Local Mailbox Management
29 *****************************************************************************/
34 #include <sys/types.h>
35 #include "email-types.h"
36 #include "email-utilities.h"
37 #include "email-debug-log.h"
38 #include "email-core-global.h"
39 #include "email-core-utils.h"
40 #include "email-core-mailbox.h"
41 #include "email-core-event.h"
42 #include "email-core-mail.h"
43 #include "email-core-imap-mailbox.h"
44 #include "email-storage.h"
45 #include "email-core-account.h"
47 #ifdef __FEATURE_KEEP_CONNECTION__
48 static void *g_receiving_thd_stream = NULL; /* Stores the recv thd stream for next time reuse */
49 static int prev_acc_id_recv_thd = 0; /* Stores the account id for which recv thd stream is open */
50 extern int recv_thread_run;
52 static void *g_partial_body_thd_stream = NULL; /* Stores the pb thd stream for next time reuse */
53 static int prev_acc_id_pb_thd = 0; /* Stores the account id for which pb thd stream is open */
55 __thread email_connection_info_t *g_connection_info_list = NULL;
57 static pthread_mutex_t _close_stream_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect closing stream */
58 #endif /* __FEATURE_KEEP_CONNECTION__ */
61 /* Binding IMAP mailbox with its function */
62 static email_mailbox_type_item_t g_mailbox_type[MAX_MAILBOX_TYPE] = {
63 {EMAIL_MAILBOX_TYPE_INBOX, "INBOX" },
65 {EMAIL_MAILBOX_TYPE_INBOX, "Inbox" },
66 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent Messages"} ,
67 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTOLpUx3zVaA-"} ,
68 {EMAIL_MAILBOX_TYPE_DRAFT, "Drafts"} ,
69 {EMAIL_MAILBOX_TYPE_TRASH, "Deleted Messages" } ,
71 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent"} ,
72 {EMAIL_MAILBOX_TYPE_SPAMBOX, "Spam" },
73 {EMAIL_MAILBOX_TYPE_DRAFT, "Drafts"} ,
74 {EMAIL_MAILBOX_TYPE_TRASH, "Trash"},
76 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTONO4ycDVaA-"},
78 {EMAIL_MAILBOX_TYPE_SENTBOX, "mail/sent-mail"},
79 {EMAIL_MAILBOX_TYPE_SPAMBOX, "mail/spam-mail" },
80 {EMAIL_MAILBOX_TYPE_DRAFT, "mail/saved-drafts"} ,
81 {EMAIL_MAILBOX_TYPE_TRASH, "mail/mail-trash"},
84 #ifdef __FEATURE_KEEP_CONNECTION__
85 email_connection_info_t* emcore_get_connection_info_by_account_id(int account_id)
87 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
88 email_connection_info_t *connection_info = g_connection_info_list;
90 while(connection_info) {
91 if(connection_info->account_id == account_id)
93 connection_info = connection_info->next;
96 EM_DEBUG_FUNC_END("connection_info [%p]", connection_info);
97 return connection_info;
100 int emcore_append_connection_info(email_connection_info_t *new_connection_info)
102 EM_DEBUG_FUNC_BEGIN("new_connection_info [%p]", new_connection_info);
103 email_connection_info_t *connection_info = g_connection_info_list;
105 if(!new_connection_info) {
106 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
107 return EMAIL_ERROR_INVALID_PARAM;
110 if(emcore_get_connection_info_by_account_id(new_connection_info->account_id)) {
111 EM_DEBUG_EXCEPTION("EMAIL_ERROR_ALREADY_EXISTS");
112 return EMAIL_ERROR_ALREADY_EXISTS;
115 if(connection_info) {
116 while(connection_info) {
117 if(connection_info->next == NULL) {
118 connection_info->next = new_connection_info;
119 new_connection_info->next = NULL;
121 connection_info = connection_info->next;
125 new_connection_info->next = NULL;
126 g_connection_info_list = new_connection_info;
129 EM_DEBUG_FUNC_END("EMAIL_ERROR_NONE");
130 return EMAIL_ERROR_NONE;
133 INTERNAL_FUNC int emcore_remove_connection_info(int account_id)
135 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
136 email_connection_info_t *connection_info = g_connection_info_list, *prev_connection_info = NULL;
138 while(connection_info) {
139 if(connection_info->account_id == account_id) {
140 if(prev_connection_info) {
141 prev_connection_info->next = connection_info->next;
144 g_connection_info_list = connection_info->next;
146 EM_SAFE_FREE(connection_info);
149 prev_connection_info = connection_info;
150 connection_info = connection_info->next;
153 EM_DEBUG_FUNC_END("");
154 return EMAIL_ERROR_NONE;
157 #endif /* __FEATURE_KEEP_CONNECTION__ */
161 * get local mailbox list
163 INTERNAL_FUNC int emcore_get_list(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
165 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
167 if (account_id <= 0 || !mailbox_list || !p_count) {
168 EM_DEBUG_EXCEPTION("PARAM Failed account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
169 if (err_code != NULL)
170 *err_code = EMAIL_ERROR_INVALID_PARAM;
175 int error = EMAIL_ERROR_NONE;
176 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
177 email_account_t *ref_account = NULL;
180 /* get mailbox list from mailbox table */
182 if (!(ref_account = emcore_get_account_reference(account_id))) {
183 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed - %d", account_id);
184 error = EMAIL_ERROR_INVALID_ACCOUNT;
188 if (!emstorage_get_mailbox_list(ref_account->account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &count, &local_mailbox_list, true, &error)) {
189 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox failed - %d", error);
195 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * count))) {
196 EM_DEBUG_EXCEPTION(" mailloc failed...");
197 error = EMAIL_ERROR_OUT_OF_MEMORY;
201 memset(*mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
203 for (i = 0; i < count; i++) {
204 (*mailbox_list)[i].mailbox_id = local_mailbox_list[i].mailbox_id;
205 (*mailbox_list)[i].account_id = account_id;
206 (*mailbox_list)[i].mailbox_name = local_mailbox_list[i].mailbox_name; local_mailbox_list[i].mailbox_name = NULL;
207 (*mailbox_list)[i].alias = local_mailbox_list[i].alias; local_mailbox_list[i].alias = NULL;
208 (*mailbox_list)[i].local = local_mailbox_list[i].local_yn;
209 (*mailbox_list)[i].mailbox_type = local_mailbox_list[i].mailbox_type;
210 (*mailbox_list)[i].unread_count = local_mailbox_list[i].unread_count;
211 (*mailbox_list)[i].total_mail_count_on_local = local_mailbox_list[i].total_mail_count_on_local;
212 (*mailbox_list)[i].total_mail_count_on_server = local_mailbox_list[i].total_mail_count_on_server;
213 (*mailbox_list)[i].mail_slot_size = local_mailbox_list[i].mail_slot_size;
224 if (local_mailbox_list != NULL)
225 emstorage_free_mailbox(&local_mailbox_list, count, NULL);
227 if (err_code != NULL)
235 * get imap sync mailbox list
237 int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
239 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
241 if (account_id <= 0 || !mailbox_list || !p_count) {
242 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
243 if (err_code != NULL)
244 *err_code = EMAIL_ERROR_INVALID_PARAM;
249 int error = EMAIL_ERROR_NONE;
250 email_mailbox_t *tmp_mailbox_list = NULL;
251 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
252 email_account_t *ref_account = NULL;
255 /* get mailbox list from mailbox table */
256 if (!(ref_account = emcore_get_account_reference(account_id))) {
257 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
258 error = EMAIL_ERROR_INVALID_ACCOUNT;
262 if (!emstorage_get_mailbox_list(ref_account->account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &count, &mailbox_tbl_list, true, &error)) {
263 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed - %d", error);
269 if (!(tmp_mailbox_list = em_malloc(sizeof(email_mailbox_t) * count))) {
270 EM_DEBUG_EXCEPTION("malloc failed...");
271 error = EMAIL_ERROR_OUT_OF_MEMORY;
275 memset(tmp_mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
277 for (i = 0; i < count; i++) {
278 tmp_mailbox_list[i].mailbox_id = mailbox_tbl_list[i].mailbox_id;
279 tmp_mailbox_list[i].account_id = account_id;
280 tmp_mailbox_list[i].mailbox_name = mailbox_tbl_list[i].mailbox_name; mailbox_tbl_list[i].mailbox_name = NULL;
281 tmp_mailbox_list[i].mailbox_type = mailbox_tbl_list[i].mailbox_type;
282 tmp_mailbox_list[i].alias = mailbox_tbl_list[i].alias; mailbox_tbl_list[i].alias = NULL;
283 tmp_mailbox_list[i].local = mailbox_tbl_list[i].local_yn;
284 tmp_mailbox_list[i].unread_count = mailbox_tbl_list[i].unread_count;
285 tmp_mailbox_list[i].total_mail_count_on_local = mailbox_tbl_list[i].total_mail_count_on_local;
286 tmp_mailbox_list[i].total_mail_count_on_server = mailbox_tbl_list[i].total_mail_count_on_server;
287 tmp_mailbox_list[i].mail_slot_size = mailbox_tbl_list[i].mail_slot_size;
291 tmp_mailbox_list = NULL;
297 *mailbox_list = tmp_mailbox_list;
300 if (mailbox_tbl_list != NULL)
301 emstorage_free_mailbox(&mailbox_tbl_list, count, NULL);
303 if (err_code != NULL)
305 EM_DEBUG_FUNC_END("error [%d]", error);
309 INTERNAL_FUNC int emcore_get_mail_count(email_mailbox_t *mailbox, int *total, int *unseen, int *err_code)
311 EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
314 int err = EMAIL_ERROR_NONE;
317 EM_DEBUG_EXCEPTION(" mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
318 err = EMAIL_ERROR_INVALID_PARAM;
322 if (!emstorage_get_mail_count(mailbox->account_id, mailbox->mailbox_name, total, unseen, true, &err)) {
323 EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
332 if (err_code != NULL)
338 INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_server, int *err_code)
340 EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
342 int err = EMAIL_ERROR_NONE;
343 emstorage_mailbox_tbl_t local_mailbox;
345 if (new_mailbox == NULL || new_mailbox->mailbox_name == NULL) {
346 err = EMAIL_ERROR_INVALID_PARAM;
351 /* Create a mailbox from Sever */
352 if (!emcore_create_imap_mailbox(new_mailbox, &err)) {
353 EM_DEBUG_EXCEPTION(">>>>> mailbox Creation in Server FAILED >>> ");
358 EM_DEBUG_LOG(">>>>> mailbox Creation in Server SUCCESS >>> ");
361 memset(&local_mailbox, 0x00, sizeof(emstorage_mailbox_tbl_t));
362 EM_DEBUG_LOG("box name[%s] local yn[%d] mailbox_type[%d]", new_mailbox->mailbox_name, local_mailbox.local_yn, new_mailbox->mailbox_type);
364 /* add local mailbox into local mailbox table */
365 local_mailbox.mailbox_id = new_mailbox->mailbox_id;
366 local_mailbox.account_id = new_mailbox->account_id;
367 local_mailbox.local_yn = new_mailbox->local;
368 local_mailbox.sync_with_server_yn = local_mailbox.local_yn ? 0 : 1;
369 local_mailbox.mailbox_name = new_mailbox->mailbox_name;
370 local_mailbox.alias = new_mailbox->alias;
371 local_mailbox.mailbox_type = new_mailbox->mailbox_type;
372 local_mailbox.unread_count = 0;
373 local_mailbox.total_mail_count_on_local = 0;
374 local_mailbox.total_mail_count_on_server = 0;
375 emcore_get_default_mail_slot_count(&local_mailbox.mail_slot_size, NULL);
377 if (strncmp(new_mailbox->mailbox_name, EMAIL_INBOX_NAME, strlen(EMAIL_INBOX_NAME)) == 0 ||
378 strncmp(new_mailbox->mailbox_name, EMAIL_DRAFTBOX_NAME, strlen(EMAIL_DRAFTBOX_NAME)) == 0 ||
379 strncmp(new_mailbox->mailbox_name, EMAIL_OUTBOX_NAME, strlen(EMAIL_OUTBOX_NAME)) == 0 ||
380 strncmp(new_mailbox->mailbox_name, EMAIL_SENTBOX_NAME, strlen(EMAIL_SENTBOX_NAME)) == 0)
381 local_mailbox.modifiable_yn = 0; /* can be deleted/modified */
383 local_mailbox.modifiable_yn = 1;
386 if (!emstorage_add_mailbox(&local_mailbox, true, &err)) {
387 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed [%d]", err);
391 new_mailbox->mailbox_id = local_mailbox.mailbox_id;
401 INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int on_server, int *err_code)
403 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
406 int err = EMAIL_ERROR_NONE;
407 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
409 if (input_mailbox_id == 0) {
410 EM_DEBUG_EXCEPTION(" input_mailbox_id == 0");
411 err = EMAIL_ERROR_INVALID_PARAM;
415 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
416 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
420 if (!emcore_delete_all_mails_of_mailbox(input_mailbox_id, on_server, &err)) {
421 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
426 EM_DEBUG_LOG(">> Delete the mailbox in Sever >>> ");
427 if (!emcore_delete_imap_mailbox(input_mailbox_id, &err))
428 EM_DEBUG_EXCEPTION("Delete the mailbox in server : failed");
430 EM_DEBUG_LOG("Delete the mailbox in server : success");
433 if (!emstorage_delete_mailbox(mailbox_tbl->account_id, -1, input_mailbox_id, true, &err)) {
434 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
443 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
445 if (err_code != NULL)
451 INTERNAL_FUNC int emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_code)
453 EM_DEBUG_FUNC_BEGIN();
455 EM_DEBUG_LOG(" mailbox[%p], err_code[%p]", mailbox, err_code);
458 int err = EMAIL_ERROR_NONE;
460 if (mailbox == NULL) {
461 EM_DEBUG_EXCEPTION(" mailbox[%p]", mailbox);
463 err = EMAIL_ERROR_INVALID_PARAM;
467 if (!emcore_delete_all_mails_of_mailbox(mailbox->mailbox_id, 0, /*NULL, */ &err)) {
468 EM_DEBUG_EXCEPTION(" emcore_delete_all_mails_of_mailbox failed - %d", err);
473 if (!emstorage_delete_mailbox(mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
474 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
483 if (err_code != NULL)
489 INTERNAL_FUNC int emcore_update_mailbox(email_mailbox_t *old_mailbox, email_mailbox_t *new_mailbox, int *err_code)
491 EM_DEBUG_FUNC_BEGIN("old_mailbox[%p], new_mailbox[%p], err_code[%p]", old_mailbox, new_mailbox, err_code);
494 int err = EMAIL_ERROR_NONE;
496 if (old_mailbox == NULL || new_mailbox == NULL) {
497 EM_DEBUG_EXCEPTION("old_mailbox[%p], new_mailbox[%p]", old_mailbox, new_mailbox);
499 err = EMAIL_ERROR_INVALID_PARAM;
503 emstorage_mailbox_tbl_t new_mailbox_tbl;
504 memset(&new_mailbox_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
506 /* Support only updating mailbox_type */
507 new_mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
509 if (old_mailbox->mailbox_type != new_mailbox_tbl.mailbox_type) {
510 if (!emstorage_update_mailbox_type(old_mailbox->account_id, -1, old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_type, true, &err)) {
511 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
516 new_mailbox_tbl.mailbox_id = old_mailbox->mailbox_id;
517 new_mailbox_tbl.account_id = old_mailbox->account_id;
518 new_mailbox_tbl.mailbox_name = new_mailbox->mailbox_name;
519 new_mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
520 new_mailbox_tbl.alias = new_mailbox->alias;
521 new_mailbox_tbl.mail_slot_size = new_mailbox->mail_slot_size;
522 new_mailbox_tbl.total_mail_count_on_server = new_mailbox->total_mail_count_on_server;
524 if (!emstorage_update_mailbox(old_mailbox->account_id, -1, old_mailbox->mailbox_id, &new_mailbox_tbl, true, &err)) {
525 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
530 if (EM_SAFE_STRCMP(old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_name) != 0) {
531 if ( (err = emstorage_rename_mailbox(old_mailbox->mailbox_id, new_mailbox_tbl.mailbox_name, new_mailbox_tbl.alias, true)) != EMAIL_ERROR_NONE) {
532 EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
547 extern int try_auth_smtp;
549 #ifdef __FEATURE_KEEP_CONNECTION__
550 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
551 #endif /* __FEATURE_KEEP_CONNECTION__ */
553 INTERNAL_FUNC int emcore_connect_to_remote_mailbox_with_account_info(email_account_t *account, int input_mailbox_id, void **result_stream, int *err_code)
555 EM_PROFILE_BEGIN(emCoreMailboxOpen);
556 EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account, input_mailbox_id, result_stream, err_code);
559 int error = EMAIL_ERROR_NONE;
560 email_session_t *session = NULL;
561 char *mbox_path = NULL;
562 void *reusable_stream = NULL;
563 int is_connection_for = _SERVICE_THREAD_TYPE_NONE;
564 emstorage_mailbox_tbl_t* mailbox = NULL;
565 char *mailbox_name = NULL;
567 if (account == NULL) {
568 EM_DEBUG_EXCEPTION("Invalid Parameter.");
569 error = EMAIL_ERROR_INVALID_PARAM;
573 if (!emcore_get_current_session(&session)) {
574 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
575 error = EMAIL_ERROR_SESSION_NOT_FOUND;
579 if (input_mailbox_id == 0 || input_mailbox_id != EMAIL_CONNECT_FOR_SENDING)
580 is_connection_for = _SERVICE_THREAD_TYPE_RECEIVING;
582 is_connection_for = _SERVICE_THREAD_TYPE_SENDING;
584 #ifdef __FEATURE_KEEP_CONNECTION__
585 email_connection_info_t *connection_info = emcore_get_connection_info_by_account_id(account->account_id);
587 if(connection_info) {
588 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
589 if(connection_info->receiving_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
590 reusable_stream = connection_info->receiving_server_stream;
593 if(connection_info->sending_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
594 reusable_stream = connection_info->sending_server_stream;
598 if (reusable_stream != NULL)
599 EM_DEBUG_LOG("Stream reuse desired");
601 reusable_stream = *result_stream;
604 session->error = EMAIL_ERROR_NONE;
605 emcore_set_network_error(EMAIL_ERROR_NONE); /* set current network error as EMAIL_ERROR_NONE before network operation */
607 if (input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
608 mailbox_name = EM_SAFE_STRDUP(ENCODED_PATH_SMTP);
610 else if (input_mailbox_id == 0) {
613 if ( (error = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
614 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
617 mailbox_name = EM_SAFE_STRDUP(mailbox->mailbox_name);
620 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
621 /* open pop3/imap server */
622 MAILSTREAM *mail_stream = NULL;
624 if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, '/', &mbox_path, &error)) {
625 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed - %d", error);
626 session->error = error;
630 EM_DEBUG_LOG("open mail connection to mbox_path [%s]", mbox_path);
632 try_auth = 0; /* ref_account->receiving_auth ? 1 : 0 */
633 session->auth = 0; /* ref_account->receiving_auth ? 1 : 0 */
635 if (!(mail_stream = mail_open(reusable_stream, mbox_path, IMAP_2004_LOG))) {
636 EM_DEBUG_EXCEPTION("mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
638 if (session->network != EMAIL_ERROR_NONE)
639 session->error = session->network;
640 if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
641 session->error = EMAIL_ERROR_CONNECTION_FAILURE;
643 error = session->error;
645 #ifdef __FEATURE_KEEP_CONNECTION__
646 /* Since mail_open failed Reset the global stream pointer as it is a dangling pointer now */
647 #endif /* __FEATURE_KEEP_CONNECTION__ */
650 *result_stream = mail_stream;
653 /* open smtp server */
654 SENDSTREAM *send_stream = NULL;
655 char *host_list[2] = {NULL, NULL};
657 #ifdef __FEATURE_KEEP_CONNECTION__
658 if (reusable_stream != NULL) {
660 /* Check whether connection is avaiable */
661 send_stream = reusable_stream;
663 send_ret = smtp_send(send_stream, "RSET", 0);
665 if (send_ret != SMTP_RESPONSE_OK) {
666 EM_DEBUG_EXCEPTION("[SMTP] RSET --> [%s]", send_stream->reply);
673 if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, 0, &mbox_path, &error)) {
674 EM_DEBUG_EXCEPTION(" emcore_get_long_encoded_path failed - %d", error);
675 session->error = error;
679 EM_DEBUG_LOG("open SMTP connection to mbox_path [%s]", mbox_path);
681 try_auth_smtp = account->outgoing_server_need_authentication ? 1 : 0;
682 session->auth = account->outgoing_server_need_authentication ? 1 : 0;
684 host_list[0] = mbox_path;
686 if (!(send_stream = smtp_open(host_list, 1))) {
687 EM_DEBUG_EXCEPTION("smtp_open failed... : current outgoing_server_secure_connection[%d] session->error[%d] session->network[%d]",
688 account->outgoing_server_secure_connection, session->error, session->network);
689 if (session->network != EMAIL_ERROR_NONE)
690 session->error = session->network;
691 if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
692 session->error = EMAIL_ERROR_CONNECTION_FAILURE;
694 error = session->error;
698 *result_stream = send_stream;
705 #ifdef __FEATURE_KEEP_CONNECTION__
707 if(!connection_info) {
708 connection_info = em_malloc(sizeof(email_connection_info_t));
709 connection_info->account_id = account->account_id;
711 EM_DEBUG_EXCEPTION("em_malloc for connection_info failed.");
713 emcore_append_connection_info(connection_info);
716 if(connection_info) {
717 /* connection_info->account_id = account->account_id; */
718 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
719 connection_info->receiving_server_stream = *result_stream;
720 connection_info->receiving_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
723 connection_info->sending_server_stream = *result_stream;
724 connection_info->sending_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
730 EM_SAFE_FREE(mbox_path);
732 EM_SAFE_FREE(mailbox_name);
735 emstorage_free_mailbox(&mailbox, 1, &error);
738 if (err_code != NULL)
740 EM_PROFILE_END(emCoreMailboxOpen);
741 EM_DEBUG_FUNC_END("ret [%d]", ret);
745 #ifdef __FEATURE_KEEP_CONNECTION__
747 /*h.gahlaut@samsung.com : 20-oct-2010*/
748 /*Precaution : When Reuse Stream feature is enabled then stream should only be closed using emcore_close_mailbox from email-service code.
749 mail_close should not be used directly from anywhere in code.
750 emcore_close_mailbox uses mail_close inside it.
752 mail_close is only used in emcore_connect_to_remote_mailbox and emcore_reset_streams as an exception to above rule*/
754 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, char *mailbox, void **mail_stream, int *err_code)
756 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], mail_stream[%p], err_code[%p]", account_id, mailbox, mail_stream, err_code);
759 int error = EMAIL_ERROR_NONE;
760 email_account_t *ref_account = emcore_get_account_reference(account_id);
763 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
764 error = EMAIL_ERROR_INVALID_ACCOUNT;
768 ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, mailbox, mail_stream, &error);
773 EM_DEBUG_FUNC_END("ret [%d]", ret);
777 INTERNAL_FUNC void emcore_close_mailbox_receiving_stream()
779 EM_DEBUG_FUNC_BEGIN("recv_thread_run [%d]", recv_thread_run);
780 if (!recv_thread_run) {
781 ENTER_CRITICAL_SECTION(_close_stream_lock);
782 mail_close(g_receiving_thd_stream);
783 g_receiving_thd_stream = NULL;
784 prev_acc_id_recv_thd = 0;
785 LEAVE_CRITICAL_SECTION(_close_stream_lock);
790 INTERNAL_FUNC void emcore_close_mailbox_partial_body_stream()
792 EM_DEBUG_FUNC_BEGIN();
793 if (false == emcore_get_pbd_thd_state()) {
794 EM_DEBUG_LOG("emcore_get_pbd_thd_state returned false");
795 mail_close(g_partial_body_thd_stream);
796 g_partial_body_thd_stream = NULL;
797 prev_acc_id_pb_thd = 0;
802 /* h.gahlaut@samsung.com : 21-10-2010 -
803 emcore_reset_stream() function is used to reset globally stored partial body thread and receiving thread streams
804 on account deletion and pdp deactivation */
806 INTERNAL_FUNC void emcore_reset_streams()
808 EM_DEBUG_FUNC_BEGIN();
810 emcore_close_mailbox_receiving_stream();
811 emcore_close_mailbox_partial_body_stream();
817 #else /* __FEATURE_KEEP_CONNECTION__ */
819 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
821 EM_DEBUG_FUNC_BEGIN("account_id[%d], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account_id, input_mailbox_id, mail_stream, err_code);
824 int error = EMAIL_ERROR_NONE;
825 email_account_t *ref_account = emcore_get_account_reference(account_id);
828 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
829 error = EMAIL_ERROR_INVALID_ACCOUNT;
833 ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, input_mailbox_id, mail_stream, &error);
841 #endif /* __FEATURE_KEEP_CONNECTION__ */
843 INTERNAL_FUNC int emcore_close_mailbox(int account_id, void *mail_stream)
845 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_stream[%p]", account_id, mail_stream);
848 EM_DEBUG_EXCEPTION("Invalid parameter");
852 #ifdef __FEATURE_KEEP_CONNECTION__
853 thread_t thread_id = THREAD_SELF();
855 if (thread_id == (thread_t)emcore_get_receiving_thd_id()) {
856 /* Receiving thread - Dont' Free Reuse feature enabled */
858 else if (thread_id == (thread_t)emcore_get_partial_body_thd_id()) {
859 /* Partial Body Download thread - Dont' Free Reuse feature enabled */
862 /* Some other thread so free stream */
863 if (g_receiving_thd_stream != mail_stream && g_partial_body_thd_stream != mail_stream)
864 mail_close((MAILSTREAM *)mail_stream);
867 mail_close((MAILSTREAM *)mail_stream);
868 #endif /* __FEATURE_KEEP_CONNECTION__ */
873 INTERNAL_FUNC void emcore_free_mailbox_list(email_mailbox_t **mailbox_list, int count)
875 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
877 if (count <= 0 || !mailbox_list || !*mailbox_list) {
878 EM_DEBUG_EXCEPTION("INVALID_PARAM: mailbox_list[%p], count[%d]", mailbox_list, count);
882 email_mailbox_t *p = *mailbox_list;
885 for (i = 0; i < count; i++)
886 emcore_free_mailbox(p+i);
889 *mailbox_list = NULL;
895 INTERNAL_FUNC void emcore_free_mailbox(email_mailbox_t *mailbox)
897 EM_DEBUG_FUNC_BEGIN();
900 EM_DEBUG_EXCEPTION("INVALID_PARAM");
904 EM_SAFE_FREE(mailbox->mailbox_name);
905 EM_SAFE_FREE(mailbox->alias);
911 INTERNAL_FUNC int emcore_free_internal_mailbox(email_internal_mailbox_t **mailbox_list, int count, int *err_code)
913 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
915 /* default variable */
917 int err = EMAIL_ERROR_NONE;
920 if (!mailbox_list || !*mailbox_list) {
921 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
923 err = EMAIL_ERROR_INVALID_PARAM;
927 email_internal_mailbox_t *p = *mailbox_list;
930 /* EM_DEBUG_LOG("before loop"); */
931 for (i = 0; i < count; i++) {
932 EM_SAFE_FREE(p[i].mailbox_name);
933 EM_SAFE_FREE(p[i].alias);
935 /* EM_DEBUG_LOG("p [%p]", p); */
937 *mailbox_list = NULL;
949 INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_list)
951 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p]", mailbox_list);
954 int bIsNotUserMailbox = false;
955 email_mailbox_type_item_t *pMailboxType1 = NULL ;
957 for (i = 0 ; i < MAX_MAILBOX_TYPE ; i++) {
958 pMailboxType1 = g_mailbox_type + i;
960 if (pMailboxType1->mailbox_name) {
961 if (0 == strcmp(pMailboxType1->mailbox_name, mailbox_list->mailbox_name)) {
962 mailbox_list->mailbox_type = pMailboxType1->mailbox_type;
963 EM_DEBUG_LOG("mailbox_list->mailbox_type[%d]", mailbox_list->mailbox_type);
964 bIsNotUserMailbox = true;
970 if (false == bIsNotUserMailbox)
971 mailbox_list->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
976 INTERNAL_FUNC int emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id , int *err_code)
978 EM_DEBUG_FUNC_BEGIN();
981 int err = EMAIL_ERROR_NONE;
983 email_event_t event_data;
985 if (!mailbox || mailbox->account_id <= 0) {
986 EM_DEBUG_LOG(" mailbox[%p]", mailbox);
988 err = EMAIL_ERROR_INVALID_PARAM;
991 memset(&event_data, 0x00, sizeof(email_event_t));
993 event_data.type = EMAIL_EVENT_SEND_MAIL;
994 event_data.account_id = mailbox->account_id;
995 event_data.event_param_data_4 = mail_id;
996 event_data.event_param_data_1 = NULL;
997 event_data.event_param_data_5 = mailbox->mailbox_id;
999 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1000 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1003 emcore_add_transaction_info(mail_id , handle , &err);
1013 INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inserted, int *err_code)
1015 EM_DEBUG_FUNC_BEGIN();
1016 int activity_count = 0;
1018 int error = EMAIL_ERROR_NONE;
1020 if (false == emstorage_get_pbd_activity_count(&activity_count, false, &error)) {
1021 EM_DEBUG_LOG("emstorage_get_pbd_activity_count failed [%d]", error);
1025 if (activity_count > 0) {
1027 email_event_partial_body_thd pbd_event;
1029 /* Carefully initialise the event */
1030 memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
1032 pbd_event.event_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1033 pbd_event.activity_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1035 if (false == emcore_insert_partial_body_thread_event(&pbd_event, &error)) {
1036 EM_DEBUG_LOG(" emcore_insert_partial_body_thread_event failed [%d]", error);
1040 /*Not checking for NULL here because is_event_inserted is never NULL. */
1041 *is_event_inserted = true;
1046 *is_event_inserted = false;
1053 if (NULL != err_code) {
1060 INTERNAL_FUNC int emcore_get_mailbox_by_type(int account_id, email_mailbox_type_e mailbox_type, email_mailbox_t *result_mailbox, int *err_code)
1062 EM_DEBUG_FUNC_BEGIN("account_id [%d], result_mailbox [%p], err_code [%p]", account_id, result_mailbox, err_code);
1063 int ret = false, err = EMAIL_ERROR_NONE;
1064 emstorage_mailbox_tbl_t *mail_box_tbl_spam = NULL;
1066 if (result_mailbox == NULL) {
1067 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1068 err = EMAIL_ERROR_INVALID_PARAM;
1072 if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &mail_box_tbl_spam, false, &err)) {
1074 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed - %d", err);
1077 if (mail_box_tbl_spam) {
1078 result_mailbox->mailbox_type = mail_box_tbl_spam->mailbox_type;
1079 result_mailbox->mailbox_name = EM_SAFE_STRDUP(mail_box_tbl_spam->mailbox_name);
1080 result_mailbox->account_id = mail_box_tbl_spam->account_id;
1081 result_mailbox->mail_slot_size = mail_box_tbl_spam->mail_slot_size;
1082 if (!emstorage_free_mailbox(&mail_box_tbl_spam, 1, &err))
1083 EM_DEBUG_EXCEPTION(" emstorage_free_mailbox Failed [%d]", err);
1091 EM_DEBUG_FUNC_END();
1095 #ifdef __FEATURE_LOCAL_ACTIVITY__
1096 INTERNAL_FUNC int emcore_local_activity_sync(int account_id, int *err_code)
1098 EM_DEBUG_FUNC_BEGIN();
1100 EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1102 int *activity_id_list = NULL;
1103 int activity_count = 0;
1107 email_event_t event_data;
1110 memset(&event_data, 0x00, sizeof(email_event_t));
1113 EM_IF_NULL_RETURN_VALUE(err_code, false);
1115 if (account_id <= 0) {
1116 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1119 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1121 if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1122 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 4 ");
1123 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list failed [ %d] ", err);
1126 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 5 ");
1128 if (activity_count > 0) {
1129 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1130 event_data.account_id = account_id;
1131 if (!emcore_insert_event(&event_data, &handle, &err)) {
1132 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1141 if (activity_id_list)
1142 emstorage_free_activity_id_list(activity_id_list, &err);
1144 if (err_code != NULL)
1151 INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
1153 EM_DEBUG_FUNC_BEGIN();
1155 EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1157 emstorage_activity_tbl_t *local_activity = NULL;
1158 int *activity_id_list = NULL;
1159 int activity_count = 0;
1163 email_event_t event_data;
1165 memset(&event_data, 0x00, sizeof(email_event_t));
1167 EM_IF_NULL_RETURN_VALUE(err_code, false);
1169 if (account_id <= 0) {
1170 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1173 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1175 if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1176 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list [ %d] ", err);
1181 if (activity_count > 0) {
1182 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1183 event_data.account_id = account_id;
1184 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1185 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1196 emstorage_free_local_activity(&local_activity, activity_count, NULL);
1198 if (activity_id_list)
1199 emstorage_free_activity_id_list(activity_id_list, &err);
1201 if (err_code != NULL)