4 # :title: IRC message datastructures
7 BotConfig.register BotConfigArrayValue.new('core.address_prefix',
8 :default => [], :wizard => true,
9 :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')"
12 BotConfig.register BotConfigBooleanValue.new('core.reply_with_nick',
13 :default => false, :wizard => true,
14 :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!')"
17 BotConfig.register BotConfigStringValue.new('core.nick_postfix',
18 :default => ':', :wizard => true,
19 :desc => "when replying with nick put this character after the nick of the user the bot is replying to"
22 # Define standard IRC attriubtes (not so standard actually,
23 # but the closest thing we have ...)
30 # Color is prefixed by \003 and followed by optional
31 # foreground and background specifications, two-digits-max
32 # numbers separated by a comma. One of the two parts
35 ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
37 # Standard color codes
64 # Convert a String or Symbol into a color number
65 def Irc.find_color(data)
69 f = if String === data
82 # Insert the full color code for a given
83 # foreground/background combination.
84 def Irc.color(fg=nil,bg=nil)
87 str << Irc.find_color(fg).to_s
90 str << "," << Irc.find_color(bg).to_s
95 # base user message class, all user messages derive from this
96 # (a user message is defined as having a source hostmask, a target
97 # nick/channel and a message part)
98 class BasicUserMessage
106 # when the message was received
109 # User that originated the message
112 # User/Channel message was sent to
115 # contents of the message
116 attr_accessor :message
118 # has the message been replied to/handled by a plugin?
119 attr_accessor :replied
121 # instantiate a new Message
122 # bot:: associated bot class
123 # server:: Server where the message took place
124 # source:: User that sent the message
125 # target:: User/Channel is destined for
126 # message:: actual message
127 def initialize(bot, server, source, target, message)
128 @msg_wants_id = false unless defined? @msg_wants_id
135 @message = BasicUserMessage.stripcolour message
140 if @msg_wants_id && @server.capabilities[:"identify-msg"]
141 if @message =~ /^([-+])(.*)/
142 @identified = ($1=="+")
145 warning "Message does not have identification"
149 if target && target == @bot.myself
155 # Access the nick of the source
158 @source.nick rescue @source.to_s
161 # Access the user@host of the source
164 "#{@source.user}@#{@source.host}" rescue @source.to_s
167 # Was the message from an identified user?
172 # returns true if the message was addressed to the bot.
173 # This includes any private message to the bot, or any public message
174 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
175 # a kick message when bot was kicked etc.
180 # has this message been replied to by a plugin?
185 # strip mIRC colour escapes from a string
186 def BasicUserMessage.stripcolour(string)
187 return "" unless string
188 ret = string.gsub(ColorRx, "")
189 #ret.tr!("\x00-\x1f", "")
195 # class for handling IRC user messages. Includes some utilities for handling
196 # the message, for example in plugins.
197 # The +message+ member will have any bot addressing "^bot: " removed
198 # (address? will return true in this case)
199 class UserMessage < BasicUserMessage
201 # for plugin messages, the name of the plugin invoked by the message
204 # for plugin messages, the rest of the message, with the plugin name
208 # convenience member. Who to reply to (i.e. would be sourcenick for a
209 # privately addressed message, or target (the channel) for a publicly
213 # channel the message was in, nil for privately addressed messages
216 # for PRIVMSGs, false unless the message was a CTCP command,
217 # in which case it evaluates to the CTCP command itself
218 # (TIME, PING, VERSION, etc). The CTCP command parameters
219 # are then stored in the message.
222 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
223 # will be stripped from the message)
226 # instantiate a new UserMessage
227 # bot:: associated bot class
228 # source:: hostmask of the message source
229 # target:: nick/channel message is destined for
230 # message:: message part
231 def initialize(bot, server, source, target, message)
232 super(bot, server, source, target, message)
239 if target == @bot.myself
249 # check for option extra addressing prefixes, e.g "|search foo", or
250 # "!version" - first match wins
251 bot.config['core.address_prefix'].each {|mprefix|
252 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
258 # even if they used above prefixes, we allow for silly people who
259 # combine all possible types, e.g. "|rbot: hello", or
260 # "/msg rbot rbot: hello", etc
261 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
265 if(@message =~ /^\001(\S+)(\s(.+))?\001/)
267 # FIXME need to support quoting of NULL and CR/LF, see
268 # http://www.irchelp.org/irchelp/rfc/ctcpspec.html
269 @message = $3 || String.new
270 @action = @ctcp == 'ACTION'
271 debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
274 # free splitting for plugins
275 @params = @message.dup
276 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
277 @plugin = $1.downcase
278 @params = nil unless @params.length > 0
282 # returns true for private messages, e.g. "/msg bot hello"
287 # returns true if the message was in a channel
296 # convenience method to reply to a message, useful in plugins. It's the
298 # <tt>@bot.say m.replyto, string</tt>
299 # So if the message is private, it will reply to the user. If it was
300 # in a channel, it will reply in the channel.
301 def plainreply(string, options={})
302 @bot.say @replyto, string, options
306 # Same as reply, but when replying in public it adds the nick of the user
307 # the bot is replying to
308 def nickreply(string, options={})
309 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
310 @bot.say @replyto, extra + string, options
314 # the default reply style is to nickreply unless the reply already contains
315 # the nick or core.reply_with_nick is set to false
317 def reply(string, options={})
318 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
319 return nickreply(string, options)
321 plainreply(string, options)
324 # convenience method to reply to a message with an action. It's the
326 # <tt>@bot.action m.replyto, string</tt>
327 # So if the message is private, it will reply to the user. If it was
328 # in a channel, it will reply in the channel.
329 def act(string, options={})
330 @bot.action @replyto, string, options
334 # send a CTCP response, i.e. a private NOTICE to the sender
335 # with the same CTCP command and the reply as a parameter
336 def ctcp_reply(string, options={})
337 @bot.ctcp_notice @source, @ctcp, string, options
340 # convenience method to reply "okay" in the current language to the
343 self.plainreply @bot.lang.get("okay")
346 # Like the above, but append the username
348 str = @bot.lang.get("okay").dup
350 # remove final punctuation
351 str.gsub!(/[!,.]$/,"")
352 str += ", #{@source}"
357 # the default okay style is the same as the default reply style
360 if @bot.config['core.reply_with_nick']
368 # class to manage IRC PRIVMSGs
369 class PrivMessage < UserMessage
370 def initialize(bot, server, source, target, message)
376 # class to manage IRC NOTICEs
377 class NoticeMessage < UserMessage
378 def initialize(bot, server, source, target, message)
384 # class to manage IRC KICKs
385 # +address?+ can be used as a shortcut to see if the bot was kicked,
386 # basically, +target+ was kicked from +channel+ by +source+ with +message+
387 class KickMessage < BasicUserMessage
388 # channel user was kicked from
391 def initialize(bot, server, source, target, channel, message="")
392 super(bot, server, source, target, message)
397 # class to pass IRC Nick changes in. @message contains the old nickame,
398 # @sourcenick contains the new one.
399 class NickMessage < BasicUserMessage
400 def initialize(bot, server, source, oldnick, newnick)
401 super(bot, server, source, oldnick, newnick)
413 class QuitMessage < BasicUserMessage
414 def initialize(bot, server, source, target, message="")
415 super(bot, server, source, target, message)
419 class TopicMessage < BasicUserMessage
422 # topic set at (unixtime)
423 attr_reader :timestamp
424 # topic set on channel
427 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
428 super(bot, server, source, channel, topic.text)
430 @timestamp = topic.set_on
435 # class to manage channel joins
436 class JoinMessage < BasicUserMessage
439 def initialize(bot, server, source, channel, message="")
440 super(bot, server, source, channel, message)
442 # in this case sourcenick is the nick that could be the bot
443 @address = (source == @bot.myself)
447 # class to manage channel parts
448 # same as a join, but can have a message too
449 class PartMessage < JoinMessage