ircbot 0.2.0

An async IRC bot framework for Rust powered by Tokio and procedural macros
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use irc_proto::Message;
use ircbot::bot::{check_trigger, glob_match};
use ircbot::handler::Trigger;

// ─── glob_match ───────────────────────────────────────────────────────────────

#[test]
fn glob_exact_match() {
    assert!(glob_match("hello", "hello").is_some());
    assert!(glob_match("hello", "world").is_none());
}

#[test]
fn glob_single_wildcard_suffix() {
    let caps = glob_match("hello *", "hello world").unwrap();
    assert_eq!(caps, vec!["world"]);
}

#[test]
fn glob_single_wildcard_prefix() {
    let caps = glob_match("* world", "hello world").unwrap();
    assert_eq!(caps, vec!["hello"]);
}

#[test]
fn glob_two_wildcards() {
    let caps = glob_match("* loves *", "alice loves rust").unwrap();
    assert_eq!(caps, vec!["alice", "rust"]);
}

#[test]
fn glob_no_wildcard_mismatch() {
    assert!(glob_match("hello world", "hello there").is_none());
}

#[test]
fn glob_empty_capture() {
    // Pattern ends with '*', empty trailing text is a valid capture.
    let caps = glob_match("hello *", "hello ").unwrap();
    assert_eq!(caps, vec![""]);
}

#[test]
fn glob_case_insensitive() {
    // The generated regex uses (?i)
    assert!(glob_match("Hello *", "hello world").is_some());
}

// ─── check_trigger: Command ───────────────────────────────────────────────────

fn privmsg(target: &str, text: &str) -> Message {
    format!(":nick!u@h PRIVMSG {} :{}", target, text)
        .parse()
        .unwrap()
}

#[test]
fn command_trigger_basic() {
    let trigger = Trigger::Command {
        name: "ping".to_string(),
        target: None,
    };
    let msg = privmsg("#chan", "!ping");
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert!(caps.is_empty());
}

#[test]
fn command_trigger_with_args() {
    let trigger = Trigger::Command {
        name: "echo".to_string(),
        target: None,
    };
    let msg = privmsg("#chan", "!echo hello world");
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["hello world"]);
}

#[test]
fn command_trigger_wrong_name() {
    let trigger = Trigger::Command {
        name: "ping".to_string(),
        target: None,
    };
    let msg = privmsg("#chan", "!pong");
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

#[test]
fn command_trigger_target_match() {
    let trigger = Trigger::Command {
        name: "hi".to_string(),
        target: Some("#general".to_string()),
    };
    assert!(check_trigger(&trigger, &privmsg("#general", "!hi"), "bot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#other", "!hi"), "bot").is_none());
}

#[test]
fn command_trigger_case_insensitive_name() {
    let trigger = Trigger::Command {
        name: "Ping".to_string(),
        target: None,
    };
    assert!(check_trigger(&trigger, &privmsg("#chan", "!ping"), "bot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#chan", "!PING"), "bot").is_some());
}

#[test]
fn command_trigger_ignores_non_privmsg() {
    let trigger = Trigger::Command {
        name: "ping".to_string(),
        target: None,
    };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

// ─── check_trigger: Message ───────────────────────────────────────────────────

#[test]
fn message_trigger_exact() {
    let trigger = Trigger::Message {
        pattern: "hello".to_string(),
        target: None,
    };
    assert!(check_trigger(&trigger, &privmsg("#chan", "hello"), "bot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#chan", "hello world"), "bot").is_none());
}

#[test]
fn message_trigger_wildcard() {
    let trigger = Trigger::Message {
        pattern: "hello *".to_string(),
        target: None,
    };
    let caps = check_trigger(&trigger, &privmsg("#chan", "hello alice"), "bot").unwrap();
    assert_eq!(caps, vec!["alice"]);
}

#[test]
fn message_trigger_target_filter() {
    let trigger = Trigger::Message {
        pattern: "hi".to_string(),
        target: Some("#rust".to_string()),
    };
    assert!(check_trigger(&trigger, &privmsg("#rust", "hi"), "bot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#other", "hi"), "bot").is_none());
}

// ─── check_trigger: Event ────────────────────────────────────────────────────

#[test]
fn event_trigger_join() {
    let trigger = Trigger::Event {
        event: "JOIN".to_string(),
        target: None,
        regex: None,
    };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_some());
}

#[test]
fn event_trigger_join_wrong_event() {
    let trigger = Trigger::Event {
        event: "PART".to_string(),
        target: None,
        regex: None,
    };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

#[test]
fn event_trigger_with_regex() {
    let trigger = Trigger::Event {
        event: "PRIVMSG".to_string(),
        target: None,
        regex: Some(r"^Hello, (\w+)!$".to_string()),
    };
    let msg = privmsg("#chan", "Hello, world!");
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["world"]);
}

#[test]
fn event_trigger_regex_no_match() {
    let trigger = Trigger::Event {
        event: "PRIVMSG".to_string(),
        target: None,
        regex: Some(r"^Hello, (\w+)!$".to_string()),
    };
    let msg = privmsg("#chan", "Goodbye, world!");
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

// ─── check_trigger: Mention ──────────────────────────────────────────────────

#[test]
fn mention_trigger_colon_separator() {
    let trigger = Trigger::Mention { target: None };
    let msg = privmsg("#chan", "rustbot: hello there");
    let caps = check_trigger(&trigger, &msg, "rustbot").unwrap();
    assert_eq!(caps, vec!["hello there"]);
}

#[test]
fn mention_trigger_comma_separator() {
    let trigger = Trigger::Mention { target: None };
    let msg = privmsg("#chan", "rustbot, what time is it?");
    let caps = check_trigger(&trigger, &msg, "rustbot").unwrap();
    assert_eq!(caps, vec!["what time is it?"]);
}

#[test]
fn mention_trigger_case_insensitive_nick() {
    let trigger = Trigger::Mention { target: None };
    assert!(check_trigger(&trigger, &privmsg("#chan", "RUSTBOT: hi"), "rustbot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#chan", "RustBot: hi"), "rustbot").is_some());
}

#[test]
fn mention_trigger_wrong_nick() {
    let trigger = Trigger::Mention { target: None };
    let msg = privmsg("#chan", "otherbot: hello");
    assert!(check_trigger(&trigger, &msg, "rustbot").is_none());
}

#[test]
fn mention_trigger_no_separator() {
    // "rustbot hello" without a separator should NOT match.
    let trigger = Trigger::Mention { target: None };
    let msg = privmsg("#chan", "rustbot hello");
    assert!(check_trigger(&trigger, &msg, "rustbot").is_none());
}

#[test]
fn mention_trigger_ignores_non_privmsg() {
    let trigger = Trigger::Mention { target: None };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "rustbot").is_none());
}

#[test]
fn mention_trigger_target_filter() {
    let trigger = Trigger::Mention {
        target: Some("#rust".to_string()),
    };
    assert!(check_trigger(&trigger, &privmsg("#rust", "rustbot: hi"), "rustbot").is_some());
    assert!(check_trigger(&trigger, &privmsg("#other", "rustbot: hi"), "rustbot").is_none());
}

#[test]
fn mention_trigger_empty_rest() {
    // "rustbot: " — only whitespace after the separator: after trimming the
    // remainder is empty, so the captures vector should be empty too.
    let trigger = Trigger::Mention { target: None };
    let msg = privmsg("#chan", "rustbot: ");
    let caps = check_trigger(&trigger, &msg, "rustbot").unwrap();
    assert!(caps.is_empty());
}

// ─── glob_match: ? wildcard and literal dot ───────────────────────────────────

#[test]
fn glob_question_mark_matches_single_char() {
    assert!(glob_match("hel?o", "hello").is_some());
    assert!(glob_match("hel?o", "helXo").is_some());
}

#[test]
fn glob_question_mark_does_not_match_zero_chars() {
    // '?' must match exactly one character, so "hel?o" does not match "helo".
    assert!(glob_match("hel?o", "helo").is_none());
}

#[test]
fn glob_question_mark_does_not_match_two_chars() {
    assert!(glob_match("hel?o", "helllo").is_none());
}

#[test]
fn glob_literal_dot_matches_dot() {
    assert!(glob_match("3.14", "3.14").is_some());
}

#[test]
fn glob_literal_dot_does_not_match_any_char() {
    // '.' is NOT a regex wildcard in the glob pattern; it must only match a literal '.'.
    assert!(glob_match("3.14", "3X14").is_none());
}

// ─── check_trigger: Cron ────────────────────────────────────────────────────

#[test]
fn cron_trigger_never_matches_privmsg() {
    let trigger = Trigger::Cron {
        schedule: "0 0 * * * *".to_string(),
        tz: "UTC".to_string(),
        target: None,
    };
    let msg = privmsg("#chan", "hello");
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

#[test]
fn cron_trigger_never_matches_join() {
    let trigger = Trigger::Cron {
        schedule: "0 0 8-16 * * MON-FRI".to_string(),
        tz: "UTC".to_string(),
        target: Some("#chan".to_string()),
    };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

#[test]
fn event_trigger_target_filter() {
    let trigger = Trigger::Event {
        event: "JOIN".to_string(),
        target: Some("#rust".to_string()),
        regex: None,
    };
    let join_rust = ":nick!u@h JOIN #rust".parse().unwrap();
    let join_other = ":nick!u@h JOIN #other".parse().unwrap();
    assert!(check_trigger(&trigger, &join_rust, "bot").is_some());
    assert!(check_trigger(&trigger, &join_other, "bot").is_none());
}

#[test]
fn event_trigger_case_insensitive_event_name() {
    // The event field in the trigger is matched case-insensitively against
    // the command name.
    let trigger = Trigger::Event {
        event: "join".to_string(),
        target: None,
        regex: None,
    };
    let msg = ":nick!u@h JOIN #chan".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_some());
}

#[test]
fn event_trigger_invalid_regex_returns_none() {
    let trigger = Trigger::Event {
        event: "PRIVMSG".to_string(),
        target: None,
        regex: Some("[invalid".to_string()),
    };
    let msg = privmsg("#chan", "hello");
    assert!(check_trigger(&trigger, &msg, "bot").is_none());
}

// ─── check_trigger: Event over other command kinds ────────────────────────────
//
// These exercise the `command_name`, `trailing_param` and `target_param`
// helpers for command variants beyond PRIVMSG/JOIN.

#[test]
fn event_notice_regex_matches_trailing_param() {
    let trigger = Trigger::Event {
        event: "NOTICE".to_string(),
        target: None,
        regex: Some(r"(\w+) back".to_string()),
    };
    let msg: Message = ":nick!u@h NOTICE #chan :ping back".parse().unwrap();
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["ping"]);
}

#[test]
fn event_topic_target_filter_and_trailing_regex() {
    let trigger = Trigger::Event {
        event: "TOPIC".to_string(),
        target: Some("#chan".to_string()),
        regex: Some(r"new (.+)".to_string()),
    };
    let msg: Message = ":nick!u@h TOPIC #chan :new topic here".parse().unwrap();
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["topic here"]);

    // Wrong channel is filtered out via target_param.
    let other: Message = ":nick!u@h TOPIC #other :new topic here".parse().unwrap();
    assert!(check_trigger(&trigger, &other, "bot").is_none());
}

#[test]
fn event_kick_target_filter_and_reason_regex() {
    let trigger = Trigger::Event {
        event: "KICK".to_string(),
        target: Some("#chan".to_string()),
        regex: Some(r"(spam)".to_string()),
    };
    let msg: Message = ":op!u@h KICK #chan baduser :spam".parse().unwrap();
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["spam"]);
}

#[test]
fn event_invite_target_filter_uses_channel_param() {
    let trigger = Trigger::Event {
        event: "INVITE".to_string(),
        target: Some("#secret".to_string()),
        regex: None,
    };
    let msg: Message = ":nick!u@h INVITE testbot #secret".parse().unwrap();
    assert!(check_trigger(&trigger, &msg, "bot").is_some());

    let wrong: Message = ":nick!u@h INVITE testbot #public".parse().unwrap();
    assert!(check_trigger(&trigger, &wrong, "bot").is_none());
}

#[test]
fn event_raw_command_matches_name_target_and_trailing() {
    // An unknown command parses to `Command::Raw`, exercising the `Raw` arms of
    // command_name / target_param / trailing_param.
    let trigger = Trigger::Event {
        event: "FOOBAR".to_string(),
        target: Some("#chan".to_string()),
        regex: Some(r"hello (\w+)".to_string()),
    };
    let msg: Message = ":serv FOOBAR #chan :hello world".parse().unwrap();
    let caps = check_trigger(&trigger, &msg, "bot").unwrap();
    assert_eq!(caps, vec!["world"]);
}