Keep track of users
This commit is contained in:
parent
673161a895
commit
ae984af15b
35
src/main.rs
35
src/main.rs
|
@ -2,10 +2,12 @@ use futures::StreamExt;
|
|||
use irc::client::prelude::Config as IrcConfig;
|
||||
use irc::client::prelude::*;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
quit_message: String,
|
||||
timeout_limit: Option<u8>,
|
||||
irc: IrcConfig,
|
||||
}
|
||||
|
||||
|
@ -18,6 +20,12 @@ macro_rules! unwrap_or_continue {
|
|||
};
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
enum Status {
|
||||
TimeoutCount(u8),
|
||||
Banned,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let filename = std::env::var("SLEEPERAGENT_CONFIG").unwrap_or("sleeperagent.toml".into());
|
||||
|
@ -30,11 +38,36 @@ async fn main() -> anyhow::Result<()> {
|
|||
|
||||
let mut stream = client.stream()?;
|
||||
|
||||
let mut channel_users: HashMap<String, HashMap<String, Status>> = HashMap::new();
|
||||
|
||||
while let Some(message) = stream.next().await.transpose()? {
|
||||
if let Command::Response(response, args) = message.command {
|
||||
if response == Response::RPL_NAMREPLY {
|
||||
let channel = args[2].to_string();
|
||||
let users = args[3]
|
||||
.split(' ')
|
||||
.map(|s| (s.to_owned(), Status::TimeoutCount(0)))
|
||||
.collect();
|
||||
|
||||
channel_users.insert(channel, users);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
let nick = unwrap_or_continue!(message.source_nickname());
|
||||
|
||||
match message.command {
|
||||
Command::JOIN(ref channel, _, _) => {
|
||||
let users = unwrap_or_continue!(channel_users.get_mut(channel));
|
||||
users.insert(nick.to_string(), Status::TimeoutCount(0));
|
||||
}
|
||||
Command::PRIVMSG(ref channel, ref message) => {
|
||||
client.send_privmsg(channel, format!("<{}> {}", nick, message))?;
|
||||
if message.starts_with("!dbg") {
|
||||
client.send_privmsg(
|
||||
channel,
|
||||
format!("{:#?}", channel_users).replace("\n", "\r\n"),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue