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"
24 #include <phone-misc.h>
27 /*==================================================================================================
29 ==================================================================================================*/
30 phone_misc_h *calllogHandle = NULL;
33 /*==================================================================================================
34 FUNCTION IMPLEMENTATION
35 ==================================================================================================*/
36 msg_error_t MsgSetFilterOperation(bool bSetFlag)
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;
51 msg_error_t MsgGetFilterOperation(bool *pSetFlag)
55 MsgSettingGetBool(MSG_BLOCK_MESSAGE, pSetFlag);
63 void GetValidNumber(const char *pSrcNum, char *pDestNum)
65 int overLen = 0, i = 0;
67 overLen = strlen(pSrcNum) - VALID_ADDRESS_LEN;
69 for (i = 0; i < VALID_ADDRESS_LEN; i++)
70 pDestNum[i] = pSrcNum[i+overLen];
76 bool MsgCheckFilter(MsgDbHandler *pDbHandle, MSG_MESSAGE_INFO_S *pMsgInfo)
80 msg_error_t err = MSG_SUCCESS;
82 // =======================================================================
83 // Check Block List of Call-log (Auto Reject)
84 // =======================================================================
87 MSG_DEBUG("bAutoReject = [%d], bUnknownAutoReject = [%d]", MsgSettingGetAutoReject(), MsgSettingGetUnknownAutoReject());
89 if (MsgSettingGetAutoReject()) {
90 char checkNumber[MAX_ADDRESS_VAL_LEN+1] = {0,};
91 snprintf(checkNumber, MAX_ADDRESS_VAL_LEN, "%s", pMsgInfo->addressList[0].addressVal);
93 if (checkNumber[0] == '\0') {
94 if (MsgSettingGetUnknownAutoReject())
97 ret = phone_misc_block_check(calllogHandle, checkNumber);
98 MSG_DEBUG("phone_misc_block_check [%d]", ret);
102 MSG_DEBUG("Message is Rejected by auto-reject option.");
107 // =======================================================================
108 // Check Filter Operation
109 // =======================================================================
110 bool filterFlag = false;
112 MsgGetFilterOperation(&filterFlag);
114 if (filterFlag == false) {
115 MSG_DEBUG("filter operation is not working");
119 // =======================================================================
120 // Check Filter by Address
121 // =======================================================================
124 MSG_DEBUG("pMsg->addressList[0].addressVal [%s]", pMsgInfo->addressList[0].addressVal);
126 char newNumber[VALID_ADDRESS_LEN+1];
127 char sqlQuery[MAX_QUERY_LEN+1];
129 if (strlen(pMsgInfo->addressList[0].addressVal) > VALID_ADDRESS_LEN) {
130 memset(newNumber, 0x00, sizeof(newNumber));
131 memset(sqlQuery, 0x00, sizeof(sqlQuery));
133 GetValidNumber(pMsgInfo->addressList[0].addressVal, newNumber);
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);
138 memset(sqlQuery, 0x00, sizeof(sqlQuery));
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);
144 err = pDbHandle->getTable(sqlQuery, &rowCnt);
147 MSG_DEBUG("Msg is Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
149 pDbHandle->freeTable();
151 pMsgInfo->folderId = MSG_SPAMBOX_ID;
155 MSG_DEBUG("Msg is NOT Filtered by Address : [%s]", pMsgInfo->addressList[0].addressVal);
157 pDbHandle->freeTable();
160 // =======================================================================
161 // Check Filter by Subject
162 // =======================================================================
164 memset(sqlQuery, 0x00, sizeof(sqlQuery));
166 snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILTER_VALUE FROM %s WHERE FILTER_TYPE = %d;",
167 MSGFW_FILTER_TABLE_NAME, MSG_FILTER_BY_SUBJECT);
171 err = pDbHandle->getTable(sqlQuery, &rowCnt);
173 if (err != MSG_SUCCESS) {
174 MSG_DEBUG("MsgGetTable() Error [%d] : [%s]", err, sqlQuery);
176 pDbHandle->freeTable();
181 char filterValue[MAX_FILTER_VALUE_LEN+1];
184 AutoPtr<char> buf(&pData);
187 bool bFiltered = false;
189 for (int i = 1; i <= rowCnt; i++)
191 memset(filterValue, 0x00, sizeof(filterValue));
193 pDbHandle->getColumnToString(i, MAX_FILTER_VALUE_LEN, filterValue);
195 MSG_DEBUG("filterValue [%s]", filterValue);
197 if (strlen(filterValue) <= 0) continue;
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();
205 MSG_DEBUG("file data [%s]", pData);
207 if (pMsgInfo->dataSize > 0) {
208 pData = new char[pMsgInfo->dataSize+1];
210 strncpy(pData, pMsgInfo->msgText, pMsgInfo->dataSize);
211 pData[strlen(pMsgInfo->msgText)] = '\0';
214 } else if(pMsgInfo->msgType.mainType == MSG_MMS_TYPE) {
215 if (strlen(pMsgInfo->subject) > 0) {
216 pData = new char[strlen(pMsgInfo->subject)+1];
218 strncpy(pData, pMsgInfo->subject, strlen(pMsgInfo->subject));
219 pData[strlen(pMsgInfo->subject)] = '\0';
225 MSG_DEBUG("pData is NULL");
231 MSG_DEBUG("pData [%s]", pData);
233 if (strcasestr(pData, filterValue) != NULL) {
234 MSG_DEBUG("Msg is Filtered by Subject [%s] Data [%s]", filterValue, pData);
241 pDbHandle->freeTable();
243 if (bFiltered == true) {
244 MSG_DEBUG("Msg is Filtered by Subject");
246 pMsgInfo->folderId = MSG_SPAMBOX_ID;
250 MSG_DEBUG("Msg is NOT Filtered by Subject");
258 void MsgCalllogDBInit(void)
262 if (calllogHandle == NULL)
263 calllogHandle = phone_misc_connect();
268 void MsgCalllogDBFinish(void)
272 if (calllogHandle != NULL) {
273 if (phone_misc_disconnect(calllogHandle) == PH_MISC_SUCCESS)
274 calllogHandle = NULL;