Permalink
Browse files

0.3.0 - Added PING and PONG events to be handled by client

  • Loading branch information...
1 parent 6b532c0 commit b9a84a593a1412a73fd2d69300f27cc68f86edc6 @sent1nel committed Jun 26, 2012
Showing with 65 additions and 27 deletions.
  1. +57 −25 index.js
  2. +1 −1 package.json
  3. +7 −1 tests/test_connect.js
View
@@ -15,12 +15,14 @@ function zIRCClient(stream, options) {
this.connections = 0;
this.send_anyway = false;
- this.command_queue = new Queue();
- this.offline_queue = new Queue();
this.commands_sent = 0;
var self = this;
+ this.on("PONG", function (hostname) {
+ this.send_command("PONG :%s\r\n", [ hostname ]);
+ });
+
this.on("say", function (message) {
this.send_command("PRIVMSG %s :%s\r\n", [ this.options.chan, message]);
});
@@ -49,13 +51,7 @@ zIRCClient.prototype.on_connect = function () {
this.connections += 1;
// Need to write stuff to do if no options have been passed to the driver
- //if (this.options.nick) {
this.do_identify();
- //}
- //else {
- // this.emit("connect");
- // this.on_ready();
- //}
};
zIRCClient.prototype.do_identify = function () {
@@ -80,28 +76,64 @@ zIRCClient.prototype.on_ready = function () {
};
zIRCClient.prototype.on_data = function (data) {
-
- var dataString = data.toString(),
- self = this;
+ var self = this,
+ lines = [],
+ delimiter = '\n',
+ message;
- dataString.split("\r\n").forEach(function (message) {
- if (!message) {
- return true;
+ lines = data.toString().split(delimiter);
+
+ lines.forEach(function (line) {
+ if (line.charAt(line.length - 1) == '\r') {
+ line = line.slice(0, -1);
}
- if (message.match(/^PING/g)) {
- self.send_command("PONG\r\n");
- return true;
+ message = self.parse_message(line);
+ if (!message) {
+ return false;
}
- self.emit("message", self.parse_message(message));
+ self.handle_command(message);
});
}
+zIRCClient.prototype.handle_command = function (message) {
+ var self = this;
+ switch (message.command) {
+ case "PING":
+ self.emit("PING");
+ break;
+ default:
+ self.emit("message", message);
+ break;
+ }
+ return false;
+}
+
zIRCClient.prototype.parse_message = function (msg) {
- // JavaScript, why don't you do regex grouping good?!
- prefix = msg.substring(1, msg.indexOf(" ") - 1);
- message = msg.substring(msg.indexOf(" :") + 2);
- command = msg.substring(msg.indexOf(" ") + 1, msg.indexOf(" :"));
- return new Message(prefix, command, message);
+ var args = [];
+ if (!msg) {
+ return false;
+ }
+ if (msg.match(/^:/g)) {
+ var prefix = msg.substring(1).split(' ', 1);
+ }
+ if (msg.indexOf(" :") != -1) {
+ if (msg.match(/^:/g)) {
+ args = msg.substring(msg.indexOf(" ") + 1, msg.indexOf(" :")).split(" ");
+ }
+ else {
+ args = msg.substring(0, msg.indexOf(" :")).split(" ");
+ }
+ args.push(msg.substring(msg.indexOf(" :") + 2));
+ }
+ else {
+ args = msg.substring(msg.indexOf(" ") + 1).split(" ");
+ }
+ command = args.shift();
+
+ // This is why I hate mIRC
+ command.toUpperCase();
+
+ return new Message(prefix, command, args);
};
zIRCClient.prototype.send_command = function (command, args) {
@@ -143,8 +175,8 @@ exports.createClient = function(port_arg, host_arg, options) {
return zirc_client;
};
-function Message(prefix, command, message) {
+function Message(prefix, command, args) {
this.prefix = prefix;
this.command = command;
- this.message = message;
+ this.args = args;
}
View
@@ -2,7 +2,7 @@
"author": "",
"name": "node_irc",
"description": "An IRC driver for Node.js",
- "version": "0.2.0",
+ "version": "0.3.0",
"repository": {
"url": ""
},
View
@@ -1,3 +1,5 @@
+var os = require("os");
+
var zirc_client = require("../index").createClient(null, null, {
nick: "zippslave",
chan: "#lolzipp"
@@ -38,10 +40,14 @@ zirc_client.on("ready", function() {
});
});
+zirc_client.on("PING", function() {
+ zirc_client.emit("PONG", os.hostname());
+});
+
zirc_client.on("error", function(err) {
console.log(err);
});
zirc_client.on("message", function(msg) {
- console.log(msg.prefix + " " + msg.command + " " + msg.message);
+ console.log(msg);
});

0 comments on commit b9a84a5

Please sign in to comment.