3 Url = Struct.new("Url", :channel, :nick, :time, :url)
4 TITLE_RE = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
6 class UrlPlugin < Plugin
7 BotConfig.register BotConfigIntegerValue.new('url.max_urls',
8 :default => 100, :validate => Proc.new{|v| v > 0},
9 :desc => "Maximum number of urls to store. New urls replace oldest ones.")
10 BotConfig.register BotConfigBooleanValue.new('url.display_link_info',
12 :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)")
13 BotConfig.register BotConfigBooleanValue.new('url.titles_only',
15 :desc => "Only show info for links that have <title> tags (in other words, don't display info for jpegs, mpegs, etc.)")
19 @registry.set_default(Array.new)
22 def help(plugin, topic="")
23 "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>"
26 def get_title_from_html(pagedata)
27 return unless TITLE_RE.match(pagedata)
28 title = $1.strip.gsub(/\s*\n+\s*/, " ")
29 title = Utils.decode_html_entities title
33 def get_title_for_url(uri_str)
35 url = uri_str.kind_of?(URI) ? uri_str : URI.parse(uri_str)
36 return if url.scheme !~ /https?/
41 @bot.httputil.get_response(url) { |response|
44 if response['content-type'] =~ /^text\//
45 # since the content is 'text/*' and is small enough to
46 # be a webpage, retrieve the title from the page
47 debug "+ getting #{url.request_uri}"
49 # we look for the title in the first 4k bytes
50 # TODO make the amount of data configurable
51 response.partial_body(4096) { |part|
52 title = get_title_from_html(part)
55 # if nothing was found, return nothing
58 unless @bot.config['url.titles_only']
59 # content doesn't have title, just display info.
60 size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
61 size = size ? ", size: #{size} bytes" : ""
62 return "type: #{response['content-type']}#{size}"
65 when Net::HTTPResponse
66 return "Error getting link (#{response.code} - #{response.message})"
72 if e.class <= StandardError
74 debug e.backtrace.join("\n")
77 msg = e.respond_to?(:message) ? e.message : e.to_s
78 return "Error connecting to site (#{e.message})"
83 return unless m.kind_of?(PrivMessage)
85 # TODO support multiple urls in one line
86 if m.message =~ /(f|ht)tps?:\/\//
87 if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
89 list = @registry[m.target]
91 if @bot.config['url.display_link_info']
93 debug "Getting title for #{urlstr}..."
95 title = get_title_for_url urlstr
97 m.reply "[Link Info] #{title}"
100 debug "Title not found!"
108 # check to see if this url is already listed
109 return if list.find {|u| u.url == urlstr }
111 url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
112 debug "#{list.length} urls so far"
113 if list.length > @bot.config['url.max_urls']
116 debug "storing url #{url.url}"
118 debug "#{list.length} urls now"
119 @registry[m.target] = list
125 channel = params[:channel] ? params[:channel] : m.target
126 max = params[:limit].to_i
129 list = @registry[channel]
131 m.reply "no urls seen yet for channel #{channel}"
133 list[0..(max-1)].each do |url|
134 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
139 def search(m, params)
140 channel = params[:channel] ? params[:channel] : m.target
141 max = params[:limit].to_i
142 string = params[:string]
145 regex = Regexp.new(string, Regexp::IGNORECASE)
146 list = @registry[channel].find_all {|url|
147 regex.match(url.url) || regex.match(url.nick)
150 m.reply "no matches for channel #{channel}"
152 list[0..(max-1)].each do |url|
153 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
158 plugin = UrlPlugin.new
159 plugin.map 'urls search :channel :limit :string', :action => 'search',
160 :defaults => {:limit => 4},
161 :requirements => {:limit => /^\d+$/},
163 plugin.map 'urls search :limit :string', :action => 'search',
164 :defaults => {:limit => 4},
165 :requirements => {:limit => /^\d+$/},
167 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
168 :requirements => {:limit => /^\d+$/},
170 plugin.map 'urls :limit', :defaults => {:limit => 4},
171 :requirements => {:limit => /^\d+$/},