Skip to content

Commit

Permalink
a working xmpp adapter for jabber chat rooms
Browse files Browse the repository at this point in the history
uses HUBOT_XMPP_USERNAME, HUBOT_XMPP_PASSWORD and comma-separated HUBOT_XMPP_ROOMS

tested with openfire 3.7.0

uses some ideas from markstory/hubot@5d47a8c - cheers!
  • Loading branch information
andyfowler committed Oct 26, 2011
1 parent 076420a commit 3934595
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bin/hubot
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ else
Hubot = require("hubot/campfire").Campfire
when "twilio"
Hubot = require("hubot/twilio").Twilio
when "xmpp"
Hubot = require("hubot/xmpp").XmppBot
else
Hubot = require("hubot/shell").Shell

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"scoped-http-client": "0.9.4",
"irc": "0.2",
"hiredis": "0.1.12",
"redis": "0.6.7"
"redis": "0.6.7",
"node-xmpp": ">=0.2.6"
},

"directories": {
Expand Down
85 changes: 85 additions & 0 deletions src/hubot/xmpp.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
Robot = require 'robot'
Xmpp = require 'node-xmpp'

class XmppBot extends Robot
run: ->
options =
username: process.env.HUBOT_XMPP_USERNAME
password: process.env.HUBOT_XMPP_PASSWORD
rooms: process.env.HUBOT_XMPP_ROOMS.split(',')
keepaliveInterval: 30000 # ms interval to send whitespace to xmpp server

console.log options

@client = new Xmpp.Client
jid: options.username
password: options.password

@client.on 'online', @.online
@client.on 'stanza', @.read

@options = options

online: =>
console.log 'Hubot XMPP client online'

@client.send new Xmpp.Element('presence', type: 'available' )
.c('show').t('chat')

# join each room
# http://xmpp.org/extensions/xep-0045.html for XMPP chat standard
for room in @options.rooms
@client.send(new Xmpp.Element('presence', to: "#{room}/#{@options.username}" )
.c('x', xmlns: 'http://jabber.org/protocol/muc' )
.c('history', seconds: 1 )) # prevent the server from confusing us with old messages
# and it seems that servers don't reliably support maxchars
# or zero values

# send raw whitespace for keepalive
setInterval =>
@client.send ' '
, @options.keepaliveInterval

read: (stanza) =>
if stanza.attrs.type is 'error'
console.error '[xmpp error]' + stanza
return

# ignore non-messages
return if !stanza.is 'message' || stanza.attrs.type not in ['groupchat', 'direct', 'chat']

# ignore our own messages
return if @options.username in stanza.attrs.from

# ignore empty bodies (i.e., topic changes -- maybe watch these someday)
body = stanza.getChild 'body'
return unless body

message = body.getText()

[room, from] = stanza.attrs.from.split '/'
user = new Robot.User from, {
room: room
}

@receive new Robot.Message user, message

send: (user, strings...) ->
strings.forEach (str) =>
console.log "Sending to #{user.room}: #{str}"

message = new Xmpp.Element('message',
from: @options.username
to: user.room
type: 'groupchat'
).
c('body').t(str)

@client.send message

reply: (user, strings...) ->
strings.forEach (str) =>
@send user, "#{user.name}: #{str}"


exports.XmppBot = XmppBot

0 comments on commit 3934595

Please sign in to comment.