modify msg-service to support dpm feature
[platform/core/messaging/msg-service.git] / plugin / mms_plugin / MmsPluginMain.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include "MsgException.h"
19 #include "MsgGconfWrapper.h"
20 #include "MsgUtilFile.h"
21 #include "MsgUtilFunction.h"
22 #include "MsgMmsMessage.h"
23 #include "MsgSerialize.h"
24 #include "MmsPluginDebug.h"
25 #include "MmsPluginTypes.h"
26 #include "MmsPluginMain.h"
27 #include "MmsPluginTransport.h"
28 #include "MmsPluginStorage.h"
29 #include "MmsPluginInternal.h"
30 #include "MmsPluginEventHandler.h"
31 #include "MmsPluginCodec.h"
32
33 /*==================================================================================================
34                                                         FUNCTION IMPLEMENTATION
35 ==================================================================================================*/
36 msg_error_t MsgPlgCreateHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
37 {
38         if (pPluginHandle == NULL) {
39                 MSG_DEBUG("MMS plugin: create handler error ");
40                 return MSG_ERR_NULL_POINTER;
41         } else {
42                 pPluginHandle->pfInitialize = MmsInitialize;
43                 pPluginHandle->pfFinalize = MmsFinalize;
44                 pPluginHandle->pfRegisterListener = MmsRegisterListener;
45                 pPluginHandle->pfSubmitRequest = MmsSubmitRequest;
46                 pPluginHandle->pfAddMessage = MmsAddMessage;
47                 pPluginHandle->pfProcessReceivedInd = MmsProcessReceivedInd;
48                 pPluginHandle->pfUpdateMessage = MmsUpdateMessage;
49                 pPluginHandle->pfGetMmsMessage = MmsGetMmsMessage;
50                 pPluginHandle->pfUpdateRejectStatus = MmsUpdateRejectStatus;
51                 pPluginHandle->pfComposeReadReport = MmsComposeReadReport;
52                 pPluginHandle->pfRestoreMsg = MmsRestoreMsg;
53
54                 MSG_DEBUG("MMS plugin: create handler OK");
55                 MSG_DEBUG("MMS plugin %p", pPluginHandle);
56         }
57
58         return MSG_SUCCESS;
59 }
60
61
62 msg_error_t MmsPlgDestroyHandle(MSG_PLUGIN_HANDLER_S *pPluginHandle)
63 {
64         MSG_BEGIN();
65
66         if (pPluginHandle != NULL) {
67                 free(pPluginHandle);
68                 pPluginHandle = NULL;
69         }
70
71         MSG_END();
72
73         return MSG_SUCCESS;
74 }
75
76
77 msg_error_t MmsInitialize()
78 {
79         MSG_BEGIN();
80
81         /* remove temp files */
82 /*      MsgMmsInitDir(); */
83
84         MSG_END();
85
86         return MSG_SUCCESS;
87 }
88
89
90 msg_error_t MmsFinalize()
91 {
92         MSG_BEGIN();
93
94         MSG_END();
95
96         return MSG_SUCCESS;
97 }
98
99
100 msg_error_t MmsRegisterListener(MSG_PLUGIN_LISTENER_S *pListener)
101 {
102         MSG_BEGIN();
103
104         MmsPluginEventHandler::instance()->registerListener(pListener);
105
106         MSG_END();
107
108         return MSG_SUCCESS;
109 }
110
111
112 msg_error_t MmsSubmitRequest(MSG_REQUEST_INFO_S *pReqInfo)
113 {
114         MSG_BEGIN();
115
116         try {
117                 if (msg_check_dpm_policy(pReqInfo->msgInfo.msgType.mainType) == false) {
118                         MSG_DEBUG("Messaging is restricted by DPM policy.");
119                         return MSG_ERR_DPM_RESTRICT;
120                 }
121
122                 MmsPluginTransport::instance()->submitRequest(pReqInfo);
123         } catch (MsgException& e) {
124                 MSG_FATAL("%s", e.what());
125
126                 if (e.errorCode() == MsgException::REQ_EXIST_ERROR)
127                         return MSG_SUCCESS;
128
129                 return MSG_ERR_PLUGIN_TRANSPORT;
130         } catch (exception& e) {
131                 MSG_FATAL("%s", e.what());
132                 return MSG_ERR_PLUGIN_TRANSPORT;
133         }
134
135         MSG_END();
136
137         return MSG_SUCCESS;
138 }
139
140
141 msg_error_t MmsAddMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char *pFileData)
142 {
143         MSG_BEGIN();
144
145         try {
146                 MmsPluginStorage::instance()->addMessage(pMsgInfo, pSendOptInfo, pFileData);
147         } catch (MsgException& e) {
148                 MSG_FATAL("%s", e.what());
149                 return MSG_ERR_PLUGIN_TRANSPORT;
150         } catch (exception& e) {
151                 MSG_FATAL("%s", e.what());
152                 return MSG_ERR_PLUGIN_TRANSPORT;
153         }
154
155         MSG_END();
156
157         return MSG_SUCCESS;
158 }
159
160
161 msg_error_t MmsProcessReceivedInd(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_REQUEST_INFO_S *pRequest, bool *bReject)
162 {
163         MSG_BEGIN();
164
165         MSG_DEBUG("MMS Plugin ProcessReceivedInd");
166
167         try {
168                 MmsPluginInternal::instance()->processReceivedInd(pMsgInfo, pRequest, bReject);
169         } catch (MsgException& e) {
170                 MSG_FATAL("%s", e.what());
171                 return MSG_ERR_PLUGIN_TRANSPORT;
172         } catch (exception& e) {
173                 MSG_FATAL("%s", e.what());
174                 return MSG_ERR_PLUGIN_TRANSPORT;
175         }
176
177         MSG_END();
178
179         return MSG_SUCCESS;
180 }
181
182
183 msg_error_t MmsUpdateMessage(MSG_MESSAGE_INFO_S *pMsgInfo, MSG_SENDINGOPT_INFO_S *pSendOptInfo,  char *pFileData)
184 {
185         MSG_BEGIN();
186
187         msg_error_t err = MSG_SUCCESS;
188
189         try {
190                 if (pMsgInfo->networkStatus == MSG_NETWORK_NOT_SEND || pMsgInfo->networkStatus == MSG_NETWORK_SENDING) { /* draft update */
191                         err = MmsPluginStorage::instance()->updateMessage(pMsgInfo, pSendOptInfo, pFileData);
192                 } else {
193                         /* [Update Message ID & File path only in case of retrieve. Else update Message ID] */
194                         if (pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS) { /* retrieve conf */
195                                 err = MmsPluginStorage::instance()->updateConfMessage(pMsgInfo);
196                         } else {
197                                 err = MmsPluginStorage::instance()->updateMsgServerID(pMsgInfo, pSendOptInfo); /* update send conf */
198                         }
199                 }
200         } catch (MsgException& e) {
201                 MSG_FATAL("%s", e.what());
202                 return MSG_ERR_PLUGIN_STORAGE;
203         } catch (exception& e) {
204                 MSG_FATAL("%s", e.what());
205                 return MSG_ERR_PLUGIN_STORAGE;
206         }
207
208         MSG_END();
209
210         return err;
211 }
212
213
214 msg_error_t MmsGetMmsMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pSendOptInfo, char **pDestMsg)
215 {
216         MSG_BEGIN();
217
218         msg_error_t     err = MSG_SUCCESS;
219
220         try {
221                 err = MmsPluginStorage::instance()->getMessage(pMsg, pSendOptInfo, pDestMsg);
222         } catch (MsgException& e) {
223                 MSG_FATAL("%s", e.what());
224                 return MSG_ERR_PLUGIN_STORAGE;
225         } catch (exception& e) {
226                 MSG_FATAL("%s", e.what());
227                 return MSG_ERR_PLUGIN_STORAGE;
228         }
229
230         MSG_END();
231
232         return err;
233 }
234
235
236 msg_error_t MmsUpdateRejectStatus(MSG_MESSAGE_INFO_S *pMsgInfo)
237 {
238         MSG_BEGIN();
239
240         msg_error_t err = MSG_SUCCESS;
241
242         try {
243                 char szTrID[MMS_TR_ID_LEN + 1] = {0x00};
244                 bool bReportAllowed;
245
246                 err = MmsPluginStorage::instance()->getTrID(pMsgInfo, szTrID, sizeof(szTrID));
247                 if (err != MSG_SUCCESS)
248                         MSG_DEBUG("MmsPlgUpdRejectStatus : Get MMS Transacation id Failed");
249
250                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN + 1);
251                 if (MsgSettingGetBool(MMS_SEND_REPORT_ALLOWED, &bReportAllowed) != MSG_SUCCESS)
252                         MSG_INFO("MsgSettingGetBool() is failed");
253
254                 if (MmsPluginInternal::instance()->encodeNotifyRespInd(szTrID, MSG_DELIVERY_REPORT_REJECTED, bReportAllowed, pMsgInfo->msgData)) {
255                         MSG_DEBUG("MmsPlgUpdRejectStatus : Encode Notify Response Success");
256                         pMsgInfo->dataSize = strlen(pMsgInfo->msgData);
257                         pMsgInfo->bTextSms = true;
258                 } else {
259                         MSG_DEBUG("MmsPlgSetRejectStatus : Encode Notify Response Failed");
260                 }
261         } catch (MsgException& e) {
262                 MSG_FATAL("%s", e.what());
263                 return MSG_ERR_PLUGIN_STORAGE;
264         } catch (exception& e) {
265                 MSG_FATAL("%s", e.what());
266                 return MSG_ERR_PLUGIN_STORAGE;
267         }
268
269         if (err != MSG_SUCCESS)
270                 MSG_DEBUG("MmsPlgSetRejectStatus : Update MMS Message Failed");
271
272         MSG_END();
273
274         return err;
275 }
276
277
278 msg_error_t MmsComposeReadReport(MSG_MESSAGE_INFO_S *pMsgInfo)
279 {
280         MSG_BEGIN();
281
282         try {
283                 MmsPluginStorage::instance()->composeReadReport(pMsgInfo);
284         } catch (MsgException& e) {
285                 MSG_FATAL("%s", e.what());
286                 return MSG_ERR_PLUGIN_STORAGE;
287         } catch (exception& e) {
288                 MSG_FATAL("%s", e.what());
289                 return MSG_ERR_PLUGIN_STORAGE;
290         }
291
292         MSG_END();
293
294         return MSG_SUCCESS;
295 }
296
297 /* FIXME::It used for kies but not now */
298 msg_error_t MmsRestoreMsg(MSG_MESSAGE_INFO_S *pMsgInfo, char *pRcvBody, int rcvdBodyLen, char *filePath)
299 {
300         MSG_BEGIN();
301
302         if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) {
303                 FILE *pFile;
304
305                 MmsInitHeader();
306                 MmsRegisterDecodeBuffer();
307
308                 if ((pFile = MsgOpenFile(pMsgInfo->msgData, "rb+")) == NULL) {
309                         MSG_DEBUG("File Open Error: %s", pMsgInfo->msgData);
310                 } else {
311                         /* Decode Header */
312                         if (!MmsBinaryDecodeMsgHeader(pFile, rcvdBodyLen))
313                                 MSG_DEBUG("Decoding Header Failed \r\n");
314
315                         MsgCloseFile(pFile);
316                 }
317         } else {
318                 MSG_DEBUG(":::%d :%s ", rcvdBodyLen, pRcvBody);
319
320                 if (filePath) {
321                         snprintf(filePath, MAX_FULL_PATH_SIZE, "%sBODY_%lu.DATA", MSG_DATA_PATH, random() % 1000000000 + 1);
322                 } else {
323                         return MSG_ERR_NULL_POINTER;
324                 }
325
326                 /* create temp file */
327                 if (!MsgOpenCreateAndOverwriteFile(filePath, (char*)pRcvBody, rcvdBodyLen))
328                         return MSG_ERR_PLUGIN_STORAGE;
329         }
330
331         MSG_END();
332
333         return MSG_SUCCESS;
334 }
335
336 msg_error_t MmsDeleteMessage(msg_message_id_t msgId)
337 {
338         MSG_BEGIN();
339
340         try {
341                 MmsPluginStorage::instance()->deleteMmsMessage(msgId);
342         } catch (MsgException& e) {
343                 MSG_FATAL("%s", e.what());
344                 return MSG_ERR_PLUGIN_STORAGE;
345         } catch (exception& e) {
346                 MSG_FATAL("%s", e.what());
347                 return MSG_ERR_PLUGIN_STORAGE;
348         }
349
350         MSG_END();
351         return MSG_SUCCESS;
352 }