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
136 alias :replied? :replied
138 # should the message be ignored?
139 attr_accessor :ignored
140 alias :ignored? :ignored
142 # set this to true if the method that delegates the message is run in a thread
143 attr_accessor :in_thread
144 alias :in_thread? :in_thread
146 def inspect(fields=nil)
147 ret = self.__to_s__[0..-2]
148 ret << ' bot=' << @bot.__to_s__
149 ret << ' server=' << server.to_s
150 ret << ' time=' << time.to_s
151 ret << ' source=' << source.to_s
152 ret << ' target=' << target.to_s
153 ret << ' message=' << message.inspect
154 ret << ' logmessage=' << logmessage.inspect
155 ret << ' plainmessage=' << plainmessage.inspect
156 ret << fields if fields
157 ret << ' (identified)' if identified?
158 ret << ' (addressed to me)' if address?
159 ret << ' (replied)' if replied?
160 ret << ' (ignored)' if ignored?
161 ret << ' (in thread)' if in_thread?
165 # instantiate a new Message
166 # bot:: associated bot class
167 # server:: Server where the message took place
168 # source:: User that sent the message
169 # target:: User/Channel is destined for
170 # message:: actual message
171 def initialize(bot, server, source, target, message)
172 @msg_wants_id = false unless defined? @msg_wants_id
179 @message = message || ""
186 if @msg_wants_id && @server.capabilities[:"identify-msg"]
187 if @message =~ /^([-+])(.*)/
188 @identified = ($1=="+")
191 warning "Message does not have identification"
194 @logmessage = @message.dup
195 @plainmessage = BasicUserMessage.strip_formatting(@message)
196 @message = BasicUserMessage.strip_initial_formatting(@message)
198 @address = true if source == @bot.myself
202 # Access the nick of the source
205 @source.nick rescue @source.to_s
208 # Access the user@host of the source
211 "#{@source.user}@#{@source.host}" rescue @source.to_s
214 # Access the botuser corresponding to the source, if any
217 source.botuser rescue @bot.auth.everyone
221 # Was the message from an identified user?
226 # returns true if the message was addressed to the bot.
227 # This includes any private message to the bot, or any public message
228 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
229 # a kick message when bot was kicked etc.
234 # strip mIRC colour escapes from a string
235 def BasicUserMessage.stripcolour(string)
236 return "" unless string
237 ret = string.gsub(ColorRx, "")
238 #ret.tr!("\x00-\x1f", "")
242 def BasicUserMessage.strip_initial_formatting(string)
243 return "" unless string
244 ret = string.gsub(/^#{FormattingRx}|#{FormattingRx}$/,"")
247 def BasicUserMessage.strip_formatting(string)
248 string.gsub(FormattingRx,"")
253 # class for handling welcome messages from the server
254 class WelcomeMessage < BasicUserMessage
257 # class for handling MOTD from the server. Yes, MotdMessage
258 # is somewhat redundant, but it fits with the naming scheme
259 class MotdMessage < BasicUserMessage
262 # class for handling IRC user messages. Includes some utilities for handling
263 # the message, for example in plugins.
264 # The +message+ member will have any bot addressing "^bot: " removed
265 # (address? will return true in this case)
266 class UserMessage < BasicUserMessage
269 fields = ' plugin=' << plugin.inspect
270 fields << ' params=' << params.inspect
271 fields << ' channel=' << channel.to_s if channel
272 fields << ' (reply to ' << replyto.to_s << ')'
274 fields << ' (private)'
276 fields << ' (public)'
279 fields << ' (action)'
281 fields << ' (CTCP ' << ctcp << ')'
286 # for plugin messages, the name of the plugin invoked by the message
289 # for plugin messages, the rest of the message, with the plugin name
293 # convenience member. Who to reply to (i.e. would be sourcenick for a
294 # privately addressed message, or target (the channel) for a publicly
298 # channel the message was in, nil for privately addressed messages
301 # for PRIVMSGs, false unless the message was a CTCP command,
302 # in which case it evaluates to the CTCP command itself
303 # (TIME, PING, VERSION, etc). The CTCP command parameters
304 # are then stored in the message.
307 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
308 # will be stripped from the message)
311 # instantiate a new UserMessage
312 # bot:: associated bot class
313 # source:: hostmask of the message source
314 # target:: nick/channel message is destined for
315 # message:: message part
316 def initialize(bot, server, source, target, message)
317 super(bot, server, source, target, message)
324 if @address = (target == @bot.myself)
333 # check for option extra addressing prefixes, e.g "|search foo", or
334 # "!version" - first match wins
335 bot.config['core.address_prefix'].each {|mprefix|
336 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
342 # even if they used above prefixes, we allow for silly people who
343 # combine all possible types, e.g. "|rbot: hello", or
344 # "/msg rbot rbot: hello", etc
345 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
349 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
351 # FIXME need to support quoting of NULL and CR/LF, see
352 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
353 @message = $3 || String.new
354 @action = @ctcp == 'ACTION'
355 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
356 @logmessage = @message.dup
357 @plainmessage = BasicUserMessage.strip_formatting(@message)
358 @message = BasicUserMessage.strip_initial_formatting(@message)
361 # free splitting for plugins
362 @params = @message.dup
363 # Created messges (such as by fake_message) can contain multiple lines
364 if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "")
365 @plugin = $1.downcase
366 @params = nil unless @params.length > 0
370 # returns true for private messages, e.g. "/msg bot hello"
375 # returns true if the message was in a channel
384 # convenience method to reply to a message, useful in plugins. It's the
386 # <tt>@bot.say m.replyto, string</tt>
387 # So if the message is private, it will reply to the user. If it was
388 # in a channel, it will reply in the channel.
389 def plainreply(string, options={})
390 @bot.say @replyto, string, options
394 # Same as reply, but when replying in public it adds the nick of the user
395 # the bot is replying to
396 def nickreply(string, options={})
397 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
398 @bot.say @replyto, extra + string, options
402 # the default reply style is to nickreply unless the reply already contains
403 # the nick or core.reply_with_nick is set to false
405 def reply(string, options={})
406 if @bot.config['core.reply_with_nick'] and not string =~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/
407 return nickreply(string, options)
409 plainreply(string, options)
412 # convenience method to reply to a message with an action. It's the
414 # <tt>@bot.action m.replyto, string</tt>
415 # So if the message is private, it will reply to the user. If it was
416 # in a channel, it will reply in the channel.
417 def act(string, options={})
418 @bot.action @replyto, string, options
422 # send a CTCP response, i.e. a private NOTICE to the sender
423 # with the same CTCP command and the reply as a parameter
424 def ctcp_reply(string, options={})
425 @bot.ctcp_notice @source, @ctcp, string, options
428 # convenience method to reply "okay" in the current language to the
431 self.plainreply @bot.lang.get("okay")
434 # Like the above, but append the username
436 str = @bot.lang.get("okay").dup
438 # remove final punctuation
439 str.gsub!(/[!,.]$/,"")
440 str += ", #{@source}"
445 # the default okay style is the same as the default reply style
448 if @bot.config['core.reply_with_nick']
454 # send a NOTICE to the message source
456 def notify(msg,opts={})
457 @bot.notice(sourcenick, msg, opts)
462 # class to manage IRC PRIVMSGs
463 class PrivMessage < UserMessage
464 def initialize(bot, server, source, target, message, opts={})
465 @msg_wants_id = opts[:handle_id]
466 super(bot, server, source, target, message)
470 # class to manage IRC NOTICEs
471 class NoticeMessage < UserMessage
472 def initialize(bot, server, source, target, message, opts={})
473 @msg_wants_id = opts[:handle_id]
474 super(bot, server, source, target, message)
478 # class to manage IRC KICKs
479 # +address?+ can be used as a shortcut to see if the bot was kicked,
480 # basically, +target+ was kicked from +channel+ by +source+ with +message+
481 class KickMessage < BasicUserMessage
482 # channel user was kicked from
486 fields = ' channel=' << channel.to_s
490 def initialize(bot, server, source, target, channel, message="")
491 super(bot, server, source, target, message)
496 # class to manage IRC INVITEs
497 # +address?+ can be used as a shortcut to see if the bot was invited,
498 # which should be true except for server bugs
499 class InviteMessage < BasicUserMessage
500 # channel user was invited to
504 fields = ' channel=' << channel.to_s
508 def initialize(bot, server, source, target, channel, message="")
509 super(bot, server, source, target, message)
514 # class to pass IRC Nick changes in. @message contains the old nickame,
515 # @sourcenick contains the new one.
516 class NickMessage < BasicUserMessage
518 def initialize(bot, server, source, oldnick, newnick)
519 super(bot, server, source, oldnick, newnick)
532 fields = ' old=' << oldnick
533 fields << ' new=' << newnick
538 # class to manage mode changes
539 class ModeChangeMessage < BasicUserMessage
541 def initialize(bot, server, source, target, message="")
542 super(bot, server, source, target, message)
547 fields = ' modes=' << modes.inspect
552 # class to manage WHOIS replies
553 class WhoisMessage < BasicUserMessage
555 def initialize(bot, server, source, target, whois)
556 super(bot, server, source, target, "")
561 fields = ' whois=' << whois.inspect
566 # class to manage NAME replies
567 class NamesMessage < BasicUserMessage
569 def initialize(bot, server, source, target, message="")
570 super(bot, server, source, target, message)
575 fields = ' users=' << users.inspect
580 class QuitMessage < BasicUserMessage
581 attr_accessor :was_on
582 def initialize(bot, server, source, target, message="")
583 super(bot, server, source, target, message)
588 class TopicMessage < BasicUserMessage
591 # topic set at (unixtime)
592 attr_reader :timestamp
593 # topic set on channel
596 # :info if topic info, :set if topic set
597 attr_accessor :info_or_set
598 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
599 super(bot, server, source, channel, topic.text)
601 @timestamp = topic.set_on
607 fields = ' topic=' << topic
608 fields << ' (set on ' << timestamp << ')'
613 # class to manage channel joins
614 class JoinMessage < BasicUserMessage
619 fields = ' channel=' << channel.to_s
623 def initialize(bot, server, source, channel, message="")
624 super(bot, server, source, channel, message)
626 # in this case sourcenick is the nick that could be the bot
630 # class to manage channel parts
631 # same as a join, but can have a message too
632 class PartMessage < JoinMessage
635 class UnknownMessage < BasicUserMessage