use std::{
io::{Error, ErrorKind},
str::FromStr,
};
use irc_proto::{Command, Message, Response};
use crate::connection::InitiatedConnection;
#[derive(Copy, Clone, Debug)]
pub enum AuthStrategy {
Plain,
}
impl AuthStrategy {
pub const SUPPORTED: &'static str = "PLAIN";
}
impl FromStr for AuthStrategy {
type Err = std::io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"PLAIN" => Ok(Self::Plain),
_ => Err(Error::new(ErrorKind::InvalidData, "unknown auth strategy")),
}
}
}
pub struct SaslAlreadyAuthenticated(pub String);
impl SaslAlreadyAuthenticated {
#[must_use]
pub fn into_message(self) -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::ERR_SASLALREADY,
vec![
self.0,
"You have already authenticated using SASL".to_string(),
],
),
}
}
}
pub struct SaslStrategyUnsupported;
impl SaslStrategyUnsupported {
#[must_use]
pub fn into_message() -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::RPL_SASLMECHS,
vec![
AuthStrategy::SUPPORTED.to_string(),
"are available SASL mechanisms".to_string(),
],
),
}
}
}
pub struct SaslSuccess;
impl SaslSuccess {
#[must_use]
pub fn into_message() -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::RPL_SASLSUCCESS,
vec!["SASL authentication successful".to_string()],
),
}
}
}
pub struct ConnectionSuccess(pub InitiatedConnection);
impl ConnectionSuccess {
#[must_use]
pub fn into_message(self) -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::RPL_LOGGEDIN,
vec![
self.0.nick.to_string(),
self.0.to_nick().to_string(),
self.0.user.to_string(),
format!("You are now logged in as {}", self.0.user),
],
),
}
}
}
pub struct SaslFail;
impl SaslFail {
#[must_use]
pub fn into_message() -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::ERR_SASLFAIL,
vec!["SASL authentication failed".to_string()],
),
}
}
}
pub struct SaslAborted;
impl SaslAborted {
#[must_use]
pub fn into_message() -> Message {
Message {
tags: None,
prefix: None,
command: Command::Response(
Response::ERR_SASLABORT,
vec!["SASL authentication aborted".to_string()],
),
}
}
}