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 common and/or complex regular
74 # A method to build a regexp that matches a list of something separated by
75 # optional commas and/or the word "and", an optionally repeated prefix,
77 def Regexp.new_list(reg, pfx = "")
78 if pfx.kind_of?(String) and pfx.empty?
79 return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
81 return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
87 # We start with some IRC related regular expressions, used to match
88 # Irc::User nicks and Irc::Channel names
90 # For each of them we define three versions of the regular expression:
91 # * a generic one, which should match for any server but may turn out to
92 # match more than a specific server would accept
93 # * an RFC-compliant matcher
94 # * TODO a server-specific one that uses the Irc::Server#supports method to build
95 # a matcher valid for a particular server.
99 CHAN_SAFE = /![A-Z0-9]{5}/
100 CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
101 GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
102 RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
104 CHAN_LIST = Regexp.new_list(GEN_CHAN)
106 # Match "in #channel" or "on #channel" and/or "in private" (optionally
107 # shortened to "in pvt"), returning the channel name or the word 'private'
108 # or 'pvt' as capture
109 IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
110 IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
112 # As above, but with channel lists
113 IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
114 IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
115 IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
116 IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
118 SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
119 NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
120 NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
121 GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
122 RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
124 # Match a list of nicknames separated by optional commas, whitespace and
125 # optionally the word "and"
126 NICK_LIST = Regexp.new_list(GEN_CHAN)
130 # Next, some general purpose ones
132 HEX_DIGIT = /[0-9A-Fa-f]/
133 HEX_DIGITS = /#{HEX_DIGIT}+/
134 HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
135 DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
136 DEC_IP = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
137 HEX_IP = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
138 IP = /#{DEC_IP}|#{HEX_IP}/
145 class BasicUserMessage
147 # We extend the BasicUserMessage class with a method that parses a string
148 # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
149 # returns an array of channel names, where 'private' or 'pvt' is replaced
150 # by the Symbol :"?", 'here' is replaced by the channel of the message or
151 # by :"?" (depending on whether the message target is the bot or a
152 # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
154 def parse_channel_list(string)
155 return [:*] if [:anywhere, :everywhere].include? string.to_sym
157 /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/