######################################################################### # Tinyurl with the service tinyurl.com # # a link by typing "/tiny link [alias]". # # If alias is avaiable, the link returned is http://tinyurl.com/alias # ######################################################################### # Copyright (C) 2009 Robertof # # # # 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 . # ######################################################################### # History: # # 2009-12-30, Robertof : # # version 0.1, initial version # # 2010-01-03, Robertof : # # version 0.2, added hook_command to prevent the weechat block # # for some seconds # ######################################################################### use strict; # Pure perl lover! ;-) use LWP::UserAgent; use URI::Escape; weechat::register ("tinyurler", "Robertof", "0.2", "GPL3", "Tinyurl links with /tiny link [alias]", "", ""); weechat::hook_command ("tiny", "Tinyurl a link. Parameters: link [alias]", "", "", "", "tinyurl_link", ""); sub tinyurl_link { # Let's start my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]); my @arg_arr = split / /, $args; my ($ltt, $alias) = ("", ""); if (scalar (@arg_arr) <= 0) { weechat::print ($buffer, "-ERROR- Too few arguments for command 'tiny'!"); return weechat::WEECHAT_RC_ERROR; } $alias = $arg_arr[1] if scalar (@arg_arr) >= 2; if ($arg_arr[0] =~ /(https?:\/\/.+?).+/) { $ltt = $arg_arr[0]; } else { weechat::print ($buffer, "-ERROR- Wrong link in command 'tiny'"); return weechat::WEECHAT_RC_ERROR; } # Now we need to hook the command ! weechat::print ($buffer, "-STATUS- Getting url.."); weechat::hook_process ("perl -e 'use LWP::UserAgent; use URI::Escape; my \$ua = LWP::UserAgent->new; my \$req = HTTP::Request->new (POST => \"http://tinyurl.com/create.php\"); \$req->content_type (\"application/x-www-form-urlencoded\"); my \$tmp_req = \"url=\".uri_escape (\"${ltt}\"); \$tmp_req = \$tmp_req . \"&alias=\" . uri_escape (\"${alias}\") if \"${alias}\" ne \"\"; \$req->content (\$tmp_req); my \$response = \$ua->request (\$req); if (\$response->is_success) { print \"OK|\".\$response->content; } else { print \"ERR\"; }'", 10000, "tinyurl_callback", $buffer); return weechat::WEECHAT_RC_OK; } sub tinyurl_callback { my ($data, $command, $return_code, $stdout, $stderr) = ($_[0], $_[1], $_[2], $_[3], $_[4]); my $buffer = $data; chomp (my $std_ch = $stdout); if ( $return_code eq -1 and $std_ch ne "ERR" and $std_ch =~ /Open in new window<\/a>/ ) { # Let's parse page my $content = $stdout; weechat::print ($buffer, "-WARNING- The alias isn't free!") if ($content =~ /The custom alias you've/); #get link if ($content =~ /Open in new window<\/a>/) { weechat::command ($buffer, "http://tinyurl.com/".$1); return weechat::WEECHAT_RC_OK; } else { weechat::print ($buffer, "Can't get url!"); return weechat::WEECHAT_RC_ERROR; } } elsif ($std_ch eq "ERR") { weechat::print ($buffer, "-ERROR- Can't make connection to tinyurl.com!"); return weechat::WEECHAT_RC_ERROR; } }