Added more strict excluded chats check.
Signed-off-by: Pavel Kirilin <win10@list.ru>
This commit is contained in:
1113
Cargo.lock
generated
1113
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -45,6 +45,9 @@ pub struct MessageDirectionFilter(pub MessageDirection);
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
|
pub struct TextFilter<'a>(pub &'a [&'a str], pub TextMatchMethod);
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct OnlyFromId(pub i64);
|
||||||
|
|
||||||
/// Filters using provided regex.
|
/// Filters using provided regex.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RegexFilter(pub Regex);
|
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 {
|
impl Filter for MessageDirectionFilter {
|
||||||
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
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!(
|
let res = matches!(
|
||||||
(self.0, message.outgoing()),
|
(self.0, message.outgoing()),
|
||||||
@ -106,7 +123,9 @@ impl<'a> Filter for TextFilter<'a> {
|
|||||||
|
|
||||||
impl Filter for SilentFilter {
|
impl Filter for SilentFilter {
|
||||||
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
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.
|
// Check that message is not silent.
|
||||||
Ok(!message.silent())
|
Ok(!message.silent())
|
||||||
}
|
}
|
||||||
@ -114,7 +133,9 @@ impl Filter for SilentFilter {
|
|||||||
|
|
||||||
impl Filter for RegexFilter {
|
impl Filter for RegexFilter {
|
||||||
fn filter(&self, update: &Update) -> anyhow::Result<bool> {
|
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()))
|
Ok(self.0.is_match(message.text()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ use super::{
|
|||||||
filters::{
|
filters::{
|
||||||
filtered_handler::FilteredHandler,
|
filtered_handler::FilteredHandler,
|
||||||
message_fitlers::{
|
message_fitlers::{
|
||||||
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, RegexFilter,
|
ExcludedChatsFilter, MessageDirection, MessageDirectionFilter, OnlyFromId, RegexFilter,
|
||||||
SilentFilter, TextFilter, TextMatchMethod, UpdateType, UpdateTypeFilter,
|
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 me = client.get_me().await?;
|
||||||
let handlers: Vec<FilteredHandler> = vec![
|
let handlers: Vec<FilteredHandler> = vec![
|
||||||
// Printing help.
|
// 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.
|
// Greeting my fellow humans.
|
||||||
FilteredHandler::new(Greeter)
|
FilteredHandler::new(Greeter)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
.add_filter(ExcludedChatsFilter(vec![me.id()]))
|
||||||
.add_filter(TextFilter(&["привет"], TextMatchMethod::StartsWith))
|
.add_filter(TextFilter(&["привет"], TextMatchMethod::StartsWith))
|
||||||
.add_filter(ExcludedChatsFilter(args.excluded_chats))
|
|
||||||
.add_middleware::<MembersCount<100>>(),
|
.add_middleware::<MembersCount<100>>(),
|
||||||
// Getting chat id.
|
// 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.
|
// Make бля fun again.
|
||||||
FilteredHandler::new(Blyaficator)
|
FilteredHandler::new(Blyaficator)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".bl"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".bl"], TextMatchMethod::StartsWith)),
|
||||||
// Handler for converting currecies.
|
// Handler for converting currecies.
|
||||||
FilteredHandler::new(CurrencyConverter::new()?)
|
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(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(ExcludedChatsFilter(args.currency_excluded_chats))
|
|
||||||
.add_filter(CurrencyTextFilter),
|
.add_filter(CurrencyTextFilter),
|
||||||
// Simlpe rotator.
|
// Simlpe rotator.
|
||||||
FilteredHandler::new(Rotator)
|
FilteredHandler::new(Rotator)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".rl"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".rl"], TextMatchMethod::StartsWith)),
|
||||||
// Weather forecast.
|
// Weather forecast.
|
||||||
FilteredHandler::new(WeatherForecaster::new()?)
|
FilteredHandler::new(WeatherForecaster::new()?)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".w"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".w"], TextMatchMethod::StartsWith)),
|
||||||
// Smiley repeator.
|
// Smiley repeator.
|
||||||
FilteredHandler::new(Repeator)
|
FilteredHandler::new(Repeator)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
.add_filter(MessageDirectionFilter(MessageDirection::Incoming))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
@ -144,22 +154,26 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
.add_middleware::<MembersCount<100>>(),
|
.add_middleware::<MembersCount<100>>(),
|
||||||
// The magic balls.
|
// The magic balls.
|
||||||
FilteredHandler::new(MagicBall)
|
FilteredHandler::new(MagicBall)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".mb"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".mb"], TextMatchMethod::StartsWith)),
|
||||||
// Random chooser.
|
// Random chooser.
|
||||||
FilteredHandler::new(Chooser)
|
FilteredHandler::new(Chooser)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".c"], TextMatchMethod::StartsWith)),
|
||||||
// Notify all.
|
// Notify all.
|
||||||
FilteredHandler::new(NotifyAll)
|
FilteredHandler::new(NotifyAll)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&["@all"], TextMatchMethod::Contains))
|
.add_filter(TextFilter(&["@all"], TextMatchMethod::Contains))
|
||||||
.add_middleware::<MembersCount<100>>(),
|
.add_middleware::<MembersCount<100>>(),
|
||||||
// Time conversion utils
|
// Time conversion utils
|
||||||
FilteredHandler::new(TimeConverter)
|
FilteredHandler::new(TimeConverter)
|
||||||
|
.add_filter(ExcludedChatsFilter(args.excluded_chats.clone()))
|
||||||
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
.add_filter(UpdateTypeFilter(&[UpdateType::New]))
|
||||||
.add_filter(SilentFilter)
|
.add_filter(SilentFilter)
|
||||||
.add_filter(TextFilter(&[".t"], TextMatchMethod::StartsWith)),
|
.add_filter(TextFilter(&[".t"], TextMatchMethod::StartsWith)),
|
||||||
@ -179,7 +193,7 @@ async fn run(args: BotConfig, client: Client) -> anyhow::Result<()> {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// We get update if there's no error
|
// 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.");
|
log::warn!("Empty update is found.");
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user