Chnage Copyright Year from 2012 to 2012-2013
[platform/core/messaging/msg-service.git] / proxy / MsgHandleControl.cpp
1 /*
2 * Copyright 2012-2013  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         // Receive Result from MSG FW
123         read(ppEvent);
124
125         if (*ppEvent == NULL) {
126                 THROW(MsgException::INVALID_RESULT, "event is NULL");
127         }
128
129 }
130
131
132 void MsgHandle::read(char **ppEvent)
133 {
134         unsigned int dataSize = 0;
135
136         dataSize = mClientSock.read(ppEvent, &dataSize);
137
138         if (dataSize == 0) {
139                 THROW(MsgException::IPC_ERROR, "Server closed connection");
140         }
141 }
142
143
144 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_HIDDEN_S *pSrc, MSG_MESSAGE_INFO_S *pDest)
145 {
146         MSG_BEGIN();
147
148         pDest->msgId = pSrc->msgId;
149         pDest->threadId = pSrc->threadId;
150         pDest->folderId = pSrc->folderId;
151         pDest->msgType.mainType = pSrc->mainType;
152         pDest->msgType.subType = pSrc->subType;
153         pDest->msgType.classType= pSrc->classType;
154         pDest->storageId = pSrc->storageId;
155
156         msg_struct_list_s *addr_info_s = pSrc->addr_list;
157
158         if (addr_info_s) {
159                 msg_struct_s *addr_info = NULL;
160                 MSG_ADDRESS_INFO_S *address = NULL;
161
162                 pDest->nAddressCnt = addr_info_s->nCount;
163
164                 for (int i = 0; i < addr_info_s->nCount; i++)
165                 {
166                         addr_info = (msg_struct_s *)addr_info_s->msg_struct_info[i];
167                         address = (MSG_ADDRESS_INFO_S *)addr_info->data;
168
169                         pDest->addressList[i].addressType = address->addressType;
170                         pDest->addressList[i].recipientType = address->recipientType;
171                         pDest->addressList[i].contactId = address->contactId;
172                         strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
173                         strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
174                         pDest->addressList[i].displayName[MAX_DISPLAY_NAME_LEN] = '\0';
175                 }
176         }
177
178         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
179         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
180
181         pDest->displayTime = pSrc->displayTime;
182         pDest->networkStatus = pSrc->networkStatus;
183         pDest->encodeType = pSrc->encodeType;
184         pDest->bRead = pSrc->bRead;
185         pDest->bProtected = pSrc->bProtected;
186         pDest->bBackup = pSrc->bBackup;
187         pDest->priority = pSrc->priority;
188         pDest->direction = pSrc->direction;
189
190         // Set Port Info.
191         pDest->msgPort.valid = pSrc->bPortValid;
192
193         if (pDest->msgPort.valid == true) {
194                 pDest->msgPort.dstPort = pSrc->dstPort;
195                 pDest->msgPort.srcPort = pSrc->srcPort;
196         }
197
198         MSG_DEBUG("nSize = %d",  pSrc->dataSize);
199
200         if (pSrc->mainType == MSG_SMS_TYPE){
201                 pDest->bTextSms = true;
202                 pDest->dataSize = pSrc->dataSize;
203
204                 memset(pDest->msgText, 0x00, sizeof(pDest->msgText));
205
206                 if (pSrc->dataSize > MAX_MSG_TEXT_LEN) {
207                         // Save Message Data into File
208                         char fileName[MSG_FILENAME_LEN_MAX+1];
209                         memset(fileName, 0x00, sizeof(fileName));
210
211                         if(MsgCreateFileName(fileName) == false)
212                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
213
214                         MSG_DEBUG("Save pSrc->pData into file : size[%d] name[%s]", pDest->dataSize, fileName);
215
216                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pData, pSrc->dataSize) == false)
217                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
218
219                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
220                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
221
222                         pDest->bTextSms = false;
223
224                 } else {
225                         if (pDest->encodeType == MSG_ENCODE_8BIT)
226                                 memcpy(pDest->msgText, pSrc->pData, pSrc->dataSize);
227                         else
228                                 strncpy(pDest->msgText, (char*)pSrc->pData, pSrc->dataSize);
229                 }
230
231                 MSG_DEBUG("pData = %s",  pSrc->pData);
232                 MSG_DEBUG("msgText = %s",  pDest->msgText);
233         } else if (pSrc->mainType == MSG_MMS_TYPE) {
234
235                 pDest->bTextSms = false;
236                 pDest->dataSize = pSrc->dataSize;
237
238                 if(pSrc->subType == MSG_READREPLY_MMS) {
239                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
240                         memcpy(pDest->msgData, pSrc->pMmsData, pSrc->dataSize);
241                 } else {
242                         // Save Message Data into File
243                         char fileName[MSG_FILENAME_LEN_MAX+1];
244                         memset(fileName, 0x00, sizeof(fileName));
245
246                         if(MsgCreateFileName(fileName) == false)
247                                 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
248
249                         // change file extension in case of java MMS msg
250                         if (pSrc->subType == MSG_SENDREQ_JAVA_MMS) {
251                                 char* pFileNameExt;
252                                 pFileNameExt = strstr(fileName,"DATA");
253                                 strncpy(pFileNameExt,"JAVA", MSG_FILENAME_LEN_MAX);
254                         }
255
256                         MSG_DEBUG("Save Message Data into file : size[%d] name[%s]", pDest->dataSize, fileName);
257
258                         if (MsgWriteIpcFile(fileName, (char*)pSrc->pMmsData, pSrc->dataSize) == false)
259                                 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
260
261                         memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
262                         strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
263                         if (pSrc->pData) {
264                                 strncpy(pDest->msgText, (char*)pSrc->pData, MAX_MSG_TEXT_LEN);
265                         }
266
267                         if (strlen(pSrc->thumbPath) > 0) {
268                                 memset(pDest->thumbPath, 0x00, sizeof(pDest->thumbPath));
269                                 memcpy(pDest->thumbPath, pSrc->thumbPath, sizeof(pDest->thumbPath));
270                         }
271                 }
272         }
273
274         MSG_END();
275 }
276
277
278 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_INFO_S *pSrc, MSG_MESSAGE_HIDDEN_S *pDest)
279 {
280         MSG_BEGIN();
281
282         pDest->msgId = pSrc->msgId;
283         pDest->threadId = pSrc->threadId;
284         pDest->folderId = pSrc->folderId;
285         pDest->mainType = pSrc->msgType.mainType;
286         pDest->subType = pSrc->msgType.subType;
287         pDest->storageId = pSrc->storageId;
288
289         strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
290         strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
291
292         pDest->displayTime = pSrc->displayTime;
293         pDest->networkStatus = pSrc->networkStatus;
294         pDest->encodeType = pSrc->encodeType;
295         pDest->bRead = pSrc->bRead;
296         pDest->bProtected = pSrc->bProtected;
297         pDest->bBackup = pSrc->bBackup;
298         pDest->priority = pSrc->priority;
299         pDest->direction = pSrc->direction;
300
301         // Set Port Info.
302         pDest->bPortValid = pSrc->msgPort.valid;
303
304         if (pDest->bPortValid == true) {
305                 pDest->dstPort = pSrc->msgPort.dstPort;
306                 pDest->srcPort = pSrc->msgPort.srcPort;
307         }
308
309         if(pSrc->thumbPath[0] != '\0')
310                 strncpy(pDest->thumbPath, pSrc->thumbPath, MSG_FILEPATH_LEN_MAX);
311
312         pDest->addr_list->nCount = pSrc->nAddressCnt;
313
314         msg_struct_s *addr_info_s = NULL;
315         MSG_ADDRESS_INFO_S *addr_info = NULL;
316
317         for (int i = 0; i < pDest->addr_list->nCount; i++)
318         {
319                 addr_info_s = (msg_struct_s *)pDest->addr_list->msg_struct_info[i];
320                 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
321
322                 addr_info->addressType = pSrc->addressList[i].addressType;
323                 addr_info->recipientType = pSrc->addressList[i].recipientType;
324                 addr_info->contactId = pSrc->addressList[i].contactId;
325                 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
326                 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
327                 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
328         }
329
330
331         if (pSrc->bTextSms == false) {
332                 int fileSize = 0;
333
334                 char* pFileData = NULL;
335                 AutoPtr<char> buf(&pFileData);
336
337                 pDest->dataSize = pSrc->dataSize;
338
339                 // Get Message Data from File
340                 if (pSrc->networkStatus != MSG_NETWORK_RETRIEVE_FAIL) {
341                         MSG_DEBUG("Get Message Data from file : size[%d] name[%s]\n", pDest->dataSize, pSrc->msgData);
342                         if (MsgOpenAndReadFile(pSrc->msgData, &pFileData, &fileSize) == false)
343                                 THROW(MsgException::FILE_ERROR, "MsgOpenAndReadFile error");
344
345                         if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
346                                 if (pDest->encodeType == MSG_ENCODE_8BIT) {
347                                         pDest->pData = (void*)new char[fileSize];
348                                         memset(pDest->pData, 0x00, fileSize);
349                                         memcpy(pDest->pData, pFileData, fileSize);
350                                 } else {
351                                         pDest->pData = (void*)new char[fileSize+1];
352                                         memset(pDest->pData, 0x00, fileSize+1);
353                                         strncpy((char*)pDest->pData, pFileData, fileSize);
354                                 }
355                         } else {
356                                 if (pSrc->msgText[0] != '\0') {
357                                         pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
358                                         memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
359                                         strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
360                                 }
361                                 pDest->pMmsData = (void*)new char[fileSize];
362                                 memset(pDest->pMmsData, 0x00, fileSize);
363                                 memcpy(pDest->pMmsData, pFileData, fileSize);
364                         }
365                 }
366         } else {
367                 pDest->dataSize = pSrc->dataSize;
368
369                 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
370                         if (pDest->encodeType == MSG_ENCODE_8BIT || pSrc->msgType.subType == MSG_ETWS_SMS) {
371                                 pDest->pData = (void*)new char[pDest->dataSize];
372                                 memset(pDest->pData, 0x00, pDest->dataSize);
373                                 memcpy(pDest->pData, pSrc->msgText, pDest->dataSize);
374                         } else {
375                                 pDest->pData = (void*)new char[pDest->dataSize+1];
376                                 memset(pDest->pData, 0x00, pDest->dataSize+1);
377                                 strncpy((char*)pDest->pData, pSrc->msgText, pDest->dataSize);
378                         }
379                 } else {
380                         if (pSrc->msgText[0] != '\0') {
381                                 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
382                                 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
383                                 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
384                         }
385
386                         pDest->pMmsData = (void*)new char[pDest->dataSize];
387                         memset(pDest->pMmsData, 0x00, pDest->dataSize);
388                         memcpy(pDest->pMmsData, pSrc->msgData, pDest->dataSize);
389                 }
390         }
391
392         MSG_END();
393 }
394
395
396 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_S* pSrc, MSG_SENDINGOPT_INFO_S* pDest, MSG_MESSAGE_TYPE_S msgType)
397 {
398         MSG_BEGIN();
399
400         pDest->bSetting = pSrc->bSetting;
401
402         if (pSrc->bSetting == false) {
403                 MSG_DEBUG("No Sending Option");
404                 return;
405         }
406
407         pDest->bDeliverReq = pSrc->bDeliverReq;
408         pDest->bKeepCopy = pSrc->bKeepCopy;
409
410         MSG_DEBUG("pDest->bSetting = %d", pDest->bSetting);
411         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
412         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
413
414         if (msgType.mainType == MSG_SMS_TYPE) {
415                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->smsSendOpt;
416                 if(pStruct)
417                 {
418                         SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
419                         if(pSms)
420                         {
421                                 pDest->option.smsSendOptInfo.bReplyPath = pSms->bReplyPath;
422                         }
423                 }
424         } else if (msgType.mainType == MSG_MMS_TYPE) {
425                 msg_struct_s *pStruct = (msg_struct_s *)pSrc->mmsSendOpt;
426                 if(pStruct)
427                 {
428                         MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
429                         if(pMms)
430                         {
431                                 pDest->option.mmsSendOptInfo.priority = pMms->priority;
432                                 pDest->option.mmsSendOptInfo.bReadReq = pMms->bReadReq;
433
434                                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
435                                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
436
437                                 if (pMms->expiryTime == 0) {
438                                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_NONE;
439                                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
440                                 } else {
441                                         pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_RELATIVE;
442                                         pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
443                                 }
444
445                                 if (pMms->bUseDeliveryCustomTime == true) {
446                                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
447                                 } else {
448                                         pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
449                                 }
450                                 pDest->option.mmsSendOptInfo.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
451                                 pDest->option.mmsSendOptInfo.deliveryTime.time = pMms->deliveryTime;
452
453                                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pDest->option.mmsSendOptInfo.expiryTime.time);
454                         }
455                 }
456         }
457
458         MSG_END();
459 }
460
461
462 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_INFO_S* pSrc, MSG_SENDINGOPT_S* pDest, MSG_MESSAGE_TYPE_S msgType)
463 {
464         MSG_BEGIN();
465
466         pDest->bDeliverReq = pSrc->bDeliverReq;
467         pDest->bKeepCopy = pSrc->bKeepCopy;
468
469         MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
470         MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
471
472         if (msgType.mainType == MSG_SMS_TYPE) {
473                 msg_struct_s *pStruct = (msg_struct_s *)pDest->smsSendOpt;
474                 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
475                 pSms->bReplyPath = pSrc->option.smsSendOptInfo.bReplyPath;
476         } else if (msgType.mainType == MSG_MMS_TYPE) {
477                 msg_struct_s *pStruct = (msg_struct_s *)pDest->mmsSendOpt;
478                 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
479                 pMms->priority = pSrc->option.mmsSendOptInfo.priority;
480                 pMms->bReadReq = pSrc->option.mmsSendOptInfo.bReadReq;
481                 pMms->expiryTime = pSrc->option.mmsSendOptInfo.expiryTime.time;
482                 pMms->deliveryTime = pSrc->option.mmsSendOptInfo.deliveryTime.time;
483
484                 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
485                 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
486                 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pMms->expiryTime);
487         }
488
489         MSG_END();
490 }
491
492
493 int MsgHandle::getSettingCmdSize(MSG_OPTION_TYPE_T optionType)
494 {
495         int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_OPTION_TYPE_T);
496
497         switch (optionType)
498         {
499                 case MSG_GENERAL_OPT :
500                         cmdSize += sizeof(MSG_GENERAL_OPT_S);
501                 break;
502                 case MSG_SMS_SENDOPT :
503                         cmdSize += sizeof(MSG_SMS_SENDOPT_S);
504                 break;
505                 case MSG_SMSC_LIST :
506                         cmdSize += sizeof(MSG_SMSC_LIST_S);
507                 break;
508                 case MSG_MMS_SENDOPT :
509                         cmdSize += sizeof(MSG_MMS_SENDOPT_S);
510                 break;
511                 case MSG_MMS_RECVOPT :
512                         cmdSize += sizeof(MSG_MMS_RECVOPT_S);
513                 break;
514                 case MSG_MMS_STYLEOPT :
515                         cmdSize += sizeof(MSG_MMS_STYLEOPT_S);
516                 break;
517                 case MSG_PUSHMSG_OPT :
518                         cmdSize += sizeof(MSG_PUSHMSG_OPT_S);
519                 break;
520                 case MSG_CBMSG_OPT :
521                         cmdSize += sizeof(MSG_CBMSG_OPT_S);
522                 break;
523                 case MSG_VOICEMAIL_OPT :
524                         cmdSize += sizeof(MSG_VOICEMAIL_OPT_S);
525                 break;
526                 case MSG_MSGSIZE_OPT :
527                         cmdSize += sizeof(MSG_MSGSIZE_OPT_S);
528                 break;
529         }
530
531         return cmdSize;
532 }
533
534
535 bool MsgHandle::CheckEventData(char *pEventData)
536 {
537         MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
538
539         switch (pEvent->eventType)
540         {
541         case MSG_EVENT_PLG_SENT_STATUS_CNF :
542         case MSG_EVENT_PLG_INCOMING_MSG_IND :
543         case MSG_EVENT_PLG_INCOMING_MMS_CONF :
544         case MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND :
545         case MSG_EVENT_PLG_INCOMING_LBS_MSG_IND :
546         case MSG_EVENT_SYNCML_OPERATION :
547         case MSG_EVENT_PLG_STORAGE_CHANGE_IND :
548         case MSG_EVENT_PLG_INCOMING_CB_MSG_IND :
549         case MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND :
550                 return false;
551                 break;
552         default :
553                 return true;
554                 break;
555         }
556
557         return true;
558 }