line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Bot::Cobalt::Plugin::Bitly; |
2
|
|
|
|
|
|
|
# ABSTRACT: Bot::Cobalt plugin for auto-shortening URL via Bit.ly |
3
|
|
|
|
|
|
|
$Bot::Cobalt::Plugin::Bitly::VERSION = '0.001'; |
4
|
1
|
|
|
1
|
|
28485
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
450
|
use Bot::Cobalt; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
use Bot::Cobalt::Common; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use URI::Find::Simple; |
11
|
|
|
|
|
|
|
use WebService::Bitly; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub new { bless {}, shift } |
14
|
|
|
|
|
|
|
sub bitly { shift->{bitly} } |
15
|
|
|
|
|
|
|
sub min_length { shift->{min_length} } |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub Cobalt_register { |
18
|
|
|
|
|
|
|
my $self = shift; |
19
|
|
|
|
|
|
|
my $core = shift; |
20
|
|
|
|
|
|
|
my $cfg = $core->get_plugin_cfg($self); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
$self->{min_length} = $cfg->{min_length} || 160; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
eval { |
25
|
|
|
|
|
|
|
$self->{bitly} = WebService::Bitly->new(%{$cfg->{creds}}); |
26
|
|
|
|
|
|
|
register( $self, 'SERVER', 'public_msg' ); |
27
|
|
|
|
|
|
|
logger->info("Registered"); |
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
if (my $err = $@) { |
31
|
|
|
|
|
|
|
logger->warn("Unable to create WebService::Bitly object: $err"); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
return PLUGIN_EAT_NONE; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub Cobalt_unregister { |
38
|
|
|
|
|
|
|
my $self = shift; |
39
|
|
|
|
|
|
|
my $core = shift; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
logger->info("Unregistered"); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
return PLUGIN_EAT_NONE; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub Bot_public_msg { |
47
|
|
|
|
|
|
|
my $self = shift; |
48
|
|
|
|
|
|
|
my $core = shift; |
49
|
|
|
|
|
|
|
my $msg = ${ shift() }; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my @url = grep { |
52
|
|
|
|
|
|
|
length($_) >= $self->min_length |
53
|
|
|
|
|
|
|
} ( URI::Find::Simple::list_uris( $msg->message ) ); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
foreach my $url (@url) { |
56
|
|
|
|
|
|
|
my $short = $self->bitly->shorten($url); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
if ($short->is_error) { |
59
|
|
|
|
|
|
|
logger->warn("Bitly error: " . $short->status_txt); |
60
|
|
|
|
|
|
|
next; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
broadcast( 'message', $msg->context, $msg->channel, $short->short_url ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
return PLUGIN_EAT_NONE; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |