Skip to content
Snippets Groups Projects
Verified Commit c8ae57db authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Implement ignore list manager

parent 78af7fa2
No related branches found
No related tags found
No related merge requests found
/*
* libquassel
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
package de.justjanne.libquassel.protocol.models.rules
import de.justjanne.libquassel.protocol.util.expression.ExpressionMatch
data class IgnoreRule(
val type: IgnoreType,
val ignoreRule: String,
val isRegEx: Boolean = false,
val strictness: StrictnessType,
val isEnabled: Boolean = true,
val scope: ScopeType,
val scopeRule: String
) {
val ignoreMatch = ExpressionMatch(
ignoreRule,
if (isRegEx) ExpressionMatch.MatchMode.MatchRegEx
else ExpressionMatch.MatchMode.MatchWildcard,
false
)
val scopeMatch = ExpressionMatch(
scopeRule,
ExpressionMatch.MatchMode.MatchMultiWildcard,
false
)
}
/*
* libquassel
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
package de.justjanne.libquassel.protocol.models.rules
enum class IgnoreType(
val value: Int
) {
SenderIgnore(0),
MessageIgnore(1),
CtcpIgnore(2);
companion object {
private val values = enumValues<IgnoreType>()
.associateBy(IgnoreType::value)
/**
* Obtain from underlying representation
*/
fun of(value: Int): IgnoreType? = values[value]
}
}
/*
* libquassel
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
package de.justjanne.libquassel.protocol.models.rules
enum class ScopeType(
val value: Int
) {
GlobalScope(0),
NetworkScope(1),
ChannelScope(2);
companion object {
private val values = enumValues<ScopeType>()
.associateBy(ScopeType::value)
/**
* Obtain from underlying representation
*/
fun of(value: Int): ScopeType? = values[value]
}
}
/*
* libquassel
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
package de.justjanne.libquassel.protocol.models.rules
enum class StrictnessType(
val value: Int
) {
UnmatchedStrictness(0),
SoftStrictness(1),
HardStrictness(2);
companion object {
private val values = enumValues<StrictnessType>()
.associateBy(StrictnessType::value)
/**
* Obtain from underlying representation
*/
fun of(value: Int): StrictnessType? = values[value]
}
}
/*
* libquassel
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*/
package de.justjanne.libquassel.protocol.syncables.state
import de.justjanne.bitflags.of
import de.justjanne.libquassel.protocol.models.flags.MessageType
import de.justjanne.libquassel.protocol.models.flags.MessageTypes
import de.justjanne.libquassel.protocol.models.rules.IgnoreRule
import de.justjanne.libquassel.protocol.models.rules.IgnoreType
import de.justjanne.libquassel.protocol.models.rules.ScopeType
import de.justjanne.libquassel.protocol.models.rules.StrictnessType
data class IgnoreListManagerState(
val rules: List<IgnoreRule> = emptyList()
) {
fun indexOf(ignoreRule: String?): Int = rules.indexOfFirst { it.ignoreRule == ignoreRule }
fun contains(ignoreRule: String?) = rules.any { it.ignoreRule == ignoreRule }
fun isEmpty() = rules.isEmpty()
fun count() = rules.size
fun matchingRules(sender: String) = rules.filter {
it.type == IgnoreType.SenderIgnore && it.ignoreMatch.match(sender)
}
fun match(
msgContents: String,
msgSender: String,
msgType: MessageTypes,
network: String,
bufferName: String
): StrictnessType {
if ((MessageType.of(MessageType.Plain, MessageType.Notice, MessageType.Action) intersect msgType).isEmpty()) {
return StrictnessType.UnmatchedStrictness
}
return rules.asSequence().filter {
it.isEnabled && it.type != IgnoreType.CtcpIgnore
}.filter {
it.scope == ScopeType.GlobalScope ||
it.scope == ScopeType.NetworkScope && it.scopeMatch.match(network) ||
it.scope == ScopeType.ChannelScope && it.scopeMatch.match(bufferName)
}.filter {
val content = if (it.type == IgnoreType.MessageIgnore) msgContents else msgSender
it.ignoreMatch.match(content)
}.map {
it.strictness
}.maxByOrNull {
it.value
} ?: StrictnessType.UnmatchedStrictness
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment