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 # Created messges (such as by fake_message) can contain multiple lines
332 if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "")
333 @plugin = $1.downcase
334 @params = nil unless @params.length > 0
338 # returns true for private messages, e.g. "/msg bot hello"
343 # returns true if the message was in a channel
352 # convenience method to reply to a message, useful in plugins. It's the
354 # <tt>@bot.say m.replyto, string</tt>
355 # So if the message is private, it will reply to the user. If it was
356 # in a channel, it will reply in the channel.
357 def plainreply(string, options={})
358 @bot.say @replyto, string, options
362 # Same as reply, but when replying in public it adds the nick of the user
363 # the bot is replying to
364 def nickreply(string, options={})
365 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
366 @bot.say @replyto, extra + string, options
370 # the default reply style is to nickreply unless the reply already contains
371 # the nick or core.reply_with_nick is set to false
373 def reply(string, options={})
374 if @bot.config['core.reply_with_nick'] and not string =~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/
375 return nickreply(string, options)
377 plainreply(string, options)
380 # convenience method to reply to a message with an action. It's the
382 # <tt>@bot.action m.replyto, string</tt>
383 # So if the message is private, it will reply to the user. If it was
384 # in a channel, it will reply in the channel.
385 def act(string, options={})
386 @bot.action @replyto, string, options
390 # send a CTCP response, i.e. a private NOTICE to the sender
391 # with the same CTCP command and the reply as a parameter
392 def ctcp_reply(string, options={})
393 @bot.ctcp_notice @source, @ctcp, string, options
396 # convenience method to reply "okay" in the current language to the
399 self.plainreply @bot.lang.get("okay")
402 # Like the above, but append the username
404 str = @bot.lang.get("okay").dup
406 # remove final punctuation
407 str.gsub!(/[!,.]$/,"")
408 str += ", #{@source}"
413 # the default okay style is the same as the default reply style
416 if @bot.config['core.reply_with_nick']
422 # send a NOTICE to the message source
424 def notify(msg,opts={})
425 @bot.notice(sourcenick, msg, opts)
430 # class to manage IRC PRIVMSGs
431 class PrivMessage < UserMessage
432 def initialize(bot, server, source, target, message)
438 # class to manage IRC NOTICEs
439 class NoticeMessage < UserMessage
440 def initialize(bot, server, source, target, message)
446 # class to manage IRC KICKs
447 # +address?+ can be used as a shortcut to see if the bot was kicked,
448 # basically, +target+ was kicked from +channel+ by +source+ with +message+
449 class KickMessage < BasicUserMessage
450 # channel user was kicked from
453 def initialize(bot, server, source, target, channel, message="")
454 super(bot, server, source, target, message)
459 # class to manage IRC INVITEs
460 # +address?+ can be used as a shortcut to see if the bot was invited,
461 # which should be true except for server bugs
462 class InviteMessage < BasicUserMessage
463 # channel user was invited to
466 def initialize(bot, server, source, target, channel, message="")
467 super(bot, server, source, target, message)
472 # class to pass IRC Nick changes in. @message contains the old nickame,
473 # @sourcenick contains the new one.
474 class NickMessage < BasicUserMessage
476 def initialize(bot, server, source, oldnick, newnick)
477 super(bot, server, source, oldnick, newnick)
490 # class to manage mode changes
491 class ModeChangeMessage < BasicUserMessage
493 def initialize(bot, server, source, target, message="")
494 super(bot, server, source, target, message)
495 @address = (source == @bot.myself)
500 # class to manage NAME replies
501 class NamesMessage < BasicUserMessage
503 def initialize(bot, server, source, target, message="")
504 super(bot, server, source, target, message)
509 class QuitMessage < BasicUserMessage
510 attr_accessor :was_on
511 def initialize(bot, server, source, target, message="")
512 super(bot, server, source, target, message)
517 class TopicMessage < BasicUserMessage
520 # topic set at (unixtime)
521 attr_reader :timestamp
522 # topic set on channel
525 # :info if topic info, :set if topic set
526 attr_accessor :info_or_set
527 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
528 super(bot, server, source, channel, topic.text)
530 @timestamp = topic.set_on
536 # class to manage channel joins
537 class JoinMessage < BasicUserMessage
540 def initialize(bot, server, source, channel, message="")
541 super(bot, server, source, channel, message)
543 # in this case sourcenick is the nick that could be the bot
544 @address = (source == @bot.myself)
548 # class to manage channel parts
549 # same as a join, but can have a message too
550 class PartMessage < JoinMessage
553 class UnknownMessage < BasicUserMessage