5 Url = Struct.new("Url", :channel, :nick, :time, :url)
6 TITLE_RE = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
8 class UrlPlugin < Plugin
9 BotConfig.register BotConfigIntegerValue.new('url.max_urls',
10 :default => 100, :validate => Proc.new{|v| v > 0},
11 :desc => "Maximum number of urls to store. New urls replace oldest ones.")
12 BotConfig.register BotConfigBooleanValue.new('url.display_link_info',
14 :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)")
18 @registry.set_default(Array.new)
21 def help(plugin, topic="")
22 "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>"
25 def get_title_from_html(pagedata)
26 return unless TITLE_RE.match(pagedata)
27 title = $1.strip.gsub(/\s*\n+\s*/, " ")
28 title = CGI::unescapeHTML title
29 title = title[0..255] if title.length > 255
30 "[Link Info] title: #{title}"
33 def get_title_for_url(uri_str, depth=10)
34 # This god-awful mess is what the ruby http library has reduced me to.
35 # Python's HTTP lib is so much nicer. :~(
38 raise "Error: Maximum redirects hit."
41 puts "+ Getting #{uri_str}"
42 url = URI.parse(uri_str)
43 return if url.scheme !~ /https?/
45 puts "+ connecting to #{url.host}:#{url.port}"
46 http = @bot.httputil.get_proxy(url)
47 title = http.start do |http|
48 url.path = '/' if url.path == ''
49 head = http.request_head(url.path)
51 when Net::HTTPRedirection then
52 # call self recursively if this is a redirect
53 redirect_to = head['location']
54 puts "+ redirect location: #{redirect_to}"
55 url = URI.join url.to_s, redirect_to
56 puts "+ whee, redirecting to #{url.to_s}!"
57 title = get_title_for_url(url.to_s, depth-1)
58 when Net::HTTPSuccess then
59 if head['content-type'] =~ /^text\// and (not head['content-length'] or head['content-length'].to_i < 400000)
60 # since the content is 'text/*' and is small enough to
61 # be a webpage, retrieve the title from the page
62 puts "+ getting #{url.request_uri}"
63 response = http.request_get(url.request_uri)
64 return get_title_from_html(response.body)
66 # content doesn't have title, just display info.
67 size = head['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
68 #lastmod = head['last-modified']
69 return "[Link Info] type: #{head['content-type']}#{size ? ", size: #{size} bytes" : ""}"
71 when Net::HTTPClientError then
72 return "[Link Info] Error getting link (#{head.code} - #{head.message})"
73 when Net::HTTPServerError then
74 return "[Link Info] Error getting link (#{head.code} - #{head.message})"
77 rescue SocketError => e
78 return "[Link Info] Error connecting to site (#{e.message})"
82 return unless m.kind_of?(PrivMessage)
84 # TODO support multiple urls in one line
85 if m.message =~ /(f|ht)tps?:\/\//
86 if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
88 list = @registry[m.target]
90 if @bot.config['url.display_link_info']
91 debug "Getting title for #{urlstr}..."
92 title = get_title_for_url urlstr
97 debug "Title not found!"
101 # check to see if this url is already listed
102 return if list.find {|u| u.url == urlstr }
104 url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
105 debug "#{list.length} urls so far"
106 if list.length > @bot.config['url.max_urls']
109 debug "storing url #{url.url}"
111 debug "#{list.length} urls now"
112 @registry[m.target] = list
118 channel = params[:channel] ? params[:channel] : m.target
119 max = params[:limit].to_i
122 list = @registry[channel]
124 m.reply "no urls seen yet for channel #{channel}"
126 list[0..(max-1)].each do |url|
127 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
132 def search(m, params)
133 channel = params[:channel] ? params[:channel] : m.target
134 max = params[:limit].to_i
135 string = params[:string]
138 regex = Regexp.new(string, Regexp::IGNORECASE)
139 list = @registry[channel].find_all {|url|
140 regex.match(url.url) || regex.match(url.nick)
143 m.reply "no matches for channel #{channel}"
145 list[0..(max-1)].each do |url|
146 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
151 plugin = UrlPlugin.new
152 plugin.map 'urls search :channel :limit :string', :action => 'search',
153 :defaults => {:limit => 4},
154 :requirements => {:limit => /^\d+$/},
156 plugin.map 'urls search :limit :string', :action => 'search',
157 :defaults => {:limit => 4},
158 :requirements => {:limit => /^\d+$/},
160 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
161 :requirements => {:limit => /^\d+$/},
163 plugin.map 'urls :limit', :defaults => {:limit => 4},
164 :requirements => {:limit => /^\d+$/},