author | alfadur |
Wed, 28 Aug 2019 00:10:41 +0300 | |
changeset 15369 | 0f2fd8d12734 |
parent 15103 | 823052e66611 |
child 15520 | fd3a20e9d095 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
15074 | 4 |
core::{ |
15075 | 5 |
client::HwClient, |
6 |
server::{HwAnteClient, HwAnteroom, HwServer}, |
|
7 |
types::ClientId, |
|
14693 | 8 |
}, |
15075 | 9 |
protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
10 |
utils::is_name_illegal, |
|
13419 | 11 |
}; |
15074 | 12 |
|
14457 | 13 |
use log::*; |
13800 | 14 |
#[cfg(feature = "official-server")] |
15 |
use openssl::sha::sha1; |
|
14693 | 16 |
use std::{ |
17 |
fmt::{Formatter, LowerHex}, |
|
18 |
num::NonZeroU16, |
|
19 |
}; |
|
13800 | 20 |
|
14693 | 21 |
pub enum LoginResult { |
22 |
Unchanged, |
|
23 |
Complete, |
|
24 |
Exit, |
|
25 |
} |
|
26 |
||
14781 | 27 |
fn completion_result<'a, I>( |
28 |
mut other_clients: I, |
|
15075 | 29 |
client: &mut HwAnteClient, |
14781 | 30 |
response: &mut super::Response, |
31 |
) -> LoginResult |
|
32 |
where |
|
15075 | 33 |
I: Iterator<Item = (ClientId, &'a HwClient)>, |
14781 | 34 |
{ |
35 |
let has_nick_clash = |
|
36 |
other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap()); |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
37 |
|
14781 | 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 |
{ |
|
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
50 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
51 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
52 |
}); |
14781 | 53 |
LoginResult::Unchanged |
54 |
} |
|
55 |
||
56 |
#[cfg(not(feature = "official-server"))] |
|
57 |
{ |
|
58 |
LoginResult::Complete |
|
59 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
60 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
61 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
62 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
63 |
pub fn handle( |
15075 | 64 |
server: &mut HwServer, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
65 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
66 |
response: &mut super::Response, |
15075 | 67 |
message: HwProtocolMessage, |
14693 | 68 |
) -> LoginResult { |
12147 | 69 |
match message { |
15075 | 70 |
HwProtocolMessage::Quit(_) => { |
14693 | 71 |
response.add(Bye("User quit".to_string()).send_self()); |
72 |
LoginResult::Exit |
|
73 |
} |
|
15075 | 74 |
HwProtocolMessage::Nick(nick) => { |
14781 | 75 |
let client = &mut server.anteroom.clients[client_id]; |
14870 | 76 |
|
14781 | 77 |
if client.nick.is_some() { |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
78 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14693 | 79 |
LoginResult::Unchanged |
14457 | 80 |
} else if is_name_illegal(&nick) { |
14693 | 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 |
|
14457 | 83 |
} else { |
14693 | 84 |
client.nick = Some(nick.clone()); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
85 |
response.add(Nick(nick).send_self()); |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
86 |
|
14693 | 87 |
if client.protocol_number.is_some() { |
14781 | 88 |
completion_result(server.clients.iter(), client, response) |
14693 | 89 |
} else { |
90 |
LoginResult::Unchanged |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
91 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
92 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
93 |
} |
15075 | 94 |
HwProtocolMessage::Proto(proto) => { |
14781 | 95 |
let client = &mut server.anteroom.clients[client_id]; |
14693 | 96 |
if client.protocol_number.is_some() { |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
97 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14693 | 98 |
LoginResult::Unchanged |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
99 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
100 |
response.add(Error("Bad number.".to_string()).send_self()); |
14693 | 101 |
LoginResult::Unchanged |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
102 |
} else { |
14693 | 103 |
client.protocol_number = NonZeroU16::new(proto); |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
104 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
105 |
|
14693 | 106 |
if client.nick.is_some() { |
14781 | 107 |
completion_result(server.clients.iter(), client, response) |
14693 | 108 |
} else { |
109 |
LoginResult::Unchanged |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
110 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
111 |
} |
13798 | 112 |
} |
113 |
#[cfg(feature = "official-server")] |
|
15075 | 114 |
HwProtocolMessage::Password(hash, salt) => { |
14781 | 115 |
let client = &server.anteroom.clients[client_id]; |
13800 | 116 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
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:
14696
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:
14696
diff
changeset
|
119 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
120 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
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:
14696
diff
changeset
|
122 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
123 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
124 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
125 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
126 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
127 |
LoginResult::Unchanged |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
128 |
} |
13800 | 129 |
#[cfg(feature = "official-server")] |
15075 | 130 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
14781 | 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 |
} |
|
13798 | 141 |
} |
14693 | 142 |
_ => { |
143 |
warn!("Incorrect command in logging-in state"); |
|
144 |
LoginResult::Unchanged |
|
145 |
} |
|
12147 | 146 |
} |
147 |
} |