1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
3 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
11 var entry = $('<div></div>').append(document.createTextNode(msg));
12 $('#log').append(entry);
15 function rawInput(data) {
19 function rawOutput(data) {
23 function onConnect(status) {
24 if (status == Strophe.Status.CONNECTING) {
25 log('Strophe is connecting.');
26 } else if (status == Strophe.Status.CONNFAIL) {
27 log('Strophe failed to connect.');
29 } else if (status == Strophe.Status.DISCONNECTING) {
30 log('Strophe is disconnecting.');
31 } else if (status == Strophe.Status.DISCONNECTED) {
32 log('Strophe is disconnected.');
34 } else if (status == Strophe.Status.AUTHFAIL) {
35 log('Authentication failed');
37 connection.disconnect();
39 } else if (status == Strophe.Status.CONNECTED) {
40 log('Strophe is connected.');
45 function displayResult(result) {
46 var status = $(result).find("command").attr("status");
48 $("#output *").remove();
49 $(result).find("note").each(function(index, e) {
50 var type = $(e).attr("type");
54 $("#output").append("<p class='" + type + "Note'>" + $(e).text() + "</p>");
56 if (status == "executing") {
57 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
58 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
59 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
60 "<input type='button' id='executeButton' value='Execute'/>"+
61 "<input type='button' id='cancelButton' value='Cancel'/>");
62 for (kind in ['prev', 'next', 'complete']) {
63 if ($(result).find('actions ' + kind).length > 0)
64 $('#' + kind + 'Button').attr("disabled", "false");
66 $('#cancelButton').bind("click", function() {
67 var cancelIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
68 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
71 connection.sendIQ(cancelIQ, displayResult);
74 input = $("<input type='button' value='Restart'/>").bind("click", function() {
75 $('#output *').remove();
80 $("#output").append(input);
84 function runCommand() {
85 cmdNode = $(this).attr("id"); // Save not of executed command (in global)
86 var execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
87 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
88 connection.sendIQ(execIQ, function(result) {
89 sessionid = $(result).find("command").attr("sessionid");
90 displayResult(result);
94 function getCommandNodes() {
95 var nodesIQ = $iq({ type: "get", to: "localhost", id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
96 connection.sendIQ(nodesIQ, function(result) {
97 $('#output').append("<ul id='items'></ul>");
98 $(result).find("item").each(function(index, e) {
99 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
100 $("#items").append(item);
105 function checkFeatures() {
106 featureIQ = $iq({ type: "get", to: "localhost", id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
107 connection.sendIQ(featureIQ, function(result) {
108 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
109 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
111 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
117 function showConnect() {
119 var pass = $('#pass');
120 var button = $('#connect').get(0);
122 button.value = 'connect';
126 $('#output *').remove();
130 function showDisconnect() {
132 var pass = $('#pass');
133 var button = $('#connect').get(0);
135 button.value = 'disconnect';
142 $(document).ready(function () {
143 connection = new Strophe.Connection(BOSH_SERVICE);
144 connection.rawInput = rawInput;
145 connection.rawOutput = rawOutput;
147 $("#log_toggle").click(function () {
151 $('#cred').bind('submit', function () {
152 var button = $('#connect').get(0);
154 var pass = $('#pass');
155 localJID = jid.get(0).value;
157 if (button.value == 'connect') {
159 $('#log *').remove();
160 connection.connect(localJID,
164 connection.disconnect();
170 onunload = function() {
172 connection.disconnect();