4 # :title: rbot utilities provider
6 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2002-2006 Tom Gilbert
10 # Copyright:: (C) 2007 Giuseppe Bilotta
12 # TODO some of these Utils should be rewritten as extensions to the approriate
13 # standard Ruby classes and accordingly be moved to extends.rb
18 # Try to load htmlentities, fall back to an HTML escape table.
20 require 'htmlentities'
24 gems = require 'rubygems'
53 # extras codes, for future use...
67 'otimes' => '⊗',
76 'Epsilon' => 'Ε',
80 'Upsilon' => 'Υ',
82 'there4' => '∴',
87 'rsaquo' => '›',
101 'rdquo' => '”',
109 'lfloor' => '⌊',
116 'clubs' => '♣',
117 'diams' => '♦',
124 'Scaron' => 'Š',
130 'sbquo' => '‚',
143 'infin' => '∞',
148 'thinsp' => ' ',
150 'bdquo' => '„',
157 'mdash' => '—',
159 'permil' => '‰',
164 'forall' => '∀',
166 'rceil' => '⌉',
169 'lambda' => 'λ',
173 'dagger' => '†',
176 'image' => 'ℑ',
177 'alefsym' => 'ℵ',
183 'frasl' => '⁄',
185 'lowast' => '∗',
196 'oline' => '‾',
203 'empty' => '∅',
210 'weierp' => '℘',
215 'omicron' => 'ο',
216 'upsilon' => 'υ',
218 'Lambda' => 'Λ',
225 'scaron' => 'š',
226 'lsquo' => '‘',
234 'hellip' => '…',
238 'rfloor' => '⌋',
240 'crarr' => '↵',
242 'notin' => '∉',
243 'exist' => '∃',
246 'Dagger' => '‡',
247 'oplus' => '⊕',
253 'lsaquo' => '‹',
255 'Omicron' => 'Ο',
270 'sigmaf' => 'ς',
272 'minus' => '−',
275 'epsilon' => 'ε',
286 'spades' => '♠',
287 'rsquo' => '’',
291 'thetasym' => 'ϑ',
295 'ldquo' => '“',
296 'hearts' => '♥',
310 AFTER_PAR_PATH = /^(?:div|span|td|tr|tbody|table)$/
311 AFTER_PAR_CLASS = /body|message|text/i
317 gems = require 'rubygems'
326 # Some regular expressions to manage HTML data
329 TITLE_REGEX = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
332 HX_REGEX = /<h(\d)(?:\s+[^>]*)?>(.*?)<\/h\1>/im
334 PAR_REGEX = /<p(?:\s+[^>]*)?>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
336 # Some blogging and forum platforms use spans or divs with a 'body' or 'message' or 'text' in their class
337 # to mark actual text
338 AFTER_PAR1_REGEX = /<\w+\s+[^>]*(?:body|message|text)[^>]*>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
340 # At worst, we can try stuff which is comprised between two <br>
341 AFTER_PAR2_REGEX = /<br(?:\s+[^>]*)?\/?>.*?<\/?(?:br|p|div|html|body|table|td|tr)(?:\s+[^>]*)?\/?>/im
349 # Miscellaneous useful functions
351 @@bot = nil unless defined? @@bot
352 @@safe_save_dir = nil unless defined?(@@safe_save_dir)
359 # Set up some Utils routines which depend on the associated bot.
361 debug "initializing utils"
363 @@safe_save_dir = "#{@@bot.botclass}/safe_save"
370 SEC_PER_HR = SEC_PER_MIN * 60
372 SEC_PER_DAY = SEC_PER_HR * 24
373 # Seconds per (30-day) month
374 SEC_PER_MNTH = SEC_PER_DAY * 30
375 # Second per (30*12 = 360 day) year
376 SEC_PER_YR = SEC_PER_MNTH * 12
378 # Auxiliary method needed by Utils.secs_to_string
379 def Utils.secs_to_string_case(array, var, string, plural)
382 array << "1 #{string}"
384 array << "#{var} #{plural}"
388 # Turn a number of seconds into a human readable string, e.g
389 # 2 days, 3 hours, 18 minutes and 10 seconds
390 def Utils.secs_to_string(secs)
392 years, secs = secs.divmod SEC_PER_YR
393 secs_to_string_case(ret, years, _("year"), _("years")) if years > 0
394 months, secs = secs.divmod SEC_PER_MNTH
395 secs_to_string_case(ret, months, _("month"), _("months")) if months > 0
396 days, secs = secs.divmod SEC_PER_DAY
397 secs_to_string_case(ret, days, _("day"), _("days")) if days > 0
398 hours, secs = secs.divmod SEC_PER_HR
399 secs_to_string_case(ret, hours, _("hour"), _("hours")) if hours > 0
400 mins, secs = secs.divmod SEC_PER_MIN
401 secs_to_string_case(ret, mins, _("minute"), _("minutes")) if mins > 0
403 secs_to_string_case(ret, secs, _("second"), _("seconds")) if secs > 0 or ret.empty?
406 raise "Empty ret array!"
410 return [ret[0, ret.length-1].join(", ") , ret[-1]].join(_(" and "))
415 # Execute an external program, returning a String obtained by redirecting
416 # the program's standards errors and output
418 def Utils.safe_exec(command, *args)
421 return p.readlines.join("\n")
424 $stderr.reopen($stdout)
426 rescue Exception => e
427 puts "exec of #{command} led to exception: #{e.pretty_inspect}"
430 puts "exec of #{command} failed"
437 # Safely (atomically) save to _file_, by passing a tempfile to the block
438 # and then moving the tempfile to its final location when done.
440 # call-seq: Utils.safe_save(file, &block)
442 def Utils.safe_save(file)
443 raise 'No safe save directory defined!' if @@safe_save_dir.nil?
444 basename = File.basename(file)
445 temp = Tempfile.new(basename,@@safe_save_dir)
447 yield temp if block_given?
449 File.rename(temp.path, file)
453 # Decode HTML entities in the String _str_, using HTMLEntities if the
454 # package was found, or UNESCAPE_TABLE otherwise.
456 def Utils.decode_html_entities(str)
457 if defined? ::HTMLEntities
458 return HTMLEntities.decode_entities(str)
460 str.gsub(/(&(.+?);)/) {
462 # remove the 0-paddng from unicode integers
464 symbol = "##{$1.to_i.to_s}"
467 # output the symbol's irc-translated character, or a * if it's unknown
468 UNESCAPE_TABLE[symbol] || [symbol[/\d+/].to_i].pack("U") rescue '*'
473 # Try to grab and IRCify the first HTML par (<p> tag) in the given string.
474 # If possible, grab the one after the first heading
476 # It is possible to pass some options to determine how the stripping
477 # occurs. Currently supported options are
478 # strip:: Regex or String to strip at the beginning of the obtained
480 # min_spaces:: minimum number of spaces a paragraph should have
482 def Utils.ircify_first_html_par(xml_org, opts={})
483 if defined? ::Hpricot
484 Utils.ircify_first_html_par_wh(xml_org, opts)
486 Utils.ircify_first_html_par_woh(xml_org, opts)
490 # HTML first par grabber using hpricot
491 def Utils.ircify_first_html_par_wh(xml_org, opts={})
492 doc = Hpricot(xml_org)
494 # Strip styles and scripts
495 (doc/"style|script").remove
500 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
502 min_spaces = opts[:min_spaces] || 8
503 min_spaces = 0 if min_spaces < 0
507 pre_h = pars = by_span = nil
510 debug "Minimum number of spaces: #{min_spaces}"
512 # Initial attempt: <p> that follows <h\d>
514 pre_h = Hpricot::Elements[]
516 doc.root.search("*") { |e|
521 pre_h << e if found_h
524 debug "Hx: found: #{pre_h.pretty_inspect}"
529 txt = p.to_html.ircify_html
530 txt.sub!(strip, '') if strip
531 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
532 break unless txt.empty? or txt.count(" ") < min_spaces
535 return txt unless txt.empty? or txt.count(" ") < min_spaces
537 # Second natural attempt: just get any <p>
538 pars = doc/"p" if pars.nil?
539 debug "par: found: #{pars.pretty_inspect}"
542 txt = p.to_html.ircify_html
543 txt.sub!(strip, '') if strip
544 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
545 break unless txt.empty? or txt.count(" ") < min_spaces
548 return txt unless txt.empty? or txt.count(" ") < min_spaces
550 # Nothing yet ... let's get drastic: we look for non-par elements too,
551 # but only for those that match something that we know is likely to
554 # Some blogging and forum platforms use spans or divs with a 'body' or
555 # 'message' or 'text' in their class to mark actual text. Since we want
556 # the class match to be partial and case insensitive, we collect
557 # the common elements that may have this class and then filter out those
560 by_span = Hpricot::Elements[]
561 doc.root.search("*") { |el|
562 by_span.push el if el.pathname =~ AFTER_PAR_PATH and (el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS)
564 debug "other \#1: found: #{by_span.pretty_inspect}"
569 txt = p.to_html.ircify_html
570 txt.sub!(strip, '') if strip
571 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
572 break unless txt.empty? or txt.count(" ") < min_spaces
575 return txt unless txt.empty? or txt.count(" ") < min_spaces
577 # At worst, we can try stuff which is comprised between two <br>
580 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
581 return txt unless txt.count(" ") < min_spaces
582 break if min_spaces == 0
587 # HTML first par grabber without hpricot
588 def Utils.ircify_first_html_par_woh(xml_org, opts={})
589 xml = xml_org.gsub(/<!--.*?-->/m, '').gsub(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "").gsub(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
592 strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
594 min_spaces = opts[:min_spaces] || 8
595 min_spaces = 0 if min_spaces < 0
600 debug "Minimum number of spaces: #{min_spaces}"
601 header_found = xml.match(HX_REGEX)
604 while txt.empty? or txt.count(" ") < min_spaces
605 candidate = header_found[PAR_REGEX]
606 break unless candidate
607 txt = candidate.ircify_html
609 txt.sub!(strip, '') if strip
610 debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
614 return txt unless txt.empty? or txt.count(" ") < min_spaces
616 # If we haven't found a first par yet, try to get it from the whole
619 while txt.empty? or txt.count(" ") < min_spaces
620 candidate = header_found[PAR_REGEX]
621 break unless candidate
622 txt = candidate.ircify_html
624 txt.sub!(strip, '') if strip
625 debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
628 return txt unless txt.empty? or txt.count(" ") < min_spaces
630 # Nothing yet ... let's get drastic: we look for non-par elements too,
631 # but only for those that match something that we know is likely to
636 while txt.empty? or txt.count(" ") < min_spaces
637 candidate = header_found[AFTER_PAR1_REGEX]
638 break unless candidate
639 txt = candidate.ircify_html
641 txt.sub!(strip, '') if strip
642 debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
645 return txt unless txt.empty? or txt.count(" ") < min_spaces
649 while txt.empty? or txt.count(" ") < min_spaces
650 candidate = header_found[AFTER_PAR2_REGEX]
651 break unless candidate
652 txt = candidate.ircify_html
654 txt.sub!(strip, '') if strip
655 debug "(other attempt \#2) #{txt.inspect} has #{txt.count(" ")} spaces"
658 debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
659 return txt unless txt.count(" ") < min_spaces
660 break if min_spaces == 0
665 # This method extracts title, content (first par) and extra
666 # information from the given document _doc_.
668 # _doc_ can be an URI, a Net::HTTPResponse or a String.
670 # If _doc_ is a String, only title and content information
671 # are retrieved (if possible), using standard methods.
673 # If _doc_ is an URI or a Net::HTTPResponse, additional
674 # information is retrieved, and special title/summary
675 # extraction routines are used if possible.
677 def Utils.get_html_info(doc, opts={})
680 Utils.get_string_html_info(doc, opts)
681 when Net::HTTPResponse
682 Utils.get_resp_html_info(doc, opts)
684 if doc.fragment and not doc.fragment.empty?
685 opts[:uri_fragment] ||= doc.fragment
688 @@bot.httputil.get_response(doc) { |resp|
689 ret = Utils.get_resp_html_info(resp, opts)
697 class ::UrlLinkError < RuntimeError
700 # This method extracts title, content (first par) and extra
701 # information from the given Net::HTTPResponse _resp_.
703 # Currently, the only accepted option (in _opts_) is
704 # uri_fragment:: the URI fragment of the original request
706 # Returns a Hash with the following keys:
707 # title:: the title of the document (if any)
708 # content:: the first paragraph of the document (if any)
710 # the headers of the Net::HTTPResponse. The value is
711 # a Hash whose keys are lowercase forms of the HTTP
712 # header fields, and whose values are Arrays.
714 def Utils.get_resp_html_info(resp, opts={})
717 when Net::HTTPSuccess
718 ret[:headers] = resp.to_hash
720 if resp['content-type'] =~ /^text\/|(?:x|ht)ml/
721 partial = resp.partial_body(@@bot.config['http.info_bytes'])
722 ret.merge!(Utils.get_string_html_info(partial, opts))
726 raise UrlLinkError, "getting link (#{resp.code} - #{resp.message})"
730 # This method extracts title and content (first par)
731 # from the given HTML or XML document _text_, using
732 # standard methods (String#ircify_html_title,
733 # Utils.ircify_first_html_par)
735 # Currently, the only accepted option (in _opts_) is
736 # uri_fragment:: the URI fragment of the original request
738 def Utils.get_string_html_info(text, opts={})
740 title = txt.ircify_html_title
741 if frag = opts[:uri_fragment] and not frag.empty?
742 fragreg = /.*?<a\s+[^>]*name=["']?#{frag}["']?.*?>/im
746 c_opts[:strip] ||= title
747 content = Utils.ircify_first_html_par(txt, c_opts)
748 content = nil if content.empty?
749 return {:title => title, :content => content}
752 # Get the first pars of the first _count_ _urls_.
753 # The pages are downloaded using the bot httputil service.
754 # Returns an array of the first paragraphs fetched.
755 # If (optional) _opts_ :message is specified, those paragraphs are
756 # echoed as replies to the IRC message passed as _opts_ :message
758 def Utils.get_first_pars(urls, count, opts={})
762 while count > 0 and urls.length > 0
767 info = Utils.get_html_info(URI.parse(url), opts)
773 msg.reply "[#{idx}] #{par}", :overlong => :truncate if msg
777 debug "Unable to retrieve #{url}: #{$!}"
787 Irc::Utils.bot = Irc::Bot::Plugins.manager.bot