Skip to content
Browse files

Merge branch 'master' of github.com:jmazzi/red

  • Loading branch information...
2 parents e054895 + 1a7a30f commit dc00a02c3ccea71827657688399ba6b6cd18d3b7 @itspriddle itspriddle committed Jul 1, 2011
Showing with 122 additions and 20 deletions.
  1. +10 −2 lib/client.js
  2. +9 −8 package.json
  3. +12 −10 src/client.coffee
  4. +91 −0 test/test_client.coffee
View
12 lib/client.js
@@ -1,6 +1,11 @@
(function() {
var Client, res, response, sys, xmpp;
- var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __indexOf = Array.prototype.indexOf || function(item) {
+ for (var i = 0, l = this.length; i < l; i++) {
+ if (this[i] === item) return i;
+ }
+ return -1;
+ };
sys = require('sys');
xmpp = require('node-xmpp');
res = require('./response').Response;
@@ -74,7 +79,10 @@
}, this));
};
Client.prototype.addRoom = function(room) {
- return this.rooms.push(room);
+ room = room.toLowerCase();
+ if (!(__indexOf.call(this.rooms, room) >= 0)) {
+ return this.rooms.push(room);
+ }
};
Client.prototype.fromMe = function(stanza) {
var room, _i, _len, _ref;
View
17 package.json
@@ -12,14 +12,15 @@
"node": ">=0.4.8"
},
"dependencies": {
- "coffee-script": ">= 1.1.0",
- "xml2js": "= 0.1.6",
- "jsdom": "= 0.2.0",
- "optimist": "= 0.2.0",
- "node-xmpp": "= 0.2.9",
- "async": "= 0.1.9",
- "request": "= 1.9.8",
- "underscore": "= 1.1.6"
+ "coffee-script": ">= 1.1.0",
+ "xml2js": "= 0.1.6",
+ "jsdom": "= 0.2.0",
+ "optimist": "= 0.2.0",
+ "node-xmpp": "= 0.2.9",
+ "async": "= 0.1.9",
+ "request": "= 1.9.8",
+ "underscore": "= 1.1.6",
+ "sinon-nodeunit": "= 0.1.2"
},
"devDependencies": {
"nodeunit": "= 0.5.1"
View
22 src/client.coffee
@@ -5,22 +5,22 @@ response = new res
exports.Client = class Client
constructor: (username, password, nickname) ->
- @username = username
- @password = password
- @nickname = nickname
+ @username = username
+ @password = password
+ @nickname = nickname
@conference = "conference.#{username.split('@')[1]}"
- @rooms = []
+ @rooms = []
connect: ->
cl = new xmpp.Client jid: @username, password: @password
response.on 'end', (reply) =>
stanza = reply.stanza
res = reply.response
if stanza? and res?
- group = reply.stanza.attrs.type == 'groupchat'
- if group == true
+ group = reply.stanza.attrs.type is 'groupchat'
+ if group is true
room = stanza.attrs.from.split('/')[0]
- msg = new xmpp.Element('message', {to: room, type: 'groupchat'}).c('body').t(res)
+ msg = new xmpp.Element('message', {to: room, type: 'groupchat'}).c('body').t(res)
else
msg = new xmpp.Element('message', {to: stanza.attrs.from, type: 'chat'}).c('body').t(res)
cl.send msg
@@ -45,10 +45,12 @@ exports.Client = class Client
sys.puts e
addRoom: (room) ->
- @rooms.push room
+ room = room.toLowerCase()
+ if not (room in @rooms)
+ @rooms.push room
fromMe: (stanza) ->
for room in @rooms
- if stanza.attrs.from == "#{room}@#{@conference}/#{@nickname}"
+ if stanza.attrs.from is "#{room}@#{@conference}/#{@nickname}"
return true
- false
+ false
View
91 test/test_client.coffee
@@ -0,0 +1,91 @@
+{testCase} = require 'nodeunit'
+{Client} = require 'client'
+require 'sinon-nodeunit'
+
+module.exports = testCase
+ setUp: (proceed) ->
+ @client = new Client 'lolz@lolz.org', 'password', 'lolz'
+ proceed()
+
+ "sets @username": (test) ->
+ test.equal 'lolz@lolz.org', @client.username
+ test.done()
+
+ "sets @password": (test) ->
+ test.equal 'password', @client.password
+ test.done()
+
+ "sets @conference domain": (test) ->
+ test.equal 'conference.lolz.org', @client.conference
+ test.done()
+
+ "addRoom() appends @rooms": (test) ->
+ test.equal 0, @client.rooms.length
+ @client.addRoom "room"
+ test.equal 1, @client.rooms.length
+ test.ok 'room' in @client.rooms
+ test.done()
+
+ "addRoom() does not appent duplicates": (test) ->
+ @client.addRoom "room"
+ @client.addRoom "room"
+ @client.addRoom "rOom"
+ test.equal 1, @client.rooms.length
+ test.done()
+
+
+ "fromMe() returns true when from self": (test)->
+ stanza = JSON.parse '
+ {
+ "name": "message",
+ "parent": null,
+ "attrs": {
+ "to": "lolz@lolz.org7ec5456",
+ "type": "groupchat",
+ "from": "room@conference.lolz.org/lolz",
+ "xmlns:stream": "http://etherx.jabber.org/streams",
+ "xmlns": "jabber:client"
+ },
+ "children": [
+ {
+ "name": "body",
+ "parent": [
+ "Circular"
+ ],
+ "attrs": {},
+ "children": []
+ }
+ ]
+ }
+ '
+ @client.addRoom 'room'
+ test.ok @client.fromMe(stanza)
+ test.done()
+
+ "fromMe() returns false when not from self": (test)->
+ stanza = JSON.parse '
+ {
+ "name": "message",
+ "parent": null,
+ "attrs": {
+ "to": "lolz@lolz.org7ec5456",
+ "type": "groupchat",
+ "from": "room@conference.lolz.org/bob",
+ "xmlns:stream": "http://etherx.jabber.org/streams",
+ "xmlns": "jabber:client"
+ },
+ "children": [
+ {
+ "name": "body",
+ "parent": [
+ "Circular"
+ ],
+ "attrs": {},
+ "children": []
+ }
+ ]
+ }
+ '
+ @client.addRoom 'room'
+ test.ok !@client.fromMe(stanza)
+ test.done()

0 comments on commit dc00a02

Please sign in to comment.
Something went wrong with that request. Please try again.