This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganized the new XMPP code into a packet file, a basic connection …
…class/file that handles XML, and a component class/file that handles authing. I think. The main file still handles Disco and is now about 30 lines. I really have to do something about config thuogh, possibly just passing the parsed config hash into Component instead of all the values individually.
- Loading branch information
Showing
4 changed files
with
165 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
module Sails | ||
module XMPP | ||
class Component < Connection | ||
attr_accessor :subdomain, :jid, :domain, :secret | ||
|
||
def initialize subdomain, domain, secret | ||
super() | ||
|
||
@subdomain = subdomain | ||
@domain = domain | ||
@jid = "#{@subdomain}.#{@domain}" | ||
@me = @jid | ||
@secret = secret | ||
|
||
start_auth | ||
rescue => e | ||
p e | ||
puts e.message, e.backtrace | ||
end | ||
|
||
def receive_object packet, node | ||
case packet.name | ||
|
||
when 'stream:error' | ||
error = node.children.first.name rescue nil | ||
message = case error | ||
when 'conflict': 'The XMPP server denied this component because it conflicts with one that is already connected.' | ||
when nil: 'Unable to connect to XMPP. The server denied the component for an unknown reason.' | ||
else; "Unable to connect to XMPP: #{error}" | ||
end | ||
raise Sails::ProviderError, message | ||
|
||
when 'stream:stream' | ||
id = packet.id | ||
|
||
puts "Stream opened, sending challenge response" | ||
|
||
key = Digest::SHA1.hexdigest(id + @secret) | ||
send_raw "<handshake>#{key}</handshake>" | ||
|
||
when 'handshake' | ||
puts "Server accepted component; we are now live" | ||
#TODO: flush queues | ||
|
||
else | ||
@@handler.call packet, node | ||
end | ||
end | ||
|
||
def start_auth | ||
send_raw "<stream:stream xmlns=\"jabber:component:accept\" xmlns:stream=\"http://etherx.jabber.org/streams\" to=\"#{@jid}\">" | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'rubygems' | ||
require 'eventmachine' | ||
require 'socket' | ||
require 'hpricot' | ||
|
||
module Sails | ||
module XMPP | ||
class Connection < EventMachine::Connection | ||
def self.connect host, port, *args | ||
EventMachine::connect host, port, self, *args | ||
end | ||
def self.start_loop *args | ||
EventMachine::run { self.connect *args } | ||
end | ||
|
||
def self.on_packet &blck | ||
@@handler = blck | ||
end | ||
|
||
def self.send *args | ||
@@instance.send *args | ||
end | ||
def send name, type, to, data, id=nil | ||
send_raw "<#{name} type=\"#{type}\" id=\"#{id}\" to=\"#{to}\" from=\"#{@me}\">#{data}</#{name}>" | ||
end | ||
|
||
def self.send_raw data | ||
@@instance.send_raw data | ||
end | ||
def send_raw data | ||
data = data.to_s | ||
puts ">> #{data}" | ||
send_data "#{data}\n" | ||
end | ||
|
||
def initialize | ||
super | ||
begin | ||
@port, @ip = Socket.unpack_sockaddr_in get_peername | ||
puts "Connected to XMPP at #{@ip}:#{@port}" | ||
rescue TypeError | ||
puts "Unable to determine endpoint (connection refused?)" | ||
end | ||
|
||
@buffer = '' | ||
@@instance = self | ||
end | ||
|
||
def receive_data data | ||
puts "<< #{data}" | ||
if @@handler | ||
doc = Hpricot "<root>#{data}</root>" | ||
doc.root.children.each do |node| | ||
unless node.is_a? Hpricot::XMLDecl | ||
packet = Packet.new self, node | ||
receive_object packet, node | ||
end | ||
end | ||
end | ||
end | ||
|
||
def receive_object packet, node | ||
@@handler.call packet, node | ||
end | ||
|
||
def unbind | ||
puts "Disconnected from XMPP, reconnecting in 5 seconds" | ||
|
||
EventMachine::add_timer 5 do | ||
EventMachine.next_tick { self.class.connect @ip, @port } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
module Sails | ||
module XMPP | ||
class Packet | ||
attr_accessor :connection, :name, :type, :to, :from, :id, :node | ||
|
||
def initialize connection, xml | ||
@connection = connection | ||
@node = xml | ||
|
||
@name = xml.name | ||
@type = xml['type'] || 'default' | ||
@to = xml['to'] | ||
@from = xml['from'] | ||
@id = xml['id'] | ||
end | ||
|
||
def respond data | ||
@connection.send @name, 'result', @from, data, @id | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters