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