-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
656 lines (604 loc) · 20.2 KB
/
index.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
let data_names = {
"jabber:iq:roster": "Contact list",
"vcard-temp": "Profile information",
"jabber:iq:private": "Chat room bookmarks",
};
// Hard-code BOSH URLs for some servers without XEP-0156 support
let static_service_urls = {
"chatterboxtown.us": "https://chatterboxtown.us:5443/bosh",
"jabber.de": "https://jabber.de:5281/http-bind",
"libre-chat.net": "https://libre-chat.net:5280/bosh",
"snopyta.org": "https://xmpp.snopyta.org/http-bind",
"wiuwiu.de": "https://wiuwiu.de/http-bind"
};
function setServiceUrl(_converse, service_url) {
if(service_url.startsWith("wss:")) {
_converse.api.settings.set("websocket_url", service_url);
} else {
_converse.api.settings.set("bosh_service_url", service_url);
}
sessionStorage.setItem('account-migrator-service-url', service_url);
}
function fetchRoster(_converse, env) {
return new Promise(function(resolve, reject) {
let rosterRequest = env.$iq({
type: "get"
})
.c("query", {
xmlns: "jabber:iq:roster"
});
let id = _converse.connection.sendIQ(rosterRequest,
(stanza) => resolve(env.sizzle("iq>query", stanza)[0]),
(err) => reject(err)
);
console.log("Fetching roster with id " + id);
if (!id) {
reject("no id");
}
});
}
function uploadRoster(_converse, env, roster, notify) {
console.log("Uploading roster...")
let {
Strophe,
$iq,
$pres
} = env;
return new Promise(function(resolve, reject) {
Strophe.forEachChild(roster, "item", (roster_item) => {
let contact_jid = roster_item.getAttribute("jid");
let contact_name = roster_item.getAttribute("name");
let old_sub_state = roster_item.getAttribute("subscription");
if (notify) {
if (old_sub_state == "both" || old_sub_state == "from") {
// We had an incoming
let preapproval = $pres({
to: contact_jid,
type: "subscribed"
});
_converse.connection.send(preapproval);
}
if (old_sub_state == "both" || old_sub_state == "to") {
// We had an outgoing
let sub = $pres({
to: roster_item.getAttribute("jid"),
type: "subscribe"
});
_converse.connection.send(sub);
let roster_name = $iq({
type: "set",
id: "import-contact-name"
})
.c("query", {
xmlns: "jabber:iq:roster"
})
.c("item", {
jid: contact_jid,
name: contact_name
});
_converse.connection.send(roster_name);
}
} else {
// No subscription requests/approvals, only add roster entry
let roster_add = $iq({
type: "set",
id: "import-contact"
})
.c("query", {
xmlns: "jabber:iq:roster"
})
.c("item", {
jid: contact_jid,
name: contact_name
});
_converse.connection.send(roster_add);
}
});
resolve("jabber:iq:roster");
});
}
function uploadVCard(_converse, env, vCard) {
console.log("Uploading vCard...");
return new Promise(function(resolve, reject) {
let request = env.$iq({
type: "set"
})
.cnode(vCard);
_converse.connection.sendIQ(request,
(result) => resolve("vcard-temp"),
(err) => reject(err)
);
});
}
function fetchVCard(_converse, env) {
return new Promise(function(resolve, reject) {
let rosterRequest = env.$iq({
type: "get"
})
.c("vCard", {
xmlns: "vcard-temp"
});
_converse.connection.sendIQ(rosterRequest,
(stanza) => resolve(env.sizzle("iq>vCard", stanza)[0]),
(err) => {
let errName = err.querySelector("error :first-child").nodeName;
reject(errName);
}
);
});
}
function uploadBookmarks(_converse, env, bookmarks) {
console.log("Uploading bookmarks...");
return new Promise(function(resolve, reject) {
let request = env.$iq({
type: "set"
})
.cnode(bookmarks);
_converse.connection.sendIQ(request,
(result) => resolve("jabber:iq:private"),
(err) => reject(err)
);
});
}
function fetchBookmarks(_converse, env) {
// Legacy bookmarks, lowest common denominator, likely to be covered by
// compat layers for some time
return new Promise(function(resolve, reject) {
let pepListRequest = env.$iq({
type: "get"
}).c("query", {
xmlns: "jabber:iq:private"
}).c("storage", {
xmlns: "storage:bookmarks"
});
_converse.connection.sendIQ(pepListRequest,
(stanza) => resolve(env.sizzle("iq>query", stanza)[0]),
(err) => {
let errName = err.querySelector("error :first-child").nodeName;
reject(errName);
}
);
});
}
function fetchData(_converse, env) {
console.log("Fetching data...");
return Promise.allSettled([
fetchRoster(_converse, env),
fetchVCard(_converse, env),
fetchBookmarks(_converse, env),
]);
}
const NS = {
xrd: 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
roster: 'jabber:iq:roster',
disco_items: 'http://jabber.org/protocol/disco#items',
disco_info: 'http://jabber.org/protocol/disco#info',
dataforms: 'jabber:x:data',
pubsub: 'http://jabber.org/protocol/pubsub',
pubsub_owner: 'http://jabber.org/protocol/pubsub#owner',
avatar_metadata: 'urn:xmpp:avatar:metadata',
avatar_data: 'urn:xmpp:avatar:data',
nickname: 'http://jabber.org/protocol/nick',
vcard4: 'urn:ietf:params:xml:ns:vcard-4.0',
mam: 'urn:xmpp:mam:2',
forward: 'urn:xmpp:forward:0',
};
function nsResolver(prefix) {
return NS[prefix] || null;
}
function parseXPath(elem, xpath, result)
{
if (result === undefined)
result = XPathResult.FIRST_ORDERED_NODE_TYPE;
const value = elem.getRootNode().evaluate(xpath, elem, nsResolver, result, null);
if (result == XPathResult.FIRST_ORDERED_NODE_TYPE)
return value.singleNodeValue;
return value;
}
function getServiceURL(jid) {
const [nodepart, domainpart] = jid.split('@', 2);
if(static_service_urls.hasOwnProperty(domainpart)) {
return Promise.resolve(static_service_urls[domainpart]);
}
let xrdPromise = new Promise((resolve, reject) => {
const url = 'https://' + domainpart + '/.well-known/host-meta';
const xhr = new XMLHttpRequest();
xhr.onabort = reject;
xhr.onerror = reject;
xhr.overrideMimeType("text/xml");
xhr.onload = function(evt) {
if(evt.target.status != 200) {
reject();
return;
}
const xml = evt.target.responseXML;
if(!xml) {
reject();
return;
}
const links = parseXPath(xml, './xrd:XRD/xrd:Link', XPathResult.ORDERED_NODE_ITERATOR_TYPE);
let bosh_service = null;
let ws_service = null;
while (true) {
const link = links.iterateNext();
if (!link)
break;
const rel = link.getAttributeNS(null, 'rel');
if (rel == 'urn:xmpp:alt-connections:xbosh')
bosh_service = link.getAttributeNS(null, 'href');
else if (rel == 'urn:xmpp:alt-connections:websocket')
ws_service = link.getAttributeNS(null, 'href');
}
if(ws_service || bosh_service) {
console.log('bosh_service', bosh_service);
console.log('ws_service', ws_service);
resolve(ws_service || bosh_service);
} else {
reject();
}
};
xhr.open('GET', url);
xhr.send();
});
let jrdPromise = new Promise((resolve, reject) => {
const url = 'https://' + domainpart + '/.well-known/host-meta.json';
const xhr = new XMLHttpRequest();
xhr.onabort = reject;
xhr.onerror = reject;
xhr.overrideMimeType("application/json");
xhr.onload = function(evt) {
if(evt.target.status != 200) {
reject();
return;
}
let jrd = null;
switch(evt.target.responseType) {
case "json":
jrd = evt.target.response;
break;
case "": // "An empty responseType string is the same as "text", the default type."
case "text":
jrd = JSON.parse(evt.target.response);
break;
default:
return reject();
}
let bosh_service = null;
let ws_service = null;
if(jrd.links) {
for(link of jrd.links) {
if (link.rel == 'urn:xmpp:alt-connections:xbosh')
bosh_service = link.href;
else if (link.rel == 'urn:xmpp:alt-connections:websocket')
ws_service = link.href;
}
}
if(ws_service || bosh_service) {
console.log('bosh_service', bosh_service);
console.log('ws_service', ws_service);
resolve(ws_service || bosh_service);
} else {
reject();
}
};
xhr.open('GET', url);
xhr.send();
});
return Promise.any([xrdPromise, jrdPromise]).catch(function (err) {
// If we wanted a fallback, here is a good place to return it
throw "XEP-0156 lookup failed.";
});
}
document.getElementById("copy-debug-info").addEventListener("click", function() {
const output_el_children = document.querySelector('.output').children;
let debug_output = ""
for (const element in output_el_children) {
const element_text = output_el_children[element].textContent
if (element_text !== undefined) {
debug_output += element_text + "\n"
}
}
navigator.clipboard.writeText(debug_output);
})
window.addEventListener('converse-loaded', function(e) {
let converse = e.detail.converse;
let {
Strophe
} = converse.env;
var result = null;
var resultJid = null;
var migrationAccount = null;
var migrationStatus = null;
console.log("Converse loaded!", converse);
converse.plugins.add('account-migrator', {
initialize: function() {
const {
_converse
} = this;
const log = _converse.log;
const body = document.querySelector('body');
const logout_form = document.querySelector('form.logout');
const output_el = document.querySelector('.output');
const account_management_card = document.getElementById("account-management-card");
const download_panel = document.querySelector('.download');
const conn_status_panel = document.querySelector('#connection-status-panel');
const login_form = document.querySelector('form.login');
const secondary_login_form = document.querySelector('form.login-secondary');
const new_account_hint = document.getElementById('new-account-hint')
login_form.addEventListener('submit', ev => {
ev.preventDefault();
const form_data = new FormData(ev.target);
new_account_hint.style.display = 'none'
account_management_card.classList.remove("d-none")
login_form.style.display = 'none';
logout_form.style.display = 'block';
hideErrorMessage();
window.scrollTo(0, 0);
getServiceURL(form_data.get('jid')).then(
(service_url) => {
console.log("Discovered service URL:", service_url);
setServiceUrl(_converse, service_url);
setTimeout(
()=>_converse.api.user.login(form_data.get('jid'), form_data.get('password')),
0
);
},
(err) => {
showErrorMessage(
"Your account is not currently compatible with this service.",
err || "The chosen server does not advertise any suitable connection methods as described in XEP-0156."
);
account_management_card.classList.add("d-none")
}
);
});
function updateLoginForm() {
let connected = _converse.connection && _converse.connection.connected;
if(connected || migrationStatus != "pending")
login_form.style.display = connected ? 'none' : 'block';
logout_form.style.display = connected ? 'block' : 'none';
download_panel.style.display = (result && result.status == "fulfilled") ? 'block' : 'none';
conn_status_panel.style.display = connected ? "block" : "none";
if (connected) {
document.getElementById("conn-status-text").innerText = "Logged in as " + Strophe.getBareJidFromJid(_converse.connection.jid);
} else {
document.getElementById("conn-status-text").innerText = "Disconnected";
}
}
function showStatus() {
let status_panel = document.getElementById("download-status-panel");
status_panel.style.display = "block";
}
function hideStatus() {
let status_panel = document.getElementById("download-status-panel");
status_panel.style.display = "none";
}
function showErrorMessage(simple_err, expert_err) {
console.log("Showing error:", simple_err, expert_err);
let err_box = document.getElementById("error-alert");
err_box.querySelector("p").innerText = simple_err;
err_box.style.display = "block";
let expert_info_container = err_box.querySelector(".expert-info-container");
if (expert_err) {
document.getElementById("expert-info").innerText = expert_err;
expert_info_container.style.display = "block";
} else {
expert_info_container.style.display = "none";
}
}
function hideErrorMessage() {
let err_box = document.getElementById("error-alert");
err_box.style.display = "none";
}
logout_form.addEventListener('submit', ev => {
ev.preventDefault();
migrationStatus = null;
migrationAccount = null;
result = null;
resultJid = null;
hideErrorMessage();
_converse.api.user.logout();
document.getElementById("loading-spinner").classList.remove("d-none")
document.getElementById("account-management-card").classList.add("d-none")
});
_converse.api.listen.on('disconnected', () => {
output_el.innerHTML = '';
updateLoginForm();
console.log("disconnected");
if (migrationAccount && migrationStatus == "pending") {
let service_url = migrationAccount.options.service_url;
console.log("Migration requested. Logging into new account...");
setServiceUrl(_converse, service_url);
setTimeout(
()=>_converse.api.user.login(migrationAccount.jid, migrationAccount.password),
0
);
}
let disconnect_feedback = _converse.connfeedback.get("message");
if (disconnect_feedback) {
showErrorMessage(disconnect_feedback);
}
});
_converse.api.listen.on('connected', () => {
document.getElementById("loading-spinner").classList.add("d-none")
output_el.innerHTML = '';
updateLoginForm();
console.log("connected");
if (!result) {
// Start data fetching
let jid = _converse.api.user.jid();
let host = Strophe.getDomainFromJid(jid);
let username = Strophe.getNodeFromJid(jid);
resultJid = Strophe.getBareJidFromJid(jid);
result = fetchData(_converse, converse.env);
showStatus();
result.then(hideStatus);
result.then(function(results) {
let ok_results = results.filter(result => result.status == "fulfilled");
if (ok_results.length == 0) {
throw "no-results";
}
return ok_results.map(result => result.value);
}).catch(function(err) {
console.log("results failed");
console.log("Failed to fetch account data:", err);
showErrorMessage("Failed to export any data.", err);
_converse.api.user.logout();
throw (err);
}).then(function(results) {
let exported_data_list_el = document.getElementById("exported-data-list");
exported_data_list_el.innerHTML = "";
for (element of results) {
let xmlns = element.getAttribute("xmlns");
if (data_names.hasOwnProperty(xmlns)) {
let li = document.createElement("li");
li.innerText = data_names[xmlns];
exported_data_list_el.appendChild(li);
}
}
return results;
}).then(function(results) {
console.log("results fetched", result);
return new Blob([].concat(
[
"<?xml version='1.0' encoding='UTF-8'?>\n",
"<server-data xmlns='urn:xmpp:pie:0'>\n",
" <host jid='" + Strophe.xmlescape(host) + "'>\n",
" <user name='" + Strophe.xmlescape(username) + "'>\n",
],
results.map((element) => (" " + Strophe.serialize(element) + "\n")), [
" </user>\n",
" </host>\n",
"</server-data>\n",
]
), {
type: "application/xpie+xml"
});
}).then(blob => {
let a = document.getElementById("download-button");
a.setAttribute("href", URL.createObjectURL(blob));
a.setAttribute("download", "account-data.xml");
document.getElementById("exported-jid").innerText = resultJid;
download_panel.style.display = 'block';
return blob;
});
result.then(function(results) {
secondary_login_form.addEventListener("submit", function(ev) {
ev.preventDefault();
const form_data = new FormData(ev.target);
let modal = bootstrap.Modal.getInstance(document.getElementById("migrate-modal"));
getServiceURL(form_data.get("jid")).then(function (service_url) {
ev.target.style.display = 'none';
migrationStatus = "pending";
migrationAccount = {
jid: form_data.get("jid"),
password: form_data.get("password"),
options: {
notify_contacts: form_data.has("notify-contacts"),
service_url: service_url,
},
};
console.log("Logging out of first account...");
_converse.api.user.logout();
modal.hide();
}).catch(function (err) {
showErrorMessage("Failed to connect to new account", err);
modal.hide();
});
});
});
} else if (migrationStatus == "pending") {
console.log("Second account connected ok");
result.then(function(results) {
console.log("Starting migration...");
let uploadResults = [];
for (result of results.filter(result => result.status == "fulfilled")) {
let element = result.value;
switch (element.getAttribute("xmlns")) {
case "jabber:iq:roster":
let rosterPromise = uploadRoster(_converse, converse.env, element, migrationAccount.options.notify_contacts);
uploadResults.push(rosterPromise.catch(err=>Promise.reject("jabber:iq:roster")));
break;
case "vcard-temp":
let vCardPromise = uploadVCard(_converse, converse.env, element);
uploadResults.push(vCardPromise.catch(err=>Promise.reject("vcard-temp")));
break;
case "jabber:iq:private":
let bookmarksPromise = uploadBookmarks(_converse, converse.env, element);
uploadResults.push(bookmarksPromise.catch(err=>Promise.reject("jabber:iq:private")));
break;
}
}
return Promise.allSettled(uploadResults);
}).then(function(uploadResults) {
let okUploads = uploadResults
.filter(result=>result.status=="fulfilled")
.map(result=>result.value);
let failedUploads = uploadResults
.filter(result=>result.status=="rejected")
.map(result=>result.reason);
console.log("Migration complete ("+ okUploads.length + "/" + uploadResults.length + " ok)");
if(okUploads.length > 0) {
document.getElementById("migration-status-success").style.display = "block";
document.getElementById("migrate-button").style.display = "none";
download_panel.style.display = "block";
} else {
showErrorMessage("Account migration failed.");
}
if(failedUploads.length > 0) {
document.getElementById("migration-status-warning").style.display = "block";
let failedMigrationList = document.getElementById("migration-failure-list");
failedMigrationList.innerHTML = "";
for (xmlns of failedUploads) {
if (data_names.hasOwnProperty(xmlns)) {
let li = document.createElement("li");
li.innerText = data_names[xmlns];
failedMigrationList.appendChild(li);
}
}
}
});
}
});
_converse.api.listen.on('connectionInitialized', () => {
updateLoginForm();
console.log("connected!", _converse.connection.connected, _converse, "and", converse);
const xmlInput = _converse.connection.xmlInput;
const xmlOutput = _converse.connection.xmlOutput;
_converse.connection.xmlInput = function(body) {
const el = document.createElement('code');
el.className = "d-block pt-2 pb-2 border-top";
el.textContent = body.outerHTML;
output_el.insertAdjacentElement('beforeend', el);
xmlInput.apply(this, arguments);
};
_converse.connection.xmlOutput = function(body) {
const el = document.createElement('code');
el.className = "d-block pt-2 pb-2 border-top";
el.textContent = body.outerHTML;
output_el.insertAdjacentElement('beforeend', el);
xmlOutput.apply(this, arguments);
};
});
}
});
converse.initialize({
// Provide a dummy BOSH URL (it's not expected to work, but
// Converse.js won't initialize without one. We later discover
// it dynamically and update before we call login().
bosh_service_url: sessionStorage.getItem('account-migrator-service-url') || "https://localhost:5281/http-bind",
authentication: 'login',
auto_reconnect: false,
persistent_store: 'sessionStorage',
discover_connection_methods: false,
whitelisted_plugins: ['account-migrator'],
blacklisted_plugins: [
'converse-vcard',
'converse-muc',
'converse-bookmarks',
'converse-carbons',
],
priority: -1,
debug: true
});
});