Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user blocking feature #428

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/libtiny_tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub(crate) struct Config {

#[serde(default)]
pub(crate) key_map: Option<KeyMap>,

#[serde(default)]
pub(crate) blocked_users: Vec<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
Expand Down
7 changes: 7 additions & 0 deletions crates/libtiny_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,11 @@ impl TUI {
.upgrade()
.map(|tui| tui.borrow().current_tab().clone())
}

pub fn check_blocked(&self, user: &String) -> bool {
match self.inner.upgrade() {
Some(tui) => tui.borrow().check_blocked(user),
None => false,
}
}
}
11 changes: 11 additions & 0 deletions crates/libtiny_tui/src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub struct TUI {

/// TabConfig settings loaded from config file
tab_configs: TabConfigs,

/// List of blocked users
blocked_users: Vec<String>,
iulianalexa marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) enum CmdResult {
Expand Down Expand Up @@ -175,6 +178,7 @@ impl TUI {
key_map: KeyMap::default(),
config_path,
tab_configs: TabConfigs::default(),
blocked_users: Vec::default(),
};

// Init "mentions" tab. This needs to happen right after creating the TUI to be able to
Expand Down Expand Up @@ -365,11 +369,13 @@ impl TUI {
max_nick_length,
key_map,
layout,
blocked_users,
..
} = config;
self.set_colors(colors);
self.scrollback = scrollback.max(1);
self.key_map.load(&key_map.unwrap_or_default());
self.blocked_users = blocked_users;
if let Some(layout) = layout {
match layout {
crate::config::Layout::Compact => self.msg_layout = Layout::Compact,
Expand Down Expand Up @@ -562,6 +568,11 @@ impl TUI {
self.fix_scroll_after_close();
}

/// Checks if user is on the block list
pub(crate) fn check_blocked(&self, user: &String) -> bool {
self.blocked_users.contains(user)
}

pub(crate) fn handle_input_event(
&mut self,
ev: Event,
Expand Down
12 changes: 12 additions & 0 deletions crates/tiny/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ fn handle_irc_msg(ui: &UI, client: &dyn Client, msg: wire::Msg) {
User { ref nick, .. } | Ambiguous(ref nick) => nick,
};

let sender_hostmask = match pfx {
User { ref user, .. } => Some(user),
_ => None,
};

if let Some(sender_hostmask_str) = sender_hostmask {
if ui.check_blocked(sender_hostmask_str) {
// Blocked
return;
}
}

if ctcp == Some(wire::CTCP::Version) {
let msg_target = if ui.user_tab_exists(serv, sender) {
MsgTarget::User { serv, nick: sender }
Expand Down
1 change: 1 addition & 0 deletions crates/tiny/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ impl UI {
delegate_ui!(set_nick(serv: &str, nick: &str,));
delegate_ui!(set_tab_style(style: TabStyle, target: &MsgTarget,));
delegate_ui!(user_tab_exists(serv_name: &str, nick: &str,) -> bool);
delegate_ui!(check_blocked(user: &String,) -> bool);
delegate_ui!(get_tab_config(serv_name: &str, chan_name: Option<&ChanNameRef>,) -> TabConfig);
delegate_ui!(set_tab_config(
serv_name: &str,
Expand Down