aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorZoffix Znet <cpan@zoffix.com>2016-07-29 19:21:20 -0400
committerZoffix Znet <cpan@zoffix.com>2016-07-29 19:21:20 -0400
commite3c619494601a8628b8a27bd855fa9b1506c0728 (patch)
tree5bce316ec49ba996b69e2d150cf075fc3ec5bd65 /examples
parentdb4b16e06ee14205a988263942524e1d754f8f64 (diff)
Add examples; fix up docs
Diffstat (limited to 'examples')
-rw-r--r--examples/01-uppercase-bot.p68
-rw-r--r--examples/02-trickster-bot.p619
-rw-r--r--examples/03-github-notifications.p638
-rw-r--r--examples/04-bash-bot.p626
-rw-r--r--examples/05-bash-bot-with-filter.p630
-rw-r--r--examples/06-multi-server.p623
-rw-r--r--examples/07-multi-server-message-forwarder.p637
-rw-r--r--examples/README.md14
-rw-r--r--examples/bot.pl623
9 files changed, 195 insertions, 23 deletions
diff --git a/examples/01-uppercase-bot.p6 b/examples/01-uppercase-bot.p6
new file mode 100644
index 0000000..d846538
--- /dev/null
+++ b/examples/01-uppercase-bot.p6
@@ -0,0 +1,8 @@
+use lib <lib>;
+use IRC::Client;
+.run with IRC::Client.new:
+ :nick<MahBot>
+ :host<irc.freenode.net>
+ :channels<#zofbot>
+ :debug
+ :plugins(class { method irc-to-me ($_) { .text.uc } })
diff --git a/examples/02-trickster-bot.p6 b/examples/02-trickster-bot.p6
new file mode 100644
index 0000000..178fcdd
--- /dev/null
+++ b/examples/02-trickster-bot.p6
@@ -0,0 +1,19 @@
+use lib <lib>;
+
+use IRC::Client;
+class Trickster {
+ multi method irc-to-me ($ where /time/) { DateTime.now }
+ multi method irc-to-me ($ where /temp \s+ $<temp>=\d+ $<unit>=[F|C]/) {
+ $<unit> eq 'F' ?? "That's {($<temp> - 32) × .5556}°C"
+ !! "That's { $<temp> × 1.8 + 32 }°F"
+ }
+}
+
+class BFF { method irc-to-me ($ where /'♥'/) { 'I ♥ YOU!' } }
+
+.run with IRC::Client.new:
+ :nick<MahBot>
+ :host<irc.freenode.net>
+ :channels<#zofbot>
+ :debug
+ :plugins(Trickster, BFF)
diff --git a/examples/03-github-notifications.p6 b/examples/03-github-notifications.p6
new file mode 100644
index 0000000..8dec6d2
--- /dev/null
+++ b/examples/03-github-notifications.p6
@@ -0,0 +1,38 @@
+use lib <lib>;
+
+use IRC::Client;
+use HTTP::Tinyish;
+use JSON::Fast;
+
+class GitHub::Notifications does IRC::Client::Plugin {
+ has Str:D $.token = %*ENV<GITHUB_TOKEN>;
+ has $!ua = HTTP::Tinyish.new;
+ constant $API_URL = 'https://api.github.com/notifications';
+
+ method irc-connected ($) {
+ start react {
+ whenever self!notification.grep(* > 0) -> $num {
+ $.irc.send: :where<Zoffix>
+ :text("You have $num unread notifications!")
+ :notice;
+ }
+ }
+ }
+
+ method !notification {
+ supply {
+ loop {
+ my $res = $!ua.get: $API_URL, :headers{ :Authorization("token $!token") };
+ $res<success> and emit +grep *.<unread>, |from-json $res<content>;
+ sleep $res<headers><X-Poll-Interval> || 60;
+ }
+ }
+ }
+}
+
+.run with IRC::Client.new:
+ :nick<MahBot>
+ :host<irc.freenode.net>
+ :channels<#zofbot>
+ :debug
+ :plugins(GitHub::Notifications.new)
diff --git a/examples/04-bash-bot.p6 b/examples/04-bash-bot.p6
new file mode 100644
index 0000000..d3aaa4c
--- /dev/null
+++ b/examples/04-bash-bot.p6
@@ -0,0 +1,26 @@
+use lib <lib>;
+
+use IRC::Client;
+use Mojo::UserAgent:from<Perl5>;
+
+class Bash {
+ constant $BASH_URL = 'http://bash.org/?random1';
+ constant $cache = Channel.new;
+ has $!ua = Mojo::UserAgent.new;
+
+ multi method irc-to-me ($ where /bash/) {
+ start $cache.poll or do { self!fetch-quotes; $cache.poll };
+ }
+
+ method !fetch-quotes {
+ $cache.send: $_
+ for $!ua.get($BASH_URL).res.dom.find('.qt').each».all_text.lines.join: ' ';
+ }
+}
+
+.run with IRC::Client.new:
+ :nick<MahBot>
+ :host<irc.freenode.net>
+ :channels<#zofbot>
+ :debug
+ :plugins(Bash.new);
diff --git a/examples/05-bash-bot-with-filter.p6 b/examples/05-bash-bot-with-filter.p6
new file mode 100644
index 0000000..f3f2c3b
--- /dev/null
+++ b/examples/05-bash-bot-with-filter.p6
@@ -0,0 +1,30 @@
+use lib <lib>;
+
+use IRC::Client;
+use Pastebin::Shadowcat;
+use Mojo::UserAgent:from<Perl5>;
+
+class Bash {
+ has @!quotes;
+ has $!ua = Mojo::UserAgent.new;
+ constant $BASH_URL = 'http://bash.org/?random1';
+
+ method irc-to-me ($ where /bash/) {
+ start self!fetch-quotes and @!quotes.shift;
+ }
+ method !fetch-quotes {
+ @!quotes ||= $!ua.get($BASH_URL).res.dom.find('.qt').each».all_text;
+ }
+}
+
+.run with IRC::Client.new:
+ :nick<MahBot>
+ :host<localhost>
+ :channels<#zofbot>
+ :debug
+ :plugins(Bash.new)
+ :filters(
+ -> $text where .lines > 1 || .chars > 300 {
+ Pastebin::Shadowcat.new.paste: $text.lines.join: "\n";
+ }
+ )
diff --git a/examples/06-multi-server.p6 b/examples/06-multi-server.p6
new file mode 100644
index 0000000..3a5f5fc
--- /dev/null
+++ b/examples/06-multi-server.p6
@@ -0,0 +1,23 @@
+use lib <lib>;
+
+use IRC::Client;
+
+class BFF {
+ method irc-to-me ($ where /'♥'/) { 'I ♥ YOU!' }
+}
+
+.run with IRC::Client.new:
+ :debug
+ :plugins(BFF)
+ :nick<MahBot>
+ :channels<#zofbot>
+ :servers(
+ freenode => %(
+ :host<irc.freenode.net>,
+ ),
+ local => %(
+ :nick<P6Bot>,
+ :channels<#zofbot #perl6>,
+ :host<localhost>,
+ )
+ )
diff --git a/examples/07-multi-server-message-forwarder.p6 b/examples/07-multi-server-message-forwarder.p6
new file mode 100644
index 0000000..06d07ec
--- /dev/null
+++ b/examples/07-multi-server-message-forwarder.p6
@@ -0,0 +1,37 @@
+use lib <lib>;
+
+use IRC::Client;
+
+class Messenger does IRC::Client::Plugin {
+ method irc-privmsg-channel ($e) {
+ for $.irc.servers.values -> $server {
+ for $server.channels -> $channel {
+ next if $server eq $e.server and $channel eq $e.channel;
+
+ $.irc.send: :$server, :where($channel), :text(
+ "$e.nick() over at $e.server.host()/$e.channel() says $e.text()"
+ );
+ }
+ }
+
+ $.irc.send: :where<Zoffix>
+ :text('I spread the messages!')
+ :server<local>;
+ }
+}
+
+.run with IRC::Client.new:
+ :debug
+ :plugins[Messenger.new]
+ :nick<MahBot>
+ :channels<#zofbot>
+ :servers{
+ freenode => %(
+ :host<irc.freenode.net>,
+ ),
+ local => %(
+ :nick<P6Bot>,
+ :channels<#zofbot #perl6>,
+ :host<localhost>,
+ )
+ }
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 0000000..49b9056
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,14 @@
+[[back to doc map]](../docs/README.md)
+
+# Examples
+
+These examples are covered in [the IRC::Client blog
+post](http://perl6.party/post/IRC-Client-Perl-6-Multi-Server-IRC-Module)
+
+* [Uppercasing bot](01-uppercase-bot.p6)
+* [Small bot with a couple of features](02-trickster-bot.p6)
+* [Bot to announce GitHub notifications](03-github-notifications.p6)
+* [Bash.org bot](04-bash-bot.p6)
+* [Bash.org bot with pastebin filter](05-bash-bot-with-filter.p6)
+* [Multi-server bot](06-multi-server.p6)
+* [Multi-server message forwarder bot](07-multi-server-message-forwarder.p6)
diff --git a/examples/bot.pl6 b/examples/bot.pl6
deleted file mode 100644
index e991a8e..0000000
--- a/examples/bot.pl6
+++ /dev/null
@@ -1,23 +0,0 @@
-use lib 'lib';
-use IRC::Client;
-
-class MyPlug does IRC::Client::Plugin {
- method irc-privmsg-channel ($msg where .text ~~ /^'say' \s+ $<cmd>=(.+)/ ) {
- $msg.reply: "How about: $<cmd>.uc()";
- }
-}
-
-my $irc = IRC::Client.new(
- :nick('IRCBot')
- :debug<2>
- :channels<#perl6 #perl7>
- # :host<irc.freenode.net>
- :port<6667>
- # :servers(
- # mine => { :port<5667> },
-
- # inspircd => { },
- # freenode => { :host<irc.freenode.net> },
- # )
- :plugins(MyPlug.new)
-).run;