Class: Blur::Network

Inherits:
Object
  • Object
show all
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

The Network module is to be percieved as an IRC network.

Although the connection is a part of the network module, it is mainly used for network-related structures, such as User, Channel and Command.

Defined Under Namespace

Classes: Channel, Command, Connection, ConnectionError, ISupport, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Network

Instantiates the network.

Parameters:

  • options (Hash)

    The network options.

Options Hash (options):

  • :hostname (String)

    The hostname or IP-address we want to connect to.

  • :nickname (String)

    The nickname to use.

  • :username (optional, String) — default: Copies :nickname

    The username to use. This is also known as the ident.

  • :realname (optional, String) — default: Copies :username

    The “real name” that we want to use. This is usually what shows up as “Name” when you whois a user.

  • :password (optional, String)

    The password for the network. This is sometimes needed for private networks.

  • :port (optional, Fixnum) — default: 6697 if ssl, otherwise 6667

    The remote port we want to connect to.

  • :secure (optional, Boolean)

    Set whether this is a secure (SSL-encrypted) connection.

  • :ssl_cert_file (optional, String)

    Local path of a readable file that contains a X509 CA certificate to validate against.

  • :ssl_fingerprint (optional, String)

    Validate that the remote certificate matches the specified fingerprint.

  • :ssl_no_verify (optional, Boolean)

    Disable verification alltogether.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'library/blur/network.rb', line 71

def initialize options
  @options  = options
  @channels = []
  @isupport = ISupport.new self
  
  unless options[: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

#channelsArray

Returns the list of channels the client is in.

Returns:

  • (Array)

    the list of channels the client is in.



17
18
19
# File 'library/blur/network.rb', line 17

def channels
  @channels
end

#connectionNetwork::Connection

Returns the connection instance.

Returns:



23
24
25
# File 'library/blur/network.rb', line 23

def connection
  @connection
end

#delegateClient

Returns the client delegate.

Returns:

  • (Client)

    the client delegate.



21
22
23
# File 'library/blur/network.rb', line 21

def delegate
  @delegate
end

#dialoguesArray

Returns the list of private messages the client remembers.

Returns:

  • (Array)

    the list of private messages the client remembers.



19
20
21
# File 'library/blur/network.rb', line 19

def dialogues
  @dialogues
end

#isupportNetwork::ISupport

Returns the network isupport specs.

Returns:



25
26
27
# File 'library/blur/network.rb', line 25

def isupport
  @isupport
end

#optionsHash

Returns the network options.

Returns:

  • (Hash)

    the network options.



15
16
17
# File 'library/blur/network.rb', line 15

def options
  @options
end

Instance Method Details

#channel_by_name(name) ⇒ Network::Channel

Find a channel by its name.

Parameters:

  • name (String)

    the channel name.

Returns:



106
107
108
# File 'library/blur/network.rb', line 106

def channel_by_name name
  @channels.find { |channel| channel.name == name }
end

#channel_flagsArray<String>

Returns a list of channel flags (channel mode D).

Returns:

  • (Array<String>)

    a list of channel flags.



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.

Parameters:

  • nick (String)

    the nickname.

Returns:

  • (Array)

    a list of channels in which the user is located, or nil.



115
116
117
# File 'library/blur/network.rb', line 115

def channels_with_user nick
  @channels.select { |channel| channel.user_by_nick nick }
end

#connectObject

Attempt to establish a connection and send initial data.

See Also:



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.

Returns:

  • (Boolean)


28
# File 'library/blur/network.rb', line 28

def connected?; @connection and @connection.established? end

#disconnectObject

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.

Returns:

  • (Boolean)


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

#hostString

Get the remote hostname.

Returns:

  • (String)

    the remote hostname.



33
# File 'library/blur/network.rb', line 33

def host; @options[:hostname] end

#portFixnum

Get the remote port. If no port is specified, it returns 6697 if using a secure connection, returns 6667 otherwise.

Returns:

  • (Fixnum)

    the remote port



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.

Parameters:

  • recipient (String, #to_s)

    the recipient.

  • message (String)

    the message.



89
90
91
92
93
94
95
# File 'library/blur/network.rb', line 89

def say recipient, message
  if recipient.is_a? Channel and recipient.encrypted?
    message = "+OK #{recipient.encryption.encrypt message}"
  end

  transmit :PRIVMSG, recipient.to_s, message
end

#secure?Boolean

Check to see if it’s a secure connection.

Returns:

  • (Boolean)


43
# File 'library/blur/network.rb', line 43

def secure?; @options[:secure] == true end

#to_sObject

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.

Parameters:

  • name (Symbol, String)

    the command name.

  • arguments (...)

    all the prepended parameters.



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_modesArray<String>

Returns a list of user modes that also gives a users nick a prefix.

Returns:

  • (Array<String>)

    a list of user modes.



129
130
131
# File 'library/blur/network.rb', line 129

def user_prefix_modes
  isupport["PREFIX"].keys
end

#user_prefixesArray<String>

Returns a list of user prefixes that a nick might contain.

Returns:

  • (Array<String>)

    a list of user prefixes.



122
123
124
# File 'library/blur/network.rb', line 122

def user_prefixes
  isupport["PREFIX"].values
end