class Cinch::Target

@since 2.0.0

Attributes

bot[R]

@return [Bot]

name[R]

@return [String]

Public Class Methods

new(name, bot) click to toggle source
# File lib/cinch/target.rb, line 12
def initialize(name, bot)
  @name = name
  @bot  = bot
end

Public Instance Methods

<=>(other) click to toggle source

@param [Target, String] other @return [-1, 0, 1, nil]

# File lib/cinch/target.rb, line 159
def <=>(other)
  casemapping = @bot.irc.isupport["CASEMAPPING"]
  left = @name.irc_downcase(casemapping)

  if other.is_a?(Target)
    left <=> other.name.irc_downcase(casemapping)
  elsif other.is_a?(String)
    left <=> other.irc_downcase(casemapping)
  end
end
action(text) click to toggle source

Invoke an action (/me) in/to the target.

@param [#to_s] text the message to send @return [void] @see safe_action

# File lib/cinch/target.rb, line 115
def action(text)
  line = text.to_s.each_line.first.chomp
  @bot.irc.send("PRIVMSG #{@name} :\001ACTION #{line}\001")
end
concretize() click to toggle source
# File lib/cinch/target.rb, line 144
def concretize
  if @bot.isupport["CHANTYPES"].include?(@name[0])
    @bot.channel_list.find_ensured(@name)
  else
    @bot.user_list.find_ensured(@name)
  end
end
ctcp(message) click to toggle source

Send a CTCP to the target.

@param [#to_s] message the ctcp message @return [void]

# File lib/cinch/target.rb, line 140
def ctcp(message)
  send "\001#{message}\001"
end
eql?(other) click to toggle source

@return [Boolean]

# File lib/cinch/target.rb, line 153
def eql?(other)
  self == other
end
msg(text, notice = false)
Alias for: send
notice(text) click to toggle source

Sends a NOTICE to the target.

@param [#to_s] text the message to send @return [void] @see safe_notice

# File lib/cinch/target.rb, line 22
def notice(text)
  send(text, true)
end
privmsg(text, notice = false)
Alias for: send
safe_action(text) click to toggle source

Like {#action}, but remove any non-printable characters from `text`. The purpose of this method is to send text from untrusted sources, like other users or feeds.

Note: this will break any mIRC color codes embedded in the string. For more fine-grained control, use {Helpers#Sanitize} and {Formatting.unformat} directly.

@param (see action) @return (see action) @see action

# File lib/cinch/target.rb, line 132
def safe_action(text)
  action(Cinch::Helpers.sanitize(text))
end
safe_msg(text, notice = false)
Also aliased as: safe_privmsg
Alias for: safe_send
safe_notice(text) click to toggle source

Like {#safe_msg} but for notices.

@return (see safe_msg) @param (see safe_msg) @see safe_notice @see notice

# File lib/cinch/target.rb, line 106
def safe_notice(text)
  safe_send(text, true)
end
safe_privmsg(text, notice = false)
Alias for: safe_msg
safe_send(text, notice = false) click to toggle source

Like {#send}, but remove any non-printable characters from `text`. The purpose of this method is to send text of untrusted sources, like other users or feeds.

Note: this will break any mIRC color codes embedded in the string. For more fine-grained control, use {Helpers#Sanitize} and {Formatting.unformat} directly.

@return (see send) @param (see send) @see send

# File lib/cinch/target.rb, line 80
def safe_send(text, notice = false)
  send(Cinch::Helpers.sanitize(text), notice)
end
Also aliased as: safe_msg
send(text, notice = false) click to toggle source

Sends a PRIVMSG to the target.

@param [#to_s] text the message to send @param [Boolean] notice Use NOTICE instead of PRIVMSG? @return [void] @see safe_msg @note The aliases `msg` and `privmsg` are deprecated and will be

removed in a future version.
# File lib/cinch/target.rb, line 34
def send(text, notice = false)
  # TODO: deprecate `notice` argument, put splitting into own
  # method
  text = text.to_s
  split_start = @bot.config.message_split_start || ""
  split_end   = @bot.config.message_split_end   || ""
  command = notice ? "NOTICE" : "PRIVMSG"
  prefix = ":#{@bot.mask} #{command} #{@name} :"

  text.lines.map(&:chomp).each do |line|
    splitted = split_message(line, prefix, split_start, split_end)

    splitted[0, (@bot.config.max_messages || splitted.size)].each do |string|
      @bot.irc.send("#{command} #{@name} :#{string}")
    end
  end
end
Also aliased as: msg, privmsg

Private Instance Methods

split_message(msg, prefix, split_start, split_end) click to toggle source
# File lib/cinch/target.rb, line 172
def split_message(msg, prefix, split_start, split_end)
  max_bytesize = 510 - prefix.bytesize
  max_bytesize_without_end = max_bytesize - split_end.bytesize

  return [msg] if msg.bytesize <= max_bytesize

  splitted = []
  while msg.bytesize > max_bytesize_without_end
    acc = 0
    acc_rune_sizes = msg.each_char.map do |ch|
      acc += ch.bytesize
    end

    max_rune = acc_rune_sizes.rindex { |bs| bs <= max_bytesize_without_end } || 0
    r = [msg.rindex(/\s/, max_rune) || (max_rune + 1), 1].max

    splitted << (msg[0...r] + split_end)
    msg = split_start.tr(" ", "\cz") + msg[r..-1].lstrip
  end
  splitted << msg

  # clean string from any substitute characters
  splitted.map { |string| string.tr("\cz", " ") }
end