Skip to content

Commit

Permalink
优化:转发规则匹配的值允许传入逻辑运算符与(&&)或(||)(用于支持多个关键词) #470
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed May 16, 2024
1 parent 9dbb257 commit 441ba14
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 65 deletions.
68 changes: 35 additions & 33 deletions app/src/main/java/com/idormy/sms/forwarder/database/entity/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.idormy.sms.forwarder.R
import com.idormy.sms.forwarder.database.ext.ConvertersSenderList
import com.idormy.sms.forwarder.entity.MsgInfo
import com.idormy.sms.forwarder.utils.*
import com.idormy.sms.forwarder.utils.RuleLine.Companion.CHECK_EQUALS
import com.xuexiang.xutil.resource.ResUtils.getString
import kotlinx.parcelize.Parcelize
import java.util.*
Expand Down Expand Up @@ -214,47 +215,48 @@ data class Rule(

//内容分支
private fun checkValue(msgValue: String?): Boolean {
var checked = false
when (this.check) {
CHECK_IS -> checked = this.value == msgValue
CHECK_NOT_IS -> checked = this.value != msgValue
CHECK_CONTAIN -> if (msgValue != null) {
checked = msgValue.contains(this.value)
}

CHECK_NOT_CONTAIN -> if (msgValue != null) {
checked = !msgValue.contains(this.value)
}
if (msgValue == null) return false

CHECK_START_WITH -> if (msgValue != null) {
checked = msgValue.startsWith(this.value)
}
fun evaluateCondition(condition: String): Boolean {
return when (check) {
CHECK_EQUALS -> value == condition
CHECK_CONTAIN -> msgValue.contains(condition)
CHECK_NOT_CONTAIN -> !msgValue.contains(condition)
CHECK_START_WITH -> msgValue.startsWith(condition)
CHECK_END_WITH -> msgValue.endsWith(condition)
CHECK_REGEX -> try {
val pattern = Pattern.compile(condition, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue)
matcher.find()
} catch (e: PatternSyntaxException) {
Log.i(TAG, "PatternSyntaxException: ${e.description}, Index: ${e.index}, Message: ${e.message}, Pattern: ${e.pattern}")
false
}

CHECK_END_WITH -> if (msgValue != null) {
checked = msgValue.endsWith(this.value)
else -> false
}
}

CHECK_REGEX -> if (msgValue != null) {
try {
//checked = Pattern.matches(this.value, msgValue);
val pattern = Pattern.compile(this.value, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue)
while (matcher.find()) {
checked = true
break
}
} catch (e: PatternSyntaxException) {
Log.d(TAG, "PatternSyntaxException: ")
Log.d(TAG, "Description: " + e.description)
Log.d(TAG, "Index: " + e.index)
Log.d(TAG, "Message: " + e.message)
Log.d(TAG, "Pattern: " + e.pattern)
fun parseAndEvaluate(expression: String): Boolean {
// Split by "||" and evaluate each segment joined by "&&"
val orGroups = expression.split("||")
return orGroups.any { orGroup ->
val andGroups = orGroup.split("&&")
andGroups.all { andGroup ->
val trimmedCondition = andGroup.trim()
evaluateCondition(trimmedCondition)
}
}
}

else -> {}
val checked = if (value.contains("&&") || value.contains("||")) {
parseAndEvaluate(value)
} else {
evaluateCondition(value)
}
Log.i(TAG, "checkValue " + msgValue + " " + this.check + " " + this.value + " checked:" + checked)

Log.i(TAG, "checkValue $msgValue $check $value checked:$checked")
return checked
}

}
65 changes: 33 additions & 32 deletions app/src/main/java/com/idormy/sms/forwarder/utils/RuleLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,45 +119,46 @@ class RuleLine(line: String, lineNum: Int, beforeRuleLine: RuleLine?) {

//内容分支
private fun checkValue(msgValue: String?): Boolean {
var checked = false
when (check) {
CHECK_EQUALS -> checked = value == msgValue
CHECK_CONTAIN -> if (msgValue != null) {
checked = msgValue.contains(value)
}

CHECK_NOT_CONTAIN -> if (msgValue != null) {
checked = !msgValue.contains(value)
}

CHECK_START_WITH -> if (msgValue != null) {
checked = msgValue.startsWith(value)
}
if (msgValue == null) return false

fun evaluateCondition(condition: String): Boolean {
return when (check) {
CHECK_EQUALS -> value == condition
CHECK_CONTAIN -> msgValue.contains(condition)
CHECK_NOT_CONTAIN -> !msgValue.contains(condition)
CHECK_START_WITH -> msgValue.startsWith(condition)
CHECK_END_WITH -> msgValue.endsWith(condition)
CHECK_REGEX -> try {
val pattern = Pattern.compile(condition, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue)
matcher.find()
} catch (e: PatternSyntaxException) {
logg("PatternSyntaxException: ${e.description}, Index: ${e.index}, Message: ${e.message}, Pattern: ${e.pattern}")
false
}

CHECK_END_WITH -> if (msgValue != null) {
checked = msgValue.endsWith(value)
else -> false
}
}

CHECK_REGEX -> if (msgValue != null) {
try {
//checked = Pattern.matches(this.value, msgValue);
val pattern = Pattern.compile(value, Pattern.CASE_INSENSITIVE)
val matcher = pattern.matcher(msgValue)
while (matcher.find()) {
checked = true
break
}
} catch (e: PatternSyntaxException) {
logg("PatternSyntaxException: ")
logg("Description: " + e.description)
logg("Index: " + e.index)
logg("Message: " + e.message)
logg("Pattern: " + e.pattern)
fun parseAndEvaluate(expression: String): Boolean {
// Split by "||" and evaluate each segment joined by "&&"
val orGroups = expression.split("||")
return orGroups.any { orGroup ->
val andGroups = orGroup.split("&&")
andGroups.all { andGroup ->
val trimmedCondition = andGroup.trim()
evaluateCondition(trimmedCondition)
}
}
}

else -> {}
val checked = if (value.contains("&&") || value.contains("||")) {
parseAndEvaluate(value)
} else {
evaluateCondition(value)
}

logg("checkValue $msgValue $check $value checked:$checked")
return checked
}
Expand Down

0 comments on commit 441ba14

Please sign in to comment.