12147
|
1 |
use mio;
|
|
2 |
|
13666
|
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
|
13419
|
13 |
};
|
12147
|
14 |
|
13419
|
15 |
pub fn handle(server: & mut HWServer, client_id: ClientId, message: HWProtocolMessage) {
|
12147
|
16 |
match message {
|
13416
|
17 |
HWProtocolMessage::Nick(nick) => {
|
13666
|
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())]
|
13416
|
25 |
}
|
13666
|
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 |
|
13419
|
35 |
server.react(client_id, actions);
|
13416
|
36 |
},
|
12147
|
37 |
HWProtocolMessage::Proto(proto) => {
|
13666
|
38 |
let client = &mut server.clients[client_id];
|
|
39 |
let actions = if client.protocol_number != 0 {
|
|
40 |
vec![ProtocolError("Protocol already known.".to_string())]
|
13416
|
41 |
}
|
13666
|
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 |
};
|
13419
|
50 |
server.react(client_id, actions);
|
12147
|
51 |
},
|
|
52 |
_ => warn!("Incorrect command in logging-in state"),
|
|
53 |
}
|
|
54 |
}
|