summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/rubyurl.rb
blob: 96e78b9264df0a1a660f0fffc86ca66be1d70d18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
require "shorturl"
require "uri"

class RubyURL < Plugin

  # return a help string when the bot is asked for help on this plugin
  def help(plugin, topic="")
    return "rubyurl <your long url>"
  end

  def shorten(m, params)
    if (params[:url] == "help")
      m.reply help(m.plugin)
      return
    end

    url = params[:url]
    begin
      to_uri = URI.parse(url)
      # We don't accept 'generic' URLs because almost everything gets in there
      raise URI::InvalidURIError if to_uri.class == URI::Generic
    rescue URI::InvalidURIError
      m.reply "#{url} doesn't look like an URL to me ..."
      return
    end

    short = WWW::ShortURL.shorten(url)

    m.reply "#{url} shortened to #{short} on RubyURL"
  end

end

# create an instance of the RubyURL class and register it as a plugin
rubyurl = RubyURL.new
rubyurl.map "rubyurl :url", :action => 'shorten'