Class: Blur::Network
- Inherits:
-
Object
- Object
- Blur::Network
- Includes:
- Logging
- Defined in:
- library/blur/network.rb,
library/blur/network/isupport.rb,
library/blur/network/connection.rb
Overview
Defined Under Namespace
Classes: Connection, ConnectionError, ISupport
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#channels ⇒ Hash
The map of channels the client is in.
-
#client ⇒ Client
The client reference.
-
#connection ⇒ Network::Connection
The connection instance.
-
#id ⇒ String
readonly
Returns a unique identifier for this network.
-
#isupport ⇒ Network::ISupport
The network isupport specs.
-
#options ⇒ Hash
The network options.
-
#users ⇒ Hash
The map of users that is known.
-
#waiting_for_cap ⇒ Boolean
readonly
True if we’re waiting for a capability negotiation.
Instance Method Summary collapse
-
#abort_cap_neg ⇒ Object
Called when the server doesn’t support capability negotiation.
-
#cap_end ⇒ Object
Called when we’re done with capability negotiation.
-
#channel_by_name(name) ⇒ Network::Channel
Find a channel by its name.
-
#channel_flags ⇒ Array<String>
Returns a list of channel flags (channel mode D).
-
#channels_with_user(nick) ⇒ Array
Find all instances of channels in which there is a user with the nick
nick
. -
#connect ⇒ Object
Attempt to establish a connection and send initial data.
-
#connected! ⇒ Object
Called when the connection was successfully established.
-
#connected? ⇒ Boolean
Check whether or not connection is established.
-
#disconnect ⇒ Object
Terminate the connection and clear all channels and users.
-
#disconnected! ⇒ Object
Called when the connection was closed.
-
#got_message(message) ⇒ Object
Forwards the received message to the client instance.
-
#host ⇒ String
Get the remote hostname.
-
#initialize(options, client = nil) ⇒ Network
constructor
Instantiates the network.
-
#join(channel) ⇒ Object
Join a channel.
-
#port ⇒ Fixnum
Get the remote port.
-
#sasl? ⇒ Boolean
Whether we want to authenticate with SASL.
-
#say(recipient, message) ⇒ Object
Send a message to a recipient.
-
#secure? ⇒ Boolean
Check to see if it’s a secure connection.
-
#send_privmsg(recipient, message) ⇒ Object
Send a private message.
-
#to_s ⇒ Object
Convert it to a debug-friendly format.
-
#transmit(name, *arguments) ⇒ Object
Transmit a command to the server.
-
#user_prefix_modes ⇒ Array<String>
Returns a list of user modes that also gives a users nick a prefix.
-
#user_prefixes ⇒ Array<String>
Returns a list of user prefixes that a nick might contain.
Methods included from Logging
Constructor Details
#initialize(options, client = nil) ⇒ Network
Instantiates the network.
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 , client = nil @client = client @options = @users = {} @channels = {} @isupport = ISupport.new self unless ['nickname'] raise ArgumentError, 'Network configuration for ' \ "`#{id}' is missing a nickname" end @options['username'] ||= @options['nickname'] @options['realname'] ||= @options['username'] @options['channels'] ||= [] @id = .fetch 'id', "#{host}:#{port}" end |
Instance Attribute Details
#channels ⇒ Hash
Returns the map of channels the client is in.
28 29 30 |
# File 'library/blur/network.rb', line 28 def channels @channels end |
#client ⇒ Client
Returns the client reference.
30 31 32 |
# File 'library/blur/network.rb', line 30 def client @client end |
#connection ⇒ Network::Connection
Returns the connection instance.
32 33 34 |
# File 'library/blur/network.rb', line 32 def connection @connection end |
#id ⇒ String (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>”
22 23 24 |
# File 'library/blur/network.rb', line 22 def id @id end |
#isupport ⇒ Network::ISupport
Returns the network isupport specs.
34 35 36 |
# File 'library/blur/network.rb', line 34 def isupport @isupport end |
#options ⇒ Hash
Returns the network options.
24 25 26 |
# File 'library/blur/network.rb', line 24 def @options end |
#users ⇒ Hash
Returns the map of users that is known.
26 27 28 |
# File 'library/blur/network.rb', line 26 def users @users end |
#waiting_for_cap ⇒ Boolean (readonly)
Returns 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_neg ⇒ Object
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_end ⇒ Object
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.
136 137 138 |
# File 'library/blur/network.rb', line 136 def channel_by_name name @channels.find { |channel| channel.name == name } end |
#channel_flags ⇒ Array<String>
Returns a list of channel flags (channel mode D).
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
.
145 146 147 |
# File 'library/blur/network.rb', line 145 def channels_with_user nick @channels.select { |channel| channel.user_by_nick nick } end |
#connect ⇒ Object
Attempt to establish a connection and send initial data.
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.
39 40 41 |
# File 'library/blur/network.rb', line 39 def connected? @connection&.established? end |
#disconnect ⇒ Object
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 @client. self, rescue StandardError => exception puts "#{exception.class}: #{exception.}" puts puts '---' puts exception.backtrace end |
#host ⇒ String
Get 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 |
#port ⇒ Fixnum
Get the remote port. If no port is specified, it returns 6697 if using a secure connection, returns 6667 otherwise.
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.
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.
116 117 118 |
# File 'library/blur/network.rb', line 116 def say recipient, transmit :PRIVMSG, recipient.to_s, end |
#secure? ⇒ Boolean
Check to see if it’s a secure connection.
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, transmit :PRIVMSG, recipient, end |
#to_s ⇒ Object
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.
222 223 224 225 226 227 228 229 230 231 232 |
# File 'library/blur/network.rb', line 222 def transmit name, *arguments = IRCParser::Message.new command: name.to_s, parameters: arguments if @client.verbose formatted_command = .command.to_s.ljust 8, ' ' formatted_params = .parameters.map(&:inspect).join ' ' log "#{'→' ^ :red} #{formatted_command} #{formatted_params}" end @connection.send_data "#{}\r\n" end |
#user_prefix_modes ⇒ Array<String>
Returns a list of user modes that also gives a users nick a prefix.
159 160 161 |
# File 'library/blur/network.rb', line 159 def user_prefix_modes isupport['PREFIX'].keys end |
#user_prefixes ⇒ Array<String>
Returns a list of user prefixes that a nick might contain.
152 153 154 |
# File 'library/blur/network.rb', line 152 def user_prefixes isupport['PREFIX'].values end |