From 31281d282028283a58332a3979e09b847f6ec529 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Tue, 9 Dec 2014 16:17:05 -0500 Subject: [PATCH] Added support for connecting to a password-protected server. --- src/data/config.rs | 18 +++++++++--------- src/server/utils.rs | 7 +++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/data/config.rs b/src/data/config.rs index 9377c97..71a5434 100644 --- a/src/data/config.rs +++ b/src/data/config.rs @@ -19,12 +19,12 @@ pub struct Config { pub username: Option, /// The bot's real name. pub realname: Option, - /// The bot's password. - pub password: Option, /// The server to connect to. pub server: Option, /// The port to connect on. pub port: Option, + /// The password to connect to the server. + pub password: Option, /// Whether or not to use SSL. /// Bots will automatically panic if this is enabled without SSL support. pub use_ssl: Option, @@ -91,13 +91,6 @@ impl Config { self.realname.as_ref().map(|s| s[]).unwrap_or(self.nickname()) } - /// Gets the password specified in the configuration. - /// This defaults to a blank string when not specified. - #[experimental] - pub fn password(&self) -> &str { - self.password.as_ref().map(|s| s[]).unwrap_or("") - } - /// Gets the address of the server specified in the configuration. /// This panics when not specified. #[experimental] @@ -116,6 +109,13 @@ impl Config { }) } + /// Gets the server password specified in the configuration. + /// This defaults to a blank string when not specified. + #[experimental] + pub fn password(&self) -> &str { + self.password.as_ref().map(|s| s[]).unwrap_or("") + } + /// Gets whether or not to use SSL with this connection. /// This defaults to false when not specified. #[experimental] diff --git a/src/server/utils.rs b/src/server/utils.rs index 071bc34..1629e99 100644 --- a/src/server/utils.rs +++ b/src/server/utils.rs @@ -3,8 +3,8 @@ use std::io::IoResult; use data::{Command, Config, User}; -use data::Command::{CAP, INVITE, JOIN, KILL, MODE, NICK, NOTICE, KICK}; -use data::Command::{OPER, PONG, PRIVMSG, SAMODE, SANICK, TOPIC, USER}; +use data::Command::{CAP, INVITE, JOIN, KICK, KILL, MODE, NICK, NOTICE}; +use data::Command::{OPER, PASS, PONG, PRIVMSG, SAMODE, SANICK, TOPIC, USER}; use data::command::CapSubCommand::{END, REQ}; use data::kinds::{IrcReader, IrcWriter}; use server::{Server, ServerIterator}; @@ -47,6 +47,9 @@ impl<'a, T: IrcReader, U: IrcWriter> Wrapper<'a, T, U> { // We'll issue a CAP REQ for multi-prefix support to improve access level tracking. try!(self.server.send(CAP(REQ, Some("multi-prefix")))); try!(self.server.send(CAP(END, None))); // Then, send a CAP END to end the negotiation. + if self.server.config().password() != "" { + try!(self.server.send(PASS(self.server.config().password()))); + } try!(self.server.send(NICK(self.server.config().nickname()))); self.server.send(USER(self.server.config().username(), "0", self.server.config().real_name()))