diff options
author | Tom Gilbert <tom@linuxbrit.co.uk> | 2006-01-20 16:03:48 +0000 |
---|---|---|
committer | Tom Gilbert <tom@linuxbrit.co.uk> | 2006-01-20 16:03:48 +0000 |
commit | ddbe1833c44cc67a2a70d6548960a25b80166633 (patch) | |
tree | 165082074fd753b67684db74c18b7bc12f57d4b6 /data/rbot/plugins | |
parent | e8cc263c8aff27f3fb2849655cabdda412e89170 (diff) |
new submission from Chris
Diffstat (limited to 'data/rbot/plugins')
-rw-r--r-- | data/rbot/plugins/url.rb | 42 |
1 files changed, 39 insertions, 3 deletions
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 [<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 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'] |