Compare commits

...

4 commits

View file

@ -26,6 +26,12 @@ enum Status {
Banned,
}
impl std::default::Default for Status {
fn default() -> Self {
Self::TimeoutCount(0)
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let filename = std::env::var("SLEEPERAGENT_CONFIG").unwrap_or("sleeperagent.toml".into());
@ -59,6 +65,19 @@ async fn main() -> anyhow::Result<()> {
match message.command {
Command::JOIN(ref channel, _, _) => {
let users = unwrap_or_continue!(channel_users.get_mut(channel));
for (user, status) in users.iter_mut() {
if nick.starts_with(user) {
// They're joining back for real, unban them
*status = match status {
Status::Banned => Status::TimeoutCount(0),
_ => *status,
};
let mode = Mode::Minus(ChannelMode::Ban, Some(format!("{}!*@*", user)));
client.send_mode(channel, &[mode])?;
client.send_privmsg(nick, "You're unbanned, welcome back!")?;
}
}
if !users.contains_key(nick) {
users.insert(nick.to_string(), Status::TimeoutCount(0));
}
@ -70,6 +89,22 @@ async fn main() -> anyhow::Result<()> {
format!("{:#?}", channel_users).replace("\n", "\r\n"),
)?;
}
let users = unwrap_or_continue!(channel_users.get_mut(channel));
let user = unwrap_or_continue!(users.get_mut(nick));
*user = match user {
Status::TimeoutCount(count) if *count > conf.timeout_limit.unwrap_or(1) => {
Status::TimeoutCount(0)
}
_ => *user,
};
}
Command::NICK(ref new_nick) => {
for (_, users) in &mut channel_users {
let status = users.remove(nick).unwrap_or_default();
users.insert(new_nick.clone(), status);
}
}
Command::PART(ref channel, Some(ref message)) => {
if message == &conf.quit_message {
@ -91,6 +126,27 @@ async fn main() -> anyhow::Result<()> {
}
}
}
Command::QUIT(Some(ref message)) => {
for (channel, users) in &mut channel_users {
let user = unwrap_or_continue!(users.get_mut(nick));
if message == &conf.quit_message {
*user = match user {
Status::TimeoutCount(count) => Status::TimeoutCount(*count + 1),
_ => *user,
};
if let Status::TimeoutCount(count) = user {
if *count > conf.timeout_limit.unwrap_or(1) {
let mode =
Mode::Plus(ChannelMode::Ban, Some(format!("{}!*@*", nick)));
client.send_mode(channel, &[mode])?;
*user = Status::Banned;
}
}
}
}
}
_ => {}
}
}