aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/state.rs
blob: b98750935912698996f2a2a5e7d8baaec3ad539d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::collections::{HashMap, HashSet};

/// The full state of a connected IRC client.
#[derive(Debug, Default)]
pub struct ClientState {
    pub nick: String,
    pub channels: HashMap<String, Channel>,
    pub caps: HashSet<String>,
    pub server_name: Option<String>,
    pub reg: RegistrationState,
}

impl ClientState {
    pub fn new(nick: impl Into<String>) -> Self {
        Self {
            nick: nick.into(),
            ..Default::default()
        }
    }

    pub fn channel(&self, name: &str) -> Option<&Channel> {
        self.channels.get(&name.to_lowercase())
    }

    pub fn channel_mut(&mut self, name: &str) -> &mut Channel {
        self.channels
            .entry(name.to_lowercase())
            .or_insert_with(|| Channel::new(name))
    }

    pub fn remove_channel(&mut self, name: &str) {
        self.channels.remove(&name.to_lowercase());
    }
}

/// State of the registration handshake.
#[derive(Debug, Default, PartialEq, Eq)]
pub enum RegistrationState {
    #[default]
    CapNegotiation,
    CapPending,
    WaitingForWelcome,
    Registered,
}

/// A joined channel and its current state.
#[derive(Debug)]
pub struct Channel {
    pub name: String,
    pub members: HashSet<String>,
    pub topic: Option<String>,
}

impl Channel {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            members: HashSet::new(),
            topic: None,
        }
    }
}