diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..954d2f4 --- /dev/null +++ b/src/context.rs @@ -0,0 +1,48 @@ +use std::collections::VecDeque; + +use log::{debug, info}; + +use crate::MAX_MSG_LEN; + +pub struct IrcContext { + pub(crate) send_queue: VecDeque, +} + +impl IrcContext { + pub fn privmsg(&mut self, channel: &str, message: &str) { + debug!("sending privmsg to {} : {}", channel, message); + self.queue(&format!("PRIVMSG {} :{}", channel, message)); + } + pub(crate) fn queue(&mut self, msg: &str) { + let mut msg = msg.replace("\r", "").replace("\n", ""); + + if msg.len() > MAX_MSG_LEN - "\r\n".len() { + let mut i = 0; + + while i < msg.len() { + let max = (MAX_MSG_LEN - "\r\n".len()).min(msg[i..].len()); + + let mut m = msg[i..(i + max)].to_owned(); + m = m + "\r\n"; + self.send_queue.push_back(m); + i += MAX_MSG_LEN - "\r\n".len() + } + } else { + msg = msg + "\r\n"; + self.send_queue.push_back(msg); + } + } + + pub fn join(&mut self, channel: &str) { + info!("Joining {channel}"); + self.queue(&format!("JOIN {}", channel)); + } + + pub fn nick(&mut self, nick: &str) { + self.queue(&format!("NICK {}", nick)); + } + + pub fn mode(&mut self, channel: &str, modes: &str) { + self.queue(&format!("MODE {} {}", channel, modes)) + } +} diff --git a/src/lib.rs b/src/lib.rs index f91d7d3..7d33159 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod context; pub mod events; pub mod factory; pub mod format; @@ -17,9 +18,10 @@ use std::{ }; use async_native_tls::TlsStream; +use context::IrcContext; use factory::Factory; use irc_command::IrcCommand; -use log::{debug, info, trace, warn}; +use log::{info, trace, warn}; use serde::{Deserialize, Serialize}; use system::{IntoSystem, Response, StoredSystem, System}; use tokio::{ @@ -176,52 +178,6 @@ pub struct IrcConfig { admins: Vec, } -// TODO: -/* - split Irc into two structs, one for the context, which is Send + Sync to be usable in tasks - one for the comms. - -*/ - -pub struct IrcContext { - send_queue: VecDeque, -} - -impl IrcContext { - pub fn privmsg(&mut self, channel: &str, message: &str) { - debug!("sending privmsg to {} : {}", channel, message); - self.queue(&format!("PRIVMSG {} :{}", channel, message)); - } - fn queue(&mut self, msg: &str) { - let mut msg = msg.replace("\r", "").replace("\n", ""); - - if msg.len() > MAX_MSG_LEN - "\r\n".len() { - let mut i = 0; - - while i < msg.len() { - let max = (MAX_MSG_LEN - "\r\n".len()).min(msg[i..].len()); - - let mut m = msg[i..(i + max)].to_owned(); - m = m + "\r\n"; - self.send_queue.push_back(m); - i += MAX_MSG_LEN - "\r\n".len() - } - } else { - msg = msg + "\r\n"; - self.send_queue.push_back(msg); - } - } - - pub fn join(&mut self, channel: &str) { - info!("Joining {channel}"); - self.queue(&format!("JOIN {}", channel)); - } - - pub fn nick(&mut self, nick: &str) { - self.queue(&format!("NICK {}", nick)); - } -} - pub trait AsyncReadWrite: AsyncRead + AsyncWrite + Send + Unpin {} impl AsyncReadWrite for T {}