Class: Blur::Network

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
library/blur/network.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: Connection, ConnectionError, ISupport

Constant Summary

Constants included from Logging

Logging::Levels

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(options, client = nil) ⇒ 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.



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 options, client = nil
  @client = client
  @options = options
  @users = {}
  @channels = {}
  @isupport = ISupport.new self
  
  unless options['nickname']
    if options['hostname']
      raise ArgumentError, "Network configuration for `#{options['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

#channelsHash

Returns the map of channels the client is in.

Returns:

  • (Hash)

    the map of channels the client is in.



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

def channels
  @channels
end

#clientClient

Returns the client reference.

Returns:

  • (Client)

    the client reference.



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

def client
  @client
end

#connectionNetwork::Connection

Returns the connection instance.

Returns:



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

def connection
  @connection
end

#isupportNetwork::ISupport

Returns the network isupport specs.

Returns:



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

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

#usersHash

Returns the map of users that is known.

Returns:

  • (Hash)

    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.

Parameters:

  • name (String)

    the channel name.

Returns:

  • (Network::Channel)

    the matching channel, or nil.



111
112
113
# File 'library/blur/network.rb', line 111

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.



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.

Parameters:

  • nick (String)

    the nickname.

Returns:

  • (Array)

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



120
121
122
# File 'library/blur/network.rb', line 120

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:



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.

Returns:

  • (Boolean)


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

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

#disconnectObject

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 got_message message
  @client.got_message self, message
rescue => e
  puts "#{e.class}: #{e.message}"
  puts
  puts "---"
  puts e.backtrace
end

#hostString

Get the remote hostname.

Returns:

  • (String)

    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

#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



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.

Parameters:

  • recipient (String, #to_s)

    the recipient.

  • message (String)

    the message.



93
94
95
# File 'library/blur/network.rb', line 93

def say recipient, message
  transmit :PRIVMSG, recipient.to_s, message
end

#secure?Boolean

Check to see if it’s a secure connection.

Returns:

  • (Boolean)


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, message
  transmit :PRIVMSG, recipient, message
end

#to_sObject

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.

Parameters:

  • name (Symbol, String)

    the command name.

  • arguments (...)

    all the prepended parameters.



177
178
179
180
181
182
183
184
185
# File 'library/blur/network.rb', line 177

def transmit name, *arguments
  message = IRCParser::Message.new command: name.to_s, parameters: arguments

  if @client.verbose
    log "#{'' ^ :red} #{message.command.to_s.ljust(8, ' ') ^ :light_gray} #{message.parameters.map(&:inspect).join ' '}"
  end
  
  @connection.send_data "#{message}\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.



134
135
136
# File 'library/blur/network.rb', line 134

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.



127
128
129
# File 'library/blur/network.rb', line 127

def user_prefixes
  isupport["PREFIX"].values
end