| 1 | package Bot::Cobalt::Plugin::URLTitle; | ||
| 2 | # ABSTRACT: Bot::Cobalt plugin for printing the title of a URL | ||
| 3 | $Bot::Cobalt::Plugin::URLTitle::VERSION = '0.003'; | ||
| 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 | 1 | ▶ | use Bot::Cobalt::Common; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | use Mojo::UserAgent; | ||
| 11 | use HTML::Entities qw(decode_entities); | ||
| 12 | use Text::Unidecode qw(unidecode); | ||
| 13 | use URI::Find::Simple qw(list_uris); | ||
| 14 | |||
| 15 | sub new { bless {}, shift } | ||
| 16 | sub ua { shift->{useragent} } | ||
| 17 | |||
| 18 | sub Cobalt_register { | ||
| 19 | my $self = shift; | ||
| 20 | my $core = shift; | ||
| 21 | my $conf = $core->get_plugin_cfg($self); | ||
| 22 | |||
| 23 | $self->{useragent} = Mojo::UserAgent->new; | ||
| 24 | |||
| 25 | register( $self, 'SERVER', 'public_msg' ); | ||
| 26 | logger->info("Registered"); | ||
| 27 | |||
| 28 | return PLUGIN_EAT_NONE; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub Cobalt_unregister { | ||
| 32 | my $self = shift; | ||
| 33 | my $core = shift; | ||
| 34 | |||
| 35 | logger->info("Unregistered"); | ||
| 36 | |||
| 37 | return PLUGIN_EAT_NONE; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub Bot_public_msg { | ||
| 41 | my $self = shift; | ||
| 42 | my $core = shift; | ||
| 43 | my $msg = ${ shift() }; | ||
| 44 | |||
| 45 | my $context = $msg->context; | ||
| 46 | my $channel = $msg->target; | ||
| 47 | |||
| 48 | foreach my $uri ( list_uris($msg->message) ) { | ||
| 49 | next if not $uri; | ||
| 50 | |||
| 51 | my $title = $self->ua->get($uri)->result->dom->at('title')->text or next; | ||
| 52 | my $uni = unidecode($title); | ||
| 53 | my $resp = sprintf('[ %s ]', $uni ? $uni : $title); | ||
| 54 | broadcast( 'message', $context, $channel, $resp ); | ||
| 55 | } | ||
| 56 | |||
| 57 | return PLUGIN_EAT_NONE; | ||
| 58 | } | ||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | __END__ | ||