Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails3 #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
*.sw?
.DS_Store
coverage
s
nbproject/*
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ The RubyBOSH library handles creating and pre-authenticating BOSH streams inside

This method allows you to hide authentication details for your users' XMPP accounts.

Tested on Rails 2.x with eJabberd 1.2+
Tested on Rails 3.x with eJabberd 2.1.5

References
==========
Expand Down
45 changes: 17 additions & 28 deletions lib/ruby_bosh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
require 'rexml/document'
require 'base64'
require 'hpricot'
require 'timeout'
require 'system_timer'

class RubyBOSH
class RubyBOSH
BOSH_XMLNS = 'http://jabber.org/protocol/httpbind'
TLS_XMLNS = 'urn:ietf:params:xml:ns:xmpp-tls'
SASL_XMLNS = 'urn:ietf:params:xml:ns:xmpp-sasl'
Expand All @@ -25,12 +23,12 @@ def self.logging=(value)
end

attr_accessor :jid, :rid, :sid, :success
def initialize(jid, pw, service_url, opts={})
def initialize(jid, pw, service_url, opts={})
@service_url = service_url
@jid, @pw = jid, pw
@host = jid.split("@").last
@success = false
@timeout = opts[:timeout] || 3 #seconds
@timeout = opts[:timeout] || 15 #seconds
@headers = {"Content-Type" => "text/xml; charset=utf-8",
"Accept" => "text/xml"}
@wait = opts[:wait] || 5
Expand All @@ -48,20 +46,20 @@ def self.initialize_session(*args)

def connect
initialize_bosh_session
if send_auth_request
if send_auth_request
send_restart_request
request_resource_binding
@success = send_session_request
end

raise RubyBOSH::AuthFailed, "could not authenticate #{@jid}" unless success?
@rid += 1 #updates the rid for the next call from the browser

[@jid, @sid, @rid]
end

private
def initialize_bosh_session
def initialize_bosh_session
response = deliver(construct_body(:wait => @wait, :to => @host,
:hold => @hold, :window => @window,
"xmpp:version" => '1.0'))
Expand All @@ -73,7 +71,7 @@ def construct_body(params={}, &block)

builder = Builder::XmlMarkup.new
parameters = {:rid => @rid, :xmlns => BOSH_XMLNS,
"xmpp:version" => "1.0",
"xmpp:version" => "1.0",
"xmlns:xmpp" => "urn:xmpp:xbosh"}.merge(params)

if block_given?
Expand All @@ -83,10 +81,10 @@ def construct_body(params={}, &block)
end
end

def send_auth_request
def send_auth_request
request = construct_body(:sid => @sid) do |body|
auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}"
body.auth(Base64.encode64(auth_string).gsub(/\s/,''),
auth_string = "#{@jid}\x00#{@jid.split("@").first.strip}\x00#{@pw}"
body.auth(Base64.encode64(auth_string).gsub(/\s/,''),
:xmlns => SASL_XMLNS, :mechanism => 'PLAIN')
end

Expand All @@ -101,28 +99,28 @@ def send_restart_request

def request_resource_binding
request = construct_body(:sid => @sid) do |body|
body.iq(:id => "bind_#{rand(100000)}", :type => "set",
body.iq(:id => "bind_#{rand(100000)}", :type => "set",
:xmlns => "jabber:client") do |iq|
iq.bind(:xmlns => BIND_XMLNS) do |bind|
bind.resource("bosh_#{rand(10000)}")
end
end
end

response = deliver(request)
response.include?("<jid>")
response.include?("<jid>")
end

def send_session_request
request = construct_body(:sid => @sid) do |body|
body.iq(:xmlns => CLIENT_XMLNS, :type => "set",
:id => "sess_#{rand(100000)}") do |iq|
iq.session(:xmlns => SESSION_XMLNS)
end
end
end

response = deliver(request)
response.include?("body")
response.include?("body")
end

def parse(_response)
Expand All @@ -134,14 +132,11 @@ def parse(_response)
end

def deliver(xml)
SystemTimer.timeout(@timeout) do
::Timeout::timeout(@timeout) do
send(xml)
recv(RestClient.post(@service_url, xml, @headers))
end
rescue ::Timeout::Error => e
raise RubyBOSH::Timeout, e.message
rescue Errno::ECONNREFUSED => e
raise RubyBOSH::ConnFailed, "could not connect to #{@host}\n#{e.message}"

rescue Exception => e
raise RubyBOSH::Error, e.message
end
Expand All @@ -154,9 +149,3 @@ def recv(msg)
puts("Ruby-BOSH - RECV\n#{msg}") if @logging; msg
end
end


if __FILE__ == $0
p RubyBOSH.initialize_session(ARGV[0], ARGV[1],
"http://localhost:5280/http-bind")
end
19 changes: 8 additions & 11 deletions ruby_bosh.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

Gem::Specification.new do |s|
s.name = %q{ruby_bosh}
s.version = "0.5.4"
s.version = "0.5.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Pradeep Elankumaran"]
s.date = %q{2009-04-21}
s.authors = ["Karametos Dimitrios"]
s.date = '2011-09-13'
s.description = %q{An XMPP BOSH session pre-initializer for Ruby web applications}
s.email = %q{[email protected]}
s.files = ["VERSION.yml", "lib/ruby_bosh.rb", "spec/ruby_bosh_spec.rb", "spec/spec_helper.rb"]
s.has_rdoc = true
s.homepage = %q{http://github.com/skyfallsin/ruby_bosh}
s.homepage = %q{http://github.com/dkarametos/ruby_bosh}
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
Expand All @@ -23,19 +23,16 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<builder>, [">= 0"])
s.add_runtime_dependency(%q<adamwiggins-rest-client>, [">= 0"])
s.add_runtime_dependency(%q<rest-client>, [">= 0"])
s.add_runtime_dependency(%q<hpricot>, [">= 0"])
s.add_runtime_dependency(%q<SystemTimer>, [">= 0"])
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<adamwiggins-rest-client>, [">= 0"])
s.add_dependency(%q<hpricot>, [">= 0"])
s.add_dependency(%q<SystemTimer>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<hpricot>, [">= 0"])
end
else
s.add_dependency(%q<builder>, [">= 0"])
s.add_dependency(%q<adamwiggins-rest-client>, [">= 0"])
s.add_dependency(%q<rest-client>, [">= 0"])
s.add_dependency(%q<hpricot>, [">= 0"])
s.add_dependency(%q<SystemTimer>, [">= 0"])
end
end