summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/shortenurls.rb
blob: 81682581ecf2b2a55cf527ac8508b87b581c0b26 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#-- vim:sw=2:et
#++
#
# :title: ShortURL plugin for rbot
#
# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
# Copyright:: (C) 2007 Giuseppe Bilotta
# License:: GPL v2
#
# Plugin to handle ShortURL, merges the funcionality of the old rubyurl and tinyurl plugins
# Note that it's called ShortenURLs and not ShortURL, to prevent conflicts with
# the actual ruby package used

require "shorturl"
require "uri"

class ShortenURLs < Plugin
  # 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)/ } - @blacklist
    if @services.include?(:rubyurl)
      @services << :shorturl
    end
  end

  # return a help string when the bot is asked for help on this plugin
  def help(plugin, topic="")
    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"
  end

  # do the dirty job. This method can be called by other plugins, in which case you
  # should set the :called param to true
  def shorten(m, params)
    url = params[:url]
    if url == "help"
      m.reply help(m.plugin) unless params[:called]
      return nil
    end
    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 ..." unless params[:called]
      return nil
    end

    service = (params[:service] || m.plugin).to_sym
    service = :rubyurl if service == :shorturl

    tried = []
    short = nil

    begin
      tried << service
      raise WWW::InvalidService, "#{service} blacklisted" if @blacklist.include?(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
      pool = services - tried
      if pool.empty?
        m.reply "#{service} failed, and I don't know what else to try next" unless params[:called]
        return nil
      else
        service = pool.pick_one
        m.reply "#{tried.last} failed, I'll try #{service} instead" unless params[:called]
        retry
      end
    end

    m.reply "#{url} shortened to #{short}" unless params[:called]
    return short
  end

end

# create an instance of the RubyURL class and register it as a plugin
plugin = ShortenURLs.new

plugin.services.each { |service|
  plugin.map "#{service} :url", :action => 'shorten'
}