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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
use async_recursion::async_recursion;
use rand::seq::IndexedRandom;
use log::{debug, warn};
use ::regex::Regex;
use crate::{RiveScript, ast, inheritance};
/// Get a reply to the username's message.
pub async fn reply(rs: &mut RiveScript, username: &str, message: &str) -> Result<String, String> {
// Store the current user's ID.
rs.in_reply_context = true;
rs.current_username = String::from(username);
let mut msg = String::from(message);
let mut answer;
// Format their message (run substitutions, etc.)
msg = format_message(rs, &msg, false);
debug!("Find a reply to: {msg}");
// If the BEGIN block exists, consult it first.
if rs.brain.has_begin_block() {
debug!("Has a BEGIN block");
match get_reply(rs, &rs.current_username, &String::from(rivescript_core::BEGIN_REQUEST), true, 0).await {
Ok(begin) => {
debug!("Answer to BEGIN request: {begin}");
// Is it OK to continue?
if begin.contains(rivescript_core::TAG_OK) {
// Get the real reply and substitute it in.
match get_reply(rs, &rs.current_username, &msg, false, 0).await {
Ok(reply) => {
debug!("Answer to reply request: {reply}");
answer = begin.replace(rivescript_core::TAG_OK, &reply);
},
Err(e) => {
return Err(e);
},
}
} else {
answer = begin;
}
// Run post-reply tags.
answer = crate::tags::process(&rs, &rs.current_username, &msg, &answer, Vec::new(), Vec::new(), 0).await;
},
Err(e) => {
return Err(e);
},
}
} else {
debug!("No BEGIN block");
match get_reply(rs, &rs.current_username, &msg, false, 0).await {
Ok(reply) => {
debug!("Answer to reply request: {reply}");
answer = reply
},
Err(e) => {
return Err(e);
},
}
}
if answer.is_empty() {
answer = String::from(rivescript_core::ERR_NO_REPLY);
}
// Save their message history.
rs.sessions.add_history(username, message, &answer).await;
// Unset the current user's ID.
rs.current_username = String::new();
rs.in_reply_context = false;
return Ok(answer);
}
// Inner reply-fetching logic, called for the >BEGIN block too.
#[async_recursion]
pub async fn get_reply(
rs: &RiveScript,
username: &String,
message: &String,
is_begin: bool,
step: usize,
) -> Result<String, String> {
// They forgot to sort replies?
if rs.sorted_topics.is_empty() {
return Err("You forgot to call sort_replies()".to_string());
}
// Avoid deep recursion.
if step > rs.depth {
return Err("Deep recursion detected".to_string())
}
// What topic are we in?
let mut topic: String;
if is_begin {
topic = String::from(rivescript_core::BEGIN_TOPIC);
} else {
topic = rs.sessions.get(username, "topic").await;
}
// Collect matched regex stars.
let mut stars: Vec<String> = Vec::new();
let mut bot_stars: Vec<String> = Vec::new();
let mut reply = String::new();
// Avoid letting them fall into a missing topic.
if !rs.brain.has_topic(&topic) {
warn!("User {username} was in an empty topic named '{topic}' - rescuing them back to '{}'", rivescript_core::DEFAULT_TOPIC);
topic = String::from(rivescript_core::DEFAULT_TOPIC)
}
// Keep a pointer to the matched Trigger once we find it.
let mut matched: &ast::Trigger = &ast::Trigger::new("");
let mut found_match = false;
// See if there were any %Previous's in this topic, or any topic related to
// it. This should only be done the first time -- not during a recursive
// redirection. This is because in a redirection, "lastReply" is still gonna
// be the same as it was the first time, resulting in an infinite loop!
if step == 0 {
// Gather all of the topics (inherits/includes).
let this_topic = rs.brain.topics.get(&topic).unwrap();
let all_topics = inheritance::get_topic_tree(&rs.brain, &this_topic, 0);
// Scan all the topics.
'previous: for topic in all_topics {
debug!("Checking topic {topic} for any %Previous's.");
let triggers = rs.sorted_thats.get(&topic).unwrap();
if triggers.len() > 0 {
debug!("There's a %Previous in this topic!");
}
for trig in triggers {
// Get the bot's last reply to the user.
let history = rs.sessions.get_history(username).await;
let last_reply = history.reply.get(0).unwrap();
// Format the bot's last reply the same way as the human's.
let last_reply = format_message(rs, last_reply, true);
debug!("Bot's last reply: '{last_reply}' vs. '{}'", trig.previous);
// See if the bot's last reply matches.
let pattern = &trig.previous;
let regexp = trigger_regexp(rs, username, pattern).await;
match regexp.captures(&last_reply) {
Some(caps) => {
// Huzzah! See if OUR message is right too...
debug!("Bot side matched!");
// Collect the bot stars while we're here.
bot_stars = caps.iter()
.filter_map(
|m|
m.map(|m| m.as_str().to_string())
)
.collect();
// Compare the trigger to the user's message.
let pattern = &trig.trigger;
let regexp = trigger_regexp(rs, username, pattern).await;
match regexp.captures(message) {
Some(caps) => {
// The user side matched too!
// Collect the stars.
stars = caps.iter()
.filter_map(
|m|
m.map(|m| m.as_str().to_string())
)
.collect();
// Mark that we found a match.
matched = trig;
found_match = true;
break 'previous;
},
None => continue,
}
},
None => continue,
}
}
}
}
// Search their topic for a match to their trigger.
if !found_match {
debug!("Searching their topic ({topic}) for a match...");
let triggers = rs.sorted_topics.get(&topic).unwrap();
for trig in triggers {
let pattern = &trig.trigger;
let regexp = trigger_regexp(rs, username, pattern).await;
debug!("Compare '{message}' to: /{regexp}/");
match regexp.captures(message) {
Some(caps) => {
stars = caps.iter()
.filter_map(
|m|
m.map(|m| m.as_str().to_string())
)
.collect();
// We found a match!
matched = trig;
found_match = true;
break;
},
None => continue,
}
}
}
// Store what trigger they last matched on.
// TODO
// Did we find a match after all?
if found_match {
// A single loop so we can break early.
loop {
// See if there are any hard redirects.
if !matched.redirect.is_empty() {
debug!("Redirecting us to: {}", matched.redirect);
let mut redirect = matched.redirect.clone();
redirect = crate::tags::process(&rs, &username, &message, &redirect, stars.clone(), bot_stars.clone(), 0).await;
redirect = redirect.to_lowercase();
debug!("Pretend user said: {redirect}");
match get_reply(&rs, &username, &redirect, false, step+1).await {
Ok(r) => {
reply = r;
break;
}
Err(e) => return Err(e),
}
}
// Check the conditionals.
for row in matched.condition.clone() {
// Process tags on the left and right sides.
let mut left = crate::tags::process(rs, username, message, &row.left, stars.clone(), bot_stars.clone(), step).await;
let mut right = crate::tags::process(rs, username, message, &row.right, stars.clone(), bot_stars.clone(), step).await;
// Defaults?
if left.len() == 0 {
left = rivescript_core::UNDEFINED.to_string();
}
if right.len() == 0 {
right = rivescript_core::UNDEFINED.to_string();
}
debug!("Check if [{left}] {} [{right}]", row.operator);
let mut passed = false;
match row.operator.as_str() {
"eq" | "==" => {
passed = left == right;
},
"ne" | "!=" | "<>" => {
passed = left != right;
},
_ => {
// The other operators deal with numbers.
if let Ok(left_value) = left.parse::<i64>() {
if let Ok(right_value) = right.parse::<i64>() {
// Do the needful.
match row.operator.as_str() {
"<" => passed = left_value < right_value,
"<=" => passed = left_value <= right_value,
">" => passed = left_value > right_value,
">=" => passed = left_value >= right_value,
_ => (),
}
} else {
warn!("Right side of condition was non-numeric: {:?}", row);
}
} else {
warn!("Left side of condition was non-numeric: {:?}", row);
}
},
}
// Did the condition pass?
if passed {
reply = row.reply;
break;
}
}
// Have our reply yet?
if !reply.is_empty() {
break;
}
// We are down to the final -Reply tags.
// Process {weight} in the replies.
let mut bucket: Vec<String> = Vec::new();
for rep in matched.reply.clone() {
match rivescript_core::regex::WEIGHT.captures(&rep) {
Some(caps) => {
let weight: usize = caps.get(1).unwrap().as_str().parse().unwrap();
for _ in 0..weight {
bucket.push(rep.clone());
}
}
None => {
bucket.push(rep);
}
}
}
// Get a random reply.
if !bucket.is_empty() {
let mut rng = rand::rng();
if let Some(selection) = &bucket[..].choose(&mut rng) {
reply = selection.to_string();
} else {
return Err("No random replies!".to_string());
}
}
break;
}
}
// Still no reply?? Give up with the fallback error replies.
if !found_match {
return Ok(String::from(rivescript_core::ERR_NO_MATCH));
} else if reply.is_empty() {
return Ok(String::from(rivescript_core::ERR_NO_REPLY));
}
// Process tags for the BEGIN block.
if is_begin {
// TODO: set topic and user vars.
} else {
reply = crate::tags::process(&rs, &username, &message, &reply, stars, bot_stars, step).await;
}
// TODO
Ok(String::from(reply))
}
// Format the input message for safe processing.
pub fn format_message(rs: &RiveScript, msg: &str, is_bot_reply: bool) -> String {
let mut msg = String::from(msg);
// Lowercase it.
if !rs.case_sensitive {
msg = msg.to_lowercase();
}
// Run substitutions and sanitize what's left.
msg = crate::tags::substitute(rs.brain.subs.clone(), rs.sorted_subs.clone(), &msg);
// In UTF-8 mode, only strip metacharacters and HTML brackets.
if rs.utf8 {
msg = rivescript_core::regex::META_CHARACTERS.replace_all(&msg, "").to_string();
msg = rs.unicode_punctuation.replace_all(&msg, "").to_string();
// For the bot's last reply, strip all extra symbols.
if is_bot_reply {
msg = rivescript_core::regex::SYMBOLS.replace_all(&msg, "").to_string();
}
} else {
// For everything else, strip all non-alphanumerics.
msg = strip_nasties(msg);
}
// Trim spaces and return.
msg.trim().to_string()
}
// Prepare a trigger pattern for the regular expression engine.
pub async fn trigger_regexp(rs: &RiveScript, username: &String, pattern: &String) -> Regex {
let mut pattern = pattern.clone();
// If the trigger is simply '*' then the * needs to become (.*?)
// instead of the usual (.+?), to match the blank string too.
pattern = rivescript_core::regex::ZERO_WIDTH_STAR.replace_all(&pattern, "<zerowidthstar>").to_string();
// Simple replacements.
pattern = pattern.replace("*", r"(.+?)"); // *
pattern = pattern.replace("#", r"(\d+?)"); // #
pattern = pattern.replace("_", r"(\w+?)"); // _
// Remove {weight} and {inherits}
pattern = rivescript_core::regex::WEIGHT.replace_all(&pattern, "").to_string();
pattern = rivescript_core::regex::INHERITS.replace_all(&pattern, "").to_string();
// Recover the zero-width star.
pattern = pattern.replace("<zerowidthstar>", r"(.*?)");
// UTF-8 mode special characters.
if rs.utf8 {
// Literal @ symbols (like in an e-mail address) conflict with arrays. If the RiveScript
// trigger had an escaped '\@' sequence, move it out of the way first.
pattern = pattern.replace(r"\@", r"\u0040");
}
// Optionals.
for (_, [inner]) in rivescript_core::regex::TRIGGER_OPTIONALS.captures_iter(&pattern.clone()).map(|c| c.extract()) {
let parts: Vec<String> = inner.split("|").map(|s| s.to_string()).collect();
let mut options: Vec<String> = Vec::new();
for p in parts {
options.push(format!(r"(?:\s|\b)+{}(?:\s|\b)+", p));
}
// If this optional had a star or anything in it, make it non-capturing.
let mut pipes = options.join("|");
pipes = pipes.replace(r"(.+?)", r"(?:.+?)");
pipes = pipes.replace(r"(\d+?)", r"(?:\d+?)");
pipes = pipes.replace(r"(\w+?)", r"(?:\w+?)");
// Substitute the original [optional] greedily back into the pattern.
let qm = regex::escape(&inner);
let replacement = format!(r"(?:{}|(?:\s+|\b))", pipes);
pattern = Regex::new(&String::from(format!(r"\s*\[{qm}\]\s*")))
.unwrap()
.replace_all(&pattern, &replacement)
.to_string();
}
// Don't let _ wildcards match numbers!
// A quick note on why it's this way: the initial replacement above that
// swaps (_ => (\w+?)) needed to be \w because the square brackets
// in [\s\d] will confuse the optionals logic just above. So then we
// switch it back down here. Also, we don't just use \w+ because that
// will match digits, and similarly [A-Za-z] would not match Unicode.
pattern = pattern.replace(r"\w", r"[^\s\d]");
// Filter in arrays.
for (m, [name]) in rivescript_core::regex::TRIGGER_ARRAY.captures_iter(&pattern.clone()).map(|c| c.extract()) {
let mut replacement = String::new();
if let Some(items) = rs.brain.arrays.get(name) {
replacement = format!(r"(?:{})", items.join("|"));
}
pattern = pattern.replace(m, &replacement);
}
// Filter in bot variables.
for (m, [name]) in rivescript_core::regex::BOT_TAG.captures_iter(&pattern.clone()).map(|c| c.extract()) {
let mut replacement = rs.brain.get_bot_var(name);
replacement = strip_nasties(replacement).to_lowercase();
pattern = pattern.replace(m, &replacement);
}
// Filter in <get> user variables.
for (m, [name]) in rivescript_core::regex::USER_VAR_TAG.captures_iter(&pattern.clone()).map(|c| c.extract()) {
let mut replacement = rs.sessions.get(username, name).await;
replacement = strip_nasties(replacement).to_lowercase();
pattern = pattern.replace(m, &replacement);
}
// Filter in <input>/<reply> tags.
if pattern.contains("<input") || pattern.contains("<reply") {
pattern = pattern.replace("<input>", "<input1>");
pattern = pattern.replace("<reply>", "<reply1>");
let history = rs.sessions.get_history(username).await;
for (_, [number]) in rivescript_core::regex::HISTORY_TAG.captures_iter(&pattern.clone()).map(|c| c.extract()) {
let mut idx: usize = 1;
if !number.is_empty() {
idx = number.parse().unwrap();
}
let input = history.input.get(idx-1).unwrap();
let reply = history.reply.get(idx-1).unwrap();
// Format the previous inputs for the regexp engine.
let input = &format_message(rs, input, true);
let reply = &format_message(rs, reply, true);
pattern = pattern.replace(
&String::from(format!("<input{idx}>")),
input,
);
pattern = pattern.replace(
&String::from(format!("<reply{idx}>")),
reply,
);
}
}
// Recover escaped Unicode symbols (@ signs).
if rs.utf8 && pattern.contains(r"\u") {
// TODO: make it more general.
pattern = pattern.replace(r"\u0040", "@");
}
pattern = String::from(format!(r"^{}$", pattern.trim()));
Regex::new(&pattern).unwrap_or(Regex::new("").unwrap())
}
pub fn strip_nasties(msg: String) -> String {
let mut msg = msg.clone();
msg = rivescript_core::regex::NASTIES.replace_all(&msg, "").to_string();
msg
}