Class: Blur::Network

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
library/blur/network.rb,
library/blur/network/isupport.rb,
library/blur/network/connection.rb

Overview

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.

Defined Under Namespace

Classes: Connection, ConnectionError, ISupport

Constant Summary

Constants included from Logging

Logging::Levels

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(options, client = nil) ⇒ Network

Instantiates the network.

Parameters:

  • options (Hash)

    The network options.

Options Hash (options):

  • :hostname (String)

    The hostname or IP-address we want to connect to.

  • :nickname (String)

    The nickname to use.

  • :username (optional, String) — default: Copies :nickname

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

  • :realname (optional, String) — default: Copies :username

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

  • :password (optional, String)

    The password for the network. This is sometimes needed for private networks.

  • :port (optional, Fixnum) — default: 6697 if ssl, otherwise 6667

    The remote port we want to connect to.

  • :secure (optional, Boolean)

    Set whether this is a secure (SSL-encrypted) connection.

  • :ssl_cert_file (optional, String)

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

  • :ssl_fingerprint (optional, String)

    Validate that the remote certificate matches the specified fingerprint.

  • :ssl_no_verify (optional, Boolean)

    Disable verification alltogether.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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

Instance Attribute Details

#channelsHash

Returns the map of channels the client is in.

Returns:

  • (Hash)

    the map of channels the client is in.



28
29
30
# File 'library/blur/network.rb', line 28

def channels
  @channels
end

#clientClient

Returns the client reference.

Returns:

  • (Client)

    the client reference.



30
31
32
# File 'library/blur/network.rb', line 30

def client
  @client
end

#connectionNetwork::Connection

Returns the connection instance.

Returns:



32
33
34
# File 'library/blur/network.rb', line 32

def connection
  @connection
end

#idString (readonly)

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>”

Returns:

  • (String)

    the unique identifier for this network.



22
23
24
# File 'library/blur/network.rb', line 22

def id
  @id
end

#isupportNetwork::ISupport

Returns the network isupport specs.

Returns:



34
35
36
# File 'library/blur/network.rb', line 34

def isupport
  @isupport
end

#optionsHash

Returns the network options.

Returns:

  • (Hash)

    the network options.



24
25
26
# File 'library/blur/network.rb', line 24

def options
  @options
end

#usersHash

Returns the map of users that is known.

Returns:

  • (Hash)

    the map of users that is known.



26
27
28
# File 'library/blur/network.rb', line 26

def users
  @users
end

#waiting_for_capBoolean (readonly)

Returns true if we’re waiting for a capability negotiation.

Returns:

  • (Boolean)

    true if we’re waiting for a capability negotiation.



36
37
38
# File 'library/blur/network.rb', line 36

def waiting_for_cap
  @waiting_for_cap
end

Instance Method Details

#abort_cap_negObject

Called when the server doesn’t support capability negotiation.



191
192
193
194
195
# File 'library/blur/network.rb', line 191

def abort_cap_neg
  @waiting_for_cap = false

  puts "Server does not support capability negotiation"
end

#cap_endObject

Called when we’re done with capability negotiation.



198
199
200
201
202
# File 'library/blur/network.rb', line 198

def cap_end
  @waiting_for_cap = false

  transmit :CAP, 'END'
end

#channel_by_name(name) ⇒ Network::Channel

Find a channel by its name.

Parameters:

  • name (String)

    the channel name.

Returns:

  • (Network::Channel)

    the matching channel, or nil.



136
137
138
# File 'library/blur/network.rb', line 136

def channel_by_name name
  @channels.find { |channel| channel.name == name }
end

#channel_flagsArray<String>

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

Returns:

  • (Array<String>)

    a list of channel flags.



166
167
168
# File 'library/blur/network.rb', line 166

def channel_flags
  isupport['CHANMODES']['D']
end

#channels_with_user(nick) ⇒ Array

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

Parameters:

  • nick (String)

    the nickname.

Returns:

  • (Array)

    a list of channels in which the user is located, or nil.



145
146
147
# File 'library/blur/network.rb', line 145

def channels_with_user nick
  @channels.select { |channel| channel.user_by_nick nick }
end

#connectObject

Attempt to establish a connection and send initial data.

See Also:



173
174
175
# File 'library/blur/network.rb', line 173

def connect
  @connection = EventMachine.connect host, port, Connection, self
end

#connected!Object

Called when the connection was successfully established.



178
179
180
181
182
183
184
185
186
187
188
# 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?Boolean

Check whether or not connection is established.

Returns:

  • (Boolean)


39
40
41
# File 'library/blur/network.rb', line 39

def connected?
  @connection&.established?
end

#disconnectObject

Terminate the connection and clear all channels and users.



214
215
216
# File 'library/blur/network.rb', line 214

def disconnect
  @connection.close_connection_after_writing
end

#disconnected!Object

Called when the connection was closed.



205
206
207
208
209
210
211
# 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) ⇒ Object

Forwards the received message to the client instance.

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



123
124
125
126
127
128
129
130
# 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

#hostString

Get the remote hostname.

Returns:

  • (String)

    the remote hostname.



46
47
48
# File 'library/blur/network.rb', line 46

def host
  @options['hostname']
end

#join(channel) ⇒ Object

Join a channel.



240
241
242
# File 'library/blur/network.rb', line 240

def join channel
  transmit :JOIN, channel
end

#portFixnum

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

Returns:

  • (Fixnum)

    the remote port



55
56
57
# File 'library/blur/network.rb', line 55

def port
  @options['port'] ||= secure? ? 6697 : 6667
end

#sasl?Boolean

Returns whether we want to authenticate with SASL.

Returns:

  • (Boolean)

    whether we want to authenticate with SASL.



65
66
67
68
69
# File 'library/blur/network.rb', line 65

def sasl?
  @options['sasl'] &&
    @options['sasl']['username'] &&
    @options['sasl']['password']
end

#say(recipient, message) ⇒ Object

Send a message to a recipient.

Parameters:

  • recipient (String, #to_s)

    the recipient.

  • message (String)

    the message.



116
117
118
# File 'library/blur/network.rb', line 116

def say recipient, message
  transmit :PRIVMSG, recipient.to_s, message
end

#secure?Boolean

Check to see if it’s a secure connection.

Returns:

  • (Boolean)


60
61
62
# File 'library/blur/network.rb', line 60

def secure?
  @options['secure'] == true
end

#send_privmsg(recipient, message) ⇒ Object

Send a private message.



235
236
237
# File 'library/blur/network.rb', line 235

def send_privmsg recipient, message
  transmit :PRIVMSG, recipient, message
end

#to_sObject

Convert it to a debug-friendly format.



245
246
247
# File 'library/blur/network.rb', line 245

def to_s
  %(#<#{self.class.name} "#{host}":#{port}>)
end

#transmit(name, *arguments) ⇒ Object

Transmit a command to the server.

Parameters:

  • name (Symbol, String)

    the command name.

  • arguments (...)

    all the prepended parameters.



222
223
224
225
226
227
228
229
230
231
232
# 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_modesArray<String>

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

Returns:

  • (Array<String>)

    a list of user modes.



159
160
161
# File 'library/blur/network.rb', line 159

def user_prefix_modes
  isupport['PREFIX'].keys
end

#user_prefixesArray<String>

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

Returns:

  • (Array<String>)

    a list of user prefixes.



152
153
154
# File 'library/blur/network.rb', line 152

def user_prefixes
  isupport['PREFIX'].values
end