Class: Irc::UserMessage

Inherits:
BasicUserMessage show all
Defined in:
lib/rbot/message.rb

Overview

class for handling IRC user messages. Includes some utilities for handling the message, for example in plugins. The message member will have any bot addressing “^bot: ” removed (address? will return true in this case)

Direct Known Subclasses

NoticeMessage, PrivMessage

Instance Attribute Summary collapse

Attributes inherited from BasicUserMessage

#bot, #ignored, #in_thread, #logmessage, #message, #plainmessage, #replied, #server, #source, #target, #time

Instance Method Summary collapse

Methods inherited from BasicUserMessage

#address?, #botuser, #identified?, #parse_channel_list, #recurse_depth, #recurse_depth=, #sourceaddress, #sourcenick, strip_formatting, strip_initial_formatting, stripcolour

Constructor Details

#initialize(bot, server, source, target, message) ⇒ UserMessage

instantiate a new UserMessage

bot

associated bot class

source

hostmask of the message source

target

nick/channel message is destined for

message

message part



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/rbot/message.rb', line 318

def initialize(bot, server, source, target, message)
  super(bot, server, source, target, message)
  @target = target
  @private = false
  @plugin = nil
  @ctcp = false
  @action = false

  if target == @bot.myself
    @private = true
    @address = true
    @channel = nil
    @replyto = source
  else
    @replyto = @target
    @channel = @target
  end

  # check for option extra addressing prefixes, e.g "|search foo", or
  # "!version" - first match wins
  bot.config['core.address_prefix'].each {|mprefix|
    if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
      @address = true
      break
    end
  }

  # even if they used above prefixes, we allow for silly people who
  # combine all possible types, e.g. "|rbot: hello", or
  # "/msg rbot rbot: hello", etc
  if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
    @address = true
  end

  if(@message =~ /^\001(\S+)(\s(.+))?\001/)
    @ctcp = $1
	# FIXME need to support quoting of NULL and CR/LF, see
	# http://www.irchelp.org/irchelp/rfc/ctcpspec.html
    @message = $3 || String.new
    @action = @ctcp == 'ACTION'
    debug "Received CTCP command #{@ctcp} with options #{@message} (action? #{@action})"
    @logmessage = @message.dup
  end

  # free splitting for plugins
  @params = @message.dup
  # Created messges (such as by fake_message) can contain multiple lines
  if @params.gsub!(/\A\s*(\S+)[\s$]*/m, "")
    @plugin = $1.downcase
    @params = nil unless @params.length > 0
  end
end

Instance Attribute Details

#actionObject (readonly)

for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff will be stripped from the message)



311
312
313
# File 'lib/rbot/message.rb', line 311

def action
  @action
end

#channelObject (readonly)

channel the message was in, nil for privately addressed messages



301
302
303
# File 'lib/rbot/message.rb', line 301

def channel
  @channel
end

#ctcpObject (readonly)

for PRIVMSGs, false unless the message was a CTCP command, in which case it evaluates to the CTCP command itself (TIME, PING, VERSION, etc). The CTCP command parameters are then stored in the message.



307
308
309
# File 'lib/rbot/message.rb', line 307

def ctcp
  @ctcp
end

#paramsObject (readonly)

for plugin messages, the rest of the message, with the plugin name removed



293
294
295
# File 'lib/rbot/message.rb', line 293

def params
  @params
end

#pluginObject (readonly)

for plugin messages, the name of the plugin invoked by the message



289
290
291
# File 'lib/rbot/message.rb', line 289

def plugin
  @plugin
end

#replytoObject (readonly)

convenience member. Who to reply to (i.e. would be sourcenick for a privately addressed message, or target (the channel) for a publicly addressed message



298
299
300
# File 'lib/rbot/message.rb', line 298

def replyto
  @replyto
end

Instance Method Details

#act(string, options = {}) ⇒ Object

convenience method to reply to a message with an action. It’s the same as doing: @bot.action m.replyto, string So if the message is private, it will reply to the user. If it was in a channel, it will reply in the channel.



418
419
420
421
# File 'lib/rbot/message.rb', line 418

def act(string, options={})
  @bot.action @replyto, string, options
  @replied = true
end

#action?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'lib/rbot/message.rb', line 381

def action?
  return @action
end

#ctcp_reply(string, options = {}) ⇒ Object

send a CTCP response, i.e. a private NOTICE to the sender with the same CTCP command and the reply as a parameter



425
426
427
# File 'lib/rbot/message.rb', line 425

def ctcp_reply(string, options={})
  @bot.ctcp_notice @source, @ctcp, string, options
end

#inspectObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/rbot/message.rb', line 270

def inspect
  fields = ' plugin=' << plugin.inspect
  fields << ' params=' << params.inspect
  fields << ' channel=' << channel.to_s if channel
  fields << ' (reply to ' << replyto.to_s << ')'
  if self.private?
    fields << ' (private)'
  else
    fields << ' (public)'
  end
  if self.action?
    fields << ' (action)'
  elsif ctcp
    fields << ' (CTCP ' << ctcp << ')'
  end
  super(fields)
end

#nickokayObject

Like the above, but append the username



436
437
438
439
440
441
442
443
444
# File 'lib/rbot/message.rb', line 436

def nickokay
  str = @bot.lang.get("okay").dup
  if self.public?
    # remove final punctuation
    str.gsub!(/[!,.]$/,"")
    str += ", #{@source}"
  end
  self.plainreply str
end

#nickreply(string, options = {}) ⇒ Object

Same as reply, but when replying in public it adds the nick of the user the bot is replying to



397
398
399
400
401
# File 'lib/rbot/message.rb', line 397

def nickreply(string, options={})
  extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
  @bot.say @replyto, extra + string, options
  @replied = true
end

#notify(msg, opts = {}) ⇒ Object

send a NOTICE to the message source



457
458
459
# File 'lib/rbot/message.rb', line 457

def notify(msg,opts={})
  @bot.notice(sourcenick, msg, opts)
end

#okayObject

the default okay style is the same as the default reply style



448
449
450
451
452
453
# File 'lib/rbot/message.rb', line 448

def okay
  if @bot.config['core.reply_with_nick']
    return nickokay
  end
  plainokay
end

#plainokayObject

convenience method to reply “okay” in the current language to the message



431
432
433
# File 'lib/rbot/message.rb', line 431

def plainokay
  self.plainreply @bot.lang.get("okay")
end

#plainreply(string, options = {}) ⇒ Object

convenience method to reply to a message, useful in plugins. It’s the same as doing: @bot.say m.replyto, string So if the message is private, it will reply to the user. If it was in a channel, it will reply in the channel.



390
391
392
393
# File 'lib/rbot/message.rb', line 390

def plainreply(string, options={})
  @bot.say @replyto, string, options
  @replied = true
end

#private?Boolean

returns true for private messages, e.g. “/msg bot hello”

Returns:

  • (Boolean)


372
373
374
# File 'lib/rbot/message.rb', line 372

def private?
  return @private
end

#public?Boolean

returns true if the message was in a channel

Returns:

  • (Boolean)


377
378
379
# File 'lib/rbot/message.rb', line 377

def public?
  return !@private
end

#reply(string, options = {}) ⇒ Object

the default reply style is to nickreply unless the reply already contains the nick or core.reply_with_nick is set to false



406
407
408
409
410
411
# File 'lib/rbot/message.rb', line 406

def reply(string, options={})
  if @bot.config['core.reply_with_nick'] and not string =~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/
    return nickreply(string, options)
  end
  plainreply(string, options)
end