Experimental topic implementation

This commit is contained in:
famfo 2022-01-10 17:33:35 +01:00
parent 340c652bb1
commit dde7aeb43f
3 changed files with 36 additions and 4 deletions

View file

@ -40,5 +40,5 @@ async fn main() -> Result<(), tokio::io::Error> {
Happy hacking!
todo list for famfo:
- [ ]: implement getting the topic of a channel
- [x]: implement getting the topic of a channel
- [ ]: implement getting users in a channel

View file

@ -217,9 +217,11 @@ pub enum Command {
/// Channel
String,
/// Topic
Option<String>,
String,
),
#[doc(hidden)]
TopicBy(String, String),
#[doc(hidden)]
USER(String, String, String, String),
}
@ -258,6 +260,16 @@ impl Command {
let msg = parts[3..].join(" ");
return Self::PRIVMSG(nick, target.to_string(), (msg[1..]).to_string());
} else if parts.get(1) == Some(&"332") {
let channel = parts[3];
let topic = parts[4..].join(" ");
return Self::TOPIC(channel.to_string(), topic.to_string());
} else if parts.get(1) == Some(&"333") {
let user = parts[4];
let time = parts[5];
return Self::TopicBy(user.to_string(), time.to_string());
}
Self::OTHER(new.to_string())

View file

@ -527,14 +527,34 @@ impl Client {
/// ```
/// # Errors
/// Returns IO errors from the TcpStream.
pub async fn topic(&mut self, channel: &str, topic: Option<&str>) -> Result<(), Error> {
pub async fn topic(
&mut self,
channel: &str,
topic: Option<&str>,
) -> Result<(String, String, String, String), Error> {
if let Some(topic) = topic {
self.write(format!("TOPIC {} :{}\r\n", channel, topic))
.await?;
return Ok((String::new(), String::new(), String::new(), String::new()));
} else {
// Can we make this more efficient? Yes! But how?
self.write(format!("TOPIC {}\r\n", channel)).await?;
let mut channel_channel = String::new();
let mut channel_topic = String::new();
if let Some(command) = self.read().await? {
if let commands::Command::TOPIC(channel, topic) = command {
channel_channel = channel;
channel_topic = topic;
}
}
let mut topic_return = (String::new(), String::new(), String::new(), String::new());
if let Some(command) = self.read().await? {
if let commands::Command::TopicBy(user, time) = command {
topic_return = (channel_channel, channel_topic, user, time);
}
}
return Ok(topic_return);
}
Ok(())
}
#[doc(hidden)]