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 $(elem).append("<p class='" + type + "Note'>" + text + "</p>");
57 function addForm(elem, x) {
58 var form = $("<form/>");
59 form.submit(function(event){event.preventDefault();});
60 var fieldset = $("<fieldset/>");
61 form.append(fieldset);
62 if ($(x).find("title").length > 0)
63 $("<legend/>").text($(x).find("title").text()).appendTo(fieldset);
64 if ($(x).find("instructions").length > 0)
65 $("<p/>").text($(x).find("instructions").text()).appendTo(fieldset);
66 $(x).find("field").each(function(){
68 var type = $(this).attr("type");
69 if($(this).attr("label")) {
70 $("<label/>").text($(this).attr("label")).attr("for", $(this).attr("var")).appendTo(fieldset);
71 $("<br/>").appendTo(fieldset);
75 item = $("<input type='hidden'/>");
78 item = $("<input type='checkbox'/>");
81 item = $("<textarea rows='10' cols='70'/>");
84 item = $("<input type='text'/>");
87 item = $("<input type='text'/>").attr("readonly",true);
90 item = $("<textarea rows='10' cols'70'/>");
93 item = $("<input type='text'/>");
96 item = $("<select multiple='multiple'/>");
97 $(this).find("option").each(function(){
98 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
102 item = $("<select/>");
103 $(this).find("option").each(function(){
104 $("<option/>").val($(this).find("value").text()).text($(this).attr("label")).appendTo(item);
108 item = $("<input type='password'/>");
111 item = $("<input/>");
113 item.addClass("df-item");
114 if ($(this).find("value").length > 0) {
116 if ((type == "text-multi") || (type == "jid-multi")) {
118 $(this).find("value").each(function() {
119 value = value + $(this).text() + "\n";
122 } else if (type == "list-multi") {
124 $(this).find("value").each(function() {
125 value[value.length] = $(this).text();
128 item.val($(this).find("value").text());
131 if ($(x).attr("type") == "result")
132 item.attr("readonly", true);
133 if ($(this).attr("var")) {
134 item.attr("name", $(this).attr("var"));
135 item.attr("id", $(this).attr("var"));
137 fieldset.append(item);
138 if (type != "hidden")
139 fieldset.append("<br/>");
141 $(elem).append(form);
144 function serializeToDataform(form, st) {
145 st.c("x", {"xmlns":"jabber:x:data", "type": "submit"});
146 $(form).find(".df-item").each(function(){
147 st.c("field", {"var": $(this).attr("name")});
148 if (this.nodeName.toLowerCase() == "select" && this.multiple) {
149 for (var i = 0; i < this.options.length; i++)
150 if (options[i].selected)
151 st.c("value").t(options[i]).up();
152 } else if (this.nodeName.toLowerCase() == "textarea") {
153 var sp_value = this.value.split(/\r?\n|\r/g);
154 for(var i = 0; i < sp_value.length; i++)
155 st.c("value").t(sp_value[i]).up();
156 } else if (this.nodeName.toLowerCase() == "input" && this.type == "checkbox") {
158 st.c("value").t("1");
160 st.c("value").t("0");
163 // if this has value then
164 st.c("value").t($(this).val()).up();
171 function displayResult(result) {
172 var status = $(result).find("command").attr("status");
174 $("#output").empty();
175 $(result).find("command > *").each(function(index, e) {
176 if ($(e).is("note")) {
177 addNote("#output", $(e).text(), $(e).attr("type"));
178 } else if ($(e).is("x[xmlns=jabber:x:data]")) {
179 addForm("#output", e);
182 if (status == "executing") {
183 $("#output").append("<input type='button' disabled='true' id='prevButton' value='Prev'/>"+
184 "<input type='button' disabled='true' id='nextButton' value='Next'/>"+
185 "<input type='button' disabled='true' id='completeButton' value='Complete'/>"+
186 "<input type='button' id='executeButton' value='Execute'/>"+
187 "<input type='button' id='cancelButton' value='Cancel'/>");
188 for (kind in ['prev', 'next', 'complete']) {
189 if ($(result).find('actions ' + kind).length > 0)
190 $('#' + kind + 'Button').attr("disabled", "false");
192 $('#executeButton').bind("click", function() {
193 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
194 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "execute" });
195 serializeToDataform($('form'), execIQ);
196 connection.sendIQ(execIQ, displayResult);
199 $('#cancelButton').bind("click", function() {
200 cancelCommand(displayResult);
203 $('#queryForm').unbind('submit');
204 $('#queryForm').bind('submit', function (event) {
205 cancelCommand(function(result) {
206 $('#queryForm').unbind('submit');
207 $('#queryForm').bind('submit', function (event) {
208 queryJID = $('#queryJID').val();
210 event.preventDefault();
213 queryJID = $('#queryJID').val();
215 event.preventDefault();
219 input = $("<input type='button' value='Start over'/>").bind("click", function() {
220 $('#output').empty();
225 $("#output").append(input);
229 function runCommand(event) {
230 cmdNode = $(this).attr("id"); // Save node of executed command (in global var)
231 var execIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
232 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, action: "execute" });
233 connection.sendIQ(execIQ, function(result) {
234 sessionid = $(result).find("command").attr("sessionid");
235 displayResult(result);
237 event.preventDefault();
240 function cancelCommand(callback) {
241 var cancelIQ = $iq({ type: "set", to: queryJID, id: connection.getUniqueId() })
242 .c("command", { xmlns: Strophe.NS.ADHOC, node: cmdNode, sessionid: sessionid, action: "cancel" });
245 connection.sendIQ(cancelIQ, callback);
248 function getCommandNodes() {
249 var nodesIQ = $iq({ type: "get", to: queryJID, id: "nodes1" }).c("query", {xmlns: Strophe.NS.DISCO_ITEMS, node: Strophe.NS.ADHOC});
250 connection.sendIQ(nodesIQ, function(result) {
251 $('#output').append("<ul id='items'></ul>");
252 $(result).find("item").each(function(index, e) {
253 link = $("<a href='#' id='" + $(e).attr("node") + "'>" + $(e).attr("name") + "</a>").click(runCommand)
254 item = $("<li></li>").append(link);
255 $("#items").append(item);
260 function checkFeatures() {
261 featureIQ = $iq({ type: "get", to: queryJID, id: "features1" }).c("query", {xmlns: Strophe.NS.DISCO_INFO});
262 connection.sendIQ(featureIQ, function(result) {
263 if ($(result).find("feature[var='" + Strophe.NS.ADHOC + "']").length > 0) {
264 $('#output').append("<p>This entitiy does support AdHoc commands</p>");
266 $('#output').append("<p>This entitiy does NOT support AdHoc commands</p>");
269 $('#output').empty();
273 function showConnect() {
275 var pass = $('#pass');
276 var button = $('#connect').get(0);
278 button.value = 'connect';
282 $('#cred label').show();
283 $('#cred br').show();
284 $('#output').empty();
287 function showDisconnect() {
289 var pass = $('#pass');
290 var button = $('#connect').get(0);
292 button.value = 'disconnect';
295 $('#cred label').hide();
296 $('#cred br').hide();
299 $(document).ready(function () {
300 connection = new Strophe.Connection(BOSH_SERVICE);
302 $('#log_container').show();
303 connection.rawInput = rawInput;
304 connection.rawOutput = rawOutput;
307 $("#log_toggle").click(function () {
311 $('#cred').bind('submit', function (event) {
312 var button = $('#connect').get(0);
314 var pass = $('#pass');
315 localJID = jid.get(0).value;
317 if (button.value == 'connect') {
320 connection.connect(localJID,
324 connection.disconnect();
326 event.preventDefault();
329 $('#queryForm').bind('submit', function (event) {
330 queryJID = $('#queryJID').val();
332 event.preventDefault();
336 onunload = function() {
338 connection.disconnect();