1 use mio; |
1 use mio; |
2 |
2 |
3 use crate::{ |
3 use crate::{ |
4 core::{ |
4 core::{ |
|
5 anteroom::{HwAnteroom, HwAnteroomClient}, |
5 client::HwClient, |
6 client::HwClient, |
6 server::{HwAnteClient, HwAnteroom, HwServer}, |
7 server::HwServer, |
7 types::ClientId, |
8 types::ClientId, |
8 }, |
9 }, |
9 protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
10 protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*}, |
10 utils::is_name_illegal, |
11 utils::is_name_illegal, |
11 }; |
12 }; |
24 Exit, |
25 Exit, |
25 } |
26 } |
26 |
27 |
27 fn completion_result<'a, I>( |
28 fn completion_result<'a, I>( |
28 mut other_clients: I, |
29 mut other_clients: I, |
29 client: &mut HwAnteClient, |
30 client: &mut HwAnteroomClient, |
30 response: &mut super::Response, |
31 response: &mut super::Response, |
31 ) -> LoginResult |
32 ) -> LoginResult |
32 where |
33 where |
33 I: Iterator<Item = (ClientId, &'a HwClient)>, |
34 I: Iterator<Item = &'a HwClient>, |
34 { |
35 { |
35 let has_nick_clash = |
36 let has_nick_clash = |
36 other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap()); |
37 other_clients.any(|c| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap()); |
37 |
38 |
38 if has_nick_clash { |
39 if has_nick_clash { |
39 if client.protocol_number.unwrap().get() < 38 { |
40 if client.protocol_number.unwrap().get() < 38 { |
40 response.add(Bye("User quit: Nickname is already in use".to_string()).send_self()); |
41 response.add(Bye("User quit: Nickname is already in use".to_string()).send_self()); |
41 LoginResult::Exit |
42 LoginResult::Exit |
59 } |
60 } |
60 } |
61 } |
61 } |
62 } |
62 |
63 |
63 pub fn handle( |
64 pub fn handle( |
64 server: &mut HwServer, |
65 server_state: &mut super::ServerState, |
65 client_id: ClientId, |
66 client_id: ClientId, |
66 response: &mut super::Response, |
67 response: &mut super::Response, |
67 message: HwProtocolMessage, |
68 message: HwProtocolMessage, |
68 ) -> LoginResult { |
69 ) -> LoginResult { |
69 match message { |
70 match message { |
70 HwProtocolMessage::Quit(_) => { |
71 HwProtocolMessage::Quit(_) => { |
71 response.add(Bye("User quit".to_string()).send_self()); |
72 response.add(Bye("User quit".to_string()).send_self()); |
72 LoginResult::Exit |
73 LoginResult::Exit |
73 } |
74 } |
74 HwProtocolMessage::Nick(nick) => { |
75 HwProtocolMessage::Nick(nick) => { |
75 let client = &mut server.anteroom.clients[client_id]; |
76 let client = &mut server_state.anteroom.clients[client_id]; |
76 |
77 |
77 if client.nick.is_some() { |
78 if client.nick.is_some() { |
78 response.add(Error("Nickname already provided.".to_string()).send_self()); |
79 response.add(Error("Nickname already provided.".to_string()).send_self()); |
79 LoginResult::Unchanged |
80 LoginResult::Unchanged |
80 } else if is_name_illegal(&nick) { |
81 } else if is_name_illegal(&nick) { |
83 } else { |
84 } else { |
84 client.nick = Some(nick.clone()); |
85 client.nick = Some(nick.clone()); |
85 response.add(Nick(nick).send_self()); |
86 response.add(Nick(nick).send_self()); |
86 |
87 |
87 if client.protocol_number.is_some() { |
88 if client.protocol_number.is_some() { |
88 completion_result(server.clients.iter(), client, response) |
89 completion_result(server_state.server.iter_clients(), client, response) |
89 } else { |
90 } else { |
90 LoginResult::Unchanged |
91 LoginResult::Unchanged |
91 } |
92 } |
92 } |
93 } |
93 } |
94 } |
94 HwProtocolMessage::Proto(proto) => { |
95 HwProtocolMessage::Proto(proto) => { |
95 let client = &mut server.anteroom.clients[client_id]; |
96 let client = &mut server_state.anteroom.clients[client_id]; |
96 if client.protocol_number.is_some() { |
97 if client.protocol_number.is_some() { |
97 response.add(Error("Protocol already known.".to_string()).send_self()); |
98 response.add(Error("Protocol already known.".to_string()).send_self()); |
98 LoginResult::Unchanged |
99 LoginResult::Unchanged |
99 } else if proto == 0 { |
100 } else if proto == 0 { |
100 response.add(Error("Bad number.".to_string()).send_self()); |
101 response.add(Error("Bad number.".to_string()).send_self()); |
102 } else { |
103 } else { |
103 client.protocol_number = NonZeroU16::new(proto); |
104 client.protocol_number = NonZeroU16::new(proto); |
104 response.add(Proto(proto).send_self()); |
105 response.add(Proto(proto).send_self()); |
105 |
106 |
106 if client.nick.is_some() { |
107 if client.nick.is_some() { |
107 completion_result(server.clients.iter(), client, response) |
108 completion_result(server_state.server.iter_clients(), client, response) |
108 } else { |
109 } else { |
109 LoginResult::Unchanged |
110 LoginResult::Unchanged |
110 } |
111 } |
111 } |
112 } |
112 } |
113 } |
113 #[cfg(feature = "official-server")] |
114 #[cfg(feature = "official-server")] |
114 HwProtocolMessage::Password(hash, salt) => { |
115 HwProtocolMessage::Password(hash, salt) => { |
115 let client = &server.anteroom.clients[client_id]; |
116 let client = &server_state.anteroom.clients[client_id]; |
116 |
117 |
117 if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) { |
118 if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) { |
118 response.request_io(super::IoTask::GetAccount { |
119 response.request_io(super::IoTask::GetAccount { |
119 nick: nick.clone(), |
120 nick: nick.clone(), |
120 protocol: protocol.get(), |
121 protocol: protocol.get(), |
126 |
127 |
127 LoginResult::Unchanged |
128 LoginResult::Unchanged |
128 } |
129 } |
129 #[cfg(feature = "official-server")] |
130 #[cfg(feature = "official-server")] |
130 HwProtocolMessage::Checker(protocol, nick, password) => { |
131 HwProtocolMessage::Checker(protocol, nick, password) => { |
131 let client = &mut server.anteroom.clients[client_id]; |
132 let client = &mut server_state.anteroom.clients[client_id]; |
132 if protocol == 0 { |
133 if protocol == 0 { |
133 response.add(Error("Bad number.".to_string()).send_self()); |
134 response.add(Error("Bad number.".to_string()).send_self()); |
134 LoginResult::Unchanged |
135 LoginResult::Unchanged |
135 } else { |
136 } else { |
136 client.protocol_number = NonZeroU16::new(protocol); |
137 client.protocol_number = NonZeroU16::new(protocol); |