From 4411e6f5498beec81c95eb9810c37cd7ab56a0db Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sat, 15 Oct 2016 14:45:23 -0400 Subject: Fix numeric events being unsubscrabable due to incorrect identifiers Fixes #31 --- docs/02-event-reference.md | 18 +++++++++--------- examples/08-numeric-bot.p6 | 32 ++++++++++++++++++++++++++++++++ lib/IRC/Client.pm6 | 5 ++++- 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 examples/08-numeric-bot.p6 diff --git a/docs/02-event-reference.md b/docs/02-event-reference.md index a2c6fdd..fdf9d0a 100644 --- a/docs/02-event-reference.md +++ b/docs/02-event-reference.md @@ -28,7 +28,7 @@ - [`irc-started`](#irc-started) - [`irc-to-me`](#irc-to-me) - [`irc-unknown`](#irc-unknown) - - [`irc-XXX`](#irc-xxx) + - [`irc-nXXX`](#irc-nXXX) - [Up Next](#up-next) --- @@ -61,7 +61,7 @@ that the returned value from the event handler should be discarded. ## Event Map -In the chart below, `irc-XXX` stands for numeric events where `XXX` is a +In the chart below, `irc-nXXX` stands for numeric events where `XXX` is a three-digit number. See [this numerics table](https://www.alien.net.au/irc/irc2numerics.html) for meaning of codes, depending on the server used. @@ -80,8 +80,8 @@ irc-addressed ▶ irc-to-me ▶ irc-notice-channel ▶ irc-notice irc-mode-channel ▶ irc-mode ▶ irc-all irc-mode-me ▶ irc-mode ▶ irc-all - irc-connected ▶ irc-XXX ▶ irc-numeric ▶ irc-all - irc-XXX ▶ irc-numeric ▶ irc-all + irc-connected ▶ irc-nXXX ▶ irc-numeric ▶ irc-all + irc-nXXX ▶ irc-numeric ▶ irc-all irc-join ▶ irc-all irc-nick ▶ irc-all irc-part ▶ irc-all @@ -141,7 +141,7 @@ Possible message objects received by event handler: ### `irc-connected` ``` -irc-connected ▶ irc-001 ▶ irc-numeric ▶ irc-all +irc-connected ▶ irc-n001 ▶ irc-numeric ▶ irc-all ``` Triggered on `001` numeric IRC command that indicates we successfully @@ -256,7 +256,7 @@ Receives `IRC::Client::Message::Notice::Me` message object. ### `irc-numeric` ``` -irc-numeric ▶ irc-XXX ▶ irc-all +irc-numeric ▶ irc-nXXX ▶ irc-all ``` Triggered on numeric IRC commands. @@ -367,14 +367,14 @@ if possible. Receives `IRC::Client::Message::Unknown` message object. -### `irc-XXX` +### `irc-nXXX` **Note:*** `XXX` stands for a three-digit numeric code of the command that -triggered the event, for example `irc-001`. See `irc-numeric` for event trigger +triggered the event, for example `irc-n001`. See `irc-numeric` for event trigger that responds to all numerics. ``` -irc-XXX ▶ irc-numeric ▶ irc-all +irc-nXXX ▶ irc-numeric ▶ irc-all ``` Triggered on numeric IRC commands. diff --git a/examples/08-numeric-bot.p6 b/examples/08-numeric-bot.p6 new file mode 100644 index 0000000..c8edac9 --- /dev/null +++ b/examples/08-numeric-bot.p6 @@ -0,0 +1,32 @@ +use lib ; +use IRC::Client; + +.run with IRC::Client.new: + :nick + :host(%*ENV // 'irc.freenode.net') + :channels<#zofbot> + :2debug + :plugins(class :: does IRC::Client::Plugin { + my class NameLookup { has $.channel; has @.users; has $.e; } + has %.lookups of NameLookup; + + method irc-to-me ($e where /^ 'users in ' $=\S+/) { + my $channel = ~$; + return 'Look up of this channel is already in progress' + if %!lookups{$channel}; + + %!lookups{$channel} = NameLookup.new: :$channel :$e; + $.irc.send-cmd: 'NAMES', $channel; + Nil; + } + method irc-n353 ($e where so %!lookups{ $e.args[2] }) { + %!lookups{ $e.args[2] }.users.append: $e.args[3].words; + Nil; + } + method irc-n366 ($e where so %!lookups{ $e.args[1] }) { + my $lookup = %!lookups{ $e.args[1] }:delete; + $lookup.e.reply: "Users in $lookup.channel(): $lookup.users()[]"; + Nil; + } + + }.new) diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index 3879ac9..e5696b4 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -267,7 +267,10 @@ method !handle-event ($e) { $s.is-connected = True; take 'irc-connected'; } - take 'irc-' ~ $e.command, $event-name; + + # prefix numerics with 'n' as irc-\d+ isn't a valid identifier + take 'irc-' ~ ('n' if $e ~~ IRC::Client::Message::Numeric) + ~ $e.command, $event-name; } default { take $event-name } } -- cgit v1.1