rust/hedgewars-server/src/handlers/inanteroom.rs
changeset 15096 e935b1ad23f3
parent 15095 c5a6e8566425
child 15124 823052e66611
equal deleted inserted replaced
15095:c5a6e8566425 15096:e935b1ad23f3
     1 use mio;
     1 use mio;
     2 
     2 
     3 use crate::{
     3 use crate::{
     4     protocol::messages::{
       
     5         HWProtocolMessage::LoadRoom,
       
     6         HWProtocolMessage,
       
     7         HWServerMessage::*},
       
     8     core::{
     4     core::{
     9         client::HWClient,
     5         client::HwClient,
    10         server::{HWServer, HWAnteClient, HWAnteroom},
     6         server::{HwAnteClient, HwAnteroom, HwServer},
    11         types::ClientId
     7         types::ClientId,
    12     },
     8     },
    13     utils::is_name_illegal
     9     protocol::messages::{HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*},
       
    10     utils::is_name_illegal,
    14 };
    11 };
    15 
    12 
    16 use log::*;
    13 use log::*;
    17 #[cfg(feature = "official-server")]
    14 #[cfg(feature = "official-server")]
    18 use openssl::sha::sha1;
    15 use openssl::sha::sha1;
    27     Exit,
    24     Exit,
    28 }
    25 }
    29 
    26 
    30 fn completion_result<'a, I>(
    27 fn completion_result<'a, I>(
    31     mut other_clients: I,
    28     mut other_clients: I,
    32     client: &mut HWAnteClient,
    29     client: &mut HwAnteClient,
    33     response: &mut super::Response,
    30     response: &mut super::Response,
    34 ) -> LoginResult
    31 ) -> LoginResult
    35 where
    32 where
    36     I: Iterator<Item = (ClientId, &'a HWClient)>,
    33     I: Iterator<Item = (ClientId, &'a HwClient)>,
    37 {
    34 {
    38     let has_nick_clash =
    35     let has_nick_clash =
    39         other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap());
    36         other_clients.any(|(_, c)| !c.is_checker() && c.nick == *client.nick.as_ref().unwrap());
    40 
    37 
    41     if has_nick_clash {
    38     if has_nick_clash {
    60         }
    57         }
    61     }
    58     }
    62 }
    59 }
    63 
    60 
    64 pub fn handle(
    61 pub fn handle(
    65     server: &mut HWServer,
    62     server: &mut HwServer,
    66     client_id: ClientId,
    63     client_id: ClientId,
    67     response: &mut super::Response,
    64     response: &mut super::Response,
    68     message: HWProtocolMessage,
    65     message: HwProtocolMessage,
    69 ) -> LoginResult {
    66 ) -> LoginResult {
    70     match message {
    67     match message {
    71         HWProtocolMessage::Quit(_) => {
    68         HwProtocolMessage::Quit(_) => {
    72             response.add(Bye("User quit".to_string()).send_self());
    69             response.add(Bye("User quit".to_string()).send_self());
    73             LoginResult::Exit
    70             LoginResult::Exit
    74         }
    71         }
    75         HWProtocolMessage::Nick(nick) => {
    72         HwProtocolMessage::Nick(nick) => {
    76             let client = &mut server.anteroom.clients[client_id];
    73             let client = &mut server.anteroom.clients[client_id];
    77 
    74 
    78             if client.nick.is_some() {
    75             if client.nick.is_some() {
    79                 response.add(Error("Nickname already provided.".to_string()).send_self());
    76                 response.add(Error("Nickname already provided.".to_string()).send_self());
    80                 LoginResult::Unchanged
    77                 LoginResult::Unchanged
    90                 } else {
    87                 } else {
    91                     LoginResult::Unchanged
    88                     LoginResult::Unchanged
    92                 }
    89                 }
    93             }
    90             }
    94         }
    91         }
    95         HWProtocolMessage::Proto(proto) => {
    92         HwProtocolMessage::Proto(proto) => {
    96             let client = &mut server.anteroom.clients[client_id];
    93             let client = &mut server.anteroom.clients[client_id];
    97             if client.protocol_number.is_some() {
    94             if client.protocol_number.is_some() {
    98                 response.add(Error("Protocol already known.".to_string()).send_self());
    95                 response.add(Error("Protocol already known.".to_string()).send_self());
    99                 LoginResult::Unchanged
    96                 LoginResult::Unchanged
   100             } else if proto == 0 {
    97             } else if proto == 0 {
   110                     LoginResult::Unchanged
   107                     LoginResult::Unchanged
   111                 }
   108                 }
   112             }
   109             }
   113         }
   110         }
   114         #[cfg(feature = "official-server")]
   111         #[cfg(feature = "official-server")]
   115         HWProtocolMessage::Password(hash, salt) => {
   112         HwProtocolMessage::Password(hash, salt) => {
   116             let client = &server.anteroom.clients[client_id];
   113             let client = &server.anteroom.clients[client_id];
   117 
   114 
   118             if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) {
   115             if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) {
   119                 response.request_io(super::IoTask::GetAccount {
   116                 response.request_io(super::IoTask::GetAccount {
   120                     nick: nick.clone(),
   117                     nick: nick.clone(),
   126             };
   123             };
   127 
   124 
   128             LoginResult::Unchanged
   125             LoginResult::Unchanged
   129         }
   126         }
   130         #[cfg(feature = "official-server")]
   127         #[cfg(feature = "official-server")]
   131         HWProtocolMessage::Checker(protocol, nick, password) => {
   128         HwProtocolMessage::Checker(protocol, nick, password) => {
   132             let client = &mut server.anteroom.clients[client_id];
   129             let client = &mut server.anteroom.clients[client_id];
   133             if protocol == 0 {
   130             if protocol == 0 {
   134                 response.add(Error("Bad number.".to_string()).send_self());
   131                 response.add(Error("Bad number.".to_string()).send_self());
   135                 LoginResult::Unchanged
   132                 LoginResult::Unchanged
   136             } else {
   133             } else {