Implement ignore for JOIN, PART and QUIT

This commit is contained in:
famfo 2023-04-17 13:56:04 +02:00
parent 0e18740c43
commit c43c14f935
3 changed files with 34 additions and 25 deletions

View file

@ -1,12 +1,13 @@
token = "..." # REQUIRED: discord bot token
nickname = "dircord" # REQUIRED: IRC nickname
server = "karx.xyz""
server = "karx.xyz"
port = 6697
tls = true # OPTIONAL: DEFAULT: false
mode = "+B" # OPTIONAL: DEFAULT: none
raw_prefix = "++" # OPTIONAL: DEFAULT: ++
ref_content_limit = 512 # OPTIONAL: where to truncate replied messages. Defaults to ~512 minus the prefix
cache_ttl = 1800 # OPTIONAL: how long to store caches, in seconds. Defaults to 1800 (30 minutes)
ignore_useractions = false # OPTIONAL: Ignores certain user actions (JOIN, PART, QUIT). DEFAULT: false
[channels]
# irc channel name -> discord channel id

View file

@ -42,6 +42,7 @@ pub async fn irc_loop(
webhooks: HashMap<String, Webhook>,
members: Arc<Mutex<Vec<Member>>>,
cache_ttl: Option<u64>,
ignore_useractions: bool,
) -> anyhow::Result<()> {
let (send, recv) = unbounded_channel();
tokio::spawn(msg_task(UnboundedReceiverStream::new(recv)));
@ -169,32 +170,21 @@ pub async fn irc_loop(
})?;
}
} else if let Command::JOIN(ref channel, _, _) = orig_message.command {
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));
let users = unwrap_or_continue!(channel_users.get_mut(channel));
if !ignore_useractions {
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));
let users = unwrap_or_continue!(channel_users.get_mut(channel));
users.push(nickname.to_string());
users.push(nickname.to_string());
send.send(QueuedMessage::Raw {
channel_id,
http: http.clone(),
message: format!("*{}* has joined the channel", nickname),
})?;
send.send(QueuedMessage::Raw {
channel_id,
http: http.clone(),
message: format!("*{}* has joined the channel", nickname),
})?;
}
} else if let Command::PART(ref channel, ref reason) = orig_message.command {
let users = unwrap_or_continue!(channel_users.get_mut(channel));
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));
let pos = unwrap_or_continue!(users.iter().position(|u| u == nickname));
users.swap_remove(pos);
let reason = reason.as_deref().unwrap_or("Connection closed");
send.send(QueuedMessage::Raw {
channel_id,
http: http.clone(),
message: format!("*{}* has quit ({})", nickname, reason),
})?;
} else if let Command::QUIT(ref reason) = orig_message.command {
for (channel, users) in &mut channel_users {
if !ignore_useractions {
let users = unwrap_or_continue!(channel_users.get_mut(channel));
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));
let pos = unwrap_or_continue!(users.iter().position(|u| u == nickname));
@ -208,6 +198,23 @@ pub async fn irc_loop(
message: format!("*{}* has quit ({})", nickname, reason),
})?;
}
} else if let Command::QUIT(ref reason) = orig_message.command {
if !ignore_useractions {
for (channel, users) in &mut channel_users {
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));
let pos = unwrap_or_continue!(users.iter().position(|u| u == nickname));
users.swap_remove(pos);
let reason = reason.as_deref().unwrap_or("Connection closed");
send.send(QueuedMessage::Raw {
channel_id,
http: http.clone(),
message: format!("*{}* has quit ({})", nickname, reason),
})?;
}
}
} else if let Command::NICK(ref new_nick) = orig_message.command {
for (channel, users) in &mut channel_users {
let channel_id = ChannelId::from(*unwrap_or_continue!(mapping.get(channel)));

View file

@ -39,6 +39,7 @@ struct DircordConfig {
webhooks: Option<HashMap<String, String>>,
ref_content_limit: Option<u16>,
cache_ttl: Option<u64>,
ignore_useractions: Option<bool>,
}
macro_rules! type_map_key {
@ -154,7 +155,7 @@ async fn main() -> anyhow::Result<()> {
}
select! {
r = irc_loop(irc_client, http.clone(), cache.clone(), channels.clone(), webhooks_transformed, members, conf.cache_ttl) => r.unwrap(),
r = irc_loop(irc_client, http.clone(), cache.clone(), channels.clone(), webhooks_transformed, members, conf.cache_ttl, conf.ignore_useractions.unwrap_or(false)) => r.unwrap(),
r = discord_client.start() => r.unwrap(),
_ = terminate_signal() => {
for (_, &v) in channels.iter() {