Browse files

Removed protocols stuff. A new implicit logging protocol

will be used in the future.
  • Loading branch information...
1 parent cbe08b1 commit 6258cc0784848e99a65d5e77fd401a91ff851d07 @Havvy Havvy committed Feb 2, 2013
Showing with 72 additions and 190 deletions.
  1. +1 −2 lib/index.js
  2. +10 −8 lib/nrc.js
  3. +0 −99 lib/protocols/event.js
  4. +0 −36 lib/protocols/log.js
  5. +1 −2 lib/socket.js
  6. +59 −41 modules/user.js
  7. +1 −2 package.json
View
3 lib/index.js
@@ -2,6 +2,5 @@ module.exports = {
NRC : require('./nrc'),
Message : require('./structures/message'),
Command : require('./structures/command'),
- Socket : require('./socket'),
- Log : require('./protocols/log')
+ Socket : require('./socket')
};
View
18 lib/nrc.js
@@ -8,7 +8,7 @@ var events = require('events');
var util = require('util');
// TODO: Remove protocol stuff
-var Log = require('./protocols/log');
+// var Log = require('./protocols/log');
var IrcSocket = require('./socket');
var Message = require('./structures/message');
@@ -66,7 +66,7 @@ var NRC = function (config, opt) {
this._socket = new IrcSocket(config, opt);
this._commander = new Commander(this, config, opt);
this._nick = config.nick;
- this.log = (opt && opt.log) || {};
+ // this.log = (opt && opt.log) || {};
this._subscriber = new Subscriber(this._nrcEmitter, this._commander);
@@ -107,10 +107,12 @@ NRC.prototype._onData = function (raw) {
// Emit the event.
// Log Format: NAME [SENDER] PARAMETERS
+ /*
Log.event(this.log,
[event.name,
"[" + event.sender + "]",
event.params.join(" ")].join(" "));
+ */
this._nrcEmitter.emit(event.name, event);
};
@@ -141,7 +143,7 @@ NRC.prototype.say = function (location, message) {
return this;
}
- Log.output(this.log, location + " " + message);
+ //Log.output(this.log, location + " " + message);
this._socket.raw(["PRIVMSG", location, ":" + message].join(" "));
return this;
};
@@ -154,7 +156,7 @@ NRC.prototype.act = function(location, message) {
return this;
}
- Log.output(this.log, location + " " + message);
+ //Log.output(this.log, location + " " + message);
this._socket.raw([
"PRIVMSG",
location,
@@ -164,13 +166,13 @@ NRC.prototype.act = function(location, message) {
};
NRC.prototype.join = function (channel) {
- Log.output(this.log, "join " + channel);
+ //Log.output(this.log, "join " + channel);
this._socket.raw(["JOIN", channel]);
return this;
};
NRC.prototype.part = function (channel, reason) {
- Log.output(this.log, "part " + channel + " " + reason);
+ //Log.output(this.log, "part " + channel + " " + reason);
this._socket.raw(function () {
var part = ["PART", channel];
@@ -184,14 +186,14 @@ NRC.prototype.part = function (channel, reason) {
};
NRC.prototype.quit = function (reason) {
- Log.output(this.log, "quit " + reason);
+ //Log.output(this.log, "quit " + reason);
this._socket.raw(["QUIT", reason ? ":" + reason : ""].join(" ").trim());
return this;
};
NRC.prototype.nick = function (newNick) {
if (newNick) {
- Log.output(this.log, "nick " + newNick);
+ //Log.output(this.log, "nick " + newNick);
this._socket.raw("NICK " + newNick);
this._nick = newNick;
return this;
View
99 lib/protocols/event.js
@@ -1,99 +0,0 @@
-/**
- * @author Irakli Gozalishvili
- * @see http://jeditoolkit.com/2012/03/21/protocol-based-polymorphism.html
- */
-
-var protocol = require('protocol').protocol
-var Event;
-
-// Defining a protocol for working with an event listeners / emitters.
-module.exports = Event = protocol({
- // Function on takes event `target` object implementing
- // `Event` protocol as first argument, event `type` string
- // as second argument and `listener` function as a third
- // argument. Optionally forth boolean argument can be
- // specified to use a capture. Function allows registration
- // of event `listeners` on the event `target` for the given
- // event `type`.
- on: [ protocol, String, Function, [ Boolean ] ],
-
- // Function allows registration of single shot event `listener`
- // on the event `target` of the given event `type`.
- once: [ protocol, 'type', 'listener', [ 'capture=false' ] ],
-
- // Unregisters event `listener` of the given `type` from the given
- // event `target` (implementing this protocol) with a given `capture`
- // face. Optional `capture` argument falls back to `false`.
- off: [ protocol, 'type', 'listener', [ 'capture=false'] ],
-
- // Emits given `event` for the listeners of the given event `type`
- // of the given event `target` (implementing this protocol) with a given
- // `capture` face. Optional `capture` argument falls back to `false`.
- emit: [ protocol, 'type', 'event', [ 'capture=false' ] ]
-})
-
-// Default implementation.
-on = Event.on
-
-// Weak registry of listener maps associated
-// to event targets.
-var map = WeakMap()
-
-// Returns listeners of the given event `target`
-// for the given `type` with a given `capture` face.
-function getListeners(target, type, capture) {
- // If there is no listeners map associated with
- // this target then create one.
- if (!map.has(target)) map.set(target, Object.create(null))
-
- var listeners = map.get(target)
- // prefix event type with a capture face flag.
- var address = (capture ? '!' : '-') + type
- // If there is no listeners array for the given type & capture
- // face than create one and return.
- return listeners[address] || (listeners[address] = [])
-}
-
-Event(Object, {
- on: function(target, type, listener, capture) {
- var listeners = getListeners(target, type, capture)
- // Add listener if not registered yet.
- if (!~listeners.indexOf(listener)) listeners.push(listener)
- },
- once: function(target, type, listener, capture) {
- on(target, type, listener, capture)
- on(target, type, function cleanup() {
- off(target, type, listener, capture)
- }, capture)
- },
- off: function(target, type, listener, capture) {
- var listeners = getListeners(target, type, capture)
- var index = listeners.indexOf(listener)
- // Remove listener if registered.
- if (~index) listeners.splice(index, 1)
- },
- emit: function(target, type, event, capture) {
- var listeners = getListeners(target, type, capture).slice()
- // TODO: Exception handling
- while (listeners.length) listeners.shift().call(target, event)
- }
-});
-
-//Implementation for EEs.
-
-var EventEmitter = require('events').EventEmitter
-
-EventProtocol(EventEmitter, {
- on: function(target, type, listener, capture) {
- target.on(type, listener)
- },
- once: function(target, type, listener, capture) {
- target.once(type, listener)
- },
- off: function(target, type, listener, capture) {
- target.removeListener(target, type)
- },
- emit: function(target, type, event, capture) {
- target.emit(type, event)
- }
-})
View
36 lib/protocols/log.js
@@ -1,36 +0,0 @@
-var protocol = require('protocol').protocol
-var Log;
-
-module.exports = Log = protocol({
- /**
- * @param protocol Implementing object of protocol
- * @param String Message to log at default logging level.
- */
- log : [ protocol, String ],
-
- /**
- * @param protocol Implementing object of protocol
- * @param String Event emitted.
- */
- event : [ protocol, String ],
-
- /**
- * @param protocol Implementing object of protocol
- * @param String Incoming message.
- */
- input : [ protocol, String ],
-
- /**
- * @param protocol Implementing object of protocol
- * @param String Outgoing message.
- */
- output : [ protocol, String ],
-});
-
-// Default implementation is to not log anything.
-Log(Object, {
- log : function () {},
- event : function () {},
- input : function () {},
- output : function () {}
-});
View
3 lib/socket.js
@@ -48,7 +48,6 @@
var net = require('net');
var events = require('events');
var util = require('util');
-var Log = require('./protocols/log');
/**
* @constructor
@@ -89,7 +88,7 @@ var Socket = function (network, opt) {
}
// Map event emitting.
- Log.input(that.log, line);
+ // Log.input(that.log, line);
that.emit('data', line);
}
};
View
100 modules/user.js
@@ -1,4 +1,6 @@
-// TODO: XXX: Make this work with multiple NRC instances!
+// XXX: Make sure to fix all the broken names to use `this`
+// This is because before, this module had a global called users.
+// But that doesn't scale to multiple NRC connections.
var util = require('util');
@@ -16,45 +18,45 @@ var filterStatusSymbol = function (nickname) {
return nickname;
};
-var onLoad = function () {
- server = this.use("server");
+var isSelf = function (nrc, nick) {
+ return nrc.nick() === nick;
};
-// TODO: Convert these values to lowercase. Keep the name case sensitive.
-var users = {};
-
var User = function (name, channel) {
this.name = name;
this.channels = new SSet([channel]);
};
-var addUserChannel = function (user, channel) {
- if (users[user]) {
- users[user].channels.add(channel);
- } else {
- users[user] = new User(user, channel);
- }
-};
-
-var removeUserChannel = function (user, channel) {
- users[user].channels.remove(channel);
-};
-
-var isSelf = function (nrc, nick) {
- return nrc.nick() === nick;
+/**
+ * Fields
+ * _server
+ */
+var UserModule = function (nrc) {
+ this._nrc = nrc;
+ this._server = null;
+ this.users = {};
};
-var selfPart = function (channel) {
- for (var user in users) {
- if (Object.hasOwnProperty(users, user)) {
- removeUserChannel(user, channel);
+// Keep the actual Module seperate from the UserModule class. If the
+// interface changes in the future, we are prepared!
+UserModule.prototype.getModule = function () {
+ return {
+ name: "users",
+ exports: {
+ users : this.users
+ },
+ handlers: {
+ "load" : this.onLoad.bind(this),
+ "join part quit" : this.onLeave.bind(this),
+ "nick" : this.onNick.bind(this),
+ "353" : this.namesHandler.bind(this)
}
- }
+ };
};
-var userQuit = function (user) {
- // Clean out the channels list of the user.
- while (users[user].channels.pop() !== null);
+UserModule.prototype.onLoad = function () {
+ // Should be a dependency...
+ this._server = this._nrc.use("server");
};
/**
@@ -63,7 +65,7 @@ var userQuit = function (user) {
* there really isn't any work that has to be done here other than actually
* adding the channels to the users.
*/
-var namesHandler = function (msg) {
+UserModule.prototype.namesHandler = function (msg) {
msg.users.forEach(function (user) {
// The numeric will add status messages (~, @, ect.) to nicks.
user = filterStatusSymbol(user);
@@ -72,7 +74,7 @@ var namesHandler = function (msg) {
});
};
-var onLeave = function () {
+UserModule.prototype.onLeave = function () {
var self = {
part: selfPart,
quit: function () {},
@@ -94,21 +96,37 @@ var onLeave = function () {
};
}();
-var onNick = function (msg) {
+UserModule.prototype.onNick = function (msg) {
users[msg.newNick] = users[msg.actor];
users[msg.newNick].name = msg.newNick;
delete users[msg.actor];
};
-module.exports = {
- name: "users",
- exports: {
- users : users
- },
- handlers: {
- "load" : onLoad,
- "join part quit" : onLeave,
- "nick" : onNick,
- "353" : namesHandler
+UserModule.prototype._addUserChannel = function (user, channel) {
+ if (users[user]) {
+ users[user].channels.add(channel);
+ } else {
+ users[user] = new User(user, channel);
+ }
+};
+
+UserModule.prototype._removeUserChannel = function (user, channel) {
+ users[user].channels.remove(channel);
+};
+
+UserModule.prototype._selfPart = function (channel) {
+ for (var user in users) {
+ if (Object.hasOwnProperty(users, user)) {
+ removeUserChannel(user, channel);
+ }
}
+};
+
+UserModule.prototype._userQuit = function (user) {
+ // Clean out the channels list of the user.
+ while (users[user].channels.pop() !== null);
+};
+
+module.exports = function (nrc) {
+ return new UserModule(nrc).getModule();
};
View
3 package.json
@@ -1,6 +1,6 @@
{
"name": "nrc",
- "version": "0.2.13",
+ "version": "0.3.0",
"description": "Node.js IRC API",
"maintainers": [{
"name": "Ryan Scheel",
@@ -19,7 +19,6 @@
},
"keywords": ["irc"],
"dependencies": {
- "protocols": "git://github.com/Gozala/protocol.git#91c76addd8bfc5cfc73c5fe1d7e7543197c582fe",
"simplesets": ">=0.1.2"
}
}

0 comments on commit 6258cc0

Please sign in to comment.