From 55b8bd87c6f07d21de8b3037317b54c6a0374c86 Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sun, 3 Jan 2016 11:37:51 -0500 Subject: Add irc-to-me method --- Changes | 5 +++-- README.md | 19 +++++++++++++++++++ examples/bot.pl6 | 13 +++++++++++-- lib/IRC/Client.pm6 | 30 ++++++++++++++++++------------ 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/Changes b/Changes index 4ecf8b8..bb4b730 100644 --- a/Changes +++ b/Changes @@ -1,9 +1,10 @@ Revision History for 'IRC::Client' Perl 6 Distribution -2.002001 2016-01-01 +2.002001 2016-01-03 - Fix grammar parsing errors that ignored nicks/usernames with underscores and digit 0 (#8) - - Added server password support (#3) + - Add server password support (#3) + - Add `irc-to-me` method 2.001002 2015-12-30 - Minor doc updates and fixed missing prepreqs in META.info file diff --git a/README.md b/README.md index 4eac154..51fe0c2 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ IRC::Client - Extendable Internet Relay Chat client - [`irc-all-events`](#irc-all-events) - [`irc-privmsg-me`](#irc-privmsg-me) - [`irc-notice-me`](#irc-notice-me) + - [`irc-to-me`](#irc-to-me) - [`irc-unhandled`](#irc-unhandled) - [Contents of the parsed IRC message](#contents-of-the-parsed-irc-message) - [`command`](#command) @@ -390,6 +391,7 @@ response. method irc-connected ($irc) { ... } # once per server connection method irc-all-events ($irc, $e) { ... } + method irc-to-me ($irc, $e) { ... } method irc-privmsg-me ($irc, $e) { ... } method irc-notice-me ($irc, $e) { ... } ... # all other handlers for standard IRC commands @@ -442,6 +444,23 @@ explicitly return [`IRC_NOT_HANDLED`](#irc_not_handled), your client will stop handling ALL other messages *** +### `irc-to-me` + +```perl6 + method irc-to-me ($irc, $e, $where, $who) { + $irc.privmsg: $where, "$who, you talkin' to me?"; + } +``` +Triggered when: the IRC `PRIVMSG` command is received, where the recipient +is the client (as opposed to some channel); the `NOTICE` command is +received, or the `PRIVMSG` command is received, where the recipient is the +channel and the message begins with client's nickname (i.e. the client +was addressed in channel. Along with `IRC::Client` object and the event +hash contained in `$e`, this method also receives two additional positional +arguments: `$where`, which is where to send the response (will be the channel +name or the name of the user who sent the message); and `$who`, which is the +nickname of the user who sent the message. + ### `irc-privmsg-me` ```perl6 diff --git a/examples/bot.pl6 b/examples/bot.pl6 index 34528c7..524a74f 100644 --- a/examples/bot.pl6 +++ b/examples/bot.pl6 @@ -3,9 +3,18 @@ use lib 'lib'; use IRC::Client; use IRC::Client::Plugin::Debugger; +class IRC::Client::Plugin::AddressedPlugin is IRC::Client::Plugin { + method irc-addressed ($irc, $e, $where) { + $irc.privmsg: $where[0], "$where[1], you addressed me"; + } +} + my $irc = IRC::Client.new( :host :channels<#perl6bot #zofbot> :debug - :plugins( IRC::Client::Plugin::Debugger.new ) -).run; + :plugins( + IRC::Client::Plugin::Debugger.new, + IRC::Client::Plugin::AddressedPlugin.new + ) +).run; \ No newline at end of file diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index d003efb..bcc1c6e 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -77,6 +77,24 @@ method handle-event ($e) { return unless $res === IRC_NOT_HANDLED; } + my $nick = $!nick; + if ( ( $e eq 'PRIVMSG' and $e[0] eq $!nick ) + or ( $e eq 'NOTICE' and $e[0] eq $!nick ) + or ( $e eq 'PRIVMSG' + and $e[1] ~~ /:i ^ $nick <[,:]> \s+/ + ) + ) { + my $where = ($e, $e); + $where[0] = $e[0] + unless ( $e eq 'PRIVMSG' and $e[0] eq $!nick ) + or ( $e eq 'NOTICE' and $e[0] eq $!nick ); + + for @!plugs.grep(*.^can: 'irc-to-me') -> $p { + my $res = $p.irc-to-me(self, $e, |$where); + return unless $res === IRC_NOT_HANDLED; + } + } + if ( $e eq 'PRIVMSG' and $e[0] eq $!nick ) { for @!plugs.grep(*.^can: 'irc-privmsg-me') -> $p { my $res = $p.irc-privmsg-me(self, $e); @@ -91,18 +109,6 @@ method handle-event ($e) { } } - if ( ( $e eq 'PRIVMSG' and $e[0] eq $!nick ) - or ( $e eq 'NOTICE' and $e[0] eq $!nick ) - or ( $e eq 'PRIVMSG' - and $e[1] ~~ /:i ^ "$.nick" <[,:]> \s+/ - ) - ) { - for @!plugs.grep(*.^can: 'irc-addressed') -> $p { - my $res = $p.irc-notice-me(self, $e); - return unless $res === IRC_NOT_HANDLED; - } - } - my $cmd = 'irc-' ~ $e.lc; for @!plugs.grep(*.^can: $cmd) -> $p { my $res = $p."$cmd"(self, $e); -- cgit v1.1