line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package POE::Component::IRC::Plugin::WWW::XKCD::AsText; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
355708
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
31
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
47
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.003'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
90
|
|
9
|
1
|
|
|
1
|
|
5
|
use POE qw(Component::WWW::XKCD::AsText); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
8
|
|
10
|
1
|
|
|
1
|
|
436
|
use POE::Component::IRC::Plugin qw(:ALL); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1771
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
0
|
|
|
0
|
1
|
|
my $package = shift; |
14
|
0
|
0
|
|
|
|
|
croak "Even number of arguments must be specified" |
15
|
|
|
|
|
|
|
if @_ & 1; |
16
|
0
|
|
|
|
|
|
my %args = @_; |
17
|
0
|
|
|
|
|
|
$args{ lc $_ } = delete $args{ $_ } for keys %args; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# fill in the defaults |
20
|
0
|
|
|
|
|
|
%args = ( |
21
|
|
|
|
|
|
|
debug => 0, |
22
|
|
|
|
|
|
|
auto => 1, |
23
|
|
|
|
|
|
|
response_event => 'irc_xkcd', |
24
|
|
|
|
|
|
|
banned => [], |
25
|
|
|
|
|
|
|
addressed => 1, |
26
|
|
|
|
|
|
|
eat => 1, |
27
|
|
|
|
|
|
|
trigger => qr/^xkcd\s+(?=\S)/i, |
28
|
|
|
|
|
|
|
listen_for_input => [ qw(public notice privmsg) ], |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
%args, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
$args{listen_for_input} = { |
34
|
0
|
0
|
|
|
|
|
map { $_ => 1 } @{ $args{listen_for_input} || [] } |
|
0
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
}; |
36
|
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
return bless \%args, $package; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _start { |
41
|
0
|
|
|
0
|
|
|
my ( $kernel, $self ) = @_[ KERNEL, OBJECT ]; |
42
|
0
|
|
|
|
|
|
$self->{_session_id} = $_[SESSION]->ID(); |
43
|
0
|
|
|
|
|
|
$kernel->refcount_increment( $self->{_session_id}, __PACKAGE__ ); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
$self->{poco} = POE::Component::WWW::XKCD::AsText->spawn( |
46
|
|
|
|
|
|
|
debug => $self->{debug}, |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
undef; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub PCI_register { |
53
|
0
|
|
|
0
|
0
|
|
my ( $self, $irc ) = splice @_, 0, 2; |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
|
$self->{irc} = $irc; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
$irc->plugin_register( $self, 'SERVER', qw(public notice msg) ); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$self->{_session_id} = POE::Session->create( |
60
|
|
|
|
|
|
|
object_states => [ |
61
|
|
|
|
|
|
|
$self => [ |
62
|
|
|
|
|
|
|
qw( |
63
|
|
|
|
|
|
|
_start |
64
|
|
|
|
|
|
|
_shutdown |
65
|
|
|
|
|
|
|
_xkcd_done |
66
|
|
|
|
|
|
|
) |
67
|
|
|
|
|
|
|
], |
68
|
|
|
|
|
|
|
], |
69
|
|
|
|
|
|
|
)->ID; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return 1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _shutdown { |
75
|
0
|
|
|
0
|
|
|
my ($kernel, $self) = @_[ KERNEL, OBJECT ]; |
76
|
0
|
|
|
|
|
|
$self->{poco}->shutdown; |
77
|
0
|
|
|
|
|
|
$kernel->alarm_remove_all(); |
78
|
0
|
|
|
|
|
|
$kernel->refcount_decrement( $self->{_session_id}, __PACKAGE__ ); |
79
|
0
|
|
|
|
|
|
undef; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub PCI_unregister { |
83
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Plugin is dying make sure our POE session does as well. |
86
|
0
|
|
|
|
|
|
$poe_kernel->call( $self->{_session_id} => '_shutdown' ); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
delete $self->{irc}; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
return 1; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub S_public { |
94
|
0
|
|
|
0
|
0
|
|
my ( $self, $irc ) = splice @_, 0, 2; |
95
|
0
|
|
|
|
|
|
my $who = ${ $_[0] }; |
|
0
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $channel = ${ $_[1] }->[0]; |
|
0
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
my $message = ${ $_[2] }; |
|
0
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
return $self->_parse_input( $irc, $who, $channel, $message, 'public' ); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub S_notice { |
102
|
0
|
|
|
0
|
0
|
|
my ( $self, $irc ) = splice @_, 0, 2; |
103
|
0
|
|
|
|
|
|
my $who = ${ $_[0] }; |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $channel = ${ $_[1] }->[0]; |
|
0
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my $message = ${ $_[2] }; |
|
0
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return $self->_parse_input( $irc, $who, $channel, $message, 'notice' ); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub S_msg { |
110
|
0
|
|
|
0
|
0
|
|
my ( $self, $irc ) = splice @_, 0, 2; |
111
|
0
|
|
|
|
|
|
my $who = ${ $_[0] }; |
|
0
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $channel = ${ $_[1] }->[0]; |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $message = ${ $_[2] }; |
|
0
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
return $self->_parse_input( $irc, $who, $channel, $message, 'privmsg' ); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub _parse_input { |
118
|
0
|
|
|
0
|
|
|
my ( $self, $irc, $who, $channel, $message, $type ) = @_; |
119
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
|
warn "Got input: [ who => $who, channel => $channel, " |
121
|
|
|
|
|
|
|
. "mesage => $message ]" |
122
|
|
|
|
|
|
|
if $self->{debug}; |
123
|
|
|
|
|
|
|
|
124
|
0
|
0
|
|
|
|
|
return PCI_EAT_NONE |
125
|
|
|
|
|
|
|
unless exists $self->{listen_for_input}{ $type }; |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
my $what; |
128
|
0
|
0
|
0
|
|
|
|
if ( $self->{addressed} and $type eq 'public' ) { |
129
|
0
|
|
|
|
|
|
my $my_nick = $irc->nick_name(); |
130
|
0
|
|
|
|
|
|
($what) = $message =~ m/^\s*\Q$my_nick\E[\:\,\;\.]?\s*(.*)$/i; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
else { |
133
|
0
|
|
|
|
|
|
$what = $message; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
0
|
0
|
|
|
|
return PCI_EAT_NONE |
137
|
|
|
|
|
|
|
unless defined $what and $what =~ s/$self->{trigger}//; |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
$what =~ s/^\s+|\s+$//; |
140
|
|
|
|
|
|
|
|
141
|
0
|
0
|
0
|
|
|
|
return PCI_EAT_NONE |
142
|
|
|
|
|
|
|
unless length $what and $what !~ /\D/; |
143
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
warn "Matched trigger: [ who => $who, channel => $channel, " |
145
|
|
|
|
|
|
|
. "what => $what ]" |
146
|
|
|
|
|
|
|
if $self->{debug}; |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
foreach my $ban_re ( @{ $self->{banned} || [] } ) { |
|
0
|
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
|
return PCI_EAT_NONE |
150
|
|
|
|
|
|
|
if $who =~ /$ban_re/; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
$self->{poco}->retrieve( { |
154
|
|
|
|
|
|
|
session => $self->{_session_id}, |
155
|
|
|
|
|
|
|
event => '_xkcd_done', |
156
|
|
|
|
|
|
|
id => $what, |
157
|
|
|
|
|
|
|
_who => $who, |
158
|
|
|
|
|
|
|
_channel => $channel, |
159
|
|
|
|
|
|
|
_message => $message, |
160
|
|
|
|
|
|
|
_type => $type, |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
); |
163
|
|
|
|
|
|
|
|
164
|
0
|
0
|
|
|
|
|
return $self->{eat} ? PCI_EAT_ALL : PCI_EAT_NONE; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub _xkcd_done { |
168
|
0
|
|
|
0
|
|
|
my ( $kernel, $self, $in_ref ) = @_[ KERNEL, OBJECT, ARG0 ]; |
169
|
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $response_message; |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if ( exists $in_ref->{error} ) { |
173
|
0
|
|
|
|
|
|
$response_message = [ $in_ref->{error} ]; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
else { |
176
|
0
|
|
|
|
|
|
$response_message = $in_ref->{text}; |
177
|
0
|
|
|
|
|
|
$response_message =~ s/\n\s*\n/\n \n/g; |
178
|
0
|
|
|
|
|
|
$response_message = [ split /\n/, $response_message ]; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
0
|
|
|
|
|
|
$self->{irc}->send_event( $self->{response_event} => { |
182
|
|
|
|
|
|
|
text => $in_ref->{text}, |
183
|
|
|
|
|
|
|
id => $in_ref->{id}, |
184
|
0
|
|
|
|
|
|
map { $_ => $in_ref->{"_$_"} } |
185
|
|
|
|
|
|
|
qw( who channel message type ), |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
); |
188
|
|
|
|
|
|
|
|
189
|
0
|
0
|
|
|
|
|
if ( $self->{auto} ) { |
190
|
0
|
0
|
|
|
|
|
my $response_type = $in_ref->{_type} eq 'public' |
191
|
|
|
|
|
|
|
? 'privmsg' |
192
|
|
|
|
|
|
|
: $in_ref->{_type}; |
193
|
|
|
|
|
|
|
|
194
|
0
|
0
|
|
|
|
|
my $where = $in_ref->{_type} eq 'public' |
195
|
|
|
|
|
|
|
? $in_ref->{_channel} |
196
|
|
|
|
|
|
|
: (split /!/, $in_ref->{_who})[0]; |
197
|
|
|
|
|
|
|
|
198
|
0
|
|
|
|
|
|
for ( @$response_message ) { |
199
|
0
|
|
|
|
|
|
$kernel->post( $self->{irc} => |
200
|
|
|
|
|
|
|
$response_type => |
201
|
|
|
|
|
|
|
$where => |
202
|
|
|
|
|
|
|
$_ |
203
|
|
|
|
|
|
|
); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
|
207
|
0
|
|
|
|
|
|
undef; |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
1; |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
__END__ |