author | Wuzzy <Wuzzy2@mail.ru> |
Mon, 29 Apr 2019 21:24:42 +0200 | |
changeset 14870 | 24df603fbcd2 |
parent 14802 | 01f8ab45f806 |
child 14891 | ce98c37826a7 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
3 |
use crate::protocol::messages::HWProtocolMessage::LoadRoom; |
14802 | 4 |
use crate::server::client::HWClient; |
5 |
use crate::server::core::HWServer; |
|
13666 | 6 |
use crate::{ |
14478 | 7 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
14714 | 8 |
server::{ |
9 |
core::{HWAnteClient, HWAnteroom}, |
|
10 |
coretypes::ClientId, |
|
11 |
}, |
|
14478 | 12 |
utils::is_name_illegal, |
13419 | 13 |
}; |
14478 | 14 |
use log::*; |
13774 | 15 |
#[cfg(feature = "official-server")] |
16 |
use openssl::sha::sha1; |
|
14714 | 17 |
use std::{ |
18 |
fmt::{Formatter, LowerHex}, |
|
19 |
num::NonZeroU16, |
|
20 |
}; |
|
13774 | 21 |
|
14714 | 22 |
pub enum LoginResult { |
23 |
Unchanged, |
|
24 |
Complete, |
|
25 |
Exit, |
|
26 |
} |
|
27 |
||
14802 | 28 |
fn completion_result<'a, I>( |
29 |
mut other_clients: I, |
|
30 |
client: &mut HWAnteClient, |
|
31 |
response: &mut super::Response, |
|
32 |
) -> LoginResult |
|
33 |
where |
|
34 |
I: Iterator<Item = (ClientId, &'a HWClient)>, |
|
35 |
{ |
|
36 |
let has_nick_clash = |
|
37 |
other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap()); |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
38 |
|
14802 | 39 |
if has_nick_clash { |
40 |
if client.protocol_number.unwrap().get() < 38 { |
|
41 |
response.add(Bye("User quit: Nickname is already in use".to_string()).send_self()); |
|
42 |
LoginResult::Exit |
|
43 |
} else { |
|
44 |
client.nick = None; |
|
45 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
46 |
LoginResult::Unchanged |
|
47 |
} |
|
48 |
} else { |
|
49 |
#[cfg(feature = "official-server")] |
|
50 |
{ |
|
51 |
response.add(AskPassword(client.server_salt.clone()).send_self()); |
|
52 |
LoginResult::Unchanged |
|
53 |
} |
|
54 |
||
55 |
#[cfg(not(feature = "official-server"))] |
|
56 |
{ |
|
57 |
LoginResult::Complete |
|
58 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
59 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
60 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
61 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
62 |
pub fn handle( |
14802 | 63 |
server: &mut HWServer, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
64 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
65 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
66 |
message: HWProtocolMessage, |
14714 | 67 |
) -> LoginResult { |
12147 | 68 |
match message { |
14714 | 69 |
HWProtocolMessage::Quit(_) => { |
70 |
response.add(Bye("User quit".to_string()).send_self()); |
|
71 |
LoginResult::Exit |
|
72 |
} |
|
13416 | 73 |
HWProtocolMessage::Nick(nick) => { |
14802 | 74 |
let client = &mut server.anteroom.clients[client_id]; |
13666 | 75 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
14802 | 76 |
if client.nick.is_some() { |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
77 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14714 | 78 |
LoginResult::Unchanged |
14478 | 79 |
} else if is_name_illegal(&nick) { |
14714 | 80 |
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()); |
81 |
LoginResult::Exit |
|
14478 | 82 |
} else { |
14714 | 83 |
client.nick = Some(nick.clone()); |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
84 |
response.add(Nick(nick).send_self()); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
85 |
|
14714 | 86 |
if client.protocol_number.is_some() { |
14802 | 87 |
completion_result(server.clients.iter(), client, response) |
14714 | 88 |
} else { |
89 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
90 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
91 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
92 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
93 |
HWProtocolMessage::Proto(proto) => { |
14802 | 94 |
let client = &mut server.anteroom.clients[client_id]; |
14714 | 95 |
if client.protocol_number.is_some() { |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
96 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14714 | 97 |
LoginResult::Unchanged |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
98 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
99 |
response.add(Error("Bad number.".to_string()).send_self()); |
14714 | 100 |
LoginResult::Unchanged |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
101 |
} else { |
14714 | 102 |
client.protocol_number = NonZeroU16::new(proto); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
103 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
104 |
|
14714 | 105 |
if client.nick.is_some() { |
14802 | 106 |
completion_result(server.clients.iter(), client, response) |
14714 | 107 |
} else { |
108 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
109 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
110 |
} |
13771 | 111 |
} |
112 |
#[cfg(feature = "official-server")] |
|
13774 | 113 |
HWProtocolMessage::Password(hash, salt) => { |
14802 | 114 |
let client = &server.anteroom.clients[client_id]; |
13774 | 115 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
116 |
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:
14717
diff
changeset
|
117 |
response.request_io(super::IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
118 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
119 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
120 |
server_salt: client.server_salt.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
121 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
122 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
123 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
124 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
125 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
126 |
LoginResult::Unchanged |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
127 |
} |
13774 | 128 |
#[cfg(feature = "official-server")] |
13771 | 129 |
HWProtocolMessage::Checker(protocol, nick, password) => { |
14802 | 130 |
let client = &mut server.anteroom.clients[client_id]; |
131 |
if protocol == 0 { |
|
132 |
response.add(Error("Bad number.".to_string()).send_self()); |
|
133 |
LoginResult::Unchanged |
|
134 |
} else { |
|
135 |
client.protocol_number = NonZeroU16::new(protocol); |
|
136 |
client.nick = Some(nick); |
|
137 |
client.is_checker = true; |
|
138 |
LoginResult::Complete |
|
139 |
} |
|
13771 | 140 |
} |
14714 | 141 |
_ => { |
142 |
warn!("Incorrect command in logging-in state"); |
|
143 |
LoginResult::Unchanged |
|
144 |
} |
|
12147 | 145 |
} |
146 |
} |