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
|
use crate::proto::error::ParseError;
use crate::proto::message::{Command, IrcMessage, Prefix};
use std::collections::HashMap;
pub fn parse(line: &str) -> Result<IrcMessage, ParseError> {
if line.is_empty() {
return Err(ParseError::EmptyMessage);
}
let mut rest = line;
// parse tags
let tags = if rest.starts_with('@') {
let (tag_str, remaining) = rest[1..]
.split_once(' ')
.ok_or(ParseError::MissingCommand)?;
rest = remaining;
parse_tags(tag_str)?
} else {
HashMap::new()
};
// parse prefix
let prefix = if rest.starts_with(':') {
let (prefix_str, remaining) = rest[1..]
.split_once(' ')
.ok_or(ParseError::MissingCommand)?;
rest = remaining;
Some(parse_prefix(prefix_str))
} else {
None
};
// parse command
let (command_str, rest) = match rest.split_once(' ') {
Some((cmd, params)) => (cmd, params),
None => (rest, ""),
};
if command_str.is_empty() {
return Err(ParseError::MissingCommand);
}
let command = Command::from_str(command_str);
// parse params
let params = parse_params(rest);
Ok(IrcMessage {
tags,
prefix,
command,
params,
})
}
fn parse_tags(tag_str: &str) -> Result<HashMap<String, Option<String>>, ParseError> {
let mut tags = HashMap::new();
for tag in tag_str.split(';') {
if tag.is_empty() {
continue;
}
match tag.split_once('=') {
Some((key, value)) => {
tags.insert(key.to_string(), Some(unescape_tag_value(value)));
}
None => {
// boolean tags
tags.insert(tag.to_string(), None);
}
}
}
Ok(tags)
}
fn unescape_tag_value(value: &str) -> String {
let mut result = String::with_capacity(value.len());
let mut chars = value.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\\' {
match chars.next() {
Some(':') => result.push(';'),
Some('s') => result.push(' '),
Some('\\') => result.push('\\'),
Some('r') => result.push('\r'),
Some('n') => result.push('\n'),
Some(c) => {
result.push('\\');
result.push(c);
}
None => result.push('\\'),
}
} else {
result.push(ch);
}
}
result
}
fn parse_prefix(prefix: &str) -> Prefix {
if prefix.contains('!') || prefix.contains('@') {
let (nick, rest) = prefix
.split_once('!')
.map(|(n, r)| (n, Some(r)))
.unwrap_or((prefix, None));
let (user, host) = match rest {
Some(r) => r
.split_once('@')
.map(|(u, h)| (Some(u.to_string()), Some(h.to_string())))
.unwrap_or((Some(r.to_string()), None)),
None => {
// Could be nick@host with no user
if let Some((n2, h)) = nick.split_once('@') {
return Prefix::User {
nick: n2.to_string(),
user: None,
host: Some(h.to_string()),
};
}
(None, None)
}
};
Prefix::User {
nick: nick.to_string(),
user,
host,
}
} else {
// Heuristic: if it contains a dot, it's likely a server name
// (nick-only prefixes are also possible but rare without user/host)
Prefix::Server(prefix.to_string())
}
}
fn parse_params(params_str: &str) -> Vec<String> {
let mut params = Vec::new();
let mut rest = params_str;
loop {
rest = rest.trim_start_matches(' ');
if rest.is_empty() {
break;
}
if rest.starts_with(':') {
params.push(rest[1..].to_string());
break;
}
match rest.split_once(' ') {
Some((param, remaining)) => {
params.push(param.to_string());
rest = remaining;
}
None => {
params.push(rest.to_string());
break;
}
}
}
params
}
|