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, #prefixed?, #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



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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/rbot/message.rb', line 333

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
      @prefixed = 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
    @plainmessage = BasicUserMessage.strip_formatting(@message)
    @message = BasicUserMessage.strip_initial_formatting(@message)
  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)



326
327
328
# File 'lib/rbot/message.rb', line 326

def action
  @action
end

#channelObject (readonly)

channel the message was in, nil for privately addressed messages



316
317
318
# File 'lib/rbot/message.rb', line 316

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.



322
323
324
# File 'lib/rbot/message.rb', line 322

def ctcp
  @ctcp
end

#paramsObject (readonly)

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



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

def params
  @params
end

#pluginObject (readonly)

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



304
305
306
# File 'lib/rbot/message.rb', line 304

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



313
314
315
# File 'lib/rbot/message.rb', line 313

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.



466
467
468
469
# File 'lib/rbot/message.rb', line 466

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

#action?Boolean

Returns:

  • (Boolean)


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

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



473
474
475
# File 'lib/rbot/message.rb', line 473

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

#inspectObject



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/rbot/message.rb', line 285

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



484
485
486
487
488
489
490
491
492
# File 'lib/rbot/message.rb', line 484

def nickokay
  str = @bot.lang.get("okay").dup
  if self.public?
    # remove final punctuation
    str.gsub!(/[!,.]$/,"")
    str += ", #{@source}"
  end
  self.reply str, :nick => false
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



414
415
416
# File 'lib/rbot/message.rb', line 414

def nickreply(string, options={})
  reply string, {:nick => true}.merge(options)
end

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

Same as nickreply, but always prepend the target’s nick.



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

def nickreply!(string, options={})
  reply string, {:nick => true, :forcenick => true}.merge(options)
end

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

send a NOTICE to the message source



502
503
504
# File 'lib/rbot/message.rb', line 502

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

#okayObject

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



496
497
498
# File 'lib/rbot/message.rb', line 496

def okay
  @bot.config['core.reply_with_nick'] ? nickokay : plainokay
end

#plainokayObject

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



479
480
481
# File 'lib/rbot/message.rb', line 479

def plainokay
  self.reply @bot.lang.get("okay"), :nick => false
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.



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

def plainreply(string, options={})
  reply string, {:nick => false}.merge(options)
end

#private?Boolean

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

Returns:

  • (Boolean)


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

def private?
  return @private
end

#public?Boolean

returns true if the message was in a channel

Returns:

  • (Boolean)


395
396
397
# File 'lib/rbot/message.rb', line 395

def public?
  return !@private
end

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

The general way to reply to a command. The following options are available: :nick [false, true, :auto]

state if the nick of the user calling the command should be prepended
:auto uses core.reply_with_nick

:forcenick [false, true]

if :nick is true, always prepend the target's nick, even if the nick
already appears in the reply. Defaults to false.

:to [:private, :public, :auto]

where should the bot reply?
:private always reply to the nick
:public reply to the channel (if available)
:auto uses core.private_replies


437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'lib/rbot/message.rb', line 437

def reply(string, options={})
  opts = {:nick => :auto, :forcenick => false, :to => :auto}.merge options

  if opts[:nick] == :auto
    opts[:nick] = @bot.config['core.reply_with_nick']
  end

  if !self.public?
    opts[:to] = :private
  elsif opts[:to] == :auto
    opts[:to] = @bot.config['core.private_replies'] ? :private : :public
  end

  if (opts[:nick] &&
      opts[:to] != :private &&
      (string !~ /(?:^|\W)#{Regexp.escape(@source.to_s)}(?:$|\W)/ ||
        opts[:forcenick]))
    string = "#{@source}#{@bot.config['core.nick_postfix']} #{string}"
  end
  to = (opts[:to] == :private) ? source : @channel
  @bot.say to, string, options
  @replied = true
end