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
|
use std::sync::LazyLock;
use regex::{Regex, RegexBuilder};
use crate::config::System;
use twilight_mention::ParseMention;
use twilight_model::id::{marker::UserMarker, Id};
use super::{FullMessage, MemberId, MessageId, Timestamp, UserId};
pub enum ParsedMessage {
Command(Command),
SetProxyAndDelete(MemberId),
ProxiedMessage {
member_id: MemberId,
message_content: String,
latch: bool,
},
UnproxiedMessage(Option<String>),
LatchClear(MemberId),
// TODO: Figure out how to represent emotes
EmoteAdd(MemberId, MessageId, ()),
EmoteRemove(MemberId, MessageId, ()),
}
pub enum Command {
Edit(MemberId, MessageId, String),
Reproxy(MemberId, MessageId),
Delete(MessageId),
Nick(MemberId, String),
Log(String),
ReloadSystemConfig,
ExitSéance,
UnknownCommand,
InvalidCommand
}
pub struct MessageParser {}
static CORRECTION_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^\*\B+$").unwrap()
});
impl MessageParser {
pub fn parse(message: &FullMessage, secondary_message: Option<&FullMessage>, system_config: &System, latch_state: Option<(MemberId, Timestamp)>) -> ParsedMessage {
if message.content == r"\\" {
return ParsedMessage::LatchClear(if let Some((member_id, _)) = latch_state {
member_id
} else {
0
})
}
if message.content.starts_with(r"\") {
return ParsedMessage::UnproxiedMessage(None)
}
if message.content.starts_with(r"!") {
if let Some(parse) = MessageParser::check_command(message, secondary_message, system_config, latch_state) {
return ParsedMessage::Command(parse);
} else {
return ParsedMessage::UnproxiedMessage(Some(format!("Unknown command string: {}", message.content)));
}
}
if CORRECTION_REGEX.is_match(message.content.as_str()) {
if let Some(parse) = MessageParser::check_correction(message, secondary_message) {
return parse
}
}
if let Some(parse) = MessageParser::check_member_patterns(message, secondary_message, system_config) {
return parse
}
if let Some(parse) = MessageParser::check_autoproxy(message, latch_state) {
return parse
}
// If nothing else
ParsedMessage::UnproxiedMessage(None)
}
fn check_command(message: &FullMessage, secondary_message: Option<&FullMessage>, system_config: &System, latch_state: Option<(MemberId, Timestamp)>) -> Option<Command> {
let mut words = message.content.strip_prefix("!").unwrap().split_whitespace();
let first_word = words.next();
match first_word {
None => return None,
Some(command_name) => match command_name {
"log" => {
let remainder = words.remainder().unwrap().to_string();
return Some(Command::Log(remainder));
},
"edit" => {
let editing_member = Self::get_member_id_from_user_id(secondary_message.as_ref().unwrap().author.id, system_config).unwrap();
return Some(Command::Edit(editing_member, secondary_message.unwrap().id, words.remainder().unwrap().to_string()));
},
"nick" => {
if let Some(member) = MessageParser::match_member(words.next(), system_config) {
return Some(Command::Nick(member, words.remainder().unwrap().to_string()));
}
},
"reproxy" => {
if let Some(member) = MessageParser::match_member(words.next(), system_config) {
return Some(Command::Reproxy(member, secondary_message.unwrap().id));
}
},
"delete" => {
return Some(Command::Delete(secondary_message.unwrap().id));
}
_ => (),
},
}
// Attempt matching !s
if message.content.chars().nth(1).unwrap() == 's' {
let separator = message.content.chars().nth(2).unwrap();
let parts: Vec<&str> = message.content.split(separator).collect();
if parts.len() != 3 && parts.len() != 4 {
return None
}
let pattern = parts.get(1).unwrap();
let replacement = parts.get(2).unwrap();
let flags = parts.get(3).unwrap_or(&"");
let mut global = false;
let mut regex = RegexBuilder::new(pattern);
for flag in flags.chars() {match flag {
'i' => {regex.case_insensitive(true);},
'm' => {regex.multi_line(true);},
'g' => {global = true;},
'x' => {regex.ignore_whitespace(true);},
'R' => {regex.crlf(true);},
's' => {regex.dot_matches_new_line(true);},
'U' => {regex.swap_greed(true);},
_ => {return None;},
}};
let valid_regex = regex.build();
let original_content = &secondary_message.as_ref().unwrap().content;
// If the regex parses, replace with that
let new_content = if let Ok(regex) = valid_regex {
if global {
regex.replace_all(original_content.as_str(), *replacement).to_string()
} else {
regex.replace(original_content.as_str(), *replacement).to_string()
}
// Else attempt replace as string
} else {
original_content.replace(pattern, replacement)
};
let editing_member = Self::get_member_id_from_user_id(secondary_message.as_ref().unwrap().author.id, system_config).unwrap();
return Some(Command::Edit(editing_member, secondary_message.as_ref().unwrap().id, new_content.to_string()));
}
// If unable to parse
None
}
fn check_correction(message: &FullMessage, secondary_message: Option<&FullMessage>) -> Option<ParsedMessage> {
None
}
fn check_member_patterns(message: &FullMessage, secondary_message: Option<&FullMessage>, system_config: &System) -> Option<ParsedMessage> {
let matches_prefix = system_config.members.iter().enumerate().find_map(|(member_id, member)|
Some((member_id, member.matches_proxy_prefix(&message)?))
);
if let Some((member_id, matched_content)) = matches_prefix {
if matched_content.trim() == "*" {
Some(ParsedMessage::Command(Command::Reproxy(member_id, secondary_message.unwrap().id)))
} else if matched_content.trim() != "" {
Some(ParsedMessage::ProxiedMessage {
member_id,
message_content: matched_content.to_string(),
latch: true,
})
} else {
Some(ParsedMessage::SetProxyAndDelete(member_id))
}
} else {
None
}
}
fn check_autoproxy(message: &FullMessage, latch_state: Option<(MemberId, Timestamp)>) -> Option<ParsedMessage> {
if let Some((member_id, _)) = latch_state {
Some(ParsedMessage::ProxiedMessage {
member_id,
message_content: message.content.clone(),
latch: true,
})
} else {
None
}
}
fn match_member(maybe_mention: Option<&str>, system_config: &System) -> Option<MemberId> {
if let Some(maybe_mention) = maybe_mention {
if let Ok(mention) = Id::<UserMarker>::parse(maybe_mention) {
return MessageParser::get_member_id_from_user_id(mention, system_config)
}
}
None
}
pub fn get_member_id_from_user_id(user_id: UserId, system_config: &System) -> Option<MemberId> {
system_config.members.iter().enumerate()
.filter(|(_id, m)| m.user_id.is_some())
.find(|(_id, m)| m.user_id.unwrap() == user_id)
.map(|(id, _m)| id)
}
}
impl crate::config::Member {
pub fn matches_proxy_prefix<'a>(&self, message: &'a FullMessage) -> Option<&'a str> {
match self.message_pattern.captures(message.content.as_str()) {
None => None,
Some(captures) => match captures.name("content") {
None => None,
Some(matched_content) => Some(matched_content.as_str()),
},
}
}
}
|