]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/url.rb
Sat Jul 30 22:33:36 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[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   BotConfig.register BotConfigIntegerValue.new('url.max_urls',
5     :default => 100, :validate => Proc.new{|v| v > 0},
6     :desc => "Maximum number of urls to store. New urls replace oldest ones.")
7   
8   def initialize
9     super
10     @registry.set_default(Array.new)
11   end
12   def help(plugin, topic="")
13     "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>"
14   end
15   def listen(m)
16     return unless m.kind_of?(PrivMessage)
17     return if m.address?
18     # TODO support multiple urls in one line
19     if m.message =~ /(f|ht)tps?:\/\//
20       if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
21         urlstr = $1
22         list = @registry[m.target]
23         # check to see if this url is already listed
24         return if list.find {|u|
25           u.url == urlstr
26         }
27         url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
28         debug "#{list.length} urls so far"
29         if list.length > @bot.config['url.max_urls']
30           list.pop
31         end
32         debug "storing url #{url.url}"
33         list.unshift url
34         debug "#{list.length} urls now"
35         @registry[m.target] = list
36       end
37     end
38   end
39
40   def urls(m, params)
41     channel = params[:channel] ? params[:channel] : m.target
42     max = params[:limit].to_i
43     max = 10 if max > 10
44     max = 1 if max < 1
45     list = @registry[channel]
46     if list.empty?
47       m.reply "no urls seen yet for channel #{channel}"
48     else
49       list[0..(max-1)].each do |url|
50         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
51       end
52     end
53   end
54
55   def search(m, params)
56     channel = params[:channel] ? params[:channel] : m.target
57     max = params[:limit].to_i
58     string = params[:string]
59     max = 10 if max > 10
60     max = 1 if max < 1
61     regex = Regexp.new(string)
62     list = @registry[channel].find_all {|url|
63       regex.match(url.url) || regex.match(url.nick)
64     }
65     if list.empty?
66       m.reply "no matches for channel #{channel}"
67     else
68       list[0..(max-1)].each do |url|
69         m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
70       end
71     end
72   end
73 end
74 plugin = UrlPlugin.new
75 plugin.map 'urls search :channel :limit :string', :action => 'search',
76                           :defaults => {:limit => 4},
77                           :requirements => {:limit => /^\d+$/},
78                           :public => false
79 plugin.map 'urls search :limit :string', :action => 'search',
80                           :defaults => {:limit => 4},
81                           :requirements => {:limit => /^\d+$/},
82                           :private => false
83 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
84                           :requirements => {:limit => /^\d+$/},
85                           :public => false
86 plugin.map 'urls :limit', :defaults => {:limit => 4},
87                           :requirements => {:limit => /^\d+$/},
88                           :private => false