Class: Jabber::Component

Inherits:
Connection show all
Defined in:
lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb

Overview

The component class provides everything needed to build a XMPP Component.

Components are more flexible as they are only restricted in the use of a fixed domain. node and resource of JIDs are freely choosable for all stanzas.

Constant Summary

Constants inherited from Stream

Stream::CONNECTED, Stream::DISCONNECTED

Instance Attribute Summary collapse

Attributes inherited from Connection

#allow_tls, #features_timeout, #host, #keepalive_interval, #port, #ssl_capath, #ssl_verifycb, #use_ssl

Attributes inherited from Stream

#fd, #status

Instance Method Summary collapse

Methods inherited from Connection

#accept_features, #close!, #is_tls?, #starttls

Methods inherited from Stream

#add_iq_callback, #add_message_callback, #add_presence_callback, #add_stanza_callback, #add_xml_callback, #close!, #delete_iq_callback, #delete_message_callback, #delete_presence_callback, #delete_stanza_callback, #delete_xml_callback, #is_connected?, #is_disconnected?, #on_exception, #parse_failure, #parser_end, #receive, #send, #send_data, #send_with_id, #stop

Constructor Details

#initialize(jid, server_address = nil, server_port = 5347) ⇒ Component

Create a new Component

jid
JID


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 26

def initialize(jid, server_address=nil, server_port=5347)
  super()
  @jid = (jid.kind_of?(JID) ? jid : JID.new(jid.to_s))

  if server_address
    $stderr.puts "Passing server and port to Jabber::Component.new is " +
                 "obsolete and will vanish in some later XMPP4R release. " +
                 "Please use these arguments when calling " +
                 "Jabber::Client#connect"
    @server_address = server_address
    @server_port = server_port
  end
end

Instance Attribute Details

#jidObject (readonly)

The component’s JID



16
17
18
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 16

def jid
  @jid
end

#server_addressObject (readonly)

The server’s address



19
20
21
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 19

def server_address
  @server_address
end

#server_portObject (readonly)

The server’s port



22
23
24
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 22

def server_port
  @server_port
end

Instance Method Details

#auth(secret) ⇒ Object

Send auth with given secret and wait for result

Throws ComponentAuthenticationFailure

secret
String

the shared secret



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 85

def auth(secret)
  hash = Digest::SHA1::hexdigest(@streamid.to_s + secret)
  authenticated = false
  send("<handshake>#{hash}</handshake>") { |r|
    if r.prefix == 'stream' and r.name == 'error'
      true
    elsif r.name == 'handshake'
      authenticated = true
      true
    else
      false
    end
  }
  unless authenticated
    raise ComponentAuthenticationFailure.new, "Component authentication failed"
  end
end

#closeObject

Close the connection, sends </stream:stream> tag first



57
58
59
60
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 57

def close
  send("</stream:stream>")
  super
end

#connect(server = nil, port = 5347) ⇒ Object

Connect to the server (chaining-friendly)

server
String

Hostname

port
Integer

TCP port (5347)

return

self



45
46
47
48
49
50
51
52
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 45

def connect(server=nil, port=5347)
  if server
    super(server, port)
  else
    super(@server_address, @server_port)
  end
  self
end

#startObject

Start the stream-parser and send the component-specific stream opening element



69
70
71
72
73
74
75
76
77
78
# File 'lib/gems/xmpp4r-0.4/lib/xmpp4r/component.rb', line 69

def start
  super
  send(generate_stream_start(@jid)) { |e|
    if e.name == 'stream'
      true
    else
      false
    end
  }
end