forked from Over23/Irssi_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_title.pl
57 lines (44 loc) · 1.37 KB
/
link_title.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl
use strict;
use warnings;
use Irssi;
use Irssi::Irc;
use LWP::UserAgent;
use vars qw($VERSION %IRSSI);
$VERSION = '0.01';
%IRSSI = (
authors => "Petr Vavrin",
contact => "pb.pb\@centrum.cz",
name => "Link Title",
description => "description",
);
sub on_public {
my ( $server, $message, $nick, $hostmask, $channel ) = @_;
my $cp = Irssi::settings_get_str ( 'bot_cmd_prefix' );
my $isprivate = !defined $channel;
my $dst = $isprivate ? $nick : $channel;
return unless $message =~ /(http(s)?:\/\/\S+)/;
my $link = $1;
my $ua = LWP::UserAgent->new ( ssl_opts => { verify_hostname => 0 } );
$ua->agent('Mozilla/5.0');
$ua->timeout(3);
$ua->env_proxy;
$ua->max_size ( 266144 );
my $response = $ua->get ( $link );
if ( $response->is_success ) {
my $title = "";
if ( defined ( $response->header ( "title" ) ) && $response->header ( "title" ) !~ /^\s*$/ ){
$title = $response->header ( "title" );
} else {
my $html = $response->decoded_content;
$html =~ s/\n/ /g;
if ( $html =~ /<title[^>+]>([^<]+)<\/title>/ ){
$title = $1;
}
}
$server->send_message ( $dst, $nick . ": LINK TITLE: " . $title, 0 );
}
}
Irssi::signal_add('message public', 'on_public');
Irssi::signal_add('message private', 'on_public');
Irssi::settings_add_str('bot', 'bot_cmd_prefix', '`');