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