]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/extends.rb
Utils: more first par enhancements
[user/henk/code/ruby/rbot.git] / lib / rbot / core / utils / extends.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Standard classes extensions
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
8 # License:: GPL v2
9 #
10 # This file collects extensions to standard Ruby classes and to some core rbot
11 # classes to be used by the various plugins
12 #
13 # Please note that global symbols have to be prefixed by :: because this plugin
14 # will be read into an anonymous module
15
16
17 # Extensions to the Array class
18 #
19 class ::Array
20
21   # This method returns a random element from the array, or nil if the array is
22   # empty
23   #
24   def pick_one
25     return nil if self.empty?
26     self[rand(self.length)]
27   end
28 end
29
30
31 # Extensions to the String class
32 #
33 # TODO make ircify_html() accept an Hash of options, and make riphtml() just
34 # call ircify_html() with stronger purify options.
35 #
36 class ::String
37
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
40   #
41   def ircify_html
42     txt = self
43
44     # remove scripts
45     txt.gsub!(/<script(?:\s+[^>]*)?>.*?<\/script>/im, "")
46
47     # remove styles
48     txt.gsub!(/<style(?:\s+[^>]*)?>.*?<\/style>/im, "")
49
50     # bold and strong -> bold
51     txt.gsub!(/<\/?(?:b|strong)(?:\s+[^>]*)?>/im, "#{Bold}")
52
53     # italic, emphasis and underline -> underline
54     txt.gsub!(/<\/?(?:i|em|u)(?:\s+[^>]*)?>/im, "#{Underline}")
55
56     ## This would be a nice addition, but the results are horrible
57     ## Maybe make it configurable?
58     # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
59
60     # Paragraph and br tags are converted to whitespace
61     txt.gsub!(/<\/?(p|br)(?:\s+[^>]*)?\s*\/?\s*>/, ' ')
62     txt.gsub!("\n", ' ')
63     txt.gsub!("\r", ' ')
64
65     # All other tags are just removed
66     txt.gsub!(/<[^>]+>/, '')
67
68     # Convert HTML entities. We do it now to be able to handle stuff
69     # such as &nbsp;
70     txt = Utils.decode_html_entities(txt)
71
72     # Remove double formatting options, since they only waste bytes
73     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
74     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
75
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')
80
81     # And finally whitespace is squeezed
82     txt.gsub!(/\s+/, ' ')
83
84     # Decode entities and strip whitespace
85     return txt.strip
86   end
87
88   # This method will strip all HTML crud from the receiver
89   #
90   def riphtml
91     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
92   end
93 end
94
95
96 # Extensions to the Regexp class, with some common and/or complex regular
97 # expressions.
98 #
99 class ::Regexp
100
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,
103   # and whitespace.
104   def Regexp.new_list(reg, pfx = "")
105     if pfx.kind_of?(String) and pfx.empty?
106       return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
107     else
108       return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
109     end
110   end
111
112   IN_ON = /in|on/
113
114   module Irc
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)
118
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)/
124
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/
130
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)
134
135   end
136
137 end
138
139
140 module ::Irc
141
142
143   class BasicUserMessage
144
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 :*
151     #
152     def parse_channel_list(string)
153       return [:*] if [:anywhere, :everywhere].include? string.to_sym
154       string.scan(
155       /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/
156                  ).map { |chan_ar|
157         chan = chan_ar.first
158         case chan.to_sym
159         when :private, :pvt
160           :"?"
161         when :here
162           case self.target
163           when Channel
164             self.target.name
165           else
166             :"?"
167           end
168         else
169           chan
170         end
171       }.uniq
172     end
173   end
174 end