1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
4 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
12 var entry = $('<div></div>').append(document.createTextNode(msg));
13 $('#log').append(entry);
16 function rawInput(data) {
20 function rawOutput(data) {
24 function onConnect(status) {
25 if (status == Strophe.Status.CONNECTING) {
26 log('Strophe is connecting.');
27 } else if (status == Strophe.Status.CONNFAIL) {
28 log('Strophe failed to connect.');
30 } else if (status == Strophe.Status.DISCONNECTING) {
31 log('Strophe is disconnecting.');
32 } else if (status == Strophe.Status.DISCONNECTED) {
33 log('Strophe is disconnected.');
35 } else if (status == Strophe.Status.AUTHFAIL) {
36 log('Authentication failed');
38 connection.disconnect();
40 } else if (status == Strophe.Status.CONNECTED) {
41 log('Strophe is connected.');
46 function addNote(elem, text, type) {
50 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
53 function addForm(elem, x) {
54 var form = $("<form/>");
55 form.submit(function(){return false;});
56 var fieldset = $("<fieldset/>");
57 form.append(fieldset);
58 if ($(x).find("title").length > 0)
59 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
60 if ($(x).find("instructions").length > 0)
61 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
62 $(x).find("field").each(function(){
64 var type = $(this).attr("type");
65 if($(this).attr("label")) {
66 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
70 item = $("<input type='hidden'/>");
73 item = $("<input type='checkbox'/>");
76 item = $("<textarea/>");
79 item = $("<input type='text'/>");
82 item = $("<input type='text'/>").attr("readonly",true);
85 item = $("<textarea/>");
88 item = $("<input type='text'/>");
91 item = $("<select multiple='multiple'/>");
92 $(this).find("option").each(function(){
93 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
97 item = $("<select/>");
98 $(this).find("option").each(function(){
99 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
103 item = $("<input type='password'/>");
106 item = $("<input/>");
108 item.addClass("df-item");
109 if ($(this).find("value").length > 0) {
111 if ((type == "text-multi") || (type == "jid-multi")) {
113 $(this).find("value").each(function() {
114 value = value + $(this).text() + "\n";
117 } else if (type == "list-multi") {
119 $(this).find("value").each(function() {
120 value[value.length] = $(this).text();
123 item.val($(this).find("value").text());
126 if ($(this).attr("var")) {
127 item.attr("name", $(this).attr("var"));
128 item.attr("id", $(this).attr("var"));
130 fieldset.append(item);
131 fieldset.append("<br/>");
133 $(elem).append(form);
136 function serializeToDataform(form, st) {
137 st.c("x", {"xmlns":"jabber:x:data", "type": "submit"});
138 $(form).find(".df-item").each(function(){
139 st.c("field", {"var": $(this).attr("name")});
140 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
141 for (var i = 0; i < this.options.length; i++)
142 if (options[i].selected)
143 st.c("value").t(options[i]).up();
144 } else if (this.nodeName.toLowerCase() == "textarea") {
145 var sp_value = this.value.split(/\r?\n|\r/g);
146 for(var i = 0; i < sp_value.length; i++)
147 st.c("value").t(sp_value[i]).up();
148 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
150 st.c("value").t("1");
152 st.c("value").t("0");
155 // if this has value then
156 st.c("value").t($(this).val()).up();
163 function displayResult(result) {
164 var status = $(result).find("command").attr("status");
166 $("#output").empty();
167 $(result).find("command > *").each(function(index, e) {
168 if ($(e).is("note")) {
169 addNote("#output", $(e).text(), $(e).attr("type"));
170 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
171 addForm("#output", e);
174 if (status == "executing") {
175 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
176 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
177 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
178 "<input type='button' id='executeButton' value='Execute'/>"+
179 "<input type='button' id='cancelButton' value='Cancel'/>");
180 for (kind in ['prev', 'next', 'complete']) {
181 if ($(result).find('actions ' + kind).length > 0)
182 $('#' + kind + 'Button').attr("disabled", "false");
184 $('#executeButton').bind("click", function() {
185 var execIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
186 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "execute" });
187 serializeToDataform($('form'), execIQ);
188 connection.sendIQ(execIQ, displayResult);
191 $('#cancelButton').bind("click", function() {
192 var cancelIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
193 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
196 connection.sendIQ(cancelIQ, displayResult);
199 input = $("<input type='button' value='Start over'/>").bind("click", function() {
200 $('#output').empty();
205 $("#output").append(input);
209 function runCommand() {
210 cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
211 var execIQ = $iq({ type: "set", to: connection.domain, id: connection.getUniqueId() })
212 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
213 connection.sendIQ(execIQ, function(result) {
214 sessionid = $(result).find("command").attr("sessionid");
215 displayResult(result);
219 function getCommandNodes() {
220 var nodesIQ = $iq({ type: "get", to: connection.domain, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
221 connection.sendIQ(nodesIQ, function(result) {
222 $('#output').append("<ul id='items'></ul>");
223 $(result).find("item").each(function(index, e) {
224 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
225 $("#items").append(item);
230 function checkFeatures() {
231 featureIQ = $iq({ type: "get", to: connection.domain, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
232 connection.sendIQ(featureIQ, function(result) {
233 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
234 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
236 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
242 function showConnect() {
244 var pass = $('#pass');
245 var button = $('#connect').get(0);
247 button.value = 'connect';
251 $('#output').empty();
255 function showDisconnect() {
257 var pass = $('#pass');
258 var button = $('#connect').get(0);
260 button.value = 'disconnect';
267 $(document).ready(function () {
268 connection = new Strophe.Connection(BOSH_SERVICE);
270 $('#log_container').show();
271 connection.rawInput = rawInput;
272 connection.rawOutput = rawOutput;
275 $("#log_toggle").click(function () {
279 $('#cred').bind('submit', function () {
280 var button = $('#connect').get(0);
282 var pass = $('#pass');
283 localJID = jid.get(0).value;
285 if (button.value == 'connect') {
288 connection.connect(localJID,
292 connection.disconnect();
298 onunload = function() {
300 connection.disconnect();