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