]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/utils/extends.rb
Utils: fix ircify_html (the final stripsvn diff lib/rbot/core/utils/extends.rb could...
[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     txt.gsub!("\r", ' ')
58
59     # All other tags are just removed
60     txt.gsub!(/<[^>]+>/, '')
61
62     # Convert HTML entities. We do it now to be able to handle stuff
63     # such as &nbsp;
64     txt = Utils.decode_html_entities(txt)
65
66     # Remove double formatting options, since they only waste bytes
67     txt.gsub!(/#{Bold}(\s*)#{Bold}/, '\1')
68     txt.gsub!(/#{Underline}(\s*)#{Underline}/, '\1')
69
70     # Simplify whitespace that appears on both sides of a formatting option
71     txt.gsub!(/\s+(#{Bold}|#{Underline})\s+/, ' \1')
72     txt.sub!(/\s+(#{Bold}|#{Underline})\z/, '\1')
73     txt.sub!(/\A(#{Bold}|#{Underline})\s+/, '\1')
74
75     # And finally whitespace is squeezed
76     txt.gsub!(/\s+/, ' ')
77
78     # Decode entities and strip whitespace
79     return txt.strip
80   end
81
82   # This method will strip all HTML crud from the receiver
83   #
84   def riphtml
85     self.gsub(/<[^>]+>/, '').gsub(/&amp;/,'&').gsub(/&quot;/,'"').gsub(/&lt;/,'<').gsub(/&gt;/,'>').gsub(/&ellip;/,'...').gsub(/&apos;/, "'").gsub("\n",'')
86   end
87 end
88
89
90 # Extensions to the Regexp class, with some common and/or complex regular
91 # expressions.
92 #
93 class ::Regexp
94
95   # A method to build a regexp that matches a list of something separated by
96   # optional commas and/or the word "and", an optionally repeated prefix,
97   # and whitespace.
98   def Regexp.new_list(reg, pfx = "")
99     if pfx.kind_of?(String) and pfx.empty?
100       return %r(#{reg}(?:,?(?:\s+and)?\s+#{reg})*)
101     else
102       return %r(#{reg}(?:,?(?:\s+and)?(?:\s+#{pfx})?\s+#{reg})*)
103     end
104   end
105
106   IN_ON = /in|on/
107
108   module Irc
109     # Match a list of channel anmes separated by optional commas, whitespace
110     # and optionally the word "and"
111     CHAN_LIST = Regexp.new_list(GEN_CHAN)
112
113     # Match "in #channel" or "on #channel" and/or "in private" (optionally
114     # shortened to "in pvt"), returning the channel name or the word 'private'
115     # or 'pvt' as capture
116     IN_CHAN = /#{IN_ON}\s+(#{GEN_CHAN})|(here)|/
117     IN_CHAN_PVT = /#{IN_CHAN}|in\s+(private|pvt)/
118
119     # As above, but with channel lists
120     IN_CHAN_LIST_SFX = Regexp.new_list(/#{GEN_CHAN}|here/, IN_ON)
121     IN_CHAN_LIST = /#{IN_ON}\s+#{IN_CHAN_LIST_SFX}|anywhere|everywhere/
122     IN_CHAN_LIST_PVT_SFX = Regexp.new_list(/#{GEN_CHAN}|here|private|pvt/, IN_ON)
123     IN_CHAN_LIST_PVT = /#{IN_ON}\s+#{IN_CHAN_LIST_PVT_SFX}|anywhere|everywhere/
124
125     # Match a list of nicknames separated by optional commas, whitespace and
126     # optionally the word "and"
127     NICK_LIST = Regexp.new_list(GEN_NICK)
128
129   end
130
131 end
132
133
134 module ::Irc
135
136
137   class BasicUserMessage
138
139     # We extend the BasicUserMessage class with a method that parses a string
140     # which is a channel list as matched by IN_CHAN(_LIST) and co. The method
141     # returns an array of channel names, where 'private' or 'pvt' is replaced
142     # by the Symbol :"?", 'here' is replaced by the channel of the message or
143     # by :"?" (depending on whether the message target is the bot or a
144     # Channel), and 'anywhere' and 'everywhere' are replaced by Symbol :*
145     #
146     def parse_channel_list(string)
147       return [:*] if [:anywhere, :everywhere].include? string.to_sym
148       string.scan(
149       /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/
150                  ).map { |chan_ar|
151         chan = chan_ar.first
152         case chan.to_sym
153         when :private, :pvt
154           :"?"
155         when :here
156           case self.target
157           when Channel
158             self.target.name
159           else
160             :"?"
161           end
162         else
163           chan
164         end
165       }.uniq
166     end
167   end
168 end