1 Url = Struct.new("Url", :channel, :nick, :time, :url)
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.")
10 @registry.set_default(Array.new)
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>"
16 return unless m.kind_of?(PrivMessage)
18 # TODO support multiple urls in one line
19 if m.message =~ /(f|ht)tps?:\/\//
20 if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
22 list = @registry[m.target]
23 # check to see if this url is already listed
24 return if list.find {|u|
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']
32 debug "storing url #{url.url}"
34 debug "#{list.length} urls now"
35 @registry[m.target] = list
41 channel = params[:channel] ? params[:channel] : m.target
42 max = params[:limit].to_i
45 list = @registry[channel]
47 m.reply "no urls seen yet for channel #{channel}"
49 list[0..(max-1)].each do |url|
50 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
56 channel = params[:channel] ? params[:channel] : m.target
57 max = params[:limit].to_i
58 string = params[:string]
61 regex = Regexp.new(string)
62 list = @registry[channel].find_all {|url|
63 regex.match(url.url) || regex.match(url.nick)
66 m.reply "no matches for channel #{channel}"
68 list[0..(max-1)].each do |url|
69 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
74 plugin = UrlPlugin.new
75 plugin.map 'urls search :channel :limit :string', :action => 'search',
76 :defaults => {:limit => 4},
77 :requirements => {:limit => /^\d+$/},
79 plugin.map 'urls search :limit :string', :action => 'search',
80 :defaults => {:limit => 4},
81 :requirements => {:limit => /^\d+$/},
83 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
84 :requirements => {:limit => /^\d+$/},
86 plugin.map 'urls :limit', :defaults => {:limit => 4},
87 :requirements => {:limit => /^\d+$/},