1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
4 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
15 var entry = $('<div></div>').append(document.createTextNode(msg));
16 $('#log').append(entry);
19 function rawInput(data) {
23 function rawOutput(data) {
27 function onConnect(status) {
28 if (status == Strophe.Status.CONNECTING) {
29 log('Strophe is connecting.');
30 } else if (status == Strophe.Status.CONNFAIL) {
31 log('Strophe failed to connect.');
33 } else if (status == Strophe.Status.DISCONNECTING) {
34 log('Strophe is disconnecting.');
35 } else if (status == Strophe.Status.DISCONNECTED) {
36 log('Strophe is disconnected.');
38 } else if (status == Strophe.Status.AUTHFAIL) {
39 log('Authentication failed');
41 connection.disconnect();
43 } else if (status == Strophe.Status.CONNECTED) {
44 log('Strophe is connected.');
45 adhoc_status.queryJID = connection.domain;
46 $('#queryJID').val(adhoc_status.queryJID);
52 function addNote(elem, text, type) {
56 text = text.replace(/\n/g, "<br/>");
57 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
60 function addForm(elem, x) {
61 var form = $("<form/>");
62 form.submit(function(event){event.preventDefault();});
63 var fieldset = $("<fieldset/>");
64 form.append(fieldset);
65 if ($(x).find("title").length > 0)
66 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
67 if ($(x).find("instructions").length > 0)
68 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
69 $(x).find("field").each(function() {
71 var type = $(this).attr("type");
72 if($(this).attr("label")) {
73 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
74 $("<br/>").appendTo(fieldset);
78 item = $("<input type='hidden'/>");
81 item = $("<input type='checkbox'/>");
84 item = $("<textarea rows='10' cols='70'/>");
87 item = $("<input type='text'/>");
90 item = $("<input type='text'/>").attr("readonly",true);
93 item = $("<textarea rows='10' cols='70'/>");
96 item = $("<input type='text'/>");
99 item = $("<select multiple='multiple'/>");
100 $(this).find("option").each(function() {
101 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
105 item = $("<select/>");
106 $(this).find("option").each(function() {
107 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
111 item = $("<input type='password'/>");
114 item = $("<input/>");
116 item.addClass("df-item");
117 if ($(this).find("value").length > 0) {
119 if ((type == "text-multi") || (type == "jid-multi")) {
121 $(this).find("value").each(function() {
122 value = value + $(this).text() + "\n";
125 } else if (type == "list-multi") {
127 $(this).find("value").each(function() {
128 value[value.length] = $(this).text();
131 item.val($(this).find("value").text());
134 if ($(x).attr("type") == "result")
135 item.attr("readonly", true);
136 if ($(this).attr("var")) {
137 item.attr("name", $(this).attr("var"));
138 item.attr("id", $(this).attr("var"));
140 fieldset.append(item);
141 if (type != "hidden")
142 fieldset.append("<br/>");
144 $(elem).append(form);
147 function serializeToDataform(form, st) {
148 st.c("x", {"xmlns": "jabber:x:data", "type": "submit"});
149 $(form).find(".df-item").each(function(){
150 st.c("field", {"var": $(this).attr("name")});
151 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
152 for (var i = 0; i < this.options.length; i++)
153 if (options[i].selected)
154 st.c("value").t(options[i]).up();
155 } else if (this.nodeName.toLowerCase() == "textarea") {
156 var sp_value = this.value.split(/\r?\n|\r/g);
157 for(var i = 0; i < sp_value.length; i++)
158 st.c("value").t(sp_value[i]).up();
159 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
161 st.c("value").t("1");
163 st.c("value").t("0");
166 // if this has value then
167 st.c("value").t($(this).val()).up();
174 function displayResult(result) {
175 var status = $(result).find("command").attr("status");
177 $("#output").empty();
178 $(result).find("command > *").each(function(index, e) {
179 if ($(e).is("note")) {
180 addNote("#output", $(e).text(), $(e).attr("type"));
181 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
182 addForm("#output", e);
185 if (status == "executing") {
186 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
187 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
188 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
189 "<input type='button' id='executeButton' value='Execute'/>"+
190 "<input type='button' id='cancelButton' value='Cancel'/>");
191 for (kind in ['prev', 'next', 'complete']) {
192 if ($(result).find('actions ' + kind).length > 0)
193 $('#' + kind + 'Button').attr("disabled", "false");
195 $('#executeButton').bind("click", function() {
196 var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
197 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "execute" });
198 serializeToDataform($('form'), execIQ);
199 connection.sendIQ(execIQ, displayResult);
202 $('#cancelButton').bind("click", function() {
203 cancelCommand(displayResult);
206 $('#queryForm').unbind('submit');
207 $('#queryForm').bind('submit', function (event) {
208 cancelCommand(function(result) {
209 $('#queryForm').unbind('submit');
210 $('#queryForm').bind('submit', function (event) {
211 adhoc_status.queryJID = $('#queryJID').val();
213 event.preventDefault();
216 adhoc_status.queryJID = $('#queryJID').val();
218 event.preventDefault();
222 input = $("<input type='button' value='Start over'/>").bind("click", function() {
223 $('#output').empty();
224 adhoc_status.sessionid = null;
225 adhoc_status.cmdNode = null;
228 $("#output").append(input);
232 function runCommand(event) {
233 adhoc_status.cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
234 var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
235 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: "execute" });
236 connection.sendIQ(execIQ, function(result) {
237 adhoc_status.sessionid = $(result).find("command").attr("sessionid");
238 displayResult(result);
240 event.preventDefault();
243 function cancelCommand(callback) {
244 var cancelIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
245 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "cancel" });
246 adhoc_status.cmdNode = null
247 adhoc_status.sessionid = null;
248 connection.sendIQ(cancelIQ, callback);
251 function getCommandNodes() {
252 var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
253 connection.sendIQ(nodesIQ, function(result) {
254 $('#output').append("<ul id='items'></ul>");
255 $(result).find("item").each(function(index, e) {
256 link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
257 item = $("<li></li>").append(link);
258 $("#items").append(item);
263 function checkFeatures() {
264 featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
265 $('#output').empty();
266 connection.sendIQ(featureIQ,
267 function(result) { /* Callback */
268 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
269 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
272 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
275 function(result) { /* Errback */
276 $('#output').append("<p>Couldn't get list of supported features</p>");
281 function showConnect() {
283 var pass = $('#pass');
284 var button = $('#connect').get(0);
286 button.value = 'connect';
290 $('#cred label').show();
291 $('#cred br').show();
292 $('#output').empty();
295 function showDisconnect() {
297 var pass = $('#pass');
298 var button = $('#connect').get(0);
300 button.value = 'disconnect';
303 $('#cred label').hide();
304 $('#cred br').hide();
307 $(document).ready(function () {
308 connection = new Strophe.Connection(BOSH_SERVICE);
310 $('#log_container').show();
311 connection.rawInput = rawInput;
312 connection.rawOutput = rawOutput;
315 $("#log_toggle").click(function () {
319 $('#cred').bind('submit', function (event) {
320 var button = $('#connect').get(0);
322 var pass = $('#pass');
323 localJID = jid.get(0).value;
325 if (button.value == 'connect') {
328 connection.connect(localJID,
332 connection.disconnect();
334 event.preventDefault();
337 $('#queryForm').bind('submit', function (event) {
338 adhoc_status.queryJID = $('#queryJID').val();
340 event.preventDefault();
344 window.onunload = window.onbeforeunload = function() {
346 connection.disconnect();