]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/url.rb
apply patch and close #37 and #33
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / url.rb
index ed82d1c19b745461a7792a3d04521425810f8d69..7972037adf68d81123bd46bd9e486fe2ff890c14 100644 (file)
@@ -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 [<max>=4] => list <max> last urls mentioned in current channel, urls <channel> [<max>=4] => list <max> last urls mentioned in <channel>, urls search <regexp> => search for matching urls, urls search <channel> <regexp>, search for matching urls in channel <channel>"
+    "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>"
   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