Class: Blur::Network
- Inherits:
-
Object
- Object
- Blur::Network
- Includes:
- Logging
- Defined in:
- library/blur/network.rb,
library/blur/network/user.rb,
library/blur/network/channel.rb,
library/blur/network/command.rb,
library/blur/network/isupport.rb,
library/blur/network/connection.rb
Overview
Defined Under Namespace
Classes: Channel, Command, Connection, ConnectionError, ISupport, User
Instance Attribute Summary collapse
-
#channels ⇒ Array
The list of channels the client is in.
-
#connection ⇒ Network::Connection
The connection instance.
-
#delegate ⇒ Client
The client delegate.
-
#dialogues ⇒ Array
The list of private messages the client remembers.
-
#isupport ⇒ Network::ISupport
The network isupport specs.
-
#options ⇒ Hash
The network options.
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.
-
#fish? ⇒ Boolean
Check to see if FiSH encryption is enabled.
-
#got_command(command) ⇒ Object
Called when the network connection has enough data to form a command.
-
#host ⇒ String
Get the remote hostname.
-
#initialize(options) ⇒ Network
constructor
Instantiates the network.
-
#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.
-
#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.
Constructor Details
#initialize(options) ⇒ Network
Instantiates the network.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'library/blur/network.rb', line 71 def initialize @options = @channels = [] @isupport = ISupport.new self unless [:nickname] raise ArgumentError, "nickname is missing from the networks option block" end @options[:username] ||= @options[:nickname] @options[:realname] ||= @options[:username] @options[:channels] ||= [] end |
Instance Attribute Details
#channels ⇒ Array
Returns the list of channels the client is in.
17 18 19 |
# File 'library/blur/network.rb', line 17 def channels @channels end |
#connection ⇒ Network::Connection
Returns the connection instance.
23 24 25 |
# File 'library/blur/network.rb', line 23 def connection @connection end |
#delegate ⇒ Client
Returns the client delegate.
21 22 23 |
# File 'library/blur/network.rb', line 21 def delegate @delegate end |
#dialogues ⇒ Array
Returns the list of private messages the client remembers.
19 20 21 |
# File 'library/blur/network.rb', line 19 def dialogues @dialogues end |
#isupport ⇒ Network::ISupport
Returns the network isupport specs.
25 26 27 |
# File 'library/blur/network.rb', line 25 def isupport @isupport end |
#options ⇒ Hash
Returns the network options.
15 16 17 |
# File 'library/blur/network.rb', line 15 def @options end |
Instance Method Details
#channel_by_name(name) ⇒ Network::Channel
Find a channel by its name.
106 107 108 |
# File 'library/blur/network.rb', line 106 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).
136 137 138 |
# File 'library/blur/network.rb', line 136 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
.
115 116 117 |
# File 'library/blur/network.rb', line 115 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.
143 144 145 |
# File 'library/blur/network.rb', line 143 def connect @connection = EventMachine.connect host, port, Connection, self end |
#connected! ⇒ Object
Called when the connection was successfully established.
148 149 150 151 152 |
# File 'library/blur/network.rb', line 148 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.
28 |
# File 'library/blur/network.rb', line 28 def connected?; @connection and @connection.established? end |
#disconnect ⇒ Object
Terminate the connection and clear all channels and users.
163 164 165 |
# File 'library/blur/network.rb', line 163 def disconnect @connection.close_connection_after_writing end |
#disconnected! ⇒ Object
Called when the connection was closed.
155 156 157 158 159 160 |
# File 'library/blur/network.rb', line 155 def disconnected! @channels.each { |channel| channel.users.clear } @channels.clear @delegate.network_connection_closed self end |
#fish? ⇒ Boolean
Check to see if FiSH encryption is enabled.
46 |
# File 'library/blur/network.rb', line 46 def fish?; not @options[:fish].nil? end |
#got_command(command) ⇒ Object
Called when the network connection has enough data to form a command.
98 99 100 |
# File 'library/blur/network.rb', line 98 def got_command command @delegate.got_command self, command end |
#host ⇒ String
Get the remote hostname.
33 |
# File 'library/blur/network.rb', line 33 def host; @options[:hostname] end |
#port ⇒ Fixnum
Get the remote port. If no port is specified, it returns 6697 if using a secure connection, returns 6667 otherwise.
40 |
# File 'library/blur/network.rb', line 40 def port; @options[:port] ||= secure? ? 6697 : 6667 end |
#say(recipient, message) ⇒ Object
Send a message to a recipient.
89 90 91 92 93 94 95 |
# File 'library/blur/network.rb', line 89 def say recipient, if recipient.is_a? Channel and recipient.encrypted? = "+OK #{recipient.encryption.encrypt }" end transmit :PRIVMSG, recipient.to_s, end |
#secure? ⇒ Boolean
Check to see if it’s a secure connection.
43 |
# File 'library/blur/network.rb', line 43 def secure?; @options[:secure] == true end |
#to_s ⇒ Object
Convert it to a debug-friendly format.
180 181 182 |
# File 'library/blur/network.rb', line 180 def to_s %{#<#{self.class.name} "#{host}":#{port}>} end |
#transmit(name, *arguments) ⇒ Object
Transmit a command to the server.
171 172 173 174 175 176 |
# File 'library/blur/network.rb', line 171 def transmit name, *arguments command = Command.new name, arguments log "#{'→' ^ :red} #{command.name.to_s.ljust(8, ' ') ^ :light_gray} #{command.params.map(&:inspect).join ' '}" @connection.send_data "#{command}\r\n" end |
#user_prefix_modes ⇒ Array<String>
Returns a list of user modes that also gives a users nick a prefix.
129 130 131 |
# File 'library/blur/network.rb', line 129 def user_prefix_modes isupport["PREFIX"].keys end |
#user_prefixes ⇒ Array<String>
Returns a list of user prefixes that a nick might contain.
122 123 124 |
# File 'library/blur/network.rb', line 122 def user_prefixes isupport["PREFIX"].values end |