# Please note that global symbols have to be prefixed by :: because this plugin
# will be read into an anonymous module
+# Extensions to the Module class
+#
+class ::Module
+
+ # Many plugins define Struct objects to hold their data. On rescans, lots of
+ # warnings are echoed because of the redefinitions. Using this method solves
+ # the problem, by checking if the Struct already exists, and if it has the
+ # same attributes
+ #
+ def define_structure(name, *members)
+ sym = name.to_sym
+ if Struct.const_defined?(sym)
+ kl = Struct.const_get(sym)
+ if kl.new.members.map { |member| member.intern } == members.map
+ debug "Struct #{sym} previously defined, skipping"
+ const_set(sym, kl)
+ return
+ end
+ end
+ debug "Defining struct #{sym} with members #{members.inspect}"
+ const_set(sym, Struct.new(name.to_s, *members))
+ end
+end
+
# Extensions to the Array class
#
end
end
+# Extensions to the Range class
+#
+class ::Range
+
+ # This method returns a random number between the lower and upper bound
+ #
+ def pick_one
+ len = self.last - self.first
+ len += 1 unless self.exclude_end?
+ self.first + Kernel::rand(len)
+ end
+ alias :rand :pick_one
+end
+
+# Extensions for the Numeric classes
+#
+class ::Numeric
+
+ # This method forces a real number to be not more than a given positive
+ # number or not less than a given positive number, or between two any given
+ # numbers
+ #
+ def clip(left,right=0)
+ raise ArgumentError unless left.kind_of?(Numeric) and right.kind_of?(Numeric)
+ l = [left,right].min
+ u = [left,right].max
+ return l if self < l
+ return u if self > u
+ return self
+ end
+end
# Extensions to the String class
#
# This method will return a purified version of the receiver, with all HTML
# stripped off and some of it converted to IRC formatting
#
- def ircify_html
- txt = self
+ def ircify_html(opts={})
+ txt = self.dup
+
+ # remove scripts
+ txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
+
+ # remove styles
+ txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
# bold and strong -> bold
- txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
+ txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
# italic, emphasis and underline -> underline
- txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
+ txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
## This would be a nice addition, but the results are horrible
## Maybe make it configurable?
# txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
+ case val = opts[:a_href]
+ when Reverse, Bold, Underline
+ txt.gsub!(/<(?:\/a\s*|a (?:[^>]*\s+)?href\s*=\s*(?:[^>]*\s*)?)>/, val)
+ when :link_out
+ # Not good for nested links, but the best we can do without something like hpricot
+ txt.gsub!(/<a (?:[^>]*\s+)?href\s*=\s*(?:([^"'>][^\s>]*)\s+|"((?:[^"]|\\")*)"|'((?:[^']|\\')*)')(?:[^>]*\s+)?>(.*?)<\/a>/) { |match|
+ debug match
+ debug [$1, $2, $3, $4].inspect
+ link = $1 || $2 || $3
+ str = $4
+ str + ": " + link
+ }
+ else
+ warn "unknown :a_href option #{val} passed to ircify_html" if val
+ end
- # Paragraph and br tags are converted to whitespace.
- txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
+ # Paragraph and br tags are converted to whitespace
+ txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
txt.gsub!("\n", ' ')
+ txt.gsub!("\r", ' ')
+
+ # Superscripts and subscripts are turned into ^{...} and _{...}
+ # where the {} are omitted for single characters
+ txt.gsub!(/<sup>(.*?)<\/sup>/, '^{\1}')
+ txt.gsub!(/<sub>(.*?)<\/sub>/, '_{\1}')
+ txt.gsub!(/(^|_)\{(.)\}/, '\1\2')
# All other tags are just removed
txt.gsub!(/<[^>]+>/, '')
+ # Convert HTML entities. We do it now to be able to handle stuff
+ # such as
+ txt = Utils.decode_html_entities(txt)
+
# Remove double formatting options, since they only waste bytes
txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
+ # Simplify whitespace that appears on both sides of a formatting option
+ txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
+ txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
+ txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
+
# And finally whitespace is squeezed
txt.gsub!(/\s+/, ' ')
# Decode entities and strip whitespace
- return Utils.decode_html_entities(txt).strip!
+ return txt.strip
+ end
+
+ # As above, but modify the receiver
+ #
+ def ircify_html!(opts={})
+ old_hash = self.hash
+ replace self.ircify_html(opts)
+ return self unless self.hash == old_hash
end
# This method will strip all HTML crud from the receiver
IN_ON = /in|on/
- # We start with some IRC related regular expressions, used to match
- # Irc::User nicks and Irc::Channel names
- #
- # For each of them we define three versions of the regular expression:
- # * a generic one, which should match for any server but may turn out to
- # match more than a specific server would accept
- # * an RFC-compliant matcher
- # * TODO a server-specific one that uses the Irc::Server#supports method to build
- # a matcher valid for a particular server.
- #
module Irc
- CHAN_FIRST = /[#&+]/
- CHAN_SAFE = /![A-Z0-9]{5}/
- CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
- GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
- RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
-
+ # Match a list of channel anmes separated by optional commas, whitespace
+ # and optionally the word "and"
CHAN_LIST = Regexp.new_list(GEN_CHAN)
# Match "in #channel" or "on #channel" and/or "in private" (optionally
IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
- SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
- NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
- NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
- GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
- RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
-
# Match a list of nicknames separated by optional commas, whitespace and
# optionally the word "and"
- NICK_LIST = Regexp.new_list(GEN_CHAN)
+ NICK_LIST = Regexp.new_list(GEN_NICK)
end
- # Next, some general purpose ones
- DIGITS = /\d+/
- HEX_DIGIT = /[0-9A-Fa-f]/
- HEX_DIGITS = /#{HEX_DIGIT}+/
- HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
- DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
- DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
- HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
- IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
-
end