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'
108 AFTER_PAR_PATH = /^(?:div|span)$/
109 AFTER_PAR_EX = /^(?:td|tr|tbody|table)$/
110 AFTER_PAR_CLASS = /body|message|text/i
116 # Some regular expressions to manage HTML data
119 TITLE_REGEX = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
122 HX_REGEX = /<h(\d)(?:\s+[^>]*)?>(.*?)<\/h\1>/im
124 PAR_REGEX = /<p(?:\s+[^>]*)?>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
126 # Some blogging and forum platforms use spans or divs with a 'body' or 'message' or 'text' in their class
127 # to mark actual text
128 AFTER_PAR1_REGEX = /<\w+\s+[^>]*(?:body|message|text)[^>]*>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
130 # At worst, we can try stuff which is comprised between two <br>
131 AFTER_PAR2_REGEX = /<br(?:\s+[^>]*)?\/?>.*?<\/?(?:br|p|div|html|body|table|td|tr)(?:\s+[^>]*)?\/?>/im
138 # Miscellaneous useful functions
140 @@bot = nil unless defined? @@bot
141 @@safe_save_dir = nil unless defined?(@@safe_save_dir)
148 # Set up some Utils routines which depend on the associated bot.
150 debug "initializing utils"
152 @@safe_save_dir = @@bot.path('safe_save')
159 SEC_PER_HR = SEC_PER_MIN * 60
161 SEC_PER_DAY = SEC_PER_HR * 24
163 SEC_PER_WK = SEC_PER_DAY * 7
164 # Seconds per (30-day) month
165 SEC_PER_MNTH = SEC_PER_DAY * 30
166 # Second per (non-leap) year
167 SEC_PER_YR = SEC_PER_DAY * 365
169 # Auxiliary method needed by Utils.secs_to_string
170 def Utils.secs_to_string_case(array, var, string, plural)
173 array << "1 #{string}"
175 array << "#{var} #{plural}"
179 # Turn a number of seconds into a human readable string, e.g
180 # 2 days, 3 hours, 18 minutes and 10 seconds
181 def Utils.secs_to_string(secs)
183 years, secs = secs.divmod SEC_PER_YR
184 secs_to_string_case(ret, years, _("year"), _("years")) if years > 0
185 months, secs = secs.divmod SEC_PER_MNTH
186 secs_to_string_case(ret, months, _("month"), _("months")) if months > 0
187 days, secs = secs.divmod SEC_PER_DAY
188 secs_to_string_case(ret, days, _("day"), _("days")) if days > 0
189 hours, secs = secs.divmod SEC_PER_HR
190 secs_to_string_case(ret, hours, _("hour"), _("hours")) if hours > 0
191 mins, secs = secs.divmod SEC_PER_MIN
192 secs_to_string_case(ret, mins, _("minute"), _("minutes")) if mins > 0
194 secs_to_string_case(ret, secs, _("second"), _("seconds")) if secs > 0 or ret.empty?
197 raise "Empty ret array!"
201 return [ret[0, ret.length-1].join(", ") , ret[-1]].join(_(" and "))
205 # Turn a number of seconds into a hours:minutes:seconds e.g.
206 # 3:18:10 or 5'12" or 7s
208 def Utils.secs_to_short(seconds)
209 secs = seconds.to_i # make sure it's an integer
210 mins, secs = secs.divmod 60
211 hours, mins = mins.divmod 60
213 return ("%s:%s:%s" % [hours, mins, secs])
215 return ("%s'%s\"" % [mins, secs])
217 return ("%ss" % [secs])
221 # Returns human readable time.
225 # :start_date, sets the time to measure against, defaults to now
226 # :date_format, used with <tt>to_formatted_s<tt>, default to :default
227 def Utils.timeago(time, options = {})
228 start_date = options.delete(:start_date) || Time.new
229 date_format = options.delete(:date_format) || "%x"
230 delta = (start_date - time).round
234 distance = Utils.age_string(delta)
236 _("%{d} from now") % {:d => distance}
238 _("%{d} ago") % {:d => distance}
243 # Converts age in seconds to "nn units". Inspired by previous attempts
244 # but also gitweb's age_string() sub
245 def Utils.age_string(secs)
248 Utils.age_string(-secs)
249 when secs > 2*SEC_PER_YR
250 _("%{m} years") % { :m => secs/SEC_PER_YR }
251 when secs > 2*SEC_PER_MNTH
252 _("%{m} months") % { :m => secs/SEC_PER_MNTH }
253 when secs > 2*SEC_PER_WK
254 _("%{m} weeks") % { :m => secs/SEC_PER_WK }
255 when secs > 2*SEC_PER_DAY
256 _("%{m} days") % { :m => secs/SEC_PER_DAY }
257 when secs > 2*SEC_PER_HR
258 _("%{m} hours") % { :m => secs/SEC_PER_HR }
259 when (20*SEC_PER_MIN..40*SEC_PER_MIN).include?(secs)
261 when (50*SEC_PER_MIN..70*SEC_PER_MIN).include?(secs)
262 # _("about one hour")
264 when (80*SEC_PER_MIN..100*SEC_PER_MIN).include?(secs)
265 _("an hour and a half")
266 when secs > 2*SEC_PER_MIN
267 _("%{m} minutes") % { :m => secs/SEC_PER_MIN }
269 _("%{m} seconds") % { :m => secs }
275 # Execute an external program, returning a String obtained by redirecting
276 # the program's standards errors and output
278 # TODO: find a way to expose some common errors (e.g. Errno::NOENT)
280 def Utils.safe_exec(command, *args)
281 output = IO.popen("-") { |p|
283 break p.readlines.join("\n")
286 $stderr.reopen($stdout)
288 rescue Exception => e
289 puts "exception #{e.pretty_inspect} trying to run #{command}"
292 puts "exec of #{command} failed"
296 raise "safe execution of #{command} returned #{$?}" unless $?.success?
300 # Try executing an external program, returning true if the run was successful
301 # and false otherwise
302 def Utils.try_exec(command, *args)
306 $stderr.reopen($stdout)
308 rescue Exception => e
320 # Safely (atomically) save to _file_, by passing a tempfile to the block
321 # and then moving the tempfile to its final location when done.
323 # call-seq: Utils.safe_save(file, &block)
325 def Utils.safe_save(file)
326 raise 'No safe save directory defined!' if @@safe_save_dir.nil?
327 basename = File.basename(file)
328 temp = Tempfile.new(basename,@@safe_save_dir)
330 yield temp if block_given?
332 File.rename(temp.path, file)
336 # Decode HTML entities in the String _str_, using HTMLEntities if the
337 # package was found, or UNESCAPE_TABLE otherwise.
339 def Utils.decode_html_entities(str)
340 if defined? ::HTMLEntities
341 return HTMLEntities.decode_entities(str)
343 str.gsub(/(&(.+?);)/) {
345 # remove the 0-paddng from unicode integers
346 if symbol =~ /^#(\d+)$/
347 symbol = $1.to_i.to_s
350 # output the symbol's irc-translated character, or a * if it's unknown
351 UNESCAPE_TABLE[symbol] || (symbol.match(/^\d+$/) ? [symbol.to_i].pack("U") : '*')
356 # Try to grab and IRCify the first HTML par (<p> tag) in the given string.
357 # If possible, grab the one after the first heading
359 # It is possible to pass some options to determine how the stripping
360 # occurs. Currently supported options are
361 # strip:: Regex or String to strip at the beginning of the obtained
363 # min_spaces:: minimum number of spaces a paragraph should have
365 def Utils.ircify_first_html_par(xml_org, opts={})
366 if defined? ::Hpricot
367 Utils.ircify_first_html_par_wh(xml_org, opts)
369 Utils.ircify_first_html_par_woh(xml_org, opts)
373 # HTML first par grabber using hpricot
374 def Utils.ircify_first_html_par_wh(xml_org, opts={})
375 doc = Hpricot(xml_org)
377 # Strip styles and scripts
378 (doc/"style|script").remove
383 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
385 min_spaces = opts[:min_spaces] || 8
386 min_spaces = 0 if min_spaces < 0
390 pre_h = pars = by_span = nil
393 debug "Minimum number of spaces: #{min_spaces}"
395 # Initial attempt: <p> that follows <h\d>
397 pre_h = Hpricot::Elements[]
399 doc.search("*") { |e|
405 pre_h << e if found_h
408 debug "Hx: found: #{pre_h.pretty_inspect}"
413 txt = p.to_html.ircify_html
414 txt.sub!(strip, '') if strip
415 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
416 break unless txt.empty? or txt.count(" ") < min_spaces
419 return txt unless txt.empty? or txt.count(" ") < min_spaces
421 # Second natural attempt: just get any <p>
422 pars = doc/"p" if pars.nil?
423 debug "par: found: #{pars.pretty_inspect}"
426 txt = p.to_html.ircify_html
427 txt.sub!(strip, '') if strip
428 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
429 break unless txt.empty? or txt.count(" ") < min_spaces
432 return txt unless txt.empty? or txt.count(" ") < min_spaces
434 # Nothing yet ... let's get drastic: we look for non-par elements too,
435 # but only for those that match something that we know is likely to
438 # Some blogging and forum platforms use spans or divs with a 'body' or
439 # 'message' or 'text' in their class to mark actual text. Since we want
440 # the class match to be partial and case insensitive, we collect
441 # the common elements that may have this class and then filter out those
442 # we don't need. If no divs or spans are found, we'll accept additional
443 # elements too (td, tr, tbody, table).
445 by_span = Hpricot::Elements[]
446 extra = Hpricot::Elements[]
447 doc.search("*") { |el|
448 next if el.bogusetag?
451 by_span.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
453 extra.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
456 if by_span.empty? and not extra.empty?
459 debug "other \#1: found: #{by_span.pretty_inspect}"
464 txt = p.to_html.ircify_html
465 txt.sub!(strip, '') if strip
466 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
467 break unless txt.empty? or txt.count(" ") < min_spaces
470 return txt unless txt.empty? or txt.count(" ") < min_spaces
472 # At worst, we can try stuff which is comprised between two <br>
475 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
476 return txt unless txt.count(" ") < min_spaces
477 break if min_spaces == 0
482 # HTML first par grabber without hpricot
483 def Utils.ircify_first_html_par_woh(xml_org, opts={})
484 xml = xml_org.gsub(/<!--.*?-->/m, '').gsub(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "").gsub(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
487 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
489 min_spaces = opts[:min_spaces] || 8
490 min_spaces = 0 if min_spaces < 0
495 debug "Minimum number of spaces: #{min_spaces}"
496 header_found = xml.match(HX_REGEX)
499 while txt.empty? or txt.count(" ") < min_spaces
500 candidate = header_found[PAR_REGEX]
501 break unless candidate
502 txt = candidate.ircify_html
504 txt.sub!(strip, '') if strip
505 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
509 return txt unless txt.empty? or txt.count(" ") < min_spaces
511 # If we haven't found a first par yet, try to get it from the whole
514 while txt.empty? or txt.count(" ") < min_spaces
515 candidate = header_found[PAR_REGEX]
516 break unless candidate
517 txt = candidate.ircify_html
519 txt.sub!(strip, '') if strip
520 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
523 return txt unless txt.empty? or txt.count(" ") < min_spaces
525 # Nothing yet ... let's get drastic: we look for non-par elements too,
526 # but only for those that match something that we know is likely to
531 while txt.empty? or txt.count(" ") < min_spaces
532 candidate = header_found[AFTER_PAR1_REGEX]
533 break unless candidate
534 txt = candidate.ircify_html
536 txt.sub!(strip, '') if strip
537 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
540 return txt unless txt.empty? or txt.count(" ") < min_spaces
544 while txt.empty? or txt.count(" ") < min_spaces
545 candidate = header_found[AFTER_PAR2_REGEX]
546 break unless candidate
547 txt = candidate.ircify_html
549 txt.sub!(strip, '') if strip
550 debug "(other attempt \#2) #{txt.inspect} has #{txt.count(" ")} spaces"
553 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
554 return txt unless txt.count(" ") < min_spaces
555 break if min_spaces == 0
560 # This method extracts title, content (first par) and extra
561 # information from the given document _doc_.
563 # _doc_ can be an URI, a Net::HTTPResponse or a String.
565 # If _doc_ is a String, only title and content information
566 # are retrieved (if possible), using standard methods.
568 # If _doc_ is an URI or a Net::HTTPResponse, additional
569 # information is retrieved, and special title/summary
570 # extraction routines are used if possible.
572 def Utils.get_html_info(doc, opts={})
575 Utils.get_string_html_info(doc, opts)
576 when Net::HTTPResponse
577 Utils.get_resp_html_info(doc, opts)
580 @@bot.httputil.get_response(doc) { |resp|
581 ret.replace Utils.get_resp_html_info(resp, opts)
589 class ::UrlLinkError < RuntimeError
592 # This method extracts title, content (first par) and extra
593 # information from the given Net::HTTPResponse _resp_.
595 # Currently, the only accepted options (in _opts_) are
596 # uri_fragment:: the URI fragment of the original request
597 # full_body:: get the whole body instead of
598 # @@bot.config['http.info_bytes'] bytes only
600 # Returns a DataStream with the following keys:
601 # text:: the (partial) body
602 # title:: the title of the document (if any)
603 # content:: the first paragraph of the document (if any)
605 # the headers of the Net::HTTPResponse. The value is
606 # a Hash whose keys are lowercase forms of the HTTP
607 # header fields, and whose values are Arrays.
609 def Utils.get_resp_html_info(resp, opts={})
611 when Net::HTTPSuccess
612 loc = URI.parse(resp['x-rbot-location'] || resp['location']) rescue nil
613 if loc and loc.fragment and not loc.fragment.empty?
614 opts[:uri_fragment] ||= loc.fragment
616 ret = DataStream.new(opts.dup)
617 ret[:headers] = resp.to_hash
618 ret[:text] = partial = opts[:full_body] ? resp.body : resp.partial_body(@@bot.config['http.info_bytes'])
620 filtered = Utils.try_htmlinfo_filters(ret)
624 elsif resp['content-type'] =~ /^text\/|(?:x|ht)ml/
625 ret.merge!(Utils.get_string_html_info(partial, opts))
629 raise UrlLinkError, "getting link (#{resp.code} - #{resp.message})"
633 # This method runs an appropriately-crafted DataStream _ds_ through the
634 # filters in the :htmlinfo filter group, in order. If one of the filters
635 # returns non-nil, its results are merged in _ds_ and returned. Otherwise
638 # The input DataStream should have the downloaded HTML as primary key
639 # (:text) and possibly a :headers key holding the resonse headers.
641 def Utils.try_htmlinfo_filters(ds)
642 filters = @@bot.filter_names(:htmlinfo)
643 return nil if filters.empty?
645 # TODO filter priority
647 debug "testing htmlinfo filter #{n}"
648 cur = @@bot.filter(@@bot.global_filter_name(n, :htmlinfo), ds)
649 debug "returned #{cur.pretty_inspect}"
652 return ds.merge(cur) if cur
655 # HTML info filters often need to check if the webpage location
656 # of a passed DataStream _ds_ matches a given Regexp.
657 def Utils.check_location(ds, rx)
660 loc = [h['x-rbot-location'],h['location']].flatten.grep(rx)
664 return loc.empty? ? nil : loc
667 # This method extracts title and content (first par)
668 # from the given HTML or XML document _text_, using
669 # standard methods (String#ircify_html_title,
670 # Utils.ircify_first_html_par)
672 # Currently, the only accepted option (in _opts_) is
673 # uri_fragment:: the URI fragment of the original request
675 def Utils.get_string_html_info(text, opts={})
676 debug "getting string html info"
678 title = txt.ircify_html_title
680 if frag = opts[:uri_fragment] and not frag.empty?
681 fragreg = /<a\s+(?:[^>]+\s+)?(?:name|id)=["']?#{frag}["']?[^>]*>/im
684 if txt.match(fragreg)
685 # grab the post-match
691 c_opts[:strip] ||= title
692 content = Utils.ircify_first_html_par(txt, c_opts)
693 content = nil if content.empty?
694 return {:title => title, :content => content}
697 # Get the first pars of the first _count_ _urls_.
698 # The pages are downloaded using the bot httputil service.
699 # Returns an array of the first paragraphs fetched.
700 # If (optional) _opts_ :message is specified, those paragraphs are
701 # echoed as replies to the IRC message passed as _opts_ :message
703 def Utils.get_first_pars(urls, count, opts={})
707 while count > 0 and urls.length > 0
712 info = Utils.get_html_info(URI.parse(url), opts)
718 msg.reply "[#{idx}] #{par}", :overlong => :truncate if msg
722 debug "Unable to retrieve #{url}: #{$!}"
729 # Returns a comma separated list except for the last element
730 # which is joined in with specified conjunction
732 def Utils.comma_list(words, options={})
733 defaults = { :join_with => ", ", :join_last_with => _(" and ") }
734 opts = defaults.merge(options)
739 [words[0..-2].join(opts[:join_with]), words.last].join(opts[:join_last_with])
746 Irc::Utils.bot = Irc::Bot::Plugins.manager.bot