Skip to content

Commit

Permalink
Rewrite the multi-user mode
Browse files Browse the repository at this point in the history
  • Loading branch information
iovxw committed Jul 31, 2021
1 parent 05a7737 commit 36f77b5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
--admins <users id> Multi-user mode, only specified users can use this bot, split users with `,'
--admin <user id>... Private mode, only specified user can use this bot. This argument can be passed
multiple times to allow multiple admins
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]
--single-user <user id> Single user mode, only specified user can use this bot
ARGS:
<token> Telegram bot token
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ FLAGS:
-V, --version Prints version information
OPTIONS:
--admins <users id> Multi-user mode, only specified users can use this bot, split users with `,'
--admin <user id>... Private mode, only specified user can use this bot. This argument can be passed
multiple times to allow multiple admins
-d, --database <path> Path to database [default: ./rssbot.json]
--max-feed-size <bytes> Maximum feed size, 0 is unlimited [default: 2097152]
--max-interval <seconds> Maximum fetch interval [default: 43200]
--min-interval <seconds> Minimum fetch interval [default: 300]
--single-user <user id> Single user mode, only specified user can use this bot
ARGS:
<token> Telegram bot token
Expand Down
7 changes: 2 additions & 5 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub use sub::sub;
pub use unsub::unsub;

pub async fn check_command(opt: &crate::Opt, cmd: Arc<Command<Text>>) -> bool {
use std::str::FromStr;
use tbot::contexts::fields::Message;
use tbot::types::chat::Kind::*;
let target = &mut MsgTarget::new(cmd.chat.id, cmd.message_id);
Expand All @@ -30,10 +29,8 @@ pub async fn check_command(opt: &crate::Opt, cmd: Arc<Command<Text>>) -> bool {
.map(|user| user.id.0)
.unwrap_or_else(|| cmd.chat.id.0);

// Single user mode
if matches!(opt.single_user, Some(owner) if owner != from)
|| matches!(&opt.admins, Some(admins) if admins.split(",").into_iter().map(|x| i64::from_str(x.trim()).expect("Not valid number")).all(|admin| admin != from))
{
// Private mode
if !opt.admin.is_empty() && !opt.admin.contains(&from) {
eprintln!(
"Unauthenticated request from user/channel: {}, command: {}, args: {}",
from, cmd.command, cmd.text.value
Expand Down
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@ pub struct Opt {
#[structopt(long, value_name = "bytes", default_value = "2097152")]
// default is 2MiB
max_feed_size: u64,
/// Single user mode, only specified user can use this bot
#[structopt(long, value_name = "user id")]
single_user: Option<i64>,
/// Multi-user mode, only specified users can use this bot, split users with `,'
#[structopt(long, value_name = "users id")]
admins: Option<String>,
/// Private mode, only specified user can use this bot.
/// This argument can be passed multiple times to allow multiple admins
#[structopt(
long,
value_name = "user id",
number_of_values = 1,
alias = "single_user" // For compatibility
)]
admin: Vec<i64>,
/// Make bot commands only accessible for group admins.
#[structopt(long)]
restricted: bool,
Expand Down

0 comments on commit 36f77b5

Please sign in to comment.