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
17 # Extensions to the Array class
21 # This method returns a random element from the array, or nil if the array is
25 return nil if self.empty?
26 self[rand(self.length)]
31 # Extensions to the String class
33 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
34 # call ircify_html() with stronger purify options.
38 # This method will return a purified version of the receiver, with all HTML
39 # stripped off and some of it converted to IRC formatting
45 txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
48 txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
50 # bold and strong -> bold
51 txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
53 # italic, emphasis and underline -> underline
54 txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
56 ## This would be a nice addition, but the results are horrible
57 ## Maybe make it configurable?
58 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
60 # Paragraph and br tags are converted to whitespace
61 txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/, ' ')
65 # All other tags are just removed
66 txt.gsub!(/<[^>]+>/, '')
68 # Convert HTML entities. We do it now to be able to handle stuff
70 txt = Utils.decode_html_entities(txt)
72 # Remove double formatting options, since they only waste bytes
73 txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
74 txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
76 # Simplify whitespace that appears on both sides of a formatting option
77 txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
78 txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
79 txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
81 # And finally whitespace is squeezed
84 # Decode entities and strip whitespace
88 # This method will strip all HTML crud from the receiver
91 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')
96 # Extensions to the Regexp class, with some common and/or complex regular
101 # A method to build a regexp that matches a list of something separated by
102 # optional commas and/or the word "and", an optionally repeated prefix,
104 def Regexp.new_list(reg, pfx = "")
105 if pfx.kind_of?(String) and pfx.empty?
106 return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
108 return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
115 # Match a list of channel anmes separated by optional commas, whitespace
116 # and optionally the word "and"
117 CHAN_LIST = Regexp.new_list(GEN_CHAN)
119 # Match "in #channel" or "on #channel" and/or "in private" (optionally
120 # shortened to "in pvt"), returning the channel name or the word 'private'
121 # or 'pvt' as capture
122 IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
123 IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
125 # As above, but with channel lists
126 IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
127 IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
128 IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
129 IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
131 # Match a list of nicknames separated by optional commas, whitespace and
132 # optionally the word "and"
133 NICK_LIST = Regexp.new_list(GEN_NICK)
143 class BasicUserMessage
145 # We extend the BasicUserMessage class with a method that parses a string
146 # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
147 # returns an array of channel names, where 'private' or 'pvt' is replaced
148 # by the Symbol :"?", 'here' is replaced by the channel of the message or
149 # by :"?" (depending on whether the message target is the bot or a
150 # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
152 def parse_channel_list(string)
153 return [:*] if [:anywhere, :everywhere].include? string.to_sym
155 /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/