| 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 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use Bot::Cobalt; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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__ | ||