use v6-alpha; use Net::IRC; # Parse @*ARGS my $nick = @*ARGS[0] // "blechbot"; my $server = @*ARGS[1] // "localhost"; my ($host, $port) = split ":", $server; $port //= 6667; my %seen; my @chans; # Create new bot "object" my $bot = new_bot(nick => $nick, host => $host, port => $port, debug_raw => 1); $bot(); $bot(); $bot("INVITE", &on_invite); $bot("PRIVMSG", &on_privmsg); $bot("loggedin", &on_loggedin); $bot(); sub pretty_duration ($seconds is copy) { return '0 seconds' unless $seconds; my %duration; %duration = int($seconds / (60*60*24)); $seconds = $seconds % (60*60*24) if %duration; %duration = int($seconds / (60*60)); $seconds = $seconds % (60*60) if %duration; %duration = int($seconds / 60); $seconds = $seconds % 60 if %duration; %duration = $seconds; my @pretty = (); for -> $key { push @pretty, "%duration{$key} $key" if %duration{$key}; } my $pretty = join(' ', grep { defined $_ }, @pretty[0..2]); debug "pretty duration is $pretty"; return $pretty; } sub on_loggedin($event) { $bot($_) for @chans; } sub on_invite($event) { my ($from, $chan) = $event; debug "Got an invitation from \"$from\" to join channel \"$chan\"."; $bot($chan); } sub on_privmsg($event) { %seen{$event} = { date => time, text => $event, }; given $event { debug "Received a ?-request from $event: $event" if substr($event, 0, 1) eq "?"; my $reply_to = substr($event, 0, 1) eq "#" ?? $event !! $event; when rx:P5/^\??seen\s+(.+?)\s*$/ { my $reply_msg = %seen{$0} ?? "$0 was last seen {pretty_duration(int(time() - %seen{$0}))} ago" ~ (%seen{$0} ?? ", saying: %seen{$0}" !! '.') !! "Never seen $0."; $bot(to => $reply_to, text => $reply_msg); } when rx:P5/^\?quit\s*(.*)$/ { $bot($0); } when rx:P5/^\?raw\s+(.+)$/ { $bot($0); } when rx:P5/^\?uptime$/ { my $start_time = INIT { time }; $bot(to => $reply_to, text => "Running for {int(time() - $start_time)} seconds."); } when rx:P5/^\?sleep\s+(\d+)$/ { sleep $0; } when rx:P5/^\?reconnect/ { @chans = $bot(); $bot(); $bot(); } # This is *not* correct CTCP parsing (the CTCP specification is much more # complex than this simple regex), but IRC clients only send this when # their users enter /PING bot. when rx:P5/^\x01(?:PING )(.*)\x01$/ { $bot(to => $event, text => "\x01PING $0\x01"); } } }