Class: Rubot::Irc::Server
- 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
-
#channels ⇒ Object
readonly
The list of channels we’re currently in.
-
#connected_at ⇒ Object
readonly
When a successful connection to the server was made.
-
#nick ⇒ Object
readonly
Our current nick.
Instance Method Summary collapse
-
#action(destination, action) ⇒ Object
Adds action(s) to the outgoing queue.
-
#change_nick(new_nick) ⇒ Object
Changes our nick.
-
#connect ⇒ Object
Attempts to make a connection to the server.
-
#initialize(dispatcher) ⇒ Server
constructor
Initializes a Server using the given dispatcher.
-
#join(channels) ⇒ Object
Joins the specified channels, which can be comma separated string or an array.
-
#msg(destination, message) ⇒ Object
Adds message(s) to the outgoing queue.
-
#names(channel) ⇒ Object
Get the list of nicks in the given channel.
-
#part(channels) ⇒ Object
Parts the specified channels, which can be comma separated string or an array.
-
#quit ⇒ Object
Sends the quit command to the IRC server, then closes the connection.
-
#raw(message) ⇒ Object
Sends a raw line to the server and dumps it the console (to be replaced with a logger).
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. do |destination, | raw "PRIVMSG #{destination} :#{}" 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
#channels ⇒ Object (readonly)
The list of channels we’re currently in
16 17 18 |
# File 'lib/irc/server.rb', line 16 def channels @channels end |
#connected_at ⇒ Object (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 |
#nick ⇒ Object (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 |
#connect ⇒ Object
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.() 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, ) = .to_s.split("\n") unless .is_a? Array ().each do |line| @message_queue.(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 |
#quit ⇒ Object
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() puts "--> #{}" @conn.puts "#{}\n" end |