author | alfadur |
Tue, 28 May 2019 21:28:32 +0300 | |
changeset 15096 | e935b1ad23f3 |
parent 15095 | c5a6e8566425 |
child 15124 | 823052e66611 |
permissions | -rw-r--r-- |
12147 | 1 |
use mio; |
2 |
||
13666 | 3 |
use crate::{ |
15095 | 4 |
core::{ |
15096 | 5 |
client::HwClient, |
6 |
server::{HwAnteClient, HwAnteroom, HwServer}, |
|
7 |
types::ClientId, |
|
14714 | 8 |
}, |
15096 | 9 |
protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
10 |
utils::is_name_illegal, |
|
13419 | 11 |
}; |
15095 | 12 |
|
14478 | 13 |
use log::*; |
13774 | 14 |
#[cfg(feature = "official-server")] |
15 |
use openssl::sha::sha1; |
|
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, |
|
15096 | 29 |
client: &mut HwAnteClient, |
14802 | 30 |
response: &mut super::Response, |
31 |
) -> LoginResult |
|
32 |
where |
|
15096 | 33 |
I: Iterator<Item = (ClientId, &'a HwClient)>, |
14802 | 34 |
{ |
35 |
let has_nick_clash = |
|
36 |
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
|
37 |
|
14802 | 38 |
if has_nick_clash { |
39 |
if client.protocol_number.unwrap().get() < 38 { |
|
40 |
response.add(Bye("User quit: Nickname is already in use".to_string()).send_self()); |
|
41 |
LoginResult::Exit |
|
42 |
} else { |
|
43 |
client.nick = None; |
|
44 |
response.add(Notice("NickAlreadyInUse".to_string()).send_self()); |
|
45 |
LoginResult::Unchanged |
|
46 |
} |
|
47 |
} else { |
|
48 |
#[cfg(feature = "official-server")] |
|
49 |
{ |
|
50 |
response.add(AskPassword(client.server_salt.clone()).send_self()); |
|
51 |
LoginResult::Unchanged |
|
52 |
} |
|
53 |
||
54 |
#[cfg(not(feature = "official-server"))] |
|
55 |
{ |
|
56 |
LoginResult::Complete |
|
57 |
} |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
58 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
59 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
60 |
|
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
61 |
pub fn handle( |
15096 | 62 |
server: &mut HwServer, |
14692
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
63 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14478
diff
changeset
|
64 |
response: &mut super::Response, |
15096 | 65 |
message: HwProtocolMessage, |
14714 | 66 |
) -> LoginResult { |
12147 | 67 |
match message { |
15096 | 68 |
HwProtocolMessage::Quit(_) => { |
14714 | 69 |
response.add(Bye("User quit".to_string()).send_self()); |
70 |
LoginResult::Exit |
|
71 |
} |
|
15096 | 72 |
HwProtocolMessage::Nick(nick) => { |
14802 | 73 |
let client = &mut server.anteroom.clients[client_id]; |
14891 | 74 |
|
14802 | 75 |
if client.nick.is_some() { |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
76 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14714 | 77 |
LoginResult::Unchanged |
14478 | 78 |
} else if is_name_illegal(&nick) { |
14714 | 79 |
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()); |
80 |
LoginResult::Exit |
|
14478 | 81 |
} else { |
14714 | 82 |
client.nick = Some(nick.clone()); |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
83 |
response.add(Nick(nick).send_self()); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
84 |
|
14714 | 85 |
if client.protocol_number.is_some() { |
14802 | 86 |
completion_result(server.clients.iter(), client, response) |
14714 | 87 |
} else { |
88 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
89 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
90 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
91 |
} |
15096 | 92 |
HwProtocolMessage::Proto(proto) => { |
14802 | 93 |
let client = &mut server.anteroom.clients[client_id]; |
14714 | 94 |
if client.protocol_number.is_some() { |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
95 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14714 | 96 |
LoginResult::Unchanged |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
97 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
98 |
response.add(Error("Bad number.".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 { |
14714 | 101 |
client.protocol_number = NonZeroU16::new(proto); |
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
102 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
103 |
|
14714 | 104 |
if client.nick.is_some() { |
14802 | 105 |
completion_result(server.clients.iter(), client, response) |
14714 | 106 |
} else { |
107 |
LoginResult::Unchanged |
|
14694
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14693
diff
changeset
|
108 |
} |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
109 |
} |
13771 | 110 |
} |
111 |
#[cfg(feature = "official-server")] |
|
15096 | 112 |
HwProtocolMessage::Password(hash, salt) => { |
14802 | 113 |
let client = &server.anteroom.clients[client_id]; |
13774 | 114 |
|
14800
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
118 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
119 |
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
|
120 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
121 |
password_hash: hash, |
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 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
124 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14717
diff
changeset
|
125 |
LoginResult::Unchanged |
14693
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14692
diff
changeset
|
126 |
} |
13774 | 127 |
#[cfg(feature = "official-server")] |
15096 | 128 |
HwProtocolMessage::Checker(protocol, nick, password) => { |
14802 | 129 |
let client = &mut server.anteroom.clients[client_id]; |
130 |
if protocol == 0 { |
|
131 |
response.add(Error("Bad number.".to_string()).send_self()); |
|
132 |
LoginResult::Unchanged |
|
133 |
} else { |
|
134 |
client.protocol_number = NonZeroU16::new(protocol); |
|
135 |
client.nick = Some(nick); |
|
136 |
client.is_checker = true; |
|
137 |
LoginResult::Complete |
|
138 |
} |
|
13771 | 139 |
} |
14714 | 140 |
_ => { |
141 |
warn!("Incorrect command in logging-in state"); |
|
142 |
LoginResult::Unchanged |
|
143 |
} |
|
12147 | 144 |
} |
145 |
} |