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