rust/hedgewars-server/src/handlers/inanteroom.rs
author alfadur
Wed, 27 Mar 2024 02:19:44 +0300
changeset 16030 8ba2b5007c29
parent 16019 c40f5e27aaf0
permissions -rw-r--r--
fix leaving rooms
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
     1
use super::strings::*;
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15562
diff changeset
     2
use crate::handlers::actions::ToPendingMessage;
13666
09f4a30e50cc Rust 2018 conversion
alfadur
parents: 13450
diff changeset
     3
use crate::{
15095
c5a6e8566425 shuffle server files
alfadur
parents: 14891
diff changeset
     4
    core::{
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
     5
        anteroom::{HwAnteroom, HwAnteroomClient},
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     6
        client::HwClient,
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
     7
        server::HwServer,
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
     8
        types::ClientId,
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
     9
    },
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    10
    utils::is_name_illegal,
13419
81e0ed105f5d implementation of team related messages
alfadur
parents: 13416
diff changeset
    11
};
15826
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15562
diff changeset
    12
use hedgewars_network_protocol::messages::{
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15562
diff changeset
    13
    HwProtocolMessage, HwProtocolMessage::LoadRoom, HwServerMessage::*,
747278149393 Extract network protocol into a separate crate
unc0rr
parents: 15562
diff changeset
    14
};
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    15
use log::*;
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    16
use std::{
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    17
    fmt::{Formatter, LowerHex},
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    18
    num::NonZeroU16,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    19
};
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
    20
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    21
pub enum LoginResult {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    22
    Unchanged,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    23
    Complete,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    24
    Exit,
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    25
}
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    26
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    27
fn completion_result<'a, I>(
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    28
    mut other_clients: I,
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    29
    client: &mut HwAnteroomClient,
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    30
    response: &mut super::Response,
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    31
) -> LoginResult
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    32
where
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    33
    I: Iterator<Item = &'a HwClient>,
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    34
{
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    35
    let has_nick_clash = other_clients.any(|c| c.nick == *client.nick.as_ref().unwrap());
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
    36
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    37
    if has_nick_clash {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    38
        client.nick = None;
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    39
        response.add(Notice("NickAlreadyInUse".to_string()).send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    40
        LoginResult::Unchanged
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    41
    } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    42
        #[cfg(feature = "official-server")]
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    43
        {
15124
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    44
            response.request_io(super::IoTask::CheckRegistered {
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    45
                nick: client.nick.as_ref().unwrap().clone(),
823052e66611 check for account existence before asking passwords
alfadur
parents: 15096
diff changeset
    46
            });
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    47
            LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    48
        }
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    49
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    50
        #[cfg(not(feature = "official-server"))]
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    51
        {
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    52
            LoginResult::Complete
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    53
        }
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
    54
    }
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
    55
}
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
    56
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    57
pub fn handle(
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    58
    server_state: &mut super::ServerState,
14692
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    59
    client_id: ClientId,
455865ccd36c Server action refactoring part 2 of N
alfadur <mail@none>
parents: 14478
diff changeset
    60
    response: &mut super::Response,
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    61
    message: HwProtocolMessage,
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    62
) -> LoginResult {
16019
c40f5e27aaf0 fix connection errors carrying over to new clients
alfadur
parents: 15967
diff changeset
    63
    //todo!("Handle parsing of empty nicks")
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
    64
    match message {
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    65
        HwProtocolMessage::Quit(_) => {
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    66
            response.add(Bye("User quit".to_string()).send_self());
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    67
            LoginResult::Exit
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    68
        }
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    69
        HwProtocolMessage::Nick(nick) => {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    70
            let client = &mut server_state.anteroom.clients[client_id];
14891
ce98c37826a7 fix accepting on wrong server socket
alfadur
parents: 14802
diff changeset
    71
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
    72
            if client.nick.is_some() {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    73
                response.error(NICKNAME_PROVIDED);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    74
                LoginResult::Unchanged
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    75
            } else if is_name_illegal(&nick) {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    76
                response.add(Bye(ILLEGAL_CLIENT_NAME.to_string()).send_self());
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    77
                LoginResult::Exit
14478
98ef2913ec73 Apply rustfmt to all files
unc0rr
parents: 14436
diff changeset
    78
            } else {
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    79
                client.nick = Some(nick.clone());
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
    80
                response.add(Nick(nick).send_self());
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    81
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    82
                if client.protocol_number.is_some() {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    83
                    completion_result(server_state.server.iter_clients(), client, response)
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    84
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    85
                    LoginResult::Unchanged
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    86
                }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    87
            }
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    88
        }
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
    89
        HwProtocolMessage::Proto(proto) => {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
    90
            let client = &mut server_state.anteroom.clients[client_id];
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    91
            if client.protocol_number.is_some() {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    92
                response.error(PROTOCOL_PROVIDED);
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    93
                LoginResult::Unchanged
15562
479911540e17 backout a798e6441a36
alfadur <mail@none>
parents: 15558
diff changeset
    94
            } else if proto < 48 {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    95
                response.add(Bye(PROTOCOL_TOO_OLD.to_string()).send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
    96
                LoginResult::Exit
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    97
            } else {
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
    98
                client.protocol_number = NonZeroU16::new(proto);
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
    99
                response.add(Proto(proto).send_self());
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   100
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   101
                if client.nick.is_some() {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
   102
                    completion_result(server_state.server.iter_clients(), client, response)
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   103
                } else {
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   104
                    LoginResult::Unchanged
14694
08a8605bafaf Server action refactoring part 4 of N
alfadur <mail@none>
parents: 14693
diff changeset
   105
                }
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   106
            }
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   107
        }
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   108
        #[cfg(feature = "official-server")]
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   109
        HwProtocolMessage::Password(hash, salt) => {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
   110
            let client = &server_state.anteroom.clients[client_id];
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
   111
14800
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   112
            if let (Some(nick), Some(protocol)) = (client.nick.as_ref(), client.protocol_number) {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   113
                response.request_io(super::IoTask::GetAccount {
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   114
                    nick: nick.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   115
                    protocol: protocol.get(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   116
                    server_salt: client.server_salt.clone(),
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   117
                    client_salt: salt,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   118
                    password_hash: hash,
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   119
                });
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   120
            };
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   121
f43ab2bd76ae add a thread for internal server IO and implement account checking with it
alfadur
parents: 14717
diff changeset
   122
            LoginResult::Unchanged
14693
6e6632068a33 Server action refactoring part 3 of N
alfadur <mail@none>
parents: 14692
diff changeset
   123
        }
13774
0118b7412570 use openssl for password message handling
alfadur
parents: 13771
diff changeset
   124
        #[cfg(feature = "official-server")]
15096
e935b1ad23f3 normalize type names
alfadur
parents: 15095
diff changeset
   125
        HwProtocolMessage::Checker(protocol, nick, password) => {
15542
fd3a20e9d095 move the anteroom out of the server
alfadur <mail@none>
parents: 15124
diff changeset
   126
            let client = &mut server_state.anteroom.clients[client_id];
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   127
            if protocol == 0 {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   128
                response.error("Bad number.");
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   129
                LoginResult::Unchanged
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   130
            } else {
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   131
                client.protocol_number = NonZeroU16::new(protocol);
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   132
                client.is_checker = true;
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   133
                #[cfg(not(feature = "official-server"))]
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   134
                {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   135
                    response.request_io(super::IoTask::GetCheckerAccount {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   136
                        nick: nick,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   137
                        password: password,
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   138
                    });
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   139
                    LoginResult::Unchanged
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   140
                }
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   141
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   142
                #[cfg(feature = "official-server")]
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   143
                {
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   144
                    response.add(LogonPassed.send_self());
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   145
                    LoginResult::Complete
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   146
                }
14802
01f8ab45f806 fix lobby joining
alfadur
parents: 14800
diff changeset
   147
            }
13771
4664da990556 Add official server feature to cargo
alfadur
parents: 13666
diff changeset
   148
        }
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   149
        _ => {
15554
f1205f33bf5b complete checker login handling
alfadur <mail@none>
parents: 15542
diff changeset
   150
            warn!("Incorrect command in anteroom");
14714
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   151
            LoginResult::Unchanged
6a2e13e36b7f add server anteroom
alfadur <mail@none>
parents: 14711
diff changeset
   152
        }
12147
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   153
    }
03ccb89820f3 Room creation halfplemented
unc0rr
parents:
diff changeset
   154
}