c86640ec78f16d0027b90eff80486bb386dcc017
[platform/core/messaging/email-service.git] / email-api / email-api-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  * This file contains the data structures and interfaces needed for application,
26  * to interact with email-service.
27  * @file                email-api-etc.c
28  * @brief               This file contains the data structures and interfaces of functionalities provided by email-service .
29  */
30
31 #include "email-types.h"
32 #include "email-ipc.h"
33 #include "email-debug-log.h"
34 #include "email-core-utils.h"
35 #include "email-core-smtp.h"
36 #include "email-core-mime.h"
37 #include "email-convert.h"
38 #include "email-utilities.h"
39 #include "email-core-gmime.h"
40
41 EXPORT_API int email_show_user_message(int id, email_action_t action, int error_code)
42 {
43         EM_DEBUG_API_BEGIN("id[%d] action[%d] error_code[%d]", id, action, error_code);
44         int err = EMAIL_ERROR_NONE;
45
46         if (id < 0 || action < 0) {
47                 EM_DEBUG_LOG("EMAIL_ERROR_INVALID_PARAM");
48                 return EMAIL_ERROR_INVALID_PARAM;
49         }
50
51         HIPC_API hAPI = emipc_create_email_api(_EMAIL_API_SHOW_USER_MESSAGE);
52
53         /* id */
54         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&id, sizeof(int))) {
55                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
56                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
57         }
58
59         /* action */
60         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&action, sizeof(int))) {
61                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
62                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
63         }
64
65         /* error_code */
66         if (!emipc_add_parameter(hAPI, ePARAMETER_IN, (char*)&error_code, sizeof(int))) {
67                 EM_DEBUG_LOG("emipc_add_parameter failed  ");
68                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_NULL_VALUE);
69         }
70
71         if (!emipc_execute_proxy_api(hAPI))  {
72                 EM_DEBUG_LOG("ipcProxy_ExecuteAsyncAPI failed");
73                 EM_PROXY_IF_NULL_RETURN_VALUE(0, hAPI, EMAIL_ERROR_IPC_SOCKET_FAILURE);
74         }
75
76         emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err);
77
78         emipc_destroy_email_api(hAPI);
79         hAPI = NULL;
80
81         EM_DEBUG_API_END("err[%d]", err);
82         return err;
83 }
84
85 EXPORT_API int email_parse_mime_file(char *eml_file_path, email_mail_data_t **output_mail_data,
86                                                                         email_attachment_data_t **output_attachment_data, int *output_attachment_count)
87 {
88         EM_DEBUG_API_BEGIN("eml_file_path[%p] output_mail_data[%p] output_attachment_data[%p]",
89                                                 eml_file_path, output_mail_data, output_attachment_data);
90         int err = EMAIL_ERROR_NONE;
91
92         EM_IF_NULL_RETURN_VALUE(eml_file_path, EMAIL_ERROR_INVALID_PARAM);
93
94         if (!emcore_parse_mime_file_to_mail(eml_file_path, output_mail_data, output_attachment_data,
95                                                                                 output_attachment_count, &err) || !*output_mail_data)
96                 EM_DEBUG_EXCEPTION("emcore_parse_mime_file_to_mail failed [%d]", err);
97
98         EM_DEBUG_API_END("err[%d]", err);
99         return err;
100 }
101
102 EXPORT_API int email_write_mime_file(email_mail_data_t *input_mail_data,
103                                                                         email_attachment_data_t *input_attachment_data,
104                                                                         int input_attachment_count, char **output_file_path)
105 {
106         EM_DEBUG_API_BEGIN("input_mail_data[%p] input_attachment_data[%p] input_attachment_count[%d]",
107                                                 input_mail_data, input_attachment_data, input_attachment_count);
108
109         int err = EMAIL_ERROR_NONE;
110         int ret_from_ipc = EMAIL_ERROR_NONE;
111         HIPC_API hAPI = NULL;
112
113         EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
114         EM_IF_NULL_RETURN_VALUE(output_file_path, EMAIL_ERROR_INVALID_PARAM);
115
116         hAPI = emipc_create_email_api(_EMAIL_API_WRITE_MIME_FILE);
117         if (!hAPI) {
118                 EM_DEBUG_EXCEPTION("emipc_create_email_api failed");
119                 err = EMAIL_ERROR_NULL_VALUE;
120                 goto FINISH_OFF;
121         }
122
123         if (!emipc_execute_proxy_api(hAPI)) {
124                 EM_DEBUG_EXCEPTION("emipc_execute_proxy_api failed");
125                 err = EMAIL_ERROR_IPC_SOCKET_FAILURE;
126                 goto FINISH_OFF;
127         }
128
129
130         if ((ret_from_ipc = emipc_get_parameter(hAPI, ePARAMETER_OUT, 0, sizeof(int), &err)) != EMAIL_ERROR_NONE) {
131                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed:[%d]", ret_from_ipc);
132                 err = ret_from_ipc;
133                 goto FINISH_OFF;
134         }
135
136         if (!emcore_make_rfc822_file(NULL, input_mail_data, input_attachment_data, input_attachment_count, false,
137                                                                 output_file_path,  &err)) {
138                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file failed : [%d]", err);
139         }
140
141 FINISH_OFF:
142
143         if (hAPI)
144                 emipc_destroy_email_api(hAPI);
145
146         EM_DEBUG_API_END("err[%d]", err);
147         return err;
148 }
149
150 EXPORT_API int email_delete_parsed_data(email_mail_data_t *input_mail_data)
151 {
152         EM_DEBUG_API_BEGIN("mail_data[%p]", input_mail_data);
153         int err = EMAIL_ERROR_NONE;
154     char *multi_user_name = NULL;
155
156         EM_IF_NULL_RETURN_VALUE(input_mail_data, EMAIL_ERROR_INVALID_PARAM);
157
158     if ((err = emipc_get_user_name(&multi_user_name)) != EMAIL_ERROR_NONE) {
159         EM_DEBUG_EXCEPTION("emipc_get_user_name failed : [%d]", err);
160         return err;
161     }
162
163         if (!emcore_delete_parsed_data(multi_user_name, input_mail_data, &err))
164                 EM_DEBUG_EXCEPTION("emcore_delete_parsed_data failed [%d]", err);
165
166     EM_SAFE_FREE(multi_user_name);
167
168         EM_DEBUG_API_END("err[%d]", err);
169         return err;
170 }
171
172 EXPORT_API int email_get_mime_entity(char *mime_path, char **mime_entity)
173 {
174         EM_DEBUG_API_BEGIN("mime_path[%p]", mime_path);
175         int err = EMAIL_ERROR_NONE;
176
177         EM_IF_NULL_RETURN_VALUE(mime_path, EMAIL_ERROR_INVALID_PARAM);
178
179         if (!emcore_get_mime_entity(mime_path, mime_entity, &err))
180                 EM_DEBUG_EXCEPTION("emcore_get_mime_entity failed [%d]", err);
181
182         EM_DEBUG_API_END();
183         return err;
184 }
185
186 EXPORT_API int email_verify_email_address(char *input_email_address)
187 {
188         EM_DEBUG_API_BEGIN("input_email_address[%p]", input_email_address);
189         int err = EMAIL_ERROR_NONE;
190
191         EM_IF_NULL_RETURN_VALUE(input_email_address, EMAIL_ERROR_INVALID_PARAM);
192
193         if ((err = em_verify_email_address(input_email_address)) != EMAIL_ERROR_NONE)
194                 EM_DEBUG_EXCEPTION("em_verify_email_address failed [%d]", err);
195
196         EM_DEBUG_API_END("err [%d]", err);
197         return err;
198 }
199
200 EXPORT_API int email_convert_mutf7_to_utf8(const char *mutf7_str, char **utf8_str)
201 {
202         EM_DEBUG_API_BEGIN("mutf7_str[%p]", mutf7_str);
203         int err = EMAIL_ERROR_NONE;
204
205         EM_IF_NULL_RETURN_VALUE(mutf7_str, EMAIL_ERROR_INVALID_PARAM);
206
207         emcore_gmime_init();
208
209         char *tmp_mutf7_str = g_strdup(mutf7_str);
210         *utf8_str = emcore_convert_mutf7_to_utf8(tmp_mutf7_str);
211         if (!(*utf8_str) || EM_SAFE_STRLEN(*utf8_str) == 0) {
212                 EM_DEBUG_EXCEPTION("emcore_convert_mutf7_to_utf8 failed");
213                 err = EMAIL_ERROR_UNKNOWN;
214         }
215         EM_SAFE_FREE(tmp_mutf7_str);
216
217         emcore_gmime_shutdown();
218
219         EM_DEBUG_API_END("ret[%d]", err);
220         return err;
221 }