X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fshortenurls.rb;h=a7577e1b4a3ad7c783c3595faba785d6d7285919;hb=24bb60775741d3731400f1e430ef6bf3a2e1b933;hp=5d8df80a15512c7bec4bb879860f81785db002e7;hpb=ad78fb47422664c9ce24a3b62194e42974274af7;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/shortenurls.rb b/data/rbot/plugins/shortenurls.rb index 5d8df80a..a7577e1b 100644 --- a/data/rbot/plugins/shortenurls.rb +++ b/data/rbot/plugins/shortenurls.rb @@ -1,13 +1,15 @@ #-- vim:sw=2:et #++ # +# :title: ShortURL plugin for rbot +# +# Author:: Giuseppe Bilotta +# Copyright:: (C) 2007 Giuseppe Bilotta +# License:: GPL v2 +# # Plugin to handle ShortURL, merges the funcionality of the old rubyurl and tinyurl plugins # Note that it's called ShortenURLs and not ShortURL, to prevent conflicts with # the actual ruby package used -# -# Author:: Giuseppe Bilotta -# (C) 2007 Giuseppe Bilotta -# Based on existing rbot plugins, as mentioned above :) require "shorturl" require "uri" @@ -15,11 +17,20 @@ require "uri" class ShortenURLs < Plugin include WWW + Config.register Config::ArrayValue.new('shortenurls.services_blacklist', + :default => ['rubyurl', 'shorterlink'], + :requires_rescan => true, + :desc => "List of nonfunctioning shorturl services") + attr_accessor :services def initialize super + @blacklist = @bot.config['shortenurls.services_blacklist'].map { |s| s.intern } # Instead of catering for all the services, we only pick the ones with 'link' or 'url' in the name - @services = ShortURL.valid_services.select { |service| service.to_s =~ /(?:link|url)/ } << :shorturl + @services = ShortURL.valid_services.select { |service| service.to_s =~ /(?:link|url)/ } - @blacklist + if @services.include?(:rubyurl) + @services << :shorturl + end end # return a help string when the bot is asked for help on this plugin @@ -33,7 +44,7 @@ class ShortenURLs < Plugin url = params[:url] if url == "help" m.reply help(m.plugin) unless params[:called] - return + return nil end begin to_uri = URI.parse(url) @@ -41,19 +52,34 @@ class ShortenURLs < Plugin raise URI::InvalidURIError if to_uri.class == URI::Generic rescue URI::InvalidURIError m.reply "#{url} doesn't look like an URL to me ..." unless params[:called] - return + return nil end - service = params[:service] || m.plugin.to_sym + service = (params[:service] || m.plugin).to_sym service = :rubyurl if service == :shorturl - short = WWW::ShortURL.shorten(url, service) + tried = [] + short = nil - if params[:called] - return short - else - m.reply "#{url} shortened to #{short}" + begin + tried << service + raise WWW::InvalidService, "#{service} blacklisted" if @blacklist.include?(service) + short = WWW::ShortURL.shorten(url, service) + raise WWW::InvalidService, "#{service} returned an empty string for #{url}" unless short and not short.empty? + rescue WWW::InvalidService + pool = services - tried + if pool.empty? + m.reply "#{service} failed, and I don't know what else to try next" unless params[:called] + return nil + else + service = pool.pick_one + m.reply "#{tried.last} failed, I'll try #{service} instead" unless params[:called] + retry + end end + + m.reply "#{url} shortened to #{short}" unless params[:called] + return short end end