]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/rubyurl.rb
quiz plugin: initial support for unicode (UTF-8) answers
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / rubyurl.rb
1 require "shorturl"
2 require "uri"
3
4 class RubyURL < Plugin
5
6   # return a help string when the bot is asked for help on this plugin
7   def help(plugin, topic="")
8     return "rubyurl <your long url>"
9   end
10
11   def shorten(m, params)
12     if (params[:url] == "help")
13       m.reply help(m.plugin)
14       return
15     end
16
17     url = params[:url]
18     begin
19       to_uri = URI.parse(url)
20       # We don't accept 'generic' URLs because almost everything gets in there
21       raise URI::InvalidURIError if to_uri.class == URI::Generic
22     rescue URI::InvalidURIError
23       m.reply "#{url} doesn't look like an URL to me ..."
24       return
25     end
26
27     short = WWW::ShortURL.shorten(url)
28
29     m.reply "#{url} shortened to #{short} on RubyURL"
30   end
31
32 end
33
34 # create an instance of the RubyURL class and register it as a plugin
35 rubyurl = RubyURL.new
36 rubyurl.map "rubyurl :url", :action => 'shorten'