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 /******************************************************************************
25 * Desc: email-daemon Account
30 * 2006.08.16 : created
31 *****************************************************************************/
37 #include "email-daemon.h"
38 #include "email-storage.h"
40 #include "email-debug-log.h"
41 #include "email-daemon-account.h"
42 #include "email-daemon-auto-poll.h"
43 #include <contacts-svc.h>
44 #include "email-types.h"
45 #include "email-core-account.h"
46 #include "email-core-event.h"
47 #include "email-core-utils.h"
48 #include "email-utilities.h"
49 #include "email-convert.h"
51 static int emdaemon_refresh_account_reference()
53 EM_DEBUG_FUNC_BEGIN();
54 return emcore_refresh_account_reference();
57 static int emdaemon_check_filter_id(int account_id, int filter_id, int* err_code)
59 EM_DEBUG_FUNC_BEGIN("account_id[%d], filter_id[%d], err_code[%p]", account_id, filter_id, err_code);
61 if (account_id != ALL_ACCOUNT) { /* only global rule supported. */
62 EM_DEBUG_EXCEPTION(" account_id[%d], filter_id[%d]", account_id, filter_id);
65 *err_code = EMAIL_ERROR_INVALID_PARAM;
70 int err = EMAIL_ERROR_NONE;
71 emstorage_rule_tbl_t* filter = NULL;
73 if (!emstorage_get_rule_by_id(account_id, filter_id, &filter, true, &err)) {
74 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
83 emstorage_free_rule(&filter, 1, NULL);
92 INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_code)
95 int err = EMAIL_ERROR_NONE;
97 if (!emcore_create_account(account, &err)) {
98 EM_DEBUG_EXCEPTION(" emcore_account_add failed [%d]", err);
101 emdaemon_refresh_account_reference();
112 INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code)
114 EM_DEBUG_FUNC_BEGIN();
117 ret = emcore_delete_account(account_id, err_code);
122 static email_account_t* duplicate_account(email_account_t *src)
125 EM_DEBUG_EXCEPTION("INVALID_PARAM");
128 email_account_t *dst = (email_account_t *)em_malloc(sizeof(email_account_t));
130 EM_DEBUG_EXCEPTION(" malloc failed...");
135 memcpy(dst, src, sizeof(email_account_t));
136 dst->account_name = EM_SAFE_STRDUP(src->account_name);
137 dst->incoming_server_address = EM_SAFE_STRDUP(src->incoming_server_address);
138 dst->user_email_address = EM_SAFE_STRDUP(src->user_email_address);
139 dst->incoming_server_user_name = EM_SAFE_STRDUP(src->user_email_address);
140 dst->incoming_server_password = EM_SAFE_STRDUP(src->incoming_server_password);
141 dst->outgoing_server_address = EM_SAFE_STRDUP(src->incoming_server_password);
142 dst->outgoing_server_user_name = EM_SAFE_STRDUP(src->outgoing_server_user_name);
143 dst->outgoing_server_password = EM_SAFE_STRDUP(src->outgoing_server_password);
144 dst->user_display_name = EM_SAFE_STRDUP(src->user_display_name);
145 dst->reply_to_address = EM_SAFE_STRDUP(src->reply_to_address);
146 dst->return_address = EM_SAFE_STRDUP(src->return_address);
147 dst->logo_icon_path = EM_SAFE_STRDUP(src->logo_icon_path);
148 dst->certificate_path = EM_SAFE_STRDUP(src->certificate_path);
149 dst->options.display_name_from = EM_SAFE_STRDUP(src->options.display_name_from);
150 dst->options.signature = EM_SAFE_STRDUP(src->options.signature);
151 dst->user_data = (void*) em_malloc(src->user_data_length);
152 if( !dst->user_data ) {
153 EM_DEBUG_EXCEPTION("em_malloc failed");
157 memcpy(dst->user_data, src->user_data, src->user_data_length);
162 INTERNAL_FUNC int emdaemon_validate_account(int account_id, unsigned* handle, int* err_code)
164 EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
167 int err = EMAIL_ERROR_NONE;
169 if (account_id < 1) {
170 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
171 err = EMAIL_ERROR_INVALID_PARAM;
175 email_event_t event_data = {0};
176 email_account_t* ref_account = NULL;
178 if (!(ref_account = emdaemon_get_account_reference(account_id))) {
179 EM_DEBUG_EXCEPTION(" emdaemon_get_account_reference failed [%d]", account_id);
180 err = EMAIL_ERROR_INVALID_ACCOUNT;
184 event_data.type = EMAIL_EVENT_VALIDATE_ACCOUNT;
185 event_data.event_param_data_1 = NULL;
186 event_data.event_param_data_3 = NULL;
187 event_data.account_id = account_id;
189 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
190 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
203 INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_account, unsigned* handle, int* err_code)
205 EM_DEBUG_FUNC_BEGIN("account[%p], handle[%p], err_code[%p]", new_account, handle, err_code);
208 int err = EMAIL_ERROR_NONE;
209 email_event_t event_data = {0};
211 event_data.type = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
212 event_data.event_param_data_1 = (void*) new_account;
213 event_data.event_param_data_3 = NULL;
214 event_data.account_id = NEW_ACCOUNT_ID;
216 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
217 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
231 INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_account, int* err_code)
233 EM_DEBUG_FUNC_BEGIN("account_id[%d], new_account[%p], err_code[%p]", account_id, new_account, err_code);
235 /* default variable */
237 int err = EMAIL_ERROR_NONE;
238 emstorage_account_tbl_t *new_account_tbl = NULL;
239 email_account_t old_account_info = {0};
241 if ((account_id <= 0) || !new_account) {
242 EM_DEBUG_EXCEPTION("Invalid Parameters.");
243 err = EMAIL_ERROR_INVALID_PARAM;
247 if(!emdaemon_get_account(account_id, GET_FULL_DATA, &old_account_info, &err)) {
248 EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
252 EM_DEBUG_LOG("new_account->email_addr[%s]", new_account->user_email_address);
253 if(new_account->user_email_address) {
254 if (!em_verify_email_address(new_account->user_email_address, true, &err)) {
255 err = EMAIL_ERROR_INVALID_ADDRESS;
256 EM_DEBUG_EXCEPTION("Invalid Email Address");
261 new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
262 if(!new_account_tbl) {
263 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
267 em_convert_account_to_account_tbl(new_account, new_account_tbl);
269 if (!emstorage_update_account(account_id, new_account_tbl, true, &err)) {
270 EM_DEBUG_EXCEPTION("emstorage_update_account falied [%d]", err);
274 emdaemon_refresh_account_reference();
276 #ifdef __FEATURE_AUTO_POLLING__
277 int old_check_interval = old_account_info.check_interval;
278 if( old_check_interval < 0 && new_account->check_interval > 0) {
279 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
280 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
283 else if( (old_check_interval > 0) && (new_account->check_interval < 0)) {
284 if(!emdaemon_remove_polling_alarm(account_id))
285 EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
287 else if(old_check_interval != new_account->check_interval && new_account->check_interval > 0) {
288 if(!emdaemon_remove_polling_alarm(account_id)) {
289 EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
292 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
293 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
301 emcore_free_account(&old_account_info);
304 emstorage_free_account(&new_account_tbl, 1, NULL);
313 INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email_account_t* new_account_info, unsigned* handle,int *err_code)
315 EM_DEBUG_FUNC_BEGIN("account[%d], new_account_info[%p], handle[%p], err_code[%p]", old_account_id, new_account_info, handle, err_code);
318 int err = EMAIL_ERROR_NONE;
319 email_event_t event_data = {0};
321 event_data.type = EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT;
322 event_data.event_param_data_1 = (char *) duplicate_account(new_account_info);
323 event_data.event_param_data_3 = NULL;
324 event_data.account_id = old_account_id;
327 email_account_t *pAccount = (email_account_t *)em_malloc(sizeof(email_account_t));
328 if (pAccount == NULL) {
329 EM_DEBUG_EXCEPTION(" malloc failed...");
330 err = EMAIL_ERROR_OUT_OF_MEMORY;
335 memcpy(pAccount, new_account_info, sizeof(email_account_t));
336 pAccount->account_name = EM_SAFE_STRDUP(new_account_info->account_name);
337 pAccount->incoming_server_address = EM_SAFE_STRDUP(new_account_info->incoming_server_address);
338 pAccount->user_email_address = EM_SAFE_STRDUP(new_account_info->user_email_address);
339 pAccount->incoming_server_user_name = EM_SAFE_STRDUP(new_account_info->user_email_address);
340 pAccount->incoming_server_password = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
341 pAccount->outgoing_server_address = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
342 pAccount->outgoing_server_user_name = EM_SAFE_STRDUP(new_account_info->outgoing_server_user_name);
343 pAccount->outgoing_server_password = EM_SAFE_STRDUP(new_account_info->outgoing_server_password);
344 pAccount->user_display_name = EM_SAFE_STRDUP(new_account_info->user_display_name);
345 pAccount->reply_to_address = EM_SAFE_STRDUP(new_account_info->reply_to_address);
346 pAccount->return_address = EM_SAFE_STRDUP(new_account_info->return_address);
347 pAccount->logo_icon_path = EM_SAFE_STRDUP(new_account_info->logo_icon_path);
348 pAccount->certificate_path = EM_SAFE_STRDUP(new_account_info->certificate_path);
349 pAccount->options.display_name_from = EM_SAFE_STRDUP(new_account_info->options.display_name_from);
350 pAccount->options.signature = EM_SAFE_STRDUP(new_account_info->options.signature);
351 memcpy(pAccount->user_data, new_account_info->user_data, new_account_info->user_data_length);
354 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
355 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
369 INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_account_t* account, int* err_code)
371 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption [%d], account[%p], err_code[%p]", account_id, pulloption, account, err_code);
373 /* default variable */
375 int err = EMAIL_ERROR_NONE;
376 emstorage_account_tbl_t *account_tbl = NULL;
379 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
380 err = EMAIL_ERROR_INVALID_PARAM;
384 if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
385 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
389 em_convert_account_tbl_to_account(account_tbl, account);
395 emstorage_free_account(&account_tbl, 1, NULL);
398 EM_DEBUG_FUNC_END("ret [%d]", ret);
402 INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** account_list, int* count, int* err_code)
404 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
406 int ret = false, err = EMAIL_ERROR_NONE, i = 0;
407 emstorage_account_tbl_t *account_tbl_array = NULL;
409 if (!account_list || !count) {
410 EM_DEBUG_EXCEPTION("account_list[%p], count[%p]", account_list, (*count));
411 err = EMAIL_ERROR_INVALID_PARAM;
417 if (!emstorage_get_account_list(count, &account_tbl_array, true, true, &err)) {
418 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
422 if(account_tbl_array && (*count) > 0) {
423 *account_list = (email_account_t*)em_malloc(sizeof(email_account_t) * (*count));
425 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
429 for(i = 0 ; i < (*count); i++)
430 em_convert_account_tbl_to_account(account_tbl_array + i, (*account_list) + i);
436 if(account_tbl_array)
437 emstorage_free_account(&account_tbl_array, (*count), NULL);
439 if (err_code != NULL)
441 EM_DEBUG_FUNC_END("ret [%d]", ret);
446 INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int count, int* err_code)
448 EM_DEBUG_FUNC_BEGIN();
449 return emcore_free_account_list(account_list, count, err_code);
453 INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filter_info, int* err_code)
455 EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
458 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
460 if (err_code != NULL)
461 *err_code = EMAIL_ERROR_INVALID_PARAM;
465 /* default variable */
467 int err = EMAIL_ERROR_NONE;
469 if (!emstorage_get_rule_by_id(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err)) {
470 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
483 INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filter_info, int* count, int* err_code)
485 EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%p], err_code[%p]", filter_info, count, err_code);
487 /* default variable */
489 int err = EMAIL_ERROR_NONE;
492 if (!filter_info || !count) {
493 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%p]", filter_info, count);
494 err = EMAIL_ERROR_INVALID_PARAM;
500 if (!emstorage_get_rule(ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err)) {
501 EM_DEBUG_EXCEPTION(" emstorage_get_rule failed [%d]", err);
514 INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
516 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
518 /* default variable */
520 int err = EMAIL_ERROR_NONE;
523 EM_DEBUG_EXCEPTION(" filter_info[%p]", filter_info);
524 err = EMAIL_ERROR_INVALID_PARAM;
528 if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->target_mailbox_id) {
529 EM_DEBUG_EXCEPTION(" filter_info->faction[%d], filter_info->target_mailbox_id[%d]", filter_info->faction, filter_info->target_mailbox_id);
530 err = EMAIL_ERROR_INVALID_FILTER;
534 filter_info->account_id = ALL_ACCOUNT; /* MUST BE */
536 if (!emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
537 EM_DEBUG_EXCEPTION(" emstorage_find_rule failed [%d]", err);
538 err = EMAIL_ERROR_FILTER_NOT_FOUND;
551 INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filter_info, int* err_code)
553 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
555 /* default variable */
556 int ret = false, err = EMAIL_ERROR_NONE;
557 if (!filter_info || !(filter_info->value)) {
558 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
559 err = EMAIL_ERROR_INVALID_PARAM;
564 if (filter_info->faction != EMAIL_FILTER_BLOCK) {
565 EM_DEBUG_EXCEPTION("filter_info->faction[%d] is not supported", filter_info->faction);
566 err = EMAIL_ERROR_NOT_SUPPORTED;
570 if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->mailbox) {
571 EM_DEBUG_EXCEPTION("filter_info->faction[%d], filter_info->mailbox[%p]", filter_info->faction, filter_info->mailbox);
572 err = EMAIL_ERROR_INVALID_FILTER;
577 filter_info->account_id = ALL_ACCOUNT;
579 if (emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
580 EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
581 err = EMAIL_ERROR_ALREADY_EXISTS;
585 if (!emstorage_add_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
586 EM_DEBUG_EXCEPTION("emstorage_add_rule failed [%d]", err);
590 if (!emcore_mail_filter_by_rule((email_rule_t*)filter_info, &err)) {
591 EM_DEBUG_EXCEPTION("emcore_mail_filter_by_rule failed [%d]", err);
605 INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* filter_info, int* err_code)
607 EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
609 /* default variable */
611 int err = EMAIL_ERROR_NONE;
613 if ((filter_id <= 0) || !filter_info) {
614 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
615 err = EMAIL_ERROR_INVALID_PARAM;
619 if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err)) {
620 EM_DEBUG_EXCEPTION("emdaemon_check_filter_id falied [%d]", err);
624 if (!emstorage_change_rule(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
625 EM_DEBUG_EXCEPTION("emstorage_change_rule falied [%d]", err);
638 INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code)
640 EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
642 /* default variable */
644 int err = EMAIL_ERROR_NONE;
646 if (filter_id <= 0) {
647 EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
648 err = EMAIL_ERROR_INVALID_PARAM;
652 if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err)) {
653 EM_DEBUG_EXCEPTION(" emdaemon_check_filter_id failed [%d]", err);
657 if (!emstorage_delete_rule(ALL_ACCOUNT, filter_id, true, &err)) {
658 EM_DEBUG_EXCEPTION(" emstorage_delete_rule failed [%d]", err);
671 INTERNAL_FUNC int emdaemon_free_filter(email_rule_t** filter_info, int count, int* err_code)
673 EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%d], err_code[%p]", filter_info, count, err_code);
675 /* default variable */
677 int err = EMAIL_ERROR_NONE;
680 if (!filter_info || !*filter_info) {
681 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%d]", filter_info, count);
682 err = EMAIL_ERROR_INVALID_PARAM;
686 email_rule_t* p = *filter_info;
689 for (i = 0; i < count; i++) {
690 EM_SAFE_FREE(p[i].value);
693 EM_SAFE_FREE(p); *filter_info = NULL;
706 /* ----- internal functions --------------------------------------------*/
707 int emdaemon_initialize_account_reference()
709 EM_DEBUG_FUNC_BEGIN();
710 int err = EMAIL_ERROR_NONE;
712 if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
713 if (err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
714 if ((err = emcore_recover_from_secured_storage_failure()) != EMAIL_ERROR_NONE) {
715 EM_DEBUG_EXCEPTION("emcore_recover_from_secured_storage_failure failed [%d]", err);
719 if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
720 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
725 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
732 EM_DEBUG_FUNC_END("err [%d]", err);
736 email_account_t* emdaemon_get_account_reference(int account_id)
738 EM_DEBUG_FUNC_BEGIN();
739 return emcore_get_account_reference(account_id);
742 int emdaemon_free_account_reference()
744 EM_DEBUG_FUNC_BEGIN();
745 return emcore_free_account_reference();
748 INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* account)
750 EM_DEBUG_FUNC_BEGIN();
760 INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_account, email_account_t* new_account)
762 EM_DEBUG_FUNC_BEGIN();
764 if(!old_account || !new_account)