]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/extends.rb
Plugin header boilerplating.
[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     # bold and strong -> bold
45     txt.gsub!(/<\/?(?:b|strong)\s*>/, "#{Bold}")
46
47     # italic, emphasis and underline -> underline
48     txt.gsub!(/<\/?(?:i|em|u)\s*>/, "#{Underline}")
49
50     ## This would be a nice addition, but the results are horrible
51     ## Maybe make it configurable?
52     # txt.gsub!(/<\/?a( [^>]*)?>/, "#{Reverse}")
53
54     # Paragraph and br tags are converted to whitespace.
55     txt.gsub!(/<\/?(p|br)\s*\/?\s*>/, ' ')
56     txt.gsub!("\n", ' ')
57
58     # All other tags are just removed
59     txt.gsub!(/<[^>]+>/, '')
60
61     # Remove double formatting options, since they only waste bytes
62     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
63     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
64
65     # And finally whitespace is squeezed
66     txt.gsub!(/\s+/, ' ')
67
68     # Decode entities and strip whitespace
69     return Utils.decode_html_entities(txt).strip!
70   end
71
72   # This method will strip all HTML crud from the receiver
73   #
74   def riphtml
75     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
76   end
77 end
78
79
80 # Extensions to the Regexp class, with some common and/or complex regular
81 # expressions.
82 #
83 class ::Regexp
84
85   # A method to build a regexp that matches a list of something separated by
86   # optional commas and/or the word "and", an optionally repeated prefix,
87   # and whitespace.
88   def Regexp.new_list(reg, pfx = "")
89     if pfx.kind_of?(String) and pfx.empty?
90       return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
91     else
92       return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
93     end
94   end
95
96   IN_ON = /in|on/
97
98   # We start with some IRC related regular expressions, used to match
99   # Irc::User nicks and Irc::Channel names
100   #
101   # For each of them we define three versions of the regular expression:
102   #  * a generic one, which should match for any server but may turn out to
103   #    match more than a specific server would accept
104   #  * an RFC-compliant matcher
105   #  * TODO a server-specific one that uses the Irc::Server#supports method to build
106   #    a matcher valid for a particular server.
107   #
108   module Irc
109     CHAN_FIRST = /[#&+]/
110     CHAN_SAFE = /![A-Z0-9]{5}/
111     CHAN_ANY = /[^\x00\x07\x0A\x0D ,:]/
112     GEN_CHAN = /(?:#{CHAN_FIRST}|#{CHAN_SAFE})#{CHAN_ANY}+/
113     RFC_CHAN = /#{CHAN_FIRST}#{CHAN_ANY}{1,49}|#{CHAN_SAFE}#{CHAN_ANY}{1,44}/
114
115     CHAN_LIST = Regexp.new_list(GEN_CHAN)
116
117     # Match "in #channel" or "on #channel" and/or "in private" (optionally
118     # shortened to "in pvt"), returning the channel name or the word 'private'
119     # or 'pvt' as capture
120     IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
121     IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
122
123     # As above, but with channel lists
124     IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
125     IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
126     IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
127     IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
128
129     SPECIAL_CHAR = /[\x5b-\x60\x7b-\x7d]/
130     NICK_FIRST = /#{SPECIAL_CHAR}|[[:alpha:]]/
131     NICK_ANY = /#{SPECIAL_CHAR}|[[:alnum:]]|-/
132     GEN_NICK = /#{NICK_FIRST}#{NICK_ANY}+/
133     RFC_NICK = /#{NICK_FIRST}#{NICK_ANY}{0,8}/
134
135     # Match a list of nicknames separated by optional commas, whitespace and
136     # optionally the word "and"
137     NICK_LIST = Regexp.new_list(GEN_CHAN)
138
139   end
140
141   # Next, some general purpose ones
142   DIGITS = /\d+/
143   HEX_DIGIT = /[0-9A-Fa-f]/
144   HEX_DIGITS = /#{HEX_DIGIT}+/
145   HEX_OCTET = /#{HEX_DIGIT}#{HEX_DIGIT}?/
146   DEC_OCTET = /[01]?\d?\d|2[0-4]\d|25[0-5]/
147   DEC_IP_ADDR = /#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}.#{DEC_OCTET}/
148   HEX_IP_ADDR = /#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}.#{HEX_OCTET}/
149   IP_ADDR = /#{DEC_IP_ADDR}|#{HEX_IP_ADDR}/
150
151 end
152
153
154 module ::Irc
155
156
157   class BasicUserMessage
158
159     # We extend the BasicUserMessage class with a method that parses a string
160     # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
161     # returns an array of channel names, where 'private' or 'pvt' is replaced
162     # by the Symbol :"?", 'here' is replaced by the channel of the message or
163     # by :"?" (depending on whether the message target is the bot or a
164     # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
165     #
166     def parse_channel_list(string)
167       return [:*] if [:anywhere, :everywhere].include? string.to_sym
168       string.scan(
169       /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/
170                  ).map { |chan_ar|
171         chan = chan_ar.first
172         case chan.to_sym
173         when :private, :pvt
174           :"?"
175         when :here
176           case self.target
177           when Channel
178             self.target.name
179           else
180             :"?"
181           end
182         else
183           chan
184         end
185       }.uniq
186     end
187   end
188 end