]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/utils.rb
084f92a5daf55d03d478ded2ba2c56d64205a695
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / utils.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot utilities provider
5 #
6 # Author:: Tom Gilbert <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
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
11
12 require 'tempfile'
13 require 'set'
14
15 # Try to load htmlentities, fall back to an HTML escape table.
16 begin
17   require 'htmlentities'
18 rescue LoadError
19   gems = nil
20   begin
21     gems = require 'rubygems'
22   rescue LoadError
23     gems = false
24   end
25   if gems
26     retry
27   else
28     module ::Irc
29       module Utils
30         UNESCAPE_TABLE = {
31     'laquo' => '«',
32     'raquo' => '»',
33     'quot' => '"',
34     'apos' => '\'',
35     'micro' => 'µ',
36     'copy' => '©',
37     'trade' => '™',
38     'reg' => '®',
39     'amp' => '&',
40     'lt' => '<',
41     'gt' => '>',
42     'hellip' => '…',
43     'nbsp' => ' ',
44     'Agrave' => 'À',
45     'Aacute' => 'Á',
46     'Acirc' => 'Â',
47     'Atilde' => 'Ã',
48     'Auml' => 'Ä',
49     'Aring' => 'Å',
50     'AElig' => 'Æ',
51     'OElig' => 'Œ',
52     'Ccedil' => 'Ç',
53     'Egrave' => 'È',
54     'Eacute' => 'É',
55     'Ecirc' => 'Ê',
56     'Euml' => 'Ë',
57     'Igrave' => 'Ì',
58     'Iacute' => 'Í',
59     'Icirc' => 'Î',
60     'Iuml' => 'Ï',
61     'ETH' => 'Ð',
62     'Ntilde' => 'Ñ',
63     'Ograve' => 'Ò',
64     'Oacute' => 'Ó',
65     'Ocirc' => 'Ô',
66     'Otilde' => 'Õ',
67     'Ouml' => 'Ö',
68     'Oslash' => 'Ø',
69     'Ugrave' => 'Ù',
70     'Uacute' => 'Ú',
71     'Ucirc' => 'Û',
72     'Uuml' => 'Ü',
73     'Yacute' => 'Ý',
74     'THORN' => 'Þ',
75     'szlig' => 'ß',
76     'agrave' => 'à',
77     'aacute' => 'á',
78     'acirc' => 'â',
79     'atilde' => 'ã',
80     'auml' => 'ä',
81     'aring' => 'å',
82     'aelig' => 'æ',
83     'oelig' => 'œ',
84     'ccedil' => 'ç',
85     'egrave' => 'è',
86     'eacute' => 'é',
87     'ecirc' => 'ê',
88     'euml' => 'ë',
89     'igrave' => 'ì',
90     'iacute' => 'í',
91     'icirc' => 'î',
92     'iuml' => 'ï',
93     'eth' => 'ð',
94     'ntilde' => 'ñ',
95     'ograve' => 'ò',
96     'oacute' => 'ó',
97     'ocirc' => 'ô',
98     'otilde' => 'õ',
99     'ouml' => 'ö',
100     'oslash' => 'ø',
101     'ugrave' => 'ù',
102     'uacute' => 'ú',
103     'ucirc' => 'û',
104     'uuml' => 'ü',
105     'yacute' => 'ý',
106     'thorn' => 'þ',
107     'yuml' => 'ÿ'
108         }
109       end
110     end
111   end
112 end
113
114 begin
115   require 'hpricot'
116   module ::Irc
117     module Utils
118       AFTER_PAR_PATH = /^(?:div|span)$/
119       AFTER_PAR_EX = /^(?:td|tr|tbody|table)$/
120       AFTER_PAR_CLASS = /body|message|text/i
121     end
122   end
123 rescue LoadError
124   gems = nil
125   begin
126     gems = require 'rubygems'
127   rescue LoadError
128     gems = false
129   end
130   if gems
131     retry
132   else
133     module ::Irc
134       module Utils
135         # Some regular expressions to manage HTML data
136
137         # Title
138         TITLE_REGEX = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
139
140         # H1, H2, etc
141         HX_REGEX = /<h(\d)(?:\s+[^>]*)?>(.*?)<\/h\1>/im
142         # A paragraph
143         PAR_REGEX = /<p(?:\s+[^>]*)?>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
144
145         # Some blogging and forum platforms use spans or divs with a 'body' or 'message' or 'text' in their class
146         # to mark actual text
147         AFTER_PAR1_REGEX = /<\w+\s+[^>]*(?:body|message|text)[^>]*>.*?<\/?(?:p|div|html|body|table|td|tr)(?:\s+[^>]*)?>/im
148
149         # At worst, we can try stuff which is comprised between two <br>
150         AFTER_PAR2_REGEX = /<br(?:\s+[^>]*)?\/?>.*?<\/?(?:br|p|div|html|body|table|td|tr)(?:\s+[^>]*)?\/?>/im
151       end
152     end
153   end
154 end
155
156 module ::Irc
157
158   # Miscellaneous useful functions
159   module Utils
160     @@bot = nil unless defined? @@bot
161     @@safe_save_dir = nil unless defined?(@@safe_save_dir)
162
163     # The bot instance
164     def Utils.bot
165       @@bot
166     end
167
168     # Set up some Utils routines which depend on the associated bot.
169     def Utils.bot=(b)
170       debug "initializing utils"
171       @@bot = b
172       @@safe_save_dir = "#{@@bot.botclass}/safe_save"
173     end
174
175
176     # Seconds per minute
177     SEC_PER_MIN = 60
178     # Seconds per hour
179     SEC_PER_HR = SEC_PER_MIN * 60
180     # Seconds per day
181     SEC_PER_DAY = SEC_PER_HR * 24
182     # Seconds per (30-day) month
183     SEC_PER_MNTH = SEC_PER_DAY * 30
184     # Second per (30*12 = 360 day) year
185     SEC_PER_YR = SEC_PER_MNTH * 12
186
187     # Auxiliary method needed by Utils.secs_to_string
188     def Utils.secs_to_string_case(array, var, string, plural)
189       case var
190       when 1
191         array << "1 #{string}"
192       else
193         array << "#{var} #{plural}"
194       end
195     end
196
197     # Turn a number of seconds into a human readable string, e.g
198     # 2 days, 3 hours, 18 minutes and 10 seconds
199     def Utils.secs_to_string(secs)
200       ret = []
201       years, secs = secs.divmod SEC_PER_YR
202       secs_to_string_case(ret, years, _("year"), _("years")) if years > 0
203       months, secs = secs.divmod SEC_PER_MNTH
204       secs_to_string_case(ret, months, _("month"), _("months")) if months > 0
205       days, secs = secs.divmod SEC_PER_DAY
206       secs_to_string_case(ret, days, _("day"), _("days")) if days > 0
207       hours, secs = secs.divmod SEC_PER_HR
208       secs_to_string_case(ret, hours, _("hour"), _("hours")) if hours > 0
209       mins, secs = secs.divmod SEC_PER_MIN
210       secs_to_string_case(ret, mins, _("minute"), _("minutes")) if mins > 0
211       secs = secs.to_i
212       secs_to_string_case(ret, secs, _("second"), _("seconds")) if secs > 0 or ret.empty?
213       case ret.length
214       when 0
215         raise "Empty ret array!"
216       when 1
217         return ret.to_s
218       else
219         return [ret[0, ret.length-1].join(", ") , ret[-1]].join(_(" and "))
220       end
221     end
222
223     # Turn a number of seconds into a hours:minutes:seconds e.g.
224     # 3:18:10 or 5'12" or 7s
225     #
226     def Utils.secs_to_short(seconds)
227       secs = seconds.to_i # make sure it's an integer
228       mins, secs = secs.divmod 60
229       hours, mins = mins.divmod 60
230       if hours > 0
231         return ("%s:%s:%s" % [hours, mins, secs])
232       elsif mins > 0
233         return ("%s'%s\"" % [mins, secs])
234       else
235         return ("%ss" % [secs])
236       end
237     end
238
239     # Returns human readable time.
240     # Like: 5 days ago
241     #       about one hour ago
242     # options
243     # :start_date, sets the time to measure against, defaults to now
244     # :date_format, used with <tt>to_formatted_s<tt>, default to :default
245     def Utils.timeago(time, options = {})
246       start_date = options.delete(:start_date) || Time.new
247       date_format = options.delete(:date_format) || :default
248       delta_minutes = (start_date.to_i - time.to_i).floor / 60
249       if delta_minutes.abs <= (8724*60) # eight weeks? I'm lazy to count days for longer than that
250         distance = Utils.distance_of_time_in_words(delta_minutes);
251         if delta_minutes < 0
252           _("%{d} from now") % {:d => distance}
253         else
254           _("%{d} ago") % {:d => distance}
255         end
256       else
257         return _("on %{date}") % {:date => system_date.to_formatted_s(date_format)}
258       end
259     end
260   # Translates a number of minutes into verbal distances.
261   # e.g. 0.5 => less than a minute
262   #      70 => about one hour
263   def Utils.distance_of_time_in_words(minutes)
264     case
265       when minutes < 1
266         _("less than a minute")
267       when minutes < 50
268         _("%{m} minutes") % {:m => minutes}
269       when minutes < 90
270         _("about one hour")
271       when minutes < 1080
272         _("%{m} hours") % {:m => (minutes / 60).round}
273       when minutes < 1440
274         _("one day")
275       when minutes < 2880
276         _("about one day")
277       else
278         _("%{m} days") % {:m => (minutes / 1440).round}
279     end
280   end
281
282
283     # Execute an external program, returning a String obtained by redirecting
284     # the program's standards errors and output 
285     #
286     def Utils.safe_exec(command, *args)
287       IO.popen("-") { |p|
288         if p
289           return p.readlines.join("\n")
290         else
291           begin
292             $stderr.reopen($stdout)
293             exec(command, *args)
294           rescue Exception => e
295             puts "exec of #{command} led to exception: #{e.pretty_inspect}"
296             Kernel::exit! 0
297           end
298           puts "exec of #{command} failed"
299           Kernel::exit! 0
300         end
301       }
302     end
303
304
305     # Safely (atomically) save to _file_, by passing a tempfile to the block
306     # and then moving the tempfile to its final location when done.
307     #
308     # call-seq: Utils.safe_save(file, &block)
309     #
310     def Utils.safe_save(file)
311       raise 'No safe save directory defined!' if @@safe_save_dir.nil?
312       basename = File.basename(file)
313       temp = Tempfile.new(basename,@@safe_save_dir)
314       temp.binmode
315       yield temp if block_given?
316       temp.close
317       File.rename(temp.path, file)
318     end
319
320
321     # Decode HTML entities in the String _str_, using HTMLEntities if the
322     # package was found, or UNESCAPE_TABLE otherwise.
323     #
324     def Utils.decode_html_entities(str)
325       if defined? ::HTMLEntities
326         return HTMLEntities.decode_entities(str)
327       else
328         str.gsub(/(&(.+?);)/) {
329           symbol = $2
330           # remove the 0-paddng from unicode integers
331           if symbol =~ /^#(\d+)$/
332             symbol = $1.to_i.to_s
333           end
334
335           # output the symbol's irc-translated character, or a * if it's unknown
336           UNESCAPE_TABLE[symbol] || (symbol.match(/^\d+$/) ? [symbol.to_i].pack("U") : '*')
337         }
338       end
339     end
340
341     # Try to grab and IRCify the first HTML par (<p> tag) in the given string.
342     # If possible, grab the one after the first heading
343     #
344     # It is possible to pass some options to determine how the stripping
345     # occurs. Currently supported options are
346     # strip:: Regex or String to strip at the beginning of the obtained
347     #         text
348     # min_spaces:: minimum number of spaces a paragraph should have
349     #
350     def Utils.ircify_first_html_par(xml_org, opts={})
351       if defined? ::Hpricot
352         Utils.ircify_first_html_par_wh(xml_org, opts)
353       else
354         Utils.ircify_first_html_par_woh(xml_org, opts)
355       end
356     end
357
358     # HTML first par grabber using hpricot
359     def Utils.ircify_first_html_par_wh(xml_org, opts={})
360       doc = Hpricot(xml_org)
361
362       # Strip styles and scripts
363       (doc/"style|script").remove
364
365       debug doc
366
367       strip = opts[:strip]
368       strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
369
370       min_spaces = opts[:min_spaces] || 8
371       min_spaces = 0 if min_spaces < 0
372
373       txt = String.new
374
375       pre_h = pars = by_span = nil
376
377       while true
378         debug "Minimum number of spaces: #{min_spaces}"
379
380         # Initial attempt: <p> that follows <h\d>
381         if pre_h.nil?
382           pre_h = Hpricot::Elements[]
383           found_h = false
384           doc.search("*") { |e|
385             next if e.bogusetag?
386             case e.pathname
387             when /^h\d/
388               found_h = true
389             when 'p'
390               pre_h << e if found_h
391             end
392           }
393           debug "Hx: found: #{pre_h.pretty_inspect}"
394         end
395
396         pre_h.each { |p|
397           debug p
398           txt = p.to_html.ircify_html
399           txt.sub!(strip, '') if strip
400           debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
401           break unless txt.empty? or txt.count(" ") < min_spaces
402         }
403
404         return txt unless txt.empty? or txt.count(" ") < min_spaces
405
406         # Second natural attempt: just get any <p>
407         pars = doc/"p" if pars.nil?
408         debug "par: found: #{pars.pretty_inspect}"
409         pars.each { |p|
410           debug p
411           txt = p.to_html.ircify_html
412           txt.sub!(strip, '') if strip
413           debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
414           break unless txt.empty? or txt.count(" ") < min_spaces
415         }
416
417         return txt unless txt.empty? or txt.count(" ") < min_spaces
418
419         # Nothing yet ... let's get drastic: we look for non-par elements too,
420         # but only for those that match something that we know is likely to
421         # contain text
422
423         # Some blogging and forum platforms use spans or divs with a 'body' or
424         # 'message' or 'text' in their class to mark actual text. Since we want
425         # the class match to be partial and case insensitive, we collect
426         # the common elements that may have this class and then filter out those
427         # we don't need. If no divs or spans are found, we'll accept additional
428         # elements too (td, tr, tbody, table).
429         if by_span.nil?
430           by_span = Hpricot::Elements[]
431           extra = Hpricot::Elements[]
432           doc.search("*") { |el|
433             next if el.bogusetag?
434             case el.pathname
435             when AFTER_PAR_PATH
436               by_span.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
437             when AFTER_PAR_EX
438               extra.push el if el[:class] =~ AFTER_PAR_CLASS or el[:id] =~ AFTER_PAR_CLASS
439             end
440           }
441           if by_span.empty? and not extra.empty?
442             by_span.concat extra
443           end
444           debug "other \#1: found: #{by_span.pretty_inspect}"
445         end
446
447         by_span.each { |p|
448           debug p
449           txt = p.to_html.ircify_html
450           txt.sub!(strip, '') if strip
451           debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
452           break unless txt.empty? or txt.count(" ") < min_spaces
453         }
454
455         return txt unless txt.empty? or txt.count(" ") < min_spaces
456
457         # At worst, we can try stuff which is comprised between two <br>
458         # TODO
459
460         debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
461         return txt unless txt.count(" ") < min_spaces
462         break if min_spaces == 0
463         min_spaces /= 2
464       end
465     end
466
467     # HTML first par grabber without hpricot
468     def Utils.ircify_first_html_par_woh(xml_org, opts={})
469       xml = xml_org.gsub(/<!--.*?-->/m, '').gsub(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "").gsub(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
470
471       strip = opts[:strip]
472       strip = Regexp.new(/^#{Regexp.escape(strip)}/) if strip.kind_of?(String)
473
474       min_spaces = opts[:min_spaces] || 8
475       min_spaces = 0 if min_spaces < 0
476
477       txt = String.new
478
479       while true
480         debug "Minimum number of spaces: #{min_spaces}"
481         header_found = xml.match(HX_REGEX)
482         if header_found
483           header_found = $'
484           while txt.empty? or txt.count(" ") < min_spaces
485             candidate = header_found[PAR_REGEX]
486             break unless candidate
487             txt = candidate.ircify_html
488             header_found = $'
489             txt.sub!(strip, '') if strip
490             debug "(Hx attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
491           end
492         end
493
494         return txt unless txt.empty? or txt.count(" ") < min_spaces
495
496         # If we haven't found a first par yet, try to get it from the whole
497         # document
498         header_found = xml
499         while txt.empty? or txt.count(" ") < min_spaces
500           candidate = header_found[PAR_REGEX]
501           break unless candidate
502           txt = candidate.ircify_html
503           header_found = $'
504           txt.sub!(strip, '') if strip
505           debug "(par attempt) #{txt.inspect} has #{txt.count(" ")} spaces"
506         end
507
508         return txt unless txt.empty? or txt.count(" ") < min_spaces
509
510         # Nothing yet ... let's get drastic: we look for non-par elements too,
511         # but only for those that match something that we know is likely to
512         # contain text
513
514         # Attempt #1
515         header_found = xml
516         while txt.empty? or txt.count(" ") < min_spaces
517           candidate = header_found[AFTER_PAR1_REGEX]
518           break unless candidate
519           txt = candidate.ircify_html
520           header_found = $'
521           txt.sub!(strip, '') if strip
522           debug "(other attempt \#1) #{txt.inspect} has #{txt.count(" ")} spaces"
523         end
524
525         return txt unless txt.empty? or txt.count(" ") < min_spaces
526
527         # Attempt #2
528         header_found = xml
529         while txt.empty? or txt.count(" ") < min_spaces
530           candidate = header_found[AFTER_PAR2_REGEX]
531           break unless candidate
532           txt = candidate.ircify_html
533           header_found = $'
534           txt.sub!(strip, '') if strip
535           debug "(other attempt \#2) #{txt.inspect} has #{txt.count(" ")} spaces"
536         end
537
538         debug "Last candidate #{txt.inspect} has #{txt.count(" ")} spaces"
539         return txt unless txt.count(" ") < min_spaces
540         break if min_spaces == 0
541         min_spaces /= 2
542       end
543     end
544
545     # This method extracts title, content (first par) and extra
546     # information from the given document _doc_.
547     #
548     # _doc_ can be an URI, a Net::HTTPResponse or a String.
549     #
550     # If _doc_ is a String, only title and content information
551     # are retrieved (if possible), using standard methods.
552     #
553     # If _doc_ is an URI or a Net::HTTPResponse, additional
554     # information is retrieved, and special title/summary
555     # extraction routines are used if possible.
556     #
557     def Utils.get_html_info(doc, opts={})
558       case doc
559       when String
560         Utils.get_string_html_info(doc, opts)
561       when Net::HTTPResponse
562         Utils.get_resp_html_info(doc, opts)
563       when URI
564         ret = DataStream.new
565         @@bot.httputil.get_response(doc) { |resp|
566           ret.replace Utils.get_resp_html_info(resp, opts)
567         }
568         return ret
569       else
570         raise
571       end
572     end
573
574     class ::UrlLinkError < RuntimeError
575     end
576
577     # This method extracts title, content (first par) and extra
578     # information from the given Net::HTTPResponse _resp_.
579     #
580     # Currently, the only accepted options (in _opts_) are
581     # uri_fragment:: the URI fragment of the original request
582     # full_body::    get the whole body instead of
583     #                @@bot.config['http.info_bytes'] bytes only
584     #
585     # Returns a DataStream with the following keys:
586     # text:: the (partial) body
587     # title:: the title of the document (if any)
588     # content:: the first paragraph of the document (if any)
589     # headers::
590     #   the headers of the Net::HTTPResponse. The value is
591     #   a Hash whose keys are lowercase forms of the HTTP
592     #   header fields, and whose values are Arrays.
593     #
594     def Utils.get_resp_html_info(resp, opts={})
595       case resp
596       when Net::HTTPSuccess
597         loc = URI.parse(resp['x-rbot-location'] || resp['location']) rescue nil
598         if loc and loc.fragment and not loc.fragment.empty?
599           opts[:uri_fragment] ||= loc.fragment
600         end
601         ret = DataStream.new(opts.dup)
602         ret[:headers] = resp.to_hash
603         ret[:text] = partial = opts[:full_body] ? resp.body : resp.partial_body(@@bot.config['http.info_bytes'])
604
605         filtered = Utils.try_htmlinfo_filters(ret)
606
607         if filtered
608           return filtered
609         elsif resp['content-type'] =~ /^text\/|(?:x|ht)ml/
610           ret.merge!(Utils.get_string_html_info(partial, opts))
611         end
612         return ret
613       else
614         raise UrlLinkError, "getting link (#{resp.code} - #{resp.message})"
615       end
616     end
617
618     # This method runs an appropriately-crafted DataStream _ds_ through the
619     # filters in the :htmlinfo filter group, in order. If one of the filters
620     # returns non-nil, its results are merged in _ds_ and returned. Otherwise
621     # nil is returned.
622     #
623     # The input DataStream shuold have the downloaded HTML as primary key
624     # (:text) and possibly a :headers key holding the resonse headers.
625     #
626     def Utils.try_htmlinfo_filters(ds)
627       filters = @@bot.filter_names(:htmlinfo)
628       return nil if filters.empty?
629       cur = nil
630       # TODO filter priority
631       filters.each { |n|
632         debug "testing filter #{n}"
633         cur = @@bot.filter(@@bot.global_filter_name(n, :htmlinfo), ds)
634         debug "returned #{cur.pretty_inspect}"
635         break if cur
636       }
637       return ds.merge(cur) if cur
638     end
639
640     # HTML info filters often need to check if the webpage location
641     # of a passed DataStream _ds_ matches a given Regexp.
642     def Utils.check_location(ds, rx)
643       debug ds[:headers]
644       if h = ds[:headers]
645         loc = [h['x-rbot-location'],h['location']].flatten.grep(rx)
646       end
647       loc ||= []
648       debug loc
649       return loc.empty? ? nil : loc
650     end
651
652     # This method extracts title and content (first par)
653     # from the given HTML or XML document _text_, using
654     # standard methods (String#ircify_html_title,
655     # Utils.ircify_first_html_par)
656     #
657     # Currently, the only accepted option (in _opts_) is
658     # uri_fragment:: the URI fragment of the original request
659     #
660     def Utils.get_string_html_info(text, opts={})
661       debug "getting string html info"
662       txt = text.dup
663       title = txt.ircify_html_title
664       debug opts
665       if frag = opts[:uri_fragment] and not frag.empty?
666         fragreg = /<a\s+(?:[^>]+\s+)?(?:name|id)=["']?#{frag}["']?[^>]*>/im
667         debug fragreg
668         debug txt
669         if txt.match(fragreg)
670           # grab the post-match
671           txt = $'
672         end
673         debug txt
674       end
675       c_opts = opts.dup
676       c_opts[:strip] ||= title
677       content = Utils.ircify_first_html_par(txt, c_opts)
678       content = nil if content.empty?
679       return {:title => title, :content => content}
680     end
681
682     # Get the first pars of the first _count_ _urls_.
683     # The pages are downloaded using the bot httputil service.
684     # Returns an array of the first paragraphs fetched.
685     # If (optional) _opts_ :message is specified, those paragraphs are
686     # echoed as replies to the IRC message passed as _opts_ :message
687     #
688     def Utils.get_first_pars(urls, count, opts={})
689       idx = 0
690       msg = opts[:message]
691       retval = Array.new
692       while count > 0 and urls.length > 0
693         url = urls.shift
694         idx += 1
695
696         begin
697           info = Utils.get_html_info(URI.parse(url), opts)
698
699           par = info[:content]
700           retval.push(par)
701
702           if par
703             msg.reply "[#{idx}] #{par}", :overlong => :truncate if msg
704             count -=1
705           end
706         rescue
707           debug "Unable to retrieve #{url}: #{$!}"
708           next
709         end
710       end
711       return retval
712     end
713
714   end
715 end
716
717 Irc::Utils.bot = Irc::Bot::Plugins.manager.bot