strophejs-plugins
Version:
Strophe.js community plugins
632 lines (561 loc) • 18.1 kB
JavaScript
// Generated by CoffeeScript 1.8.0
/*
This program is distributed under the terms of the MIT license.
Copyright 2012 - 2014 (c) Markus Kohlhase <mail@markus-kohlhase.de>
*/
(function() {
var JOAPClass, JOAPError, JOAPObject, JOAPServer, JOAP_NS, RPC_NS, add, addRPCElements, addXMLAttributes, conn, createEventWrapper, createIq, del, describe, edit, getAddress, methodCall, onError, parseAttributeDescription, parseAttributes, parseDesc, parseDescription, parseMethodDescription, parseNewAddress, parseRPCParams, parseSearch, read, search, searchAndRead, sendRequest, subscribe, unsubscribe,
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
JOAP_NS = "jabber:iq:joap";
RPC_NS = "jabber:iq:rpc";
conn = null;
onError = function(cb) {
if (cb == null) {
cb = function() {};
}
return function(iq) {
var code, err, msg;
err = iq.getElementsByTagName("error")[0];
if (err != null) {
code = err.getAttribute("code") * 1;
msg = err.textContent;
if (code === 503) {
msg = "JOAP server is unavailable";
}
return cb(iq, new JOAPError(msg, code));
} else {
return cb(iq, new JOAPError("Unknown error"));
}
};
};
addXMLAttributes = function(iq, attrs) {
var k, v, _results;
if (attrs == null) {
return;
}
if (attrs instanceof Array) {
return typeof console !== "undefined" && console !== null ? typeof console.warn === "function" ? console.warn("No attributes added: attribute parameter is not an object") : void 0 : void 0;
} else if (typeof attrs === "object") {
_results = [];
for (k in attrs) {
v = attrs[k];
_results.push(iq.c("attribute").c("name").t(k).up().cnode(conn.rpc._convertToXML(v)).up().up());
}
return _results;
}
};
addRPCElements = function(iq, method, params) {
var p, _i, _len, _results;
if (params == null) {
params = [];
}
if (typeof method !== "string") {
throw new TypeError;
}
iq.c("methodCall").c("methodName").t(method).up();
if (!(params instanceof Array)) {
if (typeof console !== "undefined" && console !== null) {
if (typeof console.warn === "function") {
console.warn("No parameters added: parameter is not an array");
}
}
return;
}
if (params.length > 0) {
iq.c("params");
_results = [];
for (_i = 0, _len = params.length; _i < _len; _i++) {
p = params[_i];
_results.push(iq.c("param").cnode(conn.rpc._convertToXML(p)).up().up());
}
return _results;
}
};
parseAttributes = function(iq) {
var a, attrs, data, key, _i, _len;
attrs = iq.getElementsByTagName("attribute");
data = {};
for (_i = 0, _len = attrs.length; _i < _len; _i++) {
a = attrs[_i];
key = a.getElementsByTagName("name")[0].textContent;
data[key] = conn.rpc._convertFromXML(a.getElementsByTagName("value")[0]);
}
return data;
};
parseRPCParams = function(iq) {
return conn.rpc._convertFromXML(iq.getElementsByTagName("param")[0].getElementsByTagName("value")[0]);
};
parseNewAddress = function(iq) {
var a;
a = iq.getElementsByTagName("newAddress")[0];
if (a != null) {
return new JID(a.textContent).toString();
} else {
return void 0;
}
};
parseSearch = function(iq) {
var i, items, _i, _len, _results;
items = iq.getElementsByTagName("item");
_results = [];
for (_i = 0, _len = items.length; _i < _len; _i++) {
i = items[_i];
_results.push(new JID(i.textContent).toString());
}
return _results;
};
parseAttributeDescription = function(d) {
var _ref, _ref1;
return {
name: (_ref = d.getElementsByTagName("name")[0]) != null ? _ref.textContent : void 0,
type: (_ref1 = d.getElementsByTagName("type")[0]) != null ? _ref1.textContent : void 0,
desc: parseDesc(d.getElementsByTagName("desc"))
};
};
parseMethodDescription = function(d) {
var _ref, _ref1;
return {
name: (_ref = d.getElementsByTagName("name")[0]) != null ? _ref.textContent : void 0,
returnType: (_ref1 = d.getElementsByTagName("returnType")[0]) != null ? _ref1.textContent : void 0,
desc: parseDesc(d.getElementsByTagName("desc"))
};
};
parseDesc = function(desc) {
var c, res, _i, _len;
res = {};
if (desc instanceof NodeList) {
for (_i = 0, _len = desc.length; _i < _len; _i++) {
c = desc[_i];
res[c.getAttribute("xml:lang")] = c.textContent;
}
} else {
res.desc[desc.getAttribute("xml:lang")] = desc.textContent;
}
return res;
};
parseDescription = function(iq) {
var ad, c, describe, md, result, _i, _len, _ref;
result = {
desc: {},
attributes: {},
methods: {},
classes: []
};
describe = iq.getElementsByTagName("describe")[0];
if (describe != null) {
_ref = describe.childNodes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
c = _ref[_i];
switch (c.tagName.toLowerCase()) {
case "desc":
result.desc[c.getAttribute("xml:lang")] = c.textContent;
break;
case "attributedescription":
ad = parseAttributeDescription(c);
result.attributes[ad.name] = ad;
break;
case "methoddescription":
md = parseMethodDescription(c);
result.methods[md.name] = md;
break;
case "superclass":
result.superclass = new JID(c.textContent).toString();
break;
case "timestamp":
result.timestamp = c.textContent;
break;
case "class":
classes.push = c.textContent;
}
}
}
return result;
};
getAddress = function(clazz, service, instance) {
return (new JID(clazz, service, instance)).toString();
};
createIq = function(type, to, customAttrs) {
var attrs, iqType, k, v, xmlns;
iqType = (type === "read" || type === "search" || type === "describe") ? "get" : "set";
xmlns = type === "query" ? RPC_NS : JOAP_NS;
attrs = {
xmlns: xmlns
};
if (customAttrs != null) {
for (k in customAttrs) {
v = customAttrs[k];
if (v != null) {
attrs[k] = v;
}
}
}
return $iq({
to: to,
type: iqType
}).c(type, attrs);
};
sendRequest = function(type, to, cb, opt) {
var iq, success;
if (opt == null) {
opt = {};
}
iq = createIq(type, to, opt.attrs);
if (typeof opt.beforeSend === "function") {
opt.beforeSend(iq);
}
success = function(res) {
return typeof cb === "function" ? cb(res, null, typeof opt.onResult === "function" ? opt.onResult(res) : void 0) : void 0;
};
return conn.sendIQ(iq, success, onError(cb));
};
describe = function(id, cb) {
return sendRequest("describe", id, cb, {
onResult: parseDescription
});
};
read = function(instance, limits, cb) {
if (typeof limits === "function") {
cb = limits;
limits = null;
}
return sendRequest("read", instance, cb, {
beforeSend: function(iq) {
var l, _i, _len, _results;
if (limits instanceof Array) {
_results = [];
for (_i = 0, _len = limits.length; _i < _len; _i++) {
l = limits[_i];
_results.push(iq.c("name").t(l).up());
}
return _results;
}
},
onResult: parseAttributes
});
};
add = function(clazz, attrs, cb) {
if (typeof attrs === "function") {
cb = attrs;
}
return sendRequest("add", clazz, cb, {
beforeSend: function(iq) {
return addXMLAttributes(iq, attrs);
},
onResult: parseNewAddress
});
};
edit = function(instance, attrs, cb) {
return sendRequest("edit", instance, cb, {
beforeSend: function(iq) {
return addXMLAttributes(iq, attrs);
},
onResult: parseNewAddress
});
};
search = function(clazz, attrs, cb) {
if (typeof attrs === "function") {
cb = attrs;
attrs = null;
}
return sendRequest("search", clazz, cb, {
beforeSend: function(iq) {
return addXMLAttributes(iq, attrs);
},
onResult: parseSearch
});
};
subscribe = function(clazz, cb, handler, opt) {
var ref;
if (opt == null) {
opt = {};
}
if (handler != null) {
ref = conn.addHandler(handler, JOAP_NS, "message");
}
return sendRequest("subscribe", clazz, cb, {
attrs: {
bare: opt.bare,
type: opt.type
},
onResult: function(iq) {
if (handler != null) {
if (iq.getAttribute('type' === 'error')) {
return conn.deleteHandler(ref);
} else {
return ref;
}
}
}
});
};
unsubscribe = function(clazz, cb, handler, opt) {
if (opt == null) {
opt = {};
}
return sendRequest("unsubscribe", clazz, cb, {
attrs: {
bare: opt.bare,
type: opt.type
},
onResult: function(iq) {
if (handler != null) {
return conn.deleteHandler(handler);
}
}
});
};
searchAndRead = function(clazz, attrs, limits, cb) {
if (typeof limits === "function") {
cb = limits;
limits = null;
}
if (typeof attrs === "function" && (limits == null)) {
cb = attrs;
attrs = null;
} else if (attrs instanceof Array && (limits == null)) {
limits = attrs;
attrs = null;
}
return search(clazz, attrs, function(iq, err, res) {
var count, id, objects, readCB, _i, _len, _results;
if (err != null) {
return cb(err);
} else {
objects = [];
count = res.length;
if (count > 0) {
readCB = function(iq, err, o) {
if (err != null) {
return cb(err);
} else {
count--;
objects.push(o);
if (count === 0) {
return cb(null, objects);
}
}
};
_results = [];
for (_i = 0, _len = res.length; _i < _len; _i++) {
id = res[_i];
_results.push((function(id) {
return read(id, limits, readCB);
})(id));
}
return _results;
} else {
return cb(null, objects);
}
}
});
};
del = function(instance, cb) {
return sendRequest("delete", instance, cb);
};
methodCall = function(method, address, params, cb) {
return sendRequest("query", address, cb, {
beforeSend: function(iq) {
return addRPCElements(iq, method, params);
},
onResult: parseRPCParams
});
};
createEventWrapper = function(type, jid, fn) {
var match;
if (typeof fn !== "function") {
return (function() {
return false;
});
}
match = (function() {
switch (type) {
case "server":
return function(from) {
return from.domain === jid.domain;
};
case "class":
return function(from) {
return from.user === jid.user && from.domain === jid.domain;
};
case "instance":
return function(from) {
return from.equals(jid);
};
}
})();
return function(xml) {
var from;
from = new JID(xml.getAttribute('from'));
if (match(from)) {
return fn(xml, parseAttributes(xml.getElementsByTagName("event")[0]), from);
} else {
return true;
}
};
};
JOAPError = (function(_super) {
__extends(JOAPError, _super);
function JOAPError(message, code) {
this.message = message;
this.code = code;
this.name = "JOAPError";
}
return JOAPError;
})(Error);
JOAPServer = (function() {
function JOAPServer(service) {
this.jid = new JID(service);
}
JOAPServer.prototype.describe = function(clazz, instance, cb) {
if (typeof clazz === "function") {
cb = clazz;
clazz = instance = null;
} else if (typeof instance === "function") {
cb = instance;
instance = null;
}
return describe(getAddress(clazz, this.jid.domain, instance), cb);
};
JOAPServer.prototype.add = function(clazz, attrs, cb) {
return add(getAddress(clazz, this.jid.domain), attrs, cb);
};
JOAPServer.prototype.read = function(clazz, instance, limits, cb) {
return read(getAddress(clazz, this.jid.domain, instance), limits, cb);
};
JOAPServer.prototype.edit = function(clazz, instance, attrs, cb) {
return edit(getAddress(clazz, this.jid.domain, instance), attrs, cb);
};
JOAPServer.prototype["delete"] = function(clazz, instance, cb) {
return del(getAddress(clazz, this.jid.domain, instance), cb);
};
JOAPServer.prototype.search = function(clazz, attrs, cb) {
return search(getAddress(clazz, this.jid.domain), attrs, cb);
};
JOAPServer.prototype.searchAndRead = function(clazz, attrs, limits, cb) {
return searchAndRead(getAddress(clazz, this.jid.domain), attrs, limits, cb);
};
JOAPServer.prototype.methodCall = function(method, clazz, instance, params, cb) {
if (typeof clazz === "function") {
cb = clazz;
clazz = instance = params = null;
} else if (typeof instance === "function") {
cb = instance;
instance = params = null;
} else if (typeof params === "function") {
cb = params;
params = null;
}
return methodCall(method, getAddress(clazz, this.jid.domain, instance), params, cb);
};
return JOAPServer;
})();
JOAPObject = (function() {
function JOAPObject(id) {
this.jid = new JID(id);
}
JOAPObject.prototype.read = function(limits, cb) {
return read(this.jid.toString(), limits, cb);
};
JOAPObject.prototype.edit = function(attrs, cb) {
return edit(this.jid.toString(), attrs, cb);
};
JOAPObject.prototype.describe = function(cb) {
return describe(this.jid.toString(), cb);
};
JOAPObject.prototype.subscribe = function(cb, handler, opt) {
var wrapper;
wrapper = createEventWrapper("instance", this.jid, handler);
return subscribe(this.jid.toString(), cb, wrapper, opt);
};
JOAPObject.prototype.unsubscribe = function(cb, handlerRef, opt) {
return unsubscribe(this.jid.toString(), cb, handlerRef, opt);
};
JOAPObject.prototype.methodCall = function(method, params, cb) {
if (typeof params === "function") {
cb = params;
params = null;
}
return methodCall(method, this.jid.toString(), params, cb);
};
return JOAPObject;
})();
JOAPClass = (function() {
function JOAPClass(id) {
this.jid = new JID(id);
}
JOAPClass.prototype.describe = function(instance, cb) {
if (typeof instance === "function") {
cb = instance;
instance = null;
}
return describe(getAddress(this.jid.user, this.jid.domain, instance), cb);
};
JOAPClass.prototype.add = function(attrs, cb) {
return add(getAddress(this.jid.user, this.jid.domain), attrs, cb);
};
JOAPClass.prototype.read = function(instance, limits, cb) {
return read(getAddress(this.jid.user, this.jid.domain, instance), limits, cb);
};
JOAPClass.prototype.edit = function(instance, attrs, cb) {
return edit(getAddress(this.jid.user, this.jid.domain, instance), attrs, cb);
};
JOAPClass.prototype["delete"] = function(instance, cb) {
return del(getAddress(this.jid.user, this.jid.domain, instance), cb);
};
JOAPClass.prototype.search = function(attrs, cb) {
return search(getAddress(this.jid.user, this.jid.domain), attrs, cb);
};
JOAPClass.prototype.searchAndRead = function(attrs, limits, cb) {
return searchAndRead(getAddress(this.jid.user, this.jid.domain), attrs, limits, cb);
};
JOAPClass.prototype.subscribe = function(cb, handler, opt) {
var wrapper;
wrapper = createEventWrapper("class", this.jid, handler);
return subscribe(getAddress(this.jid.user, this.jid.domain), cb, wrapper, opt);
};
JOAPClass.prototype.unsubscribe = function(cb, handlerRef, opt) {
return unsubscribe(getAddress(this.jid.user, this.jid.domain), cb, handlerRef, opt);
};
JOAPClass.prototype.methodCall = function(method, instance, params, cb) {
if (typeof instance === "function") {
cb = instance;
instance = params = null;
} else if (typeof params === "function") {
cb = params;
params = null;
}
return methodCall(method, getAddress(this.jid.user, this.jid.domain, instance), params, cb);
};
return JOAPClass;
})();
Strophe.addConnectionPlugin('joap', (function() {
var init;
init = function(c) {
conn = c;
Strophe.addNamespace("JOAP", JOAP_NS);
if (!conn.hasOwnProperty("disco")) {
return Strophe.warn("You need the discovery plugin to have JOAP fully implemented.");
} else {
conn.disco.addIdentity("automation", "joap");
return conn.disco.addFeature(Strophe.NS.JOAP);
}
};
/* public API */
return {
init: init,
describe: describe,
add: add,
read: read,
edit: edit,
"delete": del,
search: search,
searchAndRead: searchAndRead,
methodCall: methodCall,
JOAPError: JOAPError,
JOAPServer: JOAPServer,
JOAPObject: JOAPObject,
JOAPClass: JOAPClass
};
})());
}).call(this);