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.
-
#isupport ⇒ Network::ISupport
The network isupport specs.
-
#options ⇒ Hash
The network options.
-
#users ⇒ Hash
The map of users that is known.
Instance Method Summary collapse
-
#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
Called when the network connection has enough data to form a command.
-
#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.
-
#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.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'library/blur/network.rb', line 69 def initialize , client = nil @client = client @options = @users = {} @channels = {} @isupport = ISupport.new self unless ['nickname'] if ['hostname'] raise ArgumentError, "Network configuration for `#{['hostname']}' is missing a nickname" else raise ArgumentError, "Network configuration is missing a nickname" end end @options['username'] ||= @options['nickname'] @options['realname'] ||= @options['username'] @options['channels'] ||= [] end |
Instance Attribute Details
#channels ⇒ Hash
Returns the map of channels the client is in.
20 21 22 |
# File 'library/blur/network.rb', line 20 def channels @channels end |
#client ⇒ Client
Returns the client reference.
22 23 24 |
# File 'library/blur/network.rb', line 22 def client @client end |
#connection ⇒ Network::Connection
Returns the connection instance.
24 25 26 |
# File 'library/blur/network.rb', line 24 def connection @connection end |
#isupport ⇒ Network::ISupport
Returns the network isupport specs.
26 27 28 |
# File 'library/blur/network.rb', line 26 def isupport @isupport end |
#options ⇒ Hash
Returns the network options.
15 16 17 |
# File 'library/blur/network.rb', line 15 def @options end |
#users ⇒ Hash
Returns the map of users that is known.
18 19 20 |
# File 'library/blur/network.rb', line 18 def users @users end |
Instance Method Details
#channel_by_name(name) ⇒ Network::Channel
Find a channel by its name.
111 112 113 |
# File 'library/blur/network.rb', line 111 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).
141 142 143 |
# File 'library/blur/network.rb', line 141 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
.
120 121 122 |
# File 'library/blur/network.rb', line 120 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.
148 149 150 |
# File 'library/blur/network.rb', line 148 def connect @connection = EventMachine.connect host, port, Connection, self end |
#connected! ⇒ Object
Called when the connection was successfully established.
153 154 155 156 157 |
# File 'library/blur/network.rb', line 153 def connected! 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.
29 |
# File 'library/blur/network.rb', line 29 def connected?; @connection and @connection.established? end |
#disconnect ⇒ Object
Terminate the connection and clear all channels and users.
169 170 171 |
# File 'library/blur/network.rb', line 169 def disconnect @connection.close_connection_after_writing end |
#disconnected! ⇒ Object
Called when the connection was closed.
160 161 162 163 164 165 166 |
# File 'library/blur/network.rb', line 160 def disconnected! @channels.each {|name, channel| channel.users.clear } @channels.clear @users.clear @client.network_connection_closed self end |
#got_message(message) ⇒ Object
Called when the network connection has enough data to form a command.
98 99 100 101 102 103 104 105 |
# File 'library/blur/network.rb', line 98 def @client. self, rescue => e puts "#{e.class}: #{e.}" puts puts "---" puts e.backtrace end |
#host ⇒ String
Get the remote hostname.
34 |
# File 'library/blur/network.rb', line 34 def host; @options['hostname'] end |
#join(channel) ⇒ Object
Join a channel.
193 194 195 |
# File 'library/blur/network.rb', line 193 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.
41 |
# File 'library/blur/network.rb', line 41 def port; @options['port'] ||= secure? ? 6697 : 6667 end |
#say(recipient, message) ⇒ Object
Send a message to a recipient.
93 94 95 |
# File 'library/blur/network.rb', line 93 def say recipient, transmit :PRIVMSG, recipient.to_s, end |
#secure? ⇒ Boolean
Check to see if it’s a secure connection.
44 |
# File 'library/blur/network.rb', line 44 def secure?; @options['secure'] == true end |
#send_privmsg(recipient, message) ⇒ Object
Send a private message.
188 189 190 |
# File 'library/blur/network.rb', line 188 def send_privmsg recipient, transmit :PRIVMSG, recipient, end |
#to_s ⇒ Object
Convert it to a debug-friendly format.
198 199 200 |
# File 'library/blur/network.rb', line 198 def to_s %{#<#{self.class.name} "#{host}":#{port}>} end |
#transmit(name, *arguments) ⇒ Object
Transmit a command to the server.
177 178 179 180 181 182 183 184 185 |
# File 'library/blur/network.rb', line 177 def transmit name, *arguments = IRCParser::Message.new command: name.to_s, parameters: arguments if @client.verbose log "#{'→' ^ :red} #{.command.to_s.ljust(8, ' ') ^ :light_gray} #{.parameters.map(&:inspect).join ' '}" 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.
134 135 136 |
# File 'library/blur/network.rb', line 134 def user_prefix_modes isupport["PREFIX"].keys end |
#user_prefixes ⇒ Array<String>
Returns a list of user prefixes that a nick might contain.
127 128 129 |
# File 'library/blur/network.rb', line 127 def user_prefixes isupport["PREFIX"].values end |