Skip to content

Commit 7ac26e9

Browse files
committed
Update xmpp.coffee
send a uuid message when joining a room, only accept messages from that room after that message has been echoed back in order to ignore any history we didn't actually want, see #104
1 parent 22b7bb5 commit 7ac26e9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/xmpp.coffee

Lines changed: 21 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,16 @@ class XmppBot extends Adapter
143146
x.c('password').t(room.password)
144147
return x
145148

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

260+
# check if this is a join guid and if so start accepting messages
261+
if message of @joining
262+
@robot.logger.info "Now accepting messages from #{@joining[message]}"
263+
@joined.push @joining[message]
264+
247265
if stanza.attrs.type == 'groupchat'
248266
# Everything before the / is the room name in groupchat JID
249267
[room, user] = from.split '/'
@@ -280,6 +298,9 @@ class XmppBot extends Adapter
280298
user.room = room
281299
user.privateChatJID = privateChatJID if privateChatJID
282300

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

285306
@receive new TextMessage(user, message)

0 commit comments

Comments
 (0)