pircolate 0.2.1

Parser and interface for IRCv3 messages.
Documentation
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use message::{Message, TagRange, PrefixRange};
use error;

use std::ops::Range;

type ParseResult<'input, T> = error::Result<(T, usize)>;

pub fn parse_message<M: Into<String>>(message: M) -> error::Result<Message> {
    let message = message.into();

    let (tags, prefix, command, args) = {
        let input = message.as_bytes();
        let (tags, position) = parse_tags(input)?;

        let tags_end = position;

        if tags_end > 512 {
            return Err(
                error::ErrorKind::InputTooLong("The tags length exceeded 512 bytes.".to_owned())
                    .into(),
            );
        }

        let (prefix, position) = parse_prefix(input, position)?;
        let (command, position) = parse_command(input, position)?;
        let (args, position) = parse_args(input, position)?;

        if (position - tags_end) > 510 {
            return Err(
                error::ErrorKind::InputTooLong("The message length exceeded 512 bytes.".to_owned())
                    .into(),
            );
        }

        (tags, prefix, command, args)
    };

    Ok(Message {
        message: message,
        tags: tags,
        prefix: prefix,
        command: command,
        arguments: args,
    })
}

fn move_next(value: usize, bound: usize) -> error::Result<usize> {
    let value = value + 1;

    if value >= bound {
        Err(error::ErrorKind::UnexpectedEndOfInput.into())
    } else {
        Ok(value)
    }
}

fn parse_tags(input: &[u8]) -> ParseResult<Option<Vec<TagRange>>> {
    if input.is_empty() {
        return Err(error::ErrorKind::UnexpectedEndOfInput.into());
    }

    if input[0] == b'@' {
        let mut tags: Vec<TagRange> = Vec::new();
        let mut position = 1; // We can skip the @.
        let len = input.len();

        loop {
            let key_start = position;
            while input[position] != b'=' && input[position] != b';' {
                if input[position] == b' ' {
                    return Err(error::ErrorKind::UnexpectedEndOfInput.into());
                }

                position = move_next(position, len)?;
            }

            let key_range = key_start..position;
            if input[position] == b'=' {
                position = move_next(position, len)?;
            }

            let value_start = position;
            while input[position] != b';' && input[position] != b' ' {
                position = move_next(position, len)?;
            }

            let value_range = if value_start == position {
                None
            } else {
                Some(value_start..position)
            };

            tags.push((key_range, value_range));

            if input[position] == b' ' {
                position = move_next(position, len)?;
                break;
            }

            position = move_next(position, len)?;
        }

        Ok((Some(tags), position))
    } else {
        Ok((None, 0))
    }
}

fn parse_prefix(input: &[u8], mut position: usize) -> ParseResult<Option<PrefixRange>> {
    let len = input.len();

    if position >= len {
        return Err(error::ErrorKind::UnexpectedEndOfInput.into());
    }

    if input[position] == b':' {
        position = move_next(position, len)?;
        let prefix_start = position;

        while input[position] != b' ' && input[position] != b'!' && input[position] != b'@' {
            position = move_next(position, len)?;
        }

        let prefix_range = prefix_start..position;

        let mut user_range = None;
        if input[position] == b'!' {
            position = move_next(position, len)?;
            let user_start = position;

            while input[position] != b' ' && input[position] != b'@' {
                position = move_next(position, len)?;
            }

            user_range = Some(user_start..position);
        }

        let mut host_range = None;
        if input[position] == b'@' {
            position = move_next(position, len)?;
            let host_start = position;

            while input[position] != b' ' {
                position = move_next(position, len)?;
            }

            host_range = Some(host_start..position);
        }

        let prefix_range = PrefixRange {
            raw_prefix: prefix_start..position,
            prefix: prefix_range,
            user: user_range,
            host: host_range,
        };

        position = move_next(position, len)?;

        Ok((Some(prefix_range), position))
    } else {
        Ok((None, position))
    }
}

fn parse_command(input: &[u8], mut position: usize) -> ParseResult<Range<usize>> {
    let len = input.len();
    if position >= len {
        return Err(error::ErrorKind::UnexpectedEndOfInput.into());
    }

    if input[0] == b' ' {
        position += 1
    }

    let command_start = position;

    while position < len && input[position] != b' ' {
        position += 1;
    }

    let command_range = command_start..position;

    if position < len && input[position] == b' ' {
        position = move_next(position, len)?;
    }

    Ok((command_range, position))
}

fn parse_args(input: &[u8], mut position: usize) -> ParseResult<Option<Vec<Range<usize>>>> {
    let len = input.len();

    if position >= len {
        return Ok((None, position));
    }

    let mut args = Vec::new();
    let mut arg_start = position;

    loop {
        if input[position] == b':' {
            position += 1;
            args.push(position..len);
            break;
        }

        if input[position] == b' ' {
            args.push(arg_start..position);

            arg_start = position + 1;
        }

        position += 1;

        if position >= len {
            args.push(arg_start..position);
            break;
        }
    }

    Ok((Some(args), position))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_command() {
        let result = parse_message("TEST").unwrap();

        assert_eq!(None, result.prefix());
        assert_eq!("TEST", result.raw_command());
    }

    #[test]
    fn parse_command_with_prefix() {
        let result = parse_message(":test.server.com TEST").unwrap();

        assert_eq!("test.server.com", result.raw_prefix().unwrap());
        assert_eq!("TEST", result.raw_command());
    }

    #[test]
    fn parse_command_with_argument_following_colon() {
        let result = parse_message("TEST :test.server.com").unwrap();

        let expected_args = vec!["test.server.com"];
        let actual_args: Vec<_> = result.raw_args().collect();

        assert_eq!("TEST", result.raw_command());
        assert_eq!(expected_args, actual_args);
    }

    #[test]
    fn parse_command_with_prefix_and_argument_following_colon() {
        let result = parse_message(":other.server.com TEST :test.server.com").unwrap();

        let expected_args = vec!["test.server.com"];
        let actual_args: Vec<_> = result.raw_args().collect();

        assert_eq!("other.server.com", result.raw_prefix().unwrap());
        assert_eq!("TEST", result.raw_command());
        assert_eq!(expected_args, actual_args);
    }

    #[test]
    fn parse_command_with_multiple_arguments() {
        let result = parse_message("TEST a b c").unwrap();

        let expected_args = vec!["a", "b", "c"];
        let actual_args: Vec<_> = result.raw_args().collect();

        assert_eq!("TEST", result.raw_command());
        assert_eq!(expected_args, actual_args);
    }

    #[test]
    fn parse_command_with_multiple_arguments_and_argument_following_colon() {
        let result = parse_message("TEST a b c :Memes for all!").unwrap();
        let expected_args = vec!["a", "b", "c", "Memes for all!"];
        let actual_args: Vec<_> = result.raw_args().collect();

        assert_eq!("TEST", result.raw_command());
        assert_eq!(expected_args, actual_args);
    }

    #[test]
    fn parse_command_with_multiple_tags() {
        let result = parse_message("@a=1;b=2;d=;f;a\\b=3;c= TEST").unwrap();

        let expected_tags = vec![
            ("a", Some("1")),
            ("b", Some("2")),
            ("d", None),
            ("f", None),
            ("a\\b", Some("3")),
            ("c", None),
        ];

        let actual_tags: Vec<_> = result.raw_tags().collect();

        assert_eq!("TEST", result.raw_command());
        assert_eq!(expected_tags, actual_tags);
    }

    #[test]
    fn parse_command_with_multibyte_character_arguments() {
        let result = parse_message("TEST :💖 Love 💖 Memes 💖").unwrap();

        let expected_args = vec!["💖 Love 💖 Memes 💖"];
        let actual_args: Vec<_> = result.raw_args().collect();

        assert_eq!(expected_args, actual_args);
    }

    #[test]
    fn parse_command_with_512_byte_long_tags() {
        let message = "@a=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 TEST";
        let result = parse_message(message).unwrap();

        let (key, value) = result.raw_tags().next().unwrap();

        assert_eq!("a", key);
        assert_eq!(508, value.unwrap().len());
        assert_eq!("TEST", result.raw_command());
    }

    #[test]
    fn parse_command_with_510_byte_long_command() {
        let message = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
        let result = parse_message(message).unwrap();

        assert_eq!(510, result.raw_command().len());
    }

    #[test]
    fn parse_command_with_512_byte_long_tags_and_510_byte_long_command() {
        let message = "@a=1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111";
        let result = parse_message(message).unwrap();

        let (key, value) = result.raw_tags().next().unwrap();

        assert_eq!("a", key);
        assert_eq!(508, value.unwrap().len());
        assert_eq!(510, result.raw_command().len());
    }

    #[test]
    fn parse_command_with_basic_prefix() {
        let result = parse_message(":foo TEST").unwrap();

        let prefix = result.prefix();

        assert_eq!(Some(("foo", None, None)), prefix);
    }

    #[test]
    fn parse_command_with_user_prefix() {
        let result = parse_message(":foo!foobert TEST").unwrap();

        let prefix = result.prefix();

        assert_eq!(Some(("foo", Some("foobert"), None)), prefix);
    }

    #[test]
    fn parse_command_with_user_prefix_and_host() {
        let result = parse_message(":foo!foobert@host.test.com TEST").unwrap();

        let prefix = result.prefix();

        assert_eq!(
            Some(("foo", Some("foobert"), Some("host.test.com"))),
            prefix
        );
    }

    #[test]
    fn parse_command_with_prefix_and_host() {
        let result = parse_message(":foo@host.test.com TEST").unwrap();

        let prefix = result.prefix();

        assert_eq!(Some(("foo", None, Some("host.test.com"))), prefix);
    }

    #[test]
    fn parse_numeric_welcome() {
        let result = parse_message(
            "001 fjtest :Welcome to the Meme Loving IRC Network \
             same@me.irl",
        ).unwrap();

        assert_eq!("001", result.raw_command());
        assert_eq!(
            vec![
                "fjtest",
                "Welcome to the Meme Loving IRC Network same@me.irl",
            ],
            result.raw_args().collect::<Vec<&str>>()
        );
    }
}