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 # contents of the message (for logging purposes)
126 attr_accessor :logmessage
128 # has the message been replied to/handled by a plugin?
129 attr_accessor :replied
131 # instantiate a new Message
132 # bot:: associated bot class
133 # server:: Server where the message took place
134 # source:: User that sent the message
135 # target:: User/Channel is destined for
136 # message:: actual message
137 def initialize(bot, server, source, target, message)
138 @msg_wants_id = false unless defined? @msg_wants_id
145 @message = BasicUserMessage.stripcolour message
150 if @msg_wants_id && @server.capabilities[:"identify-msg"]
151 if @message =~ /^([-+])(.*)/
152 @identified = ($1=="+")
155 warning "Message does not have identification"
158 @logmessage = @message.dup
160 if target && target == @bot.myself
166 # Access the nick of the source
169 @source.nick rescue @source.to_s
172 # Access the user@host of the source
175 "#{@source.user}@#{@source.host}" rescue @source.to_s
178 # Access the botuser corresponding to the source, if any
181 source.botuser rescue @bot.auth.everyone
185 # Was the message from an identified user?
190 # returns true if the message was addressed to the bot.
191 # This includes any private message to the bot, or any public message
192 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
193 # a kick message when bot was kicked etc.
198 # has this message been replied to by a plugin?
203 # strip mIRC colour escapes from a string
204 def BasicUserMessage.stripcolour(string)
205 return "" unless string
206 ret = string.gsub(ColorRx, "")
207 #ret.tr!("\x00-\x1f", "")
213 # class for handling IRC user messages. Includes some utilities for handling
214 # the message, for example in plugins.
215 # The +message+ member will have any bot addressing "^bot: " removed
216 # (address? will return true in this case)
217 class UserMessage < BasicUserMessage
219 # for plugin messages, the name of the plugin invoked by the message
222 # for plugin messages, the rest of the message, with the plugin name
226 # convenience member. Who to reply to (i.e. would be sourcenick for a
227 # privately addressed message, or target (the channel) for a publicly
231 # channel the message was in, nil for privately addressed messages
234 # for PRIVMSGs, false unless the message was a CTCP command,
235 # in which case it evaluates to the CTCP command itself
236 # (TIME, PING, VERSION, etc). The CTCP command parameters
237 # are then stored in the message.
240 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
241 # will be stripped from the message)
244 # instantiate a new UserMessage
245 # bot:: associated bot class
246 # source:: hostmask of the message source
247 # target:: nick/channel message is destined for
248 # message:: message part
249 def initialize(bot, server, source, target, message)
250 super(bot, server, source, target, message)
257 if target == @bot.myself
267 # check for option extra addressing prefixes, e.g "|search foo", or
268 # "!version" - first match wins
269 bot.config['core.address_prefix'].each {|mprefix|
270 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
276 # even if they used above prefixes, we allow for silly people who
277 # combine all possible types, e.g. "|rbot: hello", or
278 # "/msg rbot rbot: hello", etc
279 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
283 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
285 # FIXME need to support quoting of NULL and CR/LF, see
286 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
287 @message = $3 || String.new
288 @action = @ctcp == 'ACTION'
289 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
292 # free splitting for plugins
293 @params = @message.dup
294 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
295 @plugin = $1.downcase
296 @params = nil unless @params.length > 0
300 # returns true for private messages, e.g. "/msg bot hello"
305 # returns true if the message was in a channel
314 # convenience method to reply to a message, useful in plugins. It's the
316 # <tt>@bot.say m.replyto, string</tt>
317 # So if the message is private, it will reply to the user. If it was
318 # in a channel, it will reply in the channel.
319 def plainreply(string, options={})
320 @bot.say @replyto, string, options
324 # Same as reply, but when replying in public it adds the nick of the user
325 # the bot is replying to
326 def nickreply(string, options={})
327 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
328 @bot.say @replyto, extra + string, options
332 # the default reply style is to nickreply unless the reply already contains
333 # the nick or core.reply_with_nick is set to false
335 def reply(string, options={})
336 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
337 return nickreply(string, options)
339 plainreply(string, options)
342 # convenience method to reply to a message with an action. It's the
344 # <tt>@bot.action m.replyto, string</tt>
345 # So if the message is private, it will reply to the user. If it was
346 # in a channel, it will reply in the channel.
347 def act(string, options={})
348 @bot.action @replyto, string, options
352 # send a CTCP response, i.e. a private NOTICE to the sender
353 # with the same CTCP command and the reply as a parameter
354 def ctcp_reply(string, options={})
355 @bot.ctcp_notice @source, @ctcp, string, options
358 # convenience method to reply "okay" in the current language to the
361 self.plainreply @bot.lang.get("okay")
364 # Like the above, but append the username
366 str = @bot.lang.get("okay").dup
368 # remove final punctuation
369 str.gsub!(/[!,.]$/,"")
370 str += ", #{@source}"
375 # the default okay style is the same as the default reply style
378 if @bot.config['core.reply_with_nick']
386 # class to manage IRC PRIVMSGs
387 class PrivMessage < UserMessage
388 def initialize(bot, server, source, target, message)
394 # class to manage IRC NOTICEs
395 class NoticeMessage < UserMessage
396 def initialize(bot, server, source, target, message)
402 # class to manage IRC KICKs
403 # +address?+ can be used as a shortcut to see if the bot was kicked,
404 # basically, +target+ was kicked from +channel+ by +source+ with +message+
405 class KickMessage < BasicUserMessage
406 # channel user was kicked from
409 def initialize(bot, server, source, target, channel, message="")
410 super(bot, server, source, target, message)
415 # class to pass IRC Nick changes in. @message contains the old nickame,
416 # @sourcenick contains the new one.
417 class NickMessage < BasicUserMessage
418 def initialize(bot, server, source, oldnick, newnick)
419 super(bot, server, source, oldnick, newnick)
431 class QuitMessage < BasicUserMessage
432 def initialize(bot, server, source, target, message="")
433 super(bot, server, source, target, message)
437 class TopicMessage < BasicUserMessage
440 # topic set at (unixtime)
441 attr_reader :timestamp
442 # topic set on channel
445 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
446 super(bot, server, source, channel, topic.text)
448 @timestamp = topic.set_on
453 # class to manage channel joins
454 class JoinMessage < BasicUserMessage
457 def initialize(bot, server, source, channel, message="")
458 super(bot, server, source, channel, message)
460 # in this case sourcenick is the nick that could be the bot
461 @address = (source == @bot.myself)
465 # class to manage channel parts
466 # same as a join, but can have a message too
467 class PartMessage < JoinMessage