forked from nioc/xmpp-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmppSocket.js
724 lines (664 loc) · 21.5 KB
/
XmppSocket.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
import { XmppClient as XMPP, NS } from './XmppClient'
import defaultAvatar from '../assets/defaultAvatar'
const transports = window.config.transports
const resource = window.config.resource
const defaultDomain = window.config.defaultDomain
const defaultMuc = window.config.defaultMuc
const connectTimeout = window.config.connectTimeout
function logError (error, defaultLevel) {
const args = Array.prototype.slice.call(arguments, 2)
if (['XMPPError', 'StanzaError'].includes(error.name)) {
console.warn(error.name, ...args.filter(arg => !(arg instanceof Error)))
return
}
if (defaultLevel === 'error') {
console.error(...args)
} else {
console.warn(...args)
}
}
export default {
jid: null,
fullJid: null,
context: null,
client: null,
nick: null,
isAnonymous: true,
defaultDomain,
defaultMuc,
// create XMPP client with credentials and context
async create (jid, password, domain, transportsUser, context) {
// clear previous session
this.nick = null
this.fullJid = null
this.jid = null
this.context = context
this.disconnect()
// handle anonymous authentication
if (jid) {
this.isAnonymous = false
} else {
this.isAnonymous = true
jid = 'anon'
}
// set domain from user jid or by default
const jidParts = jid.split('@')
if (jidParts.length > 1) {
jid = jidParts[0]
domain = jidParts[1]
}
if (!domain) {
domain = defaultDomain
}
this.jid = jid
// use transports if user provided them
if (transportsUser.websocket) {
transports.websocket = transportsUser.websocket
}
// create XMPP client
this.client = new XMPP({
service: transports.websocket,
domain,
resource: resource || 'Web XMPP',
jid,
password,
})
},
// connect client to XMPP server
connect () {
const timeoutDuration = connectTimeout || 5000
let timeoutId = null
const timeoutPromise = new Promise((resolve, reject) => {
timeoutId = setTimeout(() => {
clearTimeout(timeoutId)
reject(new Error('Server unreachable'))
}, timeoutDuration)
})
const connectPromise = new Promise((resolve, reject) => {
// listen for XMPP error
this.client.on('error', (error) => {
console.error('XMPP error', error.message)
})
// listen for authentication success
this.client.on('authenticated', (jid) => {
if (!this.isAnonymous) {
localStorage.setItem('barejid', jid.bare)
localStorage.setItem('jid', this.jid)
localStorage.setItem('auth', true)
}
// resolve when listen is resolved
clearTimeout(timeoutId)
this.fullJid = jid
this.context.$store.setOnline(true)
this.listen()
resolve()
})
this.client.connect()
.catch((error) => {
// listen for authentication failure
if (error.name === 'SASLError') {
clearTimeout(timeoutId)
return reject(new Error('Check your credentials'))
}
reject(new Error('Error during login'))
})
})
return Promise.race([
connectPromise,
timeoutPromise,
])
},
// logic post connection (listeners)
listen () {
function storeMessage (xmppSocket, type, message) {
// clean body message if it contains only a link
if (message.links) {
if (message.links.some((link) => link.url === message.body)) {
message.body = ''
}
}
xmppSocket.context.$store.storeMessage({
type,
message,
})
}
this.client.on('online', () => {
console.info('XMPP online')
this.context.$store.setOnline(true)
})
this.client.on('status', (status) => {
if (status === 'close' || status === 'disconnect') {
if (this.context.$store.isOnline) {
console.warn('XMPP connection is closed')
this.context.$store.setOnline(false)
}
}
})
// get contacts (rfc6121)
this.client.getRoster()
.then((rosterResult) => {
this.context.$store.setRoster(rosterResult)
// send presence to contacts (rfc6121)
this.client.sendPresence()
})
.catch((rosterError) => logError(rosterError, 'error', 'getRoster', rosterError.message, rosterError))
this.client.getDiscoInfo()
.catch((discoInfoError) => logError(discoInfoError, 'error', 'getDiscoInfo', discoInfoError.message, discoInfoError))
// enable carbons (XEP-0280: Message Carbons)
this.client.enableCarbons()
.catch((error) => logError(error, 'error', 'carbon', error.message, error))
// get bookmarked rooms (XEP-0048: Bookmarks)
this.client.getBookmarks()
.then((mucBookmarks) => {
mucBookmarks.forEach((bookmark) => {
const room = this.setRoomAttributes(bookmark.jid, null, bookmark.password)
room.isBookmarked = true
room.name = bookmark.name
room.autojoin = bookmark.autojoin
// @TODO handle nick
this.context.$store.setKnownRoom(room)
if (bookmark.autojoin) {
// handle autojoin
this.joinRoom(bookmark.jid, null, { muc: { password: bookmark.password } })
}
})
// get rooms attributes
mucBookmarks.forEach((muc) => {
this.client.getDiscoInfo(muc.jid)
.then((mucDiscoInfoResult) => {
const room = this.setRoomAttributes(muc.jid, mucDiscoInfoResult, muc.password)
room.isBookmarked = true
this.context.$store.setKnownRoom(room)
})
.catch((error) => logError(error, 'error', 'getBookmarks/getDiscoInfo', error.message, error))
})
})
.catch((error) => logError(error, 'error', 'getBookmarks', error.message, error))
// get HTTP file upload capacity (XEP-0363)
this.client.getUploadService()
.then((UploadServiceResult) => {
if (UploadServiceResult.maxSize) {
this.context.$store.setHttpFileUploadMaxSize(UploadServiceResult.maxSize)
}
})
.catch((error) => {
console.warn(error.message)
})
// listen for contact/room messages
this.client.on('chat', (receivedMessage) => {
storeMessage(this, receivedMessage.type, receivedMessage)
})
// listen for message sent by user (direct or carbon)
this.client.on('messageSent', (message) => {
if (!message.body && !message.url) {
// no body in message (probably a chat state)
return
}
storeMessage(this, message.type, message)
})
// listen for contact chat state (writing, pause, ...)
this.client.on('chatState', chatState => {
this.context.$store.setChatState(chatState)
})
// listen for room creation
this.client.on('mucCreated', async (presence) => {
let room = {
jid: presence.from.bare,
}
this.context.$store.setKnownRoom(room)
// get room information
try {
const mucDiscoInfoResult = await this.client.getDiscoInfo(room.jid)
room = this.setRoomAttributes(room.jid, mucDiscoInfoResult, null)
this.context.$store.setKnownRoom(room)
} catch (error) {
logError(error, 'error', 'presence/getDiscoInfo', error.message, error)
}
})
// listen for presence
this.client.on('presence', async (presence) => {
const fullJid = presence.from
if (fullJid.bare === this.fullJid.bare) {
// user presence
if (fullJid.full === this.fullJid.full) {
// user presence on current resource, emit event
this.context.$store.setPresence(presence.show)
}
return
}
// check if it is a MUC presence
if (presence.isMuc) {
if (fullJid.resource === '') {
// room presence
return
}
if (presence.isSelf) {
if (presence.type === 'unavailable') {
this.context.$store.removeJoinedRoom(fullJid.bare)
} else {
this.context.$store.setJoinedRoom(fullJid.bare)
}
}
if (presence.type === 'unavailable') {
// occupant left room
this.context.$store.removeRoomOccupant({
roomJid: fullJid.bare,
jid: fullJid.full,
})
return
}
this.context.$store.setRoomOccupant({
roomJid: fullJid.bare,
jid: fullJid.full,
presence: presence.show,
})
return
}
// contact presence commit to store
this.context.$store.setContactPresence({ jid: fullJid.bare, presence: presence.show, status: presence.status })
})
// listen for retracted messages
this.client.on('messageRetracted', (retracted) => {
const index = this.context.$store.messages.findIndex((message) => message.from.bare === retracted.from && message.stanzaId === retracted.stanzaId)
if (index === -1) {
// original message is not found (unknown or retracted sent by a third party)
return
}
this.context.$store.updateMessage({
stanzaId: retracted.stanzaId,
// replace body and links
body: `Moderated by ${retracted.by.resource}` + (retracted.reason ? ` (${retracted.reason})` : ''),
links: [],
status: {
code: 'moderated',
message: retracted.reason,
},
})
})
// listen for room subject change
this.client.on('subjectChange', (subjectChange) => {
if (subjectChange.from && subjectChange.from.bare && subjectChange.subject) {
this.context.$store.setRoomSubject (subjectChange.from.bare, subjectChange.from.resource, subjectChange.subject)
}
})
// listen for sent message errors
this.client.on('messageSentError', (error) => {
switch (error.type) {
case 'cancel':
this.context.$store.setMessageStatus(error.messageId, 'error', error.message)
break
}
})
},
async disconnect () {
if (this.context && this.client) {
try {
await this.client.disconnect()
this.context.$store.clear()
} catch (error) {
logError(error, 'error', 'disconnect error', error.message, error)
}
}
},
async sendUrl (to, url, isMuc) {
await this.client.sendMessage(to, isMuc ? 'groupchat' : 'chat', url, url)
},
async sendMessage (to, body, isMuc) {
await this.client.sendMessage(to, isMuc ? 'groupchat' : 'chat', body)
},
async sendChatState (to, isMuc, chatState) {
await this.client.sendChatState(to, isMuc ? 'groupchat' : 'chat', chatState)
},
setRoomAttributes (jid, mucDiscoInfoResult, password = null) {
const room = {
jid: jid,
name: jid,
description: null,
lang: null,
occupantsCount: null,
password,
isPublic: null,
isPersistent: null,
isPasswordProtected: null,
isMembersOnly: null,
isAnonymous: null,
isModerated: null,
isBookmarked: null,
hasVCard: null,
unreadCount: null,
}
if (mucDiscoInfoResult) {
// get room name from identities
if (
Object.prototype.hasOwnProperty.call(mucDiscoInfoResult, 'identities') &&
mucDiscoInfoResult.identities.length > 0 &&
Object.prototype.hasOwnProperty.call(mucDiscoInfoResult.identities[0], 'name')
) {
room.name = mucDiscoInfoResult.identities[0].name
}
// get room extensions
if (
mucDiscoInfoResult.extensions.length > 0 &&
Object.prototype.hasOwnProperty.call(mucDiscoInfoResult.extensions[0], 'fields')
) {
const fields = mucDiscoInfoResult.extensions[0].fields
// description
const description = fields.find((field) => field.name === 'muc#roominfo_description')
if (description) {
room.description = description.value
}
// lang
const lang = fields.find((field) => field.name === 'muc#roominfo_lang')
if (lang) {
room.lang = lang.value
}
// occupants
const occupantsCount = fields.find((field) => field.name === 'muc#roominfo_occupants')
if (occupantsCount) {
room.occupantsCount = parseInt(occupantsCount.value)
room.occupantsCount = isNaN(room.occupantsCount) ? occupantsCount.value : room.occupantsCount
}
}
// public or hidden
if (mucDiscoInfoResult.features.includes('muc_public')) {
room.isPublic = true
}
if (mucDiscoInfoResult.features.includes('muc_hidden')) {
room.isPublic = false
}
// persistent or temporary (destroyed if the last occupant exits)
if (mucDiscoInfoResult.features.includes('muc_persistent')) {
room.isPersistent = true
}
if (mucDiscoInfoResult.features.includes('muc_temporary')) {
room.isPersistent = false
}
// password protected or not
if (mucDiscoInfoResult.features.includes('muc_passwordprotected')) {
room.isPasswordProtected = true
}
if (mucDiscoInfoResult.features.includes('muc_unsecured')) {
room.isPasswordProtected = false
}
// members only or open
if (mucDiscoInfoResult.features.includes('muc_membersonly')) {
room.isMembersOnly = true
}
if (mucDiscoInfoResult.features.includes('muc_open')) {
room.isMembersOnly = false
}
// semi-anonymous (display nick) or non-anonymous (display jid)
if (mucDiscoInfoResult.features.includes('muc_semianonymous')) {
room.isAnonymous = true
}
if (mucDiscoInfoResult.features.includes('muc_nonanonymous')) {
room.isAnonymous = false
}
// moderated or not
if (mucDiscoInfoResult.features.includes('muc_moderated')) {
room.isModerated = true
}
if (mucDiscoInfoResult.features.includes('muc_unmoderated')) {
room.isModerated = false
}
// has vCard
if (mucDiscoInfoResult.features.includes('vcard-temp')) {
room.hasVCard = true
}
}
return room
},
async getJidAvatar (jid) {
try {
const uri = sessionStorage.getItem('avatar-' + jid)
if (uri) {
return { uri, isDefault: false }
}
if (!this.client) {
return { uri: defaultAvatar, isDefault: true }
}
const vCard = await this.client.getVCard(jid)
if (!vCard.records) {
return { uri: defaultAvatar, isDefault: true }
}
const avatar = vCard.records.find((record) => record.name === 'PHOTO')
if (avatar && avatar.data) {
const uri = 'data:' + avatar.mediaType + ';base64,' + avatar.data
sessionStorage.setItem('avatar-' + jid, uri)
return { uri, isDefault: false }
}
} catch (error) {
logError(error, 'warn', 'getJidAvatar error', jid, error.message)
}
return { uri: defaultAvatar, isDefault: true }
},
async getProfile () {
try {
const vCard = await this.client.getVCard()
if (!vCard.records) {
return { }
}
return vCard.records
// transform each record
.map(attr => {
const value = (attr.name === 'PHOTO') ? 'data:' + attr.mediaType + ';base64,' + attr.data : attr.value
return {
name: attr.name,
value,
}
})
// transform in object
.reduce((acc, cur) => {
acc[cur.name] = cur.value
return acc
}, {})
} catch (error) {
logError(error, 'warn', 'getProfile error', error.message)
return {}
}
},
async updateProfile (profile) {
await this.client.setVCard(profile)
if (profile.PHOTO) {
sessionStorage.setItem('avatar-' + this.fullJid.bare, profile.PHOTO)
}
return
},
async sendPresence (presence) {
try {
// send global presence
await this.client.sendPresence(presence.show)
// send presence to joined rooms
this.context.$store.joinedRooms.forEach((roomJid) => {
this.client.sendPresence(presence.show, undefined, roomJid)
})
} catch (error) {
logError(error, 'error', 'sendPresence error', error.message, error)
}
},
async searchHistory (jid, last = true) {
try {
const history = await this.client.searchHistory(jid, last, 10)
return history.paging
} catch (error) {
logError(error, 'error', 'searchHistory error', error.message, error)
}
},
async joinRoom (jid, nick = null, opts = {}, _room = {}) {
if (!this.fullJid) {
return {
isSuccess: false,
message: 'User Jid is missing',
}
}
if (nick === null) {
if (this.nick !== null) {
nick = this.nick
} else {
nick = this.fullJid.local
}
}
try {
await this.client.joinRoom(jid, nick, opts)
if (_room.jid) {
const room = Object.assign({}, _room)
if (opts && opts.muc && opts.muc.password) {
room.password = opts.muc.password
}
this.context.$store.setKnownRoom(room)
}
return {
isSuccess: true,
}
} catch (error) {
logError(error, 'error', 'joinRoom', error.message, error)
return {
isSuccess: false,
message: this.getRoomError(error),
}
}
},
async getPublicMuc () {
if (!this.context) {
return []
}
const rooms = []
// discoItems on server
try {
const serverDiscoItemsResult = await this.client.getDiscoItems(this.fullJid.domain)
if (serverDiscoItemsResult.items.length === 0) {
console.info('There is no MUC service')
return []
}
// discoInfo on every service for finding MUC services
for (const serverDiscoItem of serverDiscoItemsResult.items) {
try {
const serviceDiscoInfoResult = await this.client.getDiscoInfo(serverDiscoItem.jid)
if (serviceDiscoInfoResult.features.includes(NS.MUC)) {
// discoItems on every MUC service for listing rooms
try {
const MucDiscoItemsResult = await this.client.getDiscoItems(serverDiscoItem.jid)
// discoInfo on every room for getting attributes
for (const MucDiscoItem of MucDiscoItemsResult.items) {
const room = await this.getRoom(MucDiscoItem.jid)
if (room.jid && room.jid !== serverDiscoItem.jid) {
this.context.$store.setKnownRoom(room)
rooms.push(room)
}
}
} catch (error) {
console.warn(`getDiscoItems on MUC service ${serverDiscoItem.jid} error:`, error.message)
}
}
} catch (error) {
logError(error, 'warn', `getDiscoInfo on service ${serverDiscoItem.jid} error: `, error.message)
}
}
} catch (error) {
logError(error, 'error', 'getDiscoItems on server error', error.message, error)
}
return rooms
},
async getRoom (jid) {
if (!this.context) {
return {
message: 'Missing context',
}
}
try {
const mucDiscoInfoResult = await this.client.getDiscoInfo(jid)
if (mucDiscoInfoResult.features.includes(NS.MUC)) {
const room = this.setRoomAttributes(jid, mucDiscoInfoResult)
return room
}
} catch (error) {
return {
message: this.getRoomError(error),
}
}
return {
message: 'Not a valid room',
}
},
getRoomError (error) {
if (Object.prototype.hasOwnProperty.call(error, 'message')) {
switch (error.message) {
case 'not-authorized':
return 'Valid password is required to join this room'
case 'forbidden':
return 'You have been banned from this room'
case 'item-not-found':
return 'This room does not exist'
case 'not-allowed':
return 'Room creation is restricted'
case 'not-acceptable':
return 'Reserved roomnick must be used'
case 'registration-required':
return 'You must be on the member list to join this room'
case 'conflict':
return 'Your nickname is already used in this room'
case 'service-unavailable':
return 'Maximum number of users has been reached in this room'
}
}
return 'Unable to join room'
},
// HTTP upload (XEP-0363)
async getUploadSlot (uploadService, uploadRequest) {
try {
return this.client.getUploadSlot(uploadService, uploadRequest)
} catch (error) {
logError(error, 'error', 'getUploadSlot error', error.message, error)
throw error
}
},
async bookmarkRoom (isAdd, jid, autojoin = true, nick = null) {
try {
const room = this.context.$store.getRoom(jid)
if (isAdd) {
// add bookmark
const bookmark = {
jid,
name: room.name,
autojoin,
}
if (room.password) {
bookmark.password = room.password
}
if (nick) {
bookmark.nick = nick
} else if (this.nick) {
bookmark.nick = this.nick
}
await this.client.addBookmark(bookmark)
this.context.$store.setKnownRoom({
jid,
isBookmarked: true,
})
return true
}
// remove bookmark
await this.client.removeBookmark(jid)
this.context.$store.setKnownRoom({
jid,
isBookmarked: false,
})
return true
} catch (error) {
return false
}
},
async createRoom (roomJid) {
return this.client.joinRoom(roomJid, this.fullJid.local, {})
},
async getRoomConfig (roomJid) {
return this.client.getRoomConfig(roomJid)
},
async setRoomConfig (roomJid, form) {
return this.client.configureRoom(roomJid, form)
},
// Set nickname
setNick (nick) {
this.nick = nick
},
}