X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Futils%2Fextends.rb;h=208cb089a41b63fac593eaec6cbbc65093989ead;hb=299c928efdedfb25c76c14077fe3060db54ceeb1;hp=19157009a06b9f9d264f1c08584844421155fc1d;hpb=3220c8e65e723790dfd69e05c012ad78badfbb45;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb index 19157009..208cb089 100644 --- a/lib/rbot/core/utils/extends.rb +++ b/lib/rbot/core/utils/extends.rb @@ -4,8 +4,6 @@ # :title: Standard classes extensions # # Author:: Giuseppe "Oblomov" Bilotta -# Copyright:: (C) 2006,2007 Giuseppe Bilotta -# License:: GPL v2 # # This file collects extensions to standard Ruby classes and to some core rbot # classes to be used by the various plugins @@ -110,23 +108,77 @@ class ::Array self.delete_at(index) end - # This method shuffles the items in the array - def shuffle! - base = self.dup - self.clear - while item = base.delete_one - self << item + # This method deletes a given object _el_ if it's found at the given + # position _pos_. The position can be either an integer or a range. + def delete_if_at(el, pos) + idx = self.index(el) + if pos === idx + self.delete_at(idx) + else + nil end - self end + # shuffle and shuffle! are defined in Ruby >= 1.8.7 + # This method returns a new array with the same items as # the receiver, but shuffled - def shuffle - ret = self.dup - ret.shuffle! + unless method_defined? :shuffle + def shuffle + sort_by { rand } + end end + # This method shuffles the items in the array + unless method_defined? :shuffle! + def shuffle! + replace shuffle + end + end +end + +module ::Enumerable + # This method is an advanced version of #join + # allowing fine control of separators: + # + # [1,2,3].conjoin(', ', ' and ') + # => "1, 2 and 3 + # + # [1,2,3,4].conjoin{ |i, a, b| i % 2 == 0 ? '.' : '-' } + # => "1.2-3.4" + # + # Code lifted from the ruby facets project: + # + # git-rev: c8b7395255b977d3c7de268ff563e3c5bc7f1441 + # file: lib/core/facets/array/conjoin.rb + def conjoin(*args, &block) + num = count - 1 + + return first.to_s if num < 1 + + sep = [] + + if block_given? + num.times do |i| + sep << yield(i, *slice(i, 2)) + end + else + options = (Hash === args.last) ? args.pop : {} + separator = args.shift || "" + options[-1] = args.shift unless args.empty? + + sep = [separator] * num + + if options.key?(:last) + options[-1] = options.delete(:last) + end + options[-1] ||= _(" and ") + + options.each{ |i, s| sep[i] = s } + end + + zip(sep).join + end end # Extensions to the Range class @@ -204,6 +256,29 @@ class ::String warning "unknown :a_href option #{val} passed to ircify_html" if val end + # If opts[:img] is defined, it should be a String. Each image + # will be replaced by the string itself, replacing occurrences of + # %{alt} %{dimensions} and %{src} with the alt text, image dimensions + # and URL + if val = opts[:img] + if val.kind_of? String + txt.gsub!(//) do |imgtag| + attrs = Hash.new + imgtag.scan(/([[:alpha:]]+)\s*=\s*(['"])?(.*?)\2/) do |key, quote, value| + k = key.downcase.intern rescue 'junk' + attrs[k] = value + end + attrs[:alt] ||= attrs[:title] + attrs[:width] ||= '...' + attrs[:height] ||= '...' + attrs[:dimensions] ||= "#{attrs[:width]}x#{attrs[:height]}" + val % attrs + end + else + warning ":img option is not a string" + end + end + # Paragraph and br tags are converted to whitespace txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ') txt.gsub!("\n", ' ') @@ -266,7 +341,7 @@ class ::String # This method will strip all HTML crud from the receiver # def riphtml - self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'') + Utils.decode_html_entities(self.gsub("\n",' ').gsub(/<\s*br\s*\/?\s*>/, ' ').gsub(/<[^>]+>/, '')).gsub(/\s+/,' ') end # This method tries to find an HTML title in the string, @@ -285,6 +360,16 @@ class ::String def ircify_html_title self.get_html_title.ircify_html rescue nil end + + # This method is used to wrap a nonempty String by adding + # the prefix and postfix + def wrap_nonempty(pre, post, opts={}) + if self.empty? + String.new + else + "#{pre}#{self}#{post}" + end + end end @@ -402,6 +487,10 @@ module ::Irc # the :target, the message :class and whether or not to :delegate. To # initialize these entries from an existing message, you can use :from # + # Additionally, if :from is given, the reply method of created message + # is overriden to reply to :from instead. The #in_thread attribute + # for created mesage is also copied from :from + # # If you don't specify a :from you should specify a :source. # def fake_message(string, opts={}) @@ -420,6 +509,21 @@ module ::Irc raise RecurseTooDeep if o[:depth] > MAX_RECURSE_DEPTH new_m = o[:class].new(o[:bot], o[:server], o[:source], o[:target], string) new_m.recurse_depth = o[:depth] + if from + # the created message will reply to the originating message, but we + # must remember to set 'replied' on the fake message too, to + # prevent infinite loops that could be created for example with the reaction + # plugin by setting up a reaction to ping with cmd:ping + class << new_m + self + end.send(:define_method, :reply) do |*args| + debug "replying to '#{from.message}' with #{args.first}" + from.reply(*args) + new_m.replied = true + end + # the created message will follow originating message's in_thread + new_m.in_thread = from.in_thread if from.respond_to?(:in_thread) + end return new_m unless o[:delegate] method = o[:class].to_s.gsub(/^Irc::|Message$/,'').downcase method = 'privmsg' if method == 'priv'