3 Url = Struct.new("Url", :channel, :nick, :time, :url)
4 TITLE_RE = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
5 LINK_INFO = "[Link Info]"
7 class UrlPlugin < Plugin
8 BotConfig.register BotConfigIntegerValue.new('url.max_urls',
9 :default => 100, :validate => Proc.new{|v| v > 0},
10 :desc => "Maximum number of urls to store. New urls replace oldest ones.")
11 BotConfig.register BotConfigBooleanValue.new('url.display_link_info',
13 :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)")
14 BotConfig.register BotConfigBooleanValue.new('url.titles_only',
16 :desc => "Only show info for links that have <title> tags (in other words, don't display info for jpegs, mpegs, etc.)")
17 BotConfig.register BotConfigBooleanValue.new('url.first_par',
19 :desc => "Also try to get the first paragraph of a web page")
23 @registry.set_default(Array.new)
26 def help(plugin, topic="")
27 "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>"
30 def get_title_from_html(pagedata)
31 return unless TITLE_RE.match(pagedata)
35 def get_title_for_url(uri_str)
37 url = uri_str.kind_of?(URI) ? uri_str : URI.parse(uri_str)
38 return if url.scheme !~ /https?/
43 range = @bot.config['http.info_bytes']
44 response = @bot.httputil.get_response(url, :range => "bytes=0-#{range}")
45 if response.code != "206" && response.code != "200"
46 return "Error getting link (#{response.code} - #{response.message})"
50 if response['content-type'] =~ /^text\//
52 body = response.body.slice(0, range)
55 # since the content is 'text/*' and is small enough to
56 # be a webpage, retrieve the title from the page
57 debug "+ getting #{url.request_uri}"
59 # we act differently depending on whether we want the first par or not:
60 # in the first case we download the initial part and the parse it; in the second
61 # case we only download as much as we need to find the title
62 if @bot.config['url.first_par']
63 first_par = Utils.ircify_first_html_par(body)
64 extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty?
65 title = get_title_from_html(body)
66 return "title: #{title}#{extra}" if title
68 title = get_title_from_html(body)
69 return "title: #{title}" if title
72 # if nothing was found, provide more basic info
75 debug response.to_hash.inspect
76 unless @bot.config['url.titles_only']
77 # content doesn't have title, just display info.
78 size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil
79 size = size ? ", size: #{size} bytes" : ""
80 return "type: #{response['content-type']}#{size}#{extra}"
84 debug e.backtrace.join("\n")
85 return "Error connecting to site (#{e.message})"
90 return unless m.kind_of?(PrivMessage)
92 # TODO support multiple urls in one line
93 if m.message =~ /(f|ht)tps?:\/\//
94 if m.message =~ /((f|ht)tps?:\/\/.*?)(?:\s+|$)/
96 list = @registry[m.target]
98 if @bot.config['url.display_link_info']
100 debug "Getting title for #{urlstr}..."
102 title = get_title_for_url urlstr
104 m.reply "#{LINK_INFO} #{title}", :overlong => :truncate
107 debug "Title not found!"
115 # check to see if this url is already listed
116 return if list.find {|u| u.url == urlstr }
118 url = Url.new(m.target, m.sourcenick, Time.new, urlstr)
119 debug "#{list.length} urls so far"
120 if list.length > @bot.config['url.max_urls']
123 debug "storing url #{url.url}"
125 debug "#{list.length} urls now"
126 @registry[m.target] = list
132 channel = params[:channel] ? params[:channel] : m.target
133 max = params[:limit].to_i
136 list = @registry[channel]
138 m.reply "no urls seen yet for channel #{channel}"
140 list[0..(max-1)].each do |url|
141 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
146 def search(m, params)
147 channel = params[:channel] ? params[:channel] : m.target
148 max = params[:limit].to_i
149 string = params[:string]
152 regex = Regexp.new(string, Regexp::IGNORECASE)
153 list = @registry[channel].find_all {|url|
154 regex.match(url.url) || regex.match(url.nick)
157 m.reply "no matches for channel #{channel}"
159 list[0..(max-1)].each do |url|
160 m.reply "[#{url.time.strftime('%Y/%m/%d %H:%M:%S')}] <#{url.nick}> #{url.url}"
165 plugin = UrlPlugin.new
166 plugin.map 'urls search :channel :limit :string', :action => 'search',
167 :defaults => {:limit => 4},
168 :requirements => {:limit => /^\d+$/},
170 plugin.map 'urls search :limit :string', :action => 'search',
171 :defaults => {:limit => 4},
172 :requirements => {:limit => /^\d+$/},
174 plugin.map 'urls :channel :limit', :defaults => {:limit => 4},
175 :requirements => {:limit => /^\d+$/},
177 plugin.map 'urls :limit', :defaults => {:limit => 4},
178 :requirements => {:limit => /^\d+$/},