Skip to content

Commit

Permalink
Merge pull request #30 from node-xmpp/events-fix
Browse files Browse the repository at this point in the history
Events fix
  • Loading branch information
lloydwatkin committed Feb 4, 2014
2 parents 00d0bc7 + 018e9bc commit 82e80ba
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
34 changes: 25 additions & 9 deletions lib/c2s/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ function C2SServer(options) {
/* And now start listening to connections on the
* port provided as an option.
*/
this._setupAutoStart()
if (this.options.autostart !== false)
this.listen()
// TODO add reconnect-ability

// Load TLS key material
this._setupTls()
Expand All @@ -57,17 +59,31 @@ C2SServer.prototype._setupTls = function() {
this.credentials = crypto.createCredentials(details)
}

C2SServer.prototype._setupAutoStart = function() {
if (this.options.autostart === false) return
var port = this.options.port || this.C2S_PORT
var bindAddress = this.options.bindAddress || '::'
C2SServer.prototype.listen = function (port, bindAddress, callback) {
if (typeof port === 'function') {
callback = port
port = bindAddress = null
} else if (typeof bindAddress === 'function') {
callback = bindAddress
bindAddress = null
}
if (callback)
this.once('online', callback)
port = port || this.options.port || this.C2S_PORT
bindAddress = bindAddress || this.options.bindAddress || '::'
this.c2sPort = net.createServer(function(inStream) {
this.acceptConnection(inStream)
}.bind(this)).listen(port, bindAddress)
this.c2sPort.on('close', this.emit.bind(this, 'close'))
this.c2sPort.on('error', this.emit.bind(this, 'error'))
this.c2sPort.on('end', this.emit.bind(this, 'end'))
}.bind(this)).listen(port, bindAddress, this.emit.bind(this, 'online'))
this._addConnectionListener()
}

C2SServer.prototype._addConnectionListener = function (con) {
con = con || this.c2sPort
con.on('close', this.emit.bind(this, 'close'))
con.on('error', this.emit.bind(this, 'error'))
con.on('end', this.emit.bind(this, 'end'))
}

/**
* Called upon TCP connection from client.
* If you want to use your own C2SStream class just overwrite server.C2SStream*/
Expand Down
11 changes: 5 additions & 6 deletions lib/c2s/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ util.inherits(C2SStream, Connection)

C2SStream.prototype._addConnectionListeners = function(con) {
con = con || this.connection
con.on('streamStart', function (streamAttrs) {
this.serverdomain = streamAttrs.to
con.streamAttrs.from = streamAttrs.to
this.startStream()
}.bind(this))
con.on('stanza', this.onStanza.bind(this))
con.on('drain', this.emit.bind(this, 'drain'))
con.on('data', this.emit.bind(this, 'data'))
Expand All @@ -63,6 +58,11 @@ C2SStream.prototype._addConnectionListeners = function(con) {
con.on('connect', this.emit.bind(this, 'connect'))
con.on('reconnect', this.emit.bind(this, 'reconnect'))
con.on('disconnect', this.emit.bind(this, 'disconnect'))
con.on('streamStart', function (streamAttrs) {
this.serverdomain = streamAttrs.to
con.streamAttrs.from = streamAttrs.to
this.startStream()
}.bind(this))
}

C2SStream.prototype.send = function (stanza) {
Expand Down Expand Up @@ -150,7 +150,6 @@ C2SStream.prototype.onStanza = function (stanza) {
)
this.send(toSend)
this.connection.setSecure(this.server.credentials, true)
// FIXME: stop/startParser after 'secure'
} else if (stanza.is('auth', NS_XMPP_SASL) || stanza.is('response', NS_XMPP_SASL)) {
this.onAuth(stanza)
} else if (stanza.is('iq') && stanza.getChild('query', NS_REGISTER)) {
Expand Down
1 change: 0 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ exports.OutgoingServer = function(srcDomain, destDomain, credentials) {
})

this.listen({socket: SRV.connect({
connection: this,
services: ['_xmpp-server._tcp', '_jabber._tcp'],
domain: destDomain,
defaultPort: 5269
Expand Down
42 changes: 15 additions & 27 deletions test/sasl-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ function startServer(mechanism) {
return c2s
}

function registerHandler(cl) {
function createClient(opts) {

cl.on(
'stanza',
function(stanza) {
var cl = new Client(opts)

cl.on('stanza', function(stanza) {
if (stanza.is('message') &&
// Important: never reply to errors!
(stanza.attrs.type !== 'error')) {
Expand All @@ -108,6 +108,8 @@ function registerHandler(cl) {
}
}
)

return cl
}

describe('SASL', function() {
Expand All @@ -120,19 +122,16 @@ describe('SASL', function() {
})

after(function(done) {
c2s.shutdown()
done()
c2s.shutdown(done)
})

it('should accept plain authentication', function(done) {
var cl = new Client({
var cl = createClient({
jid: user.jid,
password: user.password,
preferred: Plain.id
})

registerHandler(cl)

cl.on('online', function() {
done()
})
Expand All @@ -144,13 +143,11 @@ describe('SASL', function() {
})

it('should not accept plain authentication', function(done) {
var cl = new Client({
var cl = createClient({
jid: user.jid,
password: 'secretsecret'
})

registerHandler(cl)

cl.on('online', function() {
done('user is not valid')
})
Expand All @@ -171,8 +168,7 @@ describe('SASL', function() {
})

after(function(done) {
c2s.shutdown()
done()
c2s.shutdown(done)
})

/*
Expand All @@ -181,15 +177,13 @@ describe('SASL', function() {
*/
it('should accept google authentication', function(done) {
/*jshint camelcase: false */
var gtalk = new Client({
var gtalk = createClient({
jid: '[email protected]',
oauth2_token: 'xxxx.xxxxxxxxxxx', // from OAuth2
oauth2_auth: 'http://www.google.com/talk/protocol/auth',
host: 'localhost'
})

registerHandler(gtalk)

gtalk.on('online', function() {
done()
})
Expand All @@ -210,20 +204,17 @@ describe('SASL', function() {
})

after(function(done) {
c2s.shutdown()
done()
c2s.shutdown(done)
})


it('should accept digest md5 authentication', function(done) {
var cl = new Client({
var cl = createClient({
jid: user.jid,
password: user.password,
preferred: 'DIGEST-MD5'
})

registerHandler(cl)

cl.on('online', function() {
done()
})
Expand Down Expand Up @@ -292,18 +283,15 @@ describe('SASL', function() {
})

after(function(done) {
c2s.shutdown()
done()
c2s.shutdown(done)
})

it('should accept anonymous authentication', function(done) {
var cl = new Client({
var cl = createClient({
jid: '@localhost',
preferred: Anonymous.id
})

registerHandler(cl)

cl.on('online', function(online) {
online.jid.local.length.should.equal(32)
online.jid.resource.length.should.equal(16)
Expand Down

0 comments on commit 82e80ba

Please sign in to comment.