Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jun 9, 2012
2 parents d35c544 + b522e38 commit dd14206
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 28 deletions.
3 changes: 3 additions & 0 deletions archive/README.markdown
Expand Up @@ -3,6 +3,9 @@
strophe.archive.js is a plugin to provide Message Archiving
( [XEP-0136](http://xmpp.org/extensions/xep-0136.html) ).

## Notes on Browser support
If you want to support Browsers which do not support setISO8601 on a Date-Object (like e.g. FF 11), include iso8601_support.js, too.

## Usage

## ToDo
Expand Down
32 changes: 32 additions & 0 deletions archive/iso8601_support.js
@@ -0,0 +1,32 @@
if (!Date.prototype.setISO6801) {
// from http://stackoverflow.com/questions/5249216/javascript-timestamp-from-iso8061
Date.prototype.setISO8601 = function(dString){
var regexp = /(\d\d\d\d)(-)?(\d\d)(-)?(\d\d)(T)?(\d\d)(:)?(\d\d)(:)?(\d\d)(\.\d+)?(Z|([+-])(\d\d)(:)?(\d\d))/;
if (dString.toString().match(new RegExp(regexp))) {
var d = dString.match(new RegExp(regexp));
var offset = 0;
this.setUTCDate(1);
this.setUTCFullYear(parseInt(d[1],10));
this.setUTCMonth(parseInt(d[3],10) - 1);
this.setUTCDate(parseInt(d[5],10));
this.setUTCHours(parseInt(d[7],10));
this.setUTCMinutes(parseInt(d[9],10));
this.setUTCSeconds(parseInt(d[11],10));
if (d[12]) {
this.setUTCMilliseconds(parseFloat(d[12]) * 1000);
}
else {
this.setUTCMilliseconds(0);
}
if (d[13] != 'Z') {
offset = (d[15] * 60) + parseInt(d[17],10);
offset *= ((d[14] == '-') ? -1 : 1);
this.setTime(this.getTime() - offset * 60 * 1000);
}
}
else {
this.setTime(Date.parse(dString));
}
return this;
}
}
4 changes: 2 additions & 2 deletions archive/strophe.archive.js
Expand Up @@ -31,8 +31,7 @@ Strophe.ArchivedCollection = function(connection, jid, start) {
this.connection = connection;
this.jid = jid;
this.start = start;
this.startDate = new Date();
this.startDate.setISO8601(start);
this.startDate = (new Date()).setISO8601(start);
};

Strophe.ArchivedCollection.prototype = {
Expand Down Expand Up @@ -86,3 +85,4 @@ Strophe.ArchivedMessage = function(timestamp, from, to, body) {

Strophe.ArchivedMessage.prototype = {
};

2 changes: 2 additions & 0 deletions caps/strophe.caps.js
Expand Up @@ -145,6 +145,8 @@
node = c.getAttribute('node');
if (!this._knownCapabilities[ver]) {
return this._requestCapabilities(from, node, ver);
} else {
this._jidVerIndex[from] = ver;
}
if (!this._jidVerIndex[from] || !this._jidVerIndex[from] !== ver) {
this._jidVerIndex[from] = ver;
Expand Down
5 changes: 3 additions & 2 deletions ping/README.markdown
Expand Up @@ -16,10 +16,11 @@ You can also add a ping handler to receive pings:

Within your ping handler function you surely want to reply with a pong iq:

myHandler = function( ping ){
handler = function( ping ){
...
connection.ping.pong( ping )
connection.ping.pong( ping );
...
return true;
}

## ToDo
Expand Down
11 changes: 5 additions & 6 deletions ping/strophe.ping.js
Expand Up @@ -25,9 +25,10 @@ Strophe.addConnectionPlugin('ping', {
* Function: ping
*
* Parameters:
* (String) to - the JID you want to ping
* (Function) success - Callback function on success
* (Function) error - Callback function on error
* (String) to - The JID you want to ping
* (Function) success - Callback function on success
* (Function) error - Callback function on error
* (Integer) timeout - Timeout in milliseconds
*/
ping: function(jid, success, error, timeout)
{
Expand All @@ -41,9 +42,7 @@ Strophe.addConnectionPlugin('ping', {
* Function: pong
*
* Parameters:
* (Object) ping -
* (Function) success -
* (Function) error -
* (Object) ping - The ping stanza from the server
*/
pong: function(ping)
{
Expand Down
164 changes: 146 additions & 18 deletions roster/strophe.roster.js
Expand Up @@ -25,6 +25,7 @@ Strophe.addConnectionPlugin('roster',
* name : "",
* jid : "",
* subscription : "",
* ask : "",
* groups : ["", ""],
* resources : {
* myresource : {
Expand Down Expand Up @@ -52,9 +53,49 @@ Strophe.addConnectionPlugin('roster',
{
this._connection = conn;
this.items = [];
// Presence subscription
conn.addHandler(this._onReceivePresence.bind(this), null, 'presence', null, null, null);
conn.addHandler(this._onReceiveIQ.bind(this), Strophe.NS.ROSTER, 'iq', "set", null, null);
// Override the connect and attach methods to always add presence and roster handlers.
// They are removed when the connection disconnects, so must be added on connection.
var oldCallback, roster = this, _connect = conn.connect, _attach = conn.attach;
var newCallback = function(status)
{
if (status == Strophe.Status.ATTACHED || status == Strophe.Status.CONNECTED)
{
try
{
// Presence subscription
conn.addHandler(roster._onReceivePresence.bind(roster), null, 'presence', null, null, null);
conn.addHandler(roster._onReceiveIQ.bind(roster), Strophe.NS.ROSTER, 'iq', "set", null, null);
}
catch (e)
{
Strophe.error(e);
}
}
if (oldCallback !== null)
oldCallback.apply(this, arguments);
};
conn.connect = function(jid, pass, callback, wait, hold)
{
oldCallback = callback;
if (typeof arguments[0] == "undefined")
arguments[0] = null;
if (typeof arguments[1] == "undefined")
arguments[1] = null;
arguments[2] = newCallback;
_connect.apply(conn, arguments);
};
conn.attach = function(jid, sid, rid, callback, wait, hold, wind)
{
oldCallback = callback;
if (typeof arguments[0] == "undefined")
arguments[0] = null;
if (typeof arguments[1] == "undefined")
arguments[1] = null;
if (typeof arguments[2] == "undefined")
arguments[2] = null;
arguments[3] = newCallback;
_attach.apply(conn, arguments);
};

Strophe.addNamespace('ROSTER_VER', 'urn:xmpp:features:rosterver');
},
Expand Down Expand Up @@ -112,52 +153,105 @@ Strophe.addConnectionPlugin('roster',
{
for (var i = 0; i < this.items.length; i++)
{
if (this.items[i].jid == jid)
if (this.items[i] && this.items[i].jid == jid)
{
return this.items[i];
}
}
return false;
},
/** Function: removeItem
* Remove item by JID
*
* Parameters:
* (String) jid
*/
removeItem : function(jid)
{
for (var i = 0; i < this.items.length; i++)
{
if (this.items[i] && this.items[i].jid == jid)
{
this.items.splice(i, 1);
return true;
}
}
return false;
},
/** Function: subscribe
* Subscribe presence
*
* Parameters:
* (String) jid
* (String) message
*/
subscribe: function(jid)
subscribe: function(jid, message)
{
this._connection.send($pres({to: jid, type: "subscribe"}));
var pres = $pres({to: jid, type: "subscribe"});
if (message && message != "")
pres.c("status").t(message);
this._connection.send(pres);
},
/** Function: unsubscribe
* Unsubscribe presence
*
* Parameters:
* (String) jid
* (String) message
*/
unsubscribe: function(jid)
unsubscribe: function(jid, message)
{
this._connection.send($pres({to: jid, type: "unsubscribe"}));
var pres = $pres({to: jid, type: "unsubscribe"});
if (message && message != "")
pres.c("status").t(message);
this._connection.send(pres);
},
/** Function: authorize
* Authorize presence subscription
*
* Parameters:
* (String) jid
* (String) message
*/
authorize: function(jid)
authorize: function(jid, message)
{
this._connection.send($pres({to: jid, type: "subscribed"}));
var pres = $pres({to: jid, type: "subscribed"});
if (message && message != "")
pres.c("status").t(message);
this._connection.send(pres);
},
/** Function: unauthorize
* Unauthorize presence subscription
*
* Parameters:
* (String) jid
* (String) message
*/
unauthorize: function(jid)
unauthorize: function(jid, message)
{
this._connection.send($pres({to: jid, type: "unsubscribed"}));
var pres = $pres({to: jid, type: "unsubscribed"});
if (message && message != "")
pres.c("status").t(message);
this._connection.send(pres);
},
/** Function: add
* Add roster item
*
* Parameters:
* (String) jid - item jid
* (String) name - name
* (Array) groups
* (Function) call_back
*/
add: function(jid, name, groups, call_back)
{
var iq = $iq({type: 'set'}).c('query', {xmlns: Strophe.NS.ROSTER}).c('item', {jid: jid,
name: name});
for (var i = 0; i < groups.length; i++)
{
iq.c('group').t(groups[i]).up();
}
this._connection.sendIQ(iq, call_back, call_back);
},
/** Function: update
* Update roster item
Expand All @@ -178,14 +272,31 @@ Strophe.addConnectionPlugin('roster',
var newName = name || item.name;
var newGroups = groups || item.groups;
var iq = $iq({type: 'set'}).c('query', {xmlns: Strophe.NS.ROSTER}).c('item', {jid: item.jid,
name: newName,
subscription: item.subscription});
name: newName});
for (var i = 0; i < newGroups.length; i++)
{
iq.c('group').t(newGroups[i]).up();
}
this._connection.sendIQ(iq, call_back, call_back);
},
/** Function: remove
* Remove roster item
*
* Parameters:
* (String) jid - item jid
* (Function) call_back
*/
remove: function(jid, call_back)
{
var item = this.findItem(jid);
if (!item)
{
throw "item not found";
}
var iq = $iq({type: 'set'}).c('query', {xmlns: Strophe.NS.ROSTER}).c('item', {jid: item.jid,
subscription: "remove"});
this._connection.sendIQ(iq, call_back, call_back);
},
/** PrivateFunction: _onReceiveRosterSuccess
*
*/
Expand Down Expand Up @@ -220,7 +331,7 @@ Strophe.addConnectionPlugin('roster',
{
delete item.resources[Strophe.getResourceFromJid(jid)];
}
else
else if (!type)
{
// TODO: add timestamp
item.resources[Strophe.getResourceFromJid(jid)] = {
Expand All @@ -229,6 +340,11 @@ Strophe.addConnectionPlugin('roster',
priority : (presence.getElementsByTagName('priority').length != 0) ? Strophe.getText(presence.getElementsByTagName('priority')[0]) : ""
};
}
else
{
// Stanza is not a presence notification. (It's probably a subscription type stanza.)
return true;
}
this._call_backs(this.items, item);
return true;
},
Expand All @@ -243,13 +359,16 @@ Strophe.addConnectionPlugin('roster',
}
},
/** PrivateFunction: _onReceiveIQ
*
* Handle roster push.
*/
_onReceiveIQ : function(iq)
{
var id = iq.getAttribute('id');
var from = iq.getAttribute('from');
var iqresult = $iq({type: 'result', id: id, to: from});
// Receiving client MUST ignore stanza unless it has no from or from = user's JID.
if (from && from != "" && from != this._connection.jid && from != Strophe.getBareJidFromJid(this._connection.jid))
return true;
var iqresult = $iq({type: 'result', id: id, from: this._connection.jid});
this._connection.send(iqresult);
this._updateItems(iq);
return true;
Expand Down Expand Up @@ -281,6 +400,7 @@ Strophe.addConnectionPlugin('roster',
var jid = item.getAttribute("jid");
var name = item.getAttribute("name");
var subscription = item.getAttribute("subscription");
var ask = item.getAttribute("ask");
var groups = [];
Strophe.forEachChild(item, 'group',
function(group)
Expand All @@ -289,13 +409,20 @@ Strophe.addConnectionPlugin('roster',
}
);

if (subscription == "remove")
{
this.removeItem(jid);
return;
}

var item = this.findItem(jid);
if (!item)
{
this.items.push({
name : name,
jid : jid,
subscription : subscription,
ask : ask,
groups : groups,
resources : {}
});
Expand All @@ -304,7 +431,8 @@ Strophe.addConnectionPlugin('roster',
{
item.name = name;
item.subscription = subscription;
item.group = groups;
item.ask = ask;
item.groups = groups;
}
}
});

0 comments on commit dd14206

Please sign in to comment.