modify msg-service to support dpm feature
[platform/core/messaging/msg-service.git] / framework / submit-handler / MsgSubmitHandler.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 <string.h>
19
20 #include "MsgDebug.h"
21 #include "MsgUtilFile.h"
22 #include "MsgUtilStorage.h"
23 #include "MsgException.h"
24 #include "MsgGconfWrapper.h"
25 #include "MsgPluginManager.h"
26 #include "MsgStorageHandler.h"
27 #include "MsgSubmitHandler.h"
28 #include "MsgUtilFunction.h"
29
30
31 /*==================================================================================================
32                                      FUNCTION IMPLEMENTATION
33 ==================================================================================================*/
34 msg_error_t MsgSubmitReq(MSG_REQUEST_INFO_S *pReqInfo, bool bScheduled)
35 {
36         msg_error_t err = MSG_SUCCESS;
37         static int reqId = 1;
38
39         pReqInfo->reqId = reqId;
40         reqId++;
41
42         if (msg_check_dpm_policy(pReqInfo->msgInfo.msgType.mainType) == false) {
43                 MSG_DEBUG("Messaging is restricted by DPM policy.");
44                 MsgDbHandler *dbHandle = getDbHandle();
45                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
46                 return MSG_ERR_DPM_RESTRICT;
47         }
48
49         MSG_DEBUG("==== Msg ID = [%d] ====", pReqInfo->msgInfo.msgId);
50         MSG_DEBUG("==== Folder ID = [%d] ====", pReqInfo->msgInfo.folderId);
51         MSG_INFO("==== Main Type = [%d] ====", pReqInfo->msgInfo.msgType.mainType);
52         MSG_INFO("==== Sub Type = [%d] ====", pReqInfo->msgInfo.msgType.subType);
53         MSG_DEBUG("==== Class Type = [%d] ====", pReqInfo->msgInfo.msgType.classType);
54         MSG_DEBUG("==== Message Data = [%s] ====", pReqInfo->msgInfo.msgData);
55         MSG_SEC_DEBUG("==== Message Text = [%s] ====", pReqInfo->msgInfo.msgText);
56         MSG_INFO("==== SIM Index = [%d] ====", pReqInfo->msgInfo.sim_idx);
57
58         MSG_DEBUG("==== bSetting = [%d] ====", pReqInfo->sendOptInfo.bSetting);
59
60         if (pReqInfo->msgInfo.msgType.mainType == MSG_SMS_TYPE) {
61                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
62                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
63                 MSG_DEBUG("==== bReplyPath = [%d] ====", pReqInfo->sendOptInfo.option.smsSendOptInfo.bReplyPath);
64
65                 err = MsgSubmitReqSMS(pReqInfo);
66         } else if (pReqInfo->msgInfo.msgType.mainType == MSG_MMS_TYPE) {
67                 MSG_DEBUG("==== deliver = [%d] ====", pReqInfo->sendOptInfo.bDeliverReq);
68                 MSG_DEBUG("==== keepcopy = [%d] ====", pReqInfo->sendOptInfo.bKeepCopy);
69                 MSG_DEBUG("==== bReadReq = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.bReadReq);
70                 MSG_DEBUG("==== priority = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.priority);
71                 MSG_DEBUG("==== expiryTime = [%d] ====", pReqInfo->sendOptInfo.option.mmsSendOptInfo.expiryTime.time);
72
73                 err = MsgSubmitReqMMS(pReqInfo);
74         }
75
76         return err;
77 }
78
79
80 msg_error_t MsgSubmitReqSMS(MSG_REQUEST_INFO_S *pReqInfo)
81 {
82         msg_error_t err = MSG_SUCCESS;
83
84         /* submit request based on msgType; */
85         MSG_MAIN_TYPE_T mainType = pReqInfo->msgInfo.msgType.mainType;
86         MsgPlugin* plg = MsgPluginManager::instance()->getPlugin(mainType);
87
88         if (plg == NULL) {
89                 MsgDbHandler *dbHandle = getDbHandle();
90                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
91                 MSG_ERR("No plugin for %d type", mainType);
92                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
93         }
94
95         /* If MSG ID > 0 -> MSG in DRAFT */
96         /* Move Folder to OUTBOX */
97         if (pReqInfo->msgInfo.msgPort.valid == false) {
98                 pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
99
100         if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID))
101                 err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
102                 if (err != MSG_SUCCESS)
103                         return err;
104         }
105
106         err = plg->submitReq(pReqInfo);
107
108         return err;
109 }
110
111 msg_error_t MsgSubmitReqMMS(MSG_REQUEST_INFO_S *pReqInfo)
112 {
113         msg_error_t err = MSG_SUCCESS;
114         MsgDbHandler *dbHandle = getDbHandle();
115         MSG_RECIPIENTS_LIST_S pRecipientList;
116
117         MsgPlugin *sms_plg = MsgPluginManager::instance()->getPlugin(MSG_SMS_TYPE);
118
119         if (sms_plg == NULL) {
120                 MsgDbHandler *dbHandle = getDbHandle();
121                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
122                 MSG_ERR("No plugin for %d type", MSG_SMS_TYPE);
123                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
124         }
125
126         int defaultNetworkSimId = 0;
127
128         err = sms_plg->getDefaultNetworkSimId(&defaultNetworkSimId);
129         if (err != MSG_SUCCESS) {
130                 MSG_ERR("getDefaultNetworkSimId is failed=[%d]", err);
131                 return err;
132         } else if (pReqInfo->msgInfo.sim_idx != defaultNetworkSimId) {
133                 MSG_ERR("It is not default network SIM ID, request SIM=[%d], activated SIM=[%d]", pReqInfo->msgInfo.sim_idx, defaultNetworkSimId);
134                 return MSG_ERR_INVALID_PARAMETER;
135         }
136
137         MSG_MAIN_TYPE_T msgMainType = pReqInfo->msgInfo.msgType.mainType;
138         MsgPlugin *plg = MsgPluginManager::instance()->getPlugin(msgMainType);
139
140         if(!plg) {
141                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
142                 MSG_DEBUG("No Plugin for %d type", msgMainType);
143
144                 return MSG_ERR_INVALID_PLUGIN_HANDLE;
145         }
146
147         /*      If MSG ID > 0 -> MSG in DRAFT */
148         /* Move Folder to OUTBOX */
149         /* reject_msg_support */
150         MSG_DEBUG("Not scheduled MMS, pReqInfo->msgInfo.msgType.subType [%d]", pReqInfo->msgInfo.msgType.subType);
151
152         if(pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_JAVA_MMS) {
153                 char fileName[MSG_FILENAME_LEN_MAX+1] = {0};
154
155                 /* copy whole of MMS PDU filepath to msgData */
156                 strncpy(fileName, pReqInfo->msgInfo.msgData, MSG_FILENAME_LEN_MAX);
157                 memset(pReqInfo->msgInfo.msgData, 0x00, sizeof(pReqInfo->msgInfo.msgData));
158                 snprintf(pReqInfo->msgInfo.msgData, sizeof(pReqInfo->msgInfo.msgData), "%s%s", MSG_IPC_DATA_PATH, fileName);
159
160                 MSG_SEC_DEBUG("JAVA MMS PDU filepath:%s", pReqInfo->msgInfo.msgData);
161
162                 /* submit request */
163                 err = plg->submitReq(pReqInfo);
164
165                 if(err != MSG_SUCCESS) {
166                         MSG_DEBUG("Update Network Status : [%d]", err);
167                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
168                 }
169
170                 return err;
171         } else if ((pReqInfo->msgInfo.msgType.subType == MSG_SENDREQ_MMS) || (pReqInfo->msgInfo.msgType.subType == MSG_FORWARD_MMS)) {
172                 MSG_SEC_DEBUG("msgdata file path = [%s]", pReqInfo->msgInfo.msgData);
173
174                 if (pReqInfo->msgInfo.msgId > 0 && (pReqInfo->msgInfo.folderId == MSG_DRAFT_ID || pReqInfo->msgInfo.folderId == MSG_OUTBOX_ID)) {
175                         MSG_DEBUG("Not New Message.");
176                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
177                         err = MsgStoUpdateMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
178                 } else {
179                         MSG_DEBUG("New Message.");
180                         pReqInfo->msgInfo.msgId = 0;
181                         pReqInfo->msgInfo.folderId = MSG_OUTBOX_ID;
182                         err = MsgStoAddMessage(&(pReqInfo->msgInfo), &(pReqInfo->sendOptInfo));
183                 }
184         } else if (pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS) {
185                 msg_read_report_status_t readStatus;
186
187                 err = MsgStoCheckReadReportStatus(pReqInfo->msgInfo.msgId);
188                 if(err != MSG_SUCCESS) {
189                         MSG_INFO("msg id [%d], read report could NOT send [%d]", pReqInfo->msgInfo.msgId, err);
190                         return err;
191                 }
192
193                 memcpy(&readStatus, pReqInfo->msgInfo.msgData, sizeof(msg_read_report_status_t));
194                 if (readStatus == MSG_READ_REPORT_REJECT_BY_USER) {
195                         MSG_INFO("msg id [%d], read report reject by user", pReqInfo->msgInfo.msgId);
196
197                         err = MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_NO_SENT);
198
199                         return err;
200                 }
201
202                 err = MsgStoGetRecipientList(pReqInfo->msgInfo.msgId, &pRecipientList);
203                 if(err != MSG_SUCCESS)
204                         return MSG_ERR_PLUGIN_STORAGE;
205
206                 pReqInfo->msgInfo.nAddressCnt = pRecipientList.recipientCnt;
207                 pReqInfo->msgInfo.addressList = pRecipientList.recipientAddr;
208
209                 /*
210                 for(int i = 0; i < pRecipientList.recipientCnt; i++)
211                 {
212                         pReqInfo->msgInfo.addressList[i].addressType = pRecipientList.recipientAddr[i].addressType;
213                         pReqInfo->msgInfo.addressList[i].recipientType = MSG_RECIPIENTS_TYPE_TO;
214                         pReqInfo->msgInfo.addressList[i].contactId = pRecipientList.recipientAddr[i].contactId;
215                         strncpy(pReqInfo->msgInfo.addressList[i].addressVal, pRecipientList.recipientAddr[i].addressVal, MAX_ADDRESS_VAL_LEN);
216                 }
217                 */
218
219                 char subject[MAX_SUBJECT_LEN+1];
220
221                 err = MsgStoGetSubject(pReqInfo->msgInfo.msgId, subject);
222                 if (err != MSG_SUCCESS)
223                         MSG_DEBUG("Getting subject returned not a MSG_SUCCESS");
224
225                 strncpy(pReqInfo->msgInfo.subject, subject, MAX_SUBJECT_LEN);
226
227                 err = plg->composeReadReport(&(pReqInfo->msgInfo));
228         } else if (pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS) {
229                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), pReqInfo->msgInfo.networkStatus);
230         }
231
232         /* reject_msg_support */
233
234         if(err != MSG_SUCCESS) {
235                 MSG_DEBUG("Fail to Add/Update Message : MsgStoMoveMessageToFolder()/MsgStoAddMessage()");
236                 return err;
237         }
238
239         switch (pReqInfo->msgInfo.msgType.subType) {
240                 case MSG_SENDREQ_MMS:
241                 case MSG_FORWARD_MMS:
242                         MsgDeleteFile(pReqInfo->msgInfo.msgData);
243                         memset(pReqInfo->msgInfo.msgData, 0x00, MAX_MSG_DATA_LEN+1);
244                         snprintf(pReqInfo->msgInfo.msgData, MAX_MSG_DATA_LEN+1, "%s%d.mms", MSG_DATA_PATH, pReqInfo->msgInfo.msgId);
245                         break;
246
247                 case MSG_READREPLY_MMS:
248                 case MSG_READRECIND_MMS:
249                         break;
250                 default:
251                         break;
252         }
253
254         /* update content location from db */
255         if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS)
256                 err = MsgStoGetContentLocation(&(pReqInfo->msgInfo));
257         /* reject_msg_support */
258         else if (pReqInfo->msgInfo.msgType.subType == MSG_NOTIFYRESPIND_MMS)
259                 err = plg->updateRejectStatus(&(pReqInfo->msgInfo));
260
261         /* Check SIM is present or not */
262         char keyName[MAX_VCONFKEY_NAME_LEN];
263         memset(keyName, 0x00, sizeof(keyName));
264         snprintf(keyName, sizeof(keyName), "%s/%d", MSG_SIM_CHANGED, pReqInfo->msgInfo.sim_idx);
265         int tmpval = 0;
266         if (MsgSettingGetInt(keyName, &tmpval) != MSG_SUCCESS) {
267                 MSG_INFO("MsgSettingGetInt() is failed");
268         }
269         MSG_SIM_STATUS_T simStatus = (MSG_SIM_STATUS_T)tmpval;
270
271         if(simStatus == MSG_SIM_STATUS_NOT_FOUND) {
272                 MSG_DEBUG("SIM is not present...");
273                 MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
274
275                 return MSG_ERR_NO_SIM;
276         }
277
278         if (err == MSG_SUCCESS)
279                 err = plg->submitReq(pReqInfo);
280
281         if (err == MSG_SUCCESS && ( pReqInfo->msgInfo.msgType.subType == MSG_READREPLY_MMS || pReqInfo->msgInfo.msgType.subType == MSG_READRECIND_MMS ))
282                 MsgStoSetReadReportSendStatus(pReqInfo->msgInfo.msgId, MMS_RECEIVE_READ_REPORT_SENT);
283
284         if (err != MSG_SUCCESS) {
285                 if(pReqInfo->msgInfo.msgType.subType == MSG_RETRIEVE_MMS )
286                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_RETRIEVE_FAIL);
287                 else
288                         MsgStoUpdateNetworkStatus(dbHandle, &(pReqInfo->msgInfo), MSG_NETWORK_SEND_FAIL);
289         }
290
291         return err;
292 }
293
294
295 msg_error_t MsgUpdateSentMsg(msg_message_id_t MsgId, msg_network_status_t Status)
296 {
297         msg_error_t err = MSG_SUCCESS;
298
299         bool bKeepCopy = true;
300
301 #ifdef MSG_MMS_KEEPCOPY
302         MSG_SENDINGOPT_INFO_S sendOpt = {};
303
304         if (msgType.mainType == MSG_MMS_TYPE) {
305                 if (MsgStoGetMmsSendOpt(MsgId, &sendOpt) == MSG_SUCCESS)
306                         bKeepCopy = sendOpt.bKeepCopy;
307                 else
308                         err = MsgSettingGetBool(MSG_KEEP_COPY, &bKeepCopy);
309         }
310
311         /* Move Msg to SENTBOX */
312         if (Status == MSG_NETWORK_SEND_SUCCESS) {
313                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
314                 if (bKeepCopy == true)
315                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
316                 else
317                         err = MsgStoDeleteMessage(MsgId, false);
318         }
319 #else
320         /* Move Msg to SENTBOX */
321         if (Status == MSG_NETWORK_SEND_SUCCESS) {
322                 MSG_DEBUG("In Status == MSG_NETWORK_SEND_SUCCESS and  bKeepCopy is [%d]", bKeepCopy);
323                 if (bKeepCopy == true)
324                         err = MsgStoMoveMessageToFolder(MsgId, MSG_SENTBOX_ID);
325         }
326 #endif
327         return err;
328 }
329
330
331 void MsgCopyReqInfo(MSG_REQUEST_INFO_S *pSrc, int addrIdx, MSG_REQUEST_INFO_S *pDest)
332 {
333         MSG_BEGIN();
334
335         memset(pDest, 0x00, sizeof(MSG_REQUEST_INFO_S));
336
337         /* Copy Request ID */
338         pDest->reqId = pSrc->reqId;
339
340         /* Copy Msg Info */
341         pDest->msgInfo.msgId = pSrc->msgInfo.msgId;
342
343         pDest->msgInfo.threadId = pSrc->msgInfo.threadId;
344
345         pDest->msgInfo.folderId = pSrc->msgInfo.folderId;
346
347         pDest->msgInfo.msgType.mainType = pSrc->msgInfo.msgType.mainType;
348         pDest->msgInfo.msgType.subType = pSrc->msgInfo.msgType.subType;
349         pDest->msgInfo.msgType.classType = pSrc->msgInfo.msgType.classType;
350
351         pDest->msgInfo.storageId = pSrc->msgInfo.storageId;
352
353         pDest->msgInfo.nAddressCnt = 1;
354
355         pDest->msgInfo.addressList[0].addressType = pSrc->msgInfo.addressList[addrIdx].addressType;
356         pDest->msgInfo.addressList[0].recipientType = pSrc->msgInfo.addressList[addrIdx].recipientType;
357         pDest->msgInfo.addressList[0].contactId = pSrc->msgInfo.addressList[addrIdx].contactId;
358         strncpy(pDest->msgInfo.addressList[0].addressVal, pSrc->msgInfo.addressList[addrIdx].addressVal, MAX_ADDRESS_VAL_LEN);
359         strncpy(pDest->msgInfo.addressList[0].displayName, pSrc->msgInfo.addressList[addrIdx].displayName, MAX_DISPLAY_NAME_LEN);
360
361         strncpy(pDest->msgInfo.replyAddress, pSrc->msgInfo.replyAddress, MAX_PHONE_NUMBER_LEN);
362         strncpy(pDest->msgInfo.subject, pSrc->msgInfo.subject, MAX_SUBJECT_LEN);
363
364         pDest->msgInfo.displayTime = pSrc->msgInfo.displayTime;
365         pDest->msgInfo.networkStatus = pSrc->msgInfo.networkStatus;
366         pDest->msgInfo.encodeType = pSrc->msgInfo.encodeType;
367         pDest->msgInfo.bRead = pSrc->msgInfo.bRead;
368         pDest->msgInfo.bProtected = pSrc->msgInfo.bProtected;
369         pDest->msgInfo.priority = pSrc->msgInfo.priority;
370         pDest->msgInfo.direction = pSrc->msgInfo.direction;
371
372         pDest->msgInfo.msgPort.valid = pSrc->msgInfo.msgPort.valid;
373
374         if (pDest->msgInfo.msgPort.valid == true) {
375                 pDest->msgInfo.msgPort.dstPort = pSrc->msgInfo.msgPort.dstPort;
376                 pDest->msgInfo.msgPort.srcPort = pSrc->msgInfo.msgPort.srcPort;
377         }
378
379         pDest->msgInfo.bTextSms = pSrc->msgInfo.bTextSms;
380         pDest->msgInfo.dataSize = pSrc->msgInfo.dataSize;
381
382         strncpy(pDest->msgInfo.msgData, pSrc->msgInfo.msgData, MAX_MSG_DATA_LEN);
383
384         if (pDest->msgInfo.bTextSms == true) {
385                 memcpy(pDest->msgInfo.msgText, pSrc->msgInfo.msgText, pDest->msgInfo.dataSize);
386                 pDest->msgInfo.msgText[pDest->msgInfo.dataSize] = '\0';
387         }
388
389         /* Set Sending Info */
390         pDest->sendOptInfo.bSetting = pSrc->sendOptInfo.bSetting;
391
392         if (pDest->sendOptInfo.bSetting == true) {
393                 pDest->sendOptInfo.bDeliverReq = pSrc->sendOptInfo.bDeliverReq;
394                 pDest->sendOptInfo.bKeepCopy = pSrc->sendOptInfo.bKeepCopy;
395
396                 if (pDest->msgInfo.msgType.mainType == MSG_SMS_TYPE) {
397                         pDest->sendOptInfo.option.smsSendOptInfo.bReplyPath = pSrc->sendOptInfo.option.smsSendOptInfo.bReplyPath;
398                 } else if (pDest->msgInfo.msgType.mainType == MSG_MMS_TYPE) {
399                         pDest->sendOptInfo.option.mmsSendOptInfo.priority = pSrc->sendOptInfo.option.mmsSendOptInfo.priority;
400                         pDest->sendOptInfo.option.mmsSendOptInfo.bReadReq = pSrc->sendOptInfo.option.mmsSendOptInfo.bReadReq;
401
402                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.type;
403                         pDest->sendOptInfo.option.mmsSendOptInfo.expiryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.expiryTime.time;
404
405                         pDest->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime = pSrc->sendOptInfo.option.mmsSendOptInfo.bUseDeliveryCustomTime;
406                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.type;
407                         pDest->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time = pSrc->sendOptInfo.option.mmsSendOptInfo.deliveryTime.time;
408                 }
409         }
410
411         MSG_END();
412 }
413