1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
5 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.');
47 function addNote(elem, text, type) {
51 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
54 function addForm(elem, x) {
55 var form = $("<form/>");
56 form.submit(function(){return false;});
57 var fieldset = $("<fieldset/>");
58 form.append(fieldset);
59 if ($(x).find("title").length > 0)
60 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
61 if ($(x).find("instructions").length > 0)
62 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
63 $(x).find("field").each(function(){
65 var type = $(this).attr("type");
66 if($(this).attr("label")) {
67 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
71 item = $("<input type='hidden'/>");
74 item = $("<input type='checkbox'/>");
77 item = $("<textarea/>");
80 item = $("<input type='text'/>");
83 item = $("<input type='text'/>").attr("readonly",true);
86 item = $("<textarea/>");
89 item = $("<input type='text'/>");
92 item = $("<select multiple='multiple'/>");
93 $(this).find("option").each(function(){
94 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
98 item = $("<select/>");
99 $(this).find("option").each(function(){
100 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
104 item = $("<input type='password'/>");
107 item = $("<input/>");
109 item.addClass("df-item");
110 if ($(this).find("value").length > 0) {
112 if ((type == "text-multi") || (type == "jid-multi")) {
114 $(this).find("value").each(function() {
115 value = value + $(this).text() + "\n";
118 } else if (type == "list-multi") {
120 $(this).find("value").each(function() {
121 value[value.length] = $(this).text();
124 item.val($(this).find("value").text());
127 if ($(this).attr("var")) {
128 item.attr("name", $(this).attr("var"));
129 item.attr("id", $(this).attr("var"));
131 fieldset.append(item);
132 fieldset.append("<br/>");
134 $(elem).append(form);
137 function serializeToDataform(form, st) {
138 st.c("x", {"xmlns":"jabber:x:data", "type": "submit"});
139 $(form).find(".df-item").each(function(){
140 st.c("field", {"var": $(this).attr("name")});
141 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
142 for (var i = 0; i < this.options.length; i++)
143 if (options[i].selected)
144 st.c("value").t(options[i]).up();
145 } else if (this.nodeName.toLowerCase() == "textarea") {
146 var sp_value = this.value.split(/\r?\n|\r/g);
147 for(var i = 0; i < sp_value.length; i++)
148 st.c("value").t(sp_value[i]).up();
149 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
151 st.c("value").t("1");
153 st.c("value").t("0");
156 // if this has value then
157 st.c("value").t($(this).val()).up();
164 function displayResult(result) {
165 var status = $(result).find("command").attr("status");
167 $("#output *").remove();
168 $(result).find("command > *").each(function(index, e) {
169 if ($(e).is("note")) {
170 addNote("#output", $(e).text(), $(e).attr("type"));
171 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
172 addForm("#output", e);
175 if (status == "executing") {
176 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
177 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
178 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
179 "<input type='button' id='executeButton' value='Execute'/>"+
180 "<input type='button' id='cancelButton' value='Cancel'/>");
181 for (kind in ['prev', 'next', 'complete']) {
182 if ($(result).find('actions ' + kind).length > 0)
183 $('#' + kind + 'Button').attr("disabled", "false");
185 $('#executeButton').bind("click", function() {
186 var execIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
187 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "execute" });
188 serializeToDataform($('form'), execIQ);
189 connection.sendIQ(execIQ, displayResult);
192 $('#cancelButton').bind("click", function() {
193 var cancelIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
194 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
197 connection.sendIQ(cancelIQ, displayResult);
200 input = $("<input type='button' value='Start over'/>").bind("click", function() {
201 $('#output *').remove();
206 $("#output").append(input);
210 function runCommand() {
211 cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
212 var execIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
213 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
214 connection.sendIQ(execIQ, function(result) {
215 sessionid = $(result).find("command").attr("sessionid");
216 displayResult(result);
220 function getCommandNodes() {
221 var nodesIQ = $iq({ type: "get", to: connection.domain, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
222 connection.sendIQ(nodesIQ, function(result) {
223 $('#output').append("<ul id='items'></ul>");
224 $(result).find("item").each(function(index, e) {
225 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
226 $("#items").append(item);
231 function checkFeatures() {
232 featureIQ = $iq({ type: "get", to: connection.domain, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
233 connection.sendIQ(featureIQ, function(result) {
234 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
235 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
237 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
243 function showConnect() {
245 var pass = $('#pass');
246 var button = $('#connect').get(0);
248 button.value = 'connect';
252 $('#output *').remove();
256 function showDisconnect() {
258 var pass = $('#pass');
259 var button = $('#connect').get(0);
261 button.value = 'disconnect';
268 $(document).ready(function () {
269 connection = new Strophe.Connection(BOSH_SERVICE);
271 $('#log_container').show();
272 connection.rawInput = rawInput;
273 connection.rawOutput = rawOutput;
276 $("#log_toggle").click(function () {
280 $('#cred').bind('submit', function () {
281 var button = $('#connect').get(0);
283 var pass = $('#pass');
284 localJID = jid.get(0).value;
286 if (button.value == 'connect') {
288 $('#log *').remove();
289 connection.connect(localJID,
293 connection.disconnect();
299 onunload = function() {
301 connection.disconnect();