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
mod parser;
use error::Result;
use command::{Command, ArgumentIter};
use tag::{Tag, TagIter};
use std::ops::Range;
#[derive(Clone, PartialEq, Eq)]
struct PrefixRange {
raw_prefix: Range<usize>,
prefix: Range<usize>,
user: Option<Range<usize>>,
host: Option<Range<usize>>
}
type TagRange = (Range<usize>, Option<Range<usize>>);
#[derive(Clone, PartialEq, Eq)]
pub struct Message {
message: String,
tags: Option<Vec<TagRange>>,
prefix: Option<PrefixRange>,
command: Range<usize>,
arguments: Option<Vec<Range<usize>>>,
}
impl Message {
pub fn try_from(value: String) -> Result<Message> {
let result = parser::parse_message(value)?;
Ok(result)
}
pub fn command<'a, T>(&'a self) -> Option<T> where T : Command<'a> {
<T as Command>::try_match(self.raw_command(), self.raw_args())
}
pub fn tag<'a, T>(&'a self) -> Option<T> where T : Tag<'a> {
<T as Tag>::try_match(self.raw_tags())
}
pub fn prefix(&self) -> Option<(&str, Option<&str>, Option<&str>)> {
if let Some(ref prefix_range) = self.prefix {
let user = prefix_range.user.clone().map(|user| &self.message[user]);
let host = prefix_range.host.clone().map(|host| &self.message[host]);
Some((&self.message[prefix_range.prefix.clone()], user, host))
} else {
None
}
}
pub fn raw_tags(&self) -> TagIter {
if let Some(ref tags) = self.tags {
TagIter::new(&self.message, tags.iter())
} else {
TagIter::new(&self.message, [].iter())
}
}
pub fn raw_prefix(&self) -> Option<&str> {
if let Some(ref prefix_range) = self.prefix {
Some(&self.message[prefix_range.raw_prefix.clone()])
} else {
None
}
}
pub fn raw_command(&self) -> &str {
&self.message[self.command.clone()]
}
pub fn raw_args(&self) -> ArgumentIter {
if let Some(ref arguments) = self.arguments {
ArgumentIter::new(&self.message, arguments.iter())
} else {
ArgumentIter::new(&self.message, [].iter())
}
}
pub fn raw_message(&self) -> &str {
&self.message
}
}
pub fn ping<H: Into<String>>(host: H) -> Result<Message> {
Message::try_from(format!("PING :{}", host.into()))
}
pub fn pong<H: Into<String>>(host: H) -> Result<Message> {
Message::try_from(format!("PONG {}", host.into()))
}
pub fn pass<P: Into<String>>(pass: P) -> Result<Message> {
Message::try_from(format!("PASS {}", pass.into()))
}
pub fn nick<N: Into<String>>(nick: N) -> Result<Message> {
Message::try_from(format!("NICK {}", nick.into()))
}
pub fn user<U: Into<String>, N: Into<String>>(username: U, real_name: N) -> Result<Message> {
Message::try_from(format!("USER {} 0 * :{}", username.into(), real_name.into()))
}
pub fn cap_req<C: Into<String>>(cap: C) -> Result<Message> {
Message::try_from(format!("CAP REQ :{}", cap.into()))
}
pub fn join<C: Into<String>>(channel: C) -> Result<Message> {
Message::try_from(format!("JOIN {}", channel.into()))
}
pub fn privmsg<T: Into<String>, M: Into<String>>(targets: T, message: M) -> Result<Message> {
Message::try_from(format!("PRIVMSG {} :{}", targets.into(), message.into()))
}