]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/shortenurls.rb
grouphug plugin: tweak regex so it captures confessions with newlines properly
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / shortenurls.rb
index 4cb96c352da1cd64351af7e137286fb7dd4d122c..7eeb005f8fce8705569e695e04a1f467d24217d6 100644 (file)
@@ -15,13 +15,23 @@ require "shorturl"
 require "uri"
 
 class ShortenURLs < Plugin
-  include WWW
+  # starting from about shorturl 0.8.4, the WWW module is not defined 
+  include WWW rescue nil
+
+  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
@@ -46,7 +56,7 @@ class ShortenURLs < Plugin
       return nil
     end
 
-    service = params[:service] || m.plugin.to_sym
+    service = (params[:service] || m.plugin).to_sym
     service = :rubyurl if service == :shorturl
 
     tried = []
@@ -54,9 +64,10 @@ class ShortenURLs < Plugin
 
     begin
       tried << 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
+      raise InvalidService, "#{service} blacklisted" if @blacklist.include?(service)
+      short = ShortURL.shorten(url, service)
+      raise InvalidService, "#{service} returned an empty string for #{url}" unless short and not short.empty?
+    rescue InvalidService
       pool = services - tried
       if pool.empty?
         m.reply "#{service} failed, and I don't know what else to try next" unless params[:called]