12152
|
1 |
use mio;
|
|
2 |
|
13671
|
3 |
use crate::{
|
|
4 |
server::{
|
|
5 |
server::HWServer,
|
|
6 |
coretypes::ClientId,
|
|
7 |
actions::{Action, Action::*}
|
|
8 |
},
|
|
9 |
protocol::messages::{
|
|
10 |
HWProtocolMessage, HWServerMessage::*
|
|
11 |
},
|
|
12 |
utils::is_name_illegal
|
13424
|
13 |
};
|
12152
|
14 |
|
13424
|
15 |
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
|
12152
|
16 |
match message {
|
13421
|
17 |
HWProtocolMessage::Nick(nick) => {
|
13671
|
18 |
let client = &mut server.clients[client_id];
|
|
19 |
debug!("{} {}", nick, is_name_illegal(&nick));
|
|
20 |
let actions = if client.room_id != None {
|
|
21 |
unreachable!()
|
|
22 |
}
|
|
23 |
else if !client.nick.is_empty() {
|
|
24 |
vec![ProtocolError("Nickname already provided.".to_string())]
|
13421
|
25 |
}
|
13671
|
26 |
else if is_name_illegal(&nick) {
|
|
27 |
vec![ByeClient("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())]
|
|
28 |
}
|
|
29 |
else {
|
|
30 |
client.nick = nick.clone();
|
|
31 |
vec![Nick(nick).send_self().action(),
|
|
32 |
CheckRegistered]
|
|
33 |
};
|
|
34 |
|
13424
|
35 |
server.react(client_id, actions);
|
13421
|
36 |
},
|
12152
|
37 |
HWProtocolMessage::Proto(proto) => {
|
13671
|
38 |
let client = &mut server.clients[client_id];
|
|
39 |
let actions = if client.protocol_number != 0 {
|
|
40 |
vec![ProtocolError("Protocol already known.".to_string())]
|
13421
|
41 |
}
|
13671
|
42 |
else if proto == 0 {
|
|
43 |
vec![ProtocolError("Bad number.".to_string())]
|
|
44 |
}
|
|
45 |
else {
|
|
46 |
client.protocol_number = proto;
|
|
47 |
vec![Proto(proto).send_self().action(),
|
|
48 |
CheckRegistered]
|
|
49 |
};
|
13424
|
50 |
server.react(client_id, actions);
|
12152
|
51 |
},
|
|
52 |
_ => warn!("Incorrect command in logging-in state"),
|
|
53 |
}
|
|
54 |
}
|