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 BotConfig.register BotConfigBooleanValue.new('core.reply_with_nick',
8 :default => false, :wizard => true,
9 :desc => "if true, the bot will prepend the nick to what he has to say when replying (e.g. 'markey: you can't do that!')"
12 BotConfig.register BotConfigStringValue.new('core.nick_postfix',
13 :default => ':', :wizard => true,
14 :desc => "when replying with nick put this character after the nick of the user the bot is replying to"
17 # base user message class, all user messages derive from this
18 # (a user message is defined as having a source hostmask, a target
19 # nick/channel and a message part)
20 class BasicUserMessage
28 # when the message was received
31 # User that originated the message
34 # User/Channel message was sent to
37 # contents of the message
38 attr_accessor :message
40 # has the message been replied to/handled by a plugin?
41 attr_accessor :replied
43 # instantiate a new Message
44 # bot:: associated bot class
45 # server:: Server where the message took place
46 # source:: User that sent the message
47 # target:: User/Channel is destined for
48 # message:: actual message
49 def initialize(bot, server, source, target, message)
50 @msg_wants_id = false unless defined? @msg_wants_id
57 @message = BasicUserMessage.stripcolour message
62 if @msg_wants_id && @server.capabilities[:"identify-msg"]
63 if @message =~ /^([-+])(.*)/
64 @identified = ($1=="+")
67 warning "Message does not have identification"
71 if target && target == @bot.myself
77 # Access the nick of the source
83 # Access the user@host of the source
86 "#{@source.user}@#{@source.host}"
89 # Was the message from an identified user?
94 # returns true if the message was addressed to the bot.
95 # This includes any private message to the bot, or any public message
96 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
97 # a kick message when bot was kicked etc.
102 # has this message been replied to by a plugin?
107 # strip mIRC colour escapes from a string
108 def BasicUserMessage.stripcolour(string)
109 return "" unless string
110 ret = string.gsub(ColorRx, "")
111 #ret.tr!("\x00-\x1f", "")
117 # class for handling IRC user messages. Includes some utilities for handling
118 # the message, for example in plugins.
119 # The +message+ member will have any bot addressing "^bot: " removed
120 # (address? will return true in this case)
121 class UserMessage < BasicUserMessage
123 # for plugin messages, the name of the plugin invoked by the message
126 # for plugin messages, the rest of the message, with the plugin name
130 # convenience member. Who to reply to (i.e. would be sourcenick for a
131 # privately addressed message, or target (the channel) for a publicly
135 # channel the message was in, nil for privately addressed messages
138 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
139 # will be stripped from the message)
142 # instantiate a new UserMessage
143 # bot:: associated bot class
144 # source:: hostmask of the message source
145 # target:: nick/channel message is destined for
146 # message:: message part
147 def initialize(bot, server, source, target, message)
148 super(bot, server, source, target, message)
154 if target == @bot.myself
164 # check for option extra addressing prefixes, e.g "|search foo", or
165 # "!version" - first match wins
166 bot.config['core.address_prefix'].each {|mprefix|
167 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
173 # even if they used above prefixes, we allow for silly people who
174 # combine all possible types, e.g. "|rbot: hello", or
175 # "/msg rbot rbot: hello", etc
176 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
180 if(@message =~ /^\001ACTION\s(.+)\001/)
185 # free splitting for plugins
186 @params = @message.dup
187 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
188 @plugin = $1.downcase
189 @params = nil unless @params.length > 0
193 # returns true for private messages, e.g. "/msg bot hello"
198 # returns true if the message was in a channel
207 # convenience method to reply to a message, useful in plugins. It's the
209 # <tt>@bot.say m.replyto, string</tt>
210 # So if the message is private, it will reply to the user. If it was
211 # in a channel, it will reply in the channel.
212 def plainreply(string, options={})
213 @bot.say @replyto, string, options
217 # Same as reply, but when replying in public it adds the nick of the user
218 # the bot is replying to
219 def nickreply(string, options={})
220 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
221 @bot.say @replyto, extra + string, options
225 # the default reply style is to nickreply unless the reply already contains
226 # the nick or core.reply_with_nick is set to false
228 def reply(string, options={})
229 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
230 return nickreply(string, options)
232 plainreply(string, options)
235 # convenience method to reply to a message with an action. It's the
237 # <tt>@bot.action m.replyto, string</tt>
238 # So if the message is private, it will reply to the user. If it was
239 # in a channel, it will reply in the channel.
240 def act(string, options={})
241 @bot.action @replyto, string, options
245 # convenience method to reply "okay" in the current language to the
248 self.plainreply @bot.lang.get("okay")
251 # Like the above, but append the username
253 str = @bot.lang.get("okay").dup
255 # remove final punctuation
256 str.gsub!(/[!,.]$/,"")
257 str += ", #{@source}"
262 # the default okay style is the same as the default reply style
265 if @bot.config['core.reply_with_nick']
273 # class to manage IRC PRIVMSGs
274 class PrivMessage < UserMessage
275 def initialize(bot, server, source, target, message)
281 # class to manage IRC NOTICEs
282 class NoticeMessage < UserMessage
283 def initialize(bot, server, source, target, message)
289 # class to manage IRC KICKs
290 # +address?+ can be used as a shortcut to see if the bot was kicked,
291 # basically, +target+ was kicked from +channel+ by +source+ with +message+
292 class KickMessage < BasicUserMessage
293 # channel user was kicked from
296 def initialize(bot, server, source, target, channel, message="")
297 super(bot, server, source, target, message)
302 # class to pass IRC Nick changes in. @message contains the old nickame,
303 # @sourcenick contains the new one.
304 class NickMessage < BasicUserMessage
305 def initialize(bot, server, source, oldnick, newnick)
306 super(bot, server, source, oldnick, newnick)
318 class QuitMessage < BasicUserMessage
319 def initialize(bot, server, source, target, message="")
320 super(bot, server, source, target, message)
324 class TopicMessage < BasicUserMessage
327 # topic set at (unixtime)
328 attr_reader :timestamp
329 # topic set on channel
332 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
333 super(bot, server, source, channel, topic.text)
335 @timestamp = topic.set_on
340 # class to manage channel joins
341 class JoinMessage < BasicUserMessage
344 def initialize(bot, server, source, channel, message="")
345 super(bot, server, source, channel, message)
347 # in this case sourcenick is the nick that could be the bot
348 @address = (source == @bot.myself)
352 # class to manage channel parts
353 # same as a join, but can have a message too
354 class PartMessage < JoinMessage