]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/url.rb
ced921330882844ee405aa74e49d820982788600
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / url.rb
1 Url = Struct.new("Url", :channel, :nick, :time, :url)
2
3 class UrlPlugin < Plugin
4   def initialize
5     super
6     @registry.set_default(Array.new)
7   end
8   def help(plugin, topic="")
9     "urls [<max>=4] => list <max> last urls mentioned in current channel, urls search [<max>=4] <regexp> => search for matching urls. In a private message, you must specify the channel to query, eg. urls <channel> [max], urls search <channel> [max] <regexp>"
10   end
11   def listen(m)
12     return unless m.kind_of?(PrivMessage)
13     return if m.address?
14     # TODO support multiple urls in one line
15     if m.message =~ /(f|ht)tps?:\/\//
16       if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
17         urlstr = $1
18         list = @registry[m.target]
19         # check to see if this url is already listed
20         return if list.find {|u|
21           u.url == urlstr
22         }
23         url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
24         debug "#{list.length} urls so far"
25         if list.length > 50 # TODO make this configurable
26           list.pop
27         end
28         debug "storing url #{url.url}"
29         list.unshift url
30         debug "#{list.length} urls now"
31         @registry[m.target] = list
32       end
33     end
34   end
35
36   def urls(m, params)
37     channel = params[:channel] ? params[:channel] : m.target
38     max = params[:limit].to_i
39     max = 10 if max > 10
40     max = 1 if max < 1
41     list = @registry[channel]
42     if list.empty?
43       m.reply "no urls seen yet for channel #{channel}"
44     else
45       list[0..(max-1)].each do |url|
46         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
47       end
48     end
49   end
50
51   def search(m, params)
52     channel = params[:channel] ? params[:channel] : m.target
53     max = params[:limit].to_i
54     string = params[:string]
55     max = 10 if max > 10
56     max = 1 if max < 1
57     regex = Regexp.new(string)
58     list = @registry[channel].find_all {|url|
59       regex.match(url.url) || regex.match(url.nick)
60     }
61     if list.empty?
62       m.reply "no matches for channel #{channel}"
63     else
64       list[0..(max-1)].each do |url|
65         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
66       end
67     end
68   end
69 end
70 plugin = UrlPlugin.new
71 plugin.map 'urls search :channel :limit :string', :action => 'search',
72                           :defaults => {:limit => 4},
73                           :requirements => {:limit => /^\d+$/},
74                           :public => false
75 plugin.map 'urls search :limit :string', :action => 'search',
76                           :defaults => {:limit => 4},
77                           :requirements => {:limit => /^\d+$/},
78                           :private => false
79 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
80                           :requirements => {:limit => /^\d+$/},
81                           :public => false
82 plugin.map 'urls :limit', :defaults => {:limit => 4},
83                           :requirements => {:limit => /^\d+$/},
84                           :private => false