| 1 | package POE::Component::IRC::Plugin::YouAreDoingItWrong; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | our $VERSION = '1.001003'; # VERSION | ||
| 7 | |||
| 8 | 1 | ▶ | use POE qw(Component::WWW::DoingItWrongCom::RandImage); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | use POE::Component::IRC::Plugin qw(:ALL); | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $package = shift; | ||
| 13 | my %args = @_; | ||
| 14 | $args{ lc $_ } = delete $args{ $_ } for keys %args; | ||
| 15 | |||
| 16 | # fill in the defaults | ||
| 17 | %args = ( | ||
| 18 | debug => 0, | ||
| 19 | auto => 1, | ||
| 20 | trigger => qr/^doing it wrong/i, | ||
| 21 | banned => [], | ||
| 22 | response_event => 'irc_you_are_doing_it_wrong_response', | ||
| 23 | |||
| 24 | %args, | ||
| 25 | ); | ||
| 26 | |||
| 27 | return bless \%args, $package; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _start { | ||
| 31 | my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; | ||
| 32 | $self->{_session_id} = $_[SESSION]->ID(); | ||
| 33 | $kernel->refcount_increment( $self->{_session_id}, __PACKAGE__ ); | ||
| 34 | |||
| 35 | $self->{poco} = POE::Component::WWW::DoingItWrongCom::RandImage->spawn( | ||
| 36 | debug => $self->{debug}, | ||
| 37 | ); | ||
| 38 | |||
| 39 | undef; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub PCI_register { | ||
| 43 | my ( $self, $irc ) = splice @_, 0, 2; | ||
| 44 | |||
| 45 | $self->{irc} = $irc; | ||
| 46 | |||
| 47 | $irc->plugin_register( $self, 'SERVER', qw(public) ); | ||
| 48 | |||
| 49 | $self->{_session_id} = POE::Session->create( | ||
| 50 | object_states => [ | ||
| 51 | $self => [ | ||
| 52 | qw( | ||
| 53 | _start | ||
| 54 | _shutdown | ||
| 55 | _pic | ||
| 56 | _get_pic | ||
| 57 | ) | ||
| 58 | ], | ||
| 59 | ], | ||
| 60 | )->ID; | ||
| 61 | |||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _shutdown { | ||
| 66 | my ($kernel, $self) = @_[ KERNEL, OBJECT ]; | ||
| 67 | $self->{poco}->shutdown; | ||
| 68 | $kernel->alarm_remove_all(); | ||
| 69 | $kernel->refcount_decrement( $self->{_session_id}, __PACKAGE__ ); | ||
| 70 | undef; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub PCI_unregister { | ||
| 74 | my $self = shift; | ||
| 75 | |||
| 76 | # Plugin is dying make sure our POE session does as well. | ||
| 77 | $poe_kernel->call( $self->{_session_id} => '_shutdown' ); | ||
| 78 | |||
| 79 | delete $self->{irc}; | ||
| 80 | |||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub S_public { | ||
| 85 | my ( $self, $irc ) = splice @_, 0, 2; | ||
| 86 | my $who = ${ $_[0] }; | ||
| 87 | my $channel = ${ $_[1] }->[0]; | ||
| 88 | my $message = ${ $_[2] }; | ||
| 89 | |||
| 90 | foreach my $ban_re ( @{ $self->{banned} || [] } ) { | ||
| 91 | return PCI_EAT_NONE | ||
| 92 | if $who =~ /$ban_re/; | ||
| 93 | } | ||
| 94 | |||
| 95 | my $my_nick = $irc->nick_name(); | ||
| 96 | my ($what) = $message =~ m/^\s*\Q$my_nick\E[\:\,\;\.]?\s*(.*)$/i; | ||
| 97 | |||
| 98 | return PCI_EAT_NONE | ||
| 99 | if !defined $what or !($what =~ s/$self->{trigger}//); | ||
| 100 | |||
| 101 | my ( $nick_to_address ) = $what =~ /(\S+)\s*$/; | ||
| 102 | |||
| 103 | warn "Got PUBLIC input: [ who => $who, channel => $channel, " | ||
| 104 | . "what => $what ]" | ||
| 105 | if $self->{debug}; | ||
| 106 | $poe_kernel->post( $self->{_session_id} => _get_pic => { | ||
| 107 | _what => $message, | ||
| 108 | _who => $who, | ||
| 109 | _channel => $channel, | ||
| 110 | _nick_to_address => $nick_to_address, | ||
| 111 | } | ||
| 112 | ); | ||
| 113 | |||
| 114 | return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _get_pic { | ||
| 118 | $_[OBJECT]->{poco}->fetch( { | ||
| 119 | event => '_pic', | ||
| 120 | %{ $_[ARG0] }, | ||
| 121 | }, | ||
| 122 | ); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub _pic { | ||
| 126 | my ( $kernel, $self, $result ) = @_[ KERNEL, OBJECT, ARG0 ]; | ||
| 127 | my $message; | ||
| 128 | if ( $result->{error} ) { | ||
| 129 | $message = "Error: $result->{error}"; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | $message = "You are doing it wrong: $result->{out}"; | ||
| 133 | } | ||
| 134 | |||
| 135 | $self->{irc}->send_event( | ||
| 136 | $self->{response_event} => { | ||
| 137 | pic => $result->{out}, | ||
| 138 | error => $result->{error}, | ||
| 139 | map { $_ => $result->{"_$_"} } | ||
| 140 | qw(who what channel nick_to_address) | ||
| 141 | } | ||
| 142 | ); | ||
| 143 | |||
| 144 | if ( $self->{auto} ) { | ||
| 145 | if ( defined $result->{_nick_to_address} ) { | ||
| 146 | $message = "$result->{_nick_to_address}, \l$message"; | ||
| 147 | } | ||
| 148 | |||
| 149 | $kernel->post( | ||
| 150 | $self->{irc} => privmsg => $result->{_channel} => $message | ||
| 151 | ); | ||
| 152 | } | ||
| 153 | |||
| 154 | undef; | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | |||
| 159 | __END__ | ||