4 # :title: rbot utilities provider
6 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # TODO some of these Utils should be rewritten as extensions to the approriate
10 # standard Ruby classes and accordingly be moved to extends.rb
15 # Try to load htmlentities, fall back to an HTML escape table.
17 require 'htmlentities'
110 AFTER_PAR_PATH = /^(?:div|span)$/
111 AFTER_PAR_EX = /^(?:td|tr|tbody|table)$/
112 AFTER_PAR_CLASS = /body|message|text/i
118 # Some regular expressions to manage HTML data
121 TITLE_REGEX = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
124 HX_REGEX = /<h(\d)(?:\s+[^>]*)?>(.*?)<\/h\1>/im
126 PAR_REGEX = /<p(?:\s+[^>]*)?>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
128 # Some blogging and forum platforms use spans or divs with a 'body' or 'message' or 'text' in their class
129 # to mark actual text
130 AFTER_PAR1_REGEX = /<\w+\s+[^>]*(?:body|message|text|post)[^>]*>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
132 # At worst, we can try stuff which is comprised between two <br>
133 AFTER_PAR2_REGEX = /<br(?:\s+[^>]*)?\/?>.*?<\/?(?:br|p|div|html|body|table|td|tr)(?:\s+[^>]*)?\/?>/im
140 # Miscellaneous useful functions
142 @@bot = nil unless defined? @@bot
143 @@safe_save_dir = nil unless defined?(@@safe_save_dir)
150 # Set up some Utils routines which depend on the associated bot.
152 debug "initializing utils"
154 @@safe_save_dir = @@bot.path('safe_save')
161 SEC_PER_HR = SEC_PER_MIN * 60
163 SEC_PER_DAY = SEC_PER_HR * 24
165 SEC_PER_WK = SEC_PER_DAY * 7
166 # Seconds per (30-day) month
167 SEC_PER_MNTH = SEC_PER_DAY * 30
168 # Second per (non-leap) year
169 SEC_PER_YR = SEC_PER_DAY * 365
171 # Auxiliary method needed by Utils.secs_to_string
172 def Utils.secs_to_string_case(array, var, string, plural)
175 array << "1 #{string}"
177 array << "#{var} #{plural}"
181 # Turn a number of seconds into a human readable string, e.g
182 # 2 days, 3 hours, 18 minutes and 10 seconds
183 def Utils.secs_to_string(secs)
185 years, secs = secs.divmod SEC_PER_YR
186 secs_to_string_case(ret, years, _("year"), _("years")) if years > 0
187 months, secs = secs.divmod SEC_PER_MNTH
188 secs_to_string_case(ret, months, _("month"), _("months")) if months > 0
189 days, secs = secs.divmod SEC_PER_DAY
190 secs_to_string_case(ret, days, _("day"), _("days")) if days > 0
191 hours, secs = secs.divmod SEC_PER_HR
192 secs_to_string_case(ret, hours, _("hour"), _("hours")) if hours > 0
193 mins, secs = secs.divmod SEC_PER_MIN
194 secs_to_string_case(ret, mins, _("minute"), _("minutes")) if mins > 0
196 secs_to_string_case(ret, secs, _("second"), _("seconds")) if secs > 0 or ret.empty?
199 raise "Empty ret array!"
203 return [ret[0, ret.length-1].join(", ") , ret[-1]].join(_(" and "))
207 # Turn a number of seconds into a hours:minutes:seconds e.g.
208 # 3:18:10 or 5'12" or 7s
210 def Utils.secs_to_short(seconds)
211 secs = seconds.to_i # make sure it's an integer
212 mins, secs = secs.divmod 60
213 hours, mins = mins.divmod 60
215 return ("%s:%s:%s" % [hours, mins, secs])
217 return ("%s'%s\"" % [mins, secs])
219 return ("%ss" % [secs])
223 # Returns human readable time.
227 # :start_date, sets the time to measure against, defaults to now
228 # :date_format, used with <tt>to_formatted_s<tt>, default to :default
229 def Utils.timeago(time, options = {})
230 start_date = options.delete(:start_date) || Time.new
231 date_format = options.delete(:date_format) || "%x"
232 delta = (start_date - time).round
236 distance = Utils.age_string(delta)
238 _("%{d} from now") % {:d => distance}
240 _("%{d} ago") % {:d => distance}
245 # Converts age in seconds to "nn units". Inspired by previous attempts
246 # but also gitweb's age_string() sub
247 def Utils.age_string(secs)
250 Utils.age_string(-secs)
251 when secs > 2*SEC_PER_YR
252 _("%{m} years") % { :m => secs/SEC_PER_YR }
253 when secs > 2*SEC_PER_MNTH
254 _("%{m} months") % { :m => secs/SEC_PER_MNTH }
255 when secs > 2*SEC_PER_WK
256 _("%{m} weeks") % { :m => secs/SEC_PER_WK }
257 when secs > 2*SEC_PER_DAY
258 _("%{m} days") % { :m => secs/SEC_PER_DAY }
259 when secs > 2*SEC_PER_HR
260 _("%{m} hours") % { :m => secs/SEC_PER_HR }
261 when (20*SEC_PER_MIN..40*SEC_PER_MIN).include?(secs)
263 when (50*SEC_PER_MIN..70*SEC_PER_MIN).include?(secs)
264 # _("about one hour")
266 when (80*SEC_PER_MIN..100*SEC_PER_MIN).include?(secs)
267 _("an hour and a half")
268 when secs > 2*SEC_PER_MIN
269 _("%{m} minutes") % { :m => secs/SEC_PER_MIN }
271 _("%{m} seconds") % { :m => secs }
277 # Execute an external program, returning a String obtained by redirecting
278 # the program's standards errors and output
280 # TODO: find a way to expose some common errors (e.g. Errno::NOENT)
282 def Utils.safe_exec(command, *args)
283 output = IO.popen("-") { |p|
285 break p.readlines.join("\n")
288 $stderr.reopen($stdout)
290 rescue Exception => e
291 puts "exception #{e.pretty_inspect} trying to run #{command}"
294 puts "exec of #{command} failed"
298 raise "safe execution of #{command} returned #{$?}" unless $?.success?
302 # Try executing an external program, returning true if the run was successful
303 # and false otherwise
304 def Utils.try_exec(command, *args)
308 $stderr.reopen($stdout)
310 rescue Exception => e
322 # Safely (atomically) save to _file_, by passing a tempfile to the block
323 # and then moving the tempfile to its final location when done.
325 # call-seq: Utils.safe_save(file, &block)
327 def Utils.safe_save(file)
328 raise 'No safe save directory defined!' if @@safe_save_dir.nil?
329 basename = File.basename(file)
330 temp = Tempfile.new(basename,@@safe_save_dir)
332 yield temp if block_given?
334 File.rename(temp.path, file)
338 # Decode HTML entities in the String _str_, using HTMLEntities if the
339 # package was found, or UNESCAPE_TABLE otherwise.
342 if defined? ::HTMLEntities
343 if ::HTMLEntities.respond_to? :decode_entities
344 def Utils.decode_html_entities(str)
345 return HTMLEntities.decode_entities(str)
348 @@html_entities = HTMLEntities.new
349 def Utils.decode_html_entities(str)
350 return @@html_entities.decode str
354 def Utils.decode_html_entities(str)
355 return str.gsub(/(&(.+?);)/) {
357 # remove the 0-paddng from unicode integers
359 when /^#x([0-9a-fA-F]+)$/
360 symbol = $1.to_i(16).to_s
362 symbol = $1.to_i.to_s
365 # output the symbol's irc-translated character, or a * if it's unknown
366 UNESCAPE_TABLE[symbol] || (symbol.match(/^\d+$/) ? [symbol.to_i].pack("U") : '*')
371 # Try to grab and IRCify the first HTML par (<p> tag) in the given string.
372 # If possible, grab the one after the first heading
374 # It is possible to pass some options to determine how the stripping
375 # occurs. Currently supported options are
376 # strip:: Regex or String to strip at the beginning of the obtained
378 # min_spaces:: minimum number of spaces a paragraph should have
380 def Utils.ircify_first_html_par(xml_org, opts={})
381 if defined? ::Hpricot
382 Utils.ircify_first_html_par_wh(xml_org, opts)
384 Utils.ircify_first_html_par_woh(xml_org, opts)
388 # HTML first par grabber using hpricot
389 def Utils.ircify_first_html_par_wh(xml_org, opts={})
390 doc = Hpricot(xml_org)
392 # Strip styles and scripts
393 (doc/"style|script").remove
398 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
400 min_spaces = opts[:min_spaces] || 8
401 min_spaces = 0 if min_spaces < 0
405 pre_h = pars = by_span = nil
408 debug "Minimum number of spaces: #{min_spaces}"
410 # Initial attempt: <p> that follows <h\d>
412 pre_h = Hpricot::Elements[]
414 doc.search("*") { |e|
420 pre_h << e if found_h
423 debug "Hx: found: #{pre_h.pretty_inspect}"
428 txt = p.to_html.ircify_html
429 txt.sub!(strip, '') if strip
430 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
431 break unless txt.empty? or txt.count(" ") < min_spaces
434 return txt unless txt.empty? or txt.count(" ") < min_spaces
436 # Second natural attempt: just get any <p>
437 pars = doc/"p" if pars.nil?
438 debug "par: found: #{pars.pretty_inspect}"
441 txt = p.to_html.ircify_html
442 txt.sub!(strip, '') if strip
443 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
444 break unless txt.empty? or txt.count(" ") < min_spaces
447 return txt unless txt.empty? or txt.count(" ") < min_spaces
449 # Nothing yet ... let's get drastic: we look for non-par elements too,
450 # but only for those that match something that we know is likely to
453 # Some blogging and forum platforms use spans or divs with a 'body' or
454 # 'message' or 'text' in their class to mark actual text. Since we want
455 # the class match to be partial and case insensitive, we collect
456 # the common elements that may have this class and then filter out those
457 # we don't need. If no divs or spans are found, we'll accept additional
458 # elements too (td, tr, tbody, table).
460 by_span = Hpricot::Elements[]
461 extra = Hpricot::Elements[]
462 doc.search("*") { |el|
463 next if el.bogusetag?
466 by_span.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
468 extra.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
471 if by_span.empty? and not extra.empty?
474 debug "other \#1: found: #{by_span.pretty_inspect}"
479 txt = p.to_html.ircify_html
480 txt.sub!(strip, '') if strip
481 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
482 break unless txt.empty? or txt.count(" ") < min_spaces
485 return txt unless txt.empty? or txt.count(" ") < min_spaces
487 # At worst, we can try stuff which is comprised between two <br>
490 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
491 return txt unless txt.count(" ") < min_spaces
492 break if min_spaces == 0
497 # HTML first par grabber without hpricot
498 def Utils.ircify_first_html_par_woh(xml_org, opts={})
499 xml = xml_org.gsub(/<!--.*?-->/m,
500 "").gsub(/<script(?:\s+[^>]*)?>.*?<\/script>/im,
501 "").gsub(/<style(?:\s+[^>]*)?>.*?<\/style>/im,
502 "").gsub(/<select(?:\s+[^>]*)?>.*?<\/select>/im,
506 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
508 min_spaces = opts[:min_spaces] || 8
509 min_spaces = 0 if min_spaces < 0
514 debug "Minimum number of spaces: #{min_spaces}"
515 header_found = xml.match(HX_REGEX)
518 while txt.empty? or txt.count(" ") < min_spaces
519 candidate = header_found[PAR_REGEX]
520 break unless candidate
521 txt = candidate.ircify_html
523 txt.sub!(strip, '') if strip
524 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
528 return txt unless txt.empty? or txt.count(" ") < min_spaces
530 # If we haven't found a first par yet, try to get it from the whole
533 while txt.empty? or txt.count(" ") < min_spaces
534 candidate = header_found[PAR_REGEX]
535 break unless candidate
536 txt = candidate.ircify_html
538 txt.sub!(strip, '') if strip
539 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
542 return txt unless txt.empty? or txt.count(" ") < min_spaces
544 # Nothing yet ... let's get drastic: we look for non-par elements too,
545 # but only for those that match something that we know is likely to
550 while txt.empty? or txt.count(" ") < min_spaces
551 candidate = header_found[AFTER_PAR1_REGEX]
552 break unless candidate
553 txt = candidate.ircify_html
555 txt.sub!(strip, '') if strip
556 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
559 return txt unless txt.empty? or txt.count(" ") < min_spaces
563 while txt.empty? or txt.count(" ") < min_spaces
564 candidate = header_found[AFTER_PAR2_REGEX]
565 break unless candidate
566 txt = candidate.ircify_html
568 txt.sub!(strip, '') if strip
569 debug "(other attempt \#2) #{txt.inspect} has #{txt.count(" ")} spaces"
572 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
573 return txt unless txt.count(" ") < min_spaces
574 break if min_spaces == 0
579 # This method extracts title, content (first par) and extra
580 # information from the given document _doc_.
582 # _doc_ can be an URI, a Net::HTTPResponse or a String.
584 # If _doc_ is a String, only title and content information
585 # are retrieved (if possible), using standard methods.
587 # If _doc_ is an URI or a Net::HTTPResponse, additional
588 # information is retrieved, and special title/summary
589 # extraction routines are used if possible.
591 def Utils.get_html_info(doc, opts={})
594 Utils.get_string_html_info(doc, opts)
595 when Net::HTTPResponse
596 Utils.get_resp_html_info(doc, opts)
599 @@bot.httputil.get_response(doc) { |resp|
600 ret.replace Utils.get_resp_html_info(resp, opts)
608 class ::UrlLinkError < RuntimeError
611 # This method extracts title, content (first par) and extra
612 # information from the given Net::HTTPResponse _resp_.
614 # Currently, the only accepted options (in _opts_) are
615 # uri_fragment:: the URI fragment of the original request
616 # full_body:: get the whole body instead of
617 # @@bot.config['http.info_bytes'] bytes only
619 # Returns a DataStream with the following keys:
620 # text:: the (partial) body
621 # title:: the title of the document (if any)
622 # content:: the first paragraph of the document (if any)
624 # the headers of the Net::HTTPResponse. The value is
625 # a Hash whose keys are lowercase forms of the HTTP
626 # header fields, and whose values are Arrays.
628 def Utils.get_resp_html_info(resp, opts={})
630 when Net::HTTPSuccess
631 loc = URI.parse(resp['x-rbot-location'] || resp['location']) rescue nil
632 if loc and loc.fragment and not loc.fragment.empty?
633 opts[:uri_fragment] ||= loc.fragment
635 ret = DataStream.new(opts.dup)
636 ret[:headers] = resp.to_hash
637 ret[:text] = partial = opts[:full_body] ? resp.body : resp.partial_body(@@bot.config['http.info_bytes'])
639 filtered = Utils.try_htmlinfo_filters(ret)
643 elsif resp['content-type'] =~ /^text\/|(?:x|ht)ml/
644 ret.merge!(Utils.get_string_html_info(partial, opts))
648 raise UrlLinkError, "getting link (#{resp.code} - #{resp.message})"
652 # This method runs an appropriately-crafted DataStream _ds_ through the
653 # filters in the :htmlinfo filter group, in order. If one of the filters
654 # returns non-nil, its results are merged in _ds_ and returned. Otherwise
657 # The input DataStream should have the downloaded HTML as primary key
658 # (:text) and possibly a :headers key holding the resonse headers.
660 def Utils.try_htmlinfo_filters(ds)
661 filters = @@bot.filter_names(:htmlinfo)
662 return nil if filters.empty?
664 # TODO filter priority
666 debug "testing htmlinfo filter #{n}"
667 cur = @@bot.filter(@@bot.global_filter_name(n, :htmlinfo), ds)
668 debug "returned #{cur.pretty_inspect}"
671 return ds.merge(cur) if cur
674 # HTML info filters often need to check if the webpage location
675 # of a passed DataStream _ds_ matches a given Regexp.
676 def Utils.check_location(ds, rx)
679 loc = [h['x-rbot-location'],h['location']].flatten.grep(rx)
683 return loc.empty? ? nil : loc
686 # This method extracts title and content (first par)
687 # from the given HTML or XML document _text_, using
688 # standard methods (String#ircify_html_title,
689 # Utils.ircify_first_html_par)
691 # Currently, the only accepted option (in _opts_) is
692 # uri_fragment:: the URI fragment of the original request
694 def Utils.get_string_html_info(text, opts={})
695 debug "getting string html info"
697 title = txt.ircify_html_title
699 if frag = opts[:uri_fragment] and not frag.empty?
700 fragreg = /<a\s+(?:[^>]+\s+)?(?:name|id)=["']?#{frag}["']?[^>]*>/im
703 if txt.match(fragreg)
704 # grab the post-match
710 c_opts[:strip] ||= title
711 content = Utils.ircify_first_html_par(txt, c_opts)
712 content = nil if content.empty?
713 return {:title => title, :content => content}
716 # Get the first pars of the first _count_ _urls_.
717 # The pages are downloaded using the bot httputil service.
718 # Returns an array of the first paragraphs fetched.
719 # If (optional) _opts_ :message is specified, those paragraphs are
720 # echoed as replies to the IRC message passed as _opts_ :message
722 def Utils.get_first_pars(urls, count, opts={})
726 while count > 0 and urls.length > 0
731 info = Utils.get_html_info(URI.parse(url), opts)
737 msg.reply "[#{idx}] #{par}", :overlong => :truncate if msg
741 debug "Unable to retrieve #{url}: #{$!}"
748 # Returns a comma separated list except for the last element
749 # which is joined in with specified conjunction
751 def Utils.comma_list(words, options={})
752 defaults = { :join_with => ", ", :join_last_with => _(" and ") }
753 opts = defaults.merge(options)
758 [words[0..-2].join(opts[:join_with]), words.last].join(opts[:join_last_with])
765 Irc::Utils.bot = Irc::Bot::Plugins.manager.bot