ircbot 0.1.9

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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
use irc_proto::Message;
use tokio::sync::mpsc::UnboundedSender;

/// IRC mandates that no message line (including the trailing `\r\n`) may
/// exceed 512 bytes.  We budget 2 bytes for `\r\n`, leaving 510 bytes for
/// the command text itself.
const MAX_IRC_LINE: usize = 510;

/// A user on IRC (nick!user@host).
#[derive(Debug, Clone, Default)]
pub struct User {
    pub nick: String,
    pub user: String,
    pub host: String,
}

/// Per-message context passed to every handler.
pub struct Context {
    pub(crate) tx: UnboundedSender<String>,
    /// The channel or nick this message was directed to.
    pub target: String,
    pub is_channel: bool,
    /// The user who sent the message (if available).
    pub sender: Option<User>,
    /// The underlying parsed IRC message.
    pub raw: Message,
    /// The bot's own nick (for self-detection).
    pub bot_nick: String,
    /// Wildcard or regex captures from the matched trigger pattern.
    pub captures: Vec<String>,
}

/// Strip characters that could be used for IRC message injection.
fn sanitize(s: &str) -> String {
    s.chars()
        .filter(|&c| c != '\r' && c != '\n' && c != '\0')
        .collect()
}

/// Split `text` into one or more complete IRC lines of the form
/// `"{header}{chunk}{suffix}\r\n"` where each line is at most 512 bytes.
///
/// When a split is necessary the function prefers to break at the last ASCII
/// space within the available window (word-wrapping); if no space exists the
/// text is hard-split at the byte limit, taking care to stay on a valid UTF-8
/// character boundary.
///
/// Returns at least one entry even when `text` is empty.
pub fn make_messages(header: &str, text: &str, suffix: &str) -> Vec<String> {
    // bytes available for `text` inside each line
    let overhead = header.len() + suffix.len() + 2; // +2 for \r\n
    let available = MAX_IRC_LINE.saturating_sub(overhead);

    if text.is_empty() || available == 0 {
        return vec![format!("{header}{suffix}\r\n")];
    }
    if text.len() <= available {
        return vec![format!("{header}{text}{suffix}\r\n")];
    }

    let mut messages = Vec::new();
    let mut remaining = text;

    while !remaining.is_empty() {
        if remaining.len() <= available {
            messages.push(format!("{header}{remaining}{suffix}\r\n"));
            break;
        }

        // Find the largest valid UTF-8 boundary that fits.
        let mut end = available;
        while end > 0 && !remaining.is_char_boundary(end) {
            end -= 1;
        }
        // If not even one character fits in `available` (the leading character
        // is wider than the budget), advance to the end of the first character
        // anyway. This overshoots the budget by a few bytes for that one line
        // but guarantees forward progress on a valid char boundary — without it
        // the `end.max(1)` fallback below would slice mid-character and panic.
        if end == 0 {
            end = remaining
                .char_indices()
                .nth(1)
                .map_or(remaining.len(), |(i, _)| i);
        }

        // Prefer breaking at a space; fall back to the hard limit.
        let split_at = remaining[..end]
            .rfind(' ')
            .filter(|&p| p > 0)
            .unwrap_or(end);

        messages.push(format!("{header}{}{suffix}\r\n", &remaining[..split_at]));
        remaining = remaining[split_at..].trim_start_matches(' ');
    }

    messages
}

/// Send one or more (split) messages through `tx`.
fn send_chunked(
    tx: &UnboundedSender<String>,
    header: &str,
    text: &str,
    suffix: &str,
) -> crate::Result {
    for line in make_messages(header, text, suffix) {
        tx.send(line).map_err(|e| Box::new(e) as crate::BoxError)?;
    }
    Ok(())
}

impl Context {
    /// Reply to the sender.  In a channel, prefixes the nick; in a query, PMs back.
    ///
    /// If the formatted message would exceed the IRC 512-byte line limit it is
    /// automatically split across multiple messages.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn reply(&self, msg: impl std::fmt::Display) -> crate::Result {
        let msg = sanitize(&msg.to_string());
        if self.is_channel {
            let prefix = self
                .sender
                .as_ref()
                .map(|u| format!("{}, ", u.nick))
                .unwrap_or_default();
            let header = format!("PRIVMSG {} :{prefix}", self.target);
            send_chunked(&self.tx, &header, &msg, "")
        } else {
            let to = self
                .sender
                .as_ref()
                .map_or(self.target.as_str(), |u| u.nick.as_str());
            let header = format!("PRIVMSG {to} :");
            send_chunked(&self.tx, &header, &msg, "")
        }
    }

    /// Send a message to the channel / private target without a nick prefix.
    ///
    /// If the formatted message would exceed the IRC 512-byte line limit it is
    /// automatically split across multiple messages.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn say(&self, msg: impl std::fmt::Display) -> crate::Result {
        let msg = sanitize(&msg.to_string());
        let target = if self.is_channel {
            self.target.clone()
        } else {
            self.sender
                .as_ref()
                .map_or_else(|| self.target.clone(), |u| u.nick.clone())
        };
        let header = format!("PRIVMSG {target} :");
        send_chunked(&self.tx, &header, &msg, "")
    }

    /// Send a `/me` action.
    ///
    /// If the formatted message would exceed the IRC 512-byte line limit it is
    /// automatically split across multiple messages.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn action(&self, msg: impl std::fmt::Display) -> crate::Result {
        let msg = sanitize(&msg.to_string());
        let target = if self.is_channel {
            self.target.clone()
        } else {
            self.sender
                .as_ref()
                .map_or_else(|| self.target.clone(), |u| u.nick.clone())
        };
        // CTCP ACTION: header is "PRIVMSG target :\x01ACTION ", suffix is "\x01"
        let header = format!("PRIVMSG {target} :\x01ACTION ");
        send_chunked(&self.tx, &header, &msg, "\x01")
    }

    /// The trailing text of the underlying IRC message.
    #[must_use]
    pub fn message_text(&self) -> &str {
        match &self.raw.command {
            irc_proto::Command::PRIVMSG(_, text) | irc_proto::Command::NOTICE(_, text) => text,
            irc_proto::Command::PING(server, _) => server,
            irc_proto::Command::PONG(_, Some(token)) => token,
            irc_proto::Command::PONG(server, None) => server,
            irc_proto::Command::JOIN(channel, _, _) => channel,
            irc_proto::Command::PART(_, Some(reason)) => reason,
            irc_proto::Command::PART(channel, None) => channel,
            irc_proto::Command::QUIT(Some(message)) => message,
            irc_proto::Command::KICK(_, _, Some(reason)) => reason,
            irc_proto::Command::TOPIC(_, Some(topic)) => topic,
            irc_proto::Command::TOPIC(channel, None) => channel,
            irc_proto::Command::Response(_, args) => args.last().map(String::as_str).unwrap_or(""),
            irc_proto::Command::Raw(_, args) => args.last().map(String::as_str).unwrap_or(""),
            _ => "",
        }
    }

    /// The nick of the user who sent this message, if known.
    ///
    /// Returns `None` for server-origin or cron-fired contexts that have no
    /// associated [`Context::sender`].
    #[must_use]
    pub fn nick(&self) -> Option<&str> {
        self.sender.as_ref().map(|u| u.nick.as_str())
    }

    /// Whether this message originated from the bot itself.
    ///
    /// Compares the sender's nick against the bot's own nick
    /// ([`Context::bot_nick`]).  The comparison is ASCII-case-insensitive,
    /// which covers the common case; it does not implement the full RFC 1459
    /// nick-casemapping (where `{}|^` fold to `[]\~`).  Useful for ignoring the
    /// bot's own echoes so a handler doesn't reply to itself.
    #[must_use]
    pub fn is_from_self(&self) -> bool {
        self.nick()
            .is_some_and(|n| n.eq_ignore_ascii_case(&self.bot_nick))
    }

    /// Whether the message text mentions the bot's nick anywhere.
    ///
    /// Performs an ASCII-case-insensitive substring search of
    /// [`Context::message_text`] for [`Context::bot_nick`].  Unlike the
    /// `#[on(mention)]` trigger — which only fires when the bot is addressed at
    /// the *start* of the line — this matches the nick in any position.
    #[must_use]
    pub fn mentions_me(&self) -> bool {
        self.message_text()
            .to_ascii_lowercase()
            .contains(&self.bot_nick.to_ascii_lowercase())
    }

    /// Send an IRC NOTICE to the channel / private target.
    ///
    /// NOTICEs are typically displayed without triggering audible alerts and
    /// must never be replied to automatically (by convention), making them
    /// suitable for bot status messages or one-shot notifications.
    ///
    /// If the formatted message would exceed the IRC 512-byte line limit it is
    /// automatically split across multiple messages.
    pub async fn notice(&self, msg: impl std::fmt::Display) -> crate::Result {
        let msg = sanitize(&msg.to_string());
        let target = if self.is_channel {
            self.target.clone()
        } else {
            self.sender
                .as_ref()
                .map(|u| u.nick.clone())
                .unwrap_or_else(|| self.target.clone())
        };
        let header = format!("NOTICE {target} :");
        send_chunked(&self.tx, &header, &msg, "")
    }

    /// Send a private message directly to the sender, regardless of whether
    /// the original message arrived in a channel or a query window.
    ///
    /// Useful for sending sensitive or verbose information out of a public
    /// channel without flooding it.
    ///
    /// If the formatted message would exceed the IRC 512-byte line limit it is
    /// automatically split across multiple messages.
    pub async fn whisper(&self, msg: impl std::fmt::Display) -> crate::Result {
        let msg = sanitize(&msg.to_string());
        let to = self
            .sender
            .as_ref()
            .map(|u| u.nick.as_str())
            .unwrap_or(self.target.as_str());
        let header = format!("PRIVMSG {to} :");
        send_chunked(&self.tx, &header, &msg, "")
    }

    /// Make the bot join `channel`.
    ///
    /// Sends a raw `JOIN` command.  The channel name is sanitized to strip the
    /// `\r`, `\n`, and `\0` characters that could be used for command
    /// injection.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn join(&self, channel: impl std::fmt::Display) -> crate::Result {
        let channel = sanitize(&channel.to_string());
        self.tx
            .send(format!("JOIN {channel}\r\n"))
            .map_err(|e| Box::new(e) as crate::BoxError)?;
        Ok(())
    }

    /// Make the bot leave `channel`.
    ///
    /// Sends a raw `PART` command.  The channel name is sanitized to strip the
    /// `\r`, `\n`, and `\0` characters that could be used for command
    /// injection.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn part(&self, channel: impl std::fmt::Display) -> crate::Result {
        let channel = sanitize(&channel.to_string());
        self.tx
            .send(format!("PART {channel}\r\n"))
            .map_err(|e| Box::new(e) as crate::BoxError)?;
        Ok(())
    }

    /// Send a raw IRC protocol line.
    ///
    /// This is a low-level escape hatch for commands the framework does not
    /// wrap with a dedicated helper (`KICK`, `MODE`, `INVITE`, `WHOIS`, …).
    /// `line` is sanitized to strip the `\r`, `\n`, and `\0` characters — so a
    /// caller cannot smuggle additional lines — and a single trailing `\r\n` is
    /// appended.
    ///
    /// The caller is responsible for the command's IRC syntax and for keeping
    /// the line within the 512-byte protocol limit; unlike [`Context::say`],
    /// `raw` does not split or wrap its argument.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn raw(&self, line: impl std::fmt::Display) -> crate::Result {
        let line = sanitize(&line.to_string());
        self.tx
            .send(format!("{line}\r\n"))
            .map_err(|e| Box::new(e) as crate::BoxError)?;
        Ok(())
    }

    /// Set the topic of the current channel ([`Context::target`]).
    ///
    /// Sends a `TOPIC` command for the channel this message arrived in,
    /// mirroring how [`Context::say`] and [`Context::reply`] act on the
    /// current target.  The topic text is sanitized to strip the `\r`, `\n`,
    /// and `\0` injection characters.  It is sent as a single line; topics
    /// longer than the 512-byte protocol limit may be truncated by the server.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn set_topic(&self, topic: impl std::fmt::Display) -> crate::Result {
        let topic = sanitize(&topic.to_string());
        self.tx
            .send(format!("TOPIC {} :{topic}\r\n", self.target))
            .map_err(|e| Box::new(e) as crate::BoxError)?;
        Ok(())
    }

    /// Kick `nick` from the current channel ([`Context::target`]) with `reason`.
    ///
    /// Sends a `KICK` command for the channel this message arrived in.  Both
    /// `nick` and `reason` are sanitized to strip the `\r`, `\n`, and `\0`
    /// injection characters.
    ///
    /// # Errors
    ///
    /// Returns an error if the write channel is closed.
    pub fn kick(
        &self,
        nick: impl std::fmt::Display,
        reason: impl std::fmt::Display,
    ) -> crate::Result {
        let nick = sanitize(&nick.to_string());
        let reason = sanitize(&reason.to_string());
        self.tx
            .send(format!("KICK {} {nick} :{reason}\r\n", self.target))
            .map_err(|e| Box::new(e) as crate::BoxError)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::sync::mpsc;

    /// Build a `Context` wired to an in-process channel for easy inspection.
    fn make_ctx(target: &str, is_channel: bool) -> (Context, mpsc::UnboundedReceiver<String>) {
        let (tx, rx) = mpsc::unbounded_channel();
        let raw = ":nick!u@h PRIVMSG #chan :hello"
            .parse::<irc_proto::Message>()
            .unwrap();
        let ctx = Context {
            tx,
            target: target.to_string(),
            is_channel,
            sender: Some(User {
                nick: "nick".to_string(),
                user: "u".to_string(),
                host: "h".to_string(),
            }),
            raw,
            bot_nick: "bot".to_string(),
            captures: vec![],
        };
        (ctx, rx)
    }

    // ── sanitize ─────────────────────────────────────────────────────────────

    #[test]
    fn sanitize_strips_carriage_return() {
        assert_eq!(sanitize("foo\rbar"), "foobar");
    }

    #[test]
    fn sanitize_strips_newline() {
        assert_eq!(sanitize("foo\nbar"), "foobar");
    }

    #[test]
    fn sanitize_strips_null_byte() {
        assert_eq!(sanitize("foo\0bar"), "foobar");
    }

    #[test]
    fn sanitize_keeps_normal_text() {
        assert_eq!(sanitize("hello world"), "hello world");
    }

    // ── message_text ─────────────────────────────────────────────────────────

    #[test]
    fn message_text_returns_trailing_param() {
        let (ctx, _rx) = make_ctx("#chan", true);
        assert_eq!(ctx.message_text(), "hello");
    }

    // ── say ──────────────────────────────────────────────────────────────────

    #[test]
    fn say_in_channel_sends_privmsg_to_channel() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.say("hi there").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG #chan :hi there\r\n");
    }

    #[test]
    fn say_in_query_sends_privmsg_to_sender_nick() {
        let (ctx, mut rx) = make_ctx("bot", false);
        ctx.say("hi there").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG nick :hi there\r\n");
    }

    #[test]
    fn say_strips_injection_characters() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.say("evil\r\nJOIN #other").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG #chan :evilJOIN #other\r\n");
    }

    // ── reply ────────────────────────────────────────────────────────────────

    #[test]
    fn reply_in_channel_prefixes_sender_nick() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.reply("pong").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG #chan :nick, pong\r\n");
    }

    #[test]
    fn reply_in_query_sends_to_sender_nick() {
        let (ctx, mut rx) = make_ctx("bot", false);
        ctx.reply("pong").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG nick :pong\r\n");
    }

    // ── action ───────────────────────────────────────────────────────────────

    #[test]
    fn action_in_channel_wraps_in_ctcp() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.action("waves").unwrap();
        assert_eq!(
            rx.try_recv().unwrap(),
            "PRIVMSG #chan :\x01ACTION waves\x01\r\n"
        );
    }

    // ── notice ───────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn notice_in_channel_sends_notice_command() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.notice("status").await.unwrap();
        assert_eq!(rx.try_recv().unwrap(), "NOTICE #chan :status\r\n");
    }

    #[tokio::test]
    async fn notice_in_query_sends_to_sender_nick() {
        let (ctx, mut rx) = make_ctx("bot", false);
        ctx.notice("status").await.unwrap();
        assert_eq!(rx.try_recv().unwrap(), "NOTICE nick :status\r\n");
    }

    // ── whisper ──────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn whisper_sends_pm_to_sender() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.whisper("secret").await.unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG nick :secret\r\n");
    }

    // ── sender-less contexts ───────────────────────────────────────────────────

    /// Build a `Context` with no known sender (e.g. a server-origin message or
    /// a cron-fired context).
    fn make_ctx_no_sender(
        target: &str,
        is_channel: bool,
    ) -> (Context, mpsc::UnboundedReceiver<String>) {
        let (tx, rx) = mpsc::unbounded_channel();
        let raw = ":server PRIVMSG #chan :hello"
            .parse::<irc_proto::Message>()
            .unwrap();
        let ctx = Context {
            tx,
            target: target.to_string(),
            is_channel,
            sender: None,
            raw,
            bot_nick: "bot".to_string(),
            captures: vec![],
        };
        (ctx, rx)
    }

    #[test]
    fn reply_in_channel_without_sender_omits_prefix() {
        let (ctx, mut rx) = make_ctx_no_sender("#chan", true);
        ctx.reply("pong").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG #chan :pong\r\n");
    }

    #[test]
    fn reply_in_query_without_sender_uses_target() {
        let (ctx, mut rx) = make_ctx_no_sender("someone", false);
        ctx.reply("pong").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG someone :pong\r\n");
    }

    #[test]
    fn say_in_query_without_sender_uses_target() {
        let (ctx, mut rx) = make_ctx_no_sender("someone", false);
        ctx.say("hi").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG someone :hi\r\n");
    }

    #[test]
    fn action_in_query_without_sender_uses_target() {
        let (ctx, mut rx) = make_ctx_no_sender("someone", false);
        ctx.action("waves").unwrap();
        assert_eq!(
            rx.try_recv().unwrap(),
            "PRIVMSG someone :\x01ACTION waves\x01\r\n"
        );
    }

    #[tokio::test]
    async fn notice_in_query_without_sender_uses_target() {
        let (ctx, mut rx) = make_ctx_no_sender("someone", false);
        ctx.notice("status").await.unwrap();
        assert_eq!(rx.try_recv().unwrap(), "NOTICE someone :status\r\n");
    }

    #[tokio::test]
    async fn whisper_without_sender_uses_target() {
        let (ctx, mut rx) = make_ctx_no_sender("someone", false);
        ctx.whisper("secret").await.unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PRIVMSG someone :secret\r\n");
    }

    #[test]
    fn action_in_query_with_sender_targets_sender_nick() {
        let (ctx, mut rx) = make_ctx("bot", false);
        ctx.action("waves").unwrap();
        assert_eq!(
            rx.try_recv().unwrap(),
            "PRIVMSG nick :\x01ACTION waves\x01\r\n"
        );
    }

    // ── automatic splitting through the public API ─────────────────────────────

    #[test]
    fn say_splits_long_message_across_multiple_lines() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        // Well over the 510-byte line limit → must be split.
        let text = "a".repeat(1_200);
        ctx.say(&text).unwrap();

        let mut lines = Vec::new();
        while let Ok(line) = rx.try_recv() {
            lines.push(line);
        }
        assert!(lines.len() >= 2, "expected the message to be split");
        for line in &lines {
            assert!(line.len() <= 512, "line exceeds 512 bytes: {}", line.len());
            assert!(line.ends_with("\r\n"));
        }
        // Reassembling the bodies reproduces the original text.
        let recovered: String = lines
            .iter()
            .map(|l| {
                l.strip_prefix("PRIVMSG #chan :")
                    .unwrap()
                    .trim_end_matches("\r\n")
            })
            .collect();
        assert_eq!(recovered, text);
    }

    #[test]
    fn make_messages_handles_leading_char_wider_than_budget() {
        // A header long enough to leave only a 1-byte text budget, with leading
        // multibyte characters that cannot fit. Must not panic, must split on
        // valid UTF-8 boundaries, and must preserve the full text.
        let header = format!("PRIVMSG {} :", "a".repeat(497)); // → available == 1
        let text = "é한hello"; // 2-byte + 3-byte leading chars
        let msgs = make_messages(&header, text, "");

        let recovered: String = msgs
            .iter()
            .map(|l| l.strip_prefix(&header).unwrap().trim_end_matches("\r\n"))
            .collect();
        assert_eq!(recovered, text);
    }

    // ── join / part ────────────────────────────────────────────────────────────

    #[test]
    fn join_sends_join_command() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.join("#other").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "JOIN #other\r\n");
    }

    #[test]
    fn part_sends_part_command() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.part("#other").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "PART #other\r\n");
    }

    #[test]
    fn join_strips_injection_characters() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.join("#evil\r\nQUIT").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "JOIN #evilQUIT\r\n");
    }

    // ── raw ────────────────────────────────────────────────────────────────────

    #[test]
    fn raw_sends_the_exact_line_with_crlf() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.raw("MODE #chan +o nick").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "MODE #chan +o nick\r\n");
    }

    #[test]
    fn raw_strips_injection_characters() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.raw("MODE #chan +o nick\r\nQUIT").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "MODE #chan +o nickQUIT\r\n");
    }

    // ── set_topic / kick ───────────────────────────────────────────────────────

    #[test]
    fn set_topic_sends_topic_for_current_channel() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.set_topic("welcome all").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "TOPIC #chan :welcome all\r\n");
    }

    #[test]
    fn set_topic_strips_injection_characters() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.set_topic("hi\r\nQUIT").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "TOPIC #chan :hiQUIT\r\n");
    }

    #[test]
    fn kick_sends_kick_for_current_channel() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.kick("baduser", "spamming").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "KICK #chan baduser :spamming\r\n");
    }

    #[test]
    fn kick_strips_injection_characters_from_nick_and_reason() {
        let (ctx, mut rx) = make_ctx("#chan", true);
        ctx.kick("bad\r\nuser", "be\r\nnice").unwrap();
        assert_eq!(rx.try_recv().unwrap(), "KICK #chan baduser :benice\r\n");
    }

    // ── nick / is_from_self / mentions_me ──────────────────────────────────────

    #[test]
    fn nick_returns_sender_nick() {
        let (ctx, _rx) = make_ctx("#chan", true);
        assert_eq!(ctx.nick(), Some("nick"));
    }

    #[test]
    fn nick_returns_none_without_sender() {
        let (ctx, _rx) = make_ctx_no_sender("#chan", true);
        assert_eq!(ctx.nick(), None);
    }

    #[test]
    fn is_from_self_true_when_sender_matches_bot_nick() {
        let (mut ctx, _rx) = make_ctx("#chan", true);
        ctx.bot_nick = "nick".to_string(); // sender nick is "nick"
        assert!(ctx.is_from_self());
    }

    #[test]
    fn is_from_self_is_ascii_case_insensitive() {
        let (mut ctx, _rx) = make_ctx("#chan", true);
        ctx.bot_nick = "NICK".to_string();
        assert!(ctx.is_from_self());
    }

    #[test]
    fn is_from_self_false_for_other_user() {
        let (ctx, _rx) = make_ctx("#chan", true);
        // sender nick is "nick", bot_nick is "bot"
        assert!(!ctx.is_from_self());
    }

    #[test]
    fn is_from_self_false_without_sender() {
        let (ctx, _rx) = make_ctx_no_sender("#chan", true);
        assert!(!ctx.is_from_self());
    }

    #[test]
    fn mentions_me_true_when_text_contains_bot_nick() {
        let (mut ctx, _rx) = make_ctx("#chan", true);
        ctx.raw = ":nick!u@h PRIVMSG #chan :hey Bot, how are you"
            .parse::<irc_proto::Message>()
            .unwrap(); // bot_nick is "bot"
        assert!(ctx.mentions_me());
    }

    #[test]
    fn mentions_me_false_when_text_does_not_contain_bot_nick() {
        let (ctx, _rx) = make_ctx("#chan", true);
        // message_text is "hello", bot_nick is "bot"
        assert!(!ctx.mentions_me());
    }
}