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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use std::collections::BTreeMap;
use std::fmt;
use std::fmt::Write;
use std::option::Option;
use std::str::FromStr;

use super::error::Error;

use crate::escaped::{escape_char, unescape_char};

/// Representation of a parsed message source. This is in the format
/// nick [ [ "!" user ] "@" host ].
///
/// ```
/// let prefix = "nick!user@host".parse::<simple_irc::Prefix>().unwrap();
/// assert_eq!(&prefix.nick[..], "nick");
/// assert_eq!(prefix.user.as_deref(), Some("user"));
/// assert_eq!(prefix.host.as_deref(), Some("host"));
/// ```
#[derive(Debug, PartialEq, Default, Clone)]
pub struct Prefix {
    pub nick: String,
    pub user: Option<String>,
    pub host: Option<String>,
}

impl Prefix {
    pub fn new(nick: &str) -> Self {
        Self::new_with_all(nick, None, None)
    }

    pub fn new_with_all(nick: &str, user: Option<&str>, host: Option<&str>) -> Self {
        Prefix {
            nick: nick.to_string(),
            user: user.map(|s| s.to_string()),
            host: host.map(|s| s.to_string()),
        }
    }
}

impl FromStr for Prefix {
    type Err = Error;

    // nickname [ [ "!" user ] "@" host ]
    fn from_str(input: &str) -> Result<Self, Self::Err> {
        let mut parts = input.splitn(2, '@');

        // Split on host first
        let rest = parts.next().unwrap_or("");
        let host = parts.next();

        let mut parts = rest.splitn(2, '!');
        let nick = parts.next().unwrap_or("").to_string();
        let user = parts.next();

        Ok(Prefix {
            nick,
            user: user.and_then(|s| if s == "" { None } else { Some(s.to_string()) }),
            host: host.and_then(|s| if s == "" { None } else { Some(s.to_string()) }),
        })
    }
}

impl fmt::Display for Prefix {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.nick)?;

        if let Some(user) = self.user.as_ref() {
            f.write_char('!')?;
            f.write_str(&user[..])?;
        }

        if let Some(host) = self.host.as_ref() {
            f.write_char('@')?;
            f.write_str(&host[..])?;
        }

        Ok(())
    }
}

/// A structural representation of an IRC message.
///
/// Note that this library does not guarantee that messages will be preserved
/// byte-for-byte, but all messages will have the same semantic meaning.
///
/// ```
/// // use std::convert::FromStr;
/// let msg = "HELLO world".parse::<simple_irc::Message>().unwrap();
/// assert_eq!(msg.tags.len(), 0);
/// assert_eq!(msg.prefix, None);
/// assert_eq!(&msg.command[..], "HELLO");
/// assert_eq!(msg.params.len(), 1);
/// assert_eq!(&msg.params[0][..], "world");
///
/// assert_eq!(&msg.to_string(), "HELLO :world");
/// ```
#[derive(Debug, PartialEq, Default, Clone)]
pub struct Message {
    pub tags: BTreeMap<String, String>,
    pub prefix: Option<Prefix>,
    pub command: String,
    pub params: Vec<String>,
}

impl Message {
    pub fn new(command: String, params: Vec<String>) -> Self {
        Message {
            command,
            params,
            ..Default::default()
        }
    }

    pub fn new_with_all(
        tags: BTreeMap<String, String>,
        prefix: Option<Prefix>,
        command: String,
        params: Vec<String>,
    ) -> Self {
        Message {
            tags,
            prefix,
            command,
            params,
        }
    }

    pub fn new_with_prefix(command: String, params: Vec<String>, prefix: Prefix) -> Self {
        Message {
            prefix: Some(prefix),
            command,
            params,
            ..Default::default()
        }
    }
}

fn parse_tags(input: &str) -> Result<BTreeMap<String, String>, Error> {
    let mut tags = BTreeMap::new();

    for tag_data in input.split(';') {
        let mut pieces = tag_data.splitn(2, '=');
        let tag_name = pieces
            .next()
            .ok_or_else(|| Error::TagError("missing tag name".to_string()))?;
        let raw_tag_value = pieces.next().unwrap_or("");

        let mut tag_value = String::new();
        let mut tag_value_chars = raw_tag_value.chars();
        while let Some(c) = tag_value_chars.next() {
            if c == '\\' {
                if let Some(escaped_char) = tag_value_chars.next() {
                    tag_value.push(unescape_char(escaped_char));
                }
            } else {
                tag_value.push(c);
            }
        }

        tags.insert(tag_name.to_string(), tag_value);
    }

    Ok(tags)
}

impl FromStr for Message {
    type Err = Error;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        // We want a mutable input so we can jump through it as we parse the
        // message. Note that this shadows the input param on purpose so it
        // cannot accidentally be used later.
        let mut input = input;

        // Possibly chop off the ending \r\n where either of those characters is
        // optional.
        if input.ends_with('\n') {
            input = &input[..input.len() - 1];
        }
        if input.ends_with('\r') {
            input = &input[..input.len() - 1];
        }

        let mut tags = BTreeMap::new();
        let mut prefix = None;

        if input.starts_with('@') {
            let mut parts = (&input[1..]).splitn(2, ' ');
            let tag_data = parts
                .next()
                .ok_or_else(|| Error::TagError("missing tag data".to_string()))?;

            tags = parse_tags(tag_data)?;

            // Either advance to the next token, or return an empty string.
            input = parts.next().unwrap_or("").trim_start_matches(' ');
        }

        if input.starts_with(':') {
            let mut parts = (&input[1..]).splitn(2, ' ');
            prefix = Some(
                parts
                    .next()
                    .ok_or_else(|| Error::TagError("missing prefix data".to_string()))?
                    .parse()
                    .or_else(|_| Err(Error::TagError("failed to parse prefix data".to_string())))?,
            );

            // Either advance to the next token, or return an empty string.
            input = parts.next().unwrap_or("").trim_start_matches(' ');
        }

        let mut parts = input.splitn(2, ' ');
        let command = parts
            .next()
            .ok_or_else(|| Error::CommandError("missing command".to_string()))?
            .to_string();

        // Either advance to the next token, or return an empty string.
        input = parts.next().unwrap_or("").trim_start_matches(' ');

        // Parse out the params
        let mut params = Vec::new();
        while !input.is_empty() {
            // Special case - if the param starts with a :, it's a trailing
            // param, so we need to include the rest of the input as the param.
            if input.starts_with(':') {
                params.push(input[1..].to_string());
                break;
            }

            let mut parts = input.splitn(2, ' ');
            if let Some(param) = parts.next() {
                params.push(param.to_string());
            }

            // Either advance to the next token, or return an empty string.
            input = parts.next().unwrap_or("").trim_start_matches(' ');
        }

        Ok(Message {
            tags,
            prefix,
            command,
            params,
        })
    }
}

impl fmt::Display for Message {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if !self.tags.is_empty() {
            f.write_char('@')?;

            for (i, (k, v)) in self.tags.iter().enumerate() {
                // We need to insert a separator for everything other than the
                // first value.
                if i != 0 {
                    f.write_char(';')?;
                }

                f.write_str(k)?;
                if v.is_empty() {
                    continue;
                }

                f.write_char('=')?;

                for c in v.chars() {
                    match escape_char(c) {
                        Some(escaped_str) => f.write_str(escaped_str)?,
                        None => f.write_char(c)?,
                    }
                }
            }

            f.write_char(' ')?;
        }

        if let Some(prefix) = &self.prefix {
            f.write_char(':')?;
            prefix.fmt(f)?;
            f.write_char(' ')?;
        }

        f.write_str(&self.command)?;

        if let Some((last, params)) = self.params.split_last() {
            for param in params {
                f.write_char(' ')?;
                f.write_str(param)?;
            }

            f.write_str(" :")?;
            f.write_str(last)?;
        }

        Ok(())
    }
}