1 var BOSH_SERVICE = 'http://localhost:5280/http-bind/';
3 Strophe.addNamespace("ADHOC", "http://jabber.org/protocol/commands");
11 var entry = $('<div></div>').append(document.createTextNode(msg));
12 $('#log').append(entry);
15 function rawInput(data) {
19 function rawOutput(data) {
23 function onConnect(status) {
24 if (status == Strophe.Status.CONNECTING) {
25 log('Strophe is connecting.');
26 } else if (status == Strophe.Status.CONNFAIL) {
27 log('Strophe failed to connect.');
29 } else if (status == Strophe.Status.DISCONNECTING) {
30 log('Strophe is disconnecting.');
31 } else if (status == Strophe.Status.DISCONNECTED) {
32 log('Strophe is disconnected.');
34 } else if (status == Strophe.Status.AUTHFAIL) {
35 log('Authentication failed');
37 connection.disconnect();
39 } else if (status == Strophe.Status.CONNECTED) {
40 log('Strophe is connected.');
45 function addNote(elem, text, type) {
49 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
52 function addForm(elem, x) {
53 var form = $("<form/>");
54 form.submit(function(){return false;});
55 var fieldset = $("<fieldset/>");
56 form.append(fieldset);
57 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
58 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
59 $(x).find("field").each(function(){
61 var type = $(this).attr("type");
62 if($(this).attr("label")) {
63 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
67 item = $("<input type='hidden'/>");
70 item = $("<input type='checkbox'/>");
73 item = $("<textarea/>");
76 item = $("<input type='text'/>");
79 item = $("<input type='text'/>").attr("readonly",true);
82 item = $("<textarea/>");
85 item = $("<input type='text'/>");
88 item = $("<select multiple='multiple'/>");
89 $(this).find("option").each(function(){
90 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
94 item = $("<select/>");
95 $(this).find("option").each(function(){
96 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
100 item = $("<input type='password'/>");
103 item = $("<input/>");
105 item.addClass("df-item");
106 if ($(this).find("value")) {
108 if ((type == "text-multi") || (type == "jid-multi")) {
110 $(this).find("value").each(function() {
111 value = value + $(this).text() + "\n";
114 } else if (type == "list-multi") {
116 $(this).find("value").each(function() {
117 value[value.length] = $(this).text();
120 item.val($(this).find("value").text());
123 if ($(this).attr("var")) {
124 item.attr("name", $(this).attr("var"));
125 item.attr("id", $(this).attr("var"));
127 fieldset.append(item);
128 fieldset.append("<br/>");
130 $(elem).append(form);
133 function serializeToDataform(form, st) {
134 st.c("x", {"xmlns":"jabber:x:data", "type": "submit"});
135 $(form).find(".df-item").each(function(){
136 st.c("field", {"var": $(this).attr("name")});
137 if(this.nodeName.toLowerCase() == "select" && this.multiple) {
138 for(var i = 0; i < this.options.length; i++)
139 if(options[i].selected)
140 st.c("value").text(options[i]).up();
141 } else if(this.nodeName.toLowerCase() == "textarea") {
142 var sp_value = this.value.split(/\r\n?|\r/g);
143 for(var i = 0; i < sp_value.length; i++)
144 st.c("value").text(sp_value[i]).up();
146 // if this has value then
147 st.c("value").text($(this).val()).up();
154 function displayResult(result) {
155 var status = $(result).find("command").attr("status");
157 $("#output *").remove();
158 $(result).find("command > *").each(function(index, e) {
159 if ($(e).is("note")) {
160 addNote("#output", $(e).text(), $(e).attr("type"));
161 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
162 addForm("#output", e);
165 if (status == "executing") {
166 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
167 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
168 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
169 "<input type='button' id='executeButton' value='Execute'/>"+
170 "<input type='button' id='cancelButton' value='Cancel'/>");
171 for (kind in ['prev', 'next', 'complete']) {
172 if ($(result).find('actions ' + kind).length > 0)
173 $('#' + kind + 'Button').attr("disabled", "false");
175 $('#cancelButton').bind("click", function() {
176 var cancelIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
177 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
180 connection.sendIQ(cancelIQ, displayResult);
183 input = $("<input type='button' value='Restart'/>").bind("click", function() {
184 $('#output *').remove();
189 $("#output").append(input);
193 function runCommand() {
194 cmdNode = $(this).attr("id"); // Save not of executed command (in global)
195 var execIQ = $iq({ type: "set", to: "localhost", id: connection.getUniqueId() })
196 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
197 connection.sendIQ(execIQ, function(result) {
198 sessionid = $(result).find("command").attr("sessionid");
199 displayResult(result);
203 function getCommandNodes() {
204 var nodesIQ = $iq({ type: "get", to: "localhost", id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
205 connection.sendIQ(nodesIQ, function(result) {
206 $('#output').append("<ul id='items'></ul>");
207 $(result).find("item").each(function(index, e) {
208 item = $("<li id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</li>").bind("click", runCommand);
209 $("#items").append(item);
214 function checkFeatures() {
215 featureIQ = $iq({ type: "get", to: "localhost", id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
216 connection.sendIQ(featureIQ, function(result) {
217 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
218 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
220 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
226 function showConnect() {
228 var pass = $('#pass');
229 var button = $('#connect').get(0);
231 button.value = 'connect';
235 $('#output *').remove();
239 function showDisconnect() {
241 var pass = $('#pass');
242 var button = $('#connect').get(0);
244 button.value = 'disconnect';
251 $(document).ready(function () {
252 connection = new Strophe.Connection(BOSH_SERVICE);
253 connection.rawInput = rawInput;
254 connection.rawOutput = rawOutput;
256 $("#log_toggle").click(function () {
260 $('#cred').bind('submit', function () {
261 var button = $('#connect').get(0);
263 var pass = $('#pass');
264 localJID = jid.get(0).value;
266 if (button.value == 'connect') {
268 $('#log *').remove();
269 connection.connect(localJID,
273 connection.disconnect();
279 onunload = function() {
281 connection.disconnect();