Skip to content

Commit e438b73

Browse files
committed
Merge pull request #107 from jonfreedman/master
Optional behavior to ignore message history if required
2 parents 529d774 + 5ad135a commit e438b73

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"node": ">=0.10"
2929
},
3030
"dependencies": {
31-
"node-xmpp-client": "3.0.0"
31+
"node-xmpp-client": "3.0.0",
32+
"uuid": "2.0.2"
3233
},
3334
"devDependencies": {
3435
"coffee-script": "1.1.3",

src/xmpp.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{Adapter,Robot,TextMessage,EnterMessage,LeaveMessage} = require 'hubot'
22
{JID, Stanza, Client, parse, Element} = require 'node-xmpp-client'
3+
uuid = require 'uuid'
34
util = require 'util'
45

56
class XmppBot extends Adapter
67

78
reconnectTryCount: 0
89
currentIqId: 1001
10+
joining: []
11+
joined: []
912

1013
constructor: ( robot ) ->
1114
@robot = robot
@@ -143,6 +146,17 @@ class XmppBot extends Adapter
143146
x.c('password').t(room.password)
144147
return x
145148

149+
if process.env.HUBOT_XMPP_UUID_ON_JOIN?
150+
# send a guid message and ignore any responses until that's been received
151+
uuid = uuid.v4()
152+
params = {
153+
to: room.jid
154+
type: 'groupchat'
155+
}
156+
@robot.logger.info "Joining #{room.jid} with #{uuid}"
157+
@joining[uuid] = room.jid
158+
@client.send new Stanza('message', params).c('body').t(uuid)
159+
146160
# XMPP Leaving a room - http://xmpp.org/extensions/xep-0045.html#exit
147161
leaveRoom: (room) ->
148162
# messageFromRoom check for joined rooms so remvove it from the list
@@ -244,6 +258,11 @@ class XmppBot extends Adapter
244258
from = stanza.attrs.from
245259
message = body.getText()
246260

261+
# check if this is a join guid and if so start accepting messages
262+
if process.env.HUBOT_XMPP_UUID_ON_JOIN? and message of @joining
263+
@robot.logger.info "Now accepting messages from #{@joining[message]}"
264+
@joined.push @joining[message]
265+
247266
if stanza.attrs.type == 'groupchat'
248267
# Everything before the / is the room name in groupchat JID
249268
[room, user] = from.split '/'
@@ -280,6 +299,9 @@ class XmppBot extends Adapter
280299
user.room = room
281300
user.privateChatJID = privateChatJID if privateChatJID
282301

302+
# only process persistent chant messages if we have matched a join
303+
return if process.env.HUBOT_XMPP_UUID_ON_JOIN? and stanza.attrs.type == 'groupchat' and user.room not in @joined
304+
283305
@robot.logger.debug "Received message: #{message} in room: #{user.room}, from: #{user.name}. Private chat JID is #{user.privateChatJID}"
284306

285307
@receive new TextMessage(user, message)

test/adapter-test.coffee

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Bot = require '../src/xmpp'
55

66
assert = require 'assert'
77
sinon = require 'sinon'
8+
uuid = require 'uuid'
89

910
describe 'XmppBot', ->
1011
describe '#parseRooms()', ->
@@ -997,3 +998,89 @@ describe 'XmppBot', ->
997998

998999
mock.verify()
9991000
assert.ok bot.robot.logger.error.called
1001+
1002+
describe 'uuid_on_join', () ->
1003+
beforeEach ->
1004+
uuid['v4'] = () -> 'fake-uuid-for-testing'
1005+
process.env.HUBOT_XMPP_UUID_ON_JOIN = true
1006+
1007+
bot = Bot.use()
1008+
1009+
bot.client =
1010+
stub: 'xmpp client'
1011+
1012+
bot.robot =
1013+
name: 'bot'
1014+
logger:
1015+
debug: () ->
1016+
info: () ->
1017+
brain:
1018+
userForId: (id, options)->
1019+
user = {}
1020+
user['name'] = id
1021+
for k of (options or {})
1022+
user[k] = options[k]
1023+
return user
1024+
1025+
room =
1026+
jid: 'test@example.com'
1027+
password: false
1028+
1029+
it 'should call @client.send() with a uuid', (done) ->
1030+
bot.client.send = (message) ->
1031+
if message.name == 'body'
1032+
assert.equal message.children.length, 1
1033+
assert.equal message.children[0], 'fake-uuid-for-testing'
1034+
done()
1035+
bot.joinRoom room
1036+
1037+
it 'should ignore messages', (done) ->
1038+
stanza =
1039+
attrs:
1040+
type: 'groupchat'
1041+
name: 'message'
1042+
flag: 'ignore_me'
1043+
proxied = bot.readMessage
1044+
bot.readMessage = (message) ->
1045+
proxied(message)
1046+
if message.flag == 'ignore_me'
1047+
done()
1048+
bot.receive = (message) ->
1049+
throw 'no message should be received'
1050+
bot.read stanza
1051+
1052+
it 'listen for the uuid before responding', (done) ->
1053+
stanza =
1054+
attrs:
1055+
type: 'groupchat'
1056+
from: 'test@example.com/bot'
1057+
name: 'message'
1058+
flag: 'join_me'
1059+
getChild: ->
1060+
body =
1061+
getText: ->
1062+
'fake-uuid-for-testing'
1063+
proxied = bot.readMessage
1064+
bot.readMessage = (message) ->
1065+
proxied(message)
1066+
assert.equal true, 'test@example.com' in bot.joined
1067+
if message.flag == 'join_me'
1068+
done()
1069+
bot.receive = (message) ->
1070+
throw 'no message should be received'
1071+
bot.read stanza
1072+
1073+
it 'should process messages after joining', (done) ->
1074+
stanza =
1075+
attrs:
1076+
type: 'groupchat'
1077+
from: 'test@example.com/someone'
1078+
name: 'message'
1079+
getChild: ->
1080+
body =
1081+
getText: ->
1082+
'@bot howdy'
1083+
bot.receive = (message) ->
1084+
assert.equal message, '@bot howdy'
1085+
done()
1086+
bot.read stanza

0 commit comments

Comments
 (0)