diff options
author | Zoffix Znet <cpan@zoffix.com> | 2016-01-03 12:37:27 -0500 |
---|---|---|
committer | Zoffix Znet <cpan@zoffix.com> | 2016-01-03 12:37:27 -0500 |
commit | f5d28c34ffc14045bc49cd3cb22af108d23886bd (patch) | |
tree | fae964d431fc82eb77e0588179735740cc52c4a1 | |
parent | 83c4503293f96dac4ec36235605cdde38f9252d6 (diff) |
Add .respond method
-rw-r--r-- | Changes | 4 | ||||
-rw-r--r-- | README.md | 70 | ||||
-rw-r--r-- | lib/IRC/Client.pm6 | 128 |
3 files changed, 134 insertions, 68 deletions
@@ -4,7 +4,9 @@ Revision History for 'IRC::Client' Perl 6 Distribution [New Events] - Added `irc-to-me` event [New Methods] - - Added `notice` event + - Added `notice` method + - Added `respond` method + 2.002001 2016-01-01 - Fixed grammar parsing errors that ignored nicks/usernames with underscores @@ -29,9 +29,10 @@ IRC::Client - Extendable Internet Relay Chat client - [`plugins-essential`](#plugins-essential) - [`run`](#run) - [METHODS FOR PLUGINS](#methods-for-plugins) - - [`.ssay`](#ssay) - - [`.privmsg`](#privmsg) - [`.notice`](#notice) + - [`.privmsg`](#privmsg) + - [`.respond`](#respond) + - [`.ssay`](#ssay) - [INCLUDED PLUGINS](#included-plugins) - [IRC::Client::Plugin::Debugger](#ircclientplugindebugger) - [IRC::Client::Plugin::PingPong](#ircclientpluginpingpong) @@ -265,14 +266,13 @@ to the IRC server ends. You can make use of these `IRC::Client` methods in your plugins: -## `.ssay` +## `.notice` ```perl6 - $irc.ssay("Foo bar!"); + $irc.notice( 'Zoffix', 'Hallo!' ); ``` -Sends a message to the server, automatically appending `\r\n`. Mnemonic: -**s**erver **say**. - +Sends a `NOTICE` message specified in the second argument +to the user/channel specified as the first argument. ## `.privmsg` @@ -282,13 +282,61 @@ Sends a message to the server, automatically appending `\r\n`. Mnemonic: Sends a `PRIVMSG` message specified in the second argument to the user/channel specified as the first argument. -## `.notice` +## `.respond` ```perl6 - $irc.notice( 'Zoffix', 'Hallo!' ); + $irc.respond: + :where<#zofbot> + :what('Hallo how are you?!') + :how<privmsg> + :who<Zoffix> + ; ``` -Sends a `NOTICE` message specified in the second argument -to the user/channel specified as the first argument. +Generates a response based on the provided arguments, which are as follows: + +### `where` + +```perl6 + $irc.respond: :where<Zoffix> ... + $irc.respond: :where<#zofbot> ... +``` +**Mandatory**. Takes either a nickname or a channel name where to +send the message to. + +### `what` + +```perl6 + $irc.respond: :what('Hallo how are you?!') ... +``` +**Mandatory**. Takes a string, which is the message to be sent. + +### `how` + +```perl6 + $irc.respond: :how<privmsg> ... + $irc.respond: :how<notice> ... +``` +**Optional**. Specifies whether the message should be sent using +`PRIVMSG` or `NOTICE` IRC commands. **Valid values** are `privmsg` and `notice` +(case-insensitive). + +### `who` + +```perl6 + $irc.respond: :who<Zoffix> ... +``` +**Optional**. Takes a string with a nickname (which doesn't need to be +valid in any way). If the `where` argument is set to a name of a channel, +then the method will modify the `what` argument, by prepending +`$who, ` to it. + +## `.ssay` + +```perl6 + $irc.ssay("Foo bar!"); +``` +Sends a message to the server, automatically appending `\r\n`. Mnemonic: +**s**erver **say**. # INCLUDED PLUGINS diff --git a/lib/IRC/Client.pm6 b/lib/IRC/Client.pm6 index cbdb335..41bb45d 100644 --- a/lib/IRC/Client.pm6 +++ b/lib/IRC/Client.pm6 @@ -20,62 +20,6 @@ has @.plugins-essential = [ ]; has @!plugs = [|@!plugins-essential, |@!plugins]; -method run { - .irc-start-up: self for @!plugs.grep(*.^can: 'irc-start-up'); - - await IO::Socket::Async.connect( $!host, $!port ).then({ - $!sock = .result; - $.ssay("PASS $!password\n") if $!password.defined; - $.ssay("NICK $!nick\n"); - $.ssay("USER $!username $!username $!host :$!userreal\n"); - $.ssay("JOIN {@!channels[]}\n"); - - .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected'); - - # my $left-overs = ''; - react { - whenever $!sock.Supply :bin -> $buf is copy { - my $str = try $buf.decode: 'utf8'; - $str or $str = $buf.decode: 'latin-1'; - # $str ~= $left-overs; - $!debug and "[server {DateTime.now}] {$str}".put; - my $events = parse-irc $str; - for @$events -> $e { - self.handle-event: $e; - CATCH { warn .backtrace } - } - } - - CATCH { warn .backtrace } - } - - say "Closing connection"; - $!sock.close; - - # CATCH { warn .backtrace } - }); -} - -method ssay (Str:D $msg) { - $!debug and "{plug-name}$msg".put; - $!sock.print("$msg\n"); - self; -} - -method privmsg (Str $who, Str $what) { - my $msg = "PRIVMSG $who :$what\n"; - $!debug and "{plug-name}$msg".put; - $!sock.print("$msg\n"); - self; -} - -method notice (Str $who, Str $what) { - my $msg = "NOTICE $who :$what\n"; - $!debug and "{plug-name}$msg".put; - $!sock.print("$msg\n"); - self; -} - method handle-event ($e) { $e<pipe> = {}; @@ -128,6 +72,78 @@ method handle-event ($e) { } } +method notice (Str $who, Str $what) { + my $msg = "NOTICE $who :$what\n"; + $!debug and "{plug-name}$msg".put; + $!sock.print("$msg\n"); + self; +} + +method privmsg (Str $who, Str $what) { + my $msg = "PRIVMSG $who :$what\n"; + $!debug and "{plug-name}$msg".put; + $!sock.print("$msg\n"); + self; +} + +method respond ( + Str:D :$how = 'privmsg', + Str:D :$where is required, + Str:D :$what is required is copy, + Str:D :$who, +) { + $what = "$who, $what" if $who and $where ~~ /^<[#&]>/; + my $method = $how.fc eq 'PRIVMSG'.fc ?? 'privmsg' + !! $how.fc eq 'NOTICE'.fc ?? 'notice' + !! fail 'Unknown :$how specified. Use PRIVMSG or NOTICE'; + + self."$method"($where, $what); +} + +method run { + .irc-start-up: self for @!plugs.grep(*.^can: 'irc-start-up'); + + await IO::Socket::Async.connect( $!host, $!port ).then({ + $!sock = .result; + $.ssay("PASS $!password\n") if $!password.defined; + $.ssay("NICK $!nick\n"); + $.ssay("USER $!username $!username $!host :$!userreal\n"); + $.ssay("JOIN {@!channels[]}\n"); + + .irc-connected: self for @!plugs.grep(*.^can: 'irc-connected'); + + # my $left-overs = ''; + react { + whenever $!sock.Supply :bin -> $buf is copy { + my $str = try $buf.decode: 'utf8'; + $str or $str = $buf.decode: 'latin-1'; + # $str ~= $left-overs; + $!debug and "[server {DateTime.now}] {$str}".put; + my $events = parse-irc $str; + for @$events -> $e { + self.handle-event: $e; + CATCH { warn .backtrace } + } + } + + CATCH { warn .backtrace } + } + + say "Closing connection"; + $!sock.close; + + # CATCH { warn .backtrace } + }); +} + +method ssay (Str:D $msg) { + $!debug and "{plug-name}$msg".put; + $!sock.print("$msg\n"); + self; +} + +#### HELPER SUBS + sub plug-name { my $plug = callframe(3).file; my $cur = $?FILE; |