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