Class: Cinch::Target

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

Overview

Since:

Direct Known Subclasses

Channel, User

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Target) initialize(name, bot)

A new instance of Target

Since:

  • 2.0.0



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

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

Instance Attribute Details

- (Bot) bot (readonly)

Returns:

Since:

  • 2.0.0



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

def bot
  @bot
end

- (String) name (readonly)

Returns:

Since:

  • 2.0.0



7
8
9
# File 'lib/cinch/target.rb', line 7

def name
  @name
end

Instance Method Details

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

Parameters:

Returns:

  • (-1, 0, 1, nil)

Since:

  • 2.0.0



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/cinch/target.rb', line 139

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)
  else
    nil
  end
end

- action(text)

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



97
98
99
# File 'lib/cinch/target.rb', line 97

def action(text)
  @bot.irc.send("PRIVMSG #@name :\001ACTION #{text}\001")
end

- (Object) concretize

Since:

  • 2.0.0



124
125
126
127
128
129
130
# File 'lib/cinch/target.rb', line 124

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)

This method returns an undefined value.

Send a CTCP to the target.

Parameters:

  • message (#to_s)

    the ctcp message

Since:

  • 2.0.0



120
121
122
# File 'lib/cinch/target.rb', line 120

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

- (Boolean) eql?(other)

Returns:

  • (Boolean)

Since:

  • 2.0.0



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

def eql?(other)
  self == other
end

- msg(text, notice = false) Also known as: send, privmsg

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cinch/target.rb', line 30

def msg(text, notice = false)
  text = text.to_s
  split_start = @bot.config.message_split_start || ""
  split_end   = @bot.config.message_split_end   || ""
  command = notice ? "NOTICE" : "PRIVMSG"

  text.split(/\r\n|\r|\n/).each do |line|
    maxlength = 510 - (":" + " #{command} " + " :").size
    maxlength = maxlength - @bot.mask.to_s.length - @name.to_s.length
    maxlength_without_end = maxlength - split_end.bytesize

    if line.bytesize > maxlength
      splitted = []

      while line.bytesize > maxlength_without_end
        pos = line.rindex(/\s/, maxlength_without_end)
        r = pos || maxlength_without_end
        splitted << line.slice!(0, r) + split_end.tr(" ", "\u00A0")
        line = split_start.tr(" ", "\u00A0") + line.lstrip
      end

      splitted << line
      splitted[0, (@bot.config.max_messages || splitted.size)].each do |string|
        string.tr!("\u00A0", " ") # clean string from any non-breaking spaces
        @bot.irc.send("#{command} #@name :#{string}")
      end
    else
      @bot.irc.send("#{command} #@name :#{line}")
    end
  end
end

- notice(text)

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



20
21
22
# File 'lib/cinch/target.rb', line 20

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

- safe_action(text)

TODO:

Handle mIRC color codes more gracefully.

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.

Parameters:

  • text (#to_s)

    the message to send

See Also:

Since:

  • 2.0.0



112
113
114
# File 'lib/cinch/target.rb', line 112

def safe_action(text)
  action(Cinch::Utilities::String.filter_string(text))
end

- safe_msg(text, notice = false) Also known as: safe_privmsg, safe_send

TODO:

Handle mIRC color codes more gracefully.

This method returns an undefined value.

Like #msg, 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.

Parameters:

  • text (#to_s)

    the message to send

  • notice (Boolean) (defaults to: false)

    Use NOTICE instead of PRIVMSG?

See Also:

Since:

  • 2.0.0



75
76
77
# File 'lib/cinch/target.rb', line 75

def safe_msg(text, notice = false)
  msg(Cinch::Utilities::String.filter_string(text), notice)
end

- safe_notice(text)

TODO:

Handle mIRC color codes more gracefully.

This method returns an undefined value.

Like #safe_msg but for notices.

Parameters:

  • text (#to_s)

    the message to send

  • notice (Boolean)

    Use NOTICE instead of PRIVMSG?

See Also:

Since:

  • 2.0.0



88
89
90
# File 'lib/cinch/target.rb', line 88

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