From ab194650e8c8dca3d7362227493b32e1615239b3 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Sun, 16 Mar 2014 11:46:43 -0400 Subject: [PATCH 01/13] Support for XMPP rosters on connect --- src/xmpp.coffee | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index c0fdb44..7c0141c 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -45,6 +45,11 @@ class XmppBot extends Adapter @connected = false @configClient(options) + # Expose the XMPP client through the robot to allow third-party scripts + # to send messages directly + @robot.xmppClient = @client + @robot.xmppRoster = [] + configClient: (options) -> @client.connection.socket.setTimeout 0 @client.connection.socket.setKeepAlive true, options.keepaliveInterval @@ -152,6 +157,14 @@ class XmppBot extends Adapter @robot.logger.debug "[sending pong] #{pong}" @client.send pong + # Keep a record of the client's primarty roster after connecting, so that + # scripts have the option of sending messages to all of the clients contacts + else (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) + roster_items = stanza.children[0]['children'] + + for item in roster_items + jid = new Xmpp.JID(item.attrs.jid) + @robot.xmppRoster.push(jid) readMessage: (stanza) => # ignore non-messages From 7cfd72c24f351a0387cb4c28884ec4b1e1191782 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 10:14:19 -0400 Subject: [PATCH 02/13] Don't need to expose the client again --- src/xmpp.coffee | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 7c0141c..00179a9 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -45,9 +45,7 @@ class XmppBot extends Adapter @connected = false @configClient(options) - # Expose the XMPP client through the robot to allow third-party scripts - # to send messages directly - @robot.xmppClient = @client + # TODO this should just be on robot.adapter.client.roster @robot.xmppRoster = [] configClient: (options) -> From 0f316279e559d5ac1211387a2464e627e4cd4a22 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 10:21:50 -0400 Subject: [PATCH 03/13] Use inline roster --- src/xmpp.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 00179a9..c46a345 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -40,6 +40,7 @@ class XmppBot extends Adapter legacySSL: options.legacySSL preferredSaslMechanism: options.preferredSaslMechanism disallowTLS: options.disallowTLS + roster: [] @options = options @connected = false @@ -162,6 +163,7 @@ class XmppBot extends Adapter for item in roster_items jid = new Xmpp.JID(item.attrs.jid) + @client.roster.push(jid) @robot.xmppRoster.push(jid) readMessage: (stanza) => From b454cc53351eef776917ea3cee25fdcc5c75b35b Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 10:36:25 -0400 Subject: [PATCH 04/13] else IF --- src/xmpp.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index c46a345..5d11a3d 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -158,7 +158,7 @@ class XmppBot extends Adapter @client.send pong # Keep a record of the client's primarty roster after connecting, so that # scripts have the option of sending messages to all of the clients contacts - else (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) + else if (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) roster_items = stanza.children[0]['children'] for item in roster_items From c0f33e7f97d008ba37122eb7a849aaf3580ac873 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 10:42:04 -0400 Subject: [PATCH 05/13] Always make a new roster --- src/xmpp.coffee | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 5d11a3d..e707922 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -40,15 +40,11 @@ class XmppBot extends Adapter legacySSL: options.legacySSL preferredSaslMechanism: options.preferredSaslMechanism disallowTLS: options.disallowTLS - roster: [] @options = options @connected = false @configClient(options) - # TODO this should just be on robot.adapter.client.roster - @robot.xmppRoster = [] - configClient: (options) -> @client.connection.socket.setTimeout 0 @client.connection.socket.setKeepAlive true, options.keepaliveInterval @@ -160,11 +156,12 @@ class XmppBot extends Adapter # scripts have the option of sending messages to all of the clients contacts else if (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) roster_items = stanza.children[0]['children'] - + + @client.roster = [] + for item in roster_items jid = new Xmpp.JID(item.attrs.jid) @client.roster.push(jid) - @robot.xmppRoster.push(jid) readMessage: (stanza) => # ignore non-messages From 582627d0d5359fb04972f8369a056fc5c9a1a696 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 10:59:56 -0400 Subject: [PATCH 06/13] Cleanup --- src/xmpp.coffee | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index e707922..8b07dbf 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -40,6 +40,7 @@ class XmppBot extends Adapter legacySSL: options.legacySSL preferredSaslMechanism: options.preferredSaslMechanism disallowTLS: options.disallowTLS + roster: [] @options = options @connected = false @@ -157,8 +158,6 @@ class XmppBot extends Adapter else if (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) roster_items = stanza.children[0]['children'] - @client.roster = [] - for item in roster_items jid = new Xmpp.JID(item.attrs.jid) @client.roster.push(jid) From d0255cd1a4ad749babdf11ae363582dfbd67f597 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 11:11:37 -0400 Subject: [PATCH 07/13] Update xmpp.coffee --- src/xmpp.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 8b07dbf..45650cc 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -40,7 +40,6 @@ class XmppBot extends Adapter legacySSL: options.legacySSL preferredSaslMechanism: options.preferredSaslMechanism disallowTLS: options.disallowTLS - roster: [] @options = options @connected = false @@ -158,9 +157,13 @@ class XmppBot extends Adapter else if (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) roster_items = stanza.children[0]['children'] + @client.roster = [] + for item in roster_items jid = new Xmpp.JID(item.attrs.jid) @client.roster.push(jid) + + @robot.logger.info "roster #{@client.roster}" readMessage: (stanza) => # ignore non-messages From 7a9e91ca26981bf9979573429fcc859fe901aecc Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 11:17:37 -0400 Subject: [PATCH 08/13] logging --- src/xmpp.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 45650cc..07cd150 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -139,7 +139,7 @@ class XmppBot extends Adapter @readIq stanza readIq: (stanza) => - @robot.logger.debug "[received iq] #{stanza}" + @robot.logger.info "[received iq] #{stanza}" # Some servers use iq pings to make sure the client is still functional. # We need to reply or we'll get kicked out of rooms we've joined. From 03053a48ca2cbfb84bc86a11806ef003747e9dad Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 11:32:06 -0400 Subject: [PATCH 09/13] Need to request roster --- src/xmpp.coffee | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index 07cd150..f8b649b 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -81,6 +81,11 @@ class XmppBot extends Adapter @client.send presence @robot.logger.info 'Hubot XMPP sent initial presence' + # Request client roster + @client.send do => + el = new Xmpp.Element('iq', from: @options.username, type: 'get', id: 'roster_1') + q = el.c('query', xmlns: 'jabber:iq:roster') + @joinRoom room for room in @options.rooms @emit if @connected then 'reconnected' else 'connected' From 520ec120aed0dc036acef9390dc33b3a2416fd48 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 11:39:09 -0400 Subject: [PATCH 10/13] Better logging --- src/xmpp.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index f8b649b..fc300d3 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -144,7 +144,7 @@ class XmppBot extends Adapter @readIq stanza readIq: (stanza) => - @robot.logger.info "[received iq] #{stanza}" + @robot.logger.debug "[received iq] #{stanza}" # Some servers use iq pings to make sure the client is still functional. # We need to reply or we'll get kicked out of rooms we've joined. @@ -168,7 +168,7 @@ class XmppBot extends Adapter jid = new Xmpp.JID(item.attrs.jid) @client.roster.push(jid) - @robot.logger.info "roster #{@client.roster}" + @robot.logger.debug "[setting roster] #{@client.roster}" readMessage: (stanza) => # ignore non-messages From 2df335ed1636f81a921daf35744ff4dd8561da4f Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 13:58:24 -0400 Subject: [PATCH 11/13] Whitespace --- README.mdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.mdown b/README.mdown index 951be5c..ddeaf79 100644 --- a/README.mdown +++ b/README.mdown @@ -1,6 +1,6 @@ # Hubot XMPP -Connects Hubot to your XMPP network +Connects Hubot to your XMPP network [![Build Status](https://secure.travis-ci.org/markstory/hubot-xmpp.png?branch=master)](http://travis-ci.org/markstory/hubot-xmpp) @@ -32,7 +32,7 @@ Optional: `HUBOT_XMPP_ROOMS` can be a comma separated list of rooms to join. If your rooms require passwords you should use the `jid:password` syntax. -Room passwords cannot contain `,`. Room names must be the full jid of the +Room passwords cannot contain `,`. Room names must be the full jid of the room for example `dev@conference.jabber.example.org`. ## Installation From 7fbbab1381cc0f573341d52f1f07f903c9c015a7 Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 13:58:50 -0400 Subject: [PATCH 12/13] Option for client rosters --- README.mdown | 1 + src/xmpp.coffee | 15 +++++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/README.mdown b/README.mdown index ddeaf79..302cbef 100644 --- a/README.mdown +++ b/README.mdown @@ -29,6 +29,7 @@ Optional: the host to be defined. * `HUBOT_XMPP_PREFERRED_SASL_MECHANISM` Used to change the encoding used for SASL. * `HUBOT_XMPP_DISALLOW_TLS` Prevent upgrading the connection to a secure one via TLS. +* `HUBOT_XMPP_CLIENT_ROSTER` Will request and cache the client's roster after connecting `HUBOT_XMPP_ROOMS` can be a comma separated list of rooms to join. If your rooms require passwords you should use the `jid:password` syntax. diff --git a/src/xmpp.coffee b/src/xmpp.coffee index fc300d3..f43025a 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -27,6 +27,7 @@ class XmppBot extends Adapter legacySSL: process.env.HUBOT_XMPP_LEGACYSSL preferredSaslMechanism: process.env.HUBOT_XMPP_PREFERRED_SASL_MECHANISM disallowTLS: process.env.HUBOT_XMPP_DISALLOW_TLS + roster: process.env.HUBOT_XMPP_CLIENT_ROSTER @robot.logger.info util.inspect(options) options.password = process.env.HUBOT_XMPP_PASSWORD @@ -81,10 +82,11 @@ class XmppBot extends Adapter @client.send presence @robot.logger.info 'Hubot XMPP sent initial presence' - # Request client roster - @client.send do => - el = new Xmpp.Element('iq', from: @options.username, type: 'get', id: 'roster_1') - q = el.c('query', xmlns: 'jabber:iq:roster') + # Request client roster if enabled + if @options.roster + @client.send do => + el = new Xmpp.Element('iq', from: @options.username, type: 'get', id: 'roster_1') + q = el.c('query', xmlns: 'jabber:iq:roster') @joinRoom room for room in @options.rooms @@ -161,13 +163,14 @@ class XmppBot extends Adapter # scripts have the option of sending messages to all of the clients contacts else if (stanza.attrs.id == 'roster_1' && stanza.children[0]['children']) roster_items = stanza.children[0]['children'] - + @client.roster = [] + for item in roster_items jid = new Xmpp.JID(item.attrs.jid) @client.roster.push(jid) - + @robot.logger.debug "[setting roster] #{@client.roster}" readMessage: (stanza) => From cac7fe005dbf5d78c65ad691d194d872c941a44a Mon Sep 17 00:00:00 2001 From: Chris Kalafarski Date: Mon, 17 Mar 2014 13:58:58 -0400 Subject: [PATCH 13/13] Whitespace --- src/xmpp.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/src/xmpp.coffee b/src/xmpp.coffee index f43025a..d8b1d8d 100644 --- a/src/xmpp.coffee +++ b/src/xmpp.coffee @@ -165,7 +165,6 @@ class XmppBot extends Adapter roster_items = stanza.children[0]['children'] @client.roster = [] - for item in roster_items jid = new Xmpp.JID(item.attrs.jid)