Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

now supports connecting to more than one account.

  • Loading branch information...
commit 905e9a0044c9596987791ffc64ff773fc1ec476a 1 parent 6004021
Joachim Kainz authored
View
10 .gitignore
@@ -0,0 +1,10 @@
+node_modules
+.DS_Store
+.nide
+.svn
+*.class
+*.gz
+*.jar
+*.tar
+*~
+*.log
View
69 .jshintrc
@@ -0,0 +1,69 @@
+{
+ // Settings
+ "passfail" : false, // Stop on first error.
+ "maxerr" : 100, // Maximum error before stopping.
+
+
+ // Predefined globals whom JSHint will ignore.
+ "browser" : false, // Standard browser globals e.g. `window`, `document`.
+ "node" : true,
+ "rhino" : false,
+ "couch" : false,
+ "wsh" : false, // Windows Scripting Host.
+ "jquery" : false,
+ "prototypejs" : false,
+ "mootools" : false,
+ "dojo" : false,
+ "predef" : [ // Custom globals.
+ //"exampleVar",
+ //"anotherCoolGlobal",
+ //"iLoveDouglas"
+ ],
+
+
+ // Development.
+ "debug" : false, // Allow debugger statements e.g. browser breakpoints.
+ "devel" : true, // Allow developments statements e.g. `console.log();`.
+
+
+ // ECMAScript 5.
+ "es5" : true, // Allow ECMAScript 5 syntax.
+ "strict" : false, // Require `use strict` pragma in every file.
+ "globalstrict" : false, // Allow global "use strict" (also enables 'strict').
+
+
+ // The Good Parts.
+ "asi" : true, // Tolerate Automatic Semicolon Insertion (no semicolons).
+ "laxbreak" : true, // Tolerate unsafe line breaks e.g. `return [\n] x` without semicolons.
+ "bitwise" : true, // Prohibit bitwise operators (&, |, ^, etc.).
+ "boss" : false, // Tolerate assignments inside if, for & while. Usually conditions & loops are for comparison, not assignments.
+ "curly" : true, // Require {} for every new block or scope.
+ "eqeqeq" : true, // Require triple equals i.e. `===`.
+ "eqnull" : false, // Tolerate use of `== null`.
+ "evil" : false, // Tolerate use of `eval`.
+ "expr" : false, // Tolerate `ExpressionStatement` as Programs.
+ "forin" : false, // Tolerate `for in` loops without `hasOwnPrototype`.
+ "immed" : true, // Require immediate invocations to be wrapped in parens e.g. `( function(){}() );`
+ "latedef" : true, // Prohipit variable use before definition.
+ "loopfunc" : false, // Allow functions to be defined within loops.
+ "noarg" : true, // Prohibit use of `arguments.caller` and `arguments.callee`.
+ "regexp" : true, // Prohibit `.` and `[^...]` in regular expressions.
+ "regexdash" : false, // Tolerate unescaped last dash i.e. `[-...]`.
+ "scripturl" : true, // Tolerate script-targeted URLs.
+ "shadow" : false, // Allows re-define variables later in code e.g. `var x=1; x=2;`.
+ "supernew" : false, // Tolerate `new function () { ... };` and `new Object;`.
+ "undef" : true, // Require all non-global variables be declared before they are used.
+
+
+ // Personal styling preferences.
+ "newcap" : true, // Require capitalization of all constructor functions e.g. `new F()`.
+ "noempty" : true, // Prohibit use of empty blocks.
+ "nonew" : true, // Prohibit use of constructors for side-effects.
+ "nomen" : true, // Prohibit use of initial or trailing underbars in names.
+ "onevar" : false, // Allow only one `var` statement per function.
+ "plusplus" : false, // Prohibit use of `++` & `--`.
+ "sub" : false, // Tolerate all forms of subscript notation besides dot notation e.g. `dict['key']` instead of `dict.key`.
+ "trailing" : true, // Prohibit trailing whitespaces.
+ "white" : false, // Check against strict whitespace and indentation rules.
+ "indent" : 4 // Specify indentation spacing
+}
View
17 examples/echo.js
@@ -24,8 +24,13 @@
*/
-var xmpp = require('../lib/simple-xmpp');
var argv = process.argv;
+var xmpp = require('../lib/simple-xmpp')({
+ jid : argv[2],
+ password : argv[3],
+ host : 'talk.google.com',
+ port : 5222
+});
xmpp.on('online', function() {
console.log('Yes, I\'m connected!');
@@ -34,7 +39,7 @@ xmpp.on('online', function() {
xmpp.on('chat', function(from, message) {
xmpp.send(from, 'echo: ' + message);
xmpp.probe('arunoda.susiripala@gmail.com', function(state) {
- console.log(state == xmpp.STATUS.ONLINE);
+ console.log(state === "online");
});
});
@@ -47,10 +52,4 @@ xmpp.on('buddy', function(jid, state) {
});
-xmpp.connect({
- jid : argv[2],
- password : argv[3],
- host : 'talk.google.com',
- host : 'talk.google.com',
- port : 5222
-});
+xmpp.connect();
View
186 lib/simple-xmpp.js
@@ -2,7 +2,7 @@
The MIT License
- Copyright (c) 2011 Arunoda Susiripala
+ Copyright (c) 2011 Arunoda Susiripala, modified by Joachim Kainz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -23,115 +23,117 @@
THE SOFTWARE.
*/
-
var xmpp = require('node-xmpp');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
-var qbox = require('qbox');
var STATUS = {
- AWAY: "away",
- DND: "dnd",
- XA: "xa",
ONLINE: "online",
OFFLINE: "offline"
};
-module.exports = new SimpleXMPP();
-
-function SimpleXMPP() {
-
- //setting status here
- this.STATUS = STATUS;
- var self = this;
- this.Element = xmpp.Element;
- var config;
- var conn;
- var probeBuddies = {};
- var $ = qbox.create();
-
- var events = new EventEmitter();
- this.on = function() {
- events.on.apply(events, arguments);
- };
-
- this.send = function(to, message) {
-
- $.ready(function() {
- var stanza = new xmpp.Element('message', { to: to, type: 'chat' });
- stanza.c('body').t(message);
- conn.send(stanza);
+function SimpleXMPP(conn, events, probeBuddies) {
+ this.send = function (to, message) {
+ var stanza = new xmpp.Element('message', {
+ to: to,
+ type: 'chat'
});
+ stanza.c('body').t(message);
+ conn.send(stanza);
};
- this.probe = function(buddy, callback) {
-
+ this.probe = function (buddy, callback) {
probeBuddies[buddy] = true;
- $.ready(function() {
- var stanza = new xmpp.Element('presence', {type: 'probe', to: buddy});
- events.once('probe_' + buddy, callback);
- conn.send(stanza);
+
+ var stanza = new xmpp.Element('presence', {
+ type: 'probe',
+ to: buddy
});
+ events.once('probe_' + buddy, callback);
+ conn.send(stanza);
};
+}
- this.connect = function(params) {
-
- config = params
- conn = new xmpp.Client(params);
- self.conn = conn;
-
- conn.on('online', function(){
- conn.send(new xmpp.Element('presence'));
- events.emit('online');
- $.start();
- //make the connection live
- setInterval(function() {
- conn.send(new xmpp.Element('presence'));
- }, 1000 * 10)
- });
+module.exports = function(config) {
+ var events = new EventEmitter();
+ var probeBuddies = {};
- conn.on('stanza', function(stanza) {
- events.emit('stanza', stanza);
- //console.log(stanza);
- //looking for message stanza
- if (stanza.is('message')) {
-
- //getting the chat message
- if(stanza.attrs.type == 'chat') {
-
- var body = stanza.getChild('body');
- if(body) {
- var message = body.getText();
- var from = stanza.attrs.from;
- var id = from.split('/')[0];
- events.emit('chat', id, message);
+ return {
+ "on": function (event, listener) {
+ return events.on(event, listener);
+ },
+
+ "once": function (event, listener) {
+ return events.once(event, listener);
+ },
+
+ "removeListener": function (event, listener) {
+ return events.removeListener(event, listener);
+ },
+
+ "removeAllListeners": function (event) {
+ return events.removeListener(event);
+ },
+
+ "setMaxListeners": function (n) {
+ return events.setMaxListeners(n);
+ },
+
+ "connect": function () {
+ var conn = new xmpp.Client(config);
+
+ conn.on('online', function () {
+ conn.send(new xmpp.Element('presence'));
+ events.emit('online');
+ //make the connection live
+ setInterval(function () {
+ conn.send(new xmpp.Element('presence'));
+ }, 1000 * 10)
+ });
+
+ conn.on('stanza', function (stanza) {
+ events.emit('stanza', stanza);
+ //console.log(stanza);
+ //looking for message stanza
+ if (stanza.is('message')) {
+
+ //getting the chat message
+ if (stanza.attrs.type === 'chat') {
+
+ var body = stanza.getChild('body');
+ if (body) {
+ var message = body.getText();
+ var from = stanza.attrs.from;
+ var id = from.split('/')[0];
+ events.emit('chat', id, message);
+ }
}
- }
- } else if(stanza.is('presence')) {
- //looking for presence stenza for availability changes
- var from = stanza.attrs.from;
- if(from) {
- var id = from.split('/')[0];
- var state = (stanza.getChild('show'))? stanza.getChild('show').getText(): STATUS.ONLINE;
- state = (state == 'chat')? STATUS.ONLINE : state;
- state = (stanza.attrs.type == 'unavailable')? STATUS.OFFLINE : state;
- //checking if this is based on probe
- if(probeBuddies[id]) {
- events.emit('probe_' + id, state);
- delete probeBuddies[id];
- } else {
- //specifying roster changes
- events.emit('buddy', id, state);
+ } else if (stanza.is('presence')) {
+ //looking for presence stenza for availability changes
+ var frm = stanza.attrs.from;
+
+ if (frm) {
+ var iD = frm.split('/')[0];
+ var state = (stanza.getChild('show')) ? stanza.getChild('show').getText() : STATUS.ONLINE;
+ state = (state === 'chat') ? STATUS.ONLINE : state;
+ state = (stanza.attrs.type === 'unavailable') ? STATUS.OFFLINE : state;
+ //checking if this is based on probe
+ if (probeBuddies[iD]) {
+ events.emit('probe_' + iD, state);
+ delete probeBuddies[iD];
+ } else {
+ //specifying roster changes
+ events.emit('buddy', iD, state);
+ }
}
}
- }
- });
-
- conn.on('error', function(err) {
- events.emit('error', err);
- });
-
+ });
+
+ conn.on('error', function (err) {
+ events.emit('error', err);
+ });
+
+ return new SimpleXMPP(conn, events, probeBuddies);
+ }
};
-}
-
-
+};
View
48 readme.md
@@ -1,6 +1,8 @@
Node Simple XMPP
================
-Simple High Level NodeJS XMPP Library
+
+Potentially Simpler High Level NodeJS XMPP Library, which supports listening to
+more than one account.
Status
------
@@ -12,11 +14,16 @@ Dependencies
Install
-------
- npm install simple-xmpp
+ npm install simpler-xmpp
Example
-------
- var xmpp = require('simple-xmpp');
+ var xmpp = require('simple-xmpp')({
+ jid : username@gmail.com,
+ password : password,
+ host : 'talk.google.com',
+ port : 5222
+ });
xmpp.on('online', function() {
console.log('Yes, I\'m connected!');
@@ -30,12 +37,7 @@ Example
console.error(err);
});
- xmpp.connect({
- jid : username@gmail.com,
- password : password,
- host : 'talk.google.com',
- port : 5222
- });
+ xmpp.connect();
Documentation
-------------
@@ -62,10 +64,10 @@ event where emits when state of the buddy on your chat list changes
/**
@param jid - is the id of buddy (eg:- hello@gmail.com)
@param state - state of the buddy. value will be one of the following constant can be access via require('simple-xmpp').STATUS
- AWAY - Buddy goes away
- DND - Buddy set its status as "Do Not Disturb" or "Busy",
- ONLINE - Buddy comes online or available to chat
- OFFLINE - Buddy goes offline
+ "away" - Buddy goes away
+ "dnd" - Buddy set its status as "Do Not Disturb" or "Busy",
+ "online" - Buddy comes online or available to chat
+ "offline" - Buddy goes offline
*/
xmpp.on('buddy', function(jid, state) {
console.log('%s is in %s state', jid, state);
@@ -109,23 +111,3 @@ Probe the state of the buddy
xmpp.probe(jid, function(state) {
})
-
-### Fields
-Fields provided Additional Core functionalies
-
-#### xmpp.conn
-The underline connection object
-
- var xmpp = simpleXMPP.connect({});
- xmpp.conn; // the connection object
-
-#### xmpp.Element
-Underline XMPP Element class
-
- var xmpp = simpleXMPP.connect({});
- xmpp.Element; // the connection objec
-
-
-
-
-
View
8 tests/simple-xmpp.js
@@ -22,7 +22,7 @@ exports.testOnline = function(test) {
conn.mock('on').takes('error', function() {});
conn.mock('send').takes(new ltx.Element('presence'));
- simpleXMPP.connect({});
+ simpleXMPP({});
test.ok(conn.assert());
test.done();
@@ -43,8 +43,8 @@ exports.testError = function(test) {
test.deepEqual(aa.toString(), stanza.toString());
};
- simpleXMPP.connect({});
- simpleXMPP.send('hello@gmail.com', 'hi');
+ var client = simpleXMPP({});
+ client.send('hello@gmail.com', 'hi');
test.ok(conn.assert());
test.done();
@@ -67,7 +67,7 @@ exports.testOnChat = function(test) {
test.equal(from, f);
test.equal(message, m);
});
- simpleXMPP.connect({});
+ simpleXMPP({});
test.ok(conn.assert());
test.done();
Please sign in to comment.
Something went wrong with that request. Please try again.