2.0_beta
[platform/core/messaging/msg-service.git] / utils / MsgSpamFilter.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 "MsgDebug.h"
18 #include "MsgUtilFile.h"
19 #include "MsgCppTypes.h"
20 #include "MsgGconfWrapper.h"
21 #include "MsgSpamFilter.h"
22
23 extern "C" {
24 #include <phone-misc.h>
25 }
26
27 /*==================================================================================================
28                                      VARIABLES
29 ==================================================================================================*/
30 phone_misc_h *calllogHandle = NULL;
31
32
33 /*==================================================================================================
34                                      FUNCTION IMPLEMENTATION
35 ==================================================================================================*/
36 msg_error_t MsgSetFilterOperation(bool bSetFlag)
37 {
38         MSG_BEGIN();
39
40         if (MsgSettingSetBool(MSG_BLOCK_MESSAGE, bSetFlag) != MSG_SUCCESS) {
41                 MSG_DEBUG("Error to set config data [%s]", MSG_BLOCK_MESSAGE);
42                 return MSG_ERR_SET_SETTING;
43         }
44
45         MSG_END();
46
47         return MSG_SUCCESS;
48 }
49
50
51 msg_error_t MsgGetFilterOperation(bool *pSetFlag)
52 {
53         MSG_BEGIN();
54
55         MsgSettingGetBool(MSG_BLOCK_MESSAGE, pSetFlag);
56
57         MSG_END();
58
59         return MSG_SUCCESS;
60 }
61
62
63 void GetValidNumber(const char *pSrcNum, char *pDestNum)
64 {
65         int overLen = 0, i = 0;
66
67         overLen = strlen(pSrcNum) - VALID_ADDRESS_LEN;
68
69         for (i = 0; i < VALID_ADDRESS_LEN; i++)
70                 pDestNum[i] = pSrcNum[i+overLen];
71
72         pDestNum[i] = '\0';
73 }
74
75
76 bool MsgCheckFilter(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsgInfo)
77 {
78         MSG_BEGIN();
79
80         msg_error_t err = MSG_SUCCESS;
81
82         // =======================================================================
83         // Check Block List of Call-log (Auto Reject)
84         // =======================================================================
85         int ret = 0;
86
87         MSG_DEBUG("bAutoReject = [%d], bUnknownAutoReject = [%d]", MsgSettingGetAutoReject(), MsgSettingGetUnknownAutoReject());
88
89         if (MsgSettingGetAutoReject()) {
90                 char checkNumber[MAX_ADDRESS_VAL_LEN+1] = {0,};
91                 snprintf(checkNumber, MAX_ADDRESS_VAL_LEN, "%s", pMsgInfo->addressList[0].addressVal);
92
93                 if (checkNumber[0] == '\0') {
94                         if (MsgSettingGetUnknownAutoReject())
95                                 return true;
96                 } else {
97                         ret = phone_misc_block_check(calllogHandle, checkNumber);
98                         MSG_DEBUG("phone_misc_block_check [%d]", ret);
99                 }
100
101                 if (ret > 0) {
102                         MSG_DEBUG("Message is Rejected by auto-reject option.");
103                         return true;
104                 }
105         }
106
107         // =======================================================================
108         // Check Filter Operation
109         // =======================================================================
110         bool filterFlag = false;
111
112         MsgGetFilterOperation(&filterFlag);
113
114         if (filterFlag == false) {
115                 MSG_DEBUG("filter operation is not working");
116                 return false;
117         }
118
119         // =======================================================================
120         // Check Filter by Address
121         // =======================================================================
122         int rowCnt = 0;
123
124         MSG_DEBUG("pMsg->addressList[0].addressVal [%s]", pMsgInfo->addressList[0].addressVal);
125
126         char newNumber[VALID_ADDRESS_LEN+1];
127         char sqlQuery[MAX_QUERY_LEN+1];
128
129         if (strlen(pMsgInfo->addressList[0].addressVal) > VALID_ADDRESS_LEN) {
130                 memset(newNumber, 0x00, sizeof(newNumber));
131                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
132
133                 GetValidNumber(pMsgInfo->addressList[0].addressVal, newNumber);
134
135                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND FILTER_VALUE LIKE '%%%s';",
136                                 MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_ADDRESS, newNumber);
137         } else {
138                 memset(sqlQuery, 0x00, sizeof(sqlQuery));
139
140                 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND FILTER_VALUE = '%s';",
141                                 MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_ADDRESS, pMsgInfo->addressList[0].addressVal);
142         }
143
144         err = pDbHandle->getTable(sqlQuery, &rowCnt);
145
146         if (rowCnt > 0) {
147                 MSG_DEBUG("Msg is Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
148
149                 pDbHandle->freeTable();
150
151                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
152
153                 return true;
154         } else {
155                 MSG_DEBUG("Msg is NOT Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
156
157                 pDbHandle->freeTable();
158         }
159
160         // =======================================================================
161         // Check Filter by Subject
162         // =======================================================================
163         // Get Filter List
164         memset(sqlQuery, 0x00, sizeof(sqlQuery));
165
166         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_VALUE FROM %s WHERE FILTER_TYPE = %d;",
167                         MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_SUBJECT);
168
169         rowCnt = 0;
170
171         err = pDbHandle->getTable(sqlQuery, &rowCnt);
172
173         if (err != MSG_SUCCESS) {
174                 MSG_DEBUG("MsgGetTable() Error [%d] : [%s]", err, sqlQuery);
175
176                 pDbHandle->freeTable();
177
178                 return false;
179         }
180
181         char filterValue[MAX_FILTER_VALUE_LEN+1];
182
183         char* pData = NULL;
184         AutoPtr<char> buf(&pData);
185
186         int fileSize = 0;
187         bool bFiltered = false;
188
189         for (int i = 1; i <= rowCnt; i++)
190         {
191                 memset(filterValue, 0x00, sizeof(filterValue));
192
193                 pDbHandle->getColumnToString(i, MAX_FILTER_VALUE_LEN, filterValue);
194
195                 MSG_DEBUG("filterValue [%s]", filterValue);
196
197                 if (strlen(filterValue) <= 0) continue;
198
199                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE && pMsgInfo->msgType.subType == MSG_NORMAL_SMS) {
200                         if (pMsgInfo->bTextSms == false) {
201                                 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pData, &fileSize) == false) {
202                                         pDbHandle->freeTable();
203                                         return false;
204                                 }
205                                 MSG_DEBUG("file data [%s]", pData);
206                         } else {
207                                 if (pMsgInfo->dataSize > 0) {
208                                         pData = new char[pMsgInfo->dataSize+1];
209
210                                         strncpy(pData, pMsgInfo->msgText, pMsgInfo->dataSize);
211                                         pData[strlen(pMsgInfo->msgText)] = '\0';
212                                 }
213                         }
214                 } else if(pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
215                         if (strlen(pMsgInfo->subject) > 0) {
216                                 pData = new char[strlen(pMsgInfo->subject)+1];
217
218                                 strncpy(pData, pMsgInfo->subject, strlen(pMsgInfo->subject));
219                                 pData[strlen(pMsgInfo->subject)] = '\0';
220                         }
221                 }
222
223                 // NULL value check
224                 if (pData == NULL) {
225                         MSG_DEBUG("pData is NULL");
226
227                         bFiltered = false;
228                         break;
229                 }
230
231                 MSG_DEBUG("pData [%s]", pData);
232
233                 if (strcasestr(pData, filterValue) != NULL) {
234                         MSG_DEBUG("Msg is Filtered by Subject [%s] Data [%s]", filterValue, pData);
235
236                         bFiltered = true;
237                         break;
238                 }
239         }
240
241         pDbHandle->freeTable();
242
243         if (bFiltered == true) {
244                 MSG_DEBUG("Msg is Filtered by Subject");
245
246                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
247
248                 return true;
249         } else {
250                 MSG_DEBUG("Msg is NOT Filtered by Subject");
251         }
252
253         MSG_END();
254
255         return false;
256 }
257
258 void MsgCalllogDBInit(void)
259 {
260         MSG_BEGIN();
261
262         if (calllogHandle == NULL)
263                 calllogHandle = phone_misc_connect();
264
265         MSG_END();
266 }
267
268 void MsgCalllogDBFinish(void)
269 {
270         MSG_BEGIN();
271
272         if (calllogHandle != NULL) {
273                 if (phone_misc_disconnect(calllogHandle) == PH_MISC_SUCCESS)
274                         calllogHandle = NULL;
275         }
276
277         MSG_END();
278 }