X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Futils%2Fextends.rb;h=b812a6766f7f587a55f823f7bdbe3814ca301926;hb=1467b66a0a18091efe71889c4dfe7f57bb9b3e04;hp=1f33026962c1de64639bff59efbe79fa0f3f5d7f;hpb=82b41fa7b22205d8eae75c1eb6999ac5fbf7849a;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb index 1f330269..b812a676 100644 --- a/lib/rbot/core/utils/extends.rb +++ b/lib/rbot/core/utils/extends.rb @@ -1,10 +1,19 @@ #-- vim:sw=2:et #++ # -# Extensions to standard classes, to be used by the various plugins +# :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 +# # Please note that global symbols have to be prefixed by :: because this plugin # will be read into an anonymous module + # Extensions to the Array class # class ::Array @@ -18,6 +27,24 @@ class ::Array end 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 # # TODO make ircify_html() accept an Hash of options, and make riphtml() just @@ -31,32 +58,54 @@ class ::String def ircify_html txt = self + # remove scripts + txt.gsub!(/]*)?>.*?<\/script>/im, "") + + # remove styles + txt.gsub!(/]*)?>.*?<\/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}") - # 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>/, '^{\1}') + txt.gsub!(/(.*?)<\/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 # This method will strip all HTML crud from the receiver @@ -66,46 +115,83 @@ class ::String end end -# Extensions to the Regexp class, with some commonly used regular expressions. + +# Extensions to the Regexp class, with some common and/or complex regular +# expressions. # -module ::Rx +class ::Regexp + + # A method to build a regexp that matches a list of something separated by + # optional commas and/or the word "and", an optionally repeated prefix, + # and whitespace. + def Regexp.new_list(reg, pfx = "") + if pfx.kind_of?(String) and pfx.empty? + return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*) + else + return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*) + end + end + 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 ,:]/ - GENERIC_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/ - RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/ - - SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/ - NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/ - NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/ - GENERIC_NICK = /#{NICK_FIRST}#{NICK_ANY}+/ - RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/ - end - + # 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 + # shortened to "in pvt"), returning the channel name or the word 'private' + # or 'pvt' as capture + IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/ + IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/ + + # As above, but with channel lists + IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON) + IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/ + 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/ + + # Match a list of nicknames separated by optional commas, whitespace and + # optionally the word "and" + NICK_LIST = Regexp.new_list(GEN_NICK) - # Next, some general purpose ones - DIGITS = /[0-9]+/ - HEX_DIGIT = /[0-9A-Za-z]/ - HEX_DIGITS = /#{HEX_DIGIT}+/ - HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/ - DEC_OCTET = /[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]/ - DEC_IP = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/ - HEX_IP = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/ - IP = /#{DEC_IP}|#{HEX_IP}/ + end end +module ::Irc + + + class BasicUserMessage + + # We extend the BasicUserMessage class with a method that parses a string + # which is a channel list as matched by IN_CHAN(_LIST) and co. The method + # returns an array of channel names, where 'private' or 'pvt' is replaced + # by the Symbol :"?", 'here' is replaced by the channel of the message or + # by :"?" (depending on whether the message target is the bot or a + # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :* + # + def parse_channel_list(string) + return [:*] if [:anywhere, :everywhere].include? string.to_sym + string.scan( + /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/ + ).map { |chan_ar| + chan = chan_ar.first + case chan.to_sym + when :private, :pvt + :"?" + when :here + case self.target + when Channel + self.target.name + else + :"?" + end + else + chan + end + }.uniq + end + end +end