4 # Extensions to standard classes, to be used by the various plugins
5 # Please note that global symbols have to be prefixed by :: because this plugin
6 # will be read into an anonymous module
8 # Extensions to the Array class
12 # This method returns a random element from the array, or nil if the array is
16 return nil if self.empty?
17 self[rand(self.length)]
21 # Extensions to the String class
23 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
24 # call ircify_html() with stronger purify options.
28 # This method will return a purified version of the receiver, with all HTML
29 # stripped off and some of it converted to IRC formatting
34 # bold and strong -> bold
35 txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
37 # italic, emphasis and underline -> underline
38 txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
40 ## This would be a nice addition, but the results are horrible
41 ## Maybe make it configurable?
42 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
44 # Paragraph and br tags are converted to whitespace.
45 txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
48 # All other tags are just removed
49 txt.gsub!(/<[^>]+>/, '')
51 # Remove double formatting options, since they only waste bytes
52 txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
53 txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
55 # And finally whitespace is squeezed
58 # Decode entities and strip whitespace
59 return Utils.decode_html_entities(txt).strip!
62 # This method will strip all HTML crud from the receiver
65 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')
69 # Extensions to the Regexp class, with some commonly used regular expressions.
74 # We start with some IRC related regular expressions, used to match
75 # Irc::User nicks and Irc::Channel names
77 # For each of them we define three versions of the regular expression:
78 # * a generic one, which should match for any server but may turn out to match
79 # more than a specific server would accept
80 # * an RFC-compliant matcher
81 # * TODO a server-specific one that uses the Irc::Server#supports method to build
82 # a matcher valid for a particular server.
86 CHAN_SAFE = /![A-Z0-9]{5}/
87 CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
88 GENERIC_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
89 RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
91 SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
92 NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
93 NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
94 GENERIC_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
95 RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
99 # Next, some general purpose ones
101 HEX_DIGIT = /[0-9A-Za-z]/
102 HEX_DIGITS = /#{HEX_DIGIT}+/
103 HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
104 DEC_OCTET = /[01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5]/
105 DEC_IP = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
106 HEX_IP = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
107 IP = /#{DEC_IP}|#{HEX_IP}/