72 lines
2 KiB
Rust
72 lines
2 KiB
Rust
use models::{IrcMessage, IrcCommand};
|
|
use futures::sync::mpsc::UnboundedSender;
|
|
use futures::sync::mpsc::SendError;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct User {
|
|
pub nickname: String,
|
|
pub username: String,
|
|
pub hostname: String,
|
|
pub realname: String,
|
|
sendchannel: UnboundedSender<String>,
|
|
registered: bool,
|
|
}
|
|
|
|
impl User {
|
|
pub fn new(send: UnboundedSender<String>) -> User {
|
|
User {
|
|
nickname: String::new(),
|
|
username: String::new(),
|
|
hostname: String::new(),
|
|
realname: String::new(),
|
|
sendchannel: send,
|
|
registered: false,
|
|
}
|
|
}
|
|
|
|
pub fn register(&mut self) {
|
|
if self.registered { return; }
|
|
self.registered = true;
|
|
|
|
let mut msg = format!(":{0} 001 {1} :Welcome to IRC!\r\n:{0} 002 {1} :Your host is plankircd, running version 0.0.1\r\n:{0} 003 {1} :This server was created tomorrow\r\n:{0} 004 {1} plankircd plankircd-0.0.1 o o\r\n:{0} 422 {1} :MOTD File is missing\r\n", self.hostname, self.nickname);
|
|
|
|
let res = self.send_raw(msg);
|
|
println!("{:?}",res);
|
|
}
|
|
|
|
pub fn is_registered(&self) -> bool {
|
|
self.registered
|
|
}
|
|
|
|
pub fn set_nick(&mut self, nick: &str) {
|
|
self.nickname = nick.to_string();
|
|
}
|
|
|
|
pub fn set_user(&mut self, user: &str, host: &str, real: &str) {
|
|
|
|
self.username = user.to_string();
|
|
self.hostname = host.to_string();
|
|
self.realname = real.to_string();
|
|
}
|
|
|
|
pub fn to_string(&self) -> String {
|
|
format!("{}!{}@{}", self.nickname, self.username, self.hostname)
|
|
}
|
|
|
|
pub fn is_valid(&self) -> bool {
|
|
!self.nickname.is_empty() && !self.username.is_empty() && !self.hostname.is_empty()
|
|
}
|
|
|
|
pub fn send_raw(&self, msg: String) -> Result<(),SendError<String>> {
|
|
self.sendchannel.unbounded_send(msg)
|
|
}
|
|
|
|
pub fn send(&self, msg: IrcMessage) -> Result<(),SendError<String>> {
|
|
self.sendchannel.unbounded_send(msg.to_string())
|
|
}
|
|
|
|
pub fn privmsg(&self, from: &User, msg: &str) -> Result<(),SendError<String>> {
|
|
self.send(IrcMessage::new(Some(&from.nickname), IrcCommand::PRIVMSG, vec![&self.nickname, msg]))
|
|
}
|
|
}
|