| 1 | package POE::Component::Client::NNTP::Tail; | ||||||||||||
| 2 | 3 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | # ABSTRACT: Sends events for new articles posted to an NNTP newsgroup | ||||||||||||
| 6 | 3 | our $VERSION = '0.03'; # VERSION | |||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | use Carp::POE; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Params::Validate; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use POE qw(Component::Client::NNTP); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 3 | my %spawn_args = ( | |||||||||||
| 13 | # required | ||||||||||||
| 14 | Group => 1, | ||||||||||||
| 15 | NNTPServer => 1, | ||||||||||||
| 16 | # optional with defaults | ||||||||||||
| 17 | Interval => { default => 60 }, | ||||||||||||
| 18 | # purely optional | ||||||||||||
| 19 | Port => 0, | ||||||||||||
| 20 | LocalAddr => 0, | ||||||||||||
| 21 | Alias => 0, | ||||||||||||
| 22 | Debug => 0, | ||||||||||||
| 23 | ); | ||||||||||||
| 24 | |||||||||||||
| 25 | sub spawn { | ||||||||||||
| 26 | 2 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
spawn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 27 | 2 | my %opts = validate( @_, \%spawn_args ); | |||||||||||
| 28 | |||||||||||||
| 29 | 2 | POE::Session->create( | |||||||||||
| 30 | heap => \%opts, | ||||||||||||
| 31 | package_states => [ | ||||||||||||
| 32 | # nntp component events | ||||||||||||
| 33 | $class => { | ||||||||||||
| 34 | nntp_connected => '_nntp_connected', | ||||||||||||
| 35 | nntp_registered => '_nntp_registered', | ||||||||||||
| 36 | nntp_socketerr => '_nntp_socketerr', | ||||||||||||
| 37 | nntp_disconnected => '_nntp_disconnected', | ||||||||||||
| 38 | nntp_200 => '_nntp_server_ready', | ||||||||||||
| 39 | nntp_201 => '_nntp_server_ready', | ||||||||||||
| 40 | nntp_211 => '_nntp_group_selected', | ||||||||||||
| 41 | nntp_220 => '_nntp_got_article', | ||||||||||||
| 42 | nntp_221 => '_nntp_got_head', | ||||||||||||
| 43 | nntp_411 => '_nntp_no_group', | ||||||||||||
| 44 | nntp_423 => '_nntp_no_article', | ||||||||||||
| 45 | nntp_430 => '_nntp_no_article', | ||||||||||||
| 46 | }, | ||||||||||||
| 47 | # session events | ||||||||||||
| 48 | $class => [ qw( _start _stop _child ) ], | ||||||||||||
| 49 | # internal events | ||||||||||||
| 50 | $class => [ qw( _poll _reconnect ) ], | ||||||||||||
| 51 | # API events | ||||||||||||
| 52 | $class => [ qw( register unregister get_article shutdown ) ], | ||||||||||||
| 53 | ], | ||||||||||||
| 54 | ); | ||||||||||||
| 55 | } | ||||||||||||
| 56 | |||||||||||||
| 57 | sub _debug { | ||||||||||||
| 58 | 0 | ▶ | my $where = (caller(1))[3]; | ||||||||||
|
Subroutine
_debug: not called CC=4 SCAR=30.0
| |||||||||||||
| 59 | 0 | $where =~ s{.*::}{P::C::C::N::T::}; | |||||||||||
| 60 | 0 | my @args = @_[ARG0 .. $#_]; | |||||||||||
| 61 | 0 | for ( @args ) { | |||||||||||
| 62 | 0 | ▶ | $_ = 'undef' if not defined $_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | } | ||||||||||||
| 64 | 0 | ▶ | my $args = @args ? join( " " => "", (map { "'$_'" } @args), "" ) : ""; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 65 | 0 | warn "$where->($args)\n"; | |||||||||||
| 66 | 0 | return; | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | #--------------------------------------------------------------------------# | ||||||||||||
| 70 | # session events | ||||||||||||
| 71 | #--------------------------------------------------------------------------# | ||||||||||||
| 72 | |||||||||||||
| 73 | sub _start { | ||||||||||||
| 74 | 2 | ▶ | my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP]; | ||||||||||
|
Subroutine
_start: called CC=5 SCAR=16.1
| |||||||||||||
| 75 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | |||||||||||||
| 77 | # alias defaults to group name if not otherwise set | ||||||||||||
| 78 | 2 | ▶ | $heap->{Alias} = $heap->{Group} unless exists $heap->{Alias}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 2 | $kernel->alias_set($heap->{Alias}); | |||||||||||
| 80 | |||||||||||||
| 81 | # setup NNTP including optional args if defined; | ||||||||||||
| 82 | 2 | my %nntp_args; | |||||||||||
| 83 | 2 | for my $k ( qw/NNTPServer Port LocalAddr/ ) { | |||||||||||
| 84 | 6 | ▶ | $nntp_args{$k} = $heap->{$k} if exists $heap->{$k}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | } | ||||||||||||
| 86 | 2 | my $alias = "NNTP-Client-" . $session->ID; | |||||||||||
| 87 | 2 | $heap->{nntp} = POE::Component::Client::NNTP->spawn($alias,\%nntp_args); | |||||||||||
| 88 | 2 | $heap->{nntp_id} = $heap->{nntp}->session_id; | |||||||||||
| 89 | |||||||||||||
| 90 | # start NNTP connection | ||||||||||||
| 91 | 2 | $kernel->yield( '_reconnect' ); | |||||||||||
| 92 | 2 | return; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | # ignore these | ||||||||||||
| 96 | sub _child { | ||||||||||||
| 97 | 4 | ▶ | my ( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||||||||||
|
Subroutine
_child: called CC=2 SCAR=7.2
| |||||||||||||
| 98 | 4 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | sub _stop { | ||||||||||||
| 102 | 2 | ▶ | my ( $kernel, $session, $heap ) = @_[KERNEL, SESSION, HEAP]; | ||||||||||
|
Subroutine
_stop: called CC=3 SCAR=11.4
| |||||||||||||
| 103 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 2 | ▶ | $kernel->call( $session, 'shutdown' ) if $heap->{nntp}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 105 | 2 | return; | |||||||||||
| 106 | } | ||||||||||||
| 107 | |||||||||||||
| 108 | #--------------------------------------------------------------------------# | ||||||||||||
| 109 | # events from our clients | ||||||||||||
| 110 | #--------------------------------------------------------------------------# | ||||||||||||
| 111 | |||||||||||||
| 112 | #--------------------------------------------------------------------------# | ||||||||||||
| 113 | # register -- [EVENT] | ||||||||||||
| 114 | # | ||||||||||||
| 115 | # EVENT - event to dispatch to the registered session on receipt of new | ||||||||||||
| 116 | # headers; defaults to "new_header" | ||||||||||||
| 117 | #--------------------------------------------------------------------------# | ||||||||||||
| 118 | |||||||||||||
| 119 | sub register { | ||||||||||||
| 120 | 2 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
register: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 121 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 2 | ▶ | my ($event) = $_[ARG0] || 'new_header'; | ||||||||||
|
Condition100%2/2
$_[10] || 'new_header'
MC/DC100%1/1
$_[10] || 'new_header'
$_[10]
Truth table100%2/2
A
A= $_[10]
| |||||||||||||
| 123 | 2 | $kernel->refcount_increment( $sender->ID, __PACKAGE__ ); | |||||||||||
| 124 | 2 | $heap->{listeners}{$sender->ID} = $event; | |||||||||||
| 125 | 2 | return; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | #--------------------------------------------------------------------------# | ||||||||||||
| 129 | # unregister -- | ||||||||||||
| 130 | # | ||||||||||||
| 131 | # removes sender registration | ||||||||||||
| 132 | #--------------------------------------------------------------------------# | ||||||||||||
| 133 | |||||||||||||
| 134 | sub unregister { | ||||||||||||
| 135 | 2 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
unregister: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 136 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | 2 | $kernel->refcount_decrement( $sender->ID, __PACKAGE__ ); | |||||||||||
| 138 | 2 | delete $heap->{listeners}{$sender->ID}; | |||||||||||
| 139 | 2 | return; | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | #--------------------------------------------------------------------------# | ||||||||||||
| 143 | # get_article -- ARTICLE_ID, EVENT | ||||||||||||
| 144 | # | ||||||||||||
| 145 | # request ARTICLE_ID be retrieved and returned via EVENT or 'got_article | ||||||||||||
| 146 | # if not specified | ||||||||||||
| 147 | #--------------------------------------------------------------------------# | ||||||||||||
| 148 | |||||||||||||
| 149 | sub get_article { | ||||||||||||
| 150 | 8 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
get_article: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 151 | 8 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | 8 | my ($article_id, $return_event) = @_[ARG0, ARG1]; | |||||||||||
| 153 | 8 | ▶ | $return_event ||= 'got_article'; | ||||||||||
|
Condition100%2/2
$return_event ||= "got_article"
MC/DC100%1/1
$return_event ||= "got_article"
$return_event
Truth table100%2/2
A
A= $return_event
| |||||||||||||
| 154 | # store requesting session and desired return event | ||||||||||||
| 155 | 8 | push @{$heap->{requests}{$article_id}}, [$sender, $return_event]; | |||||||||||
| 156 | 8 | $kernel->post( $heap->{nntp_id} => article => $article_id ); | |||||||||||
| 157 | 8 | return; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | #--------------------------------------------------------------------------# | ||||||||||||
| 161 | # shudown | ||||||||||||
| 162 | #--------------------------------------------------------------------------# | ||||||||||||
| 163 | |||||||||||||
| 164 | sub shutdown { | ||||||||||||
| 165 | 2 | ▶ | my ( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||||||||||
|
Subroutine
shutdown: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||
| 166 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | # unregister anyone that didn't do it themselves | ||||||||||||
| 168 | 2 | for my $listener ( keys %{ $heap->{listeners} } ) { | |||||||||||
| 169 | 0 | $kernel->refcount_decrement( $listener, __PACKAGE__ ); | |||||||||||
| 170 | 0 | delete $heap->{listeners}{$listener}; | |||||||||||
| 171 | } | ||||||||||||
| 172 | 2 | $kernel->alarm_remove_all(); | |||||||||||
| 173 | 2 | $kernel->call( $heap->{nntp_id} => 'unregister' => 'all' ); | |||||||||||
| 174 | 2 | $kernel->call( $heap->{nntp_id} => 'shutdown' ); | |||||||||||
| 175 | 2 | delete $heap->{nntp}; | |||||||||||
| 176 | 2 | $kernel->alias_remove($_) for $kernel->alias_list; | |||||||||||
| 177 | 2 | return; | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | #--------------------------------------------------------------------------# | ||||||||||||
| 181 | # our internal events | ||||||||||||
| 182 | #--------------------------------------------------------------------------# | ||||||||||||
| 183 | |||||||||||||
| 184 | # if connected, check for new messages, otherwise reconnect | ||||||||||||
| 185 | sub _poll { | ||||||||||||
| 186 | 10 | ▶ | my ( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||||||||||
|
Subroutine
_poll: called CC=3 SCAR=11.6
| |||||||||||||
| 187 | 10 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | 10 | ▶ | if ( $heap->{connected} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 189 | 10 | $kernel->post( $heap->{nntp_id} => group => $heap->{Group} ); | |||||||||||
| 190 | } | ||||||||||||
| 191 | else { | ||||||||||||
| 192 | 0 | $kernel->yield( '_reconnect' ); | |||||||||||
| 193 | } | ||||||||||||
| 194 | 10 | $kernel->delay( '_poll' => $heap->{Interval} ); | |||||||||||
| 195 | 10 | return; | |||||||||||
| 196 | } | ||||||||||||
| 197 | |||||||||||||
| 198 | # connect to NNTP server | ||||||||||||
| 199 | sub _reconnect { | ||||||||||||
| 200 | 2 | ▶ | my ( $kernel, $heap ) = @_[KERNEL, HEAP]; | ||||||||||
|
Subroutine
_reconnect: called CC=2 SCAR=7.0
| |||||||||||||
| 201 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | 2 | $kernel->post( $heap->{nntp_id} => 'connect' ); | |||||||||||
| 203 | 2 | return; | |||||||||||
| 204 | } | ||||||||||||
| 205 | |||||||||||||
| 206 | #--------------------------------------------------------------------------# | ||||||||||||
| 207 | # events from NNTP client | ||||||||||||
| 208 | #--------------------------------------------------------------------------# | ||||||||||||
| 209 | |||||||||||||
| 210 | # ignore event | ||||||||||||
| 211 | sub _nntp_registered { | ||||||||||||
| 212 | 2 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_registered: called CC=2 SCAR=7.1
| |||||||||||||
| 213 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 214 | 2 | return; | |||||||||||
| 215 | } | ||||||||||||
| 216 | |||||||||||||
| 217 | # ignore event | ||||||||||||
| 218 | sub _nntp_connected { | ||||||||||||
| 219 | 2 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_connected: called CC=2 SCAR=7.1
| |||||||||||||
| 220 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 2 | return; | |||||||||||
| 222 | } | ||||||||||||
| 223 | |||||||||||||
| 224 | # if connection can't be made, wait for next poll period to try again | ||||||||||||
| 225 | sub _nntp_socketerr { | ||||||||||||
| 226 | 0 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_socketerr: not called CC=2 SCAR=17.9
| |||||||||||||
| 227 | 0 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 228 | 0 | my ($error) = $_[ARG0]; | |||||||||||
| 229 | 0 | warn "Socket error: $error\n"; | |||||||||||
| 230 | 0 | $heap->{connected} = 0; | |||||||||||
| 231 | 0 | $kernel->delay( '_reconnect' => $heap->{Interval} ); | |||||||||||
| 232 | 0 | return; | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | # if we time-out, just note it and wait for next poll to reconnect | ||||||||||||
| 236 | sub _nntp_disconnected { | ||||||||||||
| 237 | 0 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_disconnected: not called CC=2 SCAR=17.9
| |||||||||||||
| 238 | 0 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 239 | 0 | $heap->{connected} = 0; | |||||||||||
| 240 | 0 | return; | |||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | # once connected, start polling loop | ||||||||||||
| 244 | sub _nntp_server_ready { | ||||||||||||
| 245 | 2 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_server_ready: called CC=2 SCAR=7.0
| |||||||||||||
| 246 | 2 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 247 | 2 | $heap->{connected} = 1; | |||||||||||
| 248 | 2 | $kernel->yield( '_poll' ); | |||||||||||
| 249 | 2 | undef; | |||||||||||
| 250 | } | ||||||||||||
| 251 | |||||||||||||
| 252 | # if the group doesn't exist, then we shut ourselves down | ||||||||||||
| 253 | sub _nntp_no_group { | ||||||||||||
| 254 | 0 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_no_group: not called CC=2 SCAR=17.9
| |||||||||||||
| 255 | 0 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 256 | 0 | warn "No such newsgroup $heap->{Group}"; | |||||||||||
| 257 | 0 | $kernel->yield( 'shutdown' ); | |||||||||||
| 258 | 0 | return; | |||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | # if the article doesn't exist, warn about it | ||||||||||||
| 262 | sub _nntp_no_article { | ||||||||||||
| 263 | 0 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_no_article: not called CC=2 SCAR=17.9
| |||||||||||||
| 264 | 0 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 265 | 0 | warn "Couldnt find article in $heap->{Group}\n"; | |||||||||||
| 266 | 0 | return; | |||||||||||
| 267 | } | ||||||||||||
| 268 | |||||||||||||
| 269 | # if there are new articles, request their headers | ||||||||||||
| 270 | # also schedules the next check | ||||||||||||
| 271 | sub _nntp_group_selected { | ||||||||||||
| 272 | 10 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_group_selected: called CC=5 SCAR=16.2
| |||||||||||||
| 273 | 10 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 274 | 10 | my ($estimate,$first,$last,$group) = split( /\s+/, $_[ARG0] ); | |||||||||||
| 275 | |||||||||||||
| 276 | # first time, we won't know last_article, so skip to the end | ||||||||||||
| 277 | 10 | ▶ | if ( exists $heap->{last_article} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 278 | # fetch new headers or articles only if people are listening | ||||||||||||
| 279 | 8 | for my $article_id ( $heap->{last_article} + 1 .. $last ) { | |||||||||||
| 280 | 8 | ▶ | if ( scalar keys %{ $heap->{listeners} } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 281 | 8 | $kernel->post( $sender => head => $article_id ); | |||||||||||
| 282 | } | ||||||||||||
| 283 | } | ||||||||||||
| 284 | } | ||||||||||||
| 285 | 10 | $heap->{last_article} = $last; | |||||||||||
| 286 | 10 | $kernel->delay( '_poll' => $heap->{Interval} ); | |||||||||||
| 287 | 10 | return; | |||||||||||
| 288 | } | ||||||||||||
| 289 | |||||||||||||
| 290 | # notify listeners of new header | ||||||||||||
| 291 | sub _nntp_got_head { | ||||||||||||
| 292 | 8 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_got_head: called CC=3 SCAR=11.0
| |||||||||||||
| 293 | 8 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 294 | 8 | my ($response, $lines) = @_[ARG0, ARG1]; | |||||||||||
| 295 | 8 | my ($article_id) = split " ", $response; | |||||||||||
| 296 | 8 | for my $who ( keys %{ $heap->{listeners} } ) { | |||||||||||
| 297 | 8 | $kernel->post( $who => $heap->{listeners}{$who} => $article_id, $lines ); | |||||||||||
| 298 | } | ||||||||||||
| 299 | 8 | return; | |||||||||||
| 300 | } | ||||||||||||
| 301 | |||||||||||||
| 302 | # return article to request queue | ||||||||||||
| 303 | sub _nntp_got_article { | ||||||||||||
| 304 | 8 | ▶ | my ($kernel, $heap, $sender) = @_[KERNEL, HEAP, SENDER]; | ||||||||||
|
Subroutine
_nntp_got_article: called CC=3 SCAR=11.0
| |||||||||||||
| 305 | 8 | ▶ | &_debug if $heap->{Debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 306 | 8 | my ($response, $lines) = @_[ARG0, ARG1]; | |||||||||||
| 307 | 8 | my ($article_id) = split " ", $response; | |||||||||||
| 308 | # dispatch for all entries in the request queue for this article | ||||||||||||
| 309 | 8 | for my $request ( @{$heap->{requests}{$article_id}} ) { | |||||||||||
| 310 | 8 | my ($who, $event) = @$request; | |||||||||||
| 311 | 8 | $kernel->post( $who, $event, $article_id, $lines ); | |||||||||||
| 312 | } | ||||||||||||
| 313 | # clear the request queue | ||||||||||||
| 314 | 8 | delete $heap->{requests}{$article_id}; | |||||||||||
| 315 | 8 | return; | |||||||||||
| 316 | } | ||||||||||||
| 317 | |||||||||||||
| 318 | 3 | 1; | |||||||||||
| 319 | |||||||||||||
| 320 | |||||||||||||
| 321 | |||||||||||||
| 322 | =pod | ||||||||||||
| 323 | |||||||||||||
| 324 | =head1 NAME | ||||||||||||
| 325 | |||||||||||||
| 326 | POE::Component::Client::NNTP::Tail - Sends events for new articles posted to an NNTP newsgroup | ||||||||||||
| 327 | |||||||||||||
| 328 | =head1 VERSION | ||||||||||||
| 329 | |||||||||||||
| 330 | version 0.03 | ||||||||||||
| 331 | |||||||||||||
| 332 | =head1 SYNOPSIS | ||||||||||||
| 333 | |||||||||||||
| 334 | use POE qw( Component::Client::NNTP::Tail ); | ||||||||||||
| 335 | use Email::Simple; | ||||||||||||
| 336 | |||||||||||||
| 337 | POE::Component::Client::NNTP::Tail->spawn( | ||||||||||||
| 338 | NNTPServer => 'nntp.perl.org', | ||||||||||||
| 339 | Group => 'perl.cpan.testers', | ||||||||||||
| 340 | ); | ||||||||||||
| 341 | |||||||||||||
| 342 | POE::Session->create( | ||||||||||||
| 343 | package_states => [ | ||||||||||||
| 344 | main => [qw(_start new_header got_article)] | ||||||||||||
| 345 | ], | ||||||||||||
| 346 | ); | ||||||||||||
| 347 | |||||||||||||
| 348 | POE::Kernel->run; | ||||||||||||
| 349 | |||||||||||||
| 350 | # register for NNTP tail events | ||||||||||||
| 351 | sub _start { | ||||||||||||
| 352 | $_[KERNEL]->post( 'perl.cpan.testers' => 'register' ); | ||||||||||||
| 353 | return; | ||||||||||||
| 354 | } | ||||||||||||
| 355 | |||||||||||||
| 356 | # get articles with subject 'FAIL' as 'got_article' events | ||||||||||||
| 357 | sub new_header { | ||||||||||||
| 358 | my ($article_id, $lines) = @_[ARG0, ARG1]; | ||||||||||||
| 359 | my $article = Email::Simple->new( join("\r\n", @$lines) ); | ||||||||||||
| 360 | if ( $article->header('Subject') =~ /^FAIL/ ) { | ||||||||||||
| 361 | $_[KERNEL]->post( | ||||||||||||
| 362 | 'perl.cpan.testers' => 'get_article' => $article_id | ||||||||||||
| 363 | ); | ||||||||||||
| 364 | } | ||||||||||||
| 365 | return; | ||||||||||||
| 366 | } | ||||||||||||
| 367 | |||||||||||||
| 368 | # find and print perl version components to terminal | ||||||||||||
| 369 | sub got_article { | ||||||||||||
| 370 | my ($article_id, $lines) = @_[ARG0, ARG1]; | ||||||||||||
| 371 | for my $text ( reverse @$lines ) { | ||||||||||||
| 372 | if ( $text =~ /^Summary of my perl5 \(([^)]+)\)/ ) { | ||||||||||||
| 373 | print "$1\n"; | ||||||||||||
| 374 | last; | ||||||||||||
| 375 | } | ||||||||||||
| 376 | } | ||||||||||||
| 377 | return; | ||||||||||||
| 378 | } | ||||||||||||
| 379 | |||||||||||||
| 380 | =head1 DESCRIPTION | ||||||||||||
| 381 | |||||||||||||
| 382 | This component periodically polls an NNTP newsgroup and posts POE events to | ||||||||||||
| 383 | component listeners as new articles are available. These events contains the | ||||||||||||
| 384 | article ID and header text for the given articles. This component also | ||||||||||||
| 385 | facilitates retrieving the full text of a particular article of interest. | ||||||||||||
| 386 | |||||||||||||
| 387 | Internally, it uses L<POE::Component::Client::NNTP> to manage the NNTP session. | ||||||||||||
| 388 | |||||||||||||
| 389 | =head1 USAGE | ||||||||||||
| 390 | |||||||||||||
| 391 | Spawn a new component session for each newsgroup to follow. Send the | ||||||||||||
| 392 | C<<< register >>> event to specify an event to sent back when new articles arrive. | ||||||||||||
| 393 | Handle the new article event. Optionally, send the C<<< get_article >>> event to | ||||||||||||
| 394 | request the full text of the article. | ||||||||||||
| 395 | |||||||||||||
| 396 | =head2 spawn | ||||||||||||
| 397 | |||||||||||||
| 398 | POE::Component::Client::NNTP::Tail->spawn( | ||||||||||||
| 399 | NNTPServer => 'nntp.perl.org', | ||||||||||||
| 400 | Group => 'perl.cpan.testers', | ||||||||||||
| 401 | ); | ||||||||||||
| 402 | |||||||||||||
| 403 | The C<<< spawn >>> class method launches a new POE::Session to follow a given | ||||||||||||
| 404 | newsgroup. The C<<< NNTPServer >>> and C<<< Group >>> arguments are required, all other | ||||||||||||
| 405 | arguments are optional: | ||||||||||||
| 406 | |||||||||||||
| 407 | =over | ||||||||||||
| 408 | |||||||||||||
| 409 | =item * | ||||||||||||
| 410 | |||||||||||||
| 411 | NNTPServer (required) -- name or IP address of the NNTP server | ||||||||||||
| 412 | |||||||||||||
| 413 | =item * | ||||||||||||
| 414 | |||||||||||||
| 415 | Group (required) -- newsgroup to follow | ||||||||||||
| 416 | |||||||||||||
| 417 | =item * | ||||||||||||
| 418 | |||||||||||||
| 419 | Interval -- minimum number of seconds between checks for new messages | ||||||||||||
| 420 | (defaults to 60) | ||||||||||||
| 421 | |||||||||||||
| 422 | =item * | ||||||||||||
| 423 | |||||||||||||
| 424 | Alias -- POE::Session alias name (defaults to the newsgroup name) | ||||||||||||
| 425 | |||||||||||||
| 426 | =item * | ||||||||||||
| 427 | |||||||||||||
| 428 | Port -- server port for NNTP connections | ||||||||||||
| 429 | |||||||||||||
| 430 | =item * | ||||||||||||
| 431 | |||||||||||||
| 432 | LocalAddr -- local address for outbound IP connection | ||||||||||||
| 433 | |||||||||||||
| 434 | =item * | ||||||||||||
| 435 | |||||||||||||
| 436 | Debug -- if true, a trace of events and arguments will be printed to STDERR | ||||||||||||
| 437 | |||||||||||||
| 438 | =back | ||||||||||||
| 439 | |||||||||||||
| 440 | You must spawn multiple times to follow multiple newsgroups. | ||||||||||||
| 441 | |||||||||||||
| 442 | =head1 INPUT EVENTS | ||||||||||||
| 443 | |||||||||||||
| 444 | The component will respond to the following events. | ||||||||||||
| 445 | |||||||||||||
| 446 | =head2 register | ||||||||||||
| 447 | |||||||||||||
| 448 | $_[KERNEL]->post( 'perl.cpan.testers' => register => $event_name ); | ||||||||||||
| 449 | |||||||||||||
| 450 | This event notifies the component to post a C<<< new_header >>> event to the sender | ||||||||||||
| 451 | when new articles arrive. The event will be sent using the C<<< $event_name >>> | ||||||||||||
| 452 | provided, or will default to 'new_header'. Multiple sessions may register | ||||||||||||
| 453 | with a single POE::Component::Client::NNTP::Tail session. | ||||||||||||
| 454 | |||||||||||||
| 455 | =head2 unregister | ||||||||||||
| 456 | |||||||||||||
| 457 | $_[KERNEL]->post( 'perl.cpan.testers' => unregister ); | ||||||||||||
| 458 | |||||||||||||
| 459 | This event will stop the component from posting new_header events to the | ||||||||||||
| 460 | sender. | ||||||||||||
| 461 | |||||||||||||
| 462 | =head2 get_article | ||||||||||||
| 463 | |||||||||||||
| 464 | $_[KERNEL]->post( | ||||||||||||
| 465 | 'perl.cpan.testers' => get_article => $article_id => $event_name | ||||||||||||
| 466 | ); | ||||||||||||
| 467 | |||||||||||||
| 468 | This event requests that the full text of C<<< $article_id >>> be returned in a | ||||||||||||
| 469 | C<<< got_article >>> event. The event will be sent using the C<<< $event_name >>> | ||||||||||||
| 470 | provided, or will default to 'got_article'. | ||||||||||||
| 471 | |||||||||||||
| 472 | =head2 shutdown | ||||||||||||
| 473 | |||||||||||||
| 474 | $_[KERNEL]->post( 'perl.cpan.testers' => 'shutdown' ); | ||||||||||||
| 475 | |||||||||||||
| 476 | This event requests that the component stop broadcasting events, | ||||||||||||
| 477 | disconnect from the NNTP server and generally stop processing. | ||||||||||||
| 478 | |||||||||||||
| 479 | =head1 OUTPUT EVENTS | ||||||||||||
| 480 | |||||||||||||
| 481 | The component sends the following events types, though the actual event | ||||||||||||
| 482 | name may be different depending on what is specified in the C<<< register >>> and | ||||||||||||
| 483 | C<<< get_article >>> events. | ||||||||||||
| 484 | |||||||||||||
| 485 | =head2 new_header | ||||||||||||
| 486 | |||||||||||||
| 487 | ($article_id, $lines) = @_[ARG0, ARG1]; | ||||||||||||
| 488 | |||||||||||||
| 489 | The C<<< new_header >>> event is sent when new articles are found in the newsgroup. | ||||||||||||
| 490 | The C<<< $lines >>> argument is a reference to an array of lines that contain the | ||||||||||||
| 491 | article header. Lines have had newlines removed. | ||||||||||||
| 492 | |||||||||||||
| 493 | =head2 got_article | ||||||||||||
| 494 | |||||||||||||
| 495 | ($article_id, $lines) = @_[ARG0, ARG1]; | ||||||||||||
| 496 | |||||||||||||
| 497 | The C<<< got_article >>> event is sent when the full text of an article is retrieved. | ||||||||||||
| 498 | The C<<< $lines >>> argument is a reference to an array of lines that contain the full | ||||||||||||
| 499 | article, including header and body text. Lines have had newlines removed. | ||||||||||||
| 500 | |||||||||||||
| 501 | =head1 BUGS | ||||||||||||
| 502 | |||||||||||||
| 503 | Please report any bugs or feature using the CPAN Request Tracker. | ||||||||||||
| 504 | Bugs can be submitted through the web interface at | ||||||||||||
| 505 | L<http://rt.cpan.org/Dist/Display.html?Queue=POE-Component-Client-NNTP-Tail> | ||||||||||||
| 506 | |||||||||||||
| 507 | When submitting a bug or request, please include a test-file or a patch to an | ||||||||||||
| 508 | existing test-file that illustrates the bug or desired feature. | ||||||||||||
| 509 | |||||||||||||
| 510 | =head1 SEE ALSO | ||||||||||||
| 511 | |||||||||||||
| 512 | =over | ||||||||||||
| 513 | |||||||||||||
| 514 | =item * | ||||||||||||
| 515 | |||||||||||||
| 516 | L<POE> | ||||||||||||
| 517 | |||||||||||||
| 518 | =item * | ||||||||||||
| 519 | |||||||||||||
| 520 | L<POE::Component::Client::NNTP> | ||||||||||||
| 521 | |||||||||||||
| 522 | =back | ||||||||||||
| 523 | |||||||||||||
| 524 | =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders | ||||||||||||
| 525 | |||||||||||||
| 526 | =head1 SUPPORT | ||||||||||||
| 527 | |||||||||||||
| 528 | =head2 Bugs / Feature Requests | ||||||||||||
| 529 | |||||||||||||
| 530 | Please report any bugs or feature requests by email to C<bug-poe-component-client-nntp-tail at rt.cpan.org>, or through | ||||||||||||
| 531 | the web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=POE-Component-Client-NNTP-Tail>. You will be automatically notified of any | ||||||||||||
| 532 | progress on the request by the system. | ||||||||||||
| 533 | |||||||||||||
| 534 | =head2 Source Code | ||||||||||||
| 535 | |||||||||||||
| 536 | This is open source software. The code repository is available for | ||||||||||||
| 537 | public review and contribution under the terms of the license. | ||||||||||||
| 538 | |||||||||||||
| 539 | L<http://github.com/dagolden/poe-component-client-nntp-tail> | ||||||||||||
| 540 | |||||||||||||
| 541 | git clone http://github.com/dagolden/poe-component-client-nntp-tail | ||||||||||||
| 542 | |||||||||||||
| 543 | =head1 AUTHOR | ||||||||||||
| 544 | |||||||||||||
| 545 | David Golden <dagolden@cpan.org> | ||||||||||||
| 546 | |||||||||||||
| 547 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 548 | |||||||||||||
| 549 | This software is Copyright (c) 2011 by David Golden. | ||||||||||||
| 550 | |||||||||||||
| 551 | This is free software, licensed under: | ||||||||||||
| 552 | |||||||||||||
| 553 | The Apache License, Version 2.0, January 2004 | ||||||||||||
| 554 | |||||||||||||
| 555 | =cut | ||||||||||||
| 556 | |||||||||||||
| 557 | |||||||||||||
| 558 | __END__ | ||||||||||||