2 BotConfig.register BotConfigArrayValue.new('core.address_prefix',
3 :default => [], :wizard => true,
4 :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')"
7 # base user message class, all user messages derive from this
8 # (a user message is defined as having a source hostmask, a target
9 # nick/channel and a message part)
10 class BasicUserMessage
15 # when the message was received
18 # hostmask of message source
21 # nick of message source
22 attr_reader :sourcenick
24 # url part of message source
25 attr_reader :sourceaddress
27 # nick/channel message was sent to
30 # contents of the message
31 attr_accessor :message
33 # has the message been replied to/handled by a plugin?
34 attr_accessor :replied
36 # instantiate a new Message
37 # bot:: associated bot class
38 # source:: hostmask of the message source
39 # target:: nick/channel message is destined for
40 # message:: message part
41 def initialize(bot, source, target, message)
42 @msg_wants_id = false unless defined? @msg_wants_id
49 @message = BasicUserMessage.stripcolour message
53 if @msg_wants_id && @bot.capabilities["identify-msg".to_sym]
54 if @message =~ /([-+])(.*)/
55 @identified = ($1=="+")
58 warning "Message does not have identification"
62 # split source into consituent parts
63 if source =~ /^((\S+)!(\S+))$/
68 if target && target.downcase == @bot.nick.downcase
78 # returns true if the message was addressed to the bot.
79 # This includes any private message to the bot, or any public message
80 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
81 # a kick message when bot was kicked etc.
86 # has this message been replied to by a plugin?
91 # strip mIRC colour escapes from a string
92 def BasicUserMessage.stripcolour(string)
93 return "" unless string
94 ret = string.gsub(/\cC\d\d?(?:,\d\d?)?/, "")
95 #ret.tr!("\x00-\x1f", "")
101 # class for handling IRC user messages. Includes some utilities for handling
102 # the message, for example in plugins.
103 # The +message+ member will have any bot addressing "^bot: " removed
104 # (address? will return true in this case)
105 class UserMessage < BasicUserMessage
107 # for plugin messages, the name of the plugin invoked by the message
110 # for plugin messages, the rest of the message, with the plugin name
114 # convenience member. Who to reply to (i.e. would be sourcenick for a
115 # privately addressed message, or target (the channel) for a publicly
119 # channel the message was in, nil for privately addressed messages
122 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
123 # will be stripped from the message)
126 # instantiate a new UserMessage
127 # bot:: associated bot class
128 # source:: hostmask of the message source
129 # target:: nick/channel message is destined for
130 # message:: message part
131 def initialize(bot, source, target, message)
132 super(bot, source, target, message)
138 if target.downcase == @bot.nick.downcase
142 @replyto = @sourcenick
148 # check for option extra addressing prefixes, e.g "|search foo", or
149 # "!version" - first match wins
150 bot.config['core.address_prefix'].each {|mprefix|
151 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
157 # even if they used above prefixes, we allow for silly people who
158 # combine all possible types, e.g. "|rbot: hello", or
159 # "/msg rbot rbot: hello", etc
160 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
164 if(@message =~ /^\001ACTION\s(.+)\001/)
169 # free splitting for plugins
170 @params = @message.dup
171 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
172 @plugin = $1.downcase
173 @params = nil unless @params.length > 0
177 # returns true for private messages, e.g. "/msg bot hello"
182 # returns true if the message was in a channel
191 # convenience method to reply to a message, useful in plugins. It's the
193 # <tt>@bot.say m.replyto, string</tt>
194 # So if the message is private, it will reply to the user. If it was
195 # in a channel, it will reply in the channel.
197 @bot.say @replyto, string
201 # convenience method to reply to a message with an action. It's the
203 # <tt>@bot.action m.replyto, string</tt>
204 # So if the message is private, it will reply to the user. If it was
205 # in a channel, it will reply in the channel.
207 @bot.action @replyto, string
211 # convenience method to reply "okay" in the current language to the
214 @bot.say @replyto, @bot.lang.get("okay")
219 # class to manage IRC PRIVMSGs
220 class PrivMessage < UserMessage
221 def initialize(bot, source, target, message)
227 # class to manage IRC NOTICEs
228 class NoticeMessage < UserMessage
229 def initialize(bot, source, target, message)
235 # class to manage IRC KICKs
236 # +address?+ can be used as a shortcut to see if the bot was kicked,
237 # basically, +target+ was kicked from +channel+ by +source+ with +message+
238 class KickMessage < BasicUserMessage
239 # channel user was kicked from
242 def initialize(bot, source, target, channel, message="")
243 super(bot, source, target, message)
248 # class to pass IRC Nick changes in. @message contains the old nickame,
249 # @sourcenick contains the new one.
250 class NickMessage < BasicUserMessage
251 def initialize(bot, source, oldnick, newnick)
252 super(bot, source, oldnick, newnick)
256 class QuitMessage < BasicUserMessage
257 def initialize(bot, source, target, message="")
258 super(bot, source, target, message)
262 class TopicMessage < BasicUserMessage
265 # topic set at (unixtime)
266 attr_reader :timestamp
267 # topic set on channel
270 def initialize(bot, source, channel, timestamp, topic="")
271 super(bot, source, channel, topic)
273 @timestamp = timestamp
278 # class to manage channel joins
279 class JoinMessage < BasicUserMessage
282 def initialize(bot, source, channel, message="")
283 super(bot, source, channel, message)
285 # in this case sourcenick is the nick that could be the bot
286 @address = (sourcenick.downcase == @bot.nick.downcase)
290 # class to manage channel parts
291 # same as a join, but can have a message too
292 class PartMessage < JoinMessage