RSA sync with private
[platform/core/messaging/msg-service.git] / framework / deliver-handler / MsgDeliverHandler.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 // for sl message browser launch
18 #include <app_service.h>
19
20 #include "MsgDebug.h"
21 #include "MsgUtilFile.h"
22 #include "MsgContact.h"
23 #include "MsgUtilStorage.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgSoundPlayer.h"
26 #include "MsgSpamFilter.h"
27 #include "MsgPluginManager.h"
28 #include "MsgStorageHandler.h"
29 #include "MsgDeliverHandler.h"
30 #include "MsgNotificationWrapper.h"
31
32 /*==================================================================================================
33                                      VARIABLES
34 ==================================================================================================*/
35 extern MsgDbHandler dbHandle;
36
37
38 /*==================================================================================================
39                                      FUNCTION IMPLEMENTATION
40 ==================================================================================================*/
41 msg_error_t MsgHandleMmsConfIncomingMsg(MSG_MESSAGE_INFO_S *pMsgInfo, msg_request_id_t reqID)
42 {
43         MSG_BEGIN();
44
45         msg_error_t err = MSG_SUCCESS;
46
47         MSG_DEBUG(" msgtype subtype is [%d]", pMsgInfo->msgType.subType);
48
49         if (pMsgInfo->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS) {
50                 // keep origianl subType
51                 MSG_SUB_TYPE_T subType = pMsgInfo->msgType.subType;
52
53                 /* If Retrieve Failed, Msg SubType is remained as Notification Ind */
54                 if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_FAIL)
55                         pMsgInfo->msgType.subType = MSG_NOTIFICATIONIND_MMS;
56
57                 err = MsgStoUpdateMMSMessage(pMsgInfo);
58
59                 if (err == MSG_SUCCESS)
60                         MSG_DEBUG("Command Handle Success : MsgStoUpdateMessage()");
61
62                 if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
63                         MSG_DEBUG("### MSG_NETWORK_RETRIEVE_SUCCESS ###");
64                         //Update Mms Message to MMS Plugin DB
65
66                         // MMS Received Ind Process Func
67                         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(pMsgInfo->msgType.mainType);
68
69                         //Contents of msg Data was removed and replaced to retrievedFilePath
70                         // NOTICE:: now it was moved to handleEvent in MsgListnerThread
71                         err = plg->updateMessage(pMsgInfo, NULL, NULL);
72                         if (err != MSG_SUCCESS)
73                                 return MSG_ERR_STORAGE_ERROR;
74                 }
75
76                 MSG_DEBUG(" ######################### MESSAGE UPDATE COMPLETED!!! ######################");
77
78                 bool readStatus = false;
79
80                 MsgStoGetReadStatus(pMsgInfo->msgId, &readStatus);
81                 MSG_DEBUG("### readStatus = %d ###", readStatus);
82
83                 //Update Read Status to Unread beacaus Noti is Read
84                 if (subType == MSG_RETRIEVE_MANUALCONF_MMS && pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
85                         MSG_DEBUG("###2. subType = %d ###", pMsgInfo->msgType.subType);
86
87                         if (readStatus == true)
88                                 MsgStoSetReadStatus(&dbHandle, pMsgInfo->msgId, false);
89                 }
90
91                 // update badge
92                 if (subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
93                         int smsCnt = 0;
94                         int mmsCnt = 0;
95
96                         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
97                         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
98
99                         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
100                 }
101
102                 if (subType == MSG_RETRIEVE_AUTOCONF_MMS) {
103                         // play message-tone when MMS retrieved
104                         MsgSoundPlayStart(false);
105
106                         // add phone log
107                         MSG_DEBUG("Enter MsgAddPhoneLog() for mms message.");
108                         MsgAddPhoneLog(pMsgInfo);
109
110                         MsgInsertNoti(&dbHandle, pMsgInfo);
111                 }  else if (subType == MSG_RETRIEVE_MANUALCONF_MMS) {
112                         if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_SUCCESS) {
113                                 MSG_DEBUG("Manual success");
114                                 MsgInsertTicker("Message Retrieved", MESSAGE_RETRIEVED);
115                         } else if (pMsgInfo->networkStatus == MSG_NETWORK_RETRIEVE_FAIL) {
116                                 MSG_DEBUG("Manual failed");
117                                 MsgInsertTicker("Retrieving message failed", RETRIEVING_MESSAGE_FAILED);
118                         }
119                 }
120         }
121         else if (pMsgInfo->msgType.subType == MSG_SENDREQ_MMS || pMsgInfo->msgType.subType == MSG_SENDCONF_MMS)
122         {
123                 MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(pMsgInfo->msgType.mainType);
124                 // change subType for storage update
125                 pMsgInfo->msgType.subType = MSG_SENDCONF_MMS;
126
127                 err = MsgStoUpdateMMSMessage(pMsgInfo);
128                 if (err == MSG_SUCCESS)
129                         MSG_DEBUG("Command Handle Success : MsgStoUpdateMMSMessage()");
130
131                 err = plg->updateMessage(pMsgInfo, NULL, NULL);
132                 if (err != MSG_SUCCESS)
133                         return MSG_ERR_STORAGE_ERROR;
134
135                 MSG_DEBUG("pMsg->networkStatus : %d", pMsgInfo->networkStatus);
136                 err = MsgStoUpdateNetworkStatus(&dbHandle, pMsgInfo, pMsgInfo->networkStatus);
137                 if (err != MSG_SUCCESS)
138                         return MSG_ERR_STORAGE_ERROR;
139
140                 if (pMsgInfo->networkStatus == MSG_NETWORK_SEND_SUCCESS)
141                 {
142                         err = MsgStoMoveMessageToFolder(pMsgInfo->msgId, MSG_SENTBOX_ID);
143                         if (err != MSG_SUCCESS)
144                                 return MSG_ERR_STORAGE_ERROR;
145                 }
146
147                 // Get subject and text
148                 MsgStoGetText(pMsgInfo->msgId, pMsgInfo->subject, pMsgInfo->msgText);
149
150                 MSG_DEBUG(" ######################### MESSAGE UPDATE COMPLETED!!! ######################");
151         }
152         return err;
153 }
154
155 msg_error_t MsgHandleIncomingMsg(MSG_MESSAGE_INFO_S *pMsgInfo, bool *pSendNoti)
156 {
157         MSG_BEGIN();
158
159         msg_error_t err = MSG_SUCCESS;
160
161         if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) {
162
163                 bool bOnlyNoti = false;
164
165                 err = MsgHandleSMS(pMsgInfo, pSendNoti, &bOnlyNoti);
166
167                 if (err == MSG_SUCCESS) {
168                         MsgSoundPlayStart(false);
169                         if (*pSendNoti == true) {
170                                 int smsCnt = 0, mmsCnt = 0;
171
172                                 smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
173                                 mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
174
175                                 MsgSettingHandleNewMsg(smsCnt, mmsCnt);
176                                 MsgInsertNoti(&dbHandle, pMsgInfo);
177                         } else if (bOnlyNoti == true) {
178                                 MsgInsertNoti(pMsgInfo);
179                         }
180                 }
181         }
182         else if (pMsgInfo->msgType.mainType == MSG_MMS_TYPE)
183         {
184                 err = MsgHandleMMS(pMsgInfo, pSendNoti);
185         }
186
187         //Add Phone Log Data
188         if ((err == MSG_SUCCESS) &&
189                 (pMsgInfo->folderId == MSG_INBOX_ID || pMsgInfo->folderId == MSG_SPAMBOX_ID) &&
190                 (pMsgInfo->msgType.mainType == MSG_SMS_TYPE) &&
191                 (pMsgInfo->msgType.subType != MSG_WAP_SL_SMS)) {
192
193                 MSG_DEBUG("Enter MsgAddPhoneLog() : pMsg->folderId [%d]", pMsgInfo->folderId);
194
195                 MsgAddPhoneLog(pMsgInfo);
196         }
197
198         MSG_END();
199
200         return err;
201 }
202
203
204 msg_error_t MsgHandleSMS(MSG_MESSAGE_INFO_S *pMsgInfo, bool *pSendNoti, bool *bOnlyNoti)
205 {
206         msg_error_t err = MSG_SUCCESS;
207
208         if (pMsgInfo->msgType.subType == MSG_NORMAL_SMS) {
209                 if (MsgCheckFilter(&dbHandle, pMsgInfo) == true) {
210                         // Move to SpamBox
211                         err = MsgStoMoveMessageToFolder(pMsgInfo->msgId, MSG_SPAMBOX_ID);
212
213                         if (err != MSG_SUCCESS) {
214                                 MSG_DEBUG("MsgStoMoveMessageToFolder() Error : [%d]", err);
215                         } else {
216                                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
217                         }
218
219                         // Update Conversation table
220                         err = MsgStoUpdateConversation(&dbHandle, pMsgInfo->threadId);
221
222                         if (err != MSG_SUCCESS)
223                                 MSG_DEBUG("MsgStoUpdateConversation() Error : [%d]", err);
224
225                         *pSendNoti = false;
226                 }
227         } else if ((pMsgInfo->msgType.subType >= MSG_WAP_SI_SMS) && (pMsgInfo->msgType.subType <= MSG_WAP_CO_SMS)) {
228                 MSG_DEBUG("Starting WAP Message Incoming.");
229
230                 MSG_PUSH_SERVICE_TYPE_T serviceType = (MSG_PUSH_SERVICE_TYPE_T)MsgSettingGetInt(PUSH_SERVICE_TYPE);
231                 service_h svc_handle = NULL;
232
233                 switch (pMsgInfo->msgType.subType) {
234                         case MSG_WAP_SL_SMS:
235                         {
236                                 *pSendNoti = false;
237
238                                 if (serviceType == MSG_PUSH_SERVICE_ALWAYS) {
239                                         if (service_create(&svc_handle) < 0) {
240                                                 MSG_DEBUG("Fail to create service handle");
241                                                 break;
242                                         }
243                                         if (!svc_handle) {
244                                                 MSG_DEBUG("Service handle is NULL");
245                                                 break;
246                                         }
247                                         if (service_set_operation(svc_handle, SERVICE_OPERATION_VIEW) < 0) {
248                                                 MSG_DEBUG("Fail to create service handle");
249                                                 service_destroy(svc_handle);
250                                                 break;
251                                         }
252                                         if (service_set_uri(svc_handle, pMsgInfo->msgText) < 0) {
253                                                 MSG_DEBUG("Fail to set uri");
254                                                 service_destroy(svc_handle);
255                                                 break;
256                                         }
257                                         if (service_set_package(svc_handle, MSG_SVC_PKG_NAME_BROWSER) < 0) {
258                                                 MSG_DEBUG("Fail to set package");
259                                                 service_destroy(svc_handle);
260                                                 break;
261                                         }
262                                         if (service_send_launch_request(svc_handle, NULL, NULL) < 0) {
263                                                 MSG_DEBUG("Fail to launch browser");
264                                                 service_destroy(svc_handle);
265                                                 break;
266                                         }
267
268                                         service_destroy(svc_handle);
269
270                                 } else if (serviceType == MSG_PUSH_SERVICE_PROMPT) {
271                                         char urlString[MAX_COMMAND_LEN+1];
272                                         memset(urlString, 0x00, sizeof(urlString));
273
274                                         snprintf(urlString, MAX_COMMAND_LEN, "/usr/apps/org.tizen.message/bin/message-dialog -m PUSH_MSG_ALWAYS_ASK -u %s &", pMsgInfo->msgText);
275
276                                         system(urlString);
277                                 }
278
279                         }
280                         break;
281
282                         case MSG_WAP_SI_SMS:
283                                 *pSendNoti = true;
284                                 break;
285                         case MSG_WAP_CO_SMS:
286                                 *pSendNoti = false;
287                                 break;
288                 }
289         } else if (pMsgInfo->msgType.subType == MSG_STATUS_REPORT_SMS) {
290                 *pSendNoti = false;
291                 *bOnlyNoti = true;
292         } else if (pMsgInfo->msgType.subType >= MSG_MWI_VOICE_SMS && pMsgInfo->msgType.subType <= MSG_MWI_OTHER_SMS) {
293                 if (pMsgInfo->bStore == false) {
294                         *pSendNoti = false;
295                         *bOnlyNoti = true;
296                 }
297         } 
298
299
300         return err;
301 }
302
303
304 msg_error_t MsgHandleMMS(MSG_MESSAGE_INFO_S *pMsgInfo,  bool *pSendNoti)
305 {
306         msg_error_t err = MSG_SUCCESS;
307
308         MSG_REQUEST_INFO_S request = {0};
309         bool bReject = false;
310         bool bFiltered = false;
311
312         // MMS Received Ind Process Func
313         MSG_MAIN_TYPE_T msgMainType = pMsgInfo->msgType.mainType;
314         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
315
316         // Need to process m-delivery-ind, m-notification-ind, m-read-orig-ind
317         err = plg->processReceivedInd(pMsgInfo, &request, &bReject);
318
319         if (err == MSG_SUCCESS) {
320                 MSG_DEBUG("Process Message Success : processReceivedInd(), btextsms %d", pMsgInfo->bTextSms);
321         } else {
322                 MSG_DEBUG("Process Message Fail : processReceivedInd()");
323                 return err;
324         }
325
326         // Add into DB
327         if ((pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS) && bReject == false) {
328                 bFiltered = MsgCheckFilter(&dbHandle, pMsgInfo);
329
330                 if (bFiltered == true) {
331                         pMsgInfo->networkStatus = MSG_NETWORK_RETRIEVE_FAIL;
332                         *pSendNoti = false;
333                 }
334
335                 err = MsgStoAddMessage(pMsgInfo, NULL);
336
337                 if (err != MSG_SUCCESS) {
338                         MSG_DEBUG("MsgStoAddMessage() Error: [%d]", err);
339                         return err;
340                 }
341         } else if (pMsgInfo->msgType.subType == MSG_READORGIND_MMS || pMsgInfo->msgType.subType == MSG_DELIVERYIND_MMS) {
342                 if (MsgInsertMmsReportToNoti(&dbHandle, pMsgInfo) == MSG_SUCCESS) {
343                         MsgSoundPlayStart(false);
344                         *pSendNoti = false;
345                 }
346         }
347
348         //In the case of m-notification-ind, we should decide whether to send m-notify-response-ind or http 'Get'
349         //submit request
350         if (pMsgInfo->msgType.subType == MSG_NOTIFICATIONIND_MMS && bFiltered == false) {
351                 if (request.msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS && bReject == false) {
352                         MsgSoundPlayStart(false);
353
354                         int smsCnt = 0;
355                         int mmsCnt = 0;
356
357                         smsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_SMS_TYPE);
358                         mmsCnt = MsgStoGetUnreadCnt(&dbHandle, MSG_MMS_TYPE);
359
360                         MsgSettingHandleNewMsg(smsCnt, mmsCnt);
361                         MsgInsertNoti(&dbHandle, pMsgInfo);
362                 }
363
364                 request.msgInfo.msgId = pMsgInfo->msgId;
365
366                 MSG_DEBUG("-=====================[[[ %s ]]]] =========================", pMsgInfo->msgData);
367                 err = plg->submitReq(&request);
368
369                 if (err == MSG_SUCCESS) {
370                         MSG_DEBUG("Process Message Success : processReceivedInd()");
371                 } else {
372                         MSG_DEBUG("Process Message Fail : processReceivedInd()");
373                 }
374         }
375
376         return err;
377 }
378