aboutsummaryrefslogtreecommitdiff
blob: 91f601ffd1fab070fc03b75d7ad1e8e2730b964e (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
require "shorturl"
class GentooShortenURLs < Plugin
  def initialize
    super
    @@cached = {}
    @@cached['lasturl'] = {}
  end

  def lurk?(m)
    replyto = nil
    replyto = m.replyto.to_s if m.is_a?(Irc::UserMessage)
    return true
    return false unless replyto
  end
  def listen(m)
    return if m.address?
    return unless m.is_a?(Irc::UserMessage)
    #return unless lurk?(m)
    return unless m.message =~ /(\b|^)[a-z]+:\/\/.*($|\s)/i
    m.message.split.each do |word|
      next unless word =~ /(\b|^)[a-z]+:\/\/.*($|\s)/i
      #next unless word.length >= 32
      #shrink(m, {:url => word})
      set_lasturl(m, word)
    end
  end
  def shrink(m, params)
    short = ShortURL.shorten(params[:url], :tinyurl)
    m.reply short
  end
  def fetch_lasturl(m)
    address = m.replyto.to_s
    url = [0, nil]
    url = @@cached['lasturl'][address] if @@cached['lasturl'].has_key?(address)
    return url
  end
  def set_lasturl(m, url)
    address = m.replyto.to_s
    @@cached['lasturl'][address] = [Time.now.tv_sec, url]
  end
  def lasturl(m, params)
  	url = fetch_lasturl(m)
	if url[1]
	  shrink(m, {:url => url})
	else
	  m.reply "No URL seen yet"
	end
  end
end
plugin = GentooShortenURLs.new
plugin.map 't :url',
  :action => 'shrink',
  :auth_path => 'view'
plugin.map 'lasturl',
  :action => 'lasturl',
  :auth_path => 'view'