strophejs-plugin-disco
Advanced tools
Comparing version 0.0.1 to 0.0.2
# Changelog | ||
## 0.0.2 (2017-07-14) | ||
- Add UMD packaging to support more NPM environments | ||
## 0.0.1 (2017-02-16) | ||
- First release as a standalone NPM package. |
@@ -1,137 +0,234 @@ | ||
(function(Strophe,$) { | ||
var DiscoNode = function(disco,cfg) { | ||
this.disco = disco; | ||
$.extend(this, cfg); | ||
}; | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('strophe.js')) : | ||
typeof define === 'function' && define.amd ? define(['strophe.js'], factory) : | ||
(factory(global.window)); | ||
}(this, (function (strophe_js) { 'use strict'; | ||
DiscoNode.prototype.parseRequest = function(iq) { | ||
if (iq.tree) { return $(Strophe.serialize(iq.tree())); } | ||
return $(iq); | ||
}; | ||
strophe_js.Strophe.addConnectionPlugin('disco', | ||
{ | ||
_connection: null, | ||
_identities : [], | ||
_features : [], | ||
_items : [], | ||
/** Function: init | ||
* Plugin init | ||
* | ||
* Parameters: | ||
* (Strophe.Connection) conn - Strophe connection | ||
*/ | ||
init: function(conn) | ||
{ | ||
this._connection = conn; | ||
this._identities = []; | ||
this._features = []; | ||
this._items = []; | ||
// disco info | ||
conn.addHandler(this._onDiscoInfo.bind(this), strophe_js.Strophe.NS.DISCO_INFO, 'iq', 'get', null, null); | ||
// disco items | ||
conn.addHandler(this._onDiscoItems.bind(this), strophe_js.Strophe.NS.DISCO_ITEMS, 'iq', 'get', null, null); | ||
}, | ||
/** Function: addIdentity | ||
* See http://xmpp.org/registrar/disco-categories.html | ||
* Parameters: | ||
* (String) category - category of identity (like client, automation, etc ...) | ||
* (String) type - type of identity (like pc, web, bot , etc ...) | ||
* (String) name - name of identity in natural language | ||
* (String) lang - lang of name parameter | ||
* | ||
* Returns: | ||
* Boolean | ||
*/ | ||
addIdentity: function(category, type, name, lang) | ||
{ | ||
for (var i=0; i<this._identities.length; i++) | ||
{ | ||
if (this._identities[i].category == category && | ||
this._identities[i].type == type && | ||
this._identities[i].name == name && | ||
this._identities[i].lang == lang) | ||
{ | ||
return false; | ||
} | ||
} | ||
this._identities.push({category: category, type: type, name: name, lang: lang}); | ||
return true; | ||
}, | ||
/** Function: addFeature | ||
* | ||
* Parameters: | ||
* (String) var_name - feature name (like jabber:iq:version) | ||
* | ||
* Returns: | ||
* boolean | ||
*/ | ||
addFeature: function(var_name) | ||
{ | ||
for (var i=0; i<this._features.length; i++) | ||
{ | ||
if (this._features[i] == var_name) | ||
return false; | ||
} | ||
this._features.push(var_name); | ||
return true; | ||
}, | ||
/** Function: removeFeature | ||
* | ||
* Parameters: | ||
* (String) var_name - feature name (like jabber:iq:version) | ||
* | ||
* Returns: | ||
* boolean | ||
*/ | ||
removeFeature: function(var_name) | ||
{ | ||
for (var i=0; i<this._features.length; i++) | ||
{ | ||
if (this._features[i] === var_name){ | ||
this._features.splice(i,1); | ||
return true; | ||
} | ||
} | ||
return false; | ||
}, | ||
/** Function: addItem | ||
* | ||
* Parameters: | ||
* (String) jid | ||
* (String) name | ||
* (String) node | ||
* (Function) call_back | ||
* | ||
* Returns: | ||
* boolean | ||
*/ | ||
addItem: function(jid, name, node, call_back) | ||
{ | ||
if (node && !call_back) | ||
return false; | ||
this._items.push({jid: jid, name: name, node: node, call_back: call_back}); | ||
return true; | ||
}, | ||
/** Function: info | ||
* Info query | ||
* | ||
* Parameters: | ||
* (Function) call_back | ||
* (String) jid | ||
* (String) node | ||
*/ | ||
info: function(jid, node, success, error, timeout) | ||
{ | ||
var attrs = {xmlns: strophe_js.Strophe.NS.DISCO_INFO}; | ||
if (node) | ||
attrs.node = node; | ||
DiscoNode.prototype.fromTo= function(iq) { | ||
var to = iq.attr('from'), id = iq.attr('id'), res; | ||
return $iq({to: to, type: 'result', id: id}); | ||
}; | ||
var info = strophe_js.$iq({from:this._connection.jid, | ||
to:jid, type:'get'}).c('query', attrs); | ||
this._connection.sendIQ(info, success, error, timeout); | ||
}, | ||
/** Function: items | ||
* Items query | ||
* | ||
* Parameters: | ||
* (Function) call_back | ||
* (String) jid | ||
* (String) node | ||
*/ | ||
items: function(jid, node, success, error, timeout) | ||
{ | ||
var attrs = {xmlns: strophe_js.Strophe.NS.DISCO_ITEMS}; | ||
if (node) | ||
attrs.node = node; | ||
DiscoNode.prototype.addFirstChild = function(req,res) { | ||
var child = req.find('> *:eq(0)'), childAttr = {}; | ||
if (child.length === 0) { return ; } | ||
if (child.attr('xmlns')) { childAttr.xmlns = child.attr('xmlns'); } | ||
if (child.attr('node')) { childAttr.node = child.attr('node'); } | ||
if ($.isEmptyObject(childAttr)) { res.c(child[0].tagName); } | ||
else { res.c(child[0].tagName, childAttr); } | ||
}; | ||
var items = strophe_js.$iq({from:this._connection.jid, | ||
to:jid, type:'get'}).c('query', attrs); | ||
this._connection.sendIQ(items, success, error, timeout); | ||
}, | ||
DiscoNode.prototype.reply = function(iq) { | ||
var req = this.parseRequest(iq); | ||
var res = this.fromTo(req); | ||
this.addFirstChild(req,res); | ||
this.addContent(req,res); | ||
return res; | ||
}; | ||
/** PrivateFunction: _buildIQResult | ||
*/ | ||
_buildIQResult: function(stanza, query_attrs) | ||
{ | ||
var id = stanza.getAttribute('id'); | ||
var from = stanza.getAttribute('from'); | ||
var iqresult = strophe_js.$iq({type: 'result', id: id}); | ||
/// DISCO_INFO | ||
DiscoInfoNode = function() { DiscoNode.apply(this,arguments); }; | ||
DiscoInfoNode.prototype = new DiscoNode(); | ||
DiscoInfoNode.prototype.addContent = function(req,res) { | ||
var nodes = this.features || this.disco.features; | ||
var identity = this.identity || this.disco.identity; | ||
res.c('identity', identity).up(); | ||
$.each(nodes, function(node){ | ||
res.c('feature', { 'var' : node}).up(); | ||
}); | ||
}; | ||
if (from !== null) { | ||
iqresult.attrs({to: from}); | ||
} | ||
/// DISCO_ITEMS | ||
DiscoItemsNode = function() { DiscoNode.apply(this,arguments); }; | ||
DiscoItemsNode.prototype = new DiscoNode(); | ||
DiscoItemsNode.prototype.addContent = function(req,res) { | ||
var items = this.items || this.disco.items; | ||
$.each(items, function(i,item){ | ||
if(!item.jid) { item.jid = this.disco._conn.jid; } | ||
res.c('item', item).up(); | ||
}.bind(this)); | ||
}; | ||
return iqresult.c('query', query_attrs); | ||
}, | ||
/// NODE_NOT_FOUND | ||
DiscoNodeNotFound = function() { DiscoNode.apply(this,arguments); }; | ||
DiscoNodeNotFound.prototype = new DiscoNode(); | ||
DiscoNodeNotFound.prototype.addContent = function(req,res) { | ||
res.c('error', { type: 'cancel'}); | ||
res.c('item-not-found', { xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas' }); | ||
}; | ||
/** PrivateFunction: _onDiscoInfo | ||
* Called when receive info request | ||
*/ | ||
_onDiscoInfo: function(stanza) | ||
{ | ||
var node = stanza.getElementsByTagName('query')[0].getAttribute('node'); | ||
var attrs = {xmlns: strophe_js.Strophe.NS.DISCO_INFO}; | ||
var i; | ||
if (node) | ||
{ | ||
attrs.node = node; | ||
} | ||
var iqresult = this._buildIQResult(stanza, attrs); | ||
for (i=0; i<this._identities.length; i++) | ||
{ | ||
attrs = {category: this._identities[i].category, | ||
type : this._identities[i].type}; | ||
if (this._identities[i].name) | ||
attrs.name = this._identities[i].name; | ||
if (this._identities[i].lang) | ||
attrs['xml:lang'] = this._identities[i].lang; | ||
iqresult.c('identity', attrs).up(); | ||
} | ||
for (i=0; i<this._features.length; i++) | ||
{ | ||
iqresult.c('feature', {'var':this._features[i]}).up(); | ||
} | ||
this._connection.send(iqresult.tree()); | ||
return true; | ||
}, | ||
/** PrivateFunction: _onDiscoItems | ||
* Called when receive items request | ||
*/ | ||
_onDiscoItems: function(stanza) | ||
{ | ||
var query_attrs = {xmlns: strophe_js.Strophe.NS.DISCO_ITEMS}; | ||
var node = stanza.getElementsByTagName('query')[0].getAttribute('node'); | ||
var items, i; | ||
if (node) | ||
{ | ||
query_attrs.node = node; | ||
items = []; | ||
for (i = 0; i < this._items.length; i++) | ||
{ | ||
if (this._items[i].node == node) | ||
{ | ||
items = this._items[i].call_back(stanza); | ||
break; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
items = this._items; | ||
} | ||
var iqresult = this._buildIQResult(stanza, query_attrs); | ||
for (i = 0; i < items.length; i++) | ||
{ | ||
var attrs = {jid: items[i].jid}; | ||
if (items[i].name) | ||
attrs.name = items[i].name; | ||
if (items[i].node) | ||
attrs.node = items[i].node; | ||
iqresult.c('item', attrs).up(); | ||
} | ||
this._connection.send(iqresult.tree()); | ||
return true; | ||
} | ||
}); | ||
function noop(stanza) { | ||
if (console) { console.log(stanza); } | ||
} | ||
Strophe.Disco = { | ||
DiscoNode: DiscoNode, | ||
DiscoInfoNode: DiscoInfoNode, | ||
DiscoNodeNotFound: DiscoNodeNotFound, | ||
noop: noop | ||
}; | ||
})(Strophe, jQuery); | ||
(function(Strophe,$) { | ||
var INFO = Strophe.NS.DISCO_INFO; | ||
var ITEMS = Strophe.NS.DISCO_ITEMS; | ||
var DiscoNode = Strophe.Disco.DiscoNode; | ||
var DiscoInfoNode = Strophe.Disco.DiscoInfoNode; | ||
var DiscoNodeNotFound = Strophe.Disco.DiscoNodeNotFound; | ||
var noop = Strophe.Disco.noop; | ||
function request(conn, type, args) { | ||
var to = args[0], node = args[1], cb = args[2], err = args[3], | ||
q = { xmlns: type }; | ||
if(typeof node === 'function') { err = cb; cb = node; node = undefined; } | ||
if(node) { q.node = node; } | ||
var iq = $iq({to: to, 'type': 'get'}).c('query',q); | ||
conn.sendIQ(iq, cb || noop, err || noop); | ||
} | ||
function reply(iq) { | ||
var node = $('query',iq).attr('node') || $('query',iq).attr('xmlns'); | ||
var nodeImpl = this.features[node] || new DiscoNodeNotFound(); | ||
if($.isPlainObject(nodeImpl)) { | ||
var xmlns = $('query',iq).attr('xmlns'); | ||
var ctr = xmlns === INFO ? DiscoInfoNode : DiscoItemsNode; | ||
nodeImpl = new ctr(this,nodeImpl); | ||
} | ||
this._conn.send(nodeImpl.reply(iq)); | ||
return true; | ||
} | ||
var disco = { | ||
_conn: null, | ||
init: function(conn) { | ||
this.jid = conn.jid; | ||
this.features = {}; | ||
this.identity = { name: 'strophe' }; | ||
this.features[INFO] = new DiscoInfoNode(this); | ||
this.features[ITEMS] = new DiscoItemsNode(this); | ||
this._conn = conn; | ||
}, | ||
statusChanged: function(status) { | ||
if (status === Strophe.Status.CONNECTED) { | ||
this._conn.addHandler(reply.bind(this), INFO, 'iq', 'get'); | ||
this._conn.addHandler(reply.bind(this), ITEMS, 'iq', 'get'); | ||
} | ||
}, | ||
info: function(to, node, callback) { | ||
request(this._conn, INFO, arguments); | ||
}, | ||
items: function(to, node, callback) { | ||
request(this._conn, ITEMS, arguments); | ||
}, | ||
addNode: function(node, args) { | ||
if(this.features[node]) { throw node + ' exists'; } | ||
this.features[node] = args; | ||
} | ||
}; | ||
Strophe.addConnectionPlugin('disco', disco); | ||
})(Strophe,jQuery); | ||
}))); | ||
//# sourceMappingURL=strophe.disco.js.map |
{ | ||
"name": "strophejs-plugin-disco", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A strophe.js plugin for XEP-0030 Service Discovery", | ||
"main": "strophe.disco.js", | ||
"main": "lib/strophe.disco.js", | ||
"scripts": { | ||
"build": "rollup src/strophe.disco.js -f umd -g strophe.js:window -o lib/strophe.disco.js -m", | ||
"prepublish": "npm run --silent build", | ||
"test": "jasmine mon" | ||
@@ -16,3 +18,4 @@ }, | ||
"strophe", | ||
"strophe.js" | ||
"strophe.js", | ||
"disco" | ||
], | ||
@@ -25,5 +28,8 @@ "author": "The strophe plugin developers", | ||
"homepage": "https://github.com/strophe/strophejs-plugin-disco#readme", | ||
"dependencies": { | ||
"devDependencies": { | ||
"rollup": "^0.45.2" | ||
}, | ||
"peerDependencies": { | ||
"strophe.js": "^1.2.12" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
52735
911
75
0
1
1
- Removedstrophe.js@^1.2.12