]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/shortenurls.rb
plugin(hangman): fixes word generator closes #9
[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   # starting from about shorturl 0.8.4, the WWW module is not defined
19   include WWW rescue nil
20
21   Config.register Config::ArrayValue.new('shortenurls.services_blacklist',
22     :default => ['rubyurl', 'shorterlink'],
23     :requires_rescan => true,
24     :desc => "List of nonfunctioning shorturl services")
25   Config.register Config::StringValue.new('shortenurls.favorite_service',
26     :default => 'tinyurl',
27     :desc => "Default shortening service. Probably only applies when other plugins " +
28              "use this one for shortening")
29
30   attr_accessor :services
31   def initialize
32     super
33     @blacklist = @bot.config['shortenurls.services_blacklist'].map { |s| s.intern }
34     # Instead of catering for all the services, we only pick the ones with 'link' or 'url' in the name
35     @services = ShortURL.valid_services.select { |service| service.to_s =~ /(?:link|url)/ } - @blacklist
36     if @services.include?(:rubyurl)
37       @services << :shorturl
38     end
39   end
40
41   # return a help string when the bot is asked for help on this plugin
42   def help(plugin, topic="")
43     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"
44   end
45
46   # do the dirty job. This method can be called by other plugins, in which case you
47   # should set the :called param to true
48   def shorten(m, params)
49     url = params[:url]
50     if url == "help"
51       m.reply help(m.plugin) unless params[:called]
52       return nil
53     end
54     begin
55       to_uri = URI.parse(url)
56       # We don't accept 'generic' URLs because almost everything gets in there
57       raise URI::InvalidURIError if to_uri.class == URI::Generic
58     rescue URI::InvalidURIError
59       m.reply "#{url} doesn't look like an URL to me ..." unless params[:called]
60       return nil
61     end
62
63     if params.has_key? :service
64       service = params[:service]
65     elsif m != nil and m.plugin != nil
66       service = m.plugin
67     else
68       service = @bot.config['shortenurls.favorite_service']
69     end
70     service = service.to_sym
71     service = :rubyurl if service == :shorturl
72
73     tried = []
74     short = nil
75
76     begin
77       tried << service
78       raise InvalidService, "#{service} blacklisted" if @blacklist.include?(service)
79       short = ShortURL.shorten(url, service)
80       raise InvalidService, "#{service} returned an empty string for #{url}" unless short and not short.empty?
81     rescue InvalidService
82       pool = services - tried
83       if pool.empty?
84         m.reply "#{service} failed, and I don't know what else to try next" unless params[:called]
85         return nil
86       else
87         service = pool.pick_one
88         m.reply "#{tried.last} failed, I'll try #{service} instead" unless params[:called]
89         retry
90       end
91     end
92
93     m.reply "#{url} shortened to #{short}" unless params[:called]
94     return short
95   end
96
97 end
98
99 # create an instance of the RubyURL class and register it as a plugin
100 plugin = ShortenURLs.new
101
102 plugin.services.each { |service|
103   plugin.map "#{service} :url", :action => 'shorten'
104 }