]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/url.rb
rss plugin: all watchers are now informed of feed updates when anybody asks for the...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / url.rb
index 373d5a876c1893a7502bc9843f9e9fe6e8e91556..c3463d5d30cbb3984b5c1b49325933f711cafd4c 100644 (file)
@@ -6,13 +6,14 @@ end
 class UrlPlugin < Plugin
   TITLE_RE = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
   LINK_INFO = "[Link Info]"
+  OUR_UNSAFE = Regexp.new("[^#{URI::PATTERN::UNRESERVED}#{URI::PATTERN::RESERVED}%# ]", false, 'N')
 
   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.")
-  BotConfig.register BotConfigBooleanValue.new('url.display_link_info',
-    :default => false,
-    :desc => "Get the title of any links pasted to the channel and display it (also tells if the link is broken or the site is down)")
+  BotConfig.register BotConfigIntegerValue.new('url.display_link_info',
+    :default => 0,
+    :desc => "Get the title of links pasted to the channel and display it (also tells if the link is broken or the site is down). Do it for at most this many links per line (set to 0 to disable)")
   BotConfig.register BotConfigBooleanValue.new('url.titles_only',
     :default => false,
     :desc => "Only show info for links that have <title> tags (in other words, don't display info for jpegs, mpegs, etc.)")
@@ -27,6 +28,9 @@ class UrlPlugin < Plugin
   def initialize
     super
     @registry.set_default(Array.new)
+    unless @bot.config['url.display_link_info'].kind_of?(Integer)
+      @bot.config.items[:'url.display_link_info'].set_string(@bot.config['url.display_link_info'].to_s)
+    end
   end
 
   def help(plugin, topic="")
@@ -131,15 +135,20 @@ class UrlPlugin < Plugin
   def listen(m)
     return unless m.kind_of?(PrivMessage)
     return if m.address?
-    urls = URI.extract(m.message)
+
+    escaped = URI.escape(m.message, OUR_UNSAFE)
+    urls = URI.extract(escaped)
     return if urls.empty?
     debug "found urls #{urls.inspect}"
     list = @registry[m.target]
+    urls_displayed = 0
     urls.each { |urlstr|
       debug "working on #{urlstr}"
       next unless urlstr =~ /^https?:/
       title = nil
-      if @bot.config['url.display_link_info']
+      debug "display link info: #{@bot.config['url.display_link_info']}"
+      if @bot.config['url.display_link_info'] > urls_displayed
+        urls_displayed += 1
         Thread.start do
           debug "Getting title for #{urlstr}..."
           begin