aboutsummaryrefslogtreecommitdiffstats
path: root/src/proto/message.rs
blob: dbe4a694229dc8c32cafd1e184125f379c194ff2 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::collections::HashMap;
use std::fmt;

#[derive(Debug, Clone, PartialEq)]
pub struct IrcMessage {
    pub tags: HashMap<String, Option<String>>,
    pub prefix: Option<Prefix>,
    pub command: Command,
    pub params: Vec<String>,
}

impl IrcMessage {
    pub fn trailing(&self) -> Option<&str> {
        self.params.last().map(|s| s.as_str())
    }

    pub fn new(command: Command, params: Vec<String>) -> Self {
        Self {
            tags: HashMap::new(),
            prefix: None,
            command,
            params,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Prefix {
    Server(String),
    User {
        nick: String,
        user: Option<String>,
        host: Option<String>,
    },
}

impl fmt::Display for Prefix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Prefix::Server(s) => write!(f, "{}", s),
            Prefix::User { nick, user, host } => {
                write!(f, "{}", nick)?;
                if let Some(u) = user {
                    write!(f, "!{}", u)?;
                }
                if let Some(h) = host {
                    write!(f, "@{}", h)?;
                }
                Ok(())
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Command {
    // connection
    Cap,
    Nick,
    User,
    Pass,
    Quit,
    Ping,
    Pong,

    // channel operations
    Join,
    Part,
    Kick,
    Topic,
    Names,
    List,
    Invite,

    // messaging
    Privmsg,
    Notice,

    // mode & status
    Mode,
    Who,
    Whois,
    Whowas,

    // Server
    Oper,
    Kill,
    Rehash,

    // Numeric (001-999)
    Numeric(u16),

    Other(String),
}

impl Command {
    pub fn from_str(s: &str) -> Self {
        if s.len() == 3 && s.chars().all(|c| c.is_ascii_digit()) {
            if let Ok(n) = s.parse::<u16>() {
                return Command::Numeric(n);
            }
        }

        match s.to_ascii_uppercase().as_str() {
            "CAP" => Command::Cap,
            "NICK" => Command::Nick,
            "USER" => Command::User,
            "PASS" => Command::Pass,
            "QUIT" => Command::Quit,
            "PING" => Command::Ping,
            "PONG" => Command::Pong,
            "JOIN" => Command::Join,
            "PART" => Command::Part,
            "KICK" => Command::Kick,
            "TOPIC" => Command::Topic,
            "NAMES" => Command::Names,
            "LIST" => Command::List,
            "INVITE" => Command::Invite,
            "PRIVMSG" => Command::Privmsg,
            "NOTICE" => Command::Notice,
            "MODE" => Command::Mode,
            "WHO" => Command::Who,
            "WHOIS" => Command::Whois,
            "WHOWAS" => Command::Whowas,
            "OPER" => Command::Oper,
            "KILL" => Command::Kill,
            "REHASH" => Command::Rehash,
            other => Command::Other(other.to_string()),
        }
    }
}

impl fmt::Display for Command {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Command::Cap => write!(f, "CAP"),
            Command::Nick => write!(f, "NICK"),
            Command::User => write!(f, "USER"),
            Command::Pass => write!(f, "PASS"),
            Command::Quit => write!(f, "QUIT"),
            Command::Ping => write!(f, "PING"),
            Command::Pong => write!(f, "PONG"),
            Command::Join => write!(f, "JOIN"),
            Command::Part => write!(f, "PART"),
            Command::Kick => write!(f, "KICK"),
            Command::Topic => write!(f, "TOPIC"),
            Command::Names => write!(f, "NAMES"),
            Command::List => write!(f, "LIST"),
            Command::Invite => write!(f, "INVITE"),
            Command::Privmsg => write!(f, "PRIVMSG"),
            Command::Notice => write!(f, "NOTICE"),
            Command::Mode => write!(f, "MODE"),
            Command::Who => write!(f, "WHO"),
            Command::Whois => write!(f, "WHOIS"),
            Command::Whowas => write!(f, "WHOWAS"),
            Command::Oper => write!(f, "OPER"),
            Command::Kill => write!(f, "KILL"),
            Command::Rehash => write!(f, "REHASH"),
            Command::Numeric(n) => write!(f, "{:03}", n),
            Command::Other(s) => write!(f, "{}", s),
        }
    }
}