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;
49 function addNote(elem, text, type) {
53 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
56 function addForm(elem, x) {
57 var form = $("<form/>");
58 form.submit(function(){return false;});
59 var fieldset = $("<fieldset/>");
60 form.append(fieldset);
61 if ($(x).find("title").length > 0)
62 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
63 if ($(x).find("instructions").length > 0)
64 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
65 $(x).find("field").each(function(){
67 var type = $(this).attr("type");
68 if($(this).attr("label")) {
69 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
73 item = $("<input type='hidden'/>");
76 item = $("<input type='checkbox'/>");
79 item = $("<textarea/>");
82 item = $("<input type='text'/>");
85 item = $("<input type='text'/>").attr("readonly",true);
88 item = $("<textarea/>");
91 item = $("<input type='text'/>");
94 item = $("<select multiple='multiple'/>");
95 $(this).find("option").each(function(){
96 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
100 item = $("<select/>");
101 $(this).find("option").each(function(){
102 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
106 item = $("<input type='password'/>");
109 item = $("<input/>");
111 item.addClass("df-item");
112 if ($(this).find("value").length > 0) {
114 if ((type == "text-multi") || (type == "jid-multi")) {
116 $(this).find("value").each(function() {
117 value = value + $(this).text() + "\n";
120 } else if (type == "list-multi") {
122 $(this).find("value").each(function() {
123 value[value.length] = $(this).text();
126 item.val($(this).find("value").text());
129 if ($(this).attr("var")) {
130 item.attr("name", $(this).attr("var"));
131 item.attr("id", $(this).attr("var"));
133 fieldset.append(item);
134 fieldset.append("<br/>");
136 $(elem).append(form);
139 function serializeToDataform(form, st) {
140 st.c("x", {"xmlns":"jabber:x:data", "type": "submit"});
141 $(form).find(".df-item").each(function(){
142 st.c("field", {"var": $(this).attr("name")});
143 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
144 for (var i = 0; i < this.options.length; i++)
145 if (options[i].selected)
146 st.c("value").t(options[i]).up();
147 } else if (this.nodeName.toLowerCase() == "textarea") {
148 var sp_value = this.value.split(/\r?\n|\r/g);
149 for(var i = 0; i < sp_value.length; i++)
150 st.c("value").t(sp_value[i]).up();
151 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
153 st.c("value").t("1");
155 st.c("value").t("0");
158 // if this has value then
159 st.c("value").t($(this).val()).up();
166 function displayResult(result) {
167 var status = $(result).find("command").attr("status");
169 $("#output").empty();
170 $(result).find("command > *").each(function(index, e) {
171 if ($(e).is("note")) {
172 addNote("#output", $(e).text(), $(e).attr("type"));
173 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
174 addForm("#output", e);
177 if (status == "executing") {
178 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
179 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
180 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
181 "<input type='button' id='executeButton' value='Execute'/>"+
182 "<input type='button' id='cancelButton' value='Cancel'/>");
183 for (kind in ['prev', 'next', 'complete']) {
184 if ($(result).find('actions ' + kind).length > 0)
185 $('#' + kind + 'Button').attr("disabled", "false");
187 $('#executeButton').bind("click", function() {
188 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
189 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "execute" });
190 serializeToDataform($('form'), execIQ);
191 connection.sendIQ(execIQ, displayResult);
194 $('#cancelButton').bind("click", function() {
195 var cancelIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
196 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
199 connection.sendIQ(cancelIQ, displayResult);
202 input = $("<input type='button' value='Start over'/>").bind("click", function() {
203 $('#output').empty();
208 $("#output").append(input);
212 function runCommand() {
213 cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
214 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
215 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
216 connection.sendIQ(execIQ, function(result) {
217 sessionid = $(result).find("command").attr("sessionid");
218 displayResult(result);
222 function getCommandNodes() {
223 var nodesIQ = $iq({ type: "get", to: queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
224 connection.sendIQ(nodesIQ, function(result) {
225 $('#output').append("<ul id='items'></ul>");
226 $(result).find("item").each(function(index, e) {
227 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
228 $("#items").append(item);
233 function checkFeatures() {
234 featureIQ = $iq({ type: "get", to: queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
235 connection.sendIQ(featureIQ, function(result) {
236 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
237 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
239 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
242 $('#output').empty();
246 function showConnect() {
248 var pass = $('#pass');
249 var button = $('#connect').get(0);
251 button.value = 'connect';
256 $('#output').empty();
260 function showDisconnect() {
262 var pass = $('#pass');
263 var button = $('#connect').get(0);
265 button.value = 'disconnect';
268 $('#cred label').hide();
272 $(document).ready(function () {
273 connection = new Strophe.Connection(BOSH_SERVICE);
275 $('#log_container').show();
276 connection.rawInput = rawInput;
277 connection.rawOutput = rawOutput;
280 $("#log_toggle").click(function () {
284 $('#cred').bind('submit', function () {
285 var button = $('#connect').get(0);
287 var pass = $('#pass');
288 localJID = jid.get(0).value;
290 if (button.value == 'connect') {
293 connection.connect(localJID,
297 connection.disconnect();
302 $('#queryForm').bind('submit', function () {
303 queryJID = $('#queryJID').val();
309 onunload = function() {
311 connection.disconnect();