author | Wuzzy <Wuzzy@disroot.org> |
Wed, 28 Jun 2023 18:57:00 +0200 | |
changeset 15967 | f362e82cddcf |
parent 15937 | e514ceb5e7d6 |
child 15990 | c40f5e27aaf0 |
permissions | -rw-r--r-- |
15532 | 1 |
use super::strings::*; |
15804 | 2 |
use crate::handlers::actions::ToPendingMessage; |
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 |
utils::is_name_illegal, |
13419 | 11 |
}; |
15804 | 12 |
use hedgewars_network_protocol::messages::{ |
13 |
HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*, |
|
14 |
}; |
|
14457 | 15 |
use log::*; |
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, |
|
15520 | 29 |
client: &mut HwAnteroomClient, |
14781 | 30 |
response: &mut super::Response, |
31 |
) -> LoginResult |
|
32 |
where |
|
15520 | 33 |
I: Iterator<Item = &'a HwClient>, |
14781 | 34 |
{ |
15532 | 35 |
let has_nick_clash = other_clients.any(|c| 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
|
36 |
|
14781 | 37 |
if has_nick_clash { |
15532 | 38 |
client.nick = None; |
39 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
40 |
LoginResult::Unchanged |
|
14781 | 41 |
} else { |
42 |
#[cfg(feature = "official-server")] |
|
43 |
{ |
|
15103
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
44 |
response.request_io(super::IoTask::CheckRegistered { |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
45 |
nick: client.nick.as_ref().unwrap().clone(), |
823052e66611
check for account existence before asking passwords
alfadur
parents:
15075
diff
changeset
|
46 |
}); |
14781 | 47 |
LoginResult::Unchanged |
48 |
} |
|
49 |
||
50 |
#[cfg(not(feature = "official-server"))] |
|
51 |
{ |
|
52 |
LoginResult::Complete |
|
53 |
} |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
54 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
55 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
56 |
|
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
57 |
pub fn handle( |
15520 | 58 |
server_state: &mut super::ServerState, |
14671
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
59 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14457
diff
changeset
|
60 |
response: &mut super::Response, |
15075 | 61 |
message: HwProtocolMessage, |
14693 | 62 |
) -> LoginResult { |
12147 | 63 |
match message { |
15075 | 64 |
HwProtocolMessage::Quit(_) => { |
14693 | 65 |
response.add(Bye("User quit".to_string()).send_self()); |
66 |
LoginResult::Exit |
|
67 |
} |
|
15075 | 68 |
HwProtocolMessage::Nick(nick) => { |
15520 | 69 |
let client = &mut server_state.anteroom.clients[client_id]; |
14870 | 70 |
|
14781 | 71 |
if client.nick.is_some() { |
15532 | 72 |
response.error(NICKNAME_PROVIDED); |
14693 | 73 |
LoginResult::Unchanged |
14457 | 74 |
} else if is_name_illegal(&nick) { |
15532 | 75 |
response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self()); |
14693 | 76 |
LoginResult::Exit |
14457 | 77 |
} else { |
14693 | 78 |
client.nick = Some(nick.clone()); |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
79 |
response.add(Nick(nick).send_self()); |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
80 |
|
14693 | 81 |
if client.protocol_number.is_some() { |
15520 | 82 |
completion_result(server_state.server.iter_clients(), client, response) |
14693 | 83 |
} else { |
84 |
LoginResult::Unchanged |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
85 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
86 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
87 |
} |
15075 | 88 |
HwProtocolMessage::Proto(proto) => { |
15520 | 89 |
let client = &mut server_state.anteroom.clients[client_id]; |
14693 | 90 |
if client.protocol_number.is_some() { |
15532 | 91 |
response.error(PROTOCOL_PROVIDED); |
14693 | 92 |
LoginResult::Unchanged |
15540 | 93 |
} else if proto < 48 { |
15532 | 94 |
response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self()); |
95 |
LoginResult::Exit |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
96 |
} else { |
14693 | 97 |
client.protocol_number = NonZeroU16::new(proto); |
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
98 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
99 |
|
14693 | 100 |
if client.nick.is_some() { |
15520 | 101 |
completion_result(server_state.server.iter_clients(), client, response) |
14693 | 102 |
} else { |
103 |
LoginResult::Unchanged |
|
14673
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14672
diff
changeset
|
104 |
} |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
105 |
} |
13798 | 106 |
} |
107 |
#[cfg(feature = "official-server")] |
|
15075 | 108 |
HwProtocolMessage::Password(hash, salt) => { |
15520 | 109 |
let client = &server_state.anteroom.clients[client_id]; |
13800 | 110 |
|
14779
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
111 |
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
|
112 |
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
|
113 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
114 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
115 |
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
|
116 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
117 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
118 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
119 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
120 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14696
diff
changeset
|
121 |
LoginResult::Unchanged |
14672
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14671
diff
changeset
|
122 |
} |
13800 | 123 |
#[cfg(feature = "official-server")] |
15075 | 124 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
15520 | 125 |
let client = &mut server_state.anteroom.clients[client_id]; |
14781 | 126 |
if protocol == 0 { |
15532 | 127 |
response.error("Bad number."); |
14781 | 128 |
LoginResult::Unchanged |
129 |
} else { |
|
130 |
client.protocol_number = NonZeroU16::new(protocol); |
|
131 |
client.is_checker = true; |
|
15532 | 132 |
#[cfg(not(feature = "official-server"))] |
133 |
{ |
|
134 |
response.request_io(super::IoTask::GetCheckerAccount { |
|
135 |
nick: nick, |
|
136 |
password: password, |
|
137 |
}); |
|
138 |
LoginResult::Unchanged |
|
139 |
} |
|
140 |
||
141 |
#[cfg(feature = "official-server")] |
|
142 |
{ |
|
143 |
response.add(LogonPassed.send_self()); |
|
144 |
LoginResult::Complete |
|
145 |
} |
|
14781 | 146 |
} |
13798 | 147 |
} |
14693 | 148 |
_ => { |
15532 | 149 |
warn!("Incorrect command in anteroom"); |
14693 | 150 |
LoginResult::Unchanged |
151 |
} |
|
12147 | 152 |
} |
153 |
} |