author | alfadur <mail@none> |
Sat, 11 Jan 2020 00:44:25 +0300 | |
changeset 15562 | 479911540e17 |
parent 15558 | a798e6441a36 |
child 15826 | 747278149393 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
15554 | 3 |
use super::strings::*; |
13666 | 4 |
use crate::{ |
15095 | 5 |
core::{ |
15542 | 6 |
anteroom::{HwAnteroom, HwAnteroomClient}, |
15096 | 7 |
client::HwClient, |
15542 | 8 |
server::HwServer, |
15096 | 9 |
types::ClientId, |
14714 | 10 |
}, |
15096 | 11 |
protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
12 |
utils::is_name_illegal, |
|
13419 | 13 |
}; |
15095 | 14 |
|
14478 | 15 |
use log::*; |
13774 | 16 |
#[cfg(feature = "official-server")] |
17 |
use openssl::sha::sha1; |
|
14714 | 18 |
use std::{ |
19 |
fmt::{Formatter, LowerHex}, |
|
20 |
num::NonZeroU16, |
|
21 |
}; |
|
13774 | 22 |
|
14714 | 23 |
pub enum LoginResult { |
24 |
Unchanged, |
|
25 |
Complete, |
|
26 |
Exit, |
|
27 |
} |
|
28 |
||
14802 | 29 |
fn completion_result<'a, I>( |
30 |
mut other_clients: I, |
|
15542 | 31 |
client: &mut HwAnteroomClient, |
14802 | 32 |
response: &mut super::Response, |
33 |
) -> LoginResult |
|
34 |
where |
|
15542 | 35 |
I: Iterator<Item = &'a HwClient>, |
14802 | 36 |
{ |
15554 | 37 |
let has_nick_clash = other_clients.any(|c| 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 { |
15554 | 40 |
client.nick = None; |
41 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
42 |
LoginResult::Unchanged |
|
14802 | 43 |
} else { |
44 |
#[cfg(feature = "official-server")] |
|
45 |
{ |
|
15124
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
46 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
47 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15096
diff
changeset
|
48 |
}); |
14802 | 49 |
LoginResult::Unchanged |
50 |
} |
|
51 |
||
52 |
#[cfg(not(feature = "official-server"))] |
|
53 |
{ |
|
54 |
LoginResult::Complete |
|
55 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
56 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
57 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
58 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
59 |
pub fn handle( |
15542 | 60 |
server_state: &mut super::ServerState, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
61 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
62 |
response: &mut super::Response, |
15096 | 63 |
message: HwProtocolMessage, |
14714 | 64 |
) -> LoginResult { |
12147 | 65 |
match message { |
15096 | 66 |
HwProtocolMessage::Quit(_) => { |
14714 | 67 |
response.add(Bye("User quit".to_string()).send_self()); |
68 |
LoginResult::Exit |
|
69 |
} |
|
15096 | 70 |
HwProtocolMessage::Nick(nick) => { |
15542 | 71 |
let client = &mut server_state.anteroom.clients[client_id]; |
14891 | 72 |
|
14802 | 73 |
if client.nick.is_some() { |
15554 | 74 |
response.error(NICKNAME_PROVIDED); |
14714 | 75 |
LoginResult::Unchanged |
14478 | 76 |
} else if is_name_illegal(&nick) { |
15554 | 77 |
response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self()); |
14714 | 78 |
LoginResult::Exit |
14478 | 79 |
} else { |
14714 | 80 |
client.nick = Some(nick.clone()); |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
81 |
response.add(Nick(nick).send_self()); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
82 |
|
14714 | 83 |
if client.protocol_number.is_some() { |
15542 | 84 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 85 |
} else { |
86 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
87 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
88 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
89 |
} |
15096 | 90 |
HwProtocolMessage::Proto(proto) => { |
15542 | 91 |
let client = &mut server_state.anteroom.clients[client_id]; |
14714 | 92 |
if client.protocol_number.is_some() { |
15554 | 93 |
response.error(PROTOCOL_PROVIDED); |
14714 | 94 |
LoginResult::Unchanged |
15562 | 95 |
} else if proto < 48 { |
15554 | 96 |
response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self()); |
97 |
LoginResult::Exit |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
98 |
} else { |
14714 | 99 |
client.protocol_number = NonZeroU16::new(proto); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
100 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
101 |
|
14714 | 102 |
if client.nick.is_some() { |
15542 | 103 |
completion_result(server_state.server.iter_clients(), client, response) |
14714 | 104 |
} else { |
105 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
106 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
107 |
} |
13771 | 108 |
} |
109 |
#[cfg(feature = "official-server")] |
|
15096 | 110 |
HwProtocolMessage::Password(hash, salt) => { |
15542 | 111 |
let client = &server_state.anteroom.clients[client_id]; |
13774 | 112 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
116 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
117 |
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
|
118 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
119 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
120 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
121 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
122 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
123 |
LoginResult::Unchanged |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
124 |
} |
13774 | 125 |
#[cfg(feature = "official-server")] |
15096 | 126 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
15542 | 127 |
let client = &mut server_state.anteroom.clients[client_id]; |
14802 | 128 |
if protocol == 0 { |
15554 | 129 |
response.error("Bad number."); |
14802 | 130 |
LoginResult::Unchanged |
131 |
} else { |
|
132 |
client.protocol_number = NonZeroU16::new(protocol); |
|
133 |
client.is_checker = true; |
|
15554 | 134 |
#[cfg(not(feature = "official-server"))] |
135 |
{ |
|
136 |
response.request_io(super::IoTask::GetCheckerAccount { |
|
137 |
nick: nick, |
|
138 |
password: password, |
|
139 |
}); |
|
140 |
LoginResult::Unchanged |
|
141 |
} |
|
142 |
||
143 |
#[cfg(feature = "official-server")] |
|
144 |
{ |
|
145 |
response.add(LogonPassed.send_self()); |
|
146 |
LoginResult::Complete |
|
147 |
} |
|
14802 | 148 |
} |
13771 | 149 |
} |
14714 | 150 |
_ => { |
15554 | 151 |
warn!("Incorrect command in anteroom"); |
14714 | 152 |
LoginResult::Unchanged |
153 |
} |
|
12147 | 154 |
} |
155 |
} |