Add filter for files
This commit is contained in:
		
							parent
							
								
									2834f31982
								
							
						
					
					
						commit
						cddbc362a8
					
				
							
								
								
									
										18
									
								
								src/filter_files/action.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/filter_files/action.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,18 @@ | |||||||
|  | use teloxide::{prelude::*, types::ChatPermissions}; | ||||||
|  | 
 | ||||||
|  | use crate::telegram_utils; | ||||||
|  | 
 | ||||||
|  | pub async fn take_actions(msg: Message, bot: Bot) -> anyhow::Result<()> { | ||||||
|  |     _ = bot.delete_message(msg.chat.id, msg.id).await; | ||||||
|  |     _ = bot.send_message(msg.chat.id, format!("Los mensajes con ficheros ejecutables no están permitidos\nAlerta para el usuario {}.\nSi consideras que ha sido un error ponte en contacto con un administrador", telegram_utils::get_alias(&msg))).await; | ||||||
|  |     let user = match msg.from() { | ||||||
|  |         Some(user) => user, | ||||||
|  |         None => return Ok(()), | ||||||
|  |     }; | ||||||
|  |     let mut permissions = msg.chat.permissions().unwrap_or(ChatPermissions::empty()); | ||||||
|  |     permissions.remove(ChatPermissions::SEND_OTHER_MESSAGES); | ||||||
|  |     permissions.insert(ChatPermissions::SEND_MESSAGES); | ||||||
|  |     let restrict = bot.restrict_chat_member(msg.chat.id, user.id, permissions); | ||||||
|  |     _ = restrict.send().await; | ||||||
|  |     Ok(()) | ||||||
|  | } | ||||||
							
								
								
									
										27
									
								
								src/filter_files/analyce_name.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/filter_files/analyce_name.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | |||||||
|  | use teloxide::{prelude::*, types::Document}; | ||||||
|  | 
 | ||||||
|  | static EXTENSIONS: [&str; 3] = ["exe", "deb", "rpm"]; | ||||||
|  | 
 | ||||||
|  | pub fn check_file(msg: Message) -> bool { | ||||||
|  |     match msg.document() { | ||||||
|  |         Some(s) => match get_extension(s.clone()) { | ||||||
|  |             Some(extension) => check_extension(extension), | ||||||
|  |             None => false, | ||||||
|  |         }, | ||||||
|  |         None => false, | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_extension(document: Document) -> Option<String> { | ||||||
|  |     match document.file_name { | ||||||
|  |         Some(name) => match name.split(".").last() { | ||||||
|  |             Some(extension) => Some(String::from(extension)), | ||||||
|  |             None => None, | ||||||
|  |         }, | ||||||
|  |         None => None, | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn check_extension(extension: String) -> bool { | ||||||
|  |     EXTENSIONS.contains(&extension.as_str()) | ||||||
|  | } | ||||||
							
								
								
									
										2
									
								
								src/filter_files/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								src/filter_files/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | |||||||
|  | pub mod action; | ||||||
|  | pub mod analyce_name; | ||||||
							
								
								
									
										88
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										88
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -1,16 +1,20 @@ | |||||||
| use teloxide::{prelude::*, utils::command::BotCommands}; |  | ||||||
| use std::sync::Arc; |  | ||||||
| use chrono::Local; | use chrono::Local; | ||||||
| mod check_permissions; | use std::sync::Arc; | ||||||
| mod pole_dialogue; | use teloxide::{prelude::*, utils::command::BotCommands}; | ||||||
| mod ban_stiker; | mod ban_stiker; | ||||||
|  | mod check_permissions; | ||||||
| mod database; | mod database; | ||||||
|  | mod filter_files; | ||||||
|  | mod pole_dialogue; | ||||||
|  | mod rewrite_links; | ||||||
| mod spoiler_mangas; | mod spoiler_mangas; | ||||||
| mod telegram_utils; | mod telegram_utils; | ||||||
| mod rewrite_links; |  | ||||||
| 
 | 
 | ||||||
| #[derive(BotCommands, Clone)] | #[derive(BotCommands, Clone)] | ||||||
| #[command(rename_rule = "lowercase", description = "These commands are supported:")] | #[command(
 | ||||||
|  |     rename_rule = "lowercase", | ||||||
|  |     description = "These commands are supported:" | ||||||
|  | )] | ||||||
| enum Command { | enum Command { | ||||||
|     #[command(description = "ban a stiker")] |     #[command(description = "ban a stiker")] | ||||||
|     Torquemada, |     Torquemada, | ||||||
| @ -38,23 +42,22 @@ async fn run(){ | |||||||
|     //Command::repl(bot.clone(), answer).await;
 |     //Command::repl(bot.clone(), answer).await;
 | ||||||
|     let handler = Update::filter_message() |     let handler = Update::filter_message() | ||||||
|         .branch( |         .branch( | ||||||
|             dptree::filter(move |msg: Message| !p1.compare(&msg.chat.id.to_string())) |             dptree::filter(move |msg: Message| !p1.compare(&msg.chat.id.to_string())).endpoint( | ||||||
|                 .endpoint(|msg: Message, bot: Bot| async move { |                 |msg: Message, bot: Bot| async move { | ||||||
|                     if !msg.chat.is_private() { |                     if !msg.chat.is_private() { | ||||||
|                         println!("{}", msg.chat.id.0); |                         println!("{}", msg.chat.id.0); | ||||||
|                         bot.leave_chat(msg.chat.id).await?; |                         bot.leave_chat(msg.chat.id).await?; | ||||||
|                     } |                     } | ||||||
|                     Ok(()) |                     Ok(()) | ||||||
|                     } |                 }, | ||||||
|             ), |             ), | ||||||
| 
 |  | ||||||
|         ) |         ) | ||||||
|         .branch( |         .branch( | ||||||
|             dptree::filter(|msg: Message| is_channel_user(msg)) |             dptree::filter(|msg: Message| is_channel_user(msg)).endpoint( | ||||||
|                 .endpoint(|msg: Message, bot: Bot| async move { |                 |msg: Message, bot: Bot| async move { | ||||||
|                     bot.delete_message(msg.chat.id, msg.id).await?; |                     bot.delete_message(msg.chat.id, msg.id).await?; | ||||||
|                     Ok(()) |                     Ok(()) | ||||||
|                     } |                 }, | ||||||
|             ), |             ), | ||||||
|         ) |         ) | ||||||
|         .branch( |         .branch( | ||||||
| @ -67,45 +70,54 @@ async fn run(){ | |||||||
|                 .endpoint(|msg: Message, bot: Bot| async move { |                 .endpoint(|msg: Message, bot: Bot| async move { | ||||||
|                     bot.delete_message(msg.chat.id, msg.id).await?; |                     bot.delete_message(msg.chat.id, msg.id).await?; | ||||||
|                     Ok(()) |                     Ok(()) | ||||||
|                     } |                 }), | ||||||
|                 ), |  | ||||||
|         ) |         ) | ||||||
|         .branch( |         .branch( | ||||||
|             dptree::filter(move |msg: Message| (is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string()))) |             dptree::filter(move |msg: Message| { | ||||||
|  |                 (is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string())) | ||||||
|  |             }) | ||||||
|             .endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)), |             .endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)), | ||||||
|         ) |         ) | ||||||
|         .branch( |         .branch( | ||||||
|             dptree::filter(move |msg: Message| rewrite_links::check_contain_links::contain_links(msg.clone())) |             dptree::filter(move |msg: Message| filter_files::analyce_name::check_file(msg.clone())) | ||||||
|                 .endpoint(|msg: Message, bot: Bot| rewrite_links::check_contain_links::fix_links(msg, bot)), |                 .endpoint(|msg: Message, bot: Bot| filter_files::action::take_actions(msg, bot)), | ||||||
|  |         ) | ||||||
|  |         .branch( | ||||||
|  |             dptree::filter(move |msg: Message| { | ||||||
|  |                 rewrite_links::check_contain_links::contain_links(msg.clone()) | ||||||
|  |             }) | ||||||
|  |             .endpoint(|msg: Message, bot: Bot| { | ||||||
|  |                 rewrite_links::check_contain_links::fix_links(msg, bot) | ||||||
|  |             }), | ||||||
|         ) |         ) | ||||||
|         .branch( |         .branch( | ||||||
|             dptree::filter(move |msg: Message| permissions.compar_party(&msg.chat.id.to_string())) |             dptree::filter(move |msg: Message| permissions.compar_party(&msg.chat.id.to_string())) | ||||||
|                 .endpoint(|msg: Message, bot: Bot| pole_dialogue::exe_pole(msg, bot)), |                 .endpoint(|msg: Message, bot: Bot| pole_dialogue::exe_pole(msg, bot)), | ||||||
|         ); |         ); | ||||||
|     Dispatcher::builder( |     Dispatcher::builder(bot, handler).build().dispatch().await; | ||||||
|         bot, |  | ||||||
|         handler, |  | ||||||
|     ) |  | ||||||
|     .build() |  | ||||||
|     .dispatch() |  | ||||||
|     .await; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async fn answer( | async fn answer(bot: Bot, msg: Message, command: Command) -> anyhow::Result<()> { | ||||||
|     bot: Bot, |  | ||||||
|     msg: Message, |  | ||||||
|     command: Command, |  | ||||||
| ) -> anyhow::Result<()> { |  | ||||||
|     match command { |     match command { | ||||||
|         Command::Torquemada => { |         Command::Torquemada => match ban_stiker::ban_media(msg.clone(), bot.clone()).await { | ||||||
|             match ban_stiker::ban_media(msg.clone(), bot.clone()).await { |             Ok(_o) => { | ||||||
|                 Ok(_o) => bot.send_message(msg.chat.id, "Otro fichero que se va a la hoguera").await?, |                 bot.send_message(msg.chat.id, "Otro fichero que se va a la hoguera") | ||||||
|                 Err(e) => bot.send_message(msg.chat.id, e.to_string()).await?, |                     .await? | ||||||
|             } |             } | ||||||
|  |             Err(e) => bot.send_message(msg.chat.id, e.to_string()).await?, | ||||||
|         }, |         }, | ||||||
|         Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?, |         Command::Help => { | ||||||
|  |             bot.send_message(msg.chat.id, Command::descriptions().to_string()) | ||||||
|  |                 .await? | ||||||
|  |         } | ||||||
|         Command::Top => pole_dialogue::get_top(msg, bot).await?, |         Command::Top => pole_dialogue::get_top(msg, bot).await?, | ||||||
|         Command::Time => bot.send_message(msg.chat.id, Local::now().format("%Y-%m-%d:%H:%M:%S").to_string()).await?, |         Command::Time => { | ||||||
|  |             bot.send_message( | ||||||
|  |                 msg.chat.id, | ||||||
|  |                 Local::now().format("%Y-%m-%d:%H:%M:%S").to_string(), | ||||||
|  |             ) | ||||||
|  |             .await? | ||||||
|  |         } | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     Ok(()) |     Ok(()) | ||||||
| @ -113,7 +125,6 @@ async fn answer( | |||||||
| 
 | 
 | ||||||
| fn is_media(msg: Message) -> bool { | fn is_media(msg: Message) -> bool { | ||||||
|     is_stiker(msg.clone()) || is_gif(msg.clone()) || is_photo(msg.clone()) |     is_stiker(msg.clone()) || is_gif(msg.clone()) || is_photo(msg.clone()) | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn is_stiker(msg: Message) -> bool { | fn is_stiker(msg: Message) -> bool { | ||||||
| @ -121,7 +132,6 @@ fn is_stiker(msg: Message) -> bool { | |||||||
|         Some(s) => true, |         Some(s) => true, | ||||||
|         None => false, |         None => false, | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn is_gif(msg: Message) -> bool { | fn is_gif(msg: Message) -> bool { | ||||||
| @ -129,7 +139,6 @@ fn is_gif(msg: Message) -> bool { | |||||||
|         Some(s) => true, |         Some(s) => true, | ||||||
|         None => false, |         None => false, | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn is_photo(msg: Message) -> bool { | fn is_photo(msg: Message) -> bool { | ||||||
| @ -137,7 +146,6 @@ fn is_photo(msg: Message) -> bool { | |||||||
|         Some(s) => true, |         Some(s) => true, | ||||||
|         None => false, |         None => false, | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fn is_channel_user(msg: Message) -> bool { | fn is_channel_user(msg: Message) -> bool { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user