v0.4.0 - Refactor scam detection code

This commit is contained in:
0xf8 2023-04-13 22:44:17 -04:00
parent 0d5ac4b344
commit 4fa1490707
Signed by: 0xf8
GPG Key ID: 446580D758689584
4 changed files with 123 additions and 55 deletions

2
Cargo.lock generated
View File

@ -2067,7 +2067,7 @@ checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041"
[[package]]
name = "scam-police"
version = "0.3.0"
version = "0.4.0"
dependencies = [
"anyhow",
"dirs",

View File

@ -1,6 +1,6 @@
[package]
name = "scam-police"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = [ "@0xf8:projectsegfau.lt", "@jjj333:pain.agency" ]

71
src/judge.rs Normal file
View File

@ -0,0 +1,71 @@
use crate::{CONFIG, keywords::{Keywords, KeywordCategory}};
use matrix_sdk::{room::Joined, ruma::events::room::message::{RoomMessageEventContent, OriginalRoomMessageEvent}};
use serde_json::json;
pub enum JudgementResult {
Ok,
MaybeScam, // hit atleast one category
LikelyScam, // hit all categories
}
pub struct Judgement {
pub text: String,
}
impl Judgement {
pub fn judge(&self) -> anyhow::Result<JudgementResult> {
// Load keywords
let mut keywords = CONFIG.keywords.clone();
let scams = keywords.as_object_mut().unwrap().get_mut("scams").unwrap();
// Turn json into Keywords
let verbs = Keywords::create("verbs", &scams["verbs"]);
let currencies = Keywords::create("currencies", &scams["currencies"]);
let socials = Keywords::create("socials", &scams["socials"]);
// Count occurences
let mut counter = KeywordCategory::create_counter_map();
counter.insert(KeywordCategory::Verb, verbs.find(&self.text));
counter.insert(KeywordCategory::Currency, currencies.find(&self.text));
counter.insert(KeywordCategory::Social, socials.find(&self.text));
let mut count_all = 0;
let total = counter.len();
for (_category, count) in counter.to_owned() {
if count > 0 {
count_all = count_all + 1;
}
}
if count_all == 0 { return Ok(JudgementResult::Ok) };
if count_all < total { return Ok(JudgementResult::MaybeScam) };
Ok(JudgementResult::LikelyScam)
}
pub async fn alert(room: &Joined, event: &OriginalRoomMessageEvent) -> anyhow::Result<()> {
let mut keywords = CONFIG.keywords.clone();
let scams = keywords.as_object_mut().unwrap().get_mut("scams").unwrap();
// Add stats to end of response
let response = scams["response"].as_str().unwrap();
let response_html = scams["response_md"].as_str().unwrap();
// Send message
let msg = RoomMessageEventContent::text_html(response, response_html);
let reply = msg.make_reply_to(event);
room.send(reply, None).await.expect("Couldn't send message");
// Send reaction
room.send_raw(json!({
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": event.event_id.to_string(),
"key": "🚨🚨 SCAM 🚨🚨"
}}), "m.reaction", None).await.expect("Couldn't send reaction");
Ok(())
}
}

View File

@ -1,86 +1,83 @@
use matrix_sdk::{
room::Room,
ruma::{events::room::message::{
MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, Relation
MessageType, OriginalSyncRoomMessageEvent, RoomMessageEventContent, Relation, RoomMessageEvent
}, OwnedRoomId},
Error, LoopCtrl,
};
use once_cell::sync::Lazy;
use serde_json::json;
pub mod matrix;
pub mod config;
pub mod keywords;
pub mod judge;
static CONFIG: Lazy<config::Config> = Lazy::new(|| config::Config::load());
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) {
async fn on_room_message(event: OriginalSyncRoomMessageEvent, room: Room) -> anyhow::Result<()> {
if let Room::Joined(room) = room {
let orig_event = event.to_owned().into_full_event(OwnedRoomId::from(room.room_id()));
// Ignore non-text
let MessageType::Text(text_content) = event.to_owned().content.msgtype else {
return;
return Ok(());
};
// Ignore own messages
if event.sender == room.client().user_id().expect("Couldn't get user_id").to_string() { return }
// Ignore replies
if let Some(relation) = orig_event.to_owned().content.relates_to {
if match relation {
Relation::Reply { in_reply_to: _ } => true,
_ => false
} {
return
}
}
if event.sender == room.client().user_id().expect("Couldn't get user_id").to_string() { return Ok(()) }
// Too short to be a scam lol
if text_content.body.chars().count() < 12 { return }
if text_content.body.chars().count() < 12 { return Ok(()) }
let text_content = text_content.body.to_lowercase();
// Load keywords
let mut keywords = CONFIG.keywords.clone();
let scams = keywords.as_object_mut().unwrap().get_mut("scams").unwrap();
let judgement = judge::Judgement { text: text_content };
// Turn json into Keywords
let verbs = keywords::Keywords::create("verbs", &scams["verbs"]);
let currencies = keywords::Keywords::create("currencies", &scams["currencies"]);
let socials = keywords::Keywords::create("socials", &scams["socials"]);
// Handle replies
if let Some(relation) = orig_event.to_owned().content.relates_to {
if let Some(event) = match relation {
Relation::Reply { in_reply_to } => {
let replied_event = room.event(&in_reply_to.event_id).await?;
let t = replied_event.event.get_field::<&str>("type")?;
// Count occurences
let mut counter = keywords::KeywordCategory::create_counter_map();
counter.insert(keywords::KeywordCategory::Verb, verbs.find(&text_content));
counter.insert(keywords::KeywordCategory::Currency, currencies.find(&text_content));
counter.insert(keywords::KeywordCategory::Social, socials.find(&text_content));
// Test if every category has atleast one hit
let mut hit_all = true;
for (_category, count) in counter.to_owned() {
hit_all = hit_all && count != 0;
if t.as_deref() == Some("m.room.message") {
let event = replied_event.event.deserialize_as::<RoomMessageEvent>()?;
Some(event.to_owned())
} else {
None
}
},
_ => None
} {
let event = event.as_original().unwrap();
let content = event.content.to_owned().body().to_lowercase();
if hit_all {
// Add stats to end of response
let response = format!("{}\nDetection stats: {:?}", scams["response"].as_str().unwrap(), counter.to_owned());
let response_html = format!("{}<br>Detection stats: <code>{:?}</code>", scams["response_md"].as_str().unwrap(), counter.to_owned());
if event.sender == room.client().user_id().expect("Couldn't get user_id").to_string() { return Ok(()) }
// Send message
let msg = RoomMessageEventContent::text_html(response, response_html);
let reply = msg.make_reply_to(&orig_event);
room.send(reply, None).await.expect("Couldn't send message");
let reply_judgement = judge::Judgement { text: content };
// Send reaction
room.send_raw(json!({
"m.relates_to": {
"rel_type": "m.annotation",
"event_id": orig_event.event_id.to_string(),
"key": "🚨🚨 SCAM 🚨🚨"
}}), "m.reaction", None).await.expect("Couldn't send reaction");
match reply_judgement.judge()? {
judge::JudgementResult::Ok => (),
judge::JudgementResult::MaybeScam => (),
judge::JudgementResult::LikelyScam => {
judge::Judgement::alert(&room, &orig_event).await?;
return Ok(());
}
}
}
}
match judgement.judge()? {
judge::JudgementResult::Ok => return Ok(()),
judge::JudgementResult::MaybeScam => return Ok(()),
judge::JudgementResult::LikelyScam => {
judge::Judgement::alert(&room, &orig_event).await?;
return Ok(());
}
}
}
Ok(())
}
#[tokio::main]