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"
25 Config.register BooleanValue.new('core.private_replies',
27 :desc => 'Should the bot reply to private instead of the channel?'
33 # Define standard IRC attriubtes (not so standard actually,
34 # but the closest thing we have ...)
40 AttributeRx = /#{Bold}|#{Underline}|#{Reverse}|#{Italic}|#{NormalText}/
42 # Color is prefixed by \003 and followed by optional
43 # foreground and background specifications, two-digits-max
44 # numbers separated by a comma. One of the two parts
47 ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
49 FormattingRx = /#{AttributeRx}|#{ColorRx}/
51 # Standard color codes
78 # Convert a String or Symbol into a color number
79 def Irc.find_color(data)
80 "%02d" % if Integer === data
83 f = if String === data
96 # Insert the full color code for a given
97 # foreground/background combination.
98 def Irc.color(fg=nil,bg=nil)
101 str << Irc.find_color(fg)
104 str << "," << Irc.find_color(bg)
109 # base user message class, all user messages derive from this
110 # (a user message is defined as having a source hostmask, a target
111 # nick/channel and a message part)
112 class BasicUserMessage
120 # when the message was received
123 # User that originated the message
126 # User/Channel message was sent to
129 # contents of the message (stripped of initial/final format codes)
130 attr_accessor :message
132 # contents of the message (for logging purposes)
133 attr_accessor :logmessage
135 # contents of the message (stripped of all formatting)
136 attr_accessor :plainmessage
138 # has the message been replied to/handled by a plugin?
139 attr_accessor :replied
140 alias :replied? :replied
142 # should the message be ignored?
143 attr_accessor :ignored
144 alias :ignored? :ignored
146 # set this to true if the method that delegates the message is run in a thread
147 attr_accessor :in_thread
148 alias :in_thread? :in_thread
150 def inspect(fields=nil)
151 ret = self.__to_s__[0..-2]
152 ret << ' bot=' << @bot.__to_s__
153 ret << ' server=' << server.to_s
154 ret << ' time=' << time.to_s
155 ret << ' source=' << source.to_s
156 ret << ' target=' << target.to_s
157 ret << ' message=' << message.inspect
158 ret << ' logmessage=' << logmessage.inspect
159 ret << ' plainmessage=' << plainmessage.inspect
160 ret << fields if fields
161 ret << ' (identified)' if identified?
162 ret << ' (addressed to me)' if address?
163 ret << ' (replied)' if replied?
164 ret << ' (ignored)' if ignored?
165 ret << ' (in thread)' if in_thread?
169 # instantiate a new Message
170 # bot:: associated bot class
171 # server:: Server where the message took place
172 # source:: User that sent the message
173 # target:: User/Channel is destined for
174 # message:: actual message
175 def initialize(bot, server, source, target, message)
176 @msg_wants_id = false unless defined? @msg_wants_id
183 @message = message || ""
190 if @msg_wants_id && @server.capabilities[:"identify-msg"]
191 if @message =~ /^([-+])(.*)/
192 @identified = ($1=="+")
195 warning "Message does not have identification"
198 @logmessage = @message.dup
199 @plainmessage = BasicUserMessage.strip_formatting(@message)
200 @message = BasicUserMessage.strip_initial_formatting(@message)
202 if target && target == @bot.myself
208 # Access the nick of the source
211 @source.nick rescue @source.to_s
214 # Access the user@host of the source
217 "#{@source.user}@#{@source.host}" rescue @source.to_s
220 # Access the botuser corresponding to the source, if any
223 source.botuser rescue @bot.auth.everyone
227 # Was the message from an identified user?
232 # returns true if the message was addressed to the bot.
233 # This includes any private message to the bot, or any public message
234 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
235 # a kick message when bot was kicked etc.
240 # strip mIRC colour escapes from a string
241 def BasicUserMessage.stripcolour(string)
242 return "" unless string
243 ret = string.gsub(ColorRx, "")
244 #ret.tr!("\x00-\x1f", "")
248 def BasicUserMessage.strip_initial_formatting(string)
249 return "" unless string
250 ret = string.gsub(/^#{FormattingRx}|#{FormattingRx}$/,"")
253 def BasicUserMessage.strip_formatting(string)
254 string.gsub(FormattingRx,"")
259 # class for handling welcome messages from the server
260 class WelcomeMessage < BasicUserMessage
263 # class for handling MOTD from the server. Yes, MotdMessage
264 # is somewhat redundant, but it fits with the naming scheme
265 class MotdMessage < BasicUserMessage
268 # class for handling IRC user messages. Includes some utilities for handling
269 # the message, for example in plugins.
270 # The +message+ member will have any bot addressing "^bot: " removed
271 # (address? will return true in this case)
272 class UserMessage < BasicUserMessage
275 fields = ' plugin=' << plugin.inspect
276 fields << ' params=' << params.inspect
277 fields << ' channel=' << channel.to_s if channel
278 fields << ' (reply to ' << replyto.to_s << ')'
280 fields << ' (private)'
282 fields << ' (public)'
285 fields << ' (action)'
287 fields << ' (CTCP ' << ctcp << ')'
292 # for plugin messages, the name of the plugin invoked by the message
295 # for plugin messages, the rest of the message, with the plugin name
299 # convenience member. Who to reply to (i.e. would be sourcenick for a
300 # privately addressed message, or target (the channel) for a publicly
304 # channel the message was in, nil for privately addressed messages
307 # for PRIVMSGs, false unless the message was a CTCP command,
308 # in which case it evaluates to the CTCP command itself
309 # (TIME, PING, VERSION, etc). The CTCP command parameters
310 # are then stored in the message.
313 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
314 # will be stripped from the message)
317 # instantiate a new UserMessage
318 # bot:: associated bot class
319 # source:: hostmask of the message source
320 # target:: nick/channel message is destined for
321 # message:: message part
322 def initialize(bot, server, source, target, message)
323 super(bot, server, source, target, message)
330 if target == @bot.myself
340 # check for option extra addressing prefixes, e.g "|search foo", or
341 # "!version" - first match wins
342 bot.config['core.address_prefix'].each {|mprefix|
343 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
349 # even if they used above prefixes, we allow for silly people who
350 # combine all possible types, e.g. "|rbot: hello", or
351 # "/msg rbot rbot: hello", etc
352 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
356 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
358 # FIXME need to support quoting of NULL and CR/LF, see
359 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
360 @message = $3 || String.new
361 @action = @ctcp == 'ACTION'
362 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
363 @logmessage = @message.dup
364 @plainmessage = BasicUserMessage.strip_formatting(@message)
365 @message = BasicUserMessage.strip_initial_formatting(@message)
368 # free splitting for plugins
369 @params = @message.dup
370 # Created messges (such as by fake_message) can contain multiple lines
371 if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "")
372 @plugin = $1.downcase
373 @params = nil unless @params.length > 0
377 # returns true for private messages, e.g. "/msg bot hello"
382 # returns true if the message was in a channel
391 # convenience method to reply to a message, useful in plugins. It's the
393 # <tt>@bot.say m.replyto, string</tt>
394 # So if the message is private, it will reply to the user. If it was
395 # in a channel, it will reply in the channel.
396 def plainreply(string, options={})
397 reply string, {:nick => false}.merge(options)
400 # Same as reply, but when replying in public it adds the nick of the user
401 # the bot is replying to
402 def nickreply(string, options={})
403 reply string, {:nick => true}.merge(options)
406 # The general way to reply to a command. The following options are available:
407 # :nick [false, true, :auto]
408 # state if the nick of the user calling the command should be prepended
409 # :auto uses core.reply_with_nick
411 # :to [:private, :public, :auto]
412 # where should the bot reply?
413 # :private always reply to the nick
414 # :public reply to the channel (if available)
415 # :auto uses core.private_replies
417 def reply(string, options={})
418 opts = {:nick => :auto, :to => :auto}.merge options
420 if opts[:nick] == :auto
421 opts[:nick] = @bot.config['core.reply_with_nick']
426 elsif opts[:to] == :auto
427 opts[:to] = @bot.config['core.private_replies'] ? :private : :public
431 opts[:to] != :private &&
432 string !~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/)
433 string = "#{@source}#{@bot.config['core.nick_postfix']} #{string}"
435 to = (opts[:to] == :private) ? source : @channel
436 @bot.say to, string, options
440 # convenience method to reply to a message with an action. It's the
442 # <tt>@bot.action m.replyto, string</tt>
443 # So if the message is private, it will reply to the user. If it was
444 # in a channel, it will reply in the channel.
445 def act(string, options={})
446 @bot.action @replyto, string, options
450 # send a CTCP response, i.e. a private NOTICE to the sender
451 # with the same CTCP command and the reply as a parameter
452 def ctcp_reply(string, options={})
453 @bot.ctcp_notice @source, @ctcp, string, options
456 # convenience method to reply "okay" in the current language to the
459 self.reply @bot.lang.get("okay"), :nick => false
462 # Like the above, but append the username
464 str = @bot.lang.get("okay").dup
466 # remove final punctuation
467 str.gsub!(/[!,.]$/,"")
468 str += ", #{@source}"
470 self.reply str, :nick => false
473 # the default okay style is the same as the default reply style
476 @bot.config['core.reply_with_nick'] ? nickokay : plainokay
479 # send a NOTICE to the message source
481 def notify(msg,opts={})
482 @bot.notice(sourcenick, msg, opts)
487 # class to manage IRC PRIVMSGs
488 class PrivMessage < UserMessage
489 def initialize(bot, server, source, target, message, opts={})
490 @msg_wants_id = opts[:handle_id]
491 super(bot, server, source, target, message)
495 # class to manage IRC NOTICEs
496 class NoticeMessage < UserMessage
497 def initialize(bot, server, source, target, message, opts={})
498 @msg_wants_id = opts[:handle_id]
499 super(bot, server, source, target, message)
503 # class to manage IRC KICKs
504 # +address?+ can be used as a shortcut to see if the bot was kicked,
505 # basically, +target+ was kicked from +channel+ by +source+ with +message+
506 class KickMessage < BasicUserMessage
507 # channel user was kicked from
511 fields = ' channel=' << channel.to_s
515 def initialize(bot, server, source, target, channel, message="")
516 super(bot, server, source, target, message)
521 # class to manage IRC INVITEs
522 # +address?+ can be used as a shortcut to see if the bot was invited,
523 # which should be true except for server bugs
524 class InviteMessage < BasicUserMessage
525 # channel user was invited to
529 fields = ' channel=' << channel.to_s
533 def initialize(bot, server, source, target, channel, message="")
534 super(bot, server, source, target, message)
539 # class to pass IRC Nick changes in. @message contains the old nickame,
540 # @sourcenick contains the new one.
541 class NickMessage < BasicUserMessage
543 def initialize(bot, server, source, oldnick, newnick)
544 super(bot, server, source, oldnick, newnick)
545 @address = (source == @bot.myself)
558 fields = ' old=' << oldnick
559 fields << ' new=' << newnick
564 # class to manage mode changes
565 class ModeChangeMessage < BasicUserMessage
567 def initialize(bot, server, source, target, message="")
568 super(bot, server, source, target, message)
569 @address = (source == @bot.myself)
574 fields = ' modes=' << modes.inspect
579 # class to manage WHOIS replies
580 class WhoisMessage < BasicUserMessage
582 def initialize(bot, server, source, target, whois)
583 super(bot, server, source, target, "")
584 @address = (target == @bot.myself)
589 fields = ' whois=' << whois.inspect
594 # class to manage NAME replies
595 class NamesMessage < BasicUserMessage
597 def initialize(bot, server, source, target, message="")
598 super(bot, server, source, target, message)
603 fields = ' users=' << users.inspect
608 # class to manager Ban list replies
609 class BanlistMessage < BasicUserMessage
613 def initialize(bot, server, source, target, message="")
614 super(bot, server, source, target, message)
619 fields = ' bans=' << bans.inspect
624 class QuitMessage < BasicUserMessage
625 attr_accessor :was_on
626 def initialize(bot, server, source, target, message="")
627 super(bot, server, source, target, message)
632 class TopicMessage < BasicUserMessage
635 # topic set at (unixtime)
636 attr_reader :timestamp
637 # topic set on channel
640 # :info if topic info, :set if topic set
641 attr_accessor :info_or_set
642 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
643 super(bot, server, source, channel, topic.text)
645 @timestamp = topic.set_on
651 fields = ' topic=' << topic
652 fields << ' (set on ' << timestamp << ')'
657 # class to manage channel joins
658 class JoinMessage < BasicUserMessage
663 fields = ' channel=' << channel.to_s
667 def initialize(bot, server, source, channel, message="")
668 super(bot, server, source, channel, message)
670 # in this case sourcenick is the nick that could be the bot
671 @address = (source == @bot.myself)
675 # class to manage channel parts
676 # same as a join, but can have a message too
677 class PartMessage < JoinMessage
680 # class to handle ERR_NOSUCHNICK and ERR_NOSUCHCHANNEL
681 class NoSuchTargetMessage < BasicUserMessage
682 # the channel or nick that was not found
685 def initialize(bot, server, source, target, message='')
686 super(bot, server, source, target, message)
692 class UnknownMessage < BasicUserMessage