#!/usr/bin/perl # This bot reports the now playing on MPD. # Needs a command line version of mpc and MPCGet.pm use strict; use warnings; use POE qw(Component::IRC); use POE::Component::IRC::Plugin::BotTraffic; use MPCGet; my @namelist; my $nickname='NowPlaying'; my $ircname='Bot for MPD'; my $ircserver='my.ircserver.com'; my $port=6667; my @channels=('#lobby'); #Create a new PoCo-IRC object/component my $irc = POE::Component::IRC->spawn( nick => $nickname, server => $ircserver, port => $port, ircname => $ircname ) or die "Crap, not connected...site must be dead?"; $irc->plugin_add( 'BotTraffic', POE::Component::IRC::Plugin::BotTraffic->new() ); $irc->plugin_add( 'BotAddressed', POE::Component::IRC::Plugin::BotTraffic->new() ); #my $irc = POE::Component::IRC->spawn( # nick => $config{nickname}, # server => $config{ircserver}, # port => $config{port}, # ircname => $config{ircname} #) POE::Session->create( package_states => [ 'main' => [ qw(_default _start irc_001 irc_public) ], ], heap => { irc => $irc }, ); $poe_kernel->run(); exit 0; sub _start { my ($kernel,$heap) = @_[KERNEL,HEAP]; #Get session ID of component from object, register, #connect to specified server. my $irc_session = $heap->{irc}->session_id(); $kernel->post( $irc_session => register => 'all' ); $kernel->post( $irc_session => connect => { } ); undef; } sub irc_001 { my ($kernel,$sender) = @_[KERNEL,SENDER]; #Get component's object by accessing heap of SENDER my $poco_object = $sender->get_heap(); print "Connected to ",$poco_object->server_name(),"\n"; #In any irc_* events, the SENDER will be the PoCo IRC session $kernel->post( $sender => join => $_ ) for @channels; $kernel->post( $sender => names => $_ ) for @channels; # my ($nick) = ( split /!/, )[0]; # my ($channel) = '#lobby'; # my ($what)="Hi" # &irc_bot_addressed($nick,$channel,$what); undef; } sub irc_public { my ($kernel,$sender,$who,$where,$what) = @_[KERNEL,SENDER,ARG0,ARG1,ARG2]; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0]; if (my ($rot13) = $what =~ /^rot13 (.+)/ ) { $rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M]; $kernel->post( $sender => privmsg => $channel => "$nick: rot13" ); } my @dialogue=&irc_bot_addressed; my $command; my @args; ($command,@args) = split(/ /, @dialogue[2]); print @dialogue[2]; if ( $command eq '!talk'){ $kernel->post( $sender => privmsg => $channel => "@dialogue[0]: You said @dialogue[2]" ); } elsif ( $command =~ "!kick" ){ $kernel->post( $sender => kick => $channel => "@args[0]" => 'Stop Idling!' ); } elsif ($command eq '!np'){ print '!np\n'; my $np=MPCout(); $kernel->post( $sender => privmsg => $channel => "Currently playing: $np"); # } elsif ( $command =~ /![a-zA-Z_0-9]/ ) { # print "Unkown Command $command!\n"; # $kernel->post( $sender => privmsg => $channel => "@dialogue[0]: Unkown Command\!") ; } elsif ( $command eq "!ison" ) { if ($kernel->post( $sender => ison => $args[0] )){ print "\n\nPublic names: @namelist"; print "\n\n$args[0] is on\n\n"; } } undef; # irc_bot_public('#lobby','Hi'); # sub irc_bot_public { # my ($kernel,$heap) = @_[KERNEL,HEAP]; # my ($channel) = $_[ARG0][0]; # my ($what) = $_[ARG1]; # # print "I said $what on $channel\n"; # } } sub _default { my ( $event, $args ) = @_[ARG0 .. $#_]; my @output = ( "$event: " ); foreach my $arg ( @$args ) { if ( ref($arg) eq 'ARRAY' ) { push( @output, "[" . join(" ,", @$arg ) . "]" ); } else { push ( @output, "'$arg'" ); } } if ( $event eq 'irc_353' ) { @namelist = @$args; } print "Namelist is @namelist\n"; print STDOUT join ' ', @output, "\n"; return 0; } #sub irc_bot_public { # my ( $kernel,$heap ) = @_[KERNEL,HEAP]; # my ($channel) = $_[ARG0]->[0]; # my ($what) = $_[ARG1]; # # print "I said $what on $channel\n"; #} sub irc_bot_addressed { my ( $kernel,$heap ) = @_[KERNEL,HEAP]; my ($nick) = (split /!/, $_[ARG0] )[0]; my ($channel) = $_[ARG1][0]; my ($what) = $_[ARG2]; # $kernel->post( $sender => privmsg => $channel => "$nick: What do you want?" ); print "$nick addressed me in $channel with the message $what"; return ($nick,$channel,$what); }