From f5d28c34ffc14045bc49cd3cb22af108d23886bd Mon Sep 17 00:00:00 2001 From: Zoffix Znet Date: Sun, 3 Jan 2016 12:37:27 -0500 Subject: Add .respond method --- Changes | 4 +- README.md | 70 ++++++++++++++++++++++++----- lib/IRC/Client.pm6 | 128 ++++++++++++++++++++++++++++++----------------------- 3 files changed, 134 insertions(+), 68 deletions(-) diff --git a/Changes b/Changes index b4c29e4..9bd7a8d 100644 --- a/Changes +++ b/Changes @@ -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 diff --git a/README.md b/README.md index 9960049..8e27d8f 100644 --- a/README.md +++ b/README.md @@ -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 + :who + ; ``` -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 ... + $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 ... + $irc.respond: :how ... +``` +**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 ... +``` +**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 = {}; @@ -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; -- cgit v1.1