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
@return [Hash] the map of channels the client is in.
@return [Client] the client reference.
@return [Network::Connection] the connection instance.
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.
@return [Network::ISupport] the network isupport specs.
@return [Hash] the network options.
@return [Hash] the map of users that is known.
@return [Boolean] true if we're waiting for a capability negotiation.
Public Class Methods
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
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
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
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
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
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
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
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
Check whether or not connection is established.
# File library/blur/network.rb, line 39 def connected? @connection&.established? end
Terminate the connection and clear all channels and users.
# File library/blur/network.rb, line 214 def disconnect @connection.close_connection_after_writing end
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
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
Get the remote hostname.
@return [String] the remote hostname.
# File library/blur/network.rb, line 46 def host @options['hostname'] end
Join a channel.
# File library/blur/network.rb, line 240 def join channel transmit :JOIN, channel end
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
@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
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
Check to see if it's a secure connection.
# File library/blur/network.rb, line 60 def secure? @options['secure'] == true end
Send a private message.
# File library/blur/network.rb, line 235 def send_privmsg recipient, message transmit :PRIVMSG, recipient, message end
Convert it to a debug-friendly format.
# File library/blur/network.rb, line 245 def to_s %(#<#{self.class.name} "#{host}":#{port}>) end
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
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
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