Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use Encode;
- use Encode::Guess ();
- use Mojo::UserAgent;
- use Mojo::IOLoop;
- use AnySan;
- use AnySan::Provider::IRC;
- my $ua = Mojo::UserAgent->new;
- my $channel_name = '#bottest';
- my $irc = irc '',
- nickname => 'logbot',
- channels => {
- $channel_name => {},
- };
- AnySan->register_listener(
- logbot => {
- cb => sub {
- my $receive = shift;
- my $message = $receive->message;
- return unless $receive->attribute('command') eq 'PRIVMSG' && $message;
- my $http_URL_regex =
- q{\b(?:https?|shttp|ftp)://(?:(?:[-_.!~*'()a-zA-Z0-9;:&=+$,]|%[0-9A-Fa-f} .
- q{][0-9A-Fa-f])*@)?(?:(?:[a-zA-Z0-9](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?\.)} .
- q{*[a-zA-Z](?:[-a-zA-Z0-9]*[a-zA-Z0-9])?\.?|[0-9]+\.[0-9]+\.[0-9]+\.} .
- q{[0-9]+)(?::[0-9]*)?(?:/(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f]} .
- q{[0-9A-Fa-f])*(?:;(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-} .
- q{Fa-f])*)*(?:/(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f} .
- q{])*(?:;(?:[-_.!~*'()a-zA-Z0-9:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])*)*)} .
- q{*)?(?:\?(?:[-_.!~*'()a-zA-Z0-9;/?:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])} .
- q{*)?(?:#(?:[-_.!~*'()a-zA-Z0-9;/?:@&=+$,]|%[0-9A-Fa-f][0-9A-Fa-f])*} .
- q{)?};
- my $r = qr/$http_URL_regex/;
- my $url; = $1;
- if ( $message =~ m/($r)/ ) {
- $url = $1;
- }
- if ( $url ) {
- my $tx = $ua->max_redirects(3)->get($url);
- my @titles = $tx->res->dom->at('title')->text;
- if ( ! @titles ) {
- @titles = $tx->res->dom->at('h1')->text;
- }
- my $title = $titles[0];
- if ( ! Encode::is_utf8($title) ) {
- my $enc = Encode::Guess::guess_encoding($tx->res->to_string, qw/euc-jp shiftjis 7bit-jis/);
- $title = $enc->decode($title);
- $title = encode_utf8( $title );
- }
- if ( $title ) {
- $receive->send_reply($title . ' ' . $url);
- }
- }
- },
- },
- );
- AnySan->run;
Add Comment
Please, Sign In to add comment