line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################### |
2
|
|
|
|
|
|
|
package Bot::Webalert; |
3
|
|
|
|
|
|
|
########################################### |
4
|
1
|
|
|
1
|
|
22417
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
39
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
6
|
1
|
|
|
1
|
|
1190
|
use Bot::BasicBot; |
|
1
|
|
|
|
|
2273939
|
|
|
1
|
|
|
|
|
70
|
|
7
|
1
|
|
|
1
|
|
1351
|
use Log::Log4perl 1.05 qw(:easy); |
|
1
|
|
|
|
|
52116
|
|
|
1
|
|
|
|
|
8
|
|
8
|
1
|
|
|
1
|
|
654
|
use POE; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
9
|
1
|
|
|
1
|
|
1850
|
use POE::Component::Client::HTTP; |
|
1
|
|
|
|
|
122444
|
|
|
1
|
|
|
|
|
26
|
|
10
|
1
|
|
|
1
|
|
933
|
use HTTP::Cookies; |
|
1
|
|
|
|
|
11528
|
|
|
1
|
|
|
|
|
30
|
|
11
|
1
|
|
|
1
|
|
9
|
use base qw( Bot::BasicBot ); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
765
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
########################################### |
16
|
|
|
|
|
|
|
sub new { |
17
|
|
|
|
|
|
|
########################################### |
18
|
2
|
|
|
2
|
1
|
10496
|
my($class, %options) = @_; |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
13
|
%options = ( |
21
|
|
|
|
|
|
|
nick => "webalert-bot", |
22
|
|
|
|
|
|
|
%options, |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
2
|
50
|
66
|
|
|
19
|
if(! exists $options{server} or |
|
|
|
33
|
|
|
|
|
26
|
|
|
|
|
|
|
! exists $options{channels} or |
27
|
|
|
|
|
|
|
! exists $options{ua_request} ) { |
28
|
1
|
|
|
|
|
6
|
LOGDIE "Missing mandatory parameters server/channels/ua_request"; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
3
|
|
|
|
|
21
|
my $self = $class->SUPER::new( |
32
|
1
|
|
|
|
|
4
|
map { $_ => $options{ $_ } } qw(server channels nick), |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
171
|
$self = { |
36
|
|
|
|
|
|
|
alias => "webalert-bot", |
37
|
|
|
|
|
|
|
ua_fetch_interval => 60*60, # every hour |
38
|
|
|
|
|
|
|
ua_timeout => 60, |
39
|
|
|
|
|
|
|
ua_alias => "webalert-bot-fetcher", |
40
|
|
|
|
|
|
|
ua_callback => \&default_callback, |
41
|
|
|
|
|
|
|
ua_request => undef, |
42
|
|
|
|
|
|
|
default_callback_status => undef, |
43
|
|
|
|
|
|
|
%options, |
44
|
|
|
|
|
|
|
%$self, |
45
|
|
|
|
|
|
|
}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# re-bless |
48
|
1
|
|
|
|
|
12
|
bless($self, $class); |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
4
|
if(! defined $self->{ua_request}) { |
51
|
0
|
|
|
|
|
0
|
LOGDIE "Missing mandatory parameters ua_callback/ua_request"; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
1
|
|
|
|
|
5
|
$self->spawn(); |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
284
|
return $self; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
########################################### |
60
|
|
|
|
|
|
|
sub spawn { |
61
|
|
|
|
|
|
|
########################################### |
62
|
1
|
|
|
1
|
0
|
2
|
my($self) = @_; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
13
|
if( POE::Kernel->alias_resolve( $self->{ua_alias} ) ) { |
65
|
0
|
|
|
|
|
0
|
DEBUG "Not spawning $self->{ua_alias} session (there's one already)"; |
66
|
0
|
|
|
|
|
0
|
return 1; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
|
|
64
|
DEBUG "Spawning POE::Component::Client::HTTP aliased '$self->{ua_alias}'"; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Spawn the UA with a cookie jar |
72
|
1
|
|
|
|
|
25
|
POE::Component::Client::HTTP->spawn( |
73
|
|
|
|
|
|
|
Alias => $self->{ua_alias}, |
74
|
|
|
|
|
|
|
Timeout => $self->{ua_timeout}, |
75
|
|
|
|
|
|
|
CookieJar => HTTP::Cookies->new(), |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
1888
|
POE::Session->create( |
79
|
|
|
|
|
|
|
object_states => [ |
80
|
|
|
|
|
|
|
$self => { |
81
|
|
|
|
|
|
|
_start => "_start", |
82
|
|
|
|
|
|
|
http_start => "http_start", |
83
|
|
|
|
|
|
|
http_ready => "http_ready", |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
] |
86
|
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
########################################### |
90
|
|
|
|
|
|
|
sub _start { |
91
|
|
|
|
|
|
|
########################################### |
92
|
1
|
|
|
1
|
|
162
|
my($self) = @_; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# Wait 20 secs before the first fetch |
95
|
1
|
|
|
|
|
7
|
POE::Kernel->delay('http_start', 20); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
########################################### |
99
|
|
|
|
|
|
|
sub http_start { |
100
|
|
|
|
|
|
|
########################################### |
101
|
0
|
|
|
0
|
0
|
|
my($self) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
DEBUG "Fetching url ", $self->{ua_request}->url->as_string(); |
104
|
0
|
|
|
|
|
|
POE::Kernel->post($self->{ua_alias}, "request", |
105
|
|
|
|
|
|
|
"http_ready", $self->{ua_request}); |
106
|
0
|
|
|
|
|
|
POE::Kernel->delay('http_start', |
107
|
|
|
|
|
|
|
$self->{ua_fetch_interval}); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
########################################### |
111
|
|
|
|
|
|
|
sub http_ready { |
112
|
|
|
|
|
|
|
########################################### |
113
|
0
|
|
|
0
|
0
|
|
my($self) = @_; |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
DEBUG "http_ready ", $self->{ua_request}->url->as_string(); |
116
|
0
|
|
|
|
|
|
my $resp= $_[ARG1]->[0]; |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
my $cb_string = $self->{ua_callback}->( $resp, $self ); |
119
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
|
if(defined $cb_string) { |
121
|
0
|
|
|
|
|
|
INFO "Sending '$cb_string' to $self->{channels}->[0]"; |
122
|
0
|
|
|
|
|
|
$self->say(channel => $self->{channels}->[0], |
123
|
|
|
|
|
|
|
body => $cb_string, |
124
|
|
|
|
|
|
|
); |
125
|
|
|
|
|
|
|
} else { |
126
|
0
|
|
|
|
|
|
DEBUG "Callback returned undef (no message to IRC)"; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
|
POE::Kernel->alias_set( $self->{alias} ); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
########################################### |
133
|
|
|
|
|
|
|
sub default_callback { |
134
|
|
|
|
|
|
|
########################################### |
135
|
0
|
|
|
0
|
0
|
|
my($response, $bot) = @_; |
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
|
if($response->is_success()) { |
138
|
0
|
0
|
0
|
|
|
|
if(! defined $bot->{default_callback_status} or |
139
|
|
|
|
|
|
|
$bot->{default_callback_status} ne $response->content()) { |
140
|
0
|
|
|
|
|
|
$bot->{default_callback_status} = $response->content(); |
141
|
0
|
|
|
|
|
|
return $response->request->url->as_string() . " has changed!"; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
return undef; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
########################################### |
149
|
|
|
|
|
|
|
sub log { |
150
|
|
|
|
|
|
|
########################################### |
151
|
0
|
|
|
0
|
1
|
|
my($self, @msgs) = @_; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
local $Log::Log4perl::caller_depth; |
154
|
0
|
|
|
|
|
|
$Log::Log4perl::caller_depth++; |
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
DEBUG @msgs; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
1; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
__END__ |