Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhanson committed May 13, 2012
0 parents commit 5cd0fcd
Show file tree
Hide file tree
Showing 12 changed files with 474 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Mac OS X
.DS_Store

# Node.js
node_modules
npm-debug.log
17 changes: 17 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Mac OS X
.DS_Store

# Node.js
.npmignore
node_modules/
npm-debug.log

# Git
.git*

# Project
README.md
Makefile
docs/
examples/
test/
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(The MIT License)

Copyright (c) 2011 Jared Hanson

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
NODE = node
TEST = ./node_modules/.bin/vows
TESTS ?= test/*-test.js test/**/*-test.js

test:
@NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)

docs: docs/api.html

docs/api.html: lib/junction/*.js
dox \
--title junction-lastactivity \
--desc "Last Activity middleware for Junction" \
$(shell find lib/junction-lastactivity/* -type f) > $@

docclean:
rm -f docs/*.{1,html}

.PHONY: test docs docclean
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# junction-lastactivity

[Last Activity](http://xmpp.org/extensions/xep-0012.html) middleware built on [Junction](http://github.com/jaredhanson/junction)
and [Node](http://nodejs.org).

## License

(The MIT License)

Copyright (c) 2011 Jared Hanson

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 12 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Module dependencies.
*/
var lastActivity = require('./middleware/lastActivity')
, lastActivityResultParser = require('./middleware/lastActivityResultParser');

/**
* Expose middleware.
*/
exports = module.exports = lastActivity;
exports.lastActivity = lastActivity;
exports.lastActivityResultParser = lastActivityResultParser;
53 changes: 53 additions & 0 deletions lib/middleware/lastActivity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Module dependencies.
*/
var StanzaError = require('junction').StanzaError;


/**
* Handle requests for the last activity associated with an XMPP entity.
*
* This middleware handles IQ-get requests within the _jabber:iq:last_ XML
* namespace. By default, the middleware responds with the uptime of the
* entity, measured in seconds.
*
* Examples:
*
* connection.use(junction.lastActivity());
*
* connection.use(
* junction.lastActivity(function() {
* return 31556926;
* })
* );
*
* References:
* - [XEP-0012: Last Activity](http://xmpp.org/extensions/xep-0012.html)
*
* @param {Function} callback
* @return {Function}
* @api public
*/

module.exports = function lastActivity(callback) {
callback = callback || uptime;

var start = Date.now();
function uptime() {
return Math.round((Date.now() - start) / 1000);
}

return function lastActivity(req, res, next) {
if (!req.is('iq')) { return next(); }
if (req.type == 'result' || req.type == 'error') { return next(); }
var query = req.getChild('query', 'jabber:iq:last');
if (!query) { return next(); }

if (req.type != 'get') {
return next(new StanzaError("last query must be sent in an IQ-get stanza", 'modify', 'bad-request'));
}

var q = res.c('query', { xmlns: 'jabber:iq:last', seconds: callback() });
res.send();
}
}
31 changes: 31 additions & 0 deletions lib/middleware/lastActivityResultParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Parse information about the last activity associated with an XMPP entity.
*
* This middleware parses last activity information contained within IQ-result
* stanzas. `stanza.lastActivity` indicates the time of last activity of the
* entity, in seconds. `stanza.lastStatus` indicates last status of the entity.
*
* Examples:
*
* connection.use(junction.lastActivityResultParser());
*
* References:
* - [XEP-0012: Last Activity](http://xmpp.org/extensions/xep-0012.html)
*
* @return {Function}
* @api public
*/

module.exports = function lastActivityResultParser() {

return function lastActivityResultParser(stanza, next) {
if (!stanza.is('iq')) { return next(); }
if (stanza.type != 'result') { return next(); }
var query = stanza.getChild('query', 'jabber:iq:last');
if (!query) { return next(); }

stanza.lastActivity = query.attrs.seconds;
stanza.lastStatus = query.getText();
next();
}
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "junction-lastactivity",
"version": "0.0.0",
"description": "Last Activity middleware for Junction.",
"keywords": ["junction", "xmpp", "jabber", "last", "lastactivity", "xep0012", "middleware"],
"repository": {
"type": "git",
"url": "git://github.com/jaredhanson/junction-lastactivity.git"
},
"bugs": {
"url": "http://github.com/jaredhanson/junction-lastactivity/issues"
},
"author": { "name": "Jared Hanson", "email": "jaredhanson@gmail.com", "url": "http://www.jaredhanson.net/" },
"licenses": [ {
"type": "MIT",
"url": "http://www.opensource.org/licenses/MIT"
} ],
"main": "./lib",
"dependencies": {
"junction": "0.2.x"
},
"devDependencies": {
"vows": "0.6.x"
},
"scripts": {
"test": "NODE_PATH=lib node_modules/.bin/vows test/*-test.js test/**/*-test.js"
},
"engines": { "node": ">= 0.4.0" }
}
16 changes: 16 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var vows = require('vows');
var assert = require('assert');
var lastactivity = require('index');


vows.describe('junction-lastactivity').addBatch({

'module': {
'should export middleware': function () {
assert.isFunction(lastactivity);
assert.isFunction(lastactivity.lastActivity);
assert.isFunction(lastactivity.lastActivityResultParser);
},
},

}).export(module);
Loading

0 comments on commit 5cd0fcd

Please sign in to comment.