/usr/share/weechat/ruby/url_shorten.rb is in weechat-scripts 20120603-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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | # Copyright (c) 2008, Daniel Bretoi <daniel@bretoi.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY Daniel Bretoi ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# * Simpler, more robust version of tinyurl (doesn't rely on html output)
# * Echo's urls from all channels that are over the plugin's maxlen value
# in a shortened format.
# * allows for manual shortening of urls
# * set config variable 'shortener' to one of:
#
# Value Service used
# ----- ------------
# qurl http://qurl.com/
# tinyurl http://tinyurl.com/
# isgd http://is.gd/
# trim http://tr.im/
# bitly http://bit.ly/
#
# Note: attempting to use the bitly shortener without setting the
# bitly_login and bitly_key config variables will fail.
#
# Note: 'trim' and 'bitly' shorteners require the 'rubygems' and
# 'json' ruby modules.
#
# 2010-01-07, nils_2 <weechatter@arcor.de>
# version 1.8: URI will be shorten only for messages
# checking for notify_message, notify_private and notify_highlight first
# 2010-10-11, Daniel
# version 1.7: google use comma in URL so make them acceptable too
# 2010-08-05, Derek Carter <goozbach@friocorte.com>
# version 1.6: add support for yourls
# 2009-12-12, FlashCode <flashcode@flashtux.org>
# version 1.5: fix wrong license in register()
# 2009-11-29, penryu <penryu@gmail.com>
# version 1.4: fixed URI encoding bug, added shorteners
# changed default shortener, as qurl is _slow_
# 2009-11-29, penryu <penryu@gmail.com>
# version 1.3: add bit.ly shortener routine
# add ability to choose shortener from config
# 2009-05-02, FlashCode <flashcode@flashtux.org>:
# version 1.2: sync with last API changes
# 2008-11-11, FlashCode <flashcode@flashtux.org>:
# version 1.1: conversion to WeeChat 0.3.0+
require 'net/http'
require 'net/https'
require 'uri'
SCRIPT_NAME = 'url_shorten'
SCRIPT_AUTHOR = 'Daniel Bretoi <daniel@bretoi.com>'
SCRIPT_DESC = 'Shorten url'
SCRIPT_VERSION = '1.8'
SCRIPT_LICENSE = 'BSD'
DEFAULTS = {
'maxlen' => '50',
'color' => 'red',
'shortener' => 'tinyurl',
'bitly_login' => '',
'bitly_key' => '',
'yourls_url' => '',
}
def weechat_init
Weechat.register SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION, SCRIPT_LICENSE, SCRIPT_DESC, "", ""
Weechat.hook_command SCRIPT_NAME, SCRIPT_DESC, "url", "url: url to shorten", "", SCRIPT_NAME, ""
#Weechat.hook_signal "*,irc_in2_privmsg", "msg_shorten", ""
Weechat.hook_print "", "notify_message", "://", 1, "msg_shorten", ""
Weechat.hook_print "", "notify_private", "://", 1, "msg_shorten", ""
Weechat.hook_print "", "notify_highlight", "://", 1, "msg_shorten", ""
DEFAULTS.each_pair { |option, def_value|
cur_value = Weechat.config_get_plugin(option)
if cur_value.nil? || cur_value.empty?
Weechat.config_set_plugin(option, def_value)
end
}
if Weechat.config_get_plugin("shortener").eql?('bitly')
cfg_bitly_login = Weechat.config_get_plugin("bitly_login")
cfg_bitly_key = Weechat.config_get_plugin("bitly_key")
if cfg_bitly_login.empty? || cfg_bitly_key.empty?
yellow = Weechat.color("yellow")
Weechat.print("", "#{yellow}WARNING: The bit.ly shortener requires a valid API login and key.")
Weechat.print("", "#{yellow}WARNING: Please configure the `bitly_login' and `bitly_key' options before using this script.")
end
end
if Weechat.config_get_plugin("shortener").eql?('yourls')
# not quite ready for key based auth
# should be researched http://code.google.com/p/yourls/wiki/PasswordlessAPI
cfg_yourls_url = Weechat.config_get_plugin("yourls_url")
if cfg_yourls_url.empty?
yellow = Weechat.color("yellow")
Weechat.print("", "#{yellow}WARNING: The yourls shortener requires a valid API.")
Weechat.print("", "#{yellow}WARNING: Please configure the `yourls_url' option before using this script.")
end
end
return Weechat::WEECHAT_RC_OK
end
def fetch(uri_str, limit = 10)
raise ArgumentError, 'HTTP redirect too deep' if limit == 0
req_url = URI.parse(uri_str)
http = Net::HTTP.new(req_url.host, req_url.port)
http.use_ssl = (req_url.port == 443)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.open_timeout = 3 # in seconds
http.read_timeout = 3 # in seconds
response = Net::HTTP.get_response(req_url)
case response
when Net::HTTPSuccess
then
response.body
when Net::HTTPRedirection
then
fetch(response['location'], limit - 1)
else
response.error!
end
end
def qurl_shorten(url)
fetch('http://www.qurl.com/automate.php?url=' + URI.encode(url)).gsub('www.','')
end
def tinyurl_shorten(url)
fetch('http://tinyurl.com/api-create.php?url=' + URI.encode(url))
end
def isgd_shorten(url)
fetch('http://is.gd/api.php?longurl=' + URI.encode(url))
end
def trim_shorten(url)
require 'rubygems'
require 'json'
params = ['url=' + URI.encode(url)]
params << 'newtrim=1'
#params << 'sandbox=1' # comment out for release
json = fetch('http://api.tr.im/v1/trim_url.json?' + params.join('&'))
data = JSON.parse(json)
if data['status']['result'].eql?('OK')
begin
data['url']
rescue NoMethodError => ex
"Failure parsing tr.im result: #{ex}"
end
else
data['status']['message']
end
end
def yourls_shorten(url)
# use yourls shortener
# need to provide url config option
require 'rubygems'
require 'json/pure'
params = ['action=shorturl']
params << 'format=simple'
params << 'url=' + URI.encode(url)
yourls_url = Weechat.config_get_plugin('yourls_url')
api_url = yourls_url + params.join('&')
begin
body_txt = fetch(api_url)
rescue Exception => ex
return "Failure yourls shortening url: " + ex.to_s
end
body_txt
end
def bitly_shorten(url)
require 'rubygems'
require 'json'
params = ['longUrl=' + URI.encode(url)]
params << 'login=' + Weechat.config_get_plugin('bitly_login')
params << 'apiKey=' + Weechat.config_get_plugin('bitly_key')
api_url = 'http://api.bit.ly/shorten?version=2.0.1&' + params.join('&')
begin
url_data = JSON.parse(fetch(api_url))
rescue Exception => ex
return "Failure shortening url: " + ex.to_s
end
if url_data['statusCode'].eql?('OK')
begin
res = url_data['results']
res[res.keys[0]]['shortUrl']
rescue NoMethodError => ex
"Failure parsing bitly result: #{ex}"
end
else
url_data['errorMessage']
end
end
def shortener(url)
service = Weechat.config_get_plugin('shortener').tr('.','')
begin
send("#{service}_shorten", url)
rescue NoMethodError => e
"Shortening service #{service} not supported... #{e}"
end
end
def regexp_url
@regexp_url ||= Regexp.new('https?://[^\s>]*')
@regexp_url
end
def url_shorten(data,buffer,msg)
if (msg.empty?)
return Weechat::WEECHAT_RC_OK
end
url = (msg.scan regexp_url).to_s
short = shortener(url)
color = Weechat.color(Weechat.config_get_plugin("color"))
Weechat.print(Weechat.current_buffer, "[url]\t#{color}#{short}");
return Weechat::WEECHAT_RC_OK
end
def msg_shorten(data,buffer,time,tags,displayed,highlight,prefix,message)
if (message.empty?)
return Weechat::WEECHAT_RC_OK
end
matchdata = message.match(regexp_url)
return Weechat::WEECHAT_RC_OK unless matchdata
url = matchdata[0].to_s
maxlen = Weechat.config_get_plugin("maxlen").to_i
return Weechat::WEECHAT_RC_OK if url.length < maxlen
short = shortener(url)
color = Weechat.color(Weechat.config_get_plugin("color"))
Weechat.print(buffer, "[url]\t%s%s" % [color, short])
return Weechat::WEECHAT_RC_OK
end
|