/usr/share/weechat/perl/expand_url.pl is in weechat-scripts 20111030-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | #
# Copyright (c) 2010 by Nils Görs <weechatter@arcor.de>
#
# Get information on a short URL. Find out where it goes.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# 0.1: internal release
# 0.2: add "://" in call to hook_print() (thanks to xt)
# 0.3: fixed: script won't worked if more than one URL per message exists.
# : fixed: output buffer wasn't correctly set for each message.
# : fixed: missing return in callback for hook_config()
# : added: if an URI exists twice in a message script will only print URI once.
# : added: option "expand_own" (default: off).
# : added: only private and public messages with string "://" will be caught
#
# requirements:
# - URI::Find
use strict;
use URI::Find;
my $prgname = "expand_url";
my $version = "0.3";
my $description = "Get information on a short URL. Find out where it goes.";
# default values
my %default_options = ( "shortener" => "tiny.cc|bit.ly|is.gd|tinyurl.com",
"expander" => "http://expandurl.com/api/v1/?url=",
# "http://api.longurl.org/v1/expand?url=";
"color" => "blue",
"expand_own" => "off",
);
my %uris;
my $uri_only;
sub hook_print_cb{
my ( $data, $buffer, $date, $tags, $displayed, $highlight, $prefix, $message ) = @_;
my $tags2 = ",$tags,";
#return weechat::WEECHAT_RC_OK if ( not $tags2 =~ /,notify_[^,]+,/ ); # return if message is not from a nick.
if ( $default_options{expand_own} eq "off" ){
# get servername from buffer
my $infolist = weechat::infolist_get("buffer",$buffer,"");
weechat::infolist_next($infolist);
my ($servername, undef) = split( /\./, weechat::infolist_string($infolist,"name") );
weechat::infolist_free($infolist);
my $my_nick = weechat::info_get( "irc_nick", $servername ); # get own nick
if ( $tags2 =~ /,nick_[$my_nick,]+,/ ){
return weechat::WEECHAT_RC_OK;
}
}
%uris = ();
my $finder = URI::Find->new( \&uri_find_cb );
my $how_many_found = $finder->find(\$message); # search uri in message. result in $uri_only
if ( $how_many_found >= 1 ){ # does message contains an url?
my @uris = keys %uris;
foreach my $uri (@uris) {
if ($uri =~ m/$default_options{shortener}/) { # shortener used?
my $get_homepage = $default_options{expander} . $uri; # add API + URL
my $homepage = qq(perl -e 'use LWP::Simple; print get(\"$get_homepage\");');
weechat::hook_process($homepage, 10000 ,"hook_process_cb",$buffer);
}
}
}
return weechat::WEECHAT_RC_OK;
}
# callback from hook_process()
sub hook_process_cb {
my ($data, $command, $return_code, $out, $err) = @_;
return weechat::WEECHAT_RC_OK if ( $return_code > 0 );
my $buffer = $data;
my @array = split(/\n/,$out);
foreach ( @array ){
$uri_only = "";
my $finder = URI::Find->new( \&uri_find_one_cb );
my $how_many_found = $finder->find(\$_);
if ( $how_many_found >= 1 ){ # does message contains an url?
weechat::print($buffer, "[url]\t". weechat::color($default_options{color}) . $uri_only);
last;
}
}
return weechat::WEECHAT_RC_OK;
}
# callback from URI::Find
sub uri_find_cb {
my ( $uri_url, $uri ) = @_;
$uris{$uri}++;
return "";
}
sub uri_find_one_cb {
my ( $uri_url, $uri ) = @_;
$uri_only = $uri;
return "";
}
sub toggled_by_set{
my ( $pointer, $option, $value ) = @_;
if ($option eq "plugins.var.perl.$prgname.expander"){
$default_options{expander} = $value;
}elsif ($option eq "plugins.var.perl.$prgname.shortener"){
$default_options{shortener} = $value;
}elsif ($option eq "plugins.var.perl.$prgname.color"){
$default_options{color} = $value;
}elsif ($option eq "plugins.var.perl.$prgname.expand_own"){
$default_options{expand_own} = $value;
}
return weechat::WEECHAT_RC_OK ;
}
# first function called by a WeeChat-script.
weechat::register($prgname, "Nils Görs <weechatter\@arcor.de>", $version,
"GPL3", $description, "", "");
# get settings or set them if they do not exists.
if (!weechat::config_is_set_plugin("shortener")){
weechat::config_set_plugin("shortener", $default_options{shortener});
}else{
$default_options{shortener} = weechat::config_get_plugin("shortener");
}
if (!weechat::config_is_set_plugin("expander")){
weechat::config_set_plugin("expander", $default_options{expander});
}else{
$default_options{expander} = weechat::config_get_plugin("expander");
}
if (!weechat::config_is_set_plugin("color")){
weechat::config_set_plugin("color", $default_options{color});
}else{
$default_options{color} = weechat::config_get_plugin("color");
}
if (!weechat::config_is_set_plugin("expand_own")){
weechat::config_set_plugin("expand_own", $default_options{expand_own});
}else{
$default_options{expand_own} = weechat::config_get_plugin("expand_own");
}
weechat::hook_print("", "notify_message", "://", 1, "hook_print_cb", ""); # only public messages with string "://" will be caught!
weechat::hook_print("", "notify_private", "://", 1, "hook_print_cb", ""); # only private messages with string "://" will be caught!
weechat::hook_print("", "notify_highlight", "://", 1, "hook_print_cb", ""); # only highlight messages with string "://" will be caught!
weechat::hook_config("plugins.var.perl.$prgname.*", "toggled_by_set", ""); # options changed?
|