-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
146 lines (137 loc) · 4.26 KB
/
helper.js
File metadata and controls
146 lines (137 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var Client = require('node-xmpp-client')
, log = require('debug')('tests:helper')
, Buddycloud = require('xmpp-ftw-buddycloud')
, Event = require('events').EventEmitter
, userMap = require('./utils/user-map')
var connectedUsers = {}
var lastConnection = null
var XmppEventer = function() {}
XmppEventer.prototype = new Event()
XmppEventer.prototype.send = function(stanza) {
this.emit('stanza', stanza.root())
}
var SocketEventer = function() {}
SocketEventer.prototype = new Event()
SocketEventer.prototype.send = function(event, data, callback) {
this.emit(event, data, callback)
}
socket = new SocketEventer()
xmpp = new XmppEventer()
manager = {
socket: socket,
client: xmpp,
tracking: {},
trackId: function(id, callback) {
if (!id)
throw new Error(this.MISSING_STANZA_ID)
var jid
if (typeof id === 'object') {
if (!id.root().attrs.id)
throw new Error(this.MISSING_STANZA_ID)
jid = id.root().attrs.to
id = id.root().attrs.id
if (!jid){
jid = [
this.getJidType('domain'),
this.getJidType('bare')
]
} else {
jid = [ jid ]
}
}
if (!callback)
throw new Error(this.MISSING_CALLBACK)
if (typeof callback !== 'function')
throw new Error(this.INVALID_CALLBACK)
if (!this.client) {
return this.handleError(this.error.condition.NOT_CONNECTED)
}
this.tracking[id] = { callback: callback, jid: jid }
},
makeCallback: function(error, data) {
this.callback(error, data)
},
setConnection: function(connection) {
this.client = connection
var self = this
this.client.on('stanza', function(stanza) { self.catchTracked(stanza) })
},
getConnection: function() {
return this.client
},
getSocket: function() {
return this.socket
},
_getLogger: function() {
return {
log: function() {},
info: function() {},
error: function() {},
warn: function() {}
}
},
catchTracked: function(stanza) {
var id = stanza.root().attr('id')
if (!id || !this.tracking[id]) return false
if (this.tracking[id].jid &&
stanza.attr('from') &&
(-1 === this.tracking[id].jid.indexOf(stanza.attr('from')))) {
// Ignore stanza its an ID spoof!
return true
}
this.tracking[id].callback(stanza)
delete this.tracking[id]
return true
},
getJidType: function(type) {
switch (type) {
case 'full':
return this.client.jid.user + '@' +
this.client.jid.domain + '/' +
this.client.jid.resource
case 'bare':
return this.client.jid.user + '@' + this.client.jid.domain
case 'domain':
return this.client.jid.domain
}
}
}
var buddycloud = new Buddycloud()
buddycloud.init(manager)
buddycloud.setDiscoveryTimeout(300)
var getConnection = function(user, server, callback, registered) {
var options = userMap.getDetails(user, server)
if (!registered) {
options.register = true
}
var client = new Client(options)
log('Attempting to log in ', options)
client.on('online', function(data) {
log('User ' + user + '@' + server + ' logged in')
connectedUsers[user + '@' + server] = client
lastConnection = client
callback(null, client)
})
client.on('error', function(error) {
if (error.message === 'Registration error') {
log('User already registered, so logging in')
delete options.register
return getConnection(user, server, callback, true)
}
log('Error logging in', error)
callback(error)
})
}
var setLastConnection = function(connection) {
manager.setConnection(connection)
}
var getLastConnection = function() {
manager.setConnection(lastConnection)
return lastConnection
}
module.exports = {
getConnection: getConnection,
buddycloud: buddycloud,
manager: manager,
getLastConnection: getLastConnection
}