Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from japhb/master
Debugging, refactoring, and some new functionality (including a CommandHandler role and the seen command)
- Loading branch information
Showing
9 changed files
with
202 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use v6; | ||
|
||
enum RequiredIntro is export < | ||
NONE | ||
NICK | ||
PREFIX | ||
EITHER | ||
BOTH | ||
>; | ||
|
||
role Net::IRC::CommandHandler { | ||
has Str $.prefix is rw = '!'; | ||
has RequiredIntro $.required-intro is rw = EITHER; | ||
|
||
method recognized($handler: $ev) { | ||
return $ev.cache<CommandHandler>{$handler.prefix} //= (gather { | ||
$ev.what ~~ token { | ||
# Intro | ||
^ | ||
[ \s* $<nick>=("$ev.state()<nick>") [ <[':' ',']> | \s ] ]? \s* | ||
$<prefix>=("$handler.prefix()"?) \s* | ||
|
||
# Actual command (and optional params) | ||
$<command>=(\w+) [ <?> | \s+ $<params>=(.*) ] | ||
$ | ||
} or take False; | ||
|
||
given $.required-intro { | ||
when NICK { take False unless $<nick> } | ||
when PREFIX { take False unless $<prefix> } | ||
when EITHER { take False unless $<prefix> || $<nick> } | ||
when BOTH { take False unless $<prefix> && $<nick> } | ||
} | ||
|
||
take $/; | ||
})[0]; | ||
} | ||
|
||
multi method said ($ev where { $/ := $.recognized($ev) }) { | ||
self.*"command_$<command>"($ev, $/); | ||
} | ||
|
||
method usage($ev, $usage) { | ||
$ev.msg("Usage: $usage"); | ||
} | ||
} | ||
|
||
# vim: ft=perl6 tabstop=4 shiftwidth=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use v6; | ||
use Net::IRC::CommandHandler; | ||
use Net::IRC::TextUtil; | ||
|
||
class Net::IRC::Modules::Seen does Net::IRC::CommandHandler { | ||
class Seen { | ||
has $.when = now; | ||
has $.what; | ||
has $.how; | ||
} | ||
has %!seen; | ||
|
||
multi method said ( $ev ) { | ||
%!seen{~$ev.who} := Seen.new(:how('saying:'), :what(~$ev.what)); | ||
} | ||
|
||
multi method emoted ( $ev ) { | ||
%!seen{~$ev.who} := Seen.new(:how("emoting: * $ev.who()"), :what(~$ev.what)); | ||
} | ||
|
||
multi method kicked ( $ev ) { | ||
%!seen{~$ev.what} := Seen.new(:how('being kicked from'), :what(~$ev.where)); | ||
} | ||
|
||
multi method joined ( $ev ) { | ||
%!seen{~$ev.who} := Seen.new(:how('joining'), :what(~$ev.where)); | ||
} | ||
|
||
multi method nickchange ( $ev ) { | ||
%!seen{~$ev.who} := Seen.new(:how('changing nick to'), :what(~$ev.what)); | ||
%!seen{~$ev.what} := Seen.new(:how('changing nick from'), :what(~$ev.who)); | ||
} | ||
|
||
|
||
method command_seen ( $ev, $/ ) { | ||
my @params = ($<params> // '').comb(/\S+/); | ||
if @params { | ||
for @params -> $nick { | ||
if %!seen{$nick} -> $seen { | ||
my $dt = DateTime.new($seen.when); | ||
my $stamp = $dt.Str.subst('T', ' '); | ||
my $seconds = now - $seen.when; | ||
my $elapsed = friendly-duration($seconds); | ||
|
||
$ev.msg("$nick was last seen at $stamp ($elapsed ago) $seen.how() $seen.what()"); | ||
} | ||
else { | ||
$ev.msg("I haven't seen $nick."); | ||
} | ||
} | ||
} | ||
else { | ||
self.usage($ev, 'seen <nick> [<nick> ...]'); | ||
} | ||
} | ||
} | ||
|
||
# vim: ft=perl6 tabstop=4 shiftwidth=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use v6; | ||
|
||
module Net::IRC::TextUtil; | ||
|
||
sub _s($num, $plural = 's', $singular = '') is export { | ||
$num == 1 ?? $singular !! $plural; | ||
} | ||
|
||
sub friendly-duration($seconds) is export { | ||
my $minute = 60; | ||
my $hour = $minute * 60; | ||
my $day = $hour * 24; | ||
my $week = $day * 7; | ||
|
||
given $seconds.Int { | ||
when * < $minute { | ||
"$_ second{_s($_)}" | ||
} | ||
when * < 59.5 * $minute { | ||
my $minutes = ($_ / $minute).round; | ||
"$minutes minute{_s($minutes)}" | ||
} | ||
when * < 23.5 * $hour { | ||
my $hours = ($_ / $hour).round; | ||
"$hours hour{_s($hours)}" | ||
} | ||
when * < 6.5 * $day { | ||
my $days = ($_ / $day).round; | ||
"$days day{_s($days)}" | ||
} | ||
default { | ||
my $weeks = ($_ / $week).round; | ||
"$weeks week{_s($weeks)}" | ||
} | ||
} | ||
} | ||
|
||
# vim: ft=perl6 tabstop=4 shiftwidth=4 |