Class: Cinch::Target

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/cinch/target.rb

Overview

Since:

  • 2.0.0

Direct Known Subclasses

Channel, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, bot) ⇒ Target

Returns a new instance of Target.

Since:

  • 2.0.0



12
13
14
15
# File 'lib/cinch/target.rb', line 12

def initialize(name, bot)
  @name = name
  @bot = bot
end

Instance Attribute Details

#botBot (readonly)

Returns:

Since:

  • 2.0.0



11
12
13
# File 'lib/cinch/target.rb', line 11

def bot
  @bot
end

#nameString (readonly)

Returns:

Since:

  • 2.0.0



9
10
11
# File 'lib/cinch/target.rb', line 9

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ -1, ...

Parameters:

Returns:

  • (-1, 0, 1, nil)

Since:

  • 2.0.0



159
160
161
162
163
164
165
166
167
168
# 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) ⇒ void

This method returns an undefined value.

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

Parameters:

  • text (#to_s)

    the message to send

See Also:

Since:

  • 2.0.0



115
116
117
118
# 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

#concretizeObject

Since:

  • 2.0.0



144
145
146
147
148
149
150
# 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) ⇒ void

This method returns an undefined value.

Send a CTCP to the target.

Parameters:

  • message (#to_s)

    the ctcp message

Since:

  • 2.0.0



140
141
142
# File 'lib/cinch/target.rb', line 140

def ctcp(message)
  send "\001#{message}\001"
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 2.0.0



153
154
155
# File 'lib/cinch/target.rb', line 153

def eql?(other)
  self == other
end

#notice(text) ⇒ void

This method returns an undefined value.

Sends a NOTICE to the target.

Parameters:

  • text (#to_s)

    the message to send

See Also:

Since:

  • 2.0.0



22
23
24
# File 'lib/cinch/target.rb', line 22

def notice(text)
  send(text, true)
end

#safe_action(text) ⇒ void

This method returns an undefined value.

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.

Parameters:

  • text (#to_s)

    the message to send

See Also:

Since:

  • 2.0.0



132
133
134
# File 'lib/cinch/target.rb', line 132

def safe_action(text)
  action(Cinch::Helpers.sanitize(text))
end

#safe_notice(text) ⇒ Object

Like #safe_msg but for notices.

See Also:

Since:

  • 2.0.0



106
107
108
# File 'lib/cinch/target.rb', line 106

def safe_notice(text)
  safe_send(text, true)
end

#safe_privmsg(*args) ⇒ Object

Deprecated.

Since:

  • 2.0.0



95
96
97
# File 'lib/cinch/target.rb', line 95

def safe_send(text, notice = false)
  send(Cinch::Helpers.sanitize(text), notice)
end

#safe_send(text, notice = false) ⇒ void Also known as: safe_msg

This method returns an undefined value.

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.

Parameters:

  • text (#to_s)

    the message to send

  • notice (Boolean) (defaults to: false)

    Use NOTICE instead of PRIVMSG?

See Also:

Since:

  • 2.0.0



80
81
82
# File 'lib/cinch/target.rb', line 80

def safe_send(text, notice = false)
  send(Cinch::Helpers.sanitize(text), notice)
end

#send(text, notice = false) ⇒ void Also known as: msg, privmsg

Note:

The aliases ‘msg` and `privmsg` are deprecated and will be removed in a future version.

This method returns an undefined value.

Sends a PRIVMSG to the target.

Parameters:

  • text (#to_s)

    the message to send

  • notice (Boolean) (defaults to: false)

    Use NOTICE instead of PRIVMSG?

See Also:

Since:

  • 2.0.0



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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