Class: Codebot::Network
- Inherits:
-
Serializable
- Object
- Serializable
- Codebot::Network
- Includes:
- Sanitizers
- Defined in:
- lib/codebot/network.rb
Overview
This class represents an IRC network notifications can be delivered to.
Instance Attribute Summary collapse
-
#bind ⇒ String
The address to bind to.
-
#host ⇒ String
readonly
The hostname or IP address used for connecting to this network.
-
#modes ⇒ String
User modes to set.
-
#name ⇒ String
The name of this network.
-
#nick ⇒ String
The primary nickname for this network.
-
#nickserv_password ⇒ String
readonly
The password for NickServ authentication.
-
#nickserv_username ⇒ String
readonly
The username for NickServ authentication.
-
#port ⇒ Integer
readonly
The port used for connecting to this network.
-
#sasl_password ⇒ String
readonly
The password for SASL authentication.
-
#sasl_username ⇒ String
readonly
The username for SASL authentication.
-
#secure ⇒ Boolean
readonly
Whether TLS should be used when connecting to this network.
-
#server_password ⇒ String
The server password.
Class Method Summary collapse
-
.deserialize(name, data) ⇒ Hash
Deserializes a network.
-
.fields ⇒ Array<Symbol>
The fields used for serializing this network.
-
.serialize_as_hash? ⇒ true
To indicate that data is serialized into a hash.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Checks whether this network is equal to another network.
-
#hash ⇒ Integer
Generates a hash for this network.
-
#initialize(params) ⇒ Network
constructor
Creates a new network from the supplied hash.
-
#name_eql?(name) ⇒ Boolean
Checks whether the name of this network is equal to another name.
-
#nickserv? ⇒ Boolean
Checks whether NickServ is enabled for this network.
-
#real_port ⇒ Integer
Returns the port used for connecting to this network, or the default port if no port is set.
-
#sasl? ⇒ Boolean
Checks whether SASL is enabled for this network.
-
#serialize(_conf) ⇒ Array, Hash
Serializes this network.
-
#update!(params) ⇒ Object
Updates the network from the supplied hash.
- #update_complicated!(params) ⇒ Object
-
#update_connection(host, port, secure) ⇒ Object
Updates the connection details of this network.
-
#update_nickserv(disable, user, pass) ⇒ Object
Updates the NickServ authentication details of this network.
-
#update_sasl(disable, user, pass) ⇒ Object
Updates the SASL authentication details of this network.
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.
54 55 56 |
# File 'lib/codebot/network.rb', line 54 def initialize(params) update!(params) end |
Instance Attribute Details
#bind ⇒ String
Returns the address to bind to.
44 45 46 |
# File 'lib/codebot/network.rb', line 44 def bind @bind end |
#host ⇒ String (readonly)
Returns 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 |
#modes ⇒ String
Returns user modes to set.
47 48 49 |
# File 'lib/codebot/network.rb', line 47 def modes @modes end |
#name ⇒ String
Returns the name of this network.
12 13 14 |
# File 'lib/codebot/network.rb', line 12 def name @name end |
#nick ⇒ String
Returns the primary nickname for this network.
29 30 31 |
# File 'lib/codebot/network.rb', line 29 def nick @nick end |
#nickserv_password ⇒ String (readonly)
Returns the password for NickServ authentication.
41 42 43 |
# File 'lib/codebot/network.rb', line 41 def nickserv_password @nickserv_password end |
#nickserv_username ⇒ String (readonly)
Returns the username for NickServ authentication.
38 39 40 |
# File 'lib/codebot/network.rb', line 38 def nickserv_username @nickserv_username end |
#port ⇒ Integer (readonly)
Returns the port used for connecting to this network.
19 20 21 |
# File 'lib/codebot/network.rb', line 19 def port @port end |
#sasl_password ⇒ String (readonly)
Returns the password for SASL authentication.
35 36 37 |
# File 'lib/codebot/network.rb', line 35 def sasl_password @sasl_password end |
#sasl_username ⇒ String (readonly)
Returns the username for SASL authentication.
32 33 34 |
# File 'lib/codebot/network.rb', line 32 def sasl_username @sasl_username end |
#secure ⇒ Boolean (readonly)
Returns 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_password ⇒ String
Returns 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.
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 |
.fields ⇒ Array<Symbol>
Returns 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.
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.
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 |
#hash ⇒ Integer
Generates a hash for this network.
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.
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.
187 188 189 |
# File 'lib/codebot/network.rb', line 187 def nickserv? !nickserv_username.to_s.empty? || !nickserv_password.to_s.empty? end |
#real_port ⇒ Integer
Returns the port used for connecting to this network, or the default port if no port is set.
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.
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.
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.
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.
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.
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.
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 |