XMPP invitation landing page, without javascript
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
(function() {
	'use strict';

	var initialized = false;
	var i18n;

	// i18n key prefix for MUC ("muc.") or 1:1 chat ("chat.")
	var key_prefix;
	var display_data = null;

	function show_clients(client_array) {
		var list = document.getElementById('client_list');
		for (var id = 0; id < client_array.length; id++) {
			var item = document.createElement('li');
			item.innerHTML = client_array[id];
			list.appendChild(item);
		}
	}

	function load_clients(url) {
		var request = new XMLHttpRequest();
		request.open('GET', url);
		request.onreadystatechange = function () {
			if (request.readyState === 4) {
				if (request.status === 200 || (isLocalFileRequest(url) && request.responseText.length > 0)) {
					show_clients(JSON.parse(request.responseText));
				}
			}
		};
		request.send(null);
	}

	function load_hash() {
		var muc = false;
		key_prefix = "chat.";
		var xmpp_uri = window.location.search || window.location.hash;
		xmpp_uri = decodeURIComponent(xmpp_uri.substring(xmpp_uri.indexOf('#') + 1, xmpp_uri.length));
		try {
			base_decoded = window.atob(xmpp_uri);
			if (base_decoded.search('@') >= 0)
				xmpp_uri = base_decoded;
		} catch (err) {
			// ignore error, JID wasn't base64 encoded
		}
		if (xmpp_uri.search("\\?join") >= 0) {
			muc = true;
			key_prefix = "muc.";
		}

		// TODO: proper error checking / display / Creation of invitations
		if (xmpp_uri.search("@") <= 0) return {xmpp_uri:xmpp_uri, xmpp_uri_encoded:xmpp_uri, name: xmpp_uri};

		var xmpp_params = {};
		var name = xmpp_uri.split("@")[0];
		xmpp_params["name"] = name.charAt(0).toUpperCase() + name.slice(1);

		var xmpp_uri_parts = xmpp_uri.split("?");

		if (xmpp_uri_parts.length > 1) {
			let parameter, parameters = xmpp_uri_parts[1].split(";")
			for (parameter of parameters) {
				let key_value = parameter.split("=")
				xmpp_params[key_value[0]] = key_value.length > 1 ? key_value[1] : "";
			}
		}

		xmpp_uri_parts[0] = encodeURIComponent(xmpp_uri_parts[0]) // URL-encode the JID only
		var xmpp_uri_encoded = xmpp_uri_parts.join("?");

		return {xmpp_uri: xmpp_uri, xmpp_uri_encoded: xmpp_uri_encoded, name: xmpp_params["name"]};
	}

	function translate_ui() {
		// translation
		document.title = i18n.text(key_prefix + 'title',  display_data);
		// MUC/chat specific
		['heading', 'button'].forEach(function(id) {
			document.getElementById(id).innerHTML = i18n.text(key_prefix + id, display_data);
		});
		// and agnostic
		['clients', 'recommend', 'checkfulllist', 'xmppis'].forEach(function(id) {
			document.getElementById(id).innerHTML = i18n.text(id, display_data);
		});
	}

	function rehash() {
		display_data = load_hash();
		document.getElementById('button').href = "xmpp:" + display_data.xmpp_uri_encoded;
		document.getElementById('url_in').value = "xmpp:" + display_data.xmpp_uri;
		translate_ui();
	}

	function createQR() {
		display_data = load_hash();
		new QRCode(document.getElementById("qrcode"), "xmpp:" + display_data.xmpp_uri);
	}

	function load_done() {
		if (initialized) return;
		initialized = true;

		// load i18n and perform translation
		i18n = new I18nText({path: 'lang'});
		i18n.once(I18nText.event.LOCALE_CHANGE, function (data) {
			rehash();
		});

		var preferredLocale, setLocale = false;
		for (preferredLocale of navigator.languages) {
			if (supportedLocales.includes(preferredLocale)) {
				i18n.setLocale(preferredLocale);
				setLocale = true;
				break;
			}
		}
		if (!setLocale) {
			i18n.setLocale(defaultLocale);
		}


		// functionality
		if (navigator.userAgent.indexOf("Android") >= 0) {
			load_clients("clients_Android.json")
		}
		else if (navigator.userAgent.indexOf("Linux") >= 0)  {
			load_clients("clients_Linux.json");
			createQR();
		}
                else if (navigator.userAgent.indexOf("iPhone") >= 0)  {
                        load_clients("clients_iOS.json")
                }
		else {
			load_clients("clients_Linux.json");
			createQR();
		}

		window.addEventListener("hashchange", rehash, false);
		document.getElementById("url_in").addEventListener("focus", function(event) {
			event.target.select();
		});
	}

	// Wait for the DOM to be ready
	document.addEventListener('DOMContentLoaded', load_done, false);
	document.onreadystatechange = function() {
		if (document.readyState === 'interactive') {
			load_done();
		}
	};
})();