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)]
30 # Extensions to the String class
32 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
33 # call ircify_html() with stronger purify options.
37 # This method will return a purified version of the receiver, with all HTML
38 # stripped off and some of it converted to IRC formatting
44 txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
47 txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
49 # bold and strong -> bold
50 txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
52 # italic, emphasis and underline -> underline
53 txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
55 ## This would be a nice addition, but the results are horrible
56 ## Maybe make it configurable?
57 # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
59 # Paragraph and br tags are converted to whitespace
60 txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/, ' ')
64 # All other tags are just removed
65 txt.gsub!(/<[^>]+>/, '')
67 # Convert HTML entities. We do it now to be able to handle stuff
69 txt = Utils.decode_html_entities(txt)
71 # Remove double formatting options, since they only waste bytes
72 txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
73 txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
75 # Simplify whitespace that appears on both sides of a formatting option
76 txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
77 txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
78 txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
80 # And finally whitespace is squeezed
83 # Decode entities and strip whitespace
87 # This method will strip all HTML crud from the receiver
90 self.gsub(/<[^>]+>/, '').gsub(/&/,'&').gsub(/"/,'"').gsub(/</,'<').gsub(/>/,'>').gsub(/&ellip;/,'...').gsub(/'/, "'").gsub("\n",'')
95 # Extensions to the Regexp class, with some common and/or complex regular
100 # A method to build a regexp that matches a list of something separated by
101 # optional commas and/or the word "and", an optionally repeated prefix,
103 def Regexp.new_list(reg, pfx = "")
104 if pfx.kind_of?(String) and pfx.empty?
105 return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
107 return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
114 # Match a list of channel anmes separated by optional commas, whitespace
115 # and optionally the word "and"
116 CHAN_LIST = Regexp.new_list(GEN_CHAN)
118 # Match "in #channel" or "on #channel" and/or "in private" (optionally
119 # shortened to "in pvt"), returning the channel name or the word 'private'
120 # or 'pvt' as capture
121 IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
122 IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
124 # As above, but with channel lists
125 IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
126 IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
127 IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
128 IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
130 # Match a list of nicknames separated by optional commas, whitespace and
131 # optionally the word "and"
132 NICK_LIST = Regexp.new_list(GEN_NICK)
142 class BasicUserMessage
144 # We extend the BasicUserMessage class with a method that parses a string
145 # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
146 # returns an array of channel names, where 'private' or 'pvt' is replaced
147 # by the Symbol :"?", 'here' is replaced by the channel of the message or
148 # by :"?" (depending on whether the message target is the bot or a
149 # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
151 def parse_channel_list(string)
152 return [:*] if [:anywhere, :everywhere].include? string.to_sym
154 /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/