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 /******************************************************************************
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"
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_check_filter_id(int account_id, int filter_id, int* err_code)
53 EM_DEBUG_FUNC_BEGIN("account_id[%d], filter_id[%d], err_code[%p]", account_id, filter_id, err_code);
55 if (account_id != ALL_ACCOUNT) { /* only global rule supported. */
56 EM_DEBUG_EXCEPTION(" account_id[%d], filter_id[%d]", account_id, filter_id);
59 *err_code = EMAIL_ERROR_INVALID_PARAM;
64 int err = EMAIL_ERROR_NONE;
65 emstorage_rule_tbl_t* filter = NULL;
67 if (!emstorage_get_rule_by_id(account_id, filter_id, &filter, true, &err)) {
68 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
77 emstorage_free_rule(&filter, 1, NULL);
86 INTERNAL_FUNC int emdaemon_create_account(email_account_t* account, int* err_code)
89 int err = EMAIL_ERROR_NONE;
91 if (!emcore_create_account(account, &err)) {
92 EM_DEBUG_EXCEPTION(" emcore_account_add failed [%d]", err);
96 emcore_init_account_reference();
107 INTERNAL_FUNC int emdaemon_delete_account(int account_id, int* err_code)
109 EM_DEBUG_FUNC_BEGIN();
112 ret = emcore_delete_account(account_id, err_code);
117 static email_account_t* duplicate_account(email_account_t *src)
120 EM_DEBUG_EXCEPTION("INVALID_PARAM");
123 email_account_t *dst = (email_account_t *)em_malloc(sizeof(email_account_t));
125 EM_DEBUG_EXCEPTION(" malloc failed...");
130 memcpy(dst, src, sizeof(email_account_t));
131 dst->account_name = EM_SAFE_STRDUP(src->account_name);
132 dst->incoming_server_address = EM_SAFE_STRDUP(src->incoming_server_address);
133 dst->user_email_address = EM_SAFE_STRDUP(src->user_email_address);
134 dst->incoming_server_user_name = EM_SAFE_STRDUP(src->incoming_server_user_name);
135 dst->incoming_server_password = EM_SAFE_STRDUP(src->incoming_server_password);
136 dst->outgoing_server_address = EM_SAFE_STRDUP(src->outgoing_server_address);
137 dst->outgoing_server_user_name = EM_SAFE_STRDUP(src->outgoing_server_user_name);
138 dst->outgoing_server_password = EM_SAFE_STRDUP(src->outgoing_server_password);
139 dst->user_display_name = EM_SAFE_STRDUP(src->user_display_name);
140 dst->reply_to_address = EM_SAFE_STRDUP(src->reply_to_address);
141 dst->return_address = EM_SAFE_STRDUP(src->return_address);
142 dst->logo_icon_path = EM_SAFE_STRDUP(src->logo_icon_path);
143 dst->certificate_path = EM_SAFE_STRDUP(src->certificate_path);
144 dst->options.display_name_from = EM_SAFE_STRDUP(src->options.display_name_from);
145 dst->options.signature = EM_SAFE_STRDUP(src->options.signature);
146 dst->user_data = (void*) em_malloc(src->user_data_length);
147 if( !dst->user_data ) {
148 EM_DEBUG_EXCEPTION("em_malloc failed");
149 emcore_free_account(dst);
154 memcpy(dst->user_data, src->user_data, src->user_data_length);
159 INTERNAL_FUNC int emdaemon_validate_account(int account_id, int *handle, int* err_code)
161 EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
164 int err = EMAIL_ERROR_NONE;
165 email_event_t event_data = {0};
166 email_account_t* ref_account = NULL;
168 if (account_id < 1) {
169 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
170 err = EMAIL_ERROR_INVALID_PARAM;
174 if (!(ref_account = emcore_get_account_reference(account_id))) {
175 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", account_id);
176 err = EMAIL_ERROR_INVALID_ACCOUNT;
180 event_data.type = EMAIL_EVENT_VALIDATE_ACCOUNT;
181 event_data.event_param_data_1 = NULL;
182 event_data.event_param_data_3 = NULL;
183 event_data.account_id = account_id;
185 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
186 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
194 emcore_free_account(ref_account);
195 EM_SAFE_FREE(ref_account);
205 INTERNAL_FUNC int emdaemon_validate_account_and_create(email_account_t* new_account, int *handle, int* err_code)
207 EM_DEBUG_FUNC_BEGIN("account[%p], handle[%p], err_code[%p]", new_account, handle, err_code);
210 int err = EMAIL_ERROR_NONE;
211 email_event_t event_data = {0};
213 event_data.type = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
214 event_data.event_param_data_1 = (void*) new_account;
215 event_data.event_param_data_3 = NULL;
216 event_data.account_id = NEW_ACCOUNT_ID;
218 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
219 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
233 INTERNAL_FUNC int emdaemon_update_account(int account_id, email_account_t* new_account, int* err_code)
235 EM_DEBUG_FUNC_BEGIN("account_id[%d], new_account[%p], err_code[%p]", account_id, new_account, err_code);
237 /* default variable */
239 int err = EMAIL_ERROR_NONE;
240 emstorage_account_tbl_t *new_account_tbl = NULL;
241 email_account_t old_account_info = {0};
243 if ((account_id <= 0) || !new_account) {
244 EM_DEBUG_EXCEPTION("Invalid Parameters.");
245 err = EMAIL_ERROR_INVALID_PARAM;
249 if(!emdaemon_get_account(account_id, GET_FULL_DATA, &old_account_info, &err)) {
250 EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
254 EM_DEBUG_LOG("new_account->email_addr[%s]", new_account->user_email_address);
255 if(new_account->user_email_address) {
256 if (!em_verify_email_address(new_account->user_email_address, true, &err)) {
257 err = EMAIL_ERROR_INVALID_ADDRESS;
258 EM_DEBUG_EXCEPTION("Invalid Email Address");
263 new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
264 if(!new_account_tbl) {
265 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
269 em_convert_account_to_account_tbl(new_account, new_account_tbl);
271 if (!emstorage_update_account(account_id, new_account_tbl, true, &err)) {
272 EM_DEBUG_EXCEPTION("emstorage_update_account falied [%d]", err);
276 emcore_init_account_reference();
278 #ifdef __FEATURE_AUTO_POLLING__
279 int old_check_interval = old_account_info.check_interval;
280 if( old_check_interval < 0 && new_account->check_interval > 0) {
281 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
282 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
285 else if( (old_check_interval > 0) && (new_account->check_interval < 0)) {
286 if(!emdaemon_remove_polling_alarm(account_id))
287 EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
289 else if(old_check_interval != new_account->check_interval && new_account->check_interval > 0) {
290 if(!emdaemon_remove_polling_alarm(account_id)) {
291 EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
294 if(!emdaemon_add_polling_alarm(account_id, new_account->check_interval))
295 EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[ CHANGEACCOUNT] : start auto poll failed >>> ");
303 emcore_free_account(&old_account_info);
306 emstorage_free_account(&new_account_tbl, 1, NULL);
315 INTERNAL_FUNC int emdaemon_validate_account_and_update(int old_account_id, email_account_t* new_account_info, int *handle,int *err_code)
317 EM_DEBUG_FUNC_BEGIN("account[%d], new_account_info[%p], handle[%p], err_code[%p]", old_account_id, new_account_info, handle, err_code);
320 int err = EMAIL_ERROR_NONE;
321 email_event_t event_data = {0};
323 event_data.type = EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT;
324 event_data.event_param_data_1 = (char *) duplicate_account(new_account_info);
325 event_data.event_param_data_3 = NULL;
326 event_data.account_id = old_account_id;
329 email_account_t *pAccount = (email_account_t *)em_malloc(sizeof(email_account_t));
330 if (pAccount == NULL) {
331 EM_DEBUG_EXCEPTION(" malloc failed...");
332 err = EMAIL_ERROR_OUT_OF_MEMORY;
337 memcpy(pAccount, new_account_info, sizeof(email_account_t));
338 pAccount->account_name = EM_SAFE_STRDUP(new_account_info->account_name);
339 pAccount->incoming_server_address = EM_SAFE_STRDUP(new_account_info->incoming_server_address);
340 pAccount->user_email_address = EM_SAFE_STRDUP(new_account_info->user_email_address);
341 pAccount->incoming_server_user_name = EM_SAFE_STRDUP(new_account_info->user_email_address);
342 pAccount->incoming_server_password = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
343 pAccount->outgoing_server_address = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
344 pAccount->outgoing_server_user_name = EM_SAFE_STRDUP(new_account_info->outgoing_server_user_name);
345 pAccount->outgoing_server_password = EM_SAFE_STRDUP(new_account_info->outgoing_server_password);
346 pAccount->user_display_name = EM_SAFE_STRDUP(new_account_info->user_display_name);
347 pAccount->reply_to_address = EM_SAFE_STRDUP(new_account_info->reply_to_address);
348 pAccount->return_address = EM_SAFE_STRDUP(new_account_info->return_address);
349 pAccount->logo_icon_path = EM_SAFE_STRDUP(new_account_info->logo_icon_path);
350 pAccount->certificate_path = EM_SAFE_STRDUP(new_account_info->certificate_path);
351 pAccount->options.display_name_from = EM_SAFE_STRDUP(new_account_info->options.display_name_from);
352 pAccount->options.signature = EM_SAFE_STRDUP(new_account_info->options.signature);
353 memcpy(pAccount->user_data, new_account_info->user_data, new_account_info->user_data_length);
356 if (!emcore_insert_event(&event_data, (int*)handle, &err)) {
357 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
371 INTERNAL_FUNC int emdaemon_get_account(int account_id, int pulloption, email_account_t* account, int* err_code)
373 EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption [%d], account[%p], err_code[%p]", account_id, pulloption, account, err_code);
375 /* default variable */
377 int err = EMAIL_ERROR_NONE;
378 emstorage_account_tbl_t *account_tbl = NULL;
381 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
382 err = EMAIL_ERROR_INVALID_PARAM;
386 if (!emstorage_get_account_by_id(account_id, pulloption, &account_tbl, true, &err)) {
387 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
391 em_convert_account_tbl_to_account(account_tbl, account);
397 emstorage_free_account(&account_tbl, 1, NULL);
400 EM_DEBUG_FUNC_END("ret [%d]", ret);
404 INTERNAL_FUNC int emdaemon_get_account_list(email_account_t** account_list, int* count, int* err_code)
406 EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
408 int ret = false, err = EMAIL_ERROR_NONE, i = 0;
409 emstorage_account_tbl_t *account_tbl_array = NULL;
411 if (!account_list || !count) {
412 err = EMAIL_ERROR_INVALID_PARAM;
418 if (!emstorage_get_account_list(count, &account_tbl_array, true, true, &err)) {
419 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
423 if(account_tbl_array && (*count) > 0) {
424 *account_list = (email_account_t*)em_malloc(sizeof(email_account_t) * (*count));
426 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
430 for(i = 0 ; i < (*count); i++)
431 em_convert_account_tbl_to_account(account_tbl_array + i, (*account_list) + i);
437 if(account_tbl_array)
438 emstorage_free_account(&account_tbl_array, (*count), NULL);
440 if (err_code != NULL)
442 EM_DEBUG_FUNC_END("ret [%d]", ret);
447 INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int count, int* err_code)
449 EM_DEBUG_FUNC_BEGIN();
450 return emcore_free_account_list(account_list, count, err_code);
454 INTERNAL_FUNC int emdaemon_get_filter(int filter_id, email_rule_t** filter_info, int* err_code)
456 EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
459 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
461 if (err_code != NULL)
462 *err_code = EMAIL_ERROR_INVALID_PARAM;
466 /* default variable */
468 int err = EMAIL_ERROR_NONE;
470 if (!emstorage_get_rule_by_id(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err)) {
471 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
484 INTERNAL_FUNC int emdaemon_get_filter_list(email_rule_t** filter_info, int* count, int* err_code)
486 EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%p], err_code[%p]", filter_info, count, err_code);
488 /* default variable */
490 int err = EMAIL_ERROR_NONE;
493 if (!filter_info || !count) {
494 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%p]", filter_info, count);
495 err = EMAIL_ERROR_INVALID_PARAM;
501 if (!emstorage_get_rule(ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err)) {
502 EM_DEBUG_EXCEPTION(" emstorage_get_rule failed [%d]", err);
515 INTERNAL_FUNC int emdaemon_find_filter(email_rule_t* filter_info, int* err_code)
517 EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
519 /* default variable */
521 int err = EMAIL_ERROR_NONE;
524 EM_DEBUG_EXCEPTION(" filter_info[%p]", filter_info);
525 err = EMAIL_ERROR_INVALID_PARAM;
529 if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->target_mailbox_id) {
530 EM_DEBUG_EXCEPTION(" filter_info->faction[%d], filter_info->target_mailbox_id[%d]", filter_info->faction, filter_info->target_mailbox_id);
531 err = EMAIL_ERROR_INVALID_FILTER;
535 filter_info->account_id = ALL_ACCOUNT; /* MUST BE */
537 if (!emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
538 EM_DEBUG_EXCEPTION(" emstorage_find_rule failed [%d]", err);
539 err = EMAIL_ERROR_FILTER_NOT_FOUND;
552 INTERNAL_FUNC int emdaemon_add_filter(email_rule_t* filter_info)
554 EM_DEBUG_FUNC_BEGIN("filter_info[%p]", filter_info);
556 /* default variable */
557 int err = EMAIL_ERROR_NONE;
558 if (!filter_info || !(filter_info->value)) {
559 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
560 err = EMAIL_ERROR_INVALID_PARAM;
565 if (filter_info->faction != EMAIL_FILTER_BLOCK) {
566 EM_DEBUG_EXCEPTION("filter_info->faction[%d] is not supported", filter_info->faction);
567 err = EMAIL_ERROR_NOT_SUPPORTED;
571 if (filter_info->faction == EMAIL_FILTER_MOVE && !filter_info->mailbox) {
572 EM_DEBUG_EXCEPTION("filter_info->faction[%d], filter_info->mailbox[%p]", filter_info->faction, filter_info->mailbox);
573 err = EMAIL_ERROR_INVALID_FILTER;
578 filter_info->account_id = ALL_ACCOUNT;
580 if (emstorage_find_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
581 EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
582 err = EMAIL_ERROR_ALREADY_EXISTS;
586 if (!emstorage_add_rule((emstorage_rule_tbl_t*)filter_info, true, &err)) {
587 EM_DEBUG_EXCEPTION("emstorage_add_rule failed [%d]", err);
591 if (filter_info->type == EMAIL_PRIORITY_SENDER) {
592 EM_DEBUG_LOG("Priority Sender add");
596 if (!emcore_mail_filter_by_rule((email_rule_t*)filter_info, &err)) {
597 EM_DEBUG_EXCEPTION("emcore_mail_filter_by_rule failed [%d]", err);
607 INTERNAL_FUNC int emdaemon_update_filter(int filter_id, email_rule_t* filter_info, int* err_code)
609 EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
611 /* default variable */
613 int err = EMAIL_ERROR_NONE;
615 if ((filter_id <= 0) || !filter_info) {
616 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
617 err = EMAIL_ERROR_INVALID_PARAM;
621 if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err)) {
622 EM_DEBUG_EXCEPTION("emdaemon_check_filter_id falied [%d]", err);
626 if (!emstorage_change_rule(ALL_ACCOUNT, filter_id, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
627 EM_DEBUG_EXCEPTION("emstorage_change_rule falied [%d]", err);
640 INTERNAL_FUNC int emdaemon_delete_filter(int filter_id, int* err_code)
642 EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
644 /* default variable */
646 int err = EMAIL_ERROR_NONE;
648 if (filter_id <= 0) {
649 EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
650 err = EMAIL_ERROR_INVALID_PARAM;
654 if (!emdaemon_check_filter_id(ALL_ACCOUNT, filter_id, &err)) {
655 EM_DEBUG_EXCEPTION(" emdaemon_check_filter_id failed [%d]", err);
659 if (!emstorage_delete_rule(ALL_ACCOUNT, filter_id, true, &err)) {
660 EM_DEBUG_EXCEPTION(" emstorage_delete_rule failed [%d]", err);
673 INTERNAL_FUNC int emdaemon_free_filter(email_rule_t** filter_info, int count, int* err_code)
675 EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%d], err_code[%p]", filter_info, count, err_code);
677 /* default variable */
679 int err = EMAIL_ERROR_NONE;
682 if (!filter_info || !*filter_info) {
683 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%d]", filter_info, count);
684 err = EMAIL_ERROR_INVALID_PARAM;
688 email_rule_t* p = *filter_info;
691 for (i = 0; i < count; i++) {
692 EM_SAFE_FREE(p[i].value);
695 EM_SAFE_FREE(p); *filter_info = NULL;
708 /* ----- internal functions --------------------------------------------*/
709 int emdaemon_initialize_account_reference()
711 EM_DEBUG_FUNC_BEGIN();
712 int err = EMAIL_ERROR_NONE;
714 if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
715 if (err == EMAIL_ERROR_SECURED_STORAGE_FAILURE) {
716 if ((err = emcore_recover_from_secured_storage_failure()) != EMAIL_ERROR_NONE) {
717 EM_DEBUG_EXCEPTION("emcore_recover_from_secured_storage_failure failed [%d]", err);
721 if ((err = emcore_init_account_reference()) != EMAIL_ERROR_NONE) {
722 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
727 EM_DEBUG_EXCEPTION("emcore_init_account_reference failed [%d]", err);
734 EM_DEBUG_FUNC_END("err [%d]", err);
738 INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* account)
740 EM_DEBUG_FUNC_BEGIN();
750 INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_account, email_account_t* new_account)
752 EM_DEBUG_FUNC_BEGIN();
754 if(!old_account || !new_account)