Class: Codebot::Network

Inherits:
Serializable show all
Includes:
Sanitizers
Defined in:
lib/codebot/network.rb

Overview

This class represents an IRC network notifications can be delivered to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sanitizers

#valid!, #valid_boolean, #valid_channel_key, #valid_channel_name, #valid_endpoint, #valid_host, #valid_identifier, #valid_network, #valid_port, #valid_secret, #valid_string

Methods inherited from Serializable

deserialize_all, serialize_all

Constructor Details

#initialize(params) ⇒ Network

Creates a new network from the supplied hash.

Parameters:

  • params (Hash)

    A hash with symbolic keys representing the instance attributes of this network. The keys :name and :host are required.



54
55
56
# File 'lib/codebot/network.rb', line 54

def initialize(params)
  update!(params)
end

Instance Attribute Details

#bindString

Returns the address to bind to.

Returns:

  • (String)

    the address to bind to



44
45
46
# File 'lib/codebot/network.rb', line 44

def bind
  @bind
end

#hostString (readonly)

Returns the hostname or IP address used for connecting to this network.

Returns:

  • (String)

    the hostname or IP address used for connecting to this network



16
17
18
# File 'lib/codebot/network.rb', line 16

def host
  @host
end

#modesString

Returns user modes to set.

Returns:

  • (String)

    user modes to set



47
48
49
# File 'lib/codebot/network.rb', line 47

def modes
  @modes
end

#nameString

Returns the name of this network.

Returns:

  • (String)

    the name of this network



12
13
14
# File 'lib/codebot/network.rb', line 12

def name
  @name
end

#nickString

Returns the primary nickname for this network.

Returns:

  • (String)

    the primary nickname for this network



29
30
31
# File 'lib/codebot/network.rb', line 29

def nick
  @nick
end

#nickserv_passwordString (readonly)

Returns the password for NickServ authentication.

Returns:

  • (String)

    the password for NickServ authentication



41
42
43
# File 'lib/codebot/network.rb', line 41

def nickserv_password
  @nickserv_password
end

#nickserv_usernameString (readonly)

Returns the username for NickServ authentication.

Returns:

  • (String)

    the username for NickServ authentication



38
39
40
# File 'lib/codebot/network.rb', line 38

def nickserv_username
  @nickserv_username
end

#portInteger (readonly)

Returns the port used for connecting to this network.

Returns:

  • (Integer)

    the port used for connecting to this network



19
20
21
# File 'lib/codebot/network.rb', line 19

def port
  @port
end

#sasl_passwordString (readonly)

Returns the password for SASL authentication.

Returns:

  • (String)

    the password for SASL authentication



35
36
37
# File 'lib/codebot/network.rb', line 35

def sasl_password
  @sasl_password
end

#sasl_usernameString (readonly)

Returns the username for SASL authentication.

Returns:

  • (String)

    the username for SASL authentication



32
33
34
# File 'lib/codebot/network.rb', line 32

def sasl_username
  @sasl_username
end

#secureBoolean (readonly)

Returns whether TLS should be used when connecting to this network.

Returns:

  • (Boolean)

    whether TLS should be used when connecting to this network



23
24
25
# File 'lib/codebot/network.rb', line 23

def secure
  @secure
end

#server_passwordString

Returns the server password.

Returns:

  • (String)

    the server password



26
27
28
# File 'lib/codebot/network.rb', line 26

def server_password
  @server_password
end

Class Method Details

.deserialize(name, data) ⇒ Hash

Deserializes a network.

Parameters:

  • name (String)

    the name of the network

  • data (Hash)

    the serialized data

Returns:

  • (Hash)

    the parameters to pass to the initializer



225
226
227
# File 'lib/codebot/network.rb', line 225

def self.deserialize(name, data)
  fields.map { |sym| [sym, data[sym.to_s]] }.to_h.merge(name: name)
end

.fieldsArray<Symbol>

Returns the fields used for serializing this network.

Returns:

  • (Array<Symbol>)

    the fields used for serializing this network



235
236
237
238
# File 'lib/codebot/network.rb', line 235

def self.fields
  i[host port secure server_password nick sasl_username sasl_password
     nickserv_username nickserv_password bind modes]
end

.serialize_as_hash?true

Returns to indicate that data is serialized into a hash.

Returns:

  • (true)

    to indicate that data is serialized into a hash



230
231
232
# File 'lib/codebot/network.rb', line 230

def self.serialize_as_hash?
  true
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Checks whether this network is equal to another network.

Parameters:

  • other (Object)

    the other network

Returns:

  • (Boolean)

    true if the networks are equal, false otherwise



195
196
197
198
199
200
201
# File 'lib/codebot/network.rb', line 195

def ==(other)
  other.is_a?(Network) &&
    name_eql?(other.name) &&
    host.eql?(other.host) &&
    port.eql?(other.port) &&
    secure.eql?(other.secure)
end

#hashInteger

Generates a hash for this network.

Returns:

  • (Integer)

    the hash



206
207
208
# File 'lib/codebot/network.rb', line 206

def hash
  [name, host, port, secure].hash
end

#name_eql?(name) ⇒ Boolean

Checks whether the name of this network is equal to another name.

Parameters:

  • name (String)

    the other name

Returns:

  • (Boolean)

    true if the names are equal, false otherwise



165
166
167
# File 'lib/codebot/network.rb', line 165

def name_eql?(name)
  @name.casecmp(name).zero?
end

#nickserv?Boolean

Checks whether NickServ is enabled for this network.

Returns:

  • (Boolean)

    whether NickServ is enabled



187
188
189
# File 'lib/codebot/network.rb', line 187

def nickserv?
  !nickserv_username.to_s.empty? || !nickserv_password.to_s.empty?
end

#real_portInteger

Returns the port used for connecting to this network, or the default port if no port is set.

Returns:

  • (Integer)

    the port



173
174
175
# File 'lib/codebot/network.rb', line 173

def real_port
  port || (secure ? 6697 : 6667)
end

#sasl?Boolean

Checks whether SASL is enabled for this network.

Returns:

  • (Boolean)

    whether SASL is enabled



180
181
182
# File 'lib/codebot/network.rb', line 180

def sasl?
  !sasl_username.to_s.empty? && !sasl_password.to_s.empty?
end

#serialize(_conf) ⇒ Array, Hash

Serializes this network.

Parameters:

  • _conf (Hash)

    the deserialized configuration

Returns:

  • (Array, Hash)

    the serialized object



216
217
218
# File 'lib/codebot/network.rb', line 216

def serialize(_conf)
  [name, Network.fields.map { |sym| [sym.to_s, send(sym)] }.to_h]
end

#update!(params) ⇒ Object

Updates the network from the supplied hash.

Parameters:

  • params (Hash)

    A hash with symbolic keys representing the instance attributes of this network.



62
63
64
65
66
67
68
69
# File 'lib/codebot/network.rb', line 62

def update!(params)
  self.name            = params[:name]
  self.server_password = params[:server_password]
  self.nick            = params[:nick]
  self.bind            = params[:bind]
  self.modes           = params[:modes]
  update_complicated!(params)
end

#update_complicated!(params) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/codebot/network.rb', line 71

def update_complicated!(params)
  update_connection(params[:host], params[:port], params[:secure])
  update_sasl(params[:disable_sasl],
              params[:sasl_username], params[:sasl_password])
  update_nickserv(params[:disable_nickserv],
                  params[:nickserv_username], params[:nickserv_password])
end

#update_connection(host, port, secure) ⇒ Object

Updates the connection details of this network.

Parameters:

  • host (String)

    the new hostname, or nil to keep the current value

  • port (Integer)

    the new port, or nil to keep the current value

  • secure (Boolean)

    whether to connect over TLS, or nil to keep the current value



92
93
94
95
96
97
98
99
100
101
# File 'lib/codebot/network.rb', line 92

def update_connection(host, port, secure)
  @host = valid! host, valid_host(host), :@host,
                 required: true,
                 required_error: 'networks must have a hostname',
                 invalid_error: 'invalid hostname %s'
  @port = valid! port, valid_port(port), :@port,
                 invalid_error: 'invalid port number %s'
  @secure = valid!(secure, valid_boolean(secure), :@secure,
                   invalid_error: 'secure must be a boolean') { false }
end

#update_nickserv(disable, user, pass) ⇒ Object

Updates the NickServ authentication details of this network.

Parameters:

  • disable (Boolean)

    whether to disable NickServ, or nil to keep the current value.

  • user (String)

    the NickServ username, or nil to keep the current value

  • pass (String)

    the NickServ password, or nil to keep the current value



140
141
142
143
144
145
146
147
148
149
# File 'lib/codebot/network.rb', line 140

def update_nickserv(disable, user, pass)
  @nickserv_username = valid! user, valid_string(user), :@nickserv_username,
                              invalid_error: 'invalid NickServ username %s'
  @nickserv_password = valid! pass, valid_string(pass), :@nickserv_password,
                              invalid_error: 'invalid NickServ password %s'
  return unless disable

  @nickserv_username = nil
  @nickserv_password = nil
end

#update_sasl(disable, user, pass) ⇒ Object

Updates the SASL authentication details of this network.

Parameters:

  • disable (Boolean)

    whether to disable SASL, or nil to keep the current value.

  • user (String)

    the SASL username, or nil to keep the current value

  • pass (String)

    the SASL password, or nil to keep the current value



121
122
123
124
125
126
127
128
129
130
# File 'lib/codebot/network.rb', line 121

def update_sasl(disable, user, pass)
  @sasl_username = valid! user, valid_string(user), :@sasl_username,
                          invalid_error: 'invalid SASL username %s'
  @sasl_password = valid! pass, valid_string(pass), :@sasl_password,
                          invalid_error: 'invalid SASL password %s'
  return unless disable

  @sasl_username = nil
  @sasl_password = nil
end