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