Class: Rubot::Irc::Server

Inherits:
Object show all
Includes:
Constants
Defined in:
lib/irc/server.rb

Overview

Performs the dirty work of communicating with an IRC server, passing messages back and forth between the server and a dispatcher.

Constant Summary

Constants included from Constants

Constants::ERR_NICK_IN_USE, Constants::MAX_MESSAGE_LENGTH, Constants::WELLCOME

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dispatcher) ⇒ Server

Initializes a Server using the given dispatcher.

The connection info to be used is extracted from the dispatcher’s config method. (This needs to be changed)

Parameters

dispatcher<Rubot::Core::Dispatcher>

The dispatcher to handle the messages we receive



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/irc/server.rb', line 25

def initialize(dispatcher)
  dispatcher.config["server"].each_pair do |key, value|
    instance_variable_set("@#{key}".to_sym, value)
  end
  @channels = @channels.split(",").collect(&:strip)
  @dispatcher = dispatcher
  
  @message_queue = MessageQueue.new(@message_delay)
  
  @message_queue.message do |destination, message|
    raw "PRIVMSG #{destination} :#{message}"
  end
  
  @message_queue.action do |destination, action|
    raw "PRIVMSG #{destination} :\001ACTION #{action}\001"
  end
  
  @message_queue.quit do |blah, more_blah|
    raw "QUIT :#{@quit_message}"
    @conn.close
  end
end

Instance Attribute Details

#channelsObject (readonly)

The list of channels we’re currently in



16
17
18
# File 'lib/irc/server.rb', line 16

def channels
  @channels
end

#connected_atObject (readonly)

When a successful connection to the server was made



13
14
15
# File 'lib/irc/server.rb', line 13

def connected_at
  @connected_at
end

#nickObject (readonly)

Our current nick



10
11
12
# File 'lib/irc/server.rb', line 10

def nick
  @nick
end

Instance Method Details

#action(destination, action) ⇒ Object

Adds action(s) to the outgoing queue

Parameters

destination<String>

Where to send the message

action<String,Array>

The actions(s) to be sent



117
118
119
# File 'lib/irc/server.rb', line 117

def action(destination, action)
  @message_queue.action(destination, action)
end

#change_nick(new_nick) ⇒ Object

Changes our nick.



75
76
77
78
# File 'lib/irc/server.rb', line 75

def change_nick(new_nick)
  raw "NICK #{new_nick}"
  @nick = new_nick
end

#connectObject

Attempts to make a connection to the server



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/irc/server.rb', line 49

def connect
  return if @is_connected

  @conn = TCPSocket.open(@host, @port, @vhost)
  raw "USER #{@nick} #{@nick} #{@nick} :#{@real_name}"
  change_nick @nick
  join @channels

  begin
    main_loop()
  rescue Interrupt
    quit
  rescue Exception => detail
    puts detail.message()
    print detail.backtrace.join("\n")
    retry
  end
end

#join(channels) ⇒ Object

Joins the specified channels, which can be comma separated string or an array.

Parameters

channels<String,Array>

The channels to join



84
85
86
87
88
# File 'lib/irc/server.rb', line 84

def join(channels)
  channels = channels.split(',') if channels.is_a? String
  @channels.concat(channels).uniq!
  bulk_command("JOIN %s", channels)
end

#msg(destination, message) ⇒ Object

Adds message(s) to the outgoing queue

Parameters

destination<String>

Where to send the message

message<String,Array>

The message(s) to be sent



105
106
107
108
109
110
# File 'lib/irc/server.rb', line 105

def msg(destination, message)
  message = message.to_s.split("\n") unless message.is_a? Array
  build_message_array(message).each do |line|
    @message_queue.message(destination, line)
  end
end

#names(channel) ⇒ Object

Get the list of nicks in the given channel. I’m not sure if the current implementation is really how it should be. TODO investigate

Parameters

channel<String>

The channel whose nicks we want



135
136
137
138
# File 'lib/irc/server.rb', line 135

def names(channel)
  raw "NAMES #{channel}"
  @conn.gets.split(":")[2].split(" ")
end

#part(channels) ⇒ Object

Parts the specified channels, which can be comma separated string or an array.

Parameters

channels<String,Array>

The channels to part



94
95
96
97
98
# File 'lib/irc/server.rb', line 94

def part(channels)
  channels = channels.split(',') if channels.is_a? String
  @channels.reject! { |channel| channels.include?(channel) }
  bulk_command("PART %s :#{@quit_message}", channels)
end

#quitObject

Sends the quit command to the IRC server, then closes the connection.



69
70
71
72
# File 'lib/irc/server.rb', line 69

def quit
  @dispatcher.on_quit(self)
  @message_queue.quit("Hades", "ice water plz").join
end

#raw(message) ⇒ Object

Sends a raw line to the server and dumps it the console (to be replaced with a logger)

Parameters

message<String>

The raw line to send



125
126
127
128
# File 'lib/irc/server.rb', line 125

def raw(message)
  puts "--> #{message}"
  @conn.puts "#{message}\n"
end