From 2cd160a72f6e17ace5d871b3d3ad4bf57c0aec25 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Mon, 22 Dec 2014 16:43:37 -0500 Subject: [PATCH] Added commands to send CTCP requests as per #7. --- src/server/mod.rs | 6 ++--- src/server/utils.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/server/mod.rs b/src/server/mod.rs index e1a6c86..03d2423 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -5,7 +5,7 @@ use std::io::{BufferedReader, BufferedWriter, IoError, IoErrorKind, IoResult}; use std::sync::{Mutex, RWLock}; use conn::{Connection, NetStream}; use data::{Command, Config, Message, Response, User}; -use data::Command::{JOIN, NICK, NICKSERV, NOTICE, PONG}; +use data::Command::{JOIN, NICK, NICKSERV, PONG}; use data::kinds::{IrcReader, IrcWriter}; #[cfg(feature = "ctcp")] use time::now; @@ -221,13 +221,13 @@ impl IrcServer { #[experimental] #[cfg(feature = "ctcp")] fn send_ctcp(&self, target: &str, msg: &str) { - self.send(NOTICE(target, format!("\u{001}{}\u{001}", msg)[])).unwrap(); + self.send(Command::NOTICE(target, format!("\u{001}{}\u{001}", msg)[])).unwrap(); } /// Handles CTCP requests if the CTCP feature is enabled. #[experimental] #[cfg(not(feature = "ctcp"))] - fn handle_ctcp(&self, msg: &Message) {} + fn handle_ctcp(&self, _: &Message) {} } /// An Iterator over an IrcServer's incoming Messages. diff --git a/src/server/utils.rs b/src/server/utils.rs index f1528aa..15938a2 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -7,6 +7,7 @@ use data::Command::{CAP, INVITE, JOIN, KICK, KILL, MODE, NICK, NOTICE}; use data::Command::{OPER, PASS, PONG, PRIVMSG, QUIT, SAMODE, SANICK, TOPIC, USER}; use data::command::CapSubCommand::{END, REQ}; use data::kinds::{IrcReader, IrcWriter}; +#[cfg(feature = "ctcp")] use time::get_time; use server::{Server, ServerIterator}; /// Functionality-providing wrapper for Server. @@ -165,11 +166,61 @@ impl<'a, T: IrcReader, U: IrcWriter> Wrapper<'a, T, U> { }))) } + /// Sends a CTCP-escaped message to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_ctcp(&self, target: &str, msg: &str) -> IoResult<()> { + self.send_privmsg(target, format!("\u{001}{}\u{001}", msg)[]) + } + /// Sends an action command to the specified target. #[experimental] #[cfg(feature = "ctcp")] pub fn send_action(&self, target: &str, msg: &str) -> IoResult<()> { - self.send_privmsg(target, format!("\u{001}ACTION {}\u{001}", msg )[]) + self.send_ctcp(target, format!("ACTION {}", msg)[]) + } + + /// Sends a finger request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_finger(&self, target: &str) -> IoResult<()> { + self.send_ctcp(target, "FINGER") + } + + /// Sends a version request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_version(&self, target: &str) -> IoResult<()> { + self.send_ctcp(target, "VERSION") + } + + /// Sends a source request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_source(&self, target: &str) -> IoResult<()> { + self.send_ctcp(target, "SOURCE") + } + + /// Sends a user info request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_user_info(&self, target: &str) -> IoResult<()> { + self.send_ctcp(target, "USERINFO") + } + + /// Sends a finger request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_ctcp_ping(&self, target: &str) -> IoResult<()> { + let time = get_time(); + self.send_ctcp(target, format!("PING {}.{}", time.sec, time.nsec)[]) + } + + /// Sends a time request to the specified target. + #[experimental] + #[cfg(feature = "ctcp")] + pub fn send_time(&self, target: &str) -> IoResult<()> { + self.send_ctcp(target, "TIME") } }