Skip to content

Commit

Permalink
Fix numeric events being unsubscrabable due to incorrect identifiers
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
zoffixznet committed Oct 15, 2016
1 parent acd52a7 commit 4411e6f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
18 changes: 9 additions & 9 deletions docs/02-event-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions examples/08-numeric-bot.p6
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use lib <lib>;
use IRC::Client;

.run with IRC::Client.new:
:nick<MahBot>
:host(%*ENV<IRC_CLIENT_HOST> // '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 ' $<channel>=\S+/) {
my $channel = ~$<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)
5 changes: 4 additions & 1 deletion lib/IRC/Client.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
Expand Down

0 comments on commit 4411e6f

Please sign in to comment.