Merge branch 'master' into tizen_2.0
[platform/core/messaging/msg-service.git] / utils / MsgSpamFilter.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://floralicense.org
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 /*==================================================================================================
24                                      FUNCTION IMPLEMENTATION
25 ==================================================================================================*/
26 msg_error_t MsgSetFilterOperation(bool bSetFlag)
27 {
28         MSG_BEGIN();
29
30         if (MsgSettingSetBool(MSG_BLOCK_MESSAGE, bSetFlag) != MSG_SUCCESS) {
31                 MSG_DEBUG("Error to set config data [%s]", MSG_BLOCK_MESSAGE);
32                 return MSG_ERR_SET_SETTING;
33         }
34
35         MSG_END();
36
37         return MSG_SUCCESS;
38 }
39
40
41 msg_error_t MsgGetFilterOperation(bool *pSetFlag)
42 {
43         MSG_BEGIN();
44
45         MsgSettingGetBool(MSG_BLOCK_MESSAGE, pSetFlag);
46
47         MSG_END();
48
49         return MSG_SUCCESS;
50 }
51
52
53 bool MsgCheckFilter(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsgInfo)
54 {
55         MSG_BEGIN();
56
57         msg_error_t err = MSG_SUCCESS;
58
59         // =======================================================================
60         // Check Filter Operation
61         // =======================================================================
62         bool filterFlag = false;
63
64         MsgGetFilterOperation(&filterFlag);
65
66         if (filterFlag == false) {
67                 MSG_DEBUG("filter operation is not working");
68                 return false;
69         }
70
71         // =======================================================================
72         // Check Filter by Address
73         // =======================================================================
74         int rowCnt = 0;
75
76         MSG_DEBUG("pMsg->addressList[0].addressVal [%s]", pMsgInfo->addressList[0].addressVal);
77
78         char sqlQuery[MAX_QUERY_LEN+1];
79
80         memset(sqlQuery, 0x00, sizeof(sqlQuery));
81
82         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND '%s' LIKE (CASE WHEN LENGTH(FILTER_VALUE)>%d-1 THEN '%%'||SUBSTR(FILTER_VALUE, LENGTH(FILTER_VALUE)-%d-1) ELSE FILTER_VALUE END) AND FILTER_ACTIVE = 1 \
83                         UNION SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND '%s' LIKE SUBSTR(FILTER_VALUE,1)||'%%' AND FILTER_ACTIVE = 1 \
84                         UNION SELECT FILTER_ID FROM %s WHERE FILTER_TYPE = %d AND '%s' LIKE '%%'||SUBSTR(FILTER_VALUE,1)||'%%' AND FILTER_ACTIVE = 1;",
85                         MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_ADDRESS_SAME, pMsgInfo->addressList[0].addressVal, MAX_PRECONFIG_NUM, MAX_PRECONFIG_NUM,
86                         MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_ADDRESS_START, pMsgInfo->addressList[0].addressVal,
87                         MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_ADDRESS_INCLUDE, pMsgInfo->addressList[0].addressVal);
88
89         err = pDbHandle->getTable(sqlQuery, &rowCnt);
90
91         if (err == MSG_ERR_DB_GETTABLE) {
92                 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
93         }
94
95         if (rowCnt > 0) {
96                 MSG_DEBUG("Msg is Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
97                 pDbHandle->freeTable();
98                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
99                 return true;
100         } else {
101                 MSG_DEBUG("Msg is NOT Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
102                 pDbHandle->freeTable();
103         }
104
105         // =======================================================================
106         // Check Filter by Subject
107         // =======================================================================
108         // Get Filter List
109         memset(sqlQuery, 0x00, sizeof(sqlQuery));
110
111         snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_VALUE FROM %s WHERE FILTER_TYPE = %d;",
112                         MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_WORD);
113
114         rowCnt = 0;
115
116         err = pDbHandle->getTable(sqlQuery, &rowCnt);
117
118         if (err != MSG_SUCCESS) {
119                 MSG_DEBUG("MsgGetTable() Error [%d] : [%s]", err, sqlQuery);
120
121                 pDbHandle->freeTable();
122
123                 return false;
124         }
125
126         char filterValue[MAX_FILTER_VALUE_LEN+1];
127
128         char* pData = NULL;
129         AutoPtr<char> buf(&pData);
130
131         int fileSize = 0;
132         bool bFiltered = false;
133
134         for (int i = 1; i <= rowCnt; i++)
135         {
136                 memset(filterValue, 0x00, sizeof(filterValue));
137
138                 pDbHandle->getColumnToString(i, MAX_FILTER_VALUE_LEN, filterValue);
139
140                 MSG_DEBUG("filterValue [%s]", filterValue);
141
142                 if (strlen(filterValue) <= 0) continue;
143
144                 if (pMsgInfo->msgType.mainType == MSG_SMS_TYPE && pMsgInfo->msgType.subType == MSG_NORMAL_SMS) {
145                         if (pMsgInfo->bTextSms == false) {
146                                 if (MsgOpenAndReadFile(pMsgInfo->msgData, &pData, &fileSize) == false) {
147                                         pDbHandle->freeTable();
148                                         return false;
149                                 }
150                                 MSG_DEBUG("file data [%s]", pData);
151                         } else {
152                                 if (pMsgInfo->dataSize > 0) {
153                                         pData = new char[pMsgInfo->dataSize+1];
154
155                                         strncpy(pData, pMsgInfo->msgText, pMsgInfo->dataSize);
156                                         pData[strlen(pMsgInfo->msgText)] = '\0';
157                                 }
158                         }
159                 } else if(pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
160                         if (strlen(pMsgInfo->subject) > 0) {
161                                 pData = new char[strlen(pMsgInfo->subject)+1];
162
163                                 strncpy(pData, pMsgInfo->subject, strlen(pMsgInfo->subject));
164                                 pData[strlen(pMsgInfo->subject)] = '\0';
165                         }
166                 }
167
168                 // NULL value check
169                 if (pData == NULL) {
170                         MSG_DEBUG("pData is NULL");
171
172                         bFiltered = false;
173                         break;
174                 }
175
176                 MSG_DEBUG("pData [%s]", pData);
177
178                 if (strcasestr(pData, filterValue) != NULL) {
179                         MSG_DEBUG("Msg is Filtered by Subject [%s] Data [%s]", filterValue, pData);
180
181                         bFiltered = true;
182                         break;
183                 }
184         }
185
186         pDbHandle->freeTable();
187
188         if (bFiltered == true) {
189                 MSG_DEBUG("Msg is Filtered by Subject");
190
191                 pMsgInfo->folderId = MSG_SPAMBOX_ID;
192
193                 return true;
194         } else {
195                 MSG_DEBUG("Msg is NOT Filtered by Subject");
196         }
197
198         MSG_END();
199
200         return false;
201 }