4 # :title: IRC message datastructures
11 Config.register ArrayValue.new('core.address_prefix',
12 :default => [], :wizard => true,
13 :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')"
16 Config.register BooleanValue.new('core.reply_with_nick',
17 :default => false, :wizard => true,
18 :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!')"
21 Config.register StringValue.new('core.nick_postfix',
22 :default => ':', :wizard => true,
23 :desc => "when replying with nick put this character after the nick of the user the bot is replying to"
29 # Define standard IRC attriubtes (not so standard actually,
30 # but the closest thing we have ...)
37 # Color is prefixed by \003 and followed by optional
38 # foreground and background specifications, two-digits-max
39 # numbers separated by a comma. One of the two parts
42 ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
44 # Standard color codes
71 # Convert a String or Symbol into a color number
72 def Irc.find_color(data)
76 f = if String === data
89 # Insert the full color code for a given
90 # foreground/background combination.
91 def Irc.color(fg=nil,bg=nil)
94 str << Irc.find_color(fg).to_s
97 str << "," << Irc.find_color(bg).to_s
102 # base user message class, all user messages derive from this
103 # (a user message is defined as having a source hostmask, a target
104 # nick/channel and a message part)
105 class BasicUserMessage
113 # when the message was received
116 # User that originated the message
119 # User/Channel message was sent to
122 # contents of the message
123 attr_accessor :message
125 # has the message been replied to/handled by a plugin?
126 attr_accessor :replied
128 # instantiate a new Message
129 # bot:: associated bot class
130 # server:: Server where the message took place
131 # source:: User that sent the message
132 # target:: User/Channel is destined for
133 # message:: actual message
134 def initialize(bot, server, source, target, message)
135 @msg_wants_id = false unless defined? @msg_wants_id
142 @message = BasicUserMessage.stripcolour message
147 if @msg_wants_id && @server.capabilities[:"identify-msg"]
148 if @message =~ /^([-+])(.*)/
149 @identified = ($1=="+")
152 warning "Message does not have identification"
156 if target && target == @bot.myself
162 # Access the nick of the source
165 @source.nick rescue @source.to_s
168 # Access the user@host of the source
171 "#{@source.user}@#{@source.host}" rescue @source.to_s
174 # Access the botuser corresponding to the source, if any
177 source.botuser rescue @bot.auth.everyone
181 # Was the message from an identified user?
186 # returns true if the message was addressed to the bot.
187 # This includes any private message to the bot, or any public message
188 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
189 # a kick message when bot was kicked etc.
194 # has this message been replied to by a plugin?
199 # strip mIRC colour escapes from a string
200 def BasicUserMessage.stripcolour(string)
201 return "" unless string
202 ret = string.gsub(ColorRx, "")
203 #ret.tr!("\x00-\x1f", "")
209 # class for handling IRC user messages. Includes some utilities for handling
210 # the message, for example in plugins.
211 # The +message+ member will have any bot addressing "^bot: " removed
212 # (address? will return true in this case)
213 class UserMessage < BasicUserMessage
215 # for plugin messages, the name of the plugin invoked by the message
218 # for plugin messages, the rest of the message, with the plugin name
222 # convenience member. Who to reply to (i.e. would be sourcenick for a
223 # privately addressed message, or target (the channel) for a publicly
227 # channel the message was in, nil for privately addressed messages
230 # for PRIVMSGs, false unless the message was a CTCP command,
231 # in which case it evaluates to the CTCP command itself
232 # (TIME, PING, VERSION, etc). The CTCP command parameters
233 # are then stored in the message.
236 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
237 # will be stripped from the message)
240 # instantiate a new UserMessage
241 # bot:: associated bot class
242 # source:: hostmask of the message source
243 # target:: nick/channel message is destined for
244 # message:: message part
245 def initialize(bot, server, source, target, message)
246 super(bot, server, source, target, message)
253 if target == @bot.myself
263 # check for option extra addressing prefixes, e.g "|search foo", or
264 # "!version" - first match wins
265 bot.config['core.address_prefix'].each {|mprefix|
266 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
272 # even if they used above prefixes, we allow for silly people who
273 # combine all possible types, e.g. "|rbot: hello", or
274 # "/msg rbot rbot: hello", etc
275 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
279 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
281 # FIXME need to support quoting of NULL and CR/LF, see
282 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
283 @message = $3 || String.new
284 @action = @ctcp == 'ACTION'
285 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
288 # free splitting for plugins
289 @params = @message.dup
290 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
291 @plugin = $1.downcase
292 @params = nil unless @params.length > 0
296 # returns true for private messages, e.g. "/msg bot hello"
301 # returns true if the message was in a channel
310 # convenience method to reply to a message, useful in plugins. It's the
312 # <tt>@bot.say m.replyto, string</tt>
313 # So if the message is private, it will reply to the user. If it was
314 # in a channel, it will reply in the channel.
315 def plainreply(string, options={})
316 @bot.say @replyto, string, options
320 # Same as reply, but when replying in public it adds the nick of the user
321 # the bot is replying to
322 def nickreply(string, options={})
323 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
324 @bot.say @replyto, extra + string, options
328 # the default reply style is to nickreply unless the reply already contains
329 # the nick or core.reply_with_nick is set to false
331 def reply(string, options={})
332 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
333 return nickreply(string, options)
335 plainreply(string, options)
338 # convenience method to reply to a message with an action. It's the
340 # <tt>@bot.action m.replyto, string</tt>
341 # So if the message is private, it will reply to the user. If it was
342 # in a channel, it will reply in the channel.
343 def act(string, options={})
344 @bot.action @replyto, string, options
348 # send a CTCP response, i.e. a private NOTICE to the sender
349 # with the same CTCP command and the reply as a parameter
350 def ctcp_reply(string, options={})
351 @bot.ctcp_notice @source, @ctcp, string, options
354 # convenience method to reply "okay" in the current language to the
357 self.plainreply @bot.lang.get("okay")
360 # Like the above, but append the username
362 str = @bot.lang.get("okay").dup
364 # remove final punctuation
365 str.gsub!(/[!,.]$/,"")
366 str += ", #{@source}"
371 # the default okay style is the same as the default reply style
374 if @bot.config['core.reply_with_nick']
382 # class to manage IRC PRIVMSGs
383 class PrivMessage < UserMessage
384 def initialize(bot, server, source, target, message)
390 # class to manage IRC NOTICEs
391 class NoticeMessage < UserMessage
392 def initialize(bot, server, source, target, message)
398 # class to manage IRC KICKs
399 # +address?+ can be used as a shortcut to see if the bot was kicked,
400 # basically, +target+ was kicked from +channel+ by +source+ with +message+
401 class KickMessage < BasicUserMessage
402 # channel user was kicked from
405 def initialize(bot, server, source, target, channel, message="")
406 super(bot, server, source, target, message)
411 # class to pass IRC Nick changes in. @message contains the old nickame,
412 # @sourcenick contains the new one.
413 class NickMessage < BasicUserMessage
414 def initialize(bot, server, source, oldnick, newnick)
415 super(bot, server, source, oldnick, newnick)
427 class QuitMessage < BasicUserMessage
428 def initialize(bot, server, source, target, message="")
429 super(bot, server, source, target, message)
433 class TopicMessage < BasicUserMessage
436 # topic set at (unixtime)
437 attr_reader :timestamp
438 # topic set on channel
441 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
442 super(bot, server, source, channel, topic.text)
444 @timestamp = topic.set_on
449 # class to manage channel joins
450 class JoinMessage < BasicUserMessage
453 def initialize(bot, server, source, channel, message="")
454 super(bot, server, source, channel, message)
456 # in this case sourcenick is the nick that could be the bot
457 @address = (source == @bot.myself)
461 # class to manage channel parts
462 # same as a join, but can have a message too
463 class PartMessage < JoinMessage