diff --git a/.gitignore b/.gitignore index 15eb06a..9ca3e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ *.sw? .DS_Store coverage -s nbproject/* \ No newline at end of file diff --git a/README b/README index b1c3a87..aa3acfc 100644 --- a/README +++ b/README @@ -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 ========== diff --git a/lib/ruby_bosh.rb b/lib/ruby_bosh.rb index 014c79e..455d4f8 100644 --- a/lib/ruby_bosh.rb +++ b/lib/ruby_bosh.rb @@ -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' @@ -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 @@ -48,7 +46,7 @@ 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 @@ -56,12 +54,12 @@ def connect 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')) @@ -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? @@ -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 @@ -101,16 +99,16 @@ 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?("") + response.include?("") end def send_session_request @@ -118,11 +116,11 @@ def send_session_request 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) @@ -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 @@ -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 diff --git a/ruby_bosh.gemspec b/ruby_bosh.gemspec index 012d6ec..621c01c 100644 --- a/ruby_bosh.gemspec +++ b/ruby_bosh.gemspec @@ -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{pradeep@intridea.com} 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} @@ -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, [">= 0"]) - s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0"]) s.add_runtime_dependency(%q, [">= 0"]) - s.add_runtime_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end else s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) s.add_dependency(%q, [">= 0"]) - s.add_dependency(%q, [">= 0"]) end end