Added more strict excluded chats check.

Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
2023-09-12 21:37:50 +04:00
parent b08bbf3a83
commit 091198f624
3 changed files with 572 additions and 594 deletions

1113
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -45,6 +45,9 @@ pub struct MessageDirectionFilter(pub MessageDirection);
#[derive(Clone)]
pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
#[derive(Clone)]
pub struct OnlyFromId(pub i64);
/// Filters using provided regex.
#[derive(Clone)]
pub struct RegexFilter(pub Regex);
@ -65,9 +68,23 @@ impl Filter for ExcludedChatsFilter {
}
}
impl Filter for OnlyFromId {
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
let Update::NewMessage(message) = update else {
return Ok(false);
};
let Some(sender) = message.sender() else {
return Ok(false);
};
Ok(sender.id() == self.0)
}
}
impl Filter for MessageDirectionFilter {
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
let Update::NewMessage(message) = update else {return Ok(false)};
let Update::NewMessage(message) = update else {
return Ok(false);
};
let res = matches!(
(self.0, message.outgoing()),
@ -106,7 +123,9 @@ impl<'a> Filter for TextFilter<'a> {
impl Filter for SilentFilter {
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
let Some(message) = get_message(update) else {return Ok(false)};
let Some(message) = get_message(update) else {
return Ok(false);
};
// Check that message is not silent.
Ok(!message.silent())
}
@ -114,7 +133,9 @@ impl Filter for SilentFilter {
impl Filter for RegexFilter {
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
let Some(message) = get_message(update) else {return Ok(false)};
let Some(message) = get_message(update) else {
return Ok(false);
};
Ok(self.0.is_match(message.text()))
}
}

View File

@ -11,7 +11,7 @@ use super::{
filters::{
filtered_handler::FilteredHandler,
message_fitlers::{
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, RegexFilter,
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, OnlyFromId, RegexFilter,
SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter,
},
},
@ -101,41 +101,51 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
let me = client.get_me().await?;
let handlers: Vec<FilteredHandler> = vec![
// Printing help.
FilteredHandler::new(Help).add_filter(TextFilter(&[".h"], TextMatchMethod::Matches)),
FilteredHandler::new(Help)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(TextFilter(&[".h"], TextMatchMethod::Matches)),
// Greeting my fellow humans.
FilteredHandler::new(Greeter)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
.add_filter(SilentFilter)
.add_filter(ExcludedChatsFilter(vec![me.id()]))
.add_filter(TextFilter(&["привет"], TextMatchMethod::StartsWith))
.add_filter(ExcludedChatsFilter(args.excluded_chats))
.add_middleware::<MembersCount<100>>(),
// Getting chat id.
FilteredHandler::new(GetChatId).add_filter(TextFilter(&[".cid"], TextMatchMethod::Matches)),
FilteredHandler::new(GetChatId)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(TextFilter(&[".cid"], TextMatchMethod::Matches))
.add_filter(OnlyFromId(me.id())),
// Make бля fun again.
FilteredHandler::new(Blyaficator)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".bl"], TextMatchMethod::StartsWith)),
// Handler for converting currecies.
FilteredHandler::new(CurrencyConverter::new()?)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
.add_filter(CurrencyTextFilter),
// Simlpe rotator.
FilteredHandler::new(Rotator)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".rl"], TextMatchMethod::StartsWith)),
// Weather forecast.
FilteredHandler::new(WeatherForecaster::new()?)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".w"], TextMatchMethod::StartsWith)),
// Smiley repeator.
FilteredHandler::new(Repeator)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
.add_filter(SilentFilter)
@ -144,22 +154,26 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
.add_middleware::<MembersCount<100>>(),
// The magic balls.
FilteredHandler::new(MagicBall)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".mb"], TextMatchMethod::StartsWith)),
// Random chooser.
FilteredHandler::new(Chooser)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith)),
// Notify all.
FilteredHandler::new(NotifyAll)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&["@all"], TextMatchMethod::Contains))
.add_middleware::<MembersCount<100>>(),
// Time conversion utils
FilteredHandler::new(TimeConverter)
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
.add_filter(SilentFilter)
.add_filter(TextFilter(&[".t"], TextMatchMethod::StartsWith)),
@ -179,7 +193,7 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
continue;
}
// We get update if there's no error
let Some(update_data) = update.ok().and_then(|inner|inner) else{
let Some(update_data) = update.ok().and_then(|inner| inner) else {
log::warn!("Empty update is found.");
continue;
};