From ddbe1833c44cc67a2a70d6548960a25b80166633 Mon Sep 17 00:00:00 2001 From: Tom Gilbert Date: Fri, 20 Jan 2006 16:03:48 +0000 Subject: new submission from Chris --- data/rbot/plugins/url.rb | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) (limited to 'data/rbot/plugins') diff --git a/data/rbot/plugins/url.rb b/data/rbot/plugins/url.rb index 7972037a..286e8c4a 100644 --- a/data/rbot/plugins/url.rb +++ b/data/rbot/plugins/url.rb @@ -1,17 +1,42 @@ +require 'open-uri' + Url = Struct.new("Url", :channel, :nick, :time, :url) +TITLE_RE = /<\s*title\s*>(.+)<\s*\/title\s*>/im 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.") + BotConfig.register BotConfigBooleanValue.new('url.say_titles', + :default => true, + :desc => "Get the title of any links pasted to the channel and display it (Also, tells if the link is broken)") def initialize super @registry.set_default(Array.new) end + def help(plugin, topic="") "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 get_title_for_url(url) + begin + pagedata = open(url).read + return unless TITLE_RE.match(pagedata) + title = $1.strip.gsub(/\s*\n\s*/, " ") + title = title[0..255] if title.length > 255 + "Title: #{title}" + + rescue SocketError => e + "Title: ^- Error connecting to site (#{e.message})" + rescue OpenURI::HTTPError => e + "Title: ^- Error getting page (#{e.message})" + rescue Exception => e + "Title: ^- Error: #{e.inspect}" + end + end + def listen(m) return unless m.kind_of?(PrivMessage) return if m.address? @@ -20,10 +45,21 @@ class UrlPlugin < Plugin if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/ urlstr = $1 list = @registry[m.target] + + if @bot.config['url.say_titles'] + debug "Getting title for #{urlstr}..." + title = get_title_for_url urlstr + if title + m.reply title + debug "Title found!" + else + debug "Title not found!" + end + end + # check to see if this url is already listed - return if list.find {|u| - u.url == urlstr - } + 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 > @bot.config['url.max_urls'] -- cgit v1.2.3