class Blur::Network

The Network module is to be percieved as an IRC network.

Although the connection is a part of the network module, it is mainly used for network-related structures, such as {User}, {Channel} and {Command}.

Attributes

channels[RW]

@return [Hash] the map of channels the client is in.

client[RW]

@return [Client] the client reference.

connection[RW]

@return [Network::Connection] the connection instance.

id[R]

Returns a unique identifier for this network.

You can override the id in your network configuration by setting an 'id' key with the id you want.. If no id is specified, the the id will be constructed from the hostname and port number in the format “<host>:<port>”

@return [String] the unique identifier for this network.

isupport[RW]

@return [Network::ISupport] the network isupport specs.

options[RW]

@return [Hash] the network options.

users[RW]

@return [Hash] the map of users that is known.

waiting_for_cap[R]

@return [Boolean] true if we're waiting for a capability negotiation.

Public Class Methods

new(options, client = nil) click to toggle source

Instantiates the network.

@param [Hash] options The network options. @option options [String] :hostname The hostname or IP-address we want to

connect to.

@option options [String] :nickname The nickname to use. @option options [optional, String] :username (Copies :nickname)

The username to use. This is also known as the ident.

@option options [optional, String] :realname (Copies :username)

The "real name" that we want to use. This is usually what shows up
as "Name" when you whois a user.

@option options [optional, String] :password The password for the network.

This is sometimes needed for private networks.

@option options [optional, Fixnum] :port (6697 if ssl, otherwise 6667)

The remote port we want to connect to.

@option options [optional, Boolean] :secure Set whether this is a secure

(SSL-encrypted) connection.

@option options [optional, String] :ssl_cert_file Local path of a

readable file that contains a X509 CA certificate to validate against.

@option options [optional, String] :ssl_fingerprint Validate that the

remote certificate matches the specified fingerprint.

@option options [optional, Boolean] :ssl_no_verify Disable verification

alltogether.
# File library/blur/network.rb, line 94
def initialize options, client = nil
  @client = client
  @options = options
  @users = {}
  @channels = {}
  @isupport = ISupport.new self

  unless options['nickname']
    raise ArgumentError, 'Network configuration for ' \
      "`#{id}' is missing a nickname"
  end

  @options['username'] ||= @options['nickname']
  @options['realname'] ||= @options['username']
  @options['channels'] ||= []
  @id = options.fetch 'id', "#{host}:#{port}"
end

Public Instance Methods

abort_cap_neg() click to toggle source

Called when the server doesn't support capability negotiation.

# File library/blur/network.rb, line 191
def abort_cap_neg
  @waiting_for_cap = false

  puts "Server does not support capability negotiation"
end
cap_end() click to toggle source

Called when we're done with capability negotiation.

# File library/blur/network.rb, line 198
def cap_end
  @waiting_for_cap = false

  transmit :CAP, 'END'
end
channel_by_name(name) click to toggle source

Find a channel by its name.

@param [String] name the channel name. @return [Network::Channel] the matching channel, or nil.

# File library/blur/network.rb, line 136
def channel_by_name name
  @channels.find { |channel| channel.name == name }
end
channel_flags() click to toggle source

Returns a list of channel flags (channel mode D).

@return [Array<String>] a list of channel flags.

# File library/blur/network.rb, line 166
def channel_flags
  isupport['CHANMODES']['D']
end
channels_with_user(nick) click to toggle source

Find all instances of channels in which there is a user with the nick nick.

@param [String] nick the nickname. @return [Array] a list of channels in which the user is located, or nil.

# File library/blur/network.rb, line 145
def channels_with_user nick
  @channels.select { |channel| channel.user_by_nick nick }
end
connect() click to toggle source

Attempt to establish a connection and send initial data.

@see Connection

# File library/blur/network.rb, line 173
def connect
  @connection = EventMachine.connect host, port, Connection, self
end
connected!() click to toggle source

Called when the connection was successfully established.

# File library/blur/network.rb, line 178
def connected!
  if sasl?
    @waiting_for_cap = true

    transmit :CAP, 'REQ', 'sasl'
  end

  transmit :PASS, @options['password'] if @options['password']
  transmit :NICK, @options['nickname']
  transmit :USER, @options['username'], 'void', 'void', @options['realname']
end
connected?() click to toggle source

Check whether or not connection is established.

# File library/blur/network.rb, line 39
def connected?
  @connection&.established?
end
disconnect() click to toggle source

Terminate the connection and clear all channels and users.

# File library/blur/network.rb, line 214
def disconnect
  @connection.close_connection_after_writing
end
disconnected!() click to toggle source

Called when the connection was closed.

# File library/blur/network.rb, line 205
def disconnected!
  @channels.each { |_name, channel| channel.users.clear }
  @channels.clear
  @users.clear

  @client.network_connection_closed self
end
got_message(message) click to toggle source

Forwards the received message to the client instance.

Called when the network connection has enough data to form a command.

# File library/blur/network.rb, line 123
def got_message message
  @client.got_message self, message
rescue StandardError => exception
  puts "#{exception.class}: #{exception.message}"
  puts
  puts '---'
  puts exception.backtrace
end
host() click to toggle source

Get the remote hostname.

@return [String] the remote hostname.

# File library/blur/network.rb, line 46
def host
  @options['hostname']
end
join(channel) click to toggle source

Join a channel.

# File library/blur/network.rb, line 240
def join channel
  transmit :JOIN, channel
end
port() click to toggle source

Get the remote port. If no port is specified, it returns 6697 if using a secure connection, returns 6667 otherwise.

@return [Fixnum] the remote port

# File library/blur/network.rb, line 55
def port
  @options['port'] ||= secure? ? 6697 : 6667
end
sasl?() click to toggle source

@return [Boolean] whether we want to authenticate with SASL.

# File library/blur/network.rb, line 65
def sasl?
  @options['sasl'] &&
    @options['sasl']['username'] &&
    @options['sasl']['password']
end
say(recipient, message) click to toggle source

Send a message to a recipient.

@param [String, to_s] recipient the recipient. @param [String] message the message.

# File library/blur/network.rb, line 116
def say recipient, message
  transmit :PRIVMSG, recipient.to_s, message
end
secure?() click to toggle source

Check to see if it's a secure connection.

# File library/blur/network.rb, line 60
def secure?
  @options['secure'] == true
end
send_privmsg(recipient, message) click to toggle source

Send a private message.

# File library/blur/network.rb, line 235
def send_privmsg recipient, message
  transmit :PRIVMSG, recipient, message
end
to_s() click to toggle source

Convert it to a debug-friendly format.

# File library/blur/network.rb, line 245
def to_s
  %(#<#{self.class.name} "#{host}":#{port}>)
end
transmit(name, *arguments) click to toggle source

Transmit a command to the server.

@param [Symbol, String] name the command name. @param […] arguments all the prepended parameters.

# File library/blur/network.rb, line 222
def transmit name, *arguments
  message = IRCParser::Message.new command: name.to_s, parameters: arguments

  if @client.verbose
    formatted_command = message.command.to_s.ljust 8, ' '
    formatted_params = message.parameters.map(&:inspect).join ' '
    log "#{'→' ^ :red} #{formatted_command} #{formatted_params}"
  end

  @connection.send_data "#{message}\r\n"
end
user_prefix_modes() click to toggle source

Returns a list of user modes that also gives a users nick a prefix.

@return [Array<String>] a list of user modes.

# File library/blur/network.rb, line 159
def user_prefix_modes
  isupport['PREFIX'].keys
end
user_prefixes() click to toggle source

Returns a list of user prefixes that a nick might contain.

@return [Array<String>] a list of user prefixes.

# File library/blur/network.rb, line 152
def user_prefixes
  isupport['PREFIX'].values
end