Fixed the bug for tizen 3.0
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-account.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22
23 /******************************************************************************
24  * File: emf-account.c
25  * Desc: email-daemon Account
26  *
27  * Auth:
28  *
29  * History:
30  *    2006.08.16 : created
31  *****************************************************************************/
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <vconf.h>
36
37 #include "email-daemon.h"
38 #include "email-storage.h"
39 #include "c-client.h"
40 #include "email-debug-log.h"
41 #include "email-daemon-account.h"
42 #include "email-daemon-auto-poll.h"
43 #include <contacts.h>
44 #include <contacts_internal.h>
45 #include "email-types.h"
46 #include "email-core-account.h"
47 #include "email-core-event.h"
48 #include "email-core-utils.h"
49 #include "email-core-imap-idle.h"
50 #include "email-utilities.h"
51 #include "email-convert.h"
52
53 static int emdaemon_check_filter_id(char *multi_user_name, int filter_id, int* err_code)
54 {
55         EM_DEBUG_FUNC_BEGIN("filter_id[%d], err_code[%p]", filter_id, err_code);
56
57         if (filter_id  <= 0) {          /*  only global rule supported. */
58                 EM_DEBUG_EXCEPTION("filter_id[%d]", filter_id);
59
60                 if (err_code != NULL)
61                         *err_code = EMAIL_ERROR_INVALID_PARAM;
62                 return false;
63         }
64
65         int ret = false;
66         int err = EMAIL_ERROR_NONE;
67         emstorage_rule_tbl_t* filter = NULL;
68
69         if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &filter, true, &err)) {
70                 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
71                 goto FINISH_OFF;
72         }
73
74         ret = true;
75
76 FINISH_OFF:
77         if (filter != NULL)
78                 emstorage_free_rule(&filter, 1, NULL);
79
80         if (err_code != NULL)
81                 *err_code = err;
82         EM_DEBUG_FUNC_END();
83         return ret;
84 }
85
86
87 INTERNAL_FUNC int emdaemon_create_account(char *multi_user_name, email_account_t* account, int* err_code)
88 {
89         int ret = false;
90         int err = EMAIL_ERROR_NONE;
91
92         if (!emcore_create_account(multi_user_name, account, false, &err)) {
93                 EM_DEBUG_EXCEPTION(" emcore_account_add failed [%d]", err);
94                 goto FINISH_OFF;
95         }
96
97         ret = true;
98 FINISH_OFF:
99         if (err_code)
100                 *err_code = err;
101         EM_DEBUG_FUNC_END();
102         return ret;
103 }
104
105
106 INTERNAL_FUNC int emdaemon_delete_account(char *multi_user_name, int account_id, int* err_code)
107 {
108         EM_DEBUG_FUNC_BEGIN("account_id[%d] err_code[%p]", account_id, err_code);
109         int ret = false;
110
111         ret = emcore_delete_account(multi_user_name, account_id, false, err_code);
112
113         EM_DEBUG_FUNC_END("ret[%d]", ret);
114         return ret;
115 }
116
117 static email_account_t* duplicate_account(email_account_t *src)
118 {
119         if(!src) {
120                 EM_DEBUG_EXCEPTION("INVALID_PARAM");
121                 return NULL;
122         }
123         email_account_t *dst = (email_account_t *)em_malloc(sizeof(email_account_t));
124         if (!dst) {
125                 EM_DEBUG_EXCEPTION(" malloc failed...");
126                 return NULL;
127         }
128
129         /* Need deep copy */
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);
150                 EM_SAFE_FREE(dst);
151                 return NULL;
152         }
153
154         memcpy(dst->user_data, src->user_data, src->user_data_length);
155
156         return dst;
157 }
158
159 INTERNAL_FUNC int emdaemon_validate_account(char *multi_user_name, int account_id, int *handle, int* err_code)
160 {
161         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
162
163         int ret = false;
164         int err = EMAIL_ERROR_NONE;
165         email_event_t *event_data = NULL;
166         email_account_t *ref_account = NULL;
167
168         if (account_id < 1) {
169                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
170                 err = EMAIL_ERROR_INVALID_PARAM;
171                 goto FINISH_OFF;
172         }
173
174         if (!(ref_account = emcore_get_account_reference(multi_user_name, account_id, false))) {
175                 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed [%d]", account_id);
176                 err = EMAIL_ERROR_INVALID_ACCOUNT;
177                 goto FINISH_OFF;
178         }
179
180         event_data = em_malloc(sizeof(email_event_t));
181         event_data->type = EMAIL_EVENT_VALIDATE_ACCOUNT;
182         event_data->event_param_data_1 = NULL;
183         event_data->event_param_data_3 = NULL;
184         event_data->account_id = account_id;
185     event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
186
187         if (!emcore_insert_event(event_data, (int*)handle, &err)) {
188                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
189                 goto FINISH_OFF;
190         }
191
192         ret = true;
193
194 FINISH_OFF:
195
196         if (ref_account) {
197                 emcore_free_account(ref_account);
198                 EM_SAFE_FREE(ref_account);
199         }
200
201         if (ret == false && event_data) {
202                 emcore_free_event(event_data);
203                 EM_SAFE_FREE(event_data);
204         }
205
206         if (err_code)
207                 *err_code = err;
208
209         EM_DEBUG_FUNC_END();
210
211         return ret;
212 }
213
214 INTERNAL_FUNC int emdaemon_validate_account_ex(char *multi_user_name, email_account_t* input_account, int *output_handle)
215 {
216         EM_DEBUG_FUNC_BEGIN("input_account[%p], output_handle[%p]", input_account, output_handle);
217
218         int ret = false;
219         int err = EMAIL_ERROR_NONE;
220         email_event_t *event_data = NULL;
221
222         event_data = em_malloc(sizeof(email_event_t));
223
224         if(!event_data) { /*prevent 53095*/
225                 EM_DEBUG_EXCEPTION("em_malloc failed");
226                 err = EMAIL_ERROR_OUT_OF_MEMORY;
227                 goto FINISH_OFF;
228         }
229
230         event_data->type               = EMAIL_EVENT_VALIDATE_ACCOUNT_EX;
231         event_data->event_param_data_1 = (void*)input_account;
232         event_data->event_param_data_3 = NULL;
233         event_data->account_id         = NEW_ACCOUNT_ID;
234     event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
235
236         if (!emcore_insert_event(event_data, (int*)output_handle, &err)) {
237                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
238                 goto FINISH_OFF;
239         }
240
241         ret = true;
242
243 FINISH_OFF:
244
245         if (ret == false && event_data) {
246                 emcore_free_event(event_data);
247                 EM_SAFE_FREE(event_data);
248         }
249
250         EM_DEBUG_FUNC_END("err [%d]", err);
251
252         return err;
253 }
254
255 INTERNAL_FUNC int emdaemon_validate_account_and_create(char *multi_user_name, email_account_t* new_account, int *handle, int* err_code)
256 {
257         EM_DEBUG_FUNC_BEGIN("account[%p], handle[%p], err_code[%p]", new_account, handle, err_code);
258
259         int ret = false;
260         int err = EMAIL_ERROR_NONE;
261         email_event_t *event_data = NULL;
262
263         event_data = em_malloc(sizeof(email_event_t));
264
265         if(!event_data) { /*prevent 53093*/
266                 EM_DEBUG_EXCEPTION("em_malloc failed");
267                 err = EMAIL_ERROR_OUT_OF_MEMORY;
268                 goto FINISH_OFF;
269         }
270
271         event_data->type               = EMAIL_EVENT_VALIDATE_AND_CREATE_ACCOUNT;
272         event_data->event_param_data_1 = (void *)new_account;
273         event_data->event_param_data_3 = NULL;
274         event_data->account_id         = NEW_ACCOUNT_ID;
275     event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
276
277         if (!emcore_insert_event(event_data, (int*)handle, &err))  {
278                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
279                 goto FINISH_OFF;
280         }
281
282         ret = true;
283
284 FINISH_OFF:
285
286         if (ret == false && event_data) {
287                 emcore_free_event(event_data);
288                 EM_SAFE_FREE(event_data);
289         }
290
291         if (err_code)
292                 *err_code = err;
293
294         EM_DEBUG_FUNC_END();
295
296         return ret;
297 }
298
299
300 INTERNAL_FUNC int emdaemon_update_account(char *multi_user_name, int account_id, email_account_t* new_account, int* err_code)
301 {
302         EM_DEBUG_FUNC_BEGIN("account_id[%d], new_account[%p], err_code[%p]", account_id, new_account, err_code);
303
304         /*  default variable */
305         int ret = false;
306         int err = EMAIL_ERROR_NONE;
307         emstorage_account_tbl_t *new_account_tbl = NULL;
308         email_account_t *old_account_info = NULL;
309
310         if ((account_id <= 0) || !new_account)  {
311                 EM_DEBUG_EXCEPTION("Invalid Parameters.");
312                 err = EMAIL_ERROR_INVALID_PARAM;
313                 goto FINISH_OFF;
314         }
315
316         if((old_account_info = emcore_get_account_reference(multi_user_name, account_id, true)) == NULL) {
317                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed ");
318                 goto FINISH_OFF;
319         }
320
321         if(new_account->user_email_address) {
322                 if ((err = em_verify_email_address (new_account->user_email_address)) != EMAIL_ERROR_NONE) {
323                         EM_DEBUG_EXCEPTION("em_verify_email_address error [%d]", err);
324                         goto FINISH_OFF;
325                 }
326         }
327
328         if (EM_SAFE_STRCMP(new_account->incoming_server_password, old_account_info->incoming_server_password) == 0) {
329                 EM_SAFE_FREE(new_account->incoming_server_password);
330         }
331
332         if (EM_SAFE_STRCMP(new_account->outgoing_server_password, old_account_info->outgoing_server_password) == 0) {
333                 EM_SAFE_FREE(new_account->outgoing_server_password);
334         }
335
336         new_account_tbl = em_malloc(sizeof(emstorage_account_tbl_t));
337         if(!new_account_tbl) {
338                 EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
339                 goto FINISH_OFF;
340         }
341
342         em_convert_account_to_account_tbl(new_account, new_account_tbl);
343
344         if (!emstorage_update_account(multi_user_name, account_id, new_account_tbl, true, &err))  {
345                 EM_DEBUG_EXCEPTION("emstorage_update_account failed [%d]", err);
346                 goto FINISH_OFF;
347         }
348
349 #ifdef __FEATURE_AUTO_POLLING__
350         int  change_in_auto_polling_option = 0;
351
352         change_in_auto_polling_option = (old_account_info->check_interval  != new_account->check_interval)  ||
353                                                                         (old_account_info->peak_interval   != new_account->peak_interval)   ||
354                                                                         (old_account_info->peak_start_time != new_account->peak_start_time) ||
355                                                                         (old_account_info->peak_end_time   != new_account->peak_end_time);
356
357         EM_DEBUG_LOG("change_in_auto_polling_option [%d]", change_in_auto_polling_option);
358 #endif
359
360
361 #ifdef __FEATURE_AUTO_POLLING__
362         if(change_in_auto_polling_option) {
363                 if(!emdaemon_remove_polling_alarm(account_id))
364                         EM_DEBUG_LOG("emdaemon_remove_polling_alarm failed");
365
366                 if(!emdaemon_add_polling_alarm(multi_user_name, account_id))
367                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm failed");
368
369 #ifdef __FEATURE_IMAP_IDLE__
370                 emcore_refresh_imap_idle_thread();
371 #endif /* __FEATURE_IMAP_IDLE__ */
372         }
373 #endif
374
375         ret = true;
376
377 FINISH_OFF:
378
379         if(new_account_tbl)
380                 emstorage_free_account(&new_account_tbl, 1, NULL);
381
382         if (old_account_info) {
383                 emcore_free_account(old_account_info);
384                 EM_SAFE_FREE(old_account_info);
385         }
386
387         if (err_code)
388                 *err_code = err;
389
390         EM_DEBUG_FUNC_END();
391         return ret;
392 }
393
394 INTERNAL_FUNC int emdaemon_validate_account_and_update(char *multi_user_name, int old_account_id, email_account_t* new_account_info, int *handle,int *err_code)
395 {
396         EM_DEBUG_FUNC_BEGIN("account[%d], new_account_info[%p], handle[%p], err_code[%p]", old_account_id, new_account_info, handle, err_code);
397
398         int ret = false;
399         int err = EMAIL_ERROR_NONE;
400         email_event_t *event_data = NULL;
401
402         event_data = em_malloc(sizeof(email_event_t));
403
404         if(!event_data) { /*prevent 53094*/
405                 EM_DEBUG_EXCEPTION("em_malloc failed");
406                 err = EMAIL_ERROR_OUT_OF_MEMORY;
407                 goto FINISH_OFF;
408         }
409
410         event_data->type = EMAIL_EVENT_VALIDATE_AND_UPDATE_ACCOUNT;
411         event_data->event_param_data_1 = (char *) duplicate_account(new_account_info);
412         event_data->event_param_data_3 = NULL;
413         event_data->account_id = old_account_id;
414         event_data->multi_user_name    = EM_SAFE_STRDUP(multi_user_name);
415
416 #if 0
417         email_account_t *pAccount = (email_account_t *)em_malloc(sizeof(email_account_t));
418         if (pAccount == NULL) {
419                 EM_DEBUG_EXCEPTION(" malloc failed...");
420                 err = EMAIL_ERROR_OUT_OF_MEMORY;
421                 goto FINISH_OFF;
422         }
423
424         /* Need deep copy */
425         memcpy(pAccount, new_account_info, sizeof(email_account_t));
426         pAccount->account_name              = EM_SAFE_STRDUP(new_account_info->account_name);
427         pAccount->incoming_server_address   = EM_SAFE_STRDUP(new_account_info->incoming_server_address);
428         pAccount->user_email_address        = EM_SAFE_STRDUP(new_account_info->user_email_address);
429         pAccount->incoming_server_user_name = EM_SAFE_STRDUP(new_account_info->user_email_address);
430         pAccount->incoming_server_password  = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
431         pAccount->outgoing_server_address   = EM_SAFE_STRDUP(new_account_info->incoming_server_password);
432         pAccount->outgoing_server_user_name = EM_SAFE_STRDUP(new_account_info->outgoing_server_user_name);
433         pAccount->outgoing_server_password  = EM_SAFE_STRDUP(new_account_info->outgoing_server_password);
434         pAccount->user_display_name         = EM_SAFE_STRDUP(new_account_info->user_display_name);
435         pAccount->reply_to_address          = EM_SAFE_STRDUP(new_account_info->reply_to_address);
436         pAccount->return_address            = EM_SAFE_STRDUP(new_account_info->return_address);
437         pAccount->logo_icon_path            = EM_SAFE_STRDUP(new_account_info->logo_icon_path);
438         pAccount->certificate_path                      = EM_SAFE_STRDUP(new_account_info->certificate_path);
439         pAccount->options.display_name_from = EM_SAFE_STRDUP(new_account_info->options.display_name_from);
440         pAccount->options.signature             = EM_SAFE_STRDUP(new_account_info->options.signature);
441         memcpy(pAccount->user_data, new_account_info->user_data, new_account_info->user_data_length);
442 #endif
443
444         if (!emcore_insert_event(event_data, (int*)handle, &err)) {
445                 EM_DEBUG_EXCEPTION("emcore_insert_event falied [%d]", err);
446                 goto FINISH_OFF;
447         }
448
449         ret = true;
450
451 FINISH_OFF:
452
453         if (ret == false && event_data) {
454                 emcore_free_event(event_data);
455                 EM_SAFE_FREE(event_data);
456         }
457
458         if (err_code)
459                 *err_code = err;
460
461         EM_DEBUG_FUNC_END();
462
463         return ret;
464 }
465
466
467 INTERNAL_FUNC int emdaemon_get_account(char *multi_user_name, int account_id, int pulloption, email_account_t* account, int* err_code)
468 {
469         EM_DEBUG_FUNC_BEGIN("account_id[%d], pulloption [%d], account[%p], err_code[%p]", account_id, pulloption, account, err_code);
470
471         /*  default variable */
472         int ret = false;
473         int err = EMAIL_ERROR_NONE;
474         emstorage_account_tbl_t *account_tbl = NULL;
475
476         if (!account)  {
477                 EM_DEBUG_EXCEPTION("account_id[%d], account[%p]", account_id, account);
478                 err = EMAIL_ERROR_INVALID_PARAM;
479                 goto FINISH_OFF;
480         }
481
482         if (!emstorage_get_account_by_id(multi_user_name, account_id, pulloption, &account_tbl, true, &err)) {
483                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
484                 goto FINISH_OFF;
485         }
486
487         em_convert_account_tbl_to_account(account_tbl, account);
488
489         ret = true;
490
491 FINISH_OFF:
492         if(account_tbl)
493                 emstorage_free_account(&account_tbl, 1, NULL);
494         if (err_code)
495                 *err_code = err;
496         EM_DEBUG_FUNC_END("ret [%d]", ret);
497         return ret;
498 }
499
500 INTERNAL_FUNC int emdaemon_get_account_list(char *multi_user_name, email_account_t** account_list, int* count, int* err_code)
501 {
502         EM_DEBUG_FUNC_BEGIN("account_list[%p], count[%p], err_code[%p]", account_list, count, err_code);
503
504         int ret = false, err = EMAIL_ERROR_NONE, i = 0;
505         emstorage_account_tbl_t *account_tbl_array = NULL;
506
507         if (!account_list || !count)  {
508                 err = EMAIL_ERROR_INVALID_PARAM;
509                 goto FINISH_OFF;
510         }
511
512         *count = 1000;
513
514         if (!emstorage_get_account_list(multi_user_name, count, &account_tbl_array, true, true, &err))  {
515                 EM_DEBUG_EXCEPTION("emstorage_get_account_list failed [%d]", err);
516                 goto FINISH_OFF;
517         }
518
519         if(account_tbl_array && (*count) > 0) {
520                 *account_list = (email_account_t*)em_malloc(sizeof(email_account_t) * (*count));
521                 if(!*account_list) {
522                         EM_DEBUG_EXCEPTION("allocation failed [%d]", err);
523                         goto FINISH_OFF;
524                 }
525
526                 for(i = 0 ; i < (*count); i++)
527                         em_convert_account_tbl_to_account(account_tbl_array + i, (*account_list) + i);
528         }
529
530         ret = true;
531
532 FINISH_OFF:
533         if(account_tbl_array)
534                 emstorage_free_account(&account_tbl_array, (*count), NULL);
535
536         if (err_code != NULL)
537                 *err_code = err;
538         EM_DEBUG_FUNC_END("ret [%d]", ret);
539         return ret;
540 }
541
542
543 INTERNAL_FUNC int emdaemon_free_account(email_account_t** account_list, int count, int* err_code)
544 {
545         EM_DEBUG_FUNC_BEGIN();
546         return emcore_free_account_list(account_list, count, err_code);
547 }
548
549
550 INTERNAL_FUNC int emdaemon_get_filter(char *multi_user_name, int filter_id, email_rule_t** filter_info, int* err_code)
551 {
552         EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
553
554         if (!filter_info) {
555                 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
556
557                 if (err_code != NULL)
558                         *err_code = EMAIL_ERROR_INVALID_PARAM;
559                 return false;
560         }
561
562         /*  default variable */
563         int ret = false;
564         int err = EMAIL_ERROR_NONE;
565
566         if (!emstorage_get_rule_by_id(multi_user_name, filter_id, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
567                 EM_DEBUG_EXCEPTION(" emstorage_get_rule_by_id failed [%d]", err);
568                 goto FINISH_OFF;
569         }
570
571         ret = true;
572
573 FINISH_OFF:
574         if (err_code)
575                 *err_code = err;
576
577         EM_DEBUG_FUNC_END();
578         return ret;
579 }
580
581 INTERNAL_FUNC int emdaemon_get_filter_list(char *multi_user_name, email_rule_t** filter_info, int* count, int* err_code)
582 {
583         EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%p], err_code[%p]", filter_info, count, err_code);
584
585         /*  default variable */
586         int ret = false;
587         int err = EMAIL_ERROR_NONE;
588         int is_completed;
589
590         if (!filter_info || !count)  {
591                 EM_DEBUG_EXCEPTION(" filter_info[%p], count[%p]", filter_info, count);
592                 err = EMAIL_ERROR_INVALID_PARAM;
593                 goto FINISH_OFF;
594         }
595
596         *count = 1000;
597
598         if (!emstorage_get_rule(multi_user_name, ALL_ACCOUNT, 0, 0, count, &is_completed, (emstorage_rule_tbl_t**)filter_info, true, &err))  {
599                 EM_DEBUG_EXCEPTION(" emstorage_get_rule failed [%d]", err);
600                 goto FINISH_OFF;
601         }
602
603         ret = true;
604
605 FINISH_OFF:
606         if (err_code)
607                 *err_code = err;
608         EM_DEBUG_FUNC_END();
609         return ret;
610 }
611
612 INTERNAL_FUNC int emdaemon_find_filter(char *multi_user_name, email_rule_t* filter_info, int* err_code)
613 {
614         EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
615
616         /*  default variable */
617         int ret = false;
618         int err = EMAIL_ERROR_NONE;
619
620         if (!filter_info) {
621                 EM_DEBUG_EXCEPTION(" filter_info[%p]", filter_info);
622                 err = EMAIL_ERROR_INVALID_PARAM;
623                 goto FINISH_OFF;
624         }
625
626         if (filter_info->faction == EMAIL_FILTER_MOVE && filter_info->target_mailbox_id <= 0) {
627                 EM_DEBUG_EXCEPTION("filter_info->faction[%d], filter_info->target_mailbox_id[%d]", filter_info->faction, filter_info->target_mailbox_id);
628                 err = EMAIL_ERROR_INVALID_FILTER;
629                 goto FINISH_OFF;
630         }
631
632         filter_info->account_id = ALL_ACCOUNT;          /*  MUST BE */
633
634         if (!emstorage_find_rule(multi_user_name, (emstorage_rule_tbl_t*)filter_info, true, &err)) {
635                 EM_DEBUG_EXCEPTION("emstorage_find_rule failed [%d]", err);
636                 goto FINISH_OFF;
637         } else {
638                 if (err == EMAIL_ERROR_FILTER_NOT_FOUND) {
639                         EM_DEBUG_LOG("EMAIL_ERROR_FILTER_NOT_FOUND");
640                         err = EMAIL_ERROR_FILTER_NOT_FOUND;
641                         goto FINISH_OFF;
642                 }
643         }
644
645         ret = true;
646
647 FINISH_OFF:
648
649         if (err_code)
650                 *err_code = err;
651         EM_DEBUG_FUNC_END();
652         return ret;
653 }
654
655 INTERNAL_FUNC int emdaemon_add_filter(char *multi_user_name, email_rule_t* filter_info)
656 {
657         EM_DEBUG_FUNC_BEGIN("filter_info[%p]", filter_info);
658
659         /*  default variable */
660         int err = EMAIL_ERROR_NONE;
661         if (!filter_info) {
662                 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
663                 err = EMAIL_ERROR_INVALID_PARAM;
664                 goto FINISH_OFF;
665         }
666
667         err = emcore_add_rule(multi_user_name, filter_info);
668         if (err != EMAIL_ERROR_NONE) {
669                 EM_DEBUG_EXCEPTION("emcore_add_rule failed : [%d]", err);
670                 goto FINISH_OFF;
671         }
672
673 FINISH_OFF:
674
675         EM_DEBUG_FUNC_END();
676         return err;
677 }
678
679 INTERNAL_FUNC int emdaemon_update_filter(char *multi_user_name, int filter_id, email_rule_t* filter_info, int* err_code)
680 {
681         EM_DEBUG_FUNC_BEGIN("filter_id[%d], filter_info[%p], err_code[%p]", filter_id, filter_info, err_code);
682
683         /*  default variable */
684         int ret = false;
685         int err = EMAIL_ERROR_NONE;
686
687         if ((filter_id <= 0) || !filter_info)  {
688                 EM_DEBUG_EXCEPTION("filter_id[%d], filter_info[%p]", filter_id, filter_info);
689                 err = EMAIL_ERROR_INVALID_PARAM;
690                 goto FINISH_OFF;
691         }
692
693         if (!emdaemon_check_filter_id(multi_user_name, filter_id, &err))  {
694                 EM_DEBUG_EXCEPTION("emdaemon_check_filter_id falied [%d]", err);
695                 goto FINISH_OFF;
696         }
697
698         err = emcore_update_rule(multi_user_name, filter_id, filter_info);
699         if (err != EMAIL_ERROR_NONE) {
700             EM_DEBUG_EXCEPTION("emcore_update_rule failed : [%d]", err);
701             goto FINISH_OFF;
702         }
703
704         ret = true;
705
706 FINISH_OFF:
707         if (err_code)
708                 *err_code = err;
709         EM_DEBUG_FUNC_END();
710         return ret;
711 }
712
713 INTERNAL_FUNC int emdaemon_delete_filter(char *multi_user_name, int filter_id, int* err_code)
714 {
715         EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
716
717         /*  default variable */
718         int ret = false;
719         int err = EMAIL_ERROR_NONE;
720
721         if (filter_id <= 0)  {
722                 EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
723                 err = EMAIL_ERROR_INVALID_PARAM;
724                 goto FINISH_OFF;
725         }
726
727         err = emcore_delete_rule(multi_user_name, filter_id);
728         if (err != EMAIL_ERROR_NONE) {
729                 EM_DEBUG_EXCEPTION("emcore_delete_rule failed : [%d]", err);
730                 goto FINISH_OFF;
731         }
732
733         ret = true;
734
735 FINISH_OFF:
736
737         if (err_code)
738                 *err_code = err;
739
740         EM_DEBUG_FUNC_END();
741         return ret;
742 }
743
744 INTERNAL_FUNC int emdaemon_free_filter(email_rule_t** filter_info, int count, int* err_code)
745 {
746         EM_DEBUG_FUNC_BEGIN("filter_info[%p], count[%d], err_code[%p]", filter_info, count, err_code);
747
748         /*  default variable */
749         int ret = false;
750         int err = EMAIL_ERROR_NONE;
751
752         if (count > 0)  {
753                 if (!filter_info || !*filter_info)  {
754                         EM_DEBUG_EXCEPTION(" filter_info[%p], count[%d]", filter_info, count);
755                         err = EMAIL_ERROR_INVALID_PARAM;
756                         goto FINISH_OFF;
757                 }
758
759                 email_rule_t* p = *filter_info;
760                 int i;
761
762                 for (i = 0; i < count; i++)  {
763                         EM_SAFE_FREE(p[i].value);
764                 }
765
766                 EM_SAFE_FREE(p); *filter_info  = NULL;
767         }
768
769         ret = true;
770
771 FINISH_OFF:
772         if (err_code)
773                 *err_code = err;
774         EM_DEBUG_FUNC_END();
775         return ret;
776 }
777
778 INTERNAL_FUNC int emdaemon_apply_filter(char *multi_user_name, int filter_id, int *err_code)
779 {
780         EM_DEBUG_FUNC_BEGIN("filter_id[%d, err_code[%p]", filter_id, err_code);
781
782         /*  default variable */
783         int ret = false;
784         int err = EMAIL_ERROR_NONE;
785         emstorage_rule_tbl_t *filter_info = NULL;
786
787         if (filter_id <= 0)  {
788                 EM_DEBUG_EXCEPTION(" fliter_id[%d]", filter_id);
789                 err = EMAIL_ERROR_INVALID_PARAM;
790                 goto FINISH_OFF;
791         }
792
793         if (!emstorage_get_rule_by_id(multi_user_name, filter_id, &filter_info, false, &err)) {
794                 EM_DEBUG_EXCEPTION("emstorage_get_rule_by_id failed : [%d]", err);
795                 goto FINISH_OFF;
796         }
797
798         if (!emcore_mail_filter_by_rule(multi_user_name, (email_rule_t *)filter_info, &err))  {
799                 EM_DEBUG_EXCEPTION(" emstorage_mail_filter_by_rule failed [%d]", err);
800                 goto FINISH_OFF;
801         }
802
803         ret = true;
804
805 FINISH_OFF:
806         if (err_code)
807                 *err_code = err;
808         EM_DEBUG_FUNC_END();
809         return ret;
810 }
811
812
813 /* ----- internal functions --------------------------------------------*/
814
815 INTERNAL_FUNC int emdaemon_insert_accountinfo_to_contact(email_account_t* account)
816 {
817         EM_DEBUG_FUNC_BEGIN();
818
819         if(!account)
820                 return false;
821
822         int ret = false;
823         EM_DEBUG_FUNC_END();
824         return ret;
825 }
826
827 INTERNAL_FUNC int emdaemon_update_accountinfo_to_contact(email_account_t* old_account, email_account_t* new_account)
828 {
829         EM_DEBUG_FUNC_BEGIN();
830
831         if(!old_account || !new_account)
832                 return false;
833
834         int ret = false;
835         EM_DEBUG_FUNC_END();
836         return ret;
837 }
838
839 INTERNAL_FUNC int emdaemon_query_smtp_mail_size_limit(char *multi_user_name, int account_id, int *handle, int* err_code)
840 {
841         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%p], err_code[%p]", account_id, handle, err_code);
842
843         int ret = false;
844         int err = EMAIL_ERROR_NONE;
845         email_event_t *event_data = NULL;
846
847         if (account_id < 1) {
848                 EM_DEBUG_EXCEPTION(" account_id[%d]", account_id);
849                 err = EMAIL_ERROR_INVALID_PARAM;
850                 goto FINISH_OFF;
851         }
852
853         event_data = em_malloc(sizeof(email_event_t));
854         event_data->type = EMAIL_EVENT_QUERY_SMTP_MAIL_SIZE_LIMIT;
855         event_data->event_param_data_1 = NULL;
856         event_data->event_param_data_3 = NULL;
857         event_data->account_id = account_id;
858    event_data->multi_user_name = EM_SAFE_STRDUP(multi_user_name);
859
860         if (!emcore_insert_event(event_data, (int*)handle, &err)) {
861                 EM_DEBUG_EXCEPTION(" emcore_insert_event falied [%d]", err);
862                 goto FINISH_OFF;
863         }
864
865         ret = true;
866
867 FINISH_OFF:
868
869         if (ret == false && event_data) {
870                 emcore_free_event(event_data);
871                 EM_SAFE_FREE(event_data);
872         }
873
874         if (err_code)
875                 *err_code = err;
876
877         EM_DEBUG_FUNC_END();
878
879         return ret;
880 }
881
882 /* EOF */