-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
strophe.emuc.js
240 lines (196 loc) · 5.97 KB
/
strophe.emuc.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { getLogger } from '@jitsi/logger';
import $ from 'jquery';
import { Strophe } from 'strophe.js';
import { CONNECTION_REDIRECTED } from '../../JitsiConnectionEvents';
import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
import ChatRoom from './ChatRoom';
import { ConnectionPluginListenable } from './ConnectionPlugin';
const logger = getLogger(__filename);
/**
* MUC connection plugin.
*/
export default class MucConnectionPlugin extends ConnectionPluginListenable {
/**
*
* @param xmpp
*/
constructor(xmpp) {
super();
this.xmpp = xmpp;
this.rooms = {};
}
/**
*
* @param connection
*/
init(connection) {
super.init(connection);
// add handlers (just once)
this.connection.addHandler(this.onPresence.bind(this), null,
'presence', null, null, null, null);
this.connection.addHandler(this.onPresenceUnavailable.bind(this),
null, 'presence', 'unavailable', null);
this.connection.addHandler(this.onPresenceError.bind(this), null,
'presence', 'error', null);
this.connection.addHandler(this.onMessage.bind(this), null,
'message', null, null);
this.connection.addHandler(this.onMute.bind(this),
'http://jitsi.org/jitmeet/audio', 'iq', 'set', null, null);
this.connection.addHandler(this.onMuteVideo.bind(this),
'http://jitsi.org/jitmeet/video', 'iq', 'set', null, null);
this.connection.addHandler(this.onVisitors.bind(this),
'jitsi:visitors', 'iq', 'set', null, null);
}
/**
*
* @param jid
* @param password
* @param options
*/
createRoom(jid, password, options) {
const roomJid = Strophe.getBareJidFromJid(jid);
if (this.isRoomCreated(roomJid)) {
const errmsg = 'You are already in the room!';
logger.error(errmsg);
throw new Error(errmsg);
}
this.rooms[roomJid] = new ChatRoom(this.connection, jid,
password, this.xmpp, options);
this.eventEmitter.emit(
XMPPEvents.EMUC_ROOM_ADDED, this.rooms[roomJid]);
return this.rooms[roomJid];
}
/**
* Check if a room with the passed JID is already created.
*
* @param {string} roomJid - The JID of the room.
* @returns {boolean}
*/
isRoomCreated(roomJid) {
return roomJid in this.rooms;
}
/**
*
* @param jid
*/
doLeave(jid) {
this.eventEmitter.emit(
XMPPEvents.EMUC_ROOM_REMOVED, this.rooms[jid]);
delete this.rooms[jid];
}
/**
*
* @param pres
*/
onPresence(pres) {
const from = pres.getAttribute('from');
// What is this for? A workaround for something?
if (pres.getAttribute('type')) {
return true;
}
const room = this.rooms[Strophe.getBareJidFromJid(from)];
if (!room) {
return true;
}
// Parse status.
if ($(pres).find('>x[xmlns="http://jabber.org/protocol/muc#user"]'
+ '>status[code="201"]').length) {
room.createNonAnonymousRoom();
}
room.onPresence(pres);
return true;
}
/**
*
* @param pres
*/
onPresenceUnavailable(pres) {
const from = pres.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
if (!room) {
return true;
}
room.onPresenceUnavailable(pres, from);
return true;
}
/**
*
* @param pres
*/
onPresenceError(pres) {
const from = pres.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
if (!room) {
return true;
}
room.onPresenceError(pres, from);
return true;
}
/**
*
* @param msg
*/
onMessage(msg) {
// FIXME: this is a hack. but jingle on muc makes nickchanges hard
const from = msg.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
if (!room) {
return true;
}
room.onMessage(msg, from);
return true;
}
/**
* TODO: Document
* @param iq
*/
onMute(iq) {
const from = iq.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
// Returning false would result in the listener being deregistered by Strophe
if (!room) {
return true;
}
room.onMute(iq);
return true;
}
/**
* TODO: Document
* @param iq
*/
onMuteVideo(iq) {
const from = iq.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
// Returning false would result in the listener being deregistered by Strophe
if (!room) {
return true;
}
room.onMuteVideo(iq);
return true;
}
/**
* A visitor IQ is received, pass it to the room.
* @param iq The received iq.
* @returns {boolean}
*/
onVisitors(iq) {
const from = iq.getAttribute('from');
const room = this.rooms[Strophe.getBareJidFromJid(from)];
if (!room) {
return true;
}
const visitors = $(iq).find('>visitors[xmlns="jitsi:visitors"]');
const response = $(iq).find('promotion-response');
if (visitors.length && response.length) {
if (String(response.attr('allow')).toLowerCase() === 'true') {
logger.info('Promotion request accepted. Redirected to main room.');
this.xmpp.eventEmitter.emit(
CONNECTION_REDIRECTED, undefined, visitors.attr('focusjid'), response.attr('username'));
} else {
logger.info('Promotion request rejected.');
this.xmpp.eventEmitter.emit(XMPPEvents.VISITORS_REJECTION);
}
}
return true;
}
}