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 ...)
36 AttributeRx = /#{Bold}|#{Underline}|#{Reverse}|#{Italic}|#{NormalText}/
38 # Color is prefixed by \003 and followed by optional
39 # foreground and background specifications, two-digits-max
40 # numbers separated by a comma. One of the two parts
43 ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
45 FormattingRx = /#{AttributeRx}|#{ColorRx}/
47 # Standard color codes
74 # Convert a String or Symbol into a color number
75 def Irc.find_color(data)
76 "%02d" % if Integer === data
79 f = if String === data
92 # Insert the full color code for a given
93 # foreground/background combination.
94 def Irc.color(fg=nil,bg=nil)
97 str << Irc.find_color(fg)
100 str << "," << Irc.find_color(bg)
105 # base user message class, all user messages derive from this
106 # (a user message is defined as having a source hostmask, a target
107 # nick/channel and a message part)
108 class BasicUserMessage
116 # when the message was received
119 # User that originated the message
122 # User/Channel message was sent to
125 # contents of the message (stripped of initial/final format codes)
126 attr_accessor :message
128 # contents of the message (for logging purposes)
129 attr_accessor :logmessage
131 # contents of the message (stripped of all formatting)
132 attr_accessor :plainmessage
134 # has the message been replied to/handled by a plugin?
135 attr_accessor :replied
137 # should the message be ignored?
138 attr_accessor :ignored
139 alias :ignored? :ignored
141 # set this to true if the method that delegates the message is run in a thread
142 attr_accessor :in_thread
143 alias :in_thread? :in_thread
145 # instantiate a new Message
146 # bot:: associated bot class
147 # server:: Server where the message took place
148 # source:: User that sent the message
149 # target:: User/Channel is destined for
150 # message:: actual message
151 def initialize(bot, server, source, target, message)
152 @msg_wants_id = false unless defined? @msg_wants_id
159 @message = message || ""
166 if @msg_wants_id && @server.capabilities[:"identify-msg"]
167 if @message =~ /^([-+])(.*)/
168 @identified = ($1=="+")
171 warning "Message does not have identification"
174 @logmessage = @message.dup
175 @plainmessage = BasicUserMessage.strip_formatting(@message)
176 @message = BasicUserMessage.strip_initial_formatting(@message)
178 if target && target == @bot.myself
184 # Access the nick of the source
187 @source.nick rescue @source.to_s
190 # Access the user@host of the source
193 "#{@source.user}@#{@source.host}" rescue @source.to_s
196 # Access the botuser corresponding to the source, if any
199 source.botuser rescue @bot.auth.everyone
203 # Was the message from an identified user?
208 # returns true if the message was addressed to the bot.
209 # This includes any private message to the bot, or any public message
210 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
211 # a kick message when bot was kicked etc.
216 # has this message been replied to by a plugin?
221 # strip mIRC colour escapes from a string
222 def BasicUserMessage.stripcolour(string)
223 return "" unless string
224 ret = string.gsub(ColorRx, "")
225 #ret.tr!("\x00-\x1f", "")
229 def BasicUserMessage.strip_initial_formatting(string)
230 return "" unless string
231 ret = string.gsub(/^#{FormattingRx}|#{FormattingRx}$/,"")
234 def BasicUserMessage.strip_formatting(string)
235 string.gsub(FormattingRx,"")
240 # class for handling welcome messages from the server
241 class WelcomeMessage < BasicUserMessage
244 # class for handling MOTD from the server. Yes, MotdMessage
245 # is somewhat redundant, but it fits with the naming scheme
246 class MotdMessage < BasicUserMessage
249 # class for handling IRC user messages. Includes some utilities for handling
250 # the message, for example in plugins.
251 # The +message+ member will have any bot addressing "^bot: " removed
252 # (address? will return true in this case)
253 class UserMessage < BasicUserMessage
255 # for plugin messages, the name of the plugin invoked by the message
258 # for plugin messages, the rest of the message, with the plugin name
262 # convenience member. Who to reply to (i.e. would be sourcenick for a
263 # privately addressed message, or target (the channel) for a publicly
267 # channel the message was in, nil for privately addressed messages
270 # for PRIVMSGs, false unless the message was a CTCP command,
271 # in which case it evaluates to the CTCP command itself
272 # (TIME, PING, VERSION, etc). The CTCP command parameters
273 # are then stored in the message.
276 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
277 # will be stripped from the message)
280 # instantiate a new UserMessage
281 # bot:: associated bot class
282 # source:: hostmask of the message source
283 # target:: nick/channel message is destined for
284 # message:: message part
285 def initialize(bot, server, source, target, message)
286 super(bot, server, source, target, message)
293 if target == @bot.myself
303 # check for option extra addressing prefixes, e.g "|search foo", or
304 # "!version" - first match wins
305 bot.config['core.address_prefix'].each {|mprefix|
306 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
312 # even if they used above prefixes, we allow for silly people who
313 # combine all possible types, e.g. "|rbot: hello", or
314 # "/msg rbot rbot: hello", etc
315 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
319 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
321 # FIXME need to support quoting of NULL and CR/LF, see
322 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
323 @message = $3 || String.new
324 @action = @ctcp == 'ACTION'
325 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
326 @logmessage = @message.dup
329 # free splitting for plugins
330 @params = @message.dup
331 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
332 @plugin = $1.downcase
333 @params = nil unless @params.length > 0
337 # returns true for private messages, e.g. "/msg bot hello"
342 # returns true if the message was in a channel
351 # convenience method to reply to a message, useful in plugins. It's the
353 # <tt>@bot.say m.replyto, string</tt>
354 # So if the message is private, it will reply to the user. If it was
355 # in a channel, it will reply in the channel.
356 def plainreply(string, options={})
357 @bot.say @replyto, string, options
361 # Same as reply, but when replying in public it adds the nick of the user
362 # the bot is replying to
363 def nickreply(string, options={})
364 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
365 @bot.say @replyto, extra + string, options
369 # the default reply style is to nickreply unless the reply already contains
370 # the nick or core.reply_with_nick is set to false
372 def reply(string, options={})
373 if @bot.config['core.reply_with_nick'] and not string =~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/
374 return nickreply(string, options)
376 plainreply(string, options)
379 # convenience method to reply to a message with an action. It's the
381 # <tt>@bot.action m.replyto, string</tt>
382 # So if the message is private, it will reply to the user. If it was
383 # in a channel, it will reply in the channel.
384 def act(string, options={})
385 @bot.action @replyto, string, options
389 # send a CTCP response, i.e. a private NOTICE to the sender
390 # with the same CTCP command and the reply as a parameter
391 def ctcp_reply(string, options={})
392 @bot.ctcp_notice @source, @ctcp, string, options
395 # convenience method to reply "okay" in the current language to the
398 self.plainreply @bot.lang.get("okay")
401 # Like the above, but append the username
403 str = @bot.lang.get("okay").dup
405 # remove final punctuation
406 str.gsub!(/[!,.]$/,"")
407 str += ", #{@source}"
412 # the default okay style is the same as the default reply style
415 if @bot.config['core.reply_with_nick']
421 # send a NOTICE to the message source
423 def notify(msg,opts={})
424 @bot.notice(sourcenick, msg, opts)
429 # class to manage IRC PRIVMSGs
430 class PrivMessage < UserMessage
431 def initialize(bot, server, source, target, message)
437 # class to manage IRC NOTICEs
438 class NoticeMessage < UserMessage
439 def initialize(bot, server, source, target, message)
445 # class to manage IRC KICKs
446 # +address?+ can be used as a shortcut to see if the bot was kicked,
447 # basically, +target+ was kicked from +channel+ by +source+ with +message+
448 class KickMessage < BasicUserMessage
449 # channel user was kicked from
452 def initialize(bot, server, source, target, channel, message="")
453 super(bot, server, source, target, message)
458 # class to manage IRC INVITEs
459 # +address?+ can be used as a shortcut to see if the bot was invited,
460 # which should be true except for server bugs
461 class InviteMessage < BasicUserMessage
462 # channel user was invited to
465 def initialize(bot, server, source, target, channel, message="")
466 super(bot, server, source, target, message)
471 # class to pass IRC Nick changes in. @message contains the old nickame,
472 # @sourcenick contains the new one.
473 class NickMessage < BasicUserMessage
475 def initialize(bot, server, source, oldnick, newnick)
476 super(bot, server, source, oldnick, newnick)
489 # class to manage mode changes
490 class ModeChangeMessage < BasicUserMessage
492 def initialize(bot, server, source, target, message="")
493 super(bot, server, source, target, message)
494 @address = (source == @bot.myself)
499 # class to manage NAME replies
500 class NamesMessage < BasicUserMessage
502 def initialize(bot, server, source, target, message="")
503 super(bot, server, source, target, message)
508 class QuitMessage < BasicUserMessage
509 attr_accessor :was_on
510 def initialize(bot, server, source, target, message="")
511 super(bot, server, source, target, message)
516 class TopicMessage < BasicUserMessage
519 # topic set at (unixtime)
520 attr_reader :timestamp
521 # topic set on channel
524 # :info if topic info, :set if topic set
525 attr_accessor :info_or_set
526 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
527 super(bot, server, source, channel, topic.text)
529 @timestamp = topic.set_on
535 # class to manage channel joins
536 class JoinMessage < BasicUserMessage
539 def initialize(bot, server, source, channel, message="")
540 super(bot, server, source, channel, message)
542 # in this case sourcenick is the nick that could be the bot
543 @address = (source == @bot.myself)
547 # class to manage channel parts
548 # same as a join, but can have a message too
549 class PartMessage < JoinMessage
552 class UnknownMessage < BasicUserMessage