]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/shortenurls.rb
search plugin: fix gcalc
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / shortenurls.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: ShortURL plugin for rbot
5 #
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2007 Giuseppe Bilotta
8 # License:: GPL v2
9 #
10 # Plugin to handle ShortURL, merges the funcionality of the old rubyurl and tinyurl plugins
11 # Note that it's called ShortenURLs and not ShortURL, to prevent conflicts with
12 # the actual ruby package used
13
14 require "shorturl"
15 require "uri"
16
17 class ShortenURLs < Plugin
18   include WWW
19
20   Config.register Config::ArrayValue.new('shortenurls.services_blacklist',
21     :default => ['rubyurl', 'shorterlink'],
22     :requires_rescan => true,
23     :desc => "List of nonfunctioning shorturl services")
24
25   attr_accessor :services
26   def initialize
27     super
28     @blacklist = @bot.config['shortenurls.services_blacklist'].map { |s| s.intern }
29     # Instead of catering for all the services, we only pick the ones with 'link' or 'url' in the name
30     @services = ShortURL.valid_services.select { |service| service.to_s =~ /(?:link|url)/ } - @blacklist
31     if @services.include?(:rubyurl)
32       @services << :shorturl
33     end
34   end
35
36   # return a help string when the bot is asked for help on this plugin
37   def help(plugin, topic="")
38     return "shorten urls. syntax: <service> <your long url> => creates a shortened url using the required service (choose between #{@services.join(', ')}). Example: #{@bot.nick}, tinyurl http://some.long.url/wow-this-is/really-long.html"
39   end
40
41   # do the dirty job. This method can be called by other plugins, in which case you
42   # should set the :called param to true
43   def shorten(m, params)
44     url = params[:url]
45     if url == "help"
46       m.reply help(m.plugin) unless params[:called]
47       return nil
48     end
49     begin
50       to_uri = URI.parse(url)
51       # We don't accept 'generic' URLs because almost everything gets in there
52       raise URI::InvalidURIError if to_uri.class == URI::Generic
53     rescue URI::InvalidURIError
54       m.reply "#{url} doesn't look like an URL to me ..." unless params[:called]
55       return nil
56     end
57
58     service = (params[:service] || m.plugin).to_sym
59     service = :rubyurl if service == :shorturl
60
61     tried = []
62     short = nil
63
64     begin
65       tried << service
66       raise WWW::InvalidService, "#{service} blacklisted" if @blacklist.include?(service)
67       short = WWW::ShortURL.shorten(url, service)
68       raise WWW::InvalidService, "#{service} returned an empty string for #{url}" unless short and not short.empty?
69     rescue WWW::InvalidService
70       pool = services - tried
71       if pool.empty?
72         m.reply "#{service} failed, and I don't know what else to try next" unless params[:called]
73         return nil
74       else
75         service = pool.pick_one
76         m.reply "#{tried.last} failed, I'll try #{service} instead" unless params[:called]
77         retry
78       end
79     end
80
81     m.reply "#{url} shortened to #{short}" unless params[:called]
82     return short
83   end
84
85 end
86
87 # create an instance of the RubyURL class and register it as a plugin
88 plugin = ShortenURLs.new
89
90 plugin.services.each { |service|
91   plugin.map "#{service} :url", :action => 'shorten'
92 }