author | alfadur |
Tue, 09 Apr 2019 21:08:35 +0300 | |
changeset 14784 | f43ab2bd76ae |
parent 14701 | 8a45c90f4580 |
child 14786 | 01f8ab45f806 |
permissions | -rw-r--r-- |
12152 | 1 |
use mio; |
2 |
||
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
3 |
use crate::protocol::messages::HWProtocolMessage::LoadRoom; |
13671 | 4 |
use crate::{ |
14462 | 5 |
protocol::messages::{HWProtocolMessage, HWServerMessage::*}, |
14698 | 6 |
server::{ |
7 |
core::{HWAnteClient, HWAnteroom}, |
|
8 |
coretypes::ClientId, |
|
9 |
}, |
|
14462 | 10 |
utils::is_name_illegal, |
13424 | 11 |
}; |
14462 | 12 |
use log::*; |
13805 | 13 |
#[cfg(feature = "official-server")] |
14 |
use openssl::sha::sha1; |
|
14698 | 15 |
use std::{ |
16 |
fmt::{Formatter, LowerHex}, |
|
17 |
num::NonZeroU16, |
|
18 |
}; |
|
13805 | 19 |
|
14698 | 20 |
pub enum LoginResult { |
21 |
Unchanged, |
|
22 |
Complete, |
|
23 |
Exit, |
|
24 |
} |
|
25 |
||
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
26 |
fn completion_result(client: &HWAnteClient, response: &mut super::Response) -> LoginResult { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
27 |
#[cfg(feature = "official-server")] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
28 |
{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
29 |
response.add(AskPassword(client.server_salt.clone()).send_self()); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
30 |
LoginResult::Unchanged |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
31 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
32 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
33 |
#[cfg(not(feature = "official-server"))] |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
34 |
{ |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
35 |
LoginResult::Complete |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
36 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
37 |
} |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
38 |
|
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
39 |
pub fn handle( |
14698 | 40 |
anteroom: &mut HWAnteroom, |
14676
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
41 |
client_id: ClientId, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
42 |
response: &mut super::Response, |
455865ccd36c
Server action refactoring part 2 of N
alfadur <mail@none>
parents:
14462
diff
changeset
|
43 |
message: HWProtocolMessage, |
14698 | 44 |
) -> LoginResult { |
12152 | 45 |
match message { |
14698 | 46 |
HWProtocolMessage::Quit(_) => { |
47 |
response.add(Bye("User quit".to_string()).send_self()); |
|
48 |
LoginResult::Exit |
|
49 |
} |
|
13421 | 50 |
HWProtocolMessage::Nick(nick) => { |
14698 | 51 |
let client = &mut anteroom.clients[client_id]; |
13671 | 52 |
debug!("{} {}", nick, is_name_illegal(&nick)); |
14698 | 53 |
if !client.nick.is_some() { |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
54 |
response.add(Error("Nickname already provided.".to_string()).send_self()); |
14698 | 55 |
LoginResult::Unchanged |
14462 | 56 |
} else if is_name_illegal(&nick) { |
14698 | 57 |
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()); |
58 |
LoginResult::Exit |
|
14462 | 59 |
} else { |
14698 | 60 |
client.nick = Some(nick.clone()); |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
61 |
response.add(Nick(nick).send_self()); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
62 |
|
14698 | 63 |
if client.protocol_number.is_some() { |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
64 |
completion_result(&client, response) |
14698 | 65 |
} else { |
66 |
LoginResult::Unchanged |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
67 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
68 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
69 |
} |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
70 |
HWProtocolMessage::Proto(proto) => { |
14698 | 71 |
let client = &mut anteroom.clients[client_id]; |
72 |
if client.protocol_number.is_some() { |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
73 |
response.add(Error("Protocol already known.".to_string()).send_self()); |
14698 | 74 |
LoginResult::Unchanged |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
75 |
} else if proto == 0 { |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
76 |
response.add(Error("Bad number.".to_string()).send_self()); |
14698 | 77 |
LoginResult::Unchanged |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
78 |
} else { |
14698 | 79 |
client.protocol_number = NonZeroU16::new(proto); |
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
80 |
response.add(Proto(proto).send_self()); |
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
81 |
|
14698 | 82 |
if client.nick.is_some() { |
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
83 |
completion_result(&client, response) |
14698 | 84 |
} else { |
85 |
LoginResult::Unchanged |
|
14678
08a8605bafaf
Server action refactoring part 4 of N
alfadur <mail@none>
parents:
14677
diff
changeset
|
86 |
} |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
87 |
} |
13803 | 88 |
} |
89 |
#[cfg(feature = "official-server")] |
|
13805 | 90 |
HWProtocolMessage::Password(hash, salt) => { |
14698 | 91 |
let client = &anteroom.clients[client_id]; |
13805 | 92 |
|
14784
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
93 |
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:
14701
diff
changeset
|
94 |
response.request_io(super::IoTask::GetAccount { |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
95 |
nick: nick.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
96 |
protocol: protocol.get(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
97 |
server_salt: client.server_salt.clone(), |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
98 |
client_salt: salt, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
99 |
password_hash: hash, |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
100 |
}); |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
101 |
}; |
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
102 |
|
f43ab2bd76ae
add a thread for internal server IO and implement account checking with it
alfadur
parents:
14701
diff
changeset
|
103 |
LoginResult::Unchanged |
14677
6e6632068a33
Server action refactoring part 3 of N
alfadur <mail@none>
parents:
14676
diff
changeset
|
104 |
} |
13805 | 105 |
#[cfg(feature = "official-server")] |
13803 | 106 |
HWProtocolMessage::Checker(protocol, nick, password) => { |
14698 | 107 |
let client = &mut anteroom.clients[client_id]; |
14700 | 108 |
client.protocol_number = NonZeroU16::new(protocol); |
14698 | 109 |
client.nick = Some(nick); |
110 |
//client.set_is_checker(true); |
|
111 |
LoginResult::Complete |
|
13803 | 112 |
} |
14698 | 113 |
_ => { |
114 |
warn!("Incorrect command in logging-in state"); |
|
115 |
LoginResult::Unchanged |
|
116 |
} |
|
12152 | 117 |
} |
118 |
} |