4 * Copyright (c) 2012 - 2013 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-convert.h"
38 #include "email-debug-log.h"
39 #include "email-core-global.h"
40 #include "email-core-utils.h"
41 #include "email-core-mailbox.h"
42 #include "email-core-event.h"
43 #include "email-network.h"
44 #include "email-core-mail.h"
45 #include "email-core-imap-mailbox.h"
46 #include "email-storage.h"
47 #include "email-core-account.h"
49 #ifdef __FEATURE_KEEP_CONNECTION__
50 static void *g_receiving_thd_stream = NULL; /* Stores the recv thd stream for next time reuse */
51 static int prev_acc_id_recv_thd = 0; /* Stores the account id for which recv thd stream is open */
52 extern int recv_thread_run;
54 static void *g_partial_body_thd_stream = NULL; /* Stores the pb thd stream for next time reuse */
55 static int prev_acc_id_pb_thd = 0; /* Stores the account id for which pb thd stream is open */
57 __thread email_connection_info_t *g_connection_info_list = NULL;
59 static pthread_mutex_t _close_stream_lock = PTHREAD_MUTEX_INITIALIZER; /* Mutex to protect closing stream */
60 #endif /* __FEATURE_KEEP_CONNECTION__ */
63 /* Binding IMAP mailbox with its function */
64 static email_mailbox_type_item_t g_mailbox_type[MAX_MAILBOX_TYPE] = {
65 {EMAIL_MAILBOX_TYPE_INBOX, "INBOX" },
67 {EMAIL_MAILBOX_TYPE_INBOX, "Inbox" },
68 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent Messages"} ,
69 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTOLpUx3zVaA-"} ,
70 {EMAIL_MAILBOX_TYPE_DRAFT, "Drafts"} ,
71 {EMAIL_MAILBOX_TYPE_TRASH, "Deleted Messages" } ,
73 {EMAIL_MAILBOX_TYPE_SENTBOX, "Sent"} ,
74 {EMAIL_MAILBOX_TYPE_SPAMBOX, "Spam" },
75 {EMAIL_MAILBOX_TYPE_DRAFT, "Drafts"} ,
76 {EMAIL_MAILBOX_TYPE_TRASH, "Trash"},
78 {EMAIL_MAILBOX_TYPE_SPAMBOX, "&wqTTONO4ycDVaA-"},
80 {EMAIL_MAILBOX_TYPE_SENTBOX, "mail/sent-mail"},
81 {EMAIL_MAILBOX_TYPE_SPAMBOX, "mail/spam-mail" },
82 {EMAIL_MAILBOX_TYPE_DRAFT, "mail/saved-drafts"} ,
83 {EMAIL_MAILBOX_TYPE_TRASH, "mail/mail-trash"},
86 #ifdef __FEATURE_KEEP_CONNECTION__
87 email_connection_info_t* emcore_get_connection_info_by_account_id(int account_id)
89 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
90 email_connection_info_t *connection_info = g_connection_info_list;
92 while(connection_info) {
93 if(connection_info->account_id == account_id)
95 connection_info = connection_info->next;
98 EM_DEBUG_FUNC_END("connection_info [%p]", connection_info);
99 return connection_info;
102 int emcore_append_connection_info(email_connection_info_t *new_connection_info)
104 EM_DEBUG_FUNC_BEGIN("new_connection_info [%p]", new_connection_info);
105 email_connection_info_t *connection_info = g_connection_info_list;
107 if(!new_connection_info) {
108 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
109 return EMAIL_ERROR_INVALID_PARAM;
112 if(emcore_get_connection_info_by_account_id(new_connection_info->account_id)) {
113 EM_DEBUG_EXCEPTION("EMAIL_ERROR_ALREADY_EXISTS");
114 return EMAIL_ERROR_ALREADY_EXISTS;
117 if(connection_info) {
118 while(connection_info) {
119 if(connection_info->next == NULL) {
120 connection_info->next = new_connection_info;
121 new_connection_info->next = NULL;
123 connection_info = connection_info->next;
127 new_connection_info->next = NULL;
128 g_connection_info_list = new_connection_info;
131 EM_DEBUG_FUNC_END("EMAIL_ERROR_NONE");
132 return EMAIL_ERROR_NONE;
135 INTERNAL_FUNC int emcore_remove_connection_info(int account_id)
137 EM_DEBUG_FUNC_BEGIN("account_id [%d]", account_id);
138 email_connection_info_t *connection_info = g_connection_info_list, *prev_connection_info = NULL;
140 while(connection_info) {
141 if(connection_info->account_id == account_id) {
142 if(prev_connection_info) {
143 prev_connection_info->next = connection_info->next;
146 g_connection_info_list = connection_info->next;
148 EM_SAFE_FREE(connection_info);
151 prev_connection_info = connection_info;
152 connection_info = connection_info->next;
155 EM_DEBUG_FUNC_END("");
156 return EMAIL_ERROR_NONE;
159 #endif /* __FEATURE_KEEP_CONNECTION__ */
163 * get local mailbox list
165 INTERNAL_FUNC int emcore_get_mailbox_list(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
167 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
169 if (account_id <= 0 || !mailbox_list || !p_count) {
170 EM_DEBUG_EXCEPTION("PARAM Failed account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
171 if (err_code != NULL)
172 *err_code = EMAIL_ERROR_INVALID_PARAM;
177 int error = EMAIL_ERROR_NONE;
178 emstorage_mailbox_tbl_t *local_mailbox_list = NULL;
179 email_account_t *ref_account = NULL;
182 /* get mailbox list from mailbox table */
184 if (!(ref_account = emcore_get_account_reference(account_id))) {
185 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed - %d", account_id);
186 error = EMAIL_ERROR_INVALID_ACCOUNT;
190 if (!emstorage_get_mailbox_list(ref_account->account_id, EMAIL_MAILBOX_ALL, EMAIL_MAILBOX_SORT_BY_NAME_ASC, &count, &local_mailbox_list, true, &error)) {
191 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox failed - %d", error);
197 if (!(*mailbox_list = em_malloc(sizeof(email_mailbox_t) * count))) {
198 EM_DEBUG_EXCEPTION(" mailloc failed...");
199 error = EMAIL_ERROR_OUT_OF_MEMORY;
203 memset(*mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
205 for (i = 0; i < count; i++) {
206 em_convert_mailbox_tbl_to_mailbox(local_mailbox_list + i, (*mailbox_list) + i);
208 (*mailbox_list)[i].mailbox_id = local_mailbox_list[i].mailbox_id;
209 (*mailbox_list)[i].account_id = account_id;
210 (*mailbox_list)[i].mailbox_name = local_mailbox_list[i].mailbox_name; local_mailbox_list[i].mailbox_name = NULL;
211 (*mailbox_list)[i].alias = local_mailbox_list[i].alias; local_mailbox_list[i].alias = NULL;
212 (*mailbox_list)[i].local = local_mailbox_list[i].local_yn;
213 (*mailbox_list)[i].mailbox_type = local_mailbox_list[i].mailbox_type;
214 (*mailbox_list)[i].unread_count = local_mailbox_list[i].unread_count;
215 (*mailbox_list)[i].total_mail_count_on_local = local_mailbox_list[i].total_mail_count_on_local;
216 (*mailbox_list)[i].total_mail_count_on_server = local_mailbox_list[i].total_mail_count_on_server;
217 (*mailbox_list)[i].mail_slot_size = local_mailbox_list[i].mail_slot_size;
218 (*mailbox_list)[i].no_select = local_mailbox_list[i].no_select;
219 (*mailbox_list)[i].last_sync_time = local_mailbox_list[i].last_sync_time;
231 if (local_mailbox_list != NULL)
232 emstorage_free_mailbox(&local_mailbox_list, count, NULL);
235 emcore_free_account(ref_account);
236 EM_SAFE_FREE(ref_account);
239 if (err_code != NULL)
247 * get imap sync mailbox list
249 int emcore_get_mailbox_list_to_be_sync(int account_id, email_mailbox_t **mailbox_list, int *p_count, int *err_code)
251 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox_list[%p], p_count[%p], err_code[%p]", account_id, mailbox_list, p_count, err_code);
253 if (account_id <= 0 || !mailbox_list || !p_count) {
254 EM_DEBUG_EXCEPTION(" account_id[%d], mailbox_list[%p], p_count[%p]", account_id, mailbox_list, p_count);
255 if (err_code != NULL)
256 *err_code = EMAIL_ERROR_INVALID_PARAM;
261 int error = EMAIL_ERROR_NONE;
262 email_mailbox_t *tmp_mailbox_list = NULL;
263 emstorage_mailbox_tbl_t *mailbox_tbl_list = NULL;
264 email_account_t *ref_account = NULL;
267 /* get mailbox list from mailbox table */
268 if (!(ref_account = emcore_get_account_reference(account_id))) {
269 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
270 error = EMAIL_ERROR_INVALID_ACCOUNT;
274 if (!emstorage_get_mailbox_list(ref_account->account_id, 0, EMAIL_MAILBOX_SORT_BY_TYPE_ASC, &count, &mailbox_tbl_list, true, &error)) {
275 EM_DEBUG_EXCEPTION("emstorage_get_mailbox failed - %d", error);
281 if (!(tmp_mailbox_list = em_malloc(sizeof(email_mailbox_t) * count))) {
282 EM_DEBUG_EXCEPTION("malloc failed...");
283 error = EMAIL_ERROR_OUT_OF_MEMORY;
287 memset(tmp_mailbox_list, 0x00, (sizeof(email_mailbox_t) * count));
289 for (i = 0; i < count; i++) {
290 em_convert_mailbox_tbl_to_mailbox(mailbox_tbl_list + i, tmp_mailbox_list + i);
292 tmp_mailbox_list[i].mailbox_id = mailbox_tbl_list[i].mailbox_id;
293 tmp_mailbox_list[i].account_id = account_id;
294 tmp_mailbox_list[i].mailbox_name = mailbox_tbl_list[i].mailbox_name; mailbox_tbl_list[i].mailbox_name = NULL;
295 tmp_mailbox_list[i].mailbox_type = mailbox_tbl_list[i].mailbox_type;
296 tmp_mailbox_list[i].alias = mailbox_tbl_list[i].alias; mailbox_tbl_list[i].alias = NULL;
297 tmp_mailbox_list[i].local = mailbox_tbl_list[i].local_yn;
298 tmp_mailbox_list[i].unread_count = mailbox_tbl_list[i].unread_count;
299 tmp_mailbox_list[i].total_mail_count_on_local = mailbox_tbl_list[i].total_mail_count_on_local;
300 tmp_mailbox_list[i].total_mail_count_on_server = mailbox_tbl_list[i].total_mail_count_on_server;
301 tmp_mailbox_list[i].mail_slot_size = mailbox_tbl_list[i].mail_slot_size;
306 tmp_mailbox_list = NULL;
312 *mailbox_list = tmp_mailbox_list;
315 emcore_free_account(ref_account);
316 EM_SAFE_FREE(ref_account);
319 if (mailbox_tbl_list != NULL)
320 emstorage_free_mailbox(&mailbox_tbl_list, count, NULL);
322 if (err_code != NULL)
324 EM_DEBUG_FUNC_END("error [%d]", error);
328 INTERNAL_FUNC int emcore_get_mail_count(email_mailbox_t *mailbox, int *total, int *unseen, int *err_code)
330 EM_DEBUG_FUNC_BEGIN("mailbox[%p], total[%p], unseen[%p], err_code[%p]", mailbox, total, unseen, err_code);
333 int err = EMAIL_ERROR_NONE;
336 EM_DEBUG_EXCEPTION(" mailbox[%p], total[%p], unseen[%p]", mailbox, total, unseen);
337 err = EMAIL_ERROR_INVALID_PARAM;
341 if (!emstorage_get_mail_count(mailbox->account_id, mailbox->mailbox_name, total, unseen, true, &err)) {
342 EM_DEBUG_EXCEPTION(" emstorage_get_mail_count failed - %d", err);
351 if (err_code != NULL)
357 INTERNAL_FUNC int emcore_create_mailbox(email_mailbox_t *new_mailbox, int on_server, int *err_code)
359 EM_DEBUG_FUNC_BEGIN("new_mailbox[%p], err_code[%p]", new_mailbox, err_code);
361 int err = EMAIL_ERROR_NONE;
362 emstorage_mailbox_tbl_t local_mailbox;
364 if (new_mailbox == NULL || new_mailbox->mailbox_name == NULL) {
365 err = EMAIL_ERROR_INVALID_PARAM;
370 /* Create a mailbox from Sever */
371 if (!emcore_create_imap_mailbox(new_mailbox, &err)) {
372 EM_DEBUG_EXCEPTION(">>>>> mailbox Creation in Server FAILED >>> ");
376 EM_DEBUG_LOG(">>>>> mailbox Creation in Server SUCCESS >>> ");
379 memset(&local_mailbox, 0x00, sizeof(emstorage_mailbox_tbl_t));
380 EM_DEBUG_LOG("box name[%s] local yn[%d] mailbox_type[%d]", new_mailbox->mailbox_name, local_mailbox.local_yn, new_mailbox->mailbox_type);
382 /* add local mailbox into local mailbox table */
383 local_mailbox.mailbox_id = new_mailbox->mailbox_id;
384 local_mailbox.account_id = new_mailbox->account_id;
385 local_mailbox.local_yn = new_mailbox->local;
386 local_mailbox.mailbox_name = new_mailbox->mailbox_name;
387 local_mailbox.alias = new_mailbox->alias;
388 local_mailbox.mailbox_type = new_mailbox->mailbox_type;
389 local_mailbox.unread_count = 0;
390 local_mailbox.total_mail_count_on_local = 0;
391 local_mailbox.total_mail_count_on_server = 0;
392 emcore_get_default_mail_slot_count(local_mailbox.account_id, &local_mailbox.mail_slot_size);
394 if (strncmp(new_mailbox->mailbox_name, EMAIL_INBOX_NAME, EM_SAFE_STRLEN(EMAIL_INBOX_NAME)) == 0 ||
395 strncmp(new_mailbox->mailbox_name, EMAIL_DRAFTBOX_NAME, EM_SAFE_STRLEN(EMAIL_DRAFTBOX_NAME)) == 0 ||
396 strncmp(new_mailbox->mailbox_name, EMAIL_OUTBOX_NAME, EM_SAFE_STRLEN(EMAIL_OUTBOX_NAME)) == 0 ||
397 strncmp(new_mailbox->mailbox_name, EMAIL_SENTBOX_NAME, EM_SAFE_STRLEN(EMAIL_SENTBOX_NAME)) == 0)
398 local_mailbox.modifiable_yn = 0; /* can be deleted/modified */
400 local_mailbox.modifiable_yn = 1;
403 if (!emstorage_add_mailbox(&local_mailbox, true, &err)) {
404 EM_DEBUG_EXCEPTION("emstorage_add_mailbox failed [%d]", err);
408 new_mailbox->mailbox_id = local_mailbox.mailbox_id;
418 INTERNAL_FUNC int emcore_delete_mailbox(int input_mailbox_id, int on_server, int *err_code)
420 EM_DEBUG_FUNC_BEGIN("input_mailbox_id[%d], err_code[%p]", input_mailbox_id, err_code);
423 int err = EMAIL_ERROR_NONE;
424 emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
426 if (input_mailbox_id == 0) {
427 EM_DEBUG_EXCEPTION(" input_mailbox_id == 0");
428 err = EMAIL_ERROR_INVALID_PARAM;
432 if ((err = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox_tbl)) != EMAIL_ERROR_NONE || !mailbox_tbl) {
433 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed. [%d]", err);
438 EM_DEBUG_LOG("Delete the mailbox in Sever >>> ");
439 if (!emcore_delete_imap_mailbox(input_mailbox_id, &err))
440 EM_DEBUG_EXCEPTION("Delete the mailbox in server : failed [%d]", err);
442 EM_DEBUG_LOG("Delete the mailbox in server : success");
445 if (!emcore_delete_all_mails_of_mailbox(mailbox_tbl->account_id, input_mailbox_id, false, &err)) {
446 EM_DEBUG_EXCEPTION("emcore_delete_all_mails_of_mailbox failed [%d]", err);
450 if (!emstorage_delete_mailbox(mailbox_tbl->account_id, -1, input_mailbox_id, true, &err)) {
451 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
460 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
462 if (err_code != NULL)
464 EM_DEBUG_FUNC_END("err[%d]", err);
469 INTERNAL_FUNC int emcore_delete_mailbox_ex(int input_account_id, int *input_mailbox_id_array, int input_mailbox_id_count, int input_on_server)
471 EM_DEBUG_FUNC_BEGIN("input_account_id [%d] input_mailbox_id_array[%p] input_mailbox_id_count[%d] input_on_server[%d]", input_mailbox_id_array, input_mailbox_id_array, input_mailbox_id_count, input_on_server);
472 int err = EMAIL_ERROR_NONE;
475 if(input_account_id == 0 || input_mailbox_id_count <= 0 || input_mailbox_id_array == NULL) {
476 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
477 err = EMAIL_ERROR_INVALID_PARAM;
481 if((err = emstorage_set_field_of_mailbox_with_integer_value(input_account_id, input_mailbox_id_array, input_mailbox_id_count, "deleted_flag", 1, true)) != EMAIL_ERROR_NONE) {
482 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mailbox_with_integer_value failed[%d]", err);
486 for(i = 0; i < input_mailbox_id_count; i++) {
487 if(!emcore_delete_mailbox(input_mailbox_id_array[i] , input_on_server, &err)) {
488 EM_DEBUG_EXCEPTION("emcore_delete_mailbox failed [%d]", err);
494 EM_DEBUG_FUNC_END("err[%d]", err);
498 INTERNAL_FUNC int emcore_delete_mailbox_all(email_mailbox_t *mailbox, int *err_code)
500 EM_DEBUG_FUNC_BEGIN(" mailbox[%p], err_code[%p]", mailbox, err_code);
503 int err = EMAIL_ERROR_NONE;
505 if (mailbox == NULL) {
506 EM_DEBUG_EXCEPTION(" mailbox[%p]", mailbox);
507 err = EMAIL_ERROR_INVALID_PARAM;
511 if (!emcore_delete_all_mails_of_mailbox(mailbox->account_id, mailbox->mailbox_id, 0, /*NULL, */ &err)) {
512 EM_DEBUG_EXCEPTION(" emcore_delete_all_mails_of_mailbox failed - %d", err);
517 if (!emstorage_delete_mailbox(mailbox->account_id, -1, mailbox->mailbox_id, true, &err)) {
518 EM_DEBUG_EXCEPTION(" emstorage_delete_mailbox failed - %d", err);
527 if (err_code != NULL)
529 EM_DEBUG_FUNC_END("err[%d]", err);
533 INTERNAL_FUNC int emcore_update_mailbox(email_mailbox_t *old_mailbox, email_mailbox_t *new_mailbox, int *err_code)
535 EM_DEBUG_FUNC_BEGIN("old_mailbox[%p], new_mailbox[%p], err_code[%p]", old_mailbox, new_mailbox, err_code);
538 int err = EMAIL_ERROR_NONE;
540 if (old_mailbox == NULL || new_mailbox == NULL) {
541 EM_DEBUG_EXCEPTION("old_mailbox[%p], new_mailbox[%p]", old_mailbox, new_mailbox);
543 err = EMAIL_ERROR_INVALID_PARAM;
547 emstorage_mailbox_tbl_t new_mailbox_tbl;
548 memset(&new_mailbox_tbl, 0x00, sizeof(emstorage_mailbox_tbl_t));
550 /* Support only updating mailbox_type */
551 new_mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
553 if (old_mailbox->mailbox_type != new_mailbox_tbl.mailbox_type) {
554 if (!emstorage_update_mailbox_type(old_mailbox->account_id, -1, old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_type, true, &err)) {
555 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
560 new_mailbox_tbl.mailbox_id = old_mailbox->mailbox_id;
561 new_mailbox_tbl.account_id = old_mailbox->account_id;
562 new_mailbox_tbl.mailbox_name = new_mailbox->mailbox_name;
563 new_mailbox_tbl.mailbox_type = new_mailbox->mailbox_type;
564 new_mailbox_tbl.alias = new_mailbox->alias;
565 new_mailbox_tbl.mail_slot_size = new_mailbox->mail_slot_size;
566 new_mailbox_tbl.total_mail_count_on_server = new_mailbox->total_mail_count_on_server;
568 if (!emstorage_update_mailbox(old_mailbox->account_id, -1, old_mailbox->mailbox_id, &new_mailbox_tbl, true, &err)) {
569 EM_DEBUG_EXCEPTION("emstorage_update_mailbox failed - %d", err);
574 if (EM_SAFE_STRCMP(old_mailbox->mailbox_name, new_mailbox_tbl.mailbox_name) != 0) {
575 if ( (err = emstorage_rename_mailbox(old_mailbox->mailbox_id, new_mailbox_tbl.mailbox_name, new_mailbox_tbl.alias, true)) != EMAIL_ERROR_NONE) {
576 EM_DEBUG_EXCEPTION("emstorage_rename_mailbox failed [%d]", err);
591 extern int try_auth_smtp;
593 #ifdef __FEATURE_KEEP_CONNECTION__
594 extern long smtp_send(SENDSTREAM *stream, char *command, char *args);
595 #endif /* __FEATURE_KEEP_CONNECTION__ */
597 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)
599 EM_PROFILE_BEGIN(emCoreMailboxOpen);
600 EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_id[%d], mail_stream[%p], err_code[%p]", account, input_mailbox_id, result_stream, err_code);
603 int error = EMAIL_ERROR_NONE;
604 email_session_t *session = NULL;
605 char *mbox_path = NULL;
606 void *reusable_stream = NULL;
607 int is_connection_for = _SERVICE_THREAD_TYPE_NONE;
608 emstorage_mailbox_tbl_t* mailbox = NULL;
609 char *mailbox_name = NULL;
611 if (account == NULL) {
612 EM_DEBUG_EXCEPTION("Invalid Parameter.");
613 error = EMAIL_ERROR_INVALID_PARAM;
617 if (!emcore_get_current_session(&session)) {
618 EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
619 error = EMAIL_ERROR_SESSION_NOT_FOUND;
623 if (input_mailbox_id == 0 || input_mailbox_id != EMAIL_CONNECT_FOR_SENDING)
624 is_connection_for = _SERVICE_THREAD_TYPE_RECEIVING;
626 is_connection_for = _SERVICE_THREAD_TYPE_SENDING;
628 #ifdef __FEATURE_KEEP_CONNECTION__
629 email_connection_info_t *connection_info = emcore_get_connection_info_by_account_id(account->account_id);
631 if(connection_info) {
632 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
633 if(connection_info->receiving_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
634 reusable_stream = connection_info->receiving_server_stream;
637 if(connection_info->sending_server_stream_status == EMAIL_STREAM_STATUS_CONNECTED)
638 reusable_stream = connection_info->sending_server_stream;
642 if (reusable_stream != NULL)
643 EM_DEBUG_LOG("Stream reuse desired");
645 reusable_stream = *result_stream;
648 session->error = EMAIL_ERROR_NONE;
649 emcore_set_network_error(EMAIL_ERROR_NONE); /* set current network error as EMAIL_ERROR_NONE before network operation */
651 if (input_mailbox_id == EMAIL_CONNECT_FOR_SENDING) {
652 mailbox_name = EM_SAFE_STRDUP(ENCODED_PATH_SMTP);
654 else if (input_mailbox_id == 0) {
657 if ( (error = emstorage_get_mailbox_by_id(input_mailbox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
658 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", error);
661 mailbox_name = EM_SAFE_STRDUP(mailbox->mailbox_name);
664 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
665 /* open pop3/imap server */
666 MAILSTREAM *mail_stream = NULL;
668 if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, '/', &mbox_path, &error)) {
669 EM_DEBUG_EXCEPTION("emcore_get_long_encoded_path failed - %d", error);
670 session->error = error;
674 EM_DEBUG_LOG("open mail connection to mbox_path [%s]", mbox_path);
676 try_auth = 0; /* ref_account->receiving_auth ? 1 : 0 */
677 session->auth = 0; /* ref_account->receiving_auth ? 1 : 0 */
679 if (!(mail_stream = mail_open(reusable_stream, mbox_path, IMAP_2004_LOG))) {
680 EM_DEBUG_EXCEPTION("mail_open failed. session->error[%d], session->network[%d]", session->error, session->network);
682 if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
683 session->error = EMAIL_ERROR_CONNECTION_FAILURE;
685 error = session->error;
687 #ifdef __FEATURE_KEEP_CONNECTION__
688 /* Since mail_open failed Reset the global stream pointer as it is a dangling pointer now */
689 #endif /* __FEATURE_KEEP_CONNECTION__ */
692 *result_stream = mail_stream;
695 /* open smtp server */
696 SENDSTREAM *send_stream = NULL;
697 char *host_list[2] = {NULL, NULL};
699 #ifdef __FEATURE_KEEP_CONNECTION__
700 if (reusable_stream != NULL) {
702 /* Check whether connection is avaiable */
703 send_stream = reusable_stream;
705 send_ret = smtp_send(send_stream, "RSET", 0);
707 if (send_ret != SMTP_RESPONSE_OK) {
708 EM_DEBUG_EXCEPTION("[SMTP] RSET --> [%s]", send_stream->reply);
715 if (!emcore_get_long_encoded_path_with_account_info(account, mailbox_name, 0, &mbox_path, &error)) {
716 EM_DEBUG_EXCEPTION(" emcore_get_long_encoded_path failed - %d", error);
717 session->error = error;
721 EM_DEBUG_LOG("open SMTP connection to mbox_path [%s]", mbox_path);
723 try_auth_smtp = account->outgoing_server_need_authentication ? 1 : 0;
724 session->auth = account->outgoing_server_need_authentication ? 1 : 0;
726 host_list[0] = mbox_path;
728 if (!(send_stream = smtp_open(host_list, 1))) {
729 EM_DEBUG_EXCEPTION("smtp_open failed... : current outgoing_server_secure_connection[%d] session->error[%d] session->network[%d]",
730 account->outgoing_server_secure_connection, session->error, session->network);
731 if (session->network != EMAIL_ERROR_NONE)
732 session->error = session->network;
733 if ((session->error == EMAIL_ERROR_UNKNOWN) || (session->error == EMAIL_ERROR_NONE))
734 session->error = EMAIL_ERROR_CONNECTION_FAILURE;
736 error = session->error;
740 *result_stream = send_stream;
747 #ifdef __FEATURE_KEEP_CONNECTION__
749 if(!connection_info) {
750 connection_info = em_malloc(sizeof(email_connection_info_t));
751 connection_info->account_id = account->account_id;
753 EM_DEBUG_EXCEPTION("em_malloc for connection_info failed.");
755 emcore_append_connection_info(connection_info);
758 if(connection_info) {
759 /* connection_info->account_id = account->account_id; */
760 if (is_connection_for == _SERVICE_THREAD_TYPE_RECEIVING) {
761 connection_info->receiving_server_stream = *result_stream;
762 connection_info->receiving_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
765 connection_info->sending_server_stream = *result_stream;
766 connection_info->sending_server_stream_status = EMAIL_STREAM_STATUS_CONNECTED;
772 EM_SAFE_FREE(mbox_path);
774 EM_SAFE_FREE(mailbox_name);
777 emstorage_free_mailbox(&mailbox, 1, NULL);
780 if (err_code != NULL)
782 EM_PROFILE_END(emCoreMailboxOpen);
783 EM_DEBUG_FUNC_END("ret [%d]", ret);
787 #ifdef __FEATURE_KEEP_CONNECTION__
789 /*h.gahlaut@samsung.com : 20-oct-2010*/
790 /*Precaution : When Reuse Stream feature is enabled then stream should only be closed using emcore_close_mailbox from email-service code.
791 mail_close should not be used directly from anywhere in code.
792 emcore_close_mailbox uses mail_close inside it.
794 mail_close is only used in emcore_connect_to_remote_mailbox and emcore_reset_streams as an exception to above rule*/
796 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, char *mailbox, void **mail_stream, int *err_code)
798 EM_DEBUG_FUNC_BEGIN("account_id[%d], mailbox[%p], mail_stream[%p], err_code[%p]", account_id, mailbox, mail_stream, err_code);
801 int error = EMAIL_ERROR_NONE;
802 email_account_t *ref_account = NULL;
804 ref_account = emcore_get_account_reference(account_id);
807 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
808 error = EMAIL_ERROR_INVALID_ACCOUNT;
812 ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, mailbox, mail_stream, &error);
817 emcore_free_account(ref_account);
818 EM_SAFE_FREE(ref_account);
823 EM_DEBUG_FUNC_END("ret [%d]", ret);
827 INTERNAL_FUNC void emcore_close_mailbox_receiving_stream()
829 EM_DEBUG_FUNC_BEGIN("recv_thread_run [%d]", recv_thread_run);
830 if (!recv_thread_run) {
831 ENTER_CRITICAL_SECTION(_close_stream_lock);
832 mail_close(g_receiving_thd_stream);
833 g_receiving_thd_stream = NULL;
834 prev_acc_id_recv_thd = 0;
835 LEAVE_CRITICAL_SECTION(_close_stream_lock);
840 INTERNAL_FUNC void emcore_close_mailbox_partial_body_stream()
842 EM_DEBUG_FUNC_BEGIN();
843 if (false == emcore_get_pbd_thd_state()) {
844 EM_DEBUG_LOG("emcore_get_pbd_thd_state returned false");
845 mail_close(g_partial_body_thd_stream);
846 g_partial_body_thd_stream = NULL;
847 prev_acc_id_pb_thd = 0;
852 /* h.gahlaut@samsung.com : 21-10-2010 -
853 emcore_reset_stream() function is used to reset globally stored partial body thread and receiving thread streams
854 on account deletion and pdp deactivation */
856 INTERNAL_FUNC void emcore_reset_streams()
858 EM_DEBUG_FUNC_BEGIN();
860 emcore_close_mailbox_receiving_stream();
861 emcore_close_mailbox_partial_body_stream();
867 #else /* __FEATURE_KEEP_CONNECTION__ */
869 INTERNAL_FUNC int emcore_connect_to_remote_mailbox(int account_id, int input_mailbox_id, void **mail_stream, int *err_code)
871 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);
874 int error = EMAIL_ERROR_NONE;
875 email_session_t *session = NULL;
876 email_account_t *ref_account = NULL;
878 ref_account = emcore_get_account_reference(account_id);
881 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - account id[%d]", account_id);
882 error = EMAIL_ERROR_INVALID_ACCOUNT;
886 if (!emcore_check_thread_status()) {
887 error = EMAIL_ERROR_CANCELLED;
891 if (!emnetwork_check_network_status(&error)) {
892 EM_DEBUG_EXCEPTION("emnetwork_check_network_status failed [%d]", error);
896 if (!emcore_get_empty_session(&session)) {
897 EM_DEBUG_EXCEPTION("emcore_get_empty_session failed...");
898 error = EMAIL_ERROR_SESSION_NOT_FOUND;
902 ret = emcore_connect_to_remote_mailbox_with_account_info(ref_account, input_mailbox_id, mail_stream, &error);
907 emcore_free_account(ref_account);
908 EM_SAFE_FREE(ref_account);
911 emcore_clear_session(session);
918 #endif /* __FEATURE_KEEP_CONNECTION__ */
920 INTERNAL_FUNC int emcore_close_mailbox(int account_id, void *mail_stream)
922 EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_stream[%p]", account_id, mail_stream);
925 EM_DEBUG_EXCEPTION("Invalid parameter");
929 #ifdef __FEATURE_KEEP_CONNECTION__
930 thread_t thread_id = THREAD_SELF();
932 if (thread_id == (thread_t)emcore_get_receiving_thd_id()) {
933 /* Receiving thread - Dont' Free Reuse feature enabled */
935 else if (thread_id == (thread_t)emcore_get_partial_body_thd_id()) {
936 /* Partial Body Download thread - Dont' Free Reuse feature enabled */
939 /* Some other thread so free stream */
940 if (g_receiving_thd_stream != mail_stream && g_partial_body_thd_stream != mail_stream)
941 mail_close((MAILSTREAM *)mail_stream);
944 mail_close((MAILSTREAM *)mail_stream);
945 #endif /* __FEATURE_KEEP_CONNECTION__ */
950 INTERNAL_FUNC void emcore_free_mailbox_list(email_mailbox_t **mailbox_list, int count)
952 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d]", mailbox_list, count);
954 if (count <= 0 || !mailbox_list || !*mailbox_list) {
955 EM_DEBUG_EXCEPTION("INVALID_PARAM: mailbox_list[%p], count[%d]", mailbox_list, count);
959 email_mailbox_t *p = *mailbox_list;
962 for (i = 0; i < count; i++)
963 emcore_free_mailbox(p+i);
966 *mailbox_list = NULL;
972 INTERNAL_FUNC void emcore_free_mailbox(email_mailbox_t *mailbox)
974 EM_DEBUG_FUNC_BEGIN();
977 EM_DEBUG_EXCEPTION("INVALID_PARAM");
981 EM_SAFE_FREE(mailbox->mailbox_name);
982 EM_SAFE_FREE(mailbox->alias);
988 INTERNAL_FUNC int emcore_free_internal_mailbox(email_internal_mailbox_t **mailbox_list, int count, int *err_code)
990 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p], count[%d], err_code[%p]", mailbox_list, count, err_code);
992 /* default variable */
994 int err = EMAIL_ERROR_NONE;
997 if (!mailbox_list || !*mailbox_list) {
998 EM_DEBUG_EXCEPTION(" mailbox_list[%p], count[%d]", mailbox_list, count);
1000 err = EMAIL_ERROR_INVALID_PARAM;
1004 email_internal_mailbox_t *p = *mailbox_list;
1007 /* EM_DEBUG_LOG("before loop"); */
1008 for (i = 0; i < count; i++) {
1009 EM_SAFE_FREE(p[i].mailbox_name);
1010 EM_SAFE_FREE(p[i].alias);
1012 /* EM_DEBUG_LOG("p [%p]", p); */
1014 *mailbox_list = NULL;
1022 EM_DEBUG_FUNC_END();
1026 INTERNAL_FUNC void emcore_bind_mailbox_type(email_internal_mailbox_t *mailbox_list)
1028 EM_DEBUG_FUNC_BEGIN("mailbox_list[%p]", mailbox_list);
1031 int bIsNotUserMailbox = false;
1032 email_mailbox_type_item_t *pMailboxType1 = NULL ;
1034 for (i = 0 ; i < MAX_MAILBOX_TYPE ; i++) {
1035 pMailboxType1 = g_mailbox_type + i;
1036 if (0 == EM_SAFE_STRCMP(pMailboxType1->mailbox_name, mailbox_list->mailbox_name)) { /*prevent 24662*/
1037 mailbox_list->mailbox_type = pMailboxType1->mailbox_type;
1038 EM_DEBUG_LOG("mailbox_list->mailbox_type[%d]", mailbox_list->mailbox_type);
1039 bIsNotUserMailbox = true;
1044 if (false == bIsNotUserMailbox)
1045 mailbox_list->mailbox_type = EMAIL_MAILBOX_TYPE_USER_DEFINED;
1047 EM_DEBUG_FUNC_END();
1050 INTERNAL_FUNC int emcore_send_mail_event(email_mailbox_t *mailbox, int mail_id , int *err_code)
1052 EM_DEBUG_FUNC_BEGIN();
1055 int err = EMAIL_ERROR_NONE;
1057 email_event_t event_data;
1059 if (!mailbox || mailbox->account_id <= 0) {
1060 EM_DEBUG_LOG(" mailbox[%p]", mailbox);
1062 err = EMAIL_ERROR_INVALID_PARAM;
1065 memset(&event_data, 0x00, sizeof(email_event_t));
1067 event_data.type = EMAIL_EVENT_SEND_MAIL;
1068 event_data.account_id = mailbox->account_id;
1069 event_data.event_param_data_4 = mail_id;
1070 event_data.event_param_data_1 = NULL;
1071 event_data.event_param_data_5 = mailbox->mailbox_id;
1073 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1074 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1077 emcore_add_transaction_info(mail_id , handle , &err);
1087 INTERNAL_FUNC int emcore_partial_body_thd_local_activity_sync(int *is_event_inserted, int *err_code)
1089 EM_DEBUG_FUNC_BEGIN();
1090 int activity_count = 0;
1092 int error = EMAIL_ERROR_NONE;
1094 if (false == emstorage_get_pbd_activity_count(&activity_count, false, &error)) {
1095 EM_DEBUG_LOG("emstorage_get_pbd_activity_count failed [%d]", error);
1099 if (activity_count > 0) {
1101 email_event_partial_body_thd pbd_event;
1103 /* Carefully initialise the event */
1104 memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
1106 pbd_event.event_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1107 pbd_event.activity_type = EMAIL_EVENT_LOCAL_ACTIVITY_SYNC_BULK_PBD;
1109 if (false == emcore_insert_partial_body_thread_event(&pbd_event, &error)) {
1110 EM_DEBUG_LOG(" emcore_insert_partial_body_thread_event failed [%d]", error);
1114 /*Not checking for NULL here because is_event_inserted is never NULL. */
1115 *is_event_inserted = true;
1120 *is_event_inserted = false;
1127 if (NULL != err_code) {
1134 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)
1136 EM_DEBUG_FUNC_BEGIN("account_id [%d], result_mailbox [%p], err_code [%p]", account_id, result_mailbox, err_code);
1137 int ret = false, err = EMAIL_ERROR_NONE;
1138 emstorage_mailbox_tbl_t *mail_box_tbl_spam = NULL;
1140 if (result_mailbox == NULL) {
1141 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1142 err = EMAIL_ERROR_INVALID_PARAM;
1146 if (!emstorage_get_mailbox_by_mailbox_type(account_id, mailbox_type, &mail_box_tbl_spam, false, &err)) {
1148 EM_DEBUG_LOG("emstorage_get_mailbox_by_mailbox_type failed - %d", err);
1151 if (mail_box_tbl_spam) {
1152 result_mailbox->mailbox_type = mail_box_tbl_spam->mailbox_type;
1153 result_mailbox->mailbox_name = EM_SAFE_STRDUP(mail_box_tbl_spam->mailbox_name);
1154 result_mailbox->account_id = mail_box_tbl_spam->account_id;
1155 result_mailbox->mail_slot_size = mail_box_tbl_spam->mail_slot_size;
1156 if (!emstorage_free_mailbox(&mail_box_tbl_spam, 1, &err))
1157 EM_DEBUG_EXCEPTION(" emstorage_free_mailbox Failed [%d]", err);
1165 EM_DEBUG_FUNC_END();
1169 #ifdef __FEATURE_LOCAL_ACTIVITY__
1170 INTERNAL_FUNC int emcore_local_activity_sync(int account_id, int *err_code)
1172 EM_DEBUG_FUNC_BEGIN();
1174 EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1176 int *activity_id_list = NULL;
1177 int activity_count = 0;
1181 email_event_t event_data;
1184 memset(&event_data, 0x00, sizeof(email_event_t));
1187 EM_IF_NULL_RETURN_VALUE(err_code, false);
1189 if (account_id <= 0) {
1190 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1193 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1195 if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_DELETEMAIL, ACTIVITY_COPYMAIL, true, &err)) {
1196 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 4 ");
1197 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list failed [ %d] ", err);
1200 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 5 ");
1202 if (activity_count > 0) {
1203 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1204 event_data.account_id = account_id;
1205 if (!emcore_insert_event(&event_data, &handle, &err)) {
1206 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1215 if (activity_id_list)
1216 emstorage_free_activity_id_list(activity_id_list, &err);
1218 if (err_code != NULL)
1225 INTERNAL_FUNC int emcore_save_local_activity_sync(int account_id, int *err_code)
1227 EM_DEBUG_FUNC_BEGIN();
1229 EM_DEBUG_LOG(">> account_id [%d], err_code [%p] ", account_id, err_code);
1231 emstorage_activity_tbl_t *local_activity = NULL;
1232 int *activity_id_list = NULL;
1233 int activity_count = 0;
1237 email_event_t event_data;
1239 memset(&event_data, 0x00, sizeof(email_event_t));
1241 EM_IF_NULL_RETURN_VALUE(err_code, false);
1243 if (account_id <= 0) {
1244 EM_DEBUG_EXCEPTION(" Invalid Account ID [%d] ", account_id);
1247 EM_DEBUG_LOG(">>> emdaemon_sync_local_activity 3 ");
1249 if (!emstorage_get_activity_id_list(account_id, &activity_id_list, &activity_count, ACTIVITY_SAVEMAIL, ACTIVITY_DELETEMAIL_SEND, true, &err)) {
1250 EM_DEBUG_EXCEPTION(" emstorage_get_activity_id_list [ %d] ", err);
1255 if (activity_count > 0) {
1256 event_data.type = EMAIL_EVENT_LOCAL_ACTIVITY;
1257 event_data.account_id = account_id;
1258 if (!emcore_insert_event_for_sending_mails(&event_data, &handle, &err)) {
1259 EM_DEBUG_LOG(" emcore_insert_event failed - %d", err);
1270 emstorage_free_local_activity(&local_activity, activity_count, NULL);
1272 if (activity_id_list)
1273 emstorage_free_activity_id_list(activity_id_list, &err);
1275 if (err_code != NULL)