From b1e795877f2447bc634cbb4b269f9ac8bebd67f3 Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sun, 7 Aug 2016 22:58:46 -0400 Subject: Add `:alias` feature Closes #22 --- Changes | 3 +++ META6.json | 2 +- docs/02-event-reference.md | 14 ++++++++------ docs/03-method-reference.md | 19 +++++++++++++++++++ examples/02-trickster-bot.p6 | 1 + lib/IRC/Client.pm6 | 15 +++++++++++---- lib/IRC/Client/Server.pm6 | 1 + 7 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Changes b/Changes index e2915a7..0aabb3e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for IRC::Client +3.006001 2016-08-07 + - Add `:alias` feature (#22) + 3.005001 2016-08-07 - Make addressed regex more restrictive (#21) diff --git a/META6.json b/META6.json index 97991dc..3ea5c8e 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "perl" : "6.c", "name" : "IRC::Client", - "version" : "3.005001", + "version" : "3.006001", "description" : "Extendable Internet Relay Chat client", "depends" : [ ], diff --git a/docs/02-event-reference.md b/docs/02-event-reference.md index cb3de1d..ab4e702 100644 --- a/docs/02-event-reference.md +++ b/docs/02-event-reference.md @@ -83,9 +83,11 @@ irc-addressed ▶ irc-to-me ▶ irc-notice-channel ▶ irc-notice ▶ This event chain is triggered when the client is addressed in a channel either via a `PRIVMSG` or `NOTICE` IRC message. 'Addressed' means the message line -starts with the current nickname of the client, followed by `;` or `,` +starts with the current nickname of the client or one of its aliases, +followed by `;` or `,` characters, followed by any number of whitespace; or -in regex terms, matches `/^ $nick <[,:]> \s* /`. This prefix portion will be +in regex terms, matches `/^ [$nick | @aliases] <[,:]> \s* /`. +This prefix portion will be **stripped** from the actual message. Possible message objects received by event handler: @@ -140,8 +142,8 @@ irc-mentioned ▶ irc-notice-channel ▶ irc-notice ▶ irc-all This event chain is triggered when the client is mentioned in a channel either via a `PRIVMSG` or `NOTICE` IRC message. Being mentioned means the message -contains our nick delimited by word boundaries on both sides; or in regex -terms, matches `/ << $nick >> /`. +contains our nick or one of the aliases delimited by word boundaries on both +sides; or in regex terms, matches `/ << [$nick | @aliases] >> /`. Possible message objects received by event handler: * `IRC::Client::Message::Privmsg::Channel` @@ -306,8 +308,8 @@ irc-addressed ▶ irc-to-me ▶ irc-notice-channel ▶ irc-notice ▶ This event chain is triggered when the client is addressed in a channel via a `PRIVMSG` or `NOTICE` IRC message or receives a private or notice message directly. In cases where the trigger happened due to being addressed -in channel, the prefix used for addressing (nick + `,` or `.` + whitespace) -will be stripped from the message. +in channel, the prefix used for addressing (nick|aliases + `,` or `.` + +whitespace) will be stripped from the message. Possible message objects received by event handler: * `IRC::Client::Message::Privmsg::Channel` diff --git a/docs/03-method-reference.md b/docs/03-method-reference.md index 9c5f1fa..6963d64 100644 --- a/docs/03-method-reference.md +++ b/docs/03-method-reference.md @@ -354,6 +354,10 @@ the channel and the value is its password. A list of nicks the client uses on this server. If one nick is taken, next one in the list will be attempted to be used. +#### `.alias` + +A list of aliases on this server. + #### `.host` The host of the server. @@ -445,6 +449,7 @@ my $irc = IRC::Client.new: :password :channels<#perl #perl6 #rust-lang> :nick + :alias('foo', /b.r/) :username :userhost :userreal('Mah awesome bot!') @@ -515,8 +520,22 @@ generate three additional nicknames that have underscores appended If one of the given nicks is in use, the client will attempt to use the next one in the list. +##### `:alias` + **Defaults to:** `P6Bot` +```perl6 + :alias('foo', /b.r/) +``` + +A list of `Str` or `Regex` objects that in the context of +`irc-addressed`, `irc-to-me`, and `irc-mentioned` events will be used +as alternative nicks. In other words, specifying `'bot'` as alias will allow +you to address the bot using `bot` nick, regardless of the actual nick the +bot is currently using. + +**Defaults to:** empty list + ##### `:password` The server password to use. On some networks (like Freenode), the server diff --git a/examples/02-trickster-bot.p6 b/examples/02-trickster-bot.p6 index 574a797..64171a7 100644 --- a/examples/02-trickster-bot.p6 +++ b/examples/02-trickster-bot.p6 @@ -13,6 +13,7 @@ class BFF { method irc-to-me ($ where /'♥'/) { 'I ♥ YOU!' } } .run with IRC::Client.new: :nick + :alias('foo', /b.r/) :host(%*ENV // 'irc.freenode.net') :channels<#zofbot> :debug diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index 7dbadf9..34e6f54 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -41,6 +41,7 @@ submethod BUILD ( Str :$password, Str:D :$host = 'localhost', :$nick = ['P6Bot'], + :$alias = [], Str:D :$username = 'Perl6IRC', Str:D :$userhost = 'localhost', Str:D :$userreal = 'Perl6 IRC Client', @@ -50,7 +51,7 @@ submethod BUILD ( @!plugins = @$plugins; my %servers = %$servers; - my %all-conf = :$port, :$password, :$host, :$nick, + my %all-conf = :$port, :$password, :$host, :$nick, :$alias, :$username, :$userhost, :$userreal, :$channels; %servers = '_' => {} unless %servers; @@ -61,6 +62,7 @@ submethod BUILD ( :$label, :channels( @($conf // %all-conf) ), :nick[ |($conf // %all-conf) ], + :alias[ |($conf // %all-conf) ], |%( .map: { $_ => $conf{$_} // %all-conf{$_} } @@ -234,11 +236,16 @@ method !handle-event ($e) { my @events = flat gather { given $event-name { when 'irc-privmsg-channel' | 'irc-notice-channel' { - my $nick = $s.current-nick; - if $e.text.subst-mutate: /^ $nick <[,:]> \s* /, '' { + my $nick = $s.current-nick; + my @aliases = $s.alias; + if $e.text.subst-mutate: + /^ [ $nick | @aliases ] <[,:]> \s* /, '' + { take 'irc-addressed', ('irc-to-me' if $s.is-connected); } - elsif $e.text ~~ / << $nick >> / and $s.is-connected { + elsif $e.text ~~ / << [ $nick | @aliases ] >> / + and $s.is-connected + { take 'irc-mentioned'; } take $event-name, $event-name eq 'irc-privmsg-channel' diff --git a/lib/IRC/Client/Server.pm6 b/lib/IRC/Client/Server.pm6 index f438d6d..2ed5d69 100644 --- a/lib/IRC/Client/Server.pm6 +++ b/lib/IRC/Client/Server.pm6 @@ -2,6 +2,7 @@ unit class IRC::Client::Server; has @.channels where .all ~~ Str|Pair; has @.nick where .all ~~ Str; +has @.alias where .all ~~ Str|Regex; has Int $.port where 0 <= $_ <= 65535; has Str $.label; has Str $.host; -- cgit v1.1