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") {
126 $(this).children("value").each(function() {
127 item.children('option[value="' + $(this).text() + '"]').each(function() {
128 $(this).attr("selected", "selected");
132 item.val($(this).find("value").text());
135 if ($(x).attr("type") == "result")
136 item.attr("readonly", true);
137 if ($(this).attr("var")) {
138 item.attr("name", $(this).attr("var"));
139 item.attr("id", $(this).attr("var"));
141 fieldset.append(item);
142 if (type != "hidden")
143 fieldset.append("<br/>");
145 $(elem).append(form);
148 function serializeToDataform(form, st) {
149 st.c("x", {"xmlns": "jabber:x:data", "type": "submit"});
150 $(form).find(".df-item").each(function(){
151 st.c("field", {"var": $(this).attr("name")});
152 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
153 for (var i = 0; i < this.options.length; i++)
154 if (this.options[i].selected)
155 st.c("value").t(this.options[i].text).up();
156 } else if (this.nodeName.toLowerCase() == "textarea") {
157 var sp_value = this.value.split(/\r?\n|\r/g);
158 for(var i = 0; i < sp_value.length; i++)
159 st.c("value").t(sp_value[i]).up();
160 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
162 st.c("value").t("1");
164 st.c("value").t("0");
167 // if this has value then
168 st.c("value").t($(this).val()).up();
175 function displayResult(result) {
176 var status = $(result).find("command").attr("status");
178 $("#output").empty();
179 $(result).find("command > *").each(function(index, e) {
180 if ($(e).is("note")) {
181 addNote("#output", $(e).text(), $(e).attr("type"));
182 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
183 addForm("#output", e);
186 if (status == "executing") {
187 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
188 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
189 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
190 "<input type='button' id='executeButton' value='Execute'/>"+
191 "<input type='button' id='cancelButton' value='Cancel'/>");
192 for (kind in ['prev', 'next', 'complete']) {
193 if ($(result).find('actions ' + kind).length > 0)
194 $('#' + kind + 'Button').attr("disabled", "false");
196 $('#executeButton').bind("click", function() {
197 var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
198 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "execute" });
199 serializeToDataform($('form'), execIQ);
200 connection.sendIQ(execIQ, displayResult);
203 $('#cancelButton').bind("click", function() {
204 cancelCommand(displayResult);
207 $('#queryForm').unbind('submit');
208 $('#queryForm').bind('submit', function (event) {
209 cancelCommand(function(result) {
210 $('#queryForm').unbind('submit');
211 $('#queryForm').bind('submit', function (event) {
212 adhoc_status.queryJID = $('#queryJID').val();
214 event.preventDefault();
217 adhoc_status.queryJID = $('#queryJID').val();
219 event.preventDefault();
223 input = $("<input type='button' value='Start over'/>").bind("click", function() {
224 $('#output').empty();
225 adhoc_status.sessionid = null;
226 adhoc_status.cmdNode = null;
229 $("#output").append(input);
233 function runCommand(event) {
234 adhoc_status.cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
235 var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
236 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, action: "execute" });
237 connection.sendIQ(execIQ, function(result) {
238 adhoc_status.sessionid = $(result).find("command").attr("sessionid");
239 displayResult(result);
241 event.preventDefault();
244 function cancelCommand(callback) {
245 var cancelIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
246 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: "cancel" });
247 adhoc_status.cmdNode = null
248 adhoc_status.sessionid = null;
249 connection.sendIQ(cancelIQ, callback);
252 function getCommandNodes() {
253 var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
254 connection.sendIQ(nodesIQ, function(result) {
255 $('#output').append("<ul id='items'></ul>");
256 $(result).find("item").each(function(index, e) {
257 link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
258 item = $("<li></li>").append(link);
259 $("#items").append(item);
264 function checkFeatures() {
265 featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
266 $('#output').empty();
267 connection.sendIQ(featureIQ,
268 function(result) { /* Callback */
269 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
270 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
273 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
276 function(result) { /* Errback */
277 $('#output').append("<p>Couldn't get list of supported features</p>");
282 function showConnect() {
284 var pass = $('#pass');
285 var button = $('#connect').get(0);
287 button.value = 'connect';
291 $('#cred label').show();
292 $('#cred br').show();
293 $('#output').empty();
296 function showDisconnect() {
298 var pass = $('#pass');
299 var button = $('#connect').get(0);
301 button.value = 'disconnect';
304 $('#cred label').hide();
305 $('#cred br').hide();
308 $(document).ready(function () {
309 connection = new Strophe.Connection(BOSH_SERVICE);
311 $('#log_container').show();
312 connection.rawInput = rawInput;
313 connection.rawOutput = rawOutput;
316 $("#log_toggle").click(function () {
320 $('#cred').bind('submit', function (event) {
321 var button = $('#connect').get(0);
323 var pass = $('#pass');
324 localJID = jid.get(0).value;
326 if (button.value == 'connect') {
329 connection.connect(localJID,
333 connection.disconnect();
335 event.preventDefault();
338 $('#queryForm').bind('submit', function (event) {
339 adhoc_status.queryJID = $('#queryJID').val();
341 event.preventDefault();
345 window.onunload = window.onbeforeunload = function() {
347 connection.disconnect();