Class: Syndi::IRC::Object::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/syndi/irc/object/entity.rb

Overview

A superclass which represents an IRC 'entity'; i.e., a channel or user. It acts as a base for User and Channel.

Author:

  • noxgirl

Since:

  • 4.0.0

Direct Known Subclasses

User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(irc, type, name) ⇒ Entity

New instance.

Parameters:

  • irc (Syndi::IRC::Server)

    The server.

  • type (Symbol)

    Either +:channel+ or +:user+.

  • name (String)

    The name of this entity (nickname or channel name).

Since:

  • 4.0.0



31
32
33
34
35
# File 'lib/syndi/irc/object/entity.rb', line 31

def initialize(irc, type, name)
  @irc         = irc
  @entity_type = type
  @name        = name
end

Instance Attribute Details

#ircSyndi::IRC::Server (readonly)

Returns The IRC server on which this entity exists.

Returns:

Since:

  • 4.0.0



22
23
24
25
26
27
28
29
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/syndi/irc/object/entity.rb', line 22

class Entity

  attr_reader :irc, :name

  # New instance.
  #
  # @param [Syndi::IRC::Server] irc The server.
  # @param [Symbol] type Either +:channel+ or +:user+.
  # @param [String] name The name of this entity (nickname or channel name).
  def initialize(irc, type, name)
    @irc         = irc
    @entity_type = type
    @name        = name
  end

  # @return [true, false] Whether we're a channel.
  def channel?
    @entity_type == :channel ? true : false
  end
  
  # @return [true, false] Whether we're a user.
  def user?
    @entity_type == :user ? true : false
  end

  # Send a message to this entity. This will additionally divide messages
  # which are too long into multiple messages.
  #
  # This will call irc:onMsg with +self+ and +message+.
  #
  # @param [String] message The message.
  # @param [true, false] notice Whether this should be a /notice as opposed
  #   to a /msg.
  def msg(message, notice=false)
    
    command = (notice == false ? 'PRIVMSG' : 'NOTICE')
    len     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :".length
    raw     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{message}\r\n"

    if raw.length > 512
      
      msgs     = []
      nmessage = message 
      until raw.length <= 512
        msgs << nmessage.slice!(0, 512-len)
        raw = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{nmessage}\r\n"
      end
      
      msgs.each { |m| @irc.snd "#{command} #@name :#{m}" }
    
    else
      @irc.snd "#{command} #@name :#{message}"
    end

    $m.events.call 'irc:onMsg', self, message

  end # def msg

  def to_s; @name; end

end

#nameString (readonly)

Returns The name of the entity.

Returns:

  • (String)

    The name of the entity.

Since:

  • 4.0.0



22
23
24
25
26
27
28
29
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/syndi/irc/object/entity.rb', line 22

class Entity

  attr_reader :irc, :name

  # New instance.
  #
  # @param [Syndi::IRC::Server] irc The server.
  # @param [Symbol] type Either +:channel+ or +:user+.
  # @param [String] name The name of this entity (nickname or channel name).
  def initialize(irc, type, name)
    @irc         = irc
    @entity_type = type
    @name        = name
  end

  # @return [true, false] Whether we're a channel.
  def channel?
    @entity_type == :channel ? true : false
  end
  
  # @return [true, false] Whether we're a user.
  def user?
    @entity_type == :user ? true : false
  end

  # Send a message to this entity. This will additionally divide messages
  # which are too long into multiple messages.
  #
  # This will call irc:onMsg with +self+ and +message+.
  #
  # @param [String] message The message.
  # @param [true, false] notice Whether this should be a /notice as opposed
  #   to a /msg.
  def msg(message, notice=false)
    
    command = (notice == false ? 'PRIVMSG' : 'NOTICE')
    len     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :".length
    raw     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{message}\r\n"

    if raw.length > 512
      
      msgs     = []
      nmessage = message 
      until raw.length <= 512
        msgs << nmessage.slice!(0, 512-len)
        raw = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{nmessage}\r\n"
      end
      
      msgs.each { |m| @irc.snd "#{command} #@name :#{m}" }
    
    else
      @irc.snd "#{command} #@name :#{message}"
    end

    $m.events.call 'irc:onMsg', self, message

  end # def msg

  def to_s; @name; end

end

Instance Method Details

#channel?true, false

Returns Whether we're a channel.

Returns:

  • (true, false)

    Whether we're a channel.

Since:

  • 4.0.0



38
39
40
# File 'lib/syndi/irc/object/entity.rb', line 38

def channel?
  @entity_type == :channel ? true : false
end

#msg(message, notice = false) ⇒ Object

Send a message to this entity. This will additionally divide messages which are too long into multiple messages.

This will call irc:onMsg with +self+ and +message+.

Parameters:

  • message (String)

    The message.

  • notice (true, false) (defaults to: false)

    Whether this should be a /notice as opposed to a /msg.

Since:

  • 4.0.0



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/syndi/irc/object/entity.rb', line 55

def msg(message, notice=false)
  
  command = (notice == false ? 'PRIVMSG' : 'NOTICE')
  len     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :".length
  raw     = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{message}\r\n"

  if raw.length > 512
    
    msgs     = []
    nmessage = message 
    until raw.length <= 512
      msgs << nmessage.slice!(0, 512-len)
      raw = ":#{@irc.nick}!#{@irc.user}@#{@irc.mask} #{command} #@name :#{nmessage}\r\n"
    end
    
    msgs.each { |m| @irc.snd "#{command} #@name :#{m}" }
  
  else
    @irc.snd "#{command} #@name :#{message}"
  end

  $m.events.call 'irc:onMsg', self, message

end

#to_sObject

def msg

Since:

  • 4.0.0



80
# File 'lib/syndi/irc/object/entity.rb', line 80

def to_s; @name; end

#user?true, false

Returns Whether we're a user.

Returns:

  • (true, false)

    Whether we're a user.

Since:

  • 4.0.0



43
44
45
# File 'lib/syndi/irc/object/entity.rb', line 43

def user?
  @entity_type == :user ? true : false
end