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);
48 checkFeatures("#output");
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 action='#'/>");
62 form.submit(function(event) {
63 executeCommand("execute", serializeToDataform('form'), function(e) { displayResult(elem, e) });
64 event.preventDefault();
66 var fieldset = $("<fieldset/>");
67 form.append(fieldset);
68 if ($(x).find("title").length > 0)
69 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
70 if ($(x).find("instructions").length > 0)
71 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
72 $(x).find("field").each(function() {
74 var type = $(this).attr("type");
75 if($(this).attr("label")) {
76 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
77 $("<br/>").appendTo(fieldset);
81 item = $("<input type='hidden'/>");
84 item = $("<input type='checkbox'/>");
87 item = $("<textarea rows='10' cols='70'/>");
90 item = $("<input type='text'/>");
93 item = $("<input type='text'/>").attr("readonly",true);
96 item = $("<textarea rows='10' cols='70'/>");
99 item = $("<input type='text'/>");
102 item = $("<select multiple='multiple'/>");
103 $(this).find("option").each(function() {
104 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
108 item = $("<select/>");
109 $(this).find("option").each(function() {
110 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
114 item = $("<input type='password'/>");
117 item = $("<input/>");
119 item.addClass("df-item");
120 if ($(this).find("value").length > 0) {
122 if ((type == "text-multi") || (type == "jid-multi")) {
124 $(this).find("value").each(function() {
125 value = value + $(this).text() + "\n";
128 } else if (type == "list-multi") {
129 $(this).children("value").each(function() {
130 item.children('option[value="' + $(this).text() + '"]').each(function() {
131 $(this).attr("selected", "selected");
135 item.val($(this).find("value").text());
138 if ($(x).attr("type") == "result")
139 item.attr("readonly", true);
140 if ($(this).attr("var")) {
141 item.attr("name", $(this).attr("var"));
142 item.attr("id", $(this).attr("var"));
144 fieldset.append(item);
145 if (type != "hidden")
146 fieldset.append("<br/>");
148 $(elem).append(form);
151 function serializeToDataform(form) {
152 st = $build("x", {"xmlns": "jabber:x:data", "type": "submit"});
153 $(form).find(".df-item").each(function(){
154 st.c("field", {"var": $(this).attr("name")});
155 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
156 for (var i = 0; i < this.options.length; i++)
157 if (this.options[i].selected)
158 st.c("value").t(this.options[i].text).up();
159 } else if (this.nodeName.toLowerCase() == "textarea") {
160 var sp_value = this.value.split(/\r?\n|\r/g);
161 for(var i = 0; i < sp_value.length; i++)
162 st.c("value").t(sp_value[i]).up();
163 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
165 st.c("value").t("1");
167 st.c("value").t("0");
170 // if this has value then
171 st.c("value").t($(this).val()).up();
179 function displayResult(elem, result) {
180 var status = $(result).find("command").attr("status");
181 var kinds = {'prev': 'Prev', 'next': 'Next', 'complete': 'Complete'};
184 $(result).find("command > *").each(function(index, e) {
185 if ($(e).is("note")) {
186 addNote(elem, $(e).text(), $(e).attr("type"));
187 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
191 if (status == "executing") {
192 for (kind in kinds) {
193 input = $("<input type='button' disabled='disabled' value='" + kinds[kind] + "'/>").click(function() {
195 executeCommand(kind, false, function(e) { displayResult(elem, e) });
197 executeCommand(kind, serializeToDataform('form'), function(e) { displayResult(elem, e) });
199 if ($(result).find('actions ' + kind).length > 0)
200 input.removeAttr("disabled");
201 $(elem).append(input);
204 $("<input type='button' id='executeButton' value='Execute'/>").click(function() {
205 executeCommand("execute", serializeToDataform('form'), function(e) { displayResult(elem, e) });
208 $("<input type='button' value='Cancel'/>").click(function() {
209 cancelCommand(function(e) { displayResult(elem, e) });
212 input = $("<input type='button' value='Start over'/>").bind("click", function() {
214 adhoc_status.sessionid = null;
215 adhoc_status.cmdNode = null;
216 getCommandNodes(elem);
218 $(elem).append(input);
222 function runCommand(item, callback) {
223 adhoc_status.cmdNode = $(item).attr("id"); // Save node of executed command (in global var)
224 executeCommand("execute", false, function(result) {
225 adhoc_status.sessionid = $(result).find("command").attr("sessionid");
230 function executeCommand(type, childs, callback) {
231 if (adhoc_status.sessionid)
232 var execIQ = $iq({ type: "set", to: adhoc_status.queryJID, id: connection.getUniqueId() })
233 .c("command", { xmlns: Strophe.NS.ADHOC, node: adhoc_status.cmdNode, sessionid: adhoc_status.sessionid, action: type });
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: type });
238 execIQ.cnode(childs);
239 connection.sendIQ(execIQ, callback);
242 function cancelCommand(callback) {
243 executeCommand("cancel", false, callback);
244 adhoc_status.cmdNode = null
245 adhoc_status.sessionid = null;
248 function getCommandNodes(elem) {
249 var nodesIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
250 connection.sendIQ(nodesIQ, function(result) {
251 var items = $("<ul></ul>");
252 $(elem).append(items);
253 $(result).find("item").each(function(index, e) {
254 $("<li></li>").append($("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(function (event) {
255 runCommand(this, function (result) { displayResult(elem, result); });
256 event.preventDefault();
262 function checkFeatures(elem) {
263 if (adhoc_status.sessionid)
265 featureIQ = $iq({ type: "get", to: adhoc_status.queryJID, id: connection.getUniqueId() }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
267 connection.sendIQ(featureIQ,
268 function(result) { /* Callback */
269 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
270 getCommandNodes(elem);
272 $(elem).append("<p>" + adhoc_status.queryJID + " does NOT support AdHoc commands</p>");
275 function(result) { /* Errback */
276 $(elem).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();
339 checkFeatures("#output");
340 event.preventDefault();
344 window.onunload = window.onbeforeunload = function() {
346 connection.disconnect();