| 1 | package POE::Component::Client::Pastebot; | ||
| 2 | $POE::Component::Client::Pastebot::VERSION = '1.18'; | ||
| 3 | #ABSTRACT: Interact with Bot::Pastebot web services from POE. | ||
| 4 | |||
| 5 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 3 | ▶ | use POE qw(Component::Client::HTTP); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | use HTTP::Request::Common; | ||
| 9 | use URI; | ||
| 10 | use HTML::TokeParser; | ||
| 11 | |||
| 12 | # Stolen from POE::Wheel. This is static data, shared by all | ||
| 13 | my $current_id = 0; | ||
| 14 | my %active_identifiers; | ||
| 15 | |||
| 16 | sub _allocate_identifier { | ||
| 17 | while (1) { | ||
| 18 | last unless exists $active_identifiers{ ++$current_id }; | ||
| 19 | } | ||
| 20 | return $active_identifiers{$current_id} = $current_id; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub _free_identifier { | ||
| 24 | my $id = shift; | ||
| 25 | delete $active_identifiers{$id}; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub spawn { | ||
| 29 | my $package = shift; | ||
| 30 | my %opts = @_; | ||
| 31 | $opts{lc $_} = delete $opts{$_} for keys %opts; | ||
| 32 | my $options = delete $opts{options}; | ||
| 33 | my $self = bless \%opts, $package; | ||
| 34 | $self->{session_id} = POE::Session->create( | ||
| 35 | object_states => [ | ||
| 36 | $self => { shutdown => '_shutdown', | ||
| 37 | paste => '_command', | ||
| 38 | fetch => '_command', | ||
| 39 | }, | ||
| 40 | $self => [ qw(_start _dispatch _http_request _http_response) ], | ||
| 41 | ], | ||
| 42 | heap => $self, | ||
| 43 | ( ref($options) eq 'HASH' ? ( options => $options ) : () ), | ||
| 44 | )->ID(); | ||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub session_id { | ||
| 49 | return $_[0]->{session_id}; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub shutdown { | ||
| 53 | my $self = shift; | ||
| 54 | $poe_kernel->post( $self->{session_id}, 'shutdown' ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub _start { | ||
| 58 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 59 | $self->{session_id} = $_[SESSION]->ID(); | ||
| 60 | if ( $self->{alias} ) { | ||
| 61 | $kernel->alias_set( $self->{alias} ); | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); | ||
| 65 | } | ||
| 66 | $self->{_httpc} = 'httpc-' . $self->{session_id}; | ||
| 67 | POE::Component::Client::HTTP->spawn( | ||
| 68 | Alias => $self->{_httpc}, | ||
| 69 | FollowRedirects => 2, | ||
| 70 | ); | ||
| 71 | undef; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _shutdown { | ||
| 75 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | ||
| 76 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | ||
| 77 | $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; | ||
| 78 | $self->{_shutdown} = 1; | ||
| 79 | $kernel->post( $self->{_httpc}, 'shutdown' ); | ||
| 80 | undef; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _dispatch { | ||
| 84 | my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; | ||
| 85 | my $session = delete $input->{sender}; | ||
| 86 | my $event = delete $input->{event}; | ||
| 87 | $kernel->post( $session, $event, $input ); | ||
| 88 | $kernel->refcount_decrement( $session => __PACKAGE__ ); | ||
| 89 | undef; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub _command { | ||
| 93 | my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE]; | ||
| 94 | my $sender = $_[SENDER]->ID(); | ||
| 95 | return if $self->{_shutdown}; | ||
| 96 | my $args; | ||
| 97 | if ( ref( $_[ARG0] ) eq 'HASH' ) { | ||
| 98 | $args = { %{ $_[ARG0] } }; | ||
| 99 | } else { | ||
| 100 | $args = { @_[ARG0..$#_] }; | ||
| 101 | } | ||
| 102 | |||
| 103 | $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args }; | ||
| 104 | |||
| 105 | unless ( $args->{event} ) { | ||
| 106 | warn "No 'event' specified for $state"; | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | unless ( $args->{url} ) { | ||
| 111 | warn "No 'url' specified for $state"; | ||
| 112 | return; | ||
| 113 | } | ||
| 114 | |||
| 115 | if ( $state eq 'paste' and !$args->{paste} ) { | ||
| 116 | warn "No 'paste' specified for paste"; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | if ( $state eq 'paste' and ref ( $args->{paste} ) eq 'ARRAY' ) { | ||
| 121 | my $paste = delete $args->{paste}; | ||
| 122 | $args->{paste} = join "\n", @{ $paste }; | ||
| 123 | } | ||
| 124 | |||
| 125 | $args->{sender} = $sender; | ||
| 126 | $args->{command} = $state; | ||
| 127 | $kernel->refcount_increment( $sender => __PACKAGE__ ); | ||
| 128 | $kernel->yield( '_http_request', $args ); | ||
| 129 | undef; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub _http_request { | ||
| 133 | my ($kernel,$self,$req) = @_[KERNEL,OBJECT,ARG0]; | ||
| 134 | if ( $req->{command} eq 'paste' ) { | ||
| 135 | my $url = | ||
| 136 | URI->new( | ||
| 137 | $req->{'url'} . ( ( $req->{'url'} !~ m,/$, ) ? '/' : '' ) . 'paste' ) | ||
| 138 | ->canonical; | ||
| 139 | unless ( defined $url ) { | ||
| 140 | $req->{error} = "could not determine url from $req->{url}"; | ||
| 141 | $kernel->yield( '_dispatch', $req ); | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $req->{'channel'} =~ s/^/#/ if $req->{'channel'} and $req->{'channel'} !~ /^#/; | ||
| 145 | my %postargs = map { | ||
| 146 | ( defined $req->{$_} and $req->{$_} ne '' ) | ||
| 147 | ? ( $_ => $req->{$_} ) | ||
| 148 | : () | ||
| 149 | } qw(channel nick summary); | ||
| 150 | $postargs{'paste'} = $req->{paste}; | ||
| 151 | my $id = _allocate_identifier(); | ||
| 152 | $self->{_requests}->{ $id } = $req; | ||
| 153 | $kernel->post( | ||
| 154 | $self->{_httpc}, | ||
| 155 | 'request', | ||
| 156 | '_http_response', | ||
| 157 | POST( $url, \%postargs ), | ||
| 158 | "$id", | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | if ( $req->{command} eq 'fetch' ) { | ||
| 164 | my $url; | ||
| 165 | my $urltmp = URI->new( $req->{url} . ( ( $req->{url} !~ m,\?tx=on$, ) ? '?tx=on' : '' ) ); | ||
| 166 | if ( defined $urltmp and defined $urltmp->scheme and $urltmp->scheme =~ /http/ ) { | ||
| 167 | $url = $urltmp->canonical; | ||
| 168 | my $id = _allocate_identifier(); | ||
| 169 | $self->{_requests}->{ $id } = $req; | ||
| 170 | $kernel->post( | ||
| 171 | $self->{_httpc}, | ||
| 172 | 'request', | ||
| 173 | '_http_response', | ||
| 174 | GET( $url ), | ||
| 175 | "$id", | ||
| 176 | ); | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | $req->{error} = 'problem with url provided'; | ||
| 180 | $kernel->yield( '_dispatch', $req ); | ||
| 181 | } | ||
| 182 | return; | ||
| 183 | } | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _http_response { | ||
| 188 | my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1]; | ||
| 189 | my $id = $request_packet->[1]; | ||
| 190 | my $req = delete $self->{_requests}->{ $id }; | ||
| 191 | _free_identifier( $id ); | ||
| 192 | my $response = $response_packet->[0]; | ||
| 193 | $req->{response} = $response; | ||
| 194 | unless ( $response->is_success ) { | ||
| 195 | if ( $response->is_error ) { | ||
| 196 | $req->{error} = $response->as_string; | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $req->{error} = 'unknown error'; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | if ( $req->{command} eq 'paste' and $response->content ) { | ||
| 204 | my $p = HTML::TokeParser->new( \$response->content ); | ||
| 205 | $p->get_tag('a'); | ||
| 206 | $req->{pastelink} = $p->get_text('/a'); | ||
| 207 | } | ||
| 208 | if ( $req->{command} eq 'fetch' and $response->content ) { | ||
| 209 | $req->{content} = $response->content; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | $kernel->yield( '_dispatch', $req ); | ||
| 213 | return; | ||
| 214 | } | ||
| 215 | |||
| 216 | 'Paste and cut'; | ||
| 217 | |||
| 218 | __END__ | ||