RSA sync with private
[platform/core/messaging/msg-service.git] / proxy / MsgHandleControl.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 <errno.h>
18 #include <stdlib.h>
19
20 #include <security-server.h>
21
22 #include "MsgDebug.h"
23 #include "MsgCppTypes.h"
24 #include "MsgException.h"
25 #include "MsgUtilFile.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgProxyListener.h"
28 #include "MsgHandle.h"
29
30 /*==================================================================================================
31                                      IMPLEMENTATION OF MsgHandle - Control Member Functions
32 ==================================================================================================*/
33 MsgHandle::MsgHandle() : mCounter(0), mClientSock()
34 {
35         memset(mConnectionId, 0x00, sizeof(mConnectionId));
36         memset(mCookie, 0x00, sizeof(mCookie));
37 }
38
39
40 MsgHandle::~MsgHandle()
41 {
42
43 }
44
45
46 void MsgHandle::openHandle()
47 {
48         int ret = 0;
49         size_t cookieSize;
50
51         bool bReady = false;
52
53         // server is currently booting and service is not available until the end of booting
54         MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady);
55
56         if (bReady == false) {
57                 THROW(MsgException::SERVER_READY_ERROR, "Msg Server is not ready !!!!!");
58         } else {
59                 MSG_DEBUG("Msg Server is ready !!!!!");
60         }
61
62         // Get Cookie Size
63 //      cookieSize = security_server_get_cookie_size();
64
65 //      MSG_DEBUG("cookie size : [%d]", cookieSize);
66
67         // Request Cookie
68 //      ret = security_server_request_cookie(mCookie, cookieSize);
69
70 //      if (ret < 0) {
71 //              MSG_DEBUG("security_server_request_cookie() error!! [%d]", ret);
72 //              return;
73 //      }
74
75         // Open Socket IPC
76         connectSocket();
77 }
78
79
80 void MsgHandle::closeHandle(MsgHandle* pHandle)
81 {
82         MSG_BEGIN();
83
84         //Remove CB List of closing Handle
85         MsgProxyListener* eventListener = MsgProxyListener::instance();
86
87         eventListener->clearListOfClosedHandle(pHandle);
88         //eventListener->stop();
89
90         // Close Socket IPC
91         disconnectSocket();
92
93         MSG_END();
94 }
95
96
97 void MsgHandle::connectSocket()
98 {
99         mClientSock.connect(MSG_SOCKET_PATH);
100 }
101
102
103 void MsgHandle::disconnectSocket()
104 {
105         mClientSock.close();
106 }
107
108
109 void MsgHandle::write(const char *pCmdData, int cmdSize, char **ppEvent)
110 {
111         if (pCmdData == NULL || ppEvent == NULL) {
112                 THROW(MsgException::INVALID_PARAM, "Param is NULL");
113         }
114
115         int ret = 0;
116
117         // Send Command to MSG FW
118         ret = mClientSock.write(pCmdData, cmdSize);
119         if (ret < 0)
120                 THROW(MsgException::IPC_ERROR, "IPC write error");
121
122         while(1)
123         {
124                 // Receive Result from MSG FW
125                 read(ppEvent);
126
127                 if(!CheckEventData(*ppEvent)) {
128                         delete [] (*ppEvent);
129                 } else {
130                         break;
131                 }
132         }
133 }
134
135
136 void MsgHandle::read(char **ppEvent)
137 {
138         unsigned int dataSize = 0;
139
140         dataSize = mClientSock.read(ppEvent, &dataSize);
141
142         if (dataSize == 0) {
143                 THROW(MsgException::IPC_ERROR, "Server closed connection");
144         }
145 }
146
147
148 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_HIDDEN_S *pSrc, MSG_MESSAGE_INFO_S *pDest)
149 {
150         MSG_BEGIN();
151
152         pDest->msgId = pSrc->msgId;
153         pDest->threadId = pSrc->threadId;
154         pDest->folderId = pSrc->folderId;
155         pDest->msgType.mainType = pSrc->mainType;
156         pDest->msgType.subType = pSrc->subType;
157         pDest->msgType.classType= pSrc->classType;
158         pDest->storageId = pSrc->storageId;
159
160         msg_struct_list_s *addr_info_s = pSrc->addr_list;
161
162         if (addr_info_s) {
163                 msg_struct_s *addr_info = NULL;
164                 MSG_ADDRESS_INFO_S *address = NULL;
165
166                 pDest->nAddressCnt = addr_info_s->nCount;
167
168                 for (int i = 0; i < addr_info_s->nCount; i++)
169                 {
170                         addr_info = (msg_struct_s *)addr_info_s->msg_struct_info[i];
171                         address = (MSG_ADDRESS_INFO_S *)addr_info->data;
172
173                         pDest->addressList[i].addressType = address->addressType;
174                         pDest->addressList[i].recipientType = address->recipientType;
175                         pDest->addressList[i].contactId = address->contactId;
176                         strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
177                         strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
178                         pDest->addressList[i].displayName[MAX_DISPLAY_NAME_LEN] = '\0';
179                 }
180         }
181
182         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
183         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
184
185         pDest->displayTime = pSrc->displayTime;
186         pDest->networkStatus = pSrc->networkStatus;
187         pDest->encodeType = pSrc->encodeType;
188         pDest->bRead = pSrc->bRead;
189         pDest->bProtected = pSrc->bProtected;
190         pDest->bBackup = pSrc->bBackup;
191         pDest->priority = pSrc->priority;
192         pDest->direction = pSrc->direction;
193
194         // Set Port Info.
195         pDest->msgPort.valid = pSrc->bPortValid;
196
197         if (pDest->msgPort.valid == true) {
198                 pDest->msgPort.dstPort = pSrc->dstPort;
199                 pDest->msgPort.srcPort = pSrc->srcPort;
200         }
201
202         MSG_DEBUG("nSize = %d",  pSrc->dataSize);
203
204         if (pSrc->mainType == MSG_SMS_TYPE){
205                 pDest->bTextSms = true;
206                 pDest->dataSize = pSrc->dataSize;
207
208                 memset(pDest->msgText, 0x00, sizeof(pDest->msgText));
209
210                 if (pSrc->dataSize > MAX_MSG_TEXT_LEN) {
211                         // Save Message Data into File
212                         char fileName[MAX_COMMON_INFO_SIZE+1];
213                         memset(fileName, 0x00, sizeof(fileName));
214
215                         if(MsgCreateFileName(fileName) == false)
216                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
217
218                         MSG_DEBUG("Save pSrc->pData into file : size[%d] name[%s]", pDest->dataSize, fileName);
219
220                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pData, pSrc->dataSize) == false)
221                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
222
223                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
224                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
225
226                         pDest->bTextSms = false;
227
228                 } else {
229                         if (pDest->encodeType == MSG_ENCODE_8BIT)
230                                 memcpy(pDest->msgText, pSrc->pData, pSrc->dataSize);
231                         else
232                                 strncpy(pDest->msgText, (char*)pSrc->pData, pSrc->dataSize);
233                 }
234
235                 MSG_DEBUG("pData = %s",  pSrc->pData);
236                 MSG_DEBUG("msgText = %s",  pDest->msgText);
237         } else if (pSrc->mainType == MSG_MMS_TYPE) {
238
239                 pDest->bTextSms = false;
240                 pDest->dataSize = pSrc->dataSize;
241
242                 if(pSrc->subType == MSG_READREPLY_MMS) {
243                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
244                         memcpy(pDest->msgData, pSrc->pMmsData, pSrc->dataSize);
245                 } else {
246                         // Save Message Data into File
247                         char fileName[MSG_FILENAME_LEN_MAX+1];
248                         memset(fileName, 0x00, sizeof(fileName));
249
250                         if(MsgCreateFileName(fileName) == false)
251                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
252
253                         // change file extension in case of java MMS msg
254                         if (pSrc->subType == MSG_SENDREQ_JAVA_MMS) {
255                                 char* pFileNameExt;
256                                 pFileNameExt = strstr(fileName,"DATA");
257                                 strncpy(pFileNameExt,"JAVA", MSG_FILENAME_LEN_MAX);
258                         }
259
260                         MSG_DEBUG("Save Message Data into file : size[%d] name[%s]", pDest->dataSize, fileName);
261
262                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pMmsData, pSrc->dataSize) == false)
263                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
264
265                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
266                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
267                         if (pSrc->pData) {
268                                 strncpy(pDest->msgText, (char*)pSrc->pData, MAX_MSG_TEXT_LEN);
269                         }
270
271                         if (strlen(pSrc->thumbPath) > 0) {
272                                 memset(pDest->thumbPath, 0x00, sizeof(pDest->thumbPath));
273                                 memcpy(pDest->thumbPath, pSrc->thumbPath, sizeof(pDest->thumbPath));
274                         }
275                 }
276         }
277
278         MSG_END();
279 }
280
281
282 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_INFO_S *pSrc, MSG_MESSAGE_HIDDEN_S *pDest)
283 {
284         MSG_BEGIN();
285
286         pDest->msgId = pSrc->msgId;
287         pDest->threadId = pSrc->threadId;
288         pDest->folderId = pSrc->folderId;
289         pDest->mainType = pSrc->msgType.mainType;
290         pDest->subType = pSrc->msgType.subType;
291         pDest->storageId = pSrc->storageId;
292
293         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
294         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
295
296         pDest->displayTime = pSrc->displayTime;
297         pDest->networkStatus = pSrc->networkStatus;
298         pDest->encodeType = pSrc->encodeType;
299         pDest->bRead = pSrc->bRead;
300         pDest->bProtected = pSrc->bProtected;
301         pDest->bBackup = pSrc->bBackup;
302         pDest->priority = pSrc->priority;
303         pDest->direction = pSrc->direction;
304
305         // Set Port Info.
306         pDest->bPortValid = pSrc->msgPort.valid;
307
308         if (pDest->bPortValid == true) {
309                 pDest->dstPort = pSrc->msgPort.dstPort;
310                 pDest->srcPort = pSrc->msgPort.srcPort;
311         }
312
313         if(pSrc->thumbPath[0] != '\0')
314                 strncpy(pDest->thumbPath, pSrc->thumbPath, MSG_FILEPATH_LEN_MAX);
315
316         pDest->addr_list->nCount = pSrc->nAddressCnt;
317
318         msg_struct_s *addr_info_s = NULL;
319         MSG_ADDRESS_INFO_S *addr_info = NULL;
320
321         for (int i = 0; i < pDest->addr_list->nCount; i++)
322         {
323                 addr_info_s = (msg_struct_s *)pDest->addr_list->msg_struct_info[i];
324                 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
325
326                 addr_info->addressType = pSrc->addressList[i].addressType;
327                 addr_info->recipientType = pSrc->addressList[i].recipientType;
328                 addr_info->contactId = pSrc->addressList[i].contactId;
329                 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
330                 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
331                 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
332         }
333
334
335         if (pSrc->bTextSms == false) {
336                 int fileSize = 0;
337
338                 char* pFileData = NULL;
339                 AutoPtr<char> buf(&pFileData);
340
341                 pDest->dataSize = pSrc->dataSize;
342
343                 // Get Message Data from File
344                 if (pSrc->networkStatus != MSG_NETWORK_RETRIEVE_FAIL) {
345                         MSG_DEBUG("Get Message Data from file : size[%d] name[%s]\n", pDest->dataSize, pSrc->msgData);
346                         if (MsgOpenAndReadFile(pSrc->msgData, &pFileData, &fileSize) == false)
347                                 THROW(MsgException::FILE_ERROR, "MsgOpenAndReadFile error");
348
349                         if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
350                                 if (pDest->encodeType == MSG_ENCODE_8BIT) {
351                                         pDest->pData = (void*)new char[fileSize];
352                                         memset(pDest->pData, 0x00, fileSize);
353                                         memcpy(pDest->pData, pFileData, fileSize);
354                                 } else {
355                                         pDest->pData = (void*)new char[fileSize+1];
356                                         memset(pDest->pData, 0x00, fileSize+1);
357                                         strncpy((char*)pDest->pData, pFileData, fileSize);
358                                 }
359                         } else {
360                                 if (pSrc->msgText[0] != '\0') {
361                                         pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
362                                         memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
363                                         strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
364                                 }
365                                 pDest->pMmsData = (void*)new char[fileSize];
366                                 memset(pDest->pMmsData, 0x00, fileSize);
367                                 memcpy(pDest->pMmsData, pFileData, fileSize);
368                         }
369                 }
370         } else {
371                 pDest->dataSize = pSrc->dataSize;
372
373                 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
374                         if (pDest->encodeType == MSG_ENCODE_8BIT || pSrc->msgType.subType == MSG_ETWS_SMS) {
375                                 pDest->pData = (void*)new char[pDest->dataSize];
376                                 memset(pDest->pData, 0x00, pDest->dataSize);
377                                 memcpy(pDest->pData, pSrc->msgText, pDest->dataSize);
378                         } else {
379                                 pDest->pData = (void*)new char[pDest->dataSize+1];
380                                 memset(pDest->pData, 0x00, pDest->dataSize+1);
381                                 strncpy((char*)pDest->pData, pSrc->msgText, pDest->dataSize);
382                         }
383                 } else {
384                         if (pSrc->msgText[0] != '\0') {
385                                 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
386                                 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
387                                 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
388                         }
389
390                         pDest->pMmsData = (void*)new char[pDest->dataSize];
391                         memset(pDest->pMmsData, 0x00, pDest->dataSize);
392                         memcpy(pDest->pMmsData, pSrc->msgData, pDest->dataSize);
393                 }
394         }
395
396         MSG_END();
397 }
398
399
400 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_S* pSrc, MSG_SENDINGOPT_INFO_S* pDest, MSG_MESSAGE_TYPE_S msgType)
401 {
402         MSG_BEGIN();
403
404         pDest->bSetting = pSrc->bSetting;
405
406         if (pDest->bSetting == false) {
407                 MSG_DEBUG("No Sending Option");
408                 return;
409         }
410
411         pDest->bDeliverReq = pSrc->bDeliverReq;
412         pDest->bKeepCopy = pSrc->bKeepCopy;
413
414         MSG_DEBUG("pDest->bSetting = %d", pDest->bSetting);
415         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
416         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
417
418         if (msgType.mainType == MSG_SMS_TYPE) {
419                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->smsSendOpt;
420                 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
421                 pDest->option.smsSendOptInfo.bReplyPath = pSms->bReplyPath;
422         } else if (msgType.mainType == MSG_MMS_TYPE) {
423                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->mmsSendOpt;
424                 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
425                 pDest->option.mmsSendOptInfo.priority = pMms->priority;
426                 pDest->option.mmsSendOptInfo.bReadReq = pMms->bReadReq;
427
428                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
429                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
430
431                 if (pMms->expiryTime == 0) {
432                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_NONE;
433                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
434                 } else {
435                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_RELATIVE;
436                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
437                 }
438
439                 if (pMms->bUseDeliveryCustomTime == true) {
440                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
441                 } else {
442                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
443                 }
444                 pDest->option.mmsSendOptInfo.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
445                 pDest->option.mmsSendOptInfo.deliveryTime.time = pMms->deliveryTime;
446
447                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pDest->option.mmsSendOptInfo.expiryTime.time);
448         }
449
450         MSG_END();
451 }
452
453
454 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_INFO_S* pSrc, MSG_SENDINGOPT_S* pDest, MSG_MESSAGE_TYPE_S msgType)
455 {
456         MSG_BEGIN();
457
458         pDest->bDeliverReq = pSrc->bDeliverReq;
459         pDest->bKeepCopy = pSrc->bKeepCopy;
460
461         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
462         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
463
464         if (msgType.mainType == MSG_SMS_TYPE) {
465                 msg_struct_s *pStruct = (msg_struct_s *)pDest->smsSendOpt;
466                 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
467                 pSms->bReplyPath = pSrc->option.smsSendOptInfo.bReplyPath;
468         } else if (msgType.mainType == MSG_MMS_TYPE) {
469                 msg_struct_s *pStruct = (msg_struct_s *)pDest->mmsSendOpt;
470                 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
471                 pMms->priority = pSrc->option.mmsSendOptInfo.priority;
472                 pMms->bReadReq = pSrc->option.mmsSendOptInfo.bReadReq;
473                 pMms->expiryTime = pSrc->option.mmsSendOptInfo.expiryTime.time;
474                 pMms->deliveryTime = pSrc->option.mmsSendOptInfo.deliveryTime.time;
475
476                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
477                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
478                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pMms->expiryTime);
479         }
480
481         MSG_END();
482 }
483
484
485 int MsgHandle::getSettingCmdSize(MSG_OPTION_TYPE_T optionType)
486 {
487         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_OPTION_TYPE_T);
488
489         switch (optionType)
490         {
491                 case MSG_GENERAL_OPT :
492                         cmdSize += sizeof(MSG_GENERAL_OPT_S);
493                 break;
494                 case MSG_SMS_SENDOPT :
495                         cmdSize += sizeof(MSG_SMS_SENDOPT_S);
496                 break;
497                 case MSG_SMSC_LIST :
498                         cmdSize += sizeof(MSG_SMSC_LIST_S);
499                 break;
500                 case MSG_MMS_SENDOPT :
501                         cmdSize += sizeof(MSG_MMS_SENDOPT_S);
502                 break;
503                 case MSG_MMS_RECVOPT :
504                         cmdSize += sizeof(MSG_MMS_RECVOPT_S);
505                 break;
506                 case MSG_MMS_STYLEOPT :
507                         cmdSize += sizeof(MSG_MMS_STYLEOPT_S);
508                 break;
509                 case MSG_PUSHMSG_OPT :
510                         cmdSize += sizeof(MSG_PUSHMSG_OPT_S);
511                 break;
512                 case MSG_CBMSG_OPT :
513                         cmdSize += sizeof(MSG_CBMSG_OPT_S);
514                 break;
515                 case MSG_VOICEMAIL_OPT :
516                         cmdSize += sizeof(MSG_VOICEMAIL_OPT_S);
517                 break;
518                 case MSG_MSGSIZE_OPT :
519                         cmdSize += sizeof(MSG_MSGSIZE_OPT_S);
520                 break;
521         }
522
523         return cmdSize;
524 }
525
526
527 bool MsgHandle::CheckEventData(char *pEventData)
528 {
529         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
530
531         switch (pEvent->eventType)
532         {
533         case MSG_EVENT_PLG_SENT_STATUS_CNF :
534         case MSG_EVENT_PLG_INCOMING_MSG_IND :
535         case MSG_EVENT_PLG_INCOMING_MMS_CONF :
536         case MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND :
537         case MSG_EVENT_PLG_INCOMING_LBS_MSG_IND :
538         case MSG_EVENT_SYNCML_OPERATION :
539         case MSG_EVENT_PLG_STORAGE_CHANGE_IND :
540         case MSG_EVENT_PLG_INCOMING_CB_MSG_IND :
541         case MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND :
542                 return false;
543                 break;
544         default :
545                 return true;
546                 break;
547         }
548
549         return true;
550 }