|
1 | 1 | {Adapter,Robot,TextMessage,EnterMessage,LeaveMessage} = require 'hubot' |
2 | 2 | {JID, Stanza, Client, parse, Element} = require 'node-xmpp-client' |
| 3 | +uuid = require 'uuid' |
3 | 4 | util = require 'util' |
4 | 5 |
|
5 | 6 | class XmppBot extends Adapter |
6 | 7 |
|
7 | 8 | reconnectTryCount: 0 |
8 | 9 | currentIqId: 1001 |
| 10 | + joining: [] |
| 11 | + joined: [] |
9 | 12 |
|
10 | 13 | constructor: ( robot ) -> |
11 | 14 | @robot = robot |
@@ -143,6 +146,16 @@ class XmppBot extends Adapter |
143 | 146 | x.c('password').t(room.password) |
144 | 147 | return x |
145 | 148 |
|
| 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 | + |
146 | 159 | # XMPP Leaving a room - http://xmpp.org/extensions/xep-0045.html#exit |
147 | 160 | leaveRoom: (room) -> |
148 | 161 | # messageFromRoom check for joined rooms so remvove it from the list |
@@ -244,6 +257,11 @@ class XmppBot extends Adapter |
244 | 257 | from = stanza.attrs.from |
245 | 258 | message = body.getText() |
246 | 259 |
|
| 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 | + |
247 | 265 | if stanza.attrs.type == 'groupchat' |
248 | 266 | # Everything before the / is the room name in groupchat JID |
249 | 267 | [room, user] = from.split '/' |
@@ -280,6 +298,9 @@ class XmppBot extends Adapter |
280 | 298 | user.room = room |
281 | 299 | user.privateChatJID = privateChatJID if privateChatJID |
282 | 300 |
|
| 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 | + |
283 | 304 | @robot.logger.debug "Received message: #{message} in room: #{user.room}, from: #{user.name}. Private chat JID is #{user.privateChatJID}" |
284 | 305 |
|
285 | 306 | @receive new TextMessage(user, message) |
|
0 commit comments