1026eae32cd6e63e78940d27359a672660bbffa3
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-etc.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 /******************************************************************************
25  * File: emf-etc.c
26  * Desc: email-daemon Etc Implementations
27  *
28  * Auth:
29  *
30  * History:
31  *    2006.08.16 : created
32  *****************************************************************************/
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <vconf.h>
37 #include "email-daemon.h"
38 #include "email-daemon-account.h"
39 #include "email-debug-log.h"
40 #include "email-internal-types.h"
41 #include "email-core-account.h"
42 #include "email-core-event.h"
43 #include "email-core-utils.h"
44 #include "email-core-alarm.h"
45 #include "email-core-smtp.h"
46 #include "email-utilities.h"
47 #include "email-storage.h"
48
49
50 int emdaemon_register_event_callback(email_action_t action, email_event_callback callback, void* event_data)
51 {
52         return emcore_register_event_callback(action, callback, event_data);
53 }
54
55 int emdaemon_unregister_event_callback(email_action_t action, email_event_callback callback)
56 {
57         return emcore_unregister_event_callback(action, callback);
58 }
59
60 INTERNAL_FUNC void emdaemon_get_event_queue_status(int* on_sending, int* on_receiving)
61 {
62         emcore_get_event_queue_status(on_sending, on_receiving);
63 }
64
65 INTERNAL_FUNC int emdaemon_get_pending_job(email_action_t action, int account_id, int mail_id, email_event_status_type_t* status)
66 {
67         EM_DEBUG_FUNC_BEGIN("action[%d], account_id[%d], mail_id[%d]", action, account_id, mail_id);
68
69         return emcore_get_pending_event(action, account_id, mail_id, status);
70 }
71
72 INTERNAL_FUNC int emdaemon_cancel_job(int account_id, int handle, int* err_code)
73 {
74         EM_DEBUG_FUNC_BEGIN("account_id[%d], handle[%d], err_code[%p]", account_id, handle, err_code);
75         
76         int ret = false;
77         int err = EMAIL_ERROR_NONE;
78         
79         
80         if (!emcore_cancel_thread(handle, NULL, &err))  {
81                 EM_DEBUG_EXCEPTION("emcore_cancel_thread failed [%d]", err);
82                 goto FINISH_OFF;
83         }
84         
85         ret = true;
86         
87 FINISH_OFF:
88         if (err_code != NULL)
89                 *err_code = err;
90         EM_DEBUG_FUNC_END();
91         return ret;
92 }
93
94
95 INTERNAL_FUNC int emdaemon_cancel_sending_mail_job(int account_id, int mail_id, int* err_code)
96 {
97         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
98         
99         int ret = false;
100         int err = EMAIL_ERROR_NONE;     
101         int handle = 0;
102         email_account_t* ref_account = NULL;
103         
104         if (account_id <= 0)  {
105                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d]", account_id, mail_id);
106                 err = EMAIL_ERROR_INVALID_PARAM;
107                 goto FINISH_OFF;
108         }
109
110 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
111
112         /*      h.gahlaut@samsung.com: Moved this code from email_cancel_sending_mail API to email-service engine
113                 since this code has update DB operation which is failing in context of email application process 
114                 with an sqlite error -> sqlite3_step fail:8 */
115                 
116         /*      which means #define SQLITE_READONLY   8 */  /* Attempt to write a readonly database */ 
117         emstorage_mail_tbl_t *mail_tbl_data = NULL;
118
119         if (!emstorage_get_mail_by_id(mail_id, &mail_tbl_data, false, &err))  {
120                 EM_DEBUG_EXCEPTION("emcore_get_mail failed [%d]", err);
121                 goto FINISH_OFF;
122         }
123
124         if (mail_tbl_data) {
125                 if (mail_tbl_data->save_status == EMAIL_MAIL_STATUS_SEND_CANCELED) {
126                         EM_DEBUG_EXCEPTION(">>>> EMAIL_MAIL_STATUS_SEND_CANCELED Already set for Mail ID [ %d ]", mail_id);
127                         goto FINISH_OFF;
128                 }
129                 else {                  
130                         mail_tbl_data->save_status = EMAIL_MAIL_STATUS_SEND_CANCELED;
131
132                         if(!emstorage_set_field_of_mails_with_integer_value(mail_tbl_data->account_id, &mail_id, 1, "save_status", EMAIL_MAIL_STATUS_SEND_CANCELED, true, &err)) {
133                                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]",err);
134                                 goto FINISH_OFF;
135                         }
136                 }
137
138
139         }
140
141         if ((err = emcore_delete_alram_data_by_reference_id(EMAIL_ALARM_CLASS_SCHEDULED_SENDING, mail_id)) != EMAIL_ERROR_NONE) {
142                 EM_DEBUG_LOG("emcore_delete_alram_data_by_reference_id failed [%d]",err);
143         }
144
145 #endif
146
147         if(!emcore_get_handle_by_mailId_from_transaction_info(mail_id , &handle )) {
148                 EM_DEBUG_EXCEPTION("emcore_get_handle_by_mailId_from_transaction_info failed for mail_id[%d]", mail_id);
149                 err = EMAIL_ERROR_HANDLE_NOT_FOUND;
150                 goto FINISH_OFF;
151         }
152
153         if (!(ref_account = emcore_get_account_reference(account_id)))  {
154                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
155                 err = EMAIL_ERROR_INVALID_ACCOUNT;
156                 goto FINISH_OFF;
157         }
158
159         if (!emcore_cancel_send_mail_thread(handle, NULL, &err))  {
160                 EM_DEBUG_EXCEPTION("emcore_cancel_send_mail_thread failed [%d]", err);
161                 goto FINISH_OFF;
162         }
163         
164         if(!emcore_delete_transaction_info_by_mailId(mail_id))
165                 EM_DEBUG_EXCEPTION("emcore_delete_transaction_info_by_mailId failed for mail_id[%d]", mail_id);
166         
167         ret = true;
168         
169 FINISH_OFF:
170         if(err_code != NULL)
171                 *err_code = err;
172         
173         if (ref_account) {
174                 emcore_free_account(ref_account);
175                 EM_SAFE_FREE(ref_account);
176         }
177
178 #ifdef __FEATURE_PROGRESS_IN_OUTBOX__
179         if(!emstorage_free_mail(&mail_tbl_data, 1, &err))
180                 EM_DEBUG_EXCEPTION("emcore_free_mail Failed [%d ]", err);       
181
182 #endif
183         EM_DEBUG_FUNC_END();
184         return ret;
185 }       
186
187 static char *_make_time_string_to_time_t(time_t time)
188 {
189         char *time_string = NULL;
190         struct tm *struct_time = NULL;
191
192         if (!time) {
193                 EM_DEBUG_EXCEPTION("Invalid paramter");
194                 return NULL;
195         }
196
197         time_string = em_malloc(MAX_DATETIME_STRING_LENGTH);
198         if (time_string == NULL) {
199                 EM_DEBUG_EXCEPTION("em_malloc failed");
200                 return NULL;
201         }
202
203         struct_time = localtime(&time);
204         SNPRINTF(time_string, MAX_DATETIME_STRING_LENGTH, "%d/%d/%d", struct_time->tm_mon + 1, struct_time->tm_mday, struct_time->tm_year + 1900);
205
206         EM_DEBUG_LOG("time string = [%s]", time_string);
207         return time_string;     
208 }
209
210 static char *_make_criteria_to_search_filter(email_search_filter_t *search_filter, int search_filter_count, int *err_code)
211 {
212         EM_DEBUG_FUNC_BEGIN("search_filter : [%p], search_filter_count : [%d]", search_filter, search_filter_count);
213
214         int i = 0;
215         int err = EMAIL_ERROR_NONE;
216         char *criteria = NULL;
217         char *temp_criteria = NULL;
218         char *time_string = NULL;
219         
220         if (search_filter == NULL || search_filter_count < 0) {
221                 EM_DEBUG_EXCEPTION("Invalid paramter");
222                 err = EMAIL_ERROR_INVALID_PARAM;
223                 goto FINISH_OFF;
224         }
225
226         criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY * search_filter_count);
227         if (criteria == NULL) {
228                 EM_DEBUG_EXCEPTION("em_malloc failed");
229                 err = EMAIL_ERROR_OUT_OF_MEMORY;
230                 goto FINISH_OFF;
231         }
232
233         temp_criteria = (char *)em_malloc(STRING_LENGTH_FOR_DISPLAY);
234         if (temp_criteria == NULL) {
235                 EM_DEBUG_EXCEPTION("em_malloc failed");
236                 err = EMAIL_ERROR_OUT_OF_MEMORY;
237                 goto FINISH_OFF;
238         }
239
240         for (i = 0; i < search_filter_count; i++) {
241                 EM_DEBUG_LOG("search_filter_type [%d]", search_filter[i].search_filter_type);
242                 memset(temp_criteria, 0x00, STRING_LENGTH_FOR_DISPLAY);
243
244                 switch (search_filter[i].search_filter_type) {
245                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
246                 case EMAIL_SEARCH_FILTER_TYPE_UID :
247                 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
248                 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
249                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
250                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
251                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
252                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
253                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
254                 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
255                         EM_DEBUG_LOG("integer_type_key_value [%d]", search_filter[i].search_filter_key_value.integer_type_key_value);
256                         break;
257
258                 case EMAIL_SEARCH_FILTER_TYPE_BCC :
259                 case EMAIL_SEARCH_FILTER_TYPE_CC :
260                 case EMAIL_SEARCH_FILTER_TYPE_TO :
261                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
262                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
263                         break;
264
265                 case EMAIL_SEARCH_FILTER_TYPE_FROM :
266                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
267                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "from %s ", search_filter[i].search_filter_key_value.string_type_key_value);
268                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
269                         break;
270
271                 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
272                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
273                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "subject %s ", search_filter[i].search_filter_key_value.string_type_key_value);
274                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
275                         break;
276
277                 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
278                         EM_DEBUG_LOG("string_type_key_value [%s]", search_filter[i].search_filter_key_value.string_type_key_value);
279                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "keyword %s ", search_filter[i].search_filter_key_value.string_type_key_value);
280                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
281                         break;
282
283                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
284                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
285                         time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
286                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "before %s ", time_string);
287                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
288                         break;
289         
290                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
291                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
292                         break;
293
294                 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
295                         EM_DEBUG_LOG("time_type_key_value [%d]", search_filter[i].search_filter_key_value.time_type_key_value);
296                         time_string = _make_time_string_to_time_t(search_filter[i].search_filter_key_value.time_type_key_value);
297                         SNPRINTF(temp_criteria, STRING_LENGTH_FOR_DISPLAY, "since %s ", time_string);
298                         strncat(criteria, temp_criteria, EM_SAFE_STRLEN(temp_criteria));
299                         break;
300
301                 default :
302                         EM_DEBUG_EXCEPTION("Invalid list_filter_item_type [%d]", search_filter);
303                         err = EMAIL_ERROR_INVALID_PARAM;
304                         goto FINISH_OFF;
305                 }
306                 EM_SAFE_FREE(time_string); /*prevent 26258*/
307         }
308
309 FINISH_OFF:
310         
311         EM_SAFE_FREE(temp_criteria);
312         EM_SAFE_FREE(time_string);
313
314         if (err_code != NULL)
315                 *err_code = err;
316                 
317         EM_DEBUG_FUNC_END();
318         return criteria;
319 }
320
321 INTERNAL_FUNC int emdaemon_search_mail_on_server(int input_account_id, int input_mailbox_id, email_search_filter_t *input_search_filter, int input_search_filter_count, unsigned int *output_handle, int *err_code)
322 {
323         EM_DEBUG_FUNC_BEGIN("input_account_id [%d], mailbox_id [%d], input_search_filter [%p], input_search_filter_count [%d], output_handle [%p]", input_account_id, input_mailbox_id, input_search_filter, input_search_filter_count, output_handle);
324         int error = EMAIL_ERROR_NONE;
325         int ret = false;
326         char *criteria = NULL;
327         
328         if (input_mailbox_id == 0 || input_account_id < 0) {
329                 EM_DEBUG_EXCEPTION("Invalid parameter");
330                 error = EMAIL_ERROR_INVALID_PARAM;
331                 return false;
332         }
333
334         email_event_t event_data;
335
336         memset(&event_data, 0x00, sizeof(email_event_t));
337
338         criteria = _make_criteria_to_search_filter(input_search_filter, input_search_filter_count, &error);
339         if (criteria == NULL) {
340                 EM_DEBUG_EXCEPTION("_make_criteria_to_search_filter failed");
341                 goto FINISH_OFF;
342         }
343                 
344         event_data.type = EMAIL_EVENT_SEARCH_ON_SERVER;
345         event_data.account_id = input_account_id;
346         event_data.event_param_data_1 = EM_SAFE_STRDUP(criteria);
347         event_data.event_param_data_4 = input_mailbox_id;
348
349         if (!emcore_insert_event(&event_data, (int *)output_handle, &error)) {
350                 EM_DEBUG_EXCEPTION("emcore_insert_event failed [%d]", error);
351                 error = EMAIL_ERROR_NONE;
352                 goto FINISH_OFF;
353         }
354
355         ret = true;
356
357 FINISH_OFF:
358         if (!ret) {
359                 EM_SAFE_FREE(event_data.event_param_data_1);
360         }
361
362         EM_SAFE_FREE(criteria);
363         
364         if (err_code != NULL)
365                 *err_code = error;
366
367         EM_DEBUG_FUNC_END("error [%d]", error);
368         return ret;
369 }
370
371 INTERNAL_FUNC int emdaemon_reschedule_sending_mail()
372 {
373         EM_DEBUG_FUNC_BEGIN();
374         int err = EMAIL_ERROR_NONE;
375         char *conditional_clause_string = NULL;
376         email_list_filter_t filter_list[3];
377         email_mail_list_item_t *result_mail_list = NULL;
378         int filter_rule_count = 3;
379         int result_mail_count = 0;
380         int i = 0;
381
382         memset(filter_list, 0 , sizeof(email_list_filter_t) * filter_rule_count);
383
384         filter_list[0].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
385         filter_list[0].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SCHEDULED_SENDING_TIME;
386         filter_list[0].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_NOT_EQUAL;
387         filter_list[0].list_filter_item.rule.key_value.integer_type_value  = 0;
388
389         filter_list[1].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_OPERATOR;
390         filter_list[1].list_filter_item.operator_type                      = EMAIL_LIST_FILTER_OPERATOR_AND;
391
392         filter_list[2].list_filter_item_type                               = EMAIL_LIST_FILTER_ITEM_RULE;
393         filter_list[2].list_filter_item.rule.target_attribute              = EMAIL_MAIL_ATTRIBUTE_SAVE_STATUS;
394         filter_list[2].list_filter_item.rule.rule_type                     = EMAIL_LIST_FILTER_RULE_EQUAL;
395         filter_list[2].list_filter_item.rule.key_value.integer_type_value  = EMAIL_MAIL_STATUS_SEND_SCHEDULED;
396
397         /* Get scheduled mail list */
398         if( (err = emstorage_write_conditional_clause_for_getting_mail_list(filter_list, filter_rule_count, NULL, 0, -1, -1, &conditional_clause_string)) != EMAIL_ERROR_NONE) {
399                 EM_DEBUG_EXCEPTION("emstorage_write_conditional_clause_for_getting_mail_list failed[%d]", err);
400                 goto FINISH_OFF;
401         }
402
403         EM_DEBUG_LOG("conditional_clause_string[%s].", conditional_clause_string);
404
405         if(!emstorage_query_mail_list(conditional_clause_string, true, &result_mail_list, &result_mail_count, &err) && !result_mail_list) {
406                 EM_DEBUG_EXCEPTION("emstorage_query_mail_list [%d]", err);
407                 goto FINISH_OFF;
408         }
409
410         /* Add alarm for scheduled mail */
411         for(i = 0; i < result_mail_count; i++) {
412                 if((err = emcore_schedule_sending_mail(result_mail_list[i].mail_id, result_mail_list[i].scheduled_sending_time)) != EMAIL_ERROR_NONE) {
413                         EM_DEBUG_EXCEPTION("emcore_schedule_sending_mail failed [%d]", err);
414                         goto FINISH_OFF;
415                 }
416         }
417
418 FINISH_OFF:
419         EM_SAFE_FREE(result_mail_list);
420
421         EM_DEBUG_FUNC_END("err [%d]", err);
422         return err;
423 }
424
425 INTERNAL_FUNC int emdaemon_clear_all_mail_data(int* err_code)
426 {
427         EM_DEBUG_FUNC_BEGIN();
428         
429         int ret = false;
430         int error = EMAIL_ERROR_NONE;
431         
432         if (emdaemon_initialize(&error)) {
433                 if (!emstorage_clear_mail_data(true, &error))
434                         EM_DEBUG_EXCEPTION("emstorage_clear_mail_data failed [%d]", error);
435         }
436         else {
437                 EM_DEBUG_EXCEPTION("emdaemon_initialize failed [%d]", error);
438                 if (err_code)
439                         *err_code = error;
440                 return false;
441         }
442
443         emcore_display_unread_in_badge();
444
445         ret = true;
446
447         if (!emstorage_create_table(EMAIL_CREATE_DB_NORMAL, &error)) 
448                 EM_DEBUG_EXCEPTION("emstorage_create_table failed [%d]", error);
449         
450         emdaemon_finalize(&error);
451         
452         if (err_code)
453                 *err_code = error;
454         EM_DEBUG_FUNC_END();
455     return ret;
456 }
457         
458 /* --------------------------------------------------------------------------------*/