author | alfadur |
Fri, 05 Jul 2019 23:40:03 +0300 | |
changeset 15221 | 387345a14b3f |
parent 15108 | 823052e66611 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
13671 | 3 |
use crate::{ |
15079 | 4 |
core::{ |
15080 | 5 |
client::HwClient, |
6 |
server::{HwAnteClient, HwAnteroom, HwServer}, |
|
7 |
types::ClientId, |
|
14698 | 8 |
}, |
15080 | 9 |
protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
10 |
utils::is_name_illegal, |
|
13424 | 11 |
}; |
15079 | 12 |
|
14462 | 13 |
use log::*; |
13805 | 14 |
#[cfg(feature = "official-server")] |
15 |
use openssl::sha::sha1; |
|
14698 | 16 |
use std::{ |
17 |
fmt::{Formatter, LowerHex}, |
|
18 |
num::NonZeroU16, |
|
19 |
}; |
|
13805 | 20 |
|
14698 | 21 |
pub enum LoginResult { |
22 |
Unchanged, |
|
23 |
Complete, |
|
24 |
Exit, |
|
25 |
} |
|
26 |
||
14786 | 27 |
fn completion_result<'a, I>( |
28 |
mut other_clients: I, |
|
15080 | 29 |
client: &mut HwAnteClient, |
14786 | 30 |
response: &mut super::Response, |
31 |
) -> LoginResult |
|
32 |
where |
|
15080 | 33 |
I: Iterator<Item = (ClientId, &'a HwClient)>, |
14786 | 34 |
{ |
35 |
let has_nick_clash = |
|
36 |
other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap()); |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
37 |
|
14786 | 38 |
if has_nick_clash { |
39 |
if client.protocol_number.unwrap().get() < 38 { |
|
40 |
response.add(Bye("User quit: Nickname is already in use".to_string()).send_self()); |
|
41 |
LoginResult::Exit |
|
42 |
} else { |
|
43 |
client.nick = None; |
|
44 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
45 |
LoginResult::Unchanged |
|
46 |
} |
|
47 |
} else { |
|
48 |
#[cfg(feature = "official-server")] |
|
49 |
{ |
|
15108
823052e66611
check for account existence before asking passwords
alfadur
parents:
15080
diff
changeset
|
50 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15080
diff
changeset
|
51 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15080
diff
changeset
|
52 |
}); |
14786 | 53 |
LoginResult::Unchanged |
54 |
} |
|
55 |
||
56 |
#[cfg(not(feature = "official-server"))] |
|
57 |
{ |
|
58 |
LoginResult::Complete |
|
59 |
} |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
60 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
61 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
62 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
63 |
pub fn handle( |
15080 | 64 |
server: &mut HwServer, |
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
65 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
66 |
response: &mut super::Response, |
15080 | 67 |
message: HwProtocolMessage, |
14698 | 68 |
) -> LoginResult { |
12152 | 69 |
match message { |
15080 | 70 |
HwProtocolMessage::Quit(_) => { |
14698 | 71 |
response.add(Bye("User quit".to_string()).send_self()); |
72 |
LoginResult::Exit |
|
73 |
} |
|
15080 | 74 |
HwProtocolMessage::Nick(nick) => { |
14786 | 75 |
let client = &mut server.anteroom.clients[client_id]; |
14875 | 76 |
|
14786 | 77 |
if client.nick.is_some() { |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
78 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14698 | 79 |
LoginResult::Unchanged |
14462 | 80 |
} else if is_name_illegal(&nick) { |
14698 | 81 |
response.add(Bye("Illegal nickname! Nicknames must be between 1-40 characters long, must not have a trailing or leading space and must not have any of these characters: $()*+?[]^{|}".to_string()).send_self()); |
82 |
LoginResult::Exit |
|
14462 | 83 |
} else { |
14698 | 84 |
client.nick = Some(nick.clone()); |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
85 |
response.add(Nick(nick).send_self()); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
86 |
|
14698 | 87 |
if client.protocol_number.is_some() { |
14786 | 88 |
completion_result(server.clients.iter(), client, response) |
14698 | 89 |
} else { |
90 |
LoginResult::Unchanged |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
91 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
92 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
93 |
} |
15080 | 94 |
HwProtocolMessage::Proto(proto) => { |
14786 | 95 |
let client = &mut server.anteroom.clients[client_id]; |
14698 | 96 |
if client.protocol_number.is_some() { |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
97 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14698 | 98 |
LoginResult::Unchanged |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
99 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
100 |
response.add(Error("Bad number.".to_string()).send_self()); |
14698 | 101 |
LoginResult::Unchanged |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
102 |
} else { |
14698 | 103 |
client.protocol_number = NonZeroU16::new(proto); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
104 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
105 |
|
14698 | 106 |
if client.nick.is_some() { |
14786 | 107 |
completion_result(server.clients.iter(), client, response) |
14698 | 108 |
} else { |
109 |
LoginResult::Unchanged |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
110 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
111 |
} |
13803 | 112 |
} |
113 |
#[cfg(feature = "official-server")] |
|
15080 | 114 |
HwProtocolMessage::Password(hash, salt) => { |
14786 | 115 |
let client = &server.anteroom.clients[client_id]; |
13805 | 116 |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
117 |
if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
118 |
response.request_io(super::IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
119 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
120 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
121 |
server_salt: client.server_salt.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
122 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
123 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
124 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
125 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
126 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
127 |
LoginResult::Unchanged |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
128 |
} |
13805 | 129 |
#[cfg(feature = "official-server")] |
15080 | 130 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
14786 | 131 |
let client = &mut server.anteroom.clients[client_id]; |
132 |
if protocol == 0 { |
|
133 |
response.add(Error("Bad number.".to_string()).send_self()); |
|
134 |
LoginResult::Unchanged |
|
135 |
} else { |
|
136 |
client.protocol_number = NonZeroU16::new(protocol); |
|
137 |
client.nick = Some(nick); |
|
138 |
client.is_checker = true; |
|
139 |
LoginResult::Complete |
|
140 |
} |
|
13803 | 141 |
} |
14698 | 142 |
_ => { |
143 |
warn!("Incorrect command in logging-in state"); |
|
144 |
LoginResult::Unchanged |
|
145 |
} |
|
12152 | 146 |
} |
147 |
} |