author | alfadur <mail@none> |
Mon, 23 Dec 2019 18:08:55 +0300 | |
changeset 15542 | fd3a20e9d095 |
parent 15124 | 823052e66611 |
child 15554 | f1205f33bf5b |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
15095 | 4 |
core::{ |
15542 | 5 |
anteroom::{HwAnteroom, HwAnteroomClient}, |
15096 | 6 |
client::HwClient, |
15542 | 7 |
server::HwServer, |
15096 | 8 |
types::ClientId, |
14714 | 9 |
}, |
15096 | 10 |
protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
11 |
utils::is_name_illegal, |
|
13419 | 12 |
}; |
15095 | 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, |
|
15542 | 30 |
client: &mut HwAnteroomClient, |
14802 | 31 |
response: &mut super::Response, |
32 |
) -> LoginResult |
|
33 |
where |
|
15542 | 34 |
I: Iterator<Item = &'a HwClient>, |
14802 | 35 |
{ |
36 |
let has_nick_clash = |
|
15542 | 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 |
{ |
|
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
51 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
52 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
53 |
}); |
14802 | 54 |
LoginResult::Unchanged |
55 |
} |
|
56 |
||
57 |
#[cfg(not(feature = "official-server"))] |
|
58 |
{ |
|
59 |
LoginResult::Complete |
|
60 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
61 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
62 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
63 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
64 |
pub fn handle( |
15542 | 65 |
server_state: &mut super::ServerState, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
66 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
67 |
response: &mut super::Response, |
15096 | 68 |
message: HwProtocolMessage, |
14714 | 69 |
) -> LoginResult { |
12147 | 70 |
match message { |
15096 | 71 |
HwProtocolMessage::Quit(_) => { |
14714 | 72 |
response.add(Bye("User quit".to_string()).send_self()); |
73 |
LoginResult::Exit |
|
74 |
} |
|
15096 | 75 |
HwProtocolMessage::Nick(nick) => { |
15542 | 76 |
let client = &mut server_state.anteroom.clients[client_id]; |
14891 | 77 |
|
14802 | 78 |
if client.nick.is_some() { |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
79 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14714 | 80 |
LoginResult::Unchanged |
14478 | 81 |
} else if is_name_illegal(&nick) { |
14714 | 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 |
|
14478 | 84 |
} else { |
14714 | 85 |
client.nick = Some(nick.clone()); |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
86 |
response.add(Nick(nick).send_self()); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
87 |
|
14714 | 88 |
if client.protocol_number.is_some() { |
15542 | 89 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 90 |
} else { |
91 |
LoginResult::Unchanged |
|
14694
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 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
94 |
} |
15096 | 95 |
HwProtocolMessage::Proto(proto) => { |
15542 | 96 |
let client = &mut server_state.anteroom.clients[client_id]; |
14714 | 97 |
if client.protocol_number.is_some() { |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
98 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14714 | 99 |
LoginResult::Unchanged |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
100 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
101 |
response.add(Error("Bad number.".to_string()).send_self()); |
14714 | 102 |
LoginResult::Unchanged |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
103 |
} else { |
14714 | 104 |
client.protocol_number = NonZeroU16::new(proto); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
105 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
106 |
|
14714 | 107 |
if client.nick.is_some() { |
15542 | 108 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 109 |
} else { |
110 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
111 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
112 |
} |
13771 | 113 |
} |
114 |
#[cfg(feature = "official-server")] |
|
15096 | 115 |
HwProtocolMessage::Password(hash, salt) => { |
15542 | 116 |
let client = &server_state.anteroom.clients[client_id]; |
13774 | 117 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
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:
14717
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:
14717
diff
changeset
|
120 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
121 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
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:
14717
diff
changeset
|
123 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
124 |
password_hash: hash, |
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 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
127 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
128 |
LoginResult::Unchanged |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
129 |
} |
13774 | 130 |
#[cfg(feature = "official-server")] |
15096 | 131 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
15542 | 132 |
let client = &mut server_state.anteroom.clients[client_id]; |
14802 | 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 |
} |
|
13771 | 142 |
} |
14714 | 143 |
_ => { |
144 |
warn!("Incorrect command in logging-in state"); |
|
145 |
LoginResult::Unchanged |
|
146 |
} |
|
12147 | 147 |
} |
148 |
} |