X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Furl.rb;h=7972037adf68d81123bd46bd9e486fe2ff890c14;hb=c2d9108e50ca073aa0670755682a355d8f04f697;hp=ed82d1c19b745461a7792a3d04521425810f8d69;hpb=21949774b91eaec6ecde4eaa8ad121e2c0a36b87;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/url.rb b/data/rbot/plugins/url.rb index ed82d1c1..7972037a 100644 --- a/data/rbot/plugins/url.rb +++ b/data/rbot/plugins/url.rb @@ -1,12 +1,16 @@ Url = Struct.new("Url", :channel, :nick, :time, :url) class UrlPlugin < Plugin + BotConfig.register BotConfigIntegerValue.new('url.max_urls', + :default => 100, :validate => Proc.new{|v| v > 0}, + :desc => "Maximum number of urls to store. New urls replace oldest ones.") + def initialize super @registry.set_default(Array.new) end def help(plugin, topic="") - "urls [=4] => list last urls mentioned in current channel, urls [=4] => list last urls mentioned in , urls search => search for matching urls, urls search , search for matching urls in channel " + "urls [=4] => list last urls mentioned in current channel, urls search [=4] => search for matching urls. In a private message, you must specify the channel to query, eg. urls [max], urls search [max] " end def listen(m) return unless m.kind_of?(PrivMessage) @@ -14,10 +18,15 @@ class UrlPlugin < Plugin # TODO support multiple urls in one line if m.message =~ /(f|ht)tps?:\/\// if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/ - url = Url.new(m.target, m.sourcenick, Time.new, $1) + urlstr = $1 list = @registry[m.target] + # check to see if this url is already listed + return if list.find {|u| + u.url == urlstr + } + url = Url.new(m.target, m.sourcenick, Time.new, urlstr) debug "#{list.length} urls so far" - if list.length > 50 + if list.length > @bot.config['url.max_urls'] list.pop end debug "storing url #{url.url}" @@ -27,45 +36,10 @@ class UrlPlugin < Plugin end end end - def privmsg(m) - case m.params - when nil - if m.public? - urls m, m.target - else - m.reply "in a private message, you need to specify a channel name for urls" - end - when (/^(\d+)$/) - max = $1.to_i - if m.public? - urls m, m.target, max - else - m.reply "in a private message, you need to specify a channel name for urls" - end - when (/^(#.*?)\s+(\d+)$/) - channel = $1 - max = $2.to_i - urls m, channel, max - when (/^(#.*?)$/) - channel = $1 - urls m, channel - when (/^search\s+(#.*?)\s+(.*)$/) - channel = $1 - string = $2 - search m, channel, string - when (/^search\s+(.*)$/) - string = $1 - if m.public? - search m, m.target, string - else - m.reply "in a private message, you need to specify a channel name for urls" - end - else - m.reply "incorrect usage: " + help(m.plugin) - end - end - def urls(m, channel, max=4) + def urls(m, params) + channel = params[:channel] ? params[:channel] : m.target + max = params[:limit].to_i max = 10 if max > 10 max = 1 if max < 1 list = @registry[channel] @@ -78,7 +52,10 @@ class UrlPlugin < Plugin end end - def search(m, channel, string, max=4) + def search(m, params) + channel = params[:channel] ? params[:channel] : m.target + max = params[:limit].to_i + string = params[:string] max = 10 if max > 10 max = 1 if max < 1 regex = Regexp.new(string) @@ -95,4 +72,17 @@ class UrlPlugin < Plugin end end plugin = UrlPlugin.new -plugin.register("urls") +plugin.map 'urls search :channel :limit :string', :action => 'search', + :defaults => {:limit => 4}, + :requirements => {:limit => /^\d+$/}, + :public => false +plugin.map 'urls search :limit :string', :action => 'search', + :defaults => {:limit => 4}, + :requirements => {:limit => /^\d+$/}, + :private => false +plugin.map 'urls :channel :limit', :defaults => {:limit => 4}, + :requirements => {:limit => /^\d+$/}, + :public => false +plugin.map 'urls :limit', :defaults => {:limit => 4}, + :requirements => {:limit => /^\d+$/}, + :private => false