4 # :title: Standard classes extensions
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
10 # This file collects extensions to standard Ruby classes and to some core rbot
11 # classes to be used by the various plugins
13 # Please note that global symbols have to be prefixed by :: because this plugin
14 # will be read into an anonymous module
16 # Extensions to the Module class
20 # Many plugins define Struct objects to hold their data. On rescans, lots of
21 # warnings are echoed because of the redefinitions. Using this method solves
22 # the problem, by checking if the Struct already exists, and if it has the
25 def define_structure(name, *members)
27 if Struct.const_defined?(sym)
28 kl = Struct.const_get(sym)
29 if kl.new.members.map { |member| member.intern } == members.map
30 debug "Struct #{sym} previously defined, skipping"
35 debug "Defining struct #{sym} with members #{members.inspect}"
36 const_set(sym, Struct.new(name.to_s, *members))
41 # DottedIndex mixin: extend a Hash or Array class with this module
42 # to achieve [] and []= methods that automatically split indices
43 # at dots (indices are automatically converted to symbols, too)
45 # You have to define the single_retrieve(_key_) and
46 # single_assign(_key_,_value_) methods (usually aliased at the
47 # original :[] and :[]= methods)
50 def rbot_index_split(*ar)
51 keys = ([] << ar).flatten
53 k.to_s.split('.').map { |kk| kk.to_sym rescue nil }.compact
58 keys = self.rbot_index_split(ar)
59 return self.single_retrieve(keys.first) if keys.length == 1
63 h[k] ||= self.class.new
72 keys = self.rbot_index_split(ar)
73 return self.single_assign(keys.first, val) if keys.length == 1
77 h[k] ||= self.class.new
85 # Extensions to the Array class
89 # This method returns a random element from the array, or nil if the array is
93 return nil if self.empty?
94 self[rand(self.length)]
97 # This method returns a random element from the array, deleting it from the
98 # array itself. The method returns nil if the array is empty
101 return nil if self.empty?
102 self.delete_at(rand(self.length))
106 # Extensions to the Range class
110 # This method returns a random number between the lower and upper bound
113 len = self.last - self.first
114 len += 1 unless self.exclude_end?
115 self.first + Kernel::rand(len)
117 alias :rand :pick_one
120 # Extensions for the Numeric classes
124 # This method forces a real number to be not more than a given positive
125 # number or not less than a given positive number, or between two any given
128 def clip(left,right=0)
129 raise ArgumentError unless left.kind_of?(Numeric) and right.kind_of?(Numeric)
138 # Extensions to the String class
140 # TODO make riphtml() just call ircify_html() with stronger purify options.
144 # This method will return a purified version of the receiver, with all HTML
145 # stripped off and some of it converted to IRC formatting
147 def ircify_html(opts={})
151 txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
154 txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
156 # bold and strong -> bold
157 txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
159 # italic, emphasis and underline -> underline
160 txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
162 ## This would be a nice addition, but the results are horrible
163 ## Maybe make it configurable?
164 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
165 case val = opts[:a_href]
166 when Reverse, Bold, Underline
167 txt.gsub!(/<(?:\/a\s*|a (?:[^>]*\s+)?href\s*=\s*(?:[^>]*\s*)?)>/, val)
169 # Not good for nested links, but the best we can do without something like hpricot
170 txt.gsub!(/<a (?:[^>]*\s+)?href\s*=\s*(?:([^"'>][^\s>]*)\s+|"((?:[^"]|\\")*)"|'((?:[^']|\\')*)')(?:[^>]*\s+)?>(.*?)<\/a>/) { |match|
172 debug [$1, $2, $3, $4].inspect
173 link = $1 || $2 || $3
178 warning "unknown :a_href option #{val} passed to ircify_html" if val
181 # Paragraph and br tags are converted to whitespace
182 txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/i, ' ')
186 # Superscripts and subscripts are turned into ^{...} and _{...}
187 # where the {} are omitted for single characters
188 txt.gsub!(/<sup>(.*?)<\/sup>/, '^{\1}')
189 txt.gsub!(/<sub>(.*?)<\/sub>/, '_{\1}')
190 txt.gsub!(/(^|_)\{(.)\}/, '\1\2')
192 # List items are converted to *). We don't have special support for
193 # nested or ordered lists.
194 txt.gsub!(/<li>/, ' *) ')
196 # All other tags are just removed
197 txt.gsub!(/<[^>]+>/, '')
199 # Convert HTML entities. We do it now to be able to handle stuff
201 txt = Utils.decode_html_entities(txt)
203 # Keep unbreakable spaces or conver them to plain spaces?
204 case val = opts[:nbsp]
206 txt.gsub!([160].pack('U'), ' ')
208 warning "unknown :nbsp option #{val} passed to ircify_html" if val
211 # Remove double formatting options, since they only waste bytes
212 txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
213 txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
215 # Simplify whitespace that appears on both sides of a formatting option
216 txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
217 txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
218 txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
220 # And finally whitespace is squeezed
221 txt.gsub!(/\s+/, ' ')
224 if opts[:limit] && txt.size > opts[:limit]
225 txt = txt.slice(0, opts[:limit]) + "#{Reverse}...#{Reverse}"
228 # Decode entities and strip whitespace
232 # As above, but modify the receiver
234 def ircify_html!(opts={})
236 replace self.ircify_html(opts)
237 return self unless self.hash == old_hash
240 # This method will strip all HTML crud from the receiver
243 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')
246 # This method tries to find an HTML title in the string,
247 # and returns it if found
249 if defined? ::Hpricot
250 Hpricot(self).at("title").inner_html
252 return unless Irc::Utils::TITLE_REGEX.match(self)
257 # This method returns the IRC-formatted version of an
258 # HTML title found in the string
259 def ircify_html_title
260 self.get_html_title.ircify_html rescue nil
265 # Extensions to the Regexp class, with some common and/or complex regular
270 # A method to build a regexp that matches a list of something separated by
271 # optional commas and/or the word "and", an optionally repeated prefix,
273 def Regexp.new_list(reg, pfx = "")
274 if pfx.kind_of?(String) and pfx.empty?
275 return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
277 return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
284 # Match a list of channel anmes separated by optional commas, whitespace
285 # and optionally the word "and"
286 CHAN_LIST = Regexp.new_list(GEN_CHAN)
288 # Match "in #channel" or "on #channel" and/or "in private" (optionally
289 # shortened to "in pvt"), returning the channel name or the word 'private'
290 # or 'pvt' as capture
291 IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
292 IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
294 # As above, but with channel lists
295 IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
296 IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
297 IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
298 IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
300 # Match a list of nicknames separated by optional commas, whitespace and
301 # optionally the word "and"
302 NICK_LIST = Regexp.new_list(GEN_NICK)
312 class BasicUserMessage
314 # We extend the BasicUserMessage class with a method that parses a string
315 # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
316 # returns an array of channel names, where 'private' or 'pvt' is replaced
317 # by the Symbol :"?", 'here' is replaced by the channel of the message or
318 # by :"?" (depending on whether the message target is the bot or a
319 # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
321 def parse_channel_list(string)
322 return [:*] if [:anywhere, :everywhere].include? string.to_sym
324 /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/