comparison mod_admin_web2/admin_web2/www_files/js/main.js @ 6520:7eb114975cec

mod_admin_web2: Fork of admin_web for prosody 13.0. It is a fork because it changes access rights and might be insecure. Mark as alpha.
author Menel <menel@snikket.de>
date Fri, 24 Apr 2026 12:40:13 +0200
parents mod_admin_web/admin_web/www_files/js/main.js@1630c6ed3814
children
comparison
equal deleted inserted replaced
6519:01d8bfbfc435 6520:7eb114975cec
1 var BOSH_SERVICE = '/http-bind/';
2 var show_log = false;
3
4 Strophe.addNamespace('C2SSTREAM', 'http://prosody.im/streams/c2s');
5 Strophe.addNamespace('S2SSTREAM', 'http://prosody.im/streams/s2s');
6 Strophe.addNamespace('ADMINSUB', 'http://prosody.im/adminsub');
7 Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
8
9 var localJID = null;
10 var connection = null;
11
12 var adminsubHost = null;
13 var adhocControl = new Adhoc('#adhocDisplay', function() {});
14
15 function log(msg) {
16 var entry = $('<div></div>').append(document.createTextNode(msg));
17 $('#log').append(entry);
18 }
19
20 function rawInput(data) {
21 log('RECV: ' + data);
22 }
23
24 function rawOutput(data) {
25 log('SENT: ' + data);
26 }
27
28 function _cbNewS2S(e) {
29 var items, item, entry, tmp, retract, id, jid, infos, info, metadata;
30 items = e.getElementsByTagName('item');
31 for (i = 0; i < items.length; i++) {
32 item = items[i];
33 id = item.attributes.getNamedItem('id').value;
34 jid = item.getElementsByTagName('session')[0].attributes.getNamedItem('jid').value;
35 infos = item.getElementsByTagName('info');
36
37 entry = $('<li id="' + id + '">' + jid + '</li>');
38 if (tmp = item.getElementsByTagName('encrypted')[0]) {
39 if (tmp.getElementsByTagName('valid')[0]) {
40 entry.append('<img src="images/secure.png" title="encrypted (certificate valid)" alt=" (secure) (encrypted)" />');
41 } else {
42 entry.append('<img src="images/encrypted.png" title="encrypted (certificate invalid)" alt=" (encrypted)" />');
43 }
44 }
45 if (item.getElementsByTagName('compressed')[0]) {
46 entry.append('<img src="images/compressed.png" title="compressed" alt=" (compressed)" />');
47 }
48 metadata = $('<ul/>').css('display', 'none');
49 entry.on('click', function() {
50 $(this).find("ul").slideToggle();
51 });
52 metadata.appendTo(entry);
53 for (j = 0; j < infos.length; j++) {
54 info = infos[j];
55 metadata.append('<li><b>' + info.attributes.getNamedItem('name').value + ':</b> ' + info.textContent + '</li>');
56 }
57 if (infos.length == 0)
58 metadata.append('<li>No information available</li>');
59
60 if (items[i].getElementsByTagName('out')[0]) {
61 entry.appendTo('#s2sout');
62 } else {
63 entry.appendTo('#s2sin');
64 }
65 }
66 retract = e.getElementsByTagName('retract')[0];
67 if (retract) {
68 id = retract.attributes.getNamedItem('id').value;
69 $('#' + id).remove();
70 }
71 return true;
72 }
73
74 function _cbNewC2S(e) {
75 var items, item, entry, retract, id, jid, infos, info, metadata;
76 items = e.getElementsByTagName('item');
77 for (i = 0; i < items.length; i++) {
78 item = items[i];
79 id = item.attributes.getNamedItem('id').value;
80 jid = item.getElementsByTagName('session')[0].attributes.getNamedItem('jid').value;
81 infos = item.getElementsByTagName('info');
82 entry = $('<li id="' + id + '">' + jid + '</li>');
83 if (item.getElementsByTagName('encrypted')[0]) {
84 entry.append('<img src="images/encrypted.png" title="encrypted" alt=" (encrypted)" />');
85 }
86 if (item.getElementsByTagName('compressed')[0]) {
87 entry.append('<img src="images/compressed.png" title="compressed" alt=" (compressed)" />');
88 }
89 metadata = $('<ul/>').css('display', 'none');
90 entry.on('click', function() {
91 $(this).find("ul").slideToggle();
92 });
93 metadata.appendTo(entry);
94 for (j = 0; j < infos.length; j++) {
95 info = infos[j];
96 metadata.append('<li><b>' + info.attributes.getNamedItem('name').value + ':</b> ' + info.textContent + '</li>');
97 }
98 if (infos.length == 0)
99 metadata.append('<li>No information available</li>');
100 entry.appendTo('#c2s');
101 }
102 retract = e.getElementsByTagName('retract')[0];
103 if (retract) {
104 id = retract.attributes.getNamedItem('id').value;
105 $('#' + id).remove();
106 }
107 return true;
108 }
109
110 function _cbAdminSub(e) {
111 var node = e.getElementsByTagName('items')[0].attributes.getNamedItem('node').value;
112 if (node == Strophe.NS.C2SSTREAM) {
113 _cbNewC2S(e);
114 } else if (node == Strophe.NS.S2SSTREAM) {
115 _cbNewS2S(e);
116 }
117
118 return true;
119 }
120
121 function onConnect(status) {
122 if (status == Strophe.Status.CONNECTING) {
123 log('Strophe is connecting.');
124 } else if (status == Strophe.Status.CONNFAIL) {
125 alert('Connection failed (Wrong host?)');
126 log('Strophe failed to connect.');
127 showConnect();
128 } else if (status == Strophe.Status.DISCONNECTING) {
129 log('Strophe is disconnecting.');
130 } else if (status == Strophe.Status.DISCONNECTED) {
131 log('Strophe is disconnected.');
132 showConnect();
133 } else if (status == Strophe.Status.AUTHFAIL) {
134 alert('Wrong username and/or password');
135 log('Authentication failed');
136 if (connection) {
137 connection.disconnect();
138 }
139 } else if (status == Strophe.Status.CONNECTED) {
140 log('Strophe is connected.');
141 connection.sendIQ($iq({to: connection.domain, type: 'get', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
142 .c('adminfor'), function(e) {
143 var items;
144 var domainpart = Strophe.getDomainFromJid(connection.jid);
145 items = e.getElementsByTagName('item');
146 if (items.length == 0) {
147 alert("You are not an administrator");
148 connection.disconnect();
149 return false;
150 }
151 for (i = 0; i < items.length; i++) {
152 var host = $(items[i]).text();
153 $('<option/>').text(host).prop('selected', host == domainpart).appendTo('#host');
154 }
155 showDisconnect();
156 adminsubHost = $('#host').val();
157 adhocControl.checkFeatures(adminsubHost,
158 function () { adhocControl.getCommandNodes(function (result) { $('#adhocDisplay').empty(); $('#adhocCommands').html(result); }) },
159 function () { $('#adhocCommands').empty(); $('#adhocDisplay').html('<p>This host does not support commands</p>'); });
160 connection.addHandler(_cbAdminSub, Strophe.NS.ADMINSUB + '#event', 'message');
161 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
162 .c('subscribe', {node: Strophe.NS.C2SSTREAM}));
163 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
164 .c('subscribe', {node: Strophe.NS.S2SSTREAM}));
165 connection.sendIQ($iq({to: adminsubHost, type: 'get', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
166 .c('items', {node: Strophe.NS.S2SSTREAM}), _cbNewS2S);
167 connection.sendIQ($iq({to: adminsubHost, type: 'get', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
168 .c('items', {node: Strophe.NS.C2SSTREAM}), _cbNewC2S);
169 });
170 }
171 }
172
173 function showConnect() {
174 $('#login').show();
175 $('#menu').hide();
176 $('#main').hide();
177 $('#s2sin').empty();
178 $('#s2sout').empty();
179 $('#c2s').empty();
180 $('#host').empty();
181 }
182
183 function showDisconnect() {
184 $('#s2sList').hide();
185 $('#c2sList').hide();
186 $('#login').hide();
187
188 $('#menu').show();
189 $('#main').show();
190 $('#adhoc').show();
191 }
192
193 $(document).ready(function () {
194 connection = new Strophe.Connection(BOSH_SERVICE);
195 if (show_log) {
196 $('#log_container').show();
197 connection.rawInput = rawInput;
198 connection.rawOutput = rawOutput;
199 }
200
201 $("#log_toggle").click(function () {
202 $("#log").toggle();
203 });
204
205 $('#cred').on('submit', function (event) {
206 var button = $('#connect').get(0);
207 var jid = $('#jid');
208 var pass = $('#pass');
209 localJID = jid.get(0).value;
210
211 $('#log').empty();
212 connection.connect(localJID, pass.get(0).value, onConnect);
213 event.preventDefault();
214 });
215
216 $('#logout').click(function (event) {
217 connection.disconnect();
218 event.preventDefault();
219 });
220
221 $('#adhocMenu, #serverMenu, #clientMenu').click(function (event) {
222 event.preventDefault();
223 var tab = $(this).attr('href');
224 $('#main > div').hide();
225 $(tab).fadeIn('fast');
226 });
227
228 $('#host').on('change', function (event) {
229 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
230 .c('unsubscribe', {node: Strophe.NS.C2SSTREAM}));
231 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
232 .c('unsubscribe', {node: Strophe.NS.S2SSTREAM}));
233 adminsubHost = $(this).val();
234 adhocControl.checkFeatures(adminsubHost,
235 function () { adhocControl.getCommandNodes(function (result) { $('#adhocDisplay').empty(); $('#adhocCommands').html(result); }) },
236 function () { $('#adhocCommands').empty(); $('#adhocDisplay').html('<p>This host does not support commands</p>'); });
237 $('#s2sin').empty();
238 $('#s2sout').empty();
239 $('#c2s').empty();
240 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
241 .c('subscribe', {node: Strophe.NS.C2SSTREAM}));
242 connection.send($iq({to: adminsubHost, type: 'set', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
243 .c('subscribe', {node: Strophe.NS.S2SSTREAM}));
244 connection.sendIQ($iq({to: adminsubHost, type: 'get', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
245 .c('items', {node: Strophe.NS.S2SSTREAM}), _cbNewS2S);
246 connection.sendIQ($iq({to: adminsubHost, type: 'get', id: connection.getUniqueId()}).c('adminsub', {xmlns: Strophe.NS.ADMINSUB})
247 .c('items', {node: Strophe.NS.C2SSTREAM}), _cbNewC2S);
248 });
249 });
250
251 window.onunload = window.onbeforeunload = function() {
252 if (connection) {
253 connection.disconnect();
254 }
255 }