Skip to content

Commit

Permalink
Add hand-rolled parser
Browse files Browse the repository at this point in the history
Use `Buffer' instead of strings for parsing

Remove PEG.js

Remove `objects' module

Fix some type errors uncovered when using `Buffer'
  • Loading branch information
Jonas Westerlund committed Aug 12, 2012
1 parent e6c081f commit f050027
Show file tree
Hide file tree
Showing 20 changed files with 982 additions and 1,923 deletions.
3 changes: 0 additions & 3 deletions Makefile
Expand Up @@ -8,9 +8,6 @@ MOCHA = ./node_modules/mocha/bin/_mocha
tags:
$(CTAGS) lib/irc.js lib/parser.js spec/**/*.js

parser:
@node --use_strict --harmony ./build.js parser;\

test-irc:
@export IRCJS_TEST=1;\
node --harmony $(MOCHA) --reporter spec --require should spec/lib/irc.spec.js;\
Expand Down
41 changes: 0 additions & 41 deletions build.js

This file was deleted.

15 changes: 8 additions & 7 deletions lib/channel.js
Expand Up @@ -50,7 +50,7 @@ property(Channel.prototype, "id", function() { return id(this.name); });
* @return {Channel}
*/
Channel.prototype.say = function(text) {
this.client.send(message(COMMAND.PRIVMSG, [this, trailing(text)]));
this.client.send(message(COMMAND.PRIVMSG, [this.name, trailing(text)]));
return this;
}

Expand All @@ -71,18 +71,19 @@ Channel.prototype.join = function(key, callback) {
}

Channel.prototype.invite = function(person) {
const nick = person.nick || person;
this.client.send(message(COMMAND.INVITE, [nick, this]));
const nick = person.nick ? person.nick : person;
this.client.send(message(COMMAND.INVITE, [nick, this.name]));
return this;
}

Channel.prototype.kick = function(person) {
this.client.send(message(COMMAND.KICK, [this, person]));
const nick = person.nick ? person.nick : person;
this.client.send(message(COMMAND.KICK, [this.name, nick]));
return this;
}

Channel.prototype.notify = function(note) {
this.client.send(message(COMMAND.NOTICE, [this, trailing(note)]));
this.client.send(message(COMMAND.NOTICE, [this.name, trailing(note)]));
return this;
}

Expand All @@ -96,12 +97,12 @@ Channel.prototype.part = function(txt) {
}

Channel.prototype.setMode = function(mode) {
this.client.send(message(COMMAND.MODE, [this, mode]));
this.client.send(message(COMMAND.MODE, [this.name, mode]));
return this;
}

Channel.prototype.setTopic = function(topic) {
this.client.send(message(COMMAND.TOPIC, [this, trailing(topic)]));
this.client.send(message(COMMAND.TOPIC, [this.name, trailing(topic)]));
return this;
}

Expand Down
36 changes: 18 additions & 18 deletions lib/constants.js
Expand Up @@ -38,7 +38,7 @@ const SOCKSTATE = {
* @enum {string}
*/
const COMMAND = {
ADMIN: "ADMIN",
ADMIN: "ADMIN",
AWAY: "AWAY",
CONNECT: "CONNECT",
DIE: "DIE",
Expand Down Expand Up @@ -246,23 +246,23 @@ const ERROR = {
* @enum {string}
*/
const CHANMODE = {
ANONYMOUS: 'a',
INVITE: 'i',
MODERATED: 'm',
NOSPAM: 'n',
PRIVATE: 'p',
QUIET: 'q',
REOP: 'r',
SECRET: 's',
TOPIC: 't',
BANMASK: 'b',
CREATOR: 'O',
EXCEPTMASK: 'e',
INVITEMASK: 'I',
KEY: 'k',
LIMIT: 'l',
OP: 'o',
VOICE: 'v'
ANONYMOUS: 'a',
INVITE: 'i',
MODERATED: 'm',
NOSPAM: 'n',
PRIVATE: 'p',
QUIET: 'q',
REOP: 'r',
SECRET: 's',
TOPIC: 't',
BANMASK: 'b',
CREATOR: 'O',
EXCEPTMASK: 'e',
INVITEMASK: 'I',
KEY: 'k',
LIMIT: 'l',
OP: 'o',
VOICE: 'v'
};

/** User modes
Expand Down
45 changes: 20 additions & 25 deletions lib/handlers.js
Expand Up @@ -4,20 +4,16 @@

"use strict";

const format = require("util");
const channel = require("./channel").channel;
const constants = require("./constants");
const format = require("util");
const id = require("./util").id;
const logger = require("./logger");
const objects = require("./objects");
const message = require("./message").message;
const parser = require("./parser");
const person = require("./person").person;
const trailing = require("./message").trailing;

const Channel = objects.Channel;
const Message = objects.Message;
const Person = objects.Person;
const Server = objects.Server;
const channel = objects.channel;
const message = objects.message;
const person = objects.person;
const trailing = objects.trailing;
const COMMAND = constants.COMMAND;
const ERROR = constants.ERROR;
const EVENT = constants.EVENT;
Expand Down Expand Up @@ -67,7 +63,7 @@ function onKickCommand(msg) {
let user = null;
let chan = null;
while (l--) {
if (!(chan = this.channels.get(objects.id(chans[l])))) {
if (!(chan = this.channels.get(id(chans[l])))) {
continue;
}
for (i = 0; i < k; ++i) {
Expand All @@ -94,8 +90,8 @@ function onKickCommand(msg) {
function onModeCommand(msg) {
const param = msg.params[0];
const target = param === this.user.nick ? this.user :
this.channels.get(objects.id(param)) || person(param);
const modes = parser.parse(msg.params[1].replace(/^:/, ""), "Mode");
this.channels.get(id(param)) || person(param);
const modes = parser.parseMode(msg.params[1].replace(/^:/, ""));
if (!target) {
log.warn("Got mode %s for %s, dunno what to do", msg.params[1], param);
return STATUS.ERROR;
Expand All @@ -106,10 +102,10 @@ function onModeCommand(msg) {
else {
log.debug("Setting mode %s for %s", msg.params[1], target);
}
for (let i = 0, m = modes.get("+"), l = m.length; i < l; ++i) {
for (let i = 0, m = modes.get(0x2B), l = m.length; i < l; ++i) {
target.mode.add(m[i]);
}
for (let i = 0, m = modes.get("-"), l = m.length; i < l; ++i) {
for (let i = 0, m = modes.get(0x2D), l = m.length; i < l; ++i) {
target.mode.delete(m[i]);
}
}
Expand All @@ -124,13 +120,13 @@ function onNickCommand(msg) {
function onPartCommand(msg) {
const name = msg.params[0];
const nick = msg.from.nick;
const chan = this.channels.get(objects.id(name));
if (chan && chan.people.has(objects.id(nick))) {
chan.people.delete(objects.id(nick));
const chan = this.channels.get(id(name));
if (chan && chan.people.has(id(nick))) {
chan.people.delete(id(nick));
log.debug("Removing %s from %s", nick, chan);
if (nick === this.user.nick) {
log.debug("Left %s, removing it", chan);
this.channels.delete(objects.id(name));
this.channels.delete(id(name));
}
return STATUS.SUCCESS;
}
Expand All @@ -151,7 +147,7 @@ function onPingCommand(ping) {
}

function onTopicCommand(msg) {
const chan = this.channels.get(objects.id(msg.params[0]));
const chan = this.channels.get(id(msg.params[0]));
const topic = msg.params[1].slice(1);
if (chan) {
if (chan.topic) {
Expand All @@ -170,7 +166,6 @@ function onTopicCommand(msg) {
}

function onQuitCommand(msg) {
// Remove from all chans
const user = msg.from;
log.debug("Got a quit message for %s, removing them from all channels", user);
user.channels.forEach(function(chan) {
Expand All @@ -188,7 +183,7 @@ function onMyInfoReply(msg) {
}

function onNameReply(msg) {
const chan = this.channels.get(objects.id(msg.params[2]));
const chan = this.channels.get(id(msg.params[2]));
const nicks = msg.params[3].split(" ");
const count = nicks.length;
if (!chan) {
Expand All @@ -205,7 +200,7 @@ function onNameReply(msg) {
}

function onTopicReply(msg) {
const chan = this.channels.get(objects.id(msg.params[1]));
const chan = this.channels.get(id(msg.params[1]));
const topic = msg.params[2].slice(1);
if (chan) {
log.debug("Setting topic for %s to %s", chan, topic);
Expand Down Expand Up @@ -238,13 +233,13 @@ function onAnyError(msg) {
function onForwardError(msg) {
const from = msg.params[1];
const to = msg.params[2];
if (this.channels.has(objects.id(to))) {
if (this.channels.has(id(to))) {
log.debug("Forwarded from %s to %s, which already existed", from, to);
return;
}
const chan = channel(to);
chan.people.set(this.user.id, this.user);
this.channels.delete(objects.id(from));
this.channels.delete(id(from));
this.channels.set(chan.id, chan);
log.info("Got forwarded from %s to %s, adding %s", from, to, to);
return STATUS.ERROR;
Expand Down

0 comments on commit f050027

Please sign in to comment.