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