| 1 | package POE::Component::IRC::Plugin::RSS::Headlines; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use POE::Component::Client::HTTP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use POE::Component::IRC::Plugin qw(:ALL); | ||
| 8 | use XML::RSS; | ||
| 9 | use HTTP::Request; | ||
| 10 | use vars qw($VERSION); | ||
| 11 | |||
| 12 | $VERSION = '1.08'; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $package = shift; | ||
| 16 | my %args = @_; | ||
| 17 | $args{lc $_} = delete $args{$_} for keys %args; | ||
| 18 | return bless \%args, $package; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub PCI_register { | ||
| 22 | my ($self,$irc) = @_; | ||
| 23 | $self->{irc} = $irc; | ||
| 24 | $irc->plugin_register( $self, 'SERVER', qw(spoof) ); | ||
| 25 | unless ( $self->{http_alias} ) { | ||
| 26 | $self->{http_alias} = join('-', 'ua-rss-headlines', $irc->session_id() ); | ||
| 27 | $self->{follow_redirects} ||= 2; | ||
| 28 | POE::Component::Client::HTTP->spawn( | ||
| 29 | Alias => $self->{http_alias}, | ||
| 30 | Timeout => 30, | ||
| 31 | FollowRedirects => $self->{follow_redirects}, | ||
| 32 | ); | ||
| 33 | } | ||
| 34 | $self->{session_id} = POE::Session->create( | ||
| 35 | object_states => [ | ||
| 36 | $self => [ qw(_shutdown _start _get_headline _response) ], | ||
| 37 | ], | ||
| 38 | )->ID(); | ||
| 39 | $poe_kernel->state( 'get_headline', $self ); | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub PCI_unregister { | ||
| 44 | my ($self,$irc) = splice @_, 0, 2; | ||
| 45 | $poe_kernel->state( 'get_headline' ); | ||
| 46 | $poe_kernel->call( $self->{session_id} => '_shutdown' ); | ||
| 47 | delete $self->{irc}; | ||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub _start { | ||
| 52 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 53 | $self->{session_id} = $_[SESSION]->ID(); | ||
| 54 | $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ ); | ||
| 55 | undef; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _shutdown { | ||
| 59 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 60 | $kernel->alarm_remove_all(); | ||
| 61 | $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ); | ||
| 62 | $kernel->call( $self->{http_alias} => 'shutdown' ); | ||
| 63 | undef; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get_headline { | ||
| 67 | my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION]; | ||
| 68 | $kernel->post( $self->{session_id}, '_get_headline', @_[ARG0..$#_] ); | ||
| 69 | undef; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _get_headline { | ||
| 73 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 74 | my %args; | ||
| 75 | if ( ref $_[ARG0] eq 'HASH' ) { | ||
| 76 | %args = %{ $_[ARG0] }; | ||
| 77 | } else { | ||
| 78 | %args = @_[ARG0..$#_]; | ||
| 79 | } | ||
| 80 | $args{lc $_} = delete $args{$_} for grep { !/^_/ } keys %args; | ||
| 81 | return unless $args{url}; | ||
| 82 | $args{irc_session} = $self->{irc}->session_id(); | ||
| 83 | $kernel->post( $self->{http_alias}, 'request', '_response', HTTP::Request->new( GET => $args{url} ), \%args ); | ||
| 84 | undef; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub _response { | ||
| 88 | my ($kernel,$self,$request,$response) = @_[KERNEL,OBJECT,ARG0,ARG1]; | ||
| 89 | my $args = $request->[1]; | ||
| 90 | my @params; | ||
| 91 | push @params, delete $args->{irc_session}, '__send_event'; | ||
| 92 | my $result = $response->[0]; | ||
| 93 | if ( $result->is_success ) { | ||
| 94 | my $str = $result->content; | ||
| 95 | my $rss = XML::RSS->new(); | ||
| 96 | eval { $rss->parse($str); }; | ||
| 97 | if ($@) { | ||
| 98 | push @params, 'irc_rssheadlines_error', $args, $@; | ||
| 99 | } else { | ||
| 100 | push @params, 'irc_rssheadlines_items', $args; | ||
| 101 | push @params, $_->{'title'} for @{ $rss->{'items'} }; | ||
| 102 | } | ||
| 103 | } else { | ||
| 104 | push @params, 'irc_rssheadlines_error', $args, $result->status_line; | ||
| 105 | } | ||
| 106 | $kernel->post( @params ); | ||
| 107 | undef; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | __END__ | ||
| 112 | |||
| 113 | =head1 NAME | ||
| 114 | |||
| 115 | POE::Component::IRC::Plugin::RSS::Headlines - A POE::Component::IRC plugin that provides RSS headline retrieval. | ||
| 116 | |||
| 117 | =head1 SYNOPSIS | ||
| 118 | |||
| 119 | use strict; | ||
| 120 | use warnings; | ||
| 121 | use POE qw(Component::IRC Component::IRC::Plugin::RSS::Headlines); | ||
| 122 | |||
| 123 | my $nickname = 'RSSHead' . $$; | ||
| 124 | my $ircname = 'RSSHead the Sailor Bot'; | ||
| 125 | my $ircserver = 'irc.perl.org'; | ||
| 126 | my $port = 6667; | ||
| 127 | my $channel = '#IRC.pm'; | ||
| 128 | my $rss_url = 'http://eekeek.org/jerkcity.cgi'; | ||
| 129 | |||
| 130 | my $irc = POE::Component::IRC->spawn( | ||
| 131 | nick => $nickname, | ||
| 132 | server => $ircserver, | ||
| 133 | port => $port, | ||
| 134 | ircname => $ircname, | ||
| 135 | debug => 0, | ||
| 136 | plugin_debug => 1, | ||
| 137 | options => { trace => 0 }, | ||
| 138 | ) or die "Oh noooo! $!"; | ||
| 139 | |||
| 140 | POE::Session->create( | ||
| 141 | package_states => [ | ||
| 142 | 'main' => [ qw(_start irc_001 irc_join irc_rssheadlines_items) ], | ||
| 143 | ], | ||
| 144 | ); | ||
| 145 | |||
| 146 | $poe_kernel->run(); | ||
| 147 | exit 0; | ||
| 148 | |||
| 149 | sub _start { | ||
| 150 | # Create and load our plugin | ||
| 151 | $irc->plugin_add( 'RSSHead' => | ||
| 152 | POE::Component::IRC::Plugin::RSS::Headlines->new() ); | ||
| 153 | |||
| 154 | $irc->yield( register => 'all' ); | ||
| 155 | $irc->yield( connect => { } ); | ||
| 156 | undef; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub irc_001 { | ||
| 160 | $irc->yield( join => $channel ); | ||
| 161 | undef; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub irc_join { | ||
| 165 | my ($kernel,$sender,$channel) = @_[KERNEL,SENDER,ARG1]; | ||
| 166 | print STDERR "$channel $rss_url\n"; | ||
| 167 | $kernel->yield( 'get_headline', { url => $rss_url, _channel => $channel } ); | ||
| 168 | undef; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub irc_rssheadlines_items { | ||
| 172 | my ($kernel,$sender,$args) = @_[KERNEL,SENDER,ARG0]; | ||
| 173 | my $channel = delete $args->{_channel}; | ||
| 174 | $kernel->post( $sender, 'privmsg', $channel, join(' ', @_[ARG1..$#_] ) ); | ||
| 175 | undef; | ||
| 176 | } | ||
| 177 | |||
| 178 | =head1 DESCRIPTION | ||
| 179 | |||
| 180 | POE::Component::IRC::Plugin::RSS::Headlines, is a L<POE::Component::IRC> plugin that provides | ||
| 181 | a mechanism for retrieving RSS headlines from given URLs. | ||
| 182 | |||
| 183 | =head1 CONSTRUCTOR | ||
| 184 | |||
| 185 | =over | ||
| 186 | |||
| 187 | =item C<new> | ||
| 188 | |||
| 189 | Creates a new plugin object. Takes the following optional arguments: | ||
| 190 | |||
| 191 | 'http_alias', you may provide the alias of an existing POE::Component::Client::HTTP | ||
| 192 | component that the plugin will use instead of spawning it's own; | ||
| 193 | 'follow_redirects', this argument is passed to PoCoCl::HTTP to inform it how to deal with | ||
| 194 | following redirects, default is 2; | ||
| 195 | |||
| 196 | =back | ||
| 197 | |||
| 198 | =head1 INPUT EVENTS | ||
| 199 | |||
| 200 | The plugin registers the following state handler within your session: | ||
| 201 | |||
| 202 | =over | ||
| 203 | |||
| 204 | =item C<get_headline> | ||
| 205 | |||
| 206 | Takes a hashref as an argument with the following keys: | ||
| 207 | |||
| 208 | 'url', the RSS based url to retrieve items for; | ||
| 209 | |||
| 210 | You may pass arbitary key/value pairs, but the keys must be prefixed with an underscore. | ||
| 211 | |||
| 212 | =back | ||
| 213 | |||
| 214 | =head1 OUTPUT | ||
| 215 | |||
| 216 | The following irc event is generated with the result of a 'get_headline' command: | ||
| 217 | |||
| 218 | =over | ||
| 219 | |||
| 220 | =item C<irc_rssheadlines_items> | ||
| 221 | |||
| 222 | Has the following parameters: | ||
| 223 | |||
| 224 | 'ARG0', the original hashref that was passed; | ||
| 225 | 'ARG1' .. $#_, RSS headline item titles; | ||
| 226 | |||
| 227 | =item C<irc_rssheadlines_error> | ||
| 228 | |||
| 229 | Has the following parameters: | ||
| 230 | |||
| 231 | 'ARG0', the original hashref that was passed; | ||
| 232 | 'ARG1', the error text; | ||
| 233 | |||
| 234 | =back | ||
| 235 | |||
| 236 | =head1 AUTHOR | ||
| 237 | |||
| 238 | Chris 'BinGOs' Williams | ||
| 239 | |||
| 240 | =head1 LICENSE | ||
| 241 | |||
| 242 | Copyright E<copy> Chris Williams | ||
| 243 | |||
| 244 | This module may be used, modified, and distributed under the same terms as Perl itself. Please see the license that came with your Perl distribution for details. | ||
| 245 | |||
| 246 | =head1 SEE ALSO | ||
| 247 | |||
| 248 | L<POE::Component::IRC> | ||
| 249 | |||