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 addNote(elem, text, type) {
49 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
52 function addForm(elem, x) {
53 var form = $("<form/>");
54 form.submit(function(){return false;});
55 var fieldset = $("<fieldset/>");
56 form.append(fieldset);
57 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
58 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
59 $(x).find("field").each(function(){
61 var type = $(this).attr("type");
62 if($(this).attr("label")) {
63 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
67 item = $("<input type='hidden'/>");
70 item = $("<input type='checkbox'/>");
73 item = $("<textarea/>");
76 item = $("<input type='text'/>");
79 item = $("<input type='text'/>").attr("readonly",true);
82 item = $("<textarea/>");
85 item = $("<input type='text'/>");
88 item = $("<select multiple='multiple'/>");
89 $(this).find("option").each(function(){
90 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
94 item = $("<select/>");
95 $(this).find("option").each(function(){
96 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
100 item = $("<input type='password'/>");
103 item = $("<input/>");
105 if ($(this).find("value")) {
107 if ((type == "text-multi") || (type == "jid-multi")) {
109 $(this).find("value").each(function() {
110 value = value + $(this).text() + "\n";
113 } else if (type == "list-multi") {
115 $(this).find("value").each(function() {
116 value[value.length] = $(this).text();
119 item.val($(this).find("value").text());
122 if ($(this).attr("var")) {
123 item.attr("name", $(this).attr("var"));
124 item.attr("id", $(this).attr("var"));
126 fieldset.append(item);
127 fieldset.append("<br/>");
129 $(elem).append(form);
132 function displayResult(result) {
133 var status = $(result).find("command").attr("status");
135 $("#output *").remove();
136 $(result).find("command > *").each(function(index, e) {
137 if ($(e).is("note")) {
138 addNote("#output", $(e).text(), $(e).attr("type"));
139 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
140 addForm("#output", e);
143 if (status == "executing") {
144 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
145 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
146 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
147 "<input type='button' id='executeButton' value='Execute'/>"+
148 "<input type='button' id='cancelButton' value='Cancel'/>");
149 for (kind in ['prev', 'next', 'complete']) {
150 if ($(result).find('actions ' + kind).length > 0)
151 $('#' + kind + 'Button').attr("disabled", "false");
153 $('#cancelButton').bind("click", function() {
154 var cancelIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
155 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
158 connection.sendIQ(cancelIQ, displayResult);
161 input = $("<input type='button' value='Restart'/>").bind("click", function() {
162 $('#output *').remove();
167 $("#output").append(input);
171 function runCommand() {
172 cmdNode = $(this).attr("id"); // Save not of executed command (in global)
173 var execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
174 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
175 connection.sendIQ(execIQ, function(result) {
176 sessionid = $(result).find("command").attr("sessionid");
177 displayResult(result);
181 function getCommandNodes() {
182 var nodesIQ = $iq({ type: "get", to: "localhost", id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
183 connection.sendIQ(nodesIQ, function(result) {
184 $('#output').append("<ul id='items'></ul>");
185 $(result).find("item").each(function(index, e) {
186 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
187 $("#items").append(item);
192 function checkFeatures() {
193 featureIQ = $iq({ type: "get", to: "localhost", id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
194 connection.sendIQ(featureIQ, function(result) {
195 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
196 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
198 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
204 function showConnect() {
206 var pass = $('#pass');
207 var button = $('#connect').get(0);
209 button.value = 'connect';
213 $('#output *').remove();
217 function showDisconnect() {
219 var pass = $('#pass');
220 var button = $('#connect').get(0);
222 button.value = 'disconnect';
229 $(document).ready(function () {
230 connection = new Strophe.Connection(BOSH_SERVICE);
231 connection.rawInput = rawInput;
232 connection.rawOutput = rawOutput;
234 $("#log_toggle").click(function () {
238 $('#cred').bind('submit', function () {
239 var button = $('#connect').get(0);
241 var pass = $('#pass');
242 localJID = jid.get(0).value;
244 if (button.value == 'connect') {
246 $('#log *').remove();
247 connection.connect(localJID,
251 connection.disconnect();
257 onunload = function() {
259 connection.disconnect();