Class | Jabber::Client |
In: |
lib/xmpp4r/client.rb
|
Parent: | Connection |
The client class provides everything needed to build a basic XMPP Client.
jid | [R] | The client’s JID |
Create a new Client. If threaded mode is activated, callbacks are called as soon as messages are received; If it isn’t, you have to call Stream#process from time to time. TODO SSL mode is not implemented yet.
# File lib/xmpp4r/client.rb, line 20 20: def initialize(jid, threaded = true) 21: super(threaded) 22: @jid = jid 23: end
Send auth with given password and wait for result
Throws AuthenticationFailure
password: | [String] the password |
digest: | [Boolean] use Digest authentication |
# File lib/xmpp4r/client.rb, line 53 53: def auth(password, digest=true) 54: authset = nil 55: if digest 56: authset = Iq::new_authset_digest(@jid, @streamid.to_s, password) 57: else 58: authset = Iq::new_authset(@jid, password) 59: end 60: authenticated = false 61: send(authset) do |r| 62: if r.kind_of?(Iq) and r.type == :result 63: authenticated = true 64: true 65: elsif r.kind_of?(Iq) and r.type == :error 66: true 67: else 68: false 69: end 70: end 71: $defout.flush 72: unless authenticated 73: raise AuthenticationFailure.new, "Client authentication failed" 74: end 75: end
Close the connection, sends </stream:stream> tag first
# File lib/xmpp4r/client.rb, line 42 42: def close 43: send("</stream:stream>") 44: super 45: end
connect to the server (chaining-friendly)
host: | [String] Optional c2s host, will be extracted from jid if nil |
return: | self |
# File lib/xmpp4r/client.rb, line 30 30: def connect(host = nil, port = 5222) 31: super(host.nil? ? jid.domain : host, port) 32: send("<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='jabber:client' to='#{@jid.domain}'>") { |b| 33: # TODO sanity check : is b a stream ? get version, etc. 34: true 35: } 36: self 37: end
Change the client’s password
Threading is suggested, as this code waits for an answer.
Raises an exception upon error response (ErrorException from Stream#send_with_id).
new_password: | [String] New password |
# File lib/xmpp4r/client.rb, line 86 86: def password=(new_password) 87: iq = Iq::new_query(:set, @jid.domain) 88: iq.query.add_namespace('jabber:iq:register') 89: iq.query.add(REXML::Element.new('username')).text = @jid.node 90: iq.query.add(REXML::Element.new('password')).text = new_password 91: 92: err = nil 93: send_with_id(iq) { |answer| 94: if answer.type == :result 95: true 96: else 97: false 98: end 99: } 100: end