1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
4 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
13 var entry = $('<div></div>').append(document.createTextNode(msg));
14 $('#log').append(entry);
17 function rawInput(data) {
21 function rawOutput(data) {
25 function onConnect(status) {
26 if (status == Strophe.Status.CONNECTING) {
27 log('Strophe is connecting.');
28 } else if (status == Strophe.Status.CONNFAIL) {
29 log('Strophe failed to connect.');
31 } else if (status == Strophe.Status.DISCONNECTING) {
32 log('Strophe is disconnecting.');
33 } else if (status == Strophe.Status.DISCONNECTED) {
34 log('Strophe is disconnected.');
36 } else if (status == Strophe.Status.AUTHFAIL) {
37 log('Authentication failed');
39 connection.disconnect();
41 } else if (status == Strophe.Status.CONNECTED) {
42 log('Strophe is connected.');
43 queryJID = connection.domain;
44 $('#queryJID').val(queryJID);
50 function addNote(elem, text, type) {
54 text = text.replace(/\n/g, "<br/>");
55 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
58 function addForm(elem, x) {
59 var form = $("<form/>");
60 form.submit(function(event){event.preventDefault();});
61 var fieldset = $("<fieldset/>");
62 form.append(fieldset);
63 if ($(x).find("title").length > 0)
64 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
65 if ($(x).find("instructions").length > 0)
66 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
67 $(x).find("field").each(function(){
69 var type = $(this).attr("type");
70 if($(this).attr("label")) {
71 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
72 $("<br/>").appendTo(fieldset);
76 item = $("<input type='hidden'/>");
79 item = $("<input type='checkbox'/>");
82 item = $("<textarea rows='10' cols='70'/>");
85 item = $("<input type='text'/>");
88 item = $("<input type='text'/>").attr("readonly",true);
91 item = $("<textarea rows='10' cols='70'/>");
94 item = $("<input type='text'/>");
97 item = $("<select multiple='multiple'/>");
98 $(this).find("option").each(function(){
99 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
103 item = $("<select/>");
104 $(this).find("option").each(function(){
105 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
109 item = $("<input type='password'/>");
112 item = $("<input/>");
114 item.addClass("df-item");
115 if ($(this).find("value").length > 0) {
117 if ((type == "text-multi") || (type == "jid-multi")) {
119 $(this).find("value").each(function() {
120 value = value + $(this).text() + "\n";
123 } else if (type == "list-multi") {
125 $(this).find("value").each(function() {
126 value[value.length] = $(this).text();
129 item.val($(this).find("value").text());
132 if ($(x).attr("type") == "result")
133 item.attr("readonly", true);
134 if ($(this).attr("var")) {
135 item.attr("name", $(this).attr("var"));
136 item.attr("id", $(this).attr("var"));
138 fieldset.append(item);
139 if (type != "hidden")
140 fieldset.append("<br/>");
142 $(elem).append(form);
145 function serializeToDataform(form, st) {
146 st.c("x", {"xmlns": "jabber:x:data", "type": "submit"});
147 $(form).find(".df-item").each(function(){
148 st.c("field", {"var": $(this).attr("name")});
149 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
150 for (var i = 0; i < this.options.length; i++)
151 if (options[i].selected)
152 st.c("value").t(options[i]).up();
153 } else if (this.nodeName.toLowerCase() == "textarea") {
154 var sp_value = this.value.split(/\r?\n|\r/g);
155 for(var i = 0; i < sp_value.length; i++)
156 st.c("value").t(sp_value[i]).up();
157 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
159 st.c("value").t("1");
161 st.c("value").t("0");
164 // if this has value then
165 st.c("value").t($(this).val()).up();
172 function displayResult(result) {
173 var status = $(result).find("command").attr("status");
175 $("#output").empty();
176 $(result).find("command > *").each(function(index, e) {
177 if ($(e).is("note")) {
178 addNote("#output", $(e).text(), $(e).attr("type"));
179 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
180 addForm("#output", e);
183 if (status == "executing") {
184 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
185 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
186 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
187 "<input type='button' id='executeButton' value='Execute'/>"+
188 "<input type='button' id='cancelButton' value='Cancel'/>");
189 for (kind in ['prev', 'next', 'complete']) {
190 if ($(result).find('actions ' + kind).length > 0)
191 $('#' + kind + 'Button').attr("disabled", "false");
193 $('#executeButton').bind("click", function() {
194 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
195 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "execute" });
196 serializeToDataform($('form'), execIQ);
197 connection.sendIQ(execIQ, displayResult);
200 $('#cancelButton').bind("click", function() {
201 cancelCommand(displayResult);
204 $('#queryForm').unbind('submit');
205 $('#queryForm').bind('submit', function (event) {
206 cancelCommand(function(result) {
207 $('#queryForm').unbind('submit');
208 $('#queryForm').bind('submit', function (event) {
209 queryJID = $('#queryJID').val();
211 event.preventDefault();
214 queryJID = $('#queryJID').val();
216 event.preventDefault();
220 input = $("<input type='button' value='Start over'/>").bind("click", function() {
221 $('#output').empty();
226 $("#output").append(input);
230 function runCommand(event) {
231 cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
232 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
233 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
234 connection.sendIQ(execIQ, function(result) {
235 sessionid = $(result).find("command").attr("sessionid");
236 displayResult(result);
238 event.preventDefault();
241 function cancelCommand(callback) {
242 var cancelIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
243 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
246 connection.sendIQ(cancelIQ, callback);
249 function getCommandNodes() {
250 var nodesIQ = $iq({ type: "get", to: queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
251 connection.sendIQ(nodesIQ, function(result) {
252 $('#output').append("<ul id='items'></ul>");
253 $(result).find("item").each(function(index, e) {
254 link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
255 item = $("<li></li>").append(link);
256 $("#items").append(item);
261 function checkFeatures() {
262 featureIQ = $iq({ type: "get", to: queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
263 $('#output').empty();
264 connection.sendIQ(featureIQ,
265 function(result) { /* Callback */
266 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
267 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
270 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
273 function(result) { /* Errback */
274 $('#output').append("<p>Couldn't get list of supported features</p>");
279 function showConnect() {
281 var pass = $('#pass');
282 var button = $('#connect').get(0);
284 button.value = 'connect';
288 $('#cred label').show();
289 $('#cred br').show();
290 $('#output').empty();
293 function showDisconnect() {
295 var pass = $('#pass');
296 var button = $('#connect').get(0);
298 button.value = 'disconnect';
301 $('#cred label').hide();
302 $('#cred br').hide();
305 $(document).ready(function () {
306 connection = new Strophe.Connection(BOSH_SERVICE);
308 $('#log_container').show();
309 connection.rawInput = rawInput;
310 connection.rawOutput = rawOutput;
313 $("#log_toggle").click(function () {
317 $('#cred').bind('submit', function (event) {
318 var button = $('#connect').get(0);
320 var pass = $('#pass');
321 localJID = jid.get(0).value;
323 if (button.value == 'connect') {
326 connection.connect(localJID,
330 connection.disconnect();
332 event.preventDefault();
335 $('#queryForm').bind('submit', function (event) {
336 queryJID = $('#queryJID').val();
338 event.preventDefault();
342 onunload = function() {
344 connection.disconnect();