2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://www.tizenopensource.org/license
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.
18 #include "MsgUtilFile.h"
19 #include "MsgCppTypes.h"
20 #include "MsgGconfWrapper.h"
21 #include "MsgSpamFilter.h"
23 /*==================================================================================================
24 FUNCTION IMPLEMENTATION
25 ==================================================================================================*/
26 msg_error_t MsgSetFilterOperation(bool bSetFlag)
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;
41 msg_error_t MsgGetFilterOperation(bool *pSetFlag)
45 MsgSettingGetBool(MSG_BLOCK_MESSAGE, pSetFlag);
53 bool MsgCheckFilter(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsgInfo)
57 msg_error_t err = MSG_SUCCESS;
59 // =======================================================================
60 // Check Filter Operation
61 // =======================================================================
62 bool filterFlag = false;
64 MsgGetFilterOperation(&filterFlag);
66 if (filterFlag == false) {
67 MSG_DEBUG("filter operation is not working");
71 // =======================================================================
72 // Check Filter by Address
73 // =======================================================================
76 MSG_DEBUG("pMsg->addressList[0].addressVal [%s]", pMsgInfo->addressList[0].addressVal);
78 char sqlQuery[MAX_QUERY_LEN+1];
80 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
89 err = pDbHandle->getTable(sqlQuery, &rowCnt);
91 if (err == MSG_ERR_DB_GETTABLE) {
92 MSG_DEBUG("sqlQuery [%s]", sqlQuery);
96 MSG_DEBUG("Msg is Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
97 pDbHandle->freeTable();
98 pMsgInfo->folderId = MSG_SPAMBOX_ID;
101 MSG_DEBUG("Msg is NOT Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
102 pDbHandle->freeTable();
105 // =======================================================================
106 // Check Filter by Subject
107 // =======================================================================
109 memset(sqlQuery, 0x00, sizeof(sqlQuery));
111 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_VALUE FROM %s WHERE FILTER_TYPE = %d;",
112 MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_WORD);
116 err = pDbHandle->getTable(sqlQuery, &rowCnt);
118 if (err != MSG_SUCCESS) {
119 MSG_DEBUG("MsgGetTable() Error [%d] : [%s]", err, sqlQuery);
121 pDbHandle->freeTable();
126 char filterValue[MAX_FILTER_VALUE_LEN+1];
129 AutoPtr<char> buf(&pData);
132 bool bFiltered = false;
134 for (int i = 1; i <= rowCnt; i++)
136 memset(filterValue, 0x00, sizeof(filterValue));
138 pDbHandle->getColumnToString(i, MAX_FILTER_VALUE_LEN, filterValue);
140 MSG_DEBUG("filterValue [%s]", filterValue);
142 if (strlen(filterValue) <= 0) continue;
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();
150 MSG_DEBUG("file data [%s]", pData);
152 if (pMsgInfo->dataSize > 0) {
153 pData = new char[pMsgInfo->dataSize+1];
155 strncpy(pData, pMsgInfo->msgText, pMsgInfo->dataSize);
156 pData[strlen(pMsgInfo->msgText)] = '\0';
159 } else if(pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
160 if (strlen(pMsgInfo->subject) > 0) {
161 pData = new char[strlen(pMsgInfo->subject)+1];
163 strncpy(pData, pMsgInfo->subject, strlen(pMsgInfo->subject));
164 pData[strlen(pMsgInfo->subject)] = '\0';
170 MSG_DEBUG("pData is NULL");
176 MSG_DEBUG("pData [%s]", pData);
178 if (strcasestr(pData, filterValue) != NULL) {
179 MSG_DEBUG("Msg is Filtered by Subject [%s] Data [%s]", filterValue, pData);
186 pDbHandle->freeTable();
188 if (bFiltered == true) {
189 MSG_DEBUG("Msg is Filtered by Subject");
191 pMsgInfo->folderId = MSG_SPAMBOX_ID;
195 MSG_DEBUG("Msg is NOT Filtered by Subject");