Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for XMPP rosters on connect #65

Closed
wants to merge 13 commits into from
5 changes: 3 additions & 2 deletions README.mdown
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -29,10 +29,11 @@ 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.
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
Expand Down
19 changes: 19 additions & 0 deletions src/xmpp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,6 +82,12 @@ class XmppBot extends Adapter
@client.send presence
@robot.logger.info 'Hubot XMPP sent initial presence'

# 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

@emit if @connected then 'reconnected' else 'connected'
Expand Down Expand Up @@ -152,6 +159,18 @@ 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 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) =>
# ignore non-messages
Expand Down