Skip to content

Commit

Permalink
0.7.1 Bugfix release
Browse files Browse the repository at this point in the history
  • Loading branch information
Havvy committed Dec 26, 2013
1 parent 2578852 commit 8726e3b
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 79 deletions.
57 changes: 46 additions & 11 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
var program = require('commander');
var Client = require('../lib/client.js');
var fs = require('fs');
var inspect = require('util').inspect;
var format = require('util').format;

program
.version('0.7.0')
.usage('[options] <config file>')
.option('-v, --verbose', 'Log to standard out')
.option('-d, --debug', 'Log debug messages. Requires -v')
.parse(process.argv);


// Get the configuration.
var config_path = program.args[0];

if (!config_path) {
Expand All @@ -22,30 +27,47 @@ config_path = process.cwd() + '/' + config_path;
try {
var config = fs.readFileSync(config_path, {encoding: 'utf-8'});
} catch (e) {
console.log("Error detected!");
console.log(e);
console.log("Unknown Error detected!");
console.log();
console.log(e.stack);
process.exit(2);
}

try {
config = JSON.parse(config)
} catch (e) {
console.log(e);
console.log("Failed to parse configuration file.");
console.log();
console.log(e.stack);
process.exit(3);
}

var di = {};
// Say what you're about to do (if -v)
if (program.verbose) {
console.log(format("Connecting to %s:%d", config.server, config.port));
}

// Create the dependency management object.
var parts = {};

if (program.verbose) {
var log = function (level) {
return function (line) {
console.log(String(Date()), level, line);
return function () {
var args = Array.prototype.slice.call(arguments)
.map(function (arg) {
if (typeof arg === 'object') {
return inspect(arg);
} else {
return String(arg);
}
});
console.log(String(Date()), level, args.join(" "));
};
};

var Logger = function () {
return {
debug: function () {},
debug: program.debug ? log('debug') : function () {},
info: log('info'),
notice: log('notice'),
warn: log('warn'),
Expand All @@ -56,14 +78,27 @@ if (program.verbose) {
};
};

di.Logger = Logger;
parts.Logger = Logger;
}

// Try to connect, or print why it couldn't.
try {
var client = Client(config, di);
var client = Client(config, parts);
client.connect();
} catch (e) {
console.log("Error occurred creating and connecting to Tennu instance.");
console.log(e);
console.log();
console.log(e.stack);
process.exit(4);
}
}

// Register hangup functions
var onabort = function () {
client.quit("Bot terminated.");
};

process.on('SIGHUP', onabort);
process.on('SIGINT', onabort);
process.on('SIGQUIT', onabort);
process.on('SIGABRT', onabort);
process.on('SIGTERM', onabort);
6 changes: 2 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ var delegate = function (property, method) {
client._config = config = Object.freeze(lodash.defaults({}, config, defaultClientConfiguration));
di = lodash.defaults({}, dependencies || {}, defaultFactoryConfiguration);

console.log(config.server, config.port);

// Create a logger.
// Default logger is a bunch of NOOPs.
client._logger = new di.Logger();
Expand All @@ -83,7 +81,7 @@ var delegate = function (property, method) {
// Create the listener to the socket.
// This listener will parse the raw messages of the socket, and
// emits specific events to listen to.
var messageParser = new di.MessageParser(client, client._socket, client._logger);
var messageParser = new di.MessageParser(client, client._logger, client._socket);

// Create the listener to private messages from the IRCMessageEmitter
// The commander will parse these private messages for commands, and
Expand All @@ -95,7 +93,7 @@ var delegate = function (property, method) {
// determining whether they should be handled by the IrcMessageEmitter
// or the Command Parser.
client._subscriber = new di.BiSubscriber(messageParser, commandParser);
client._subscriber.on("privmsg", commandParser.parse.bind(commandParser));
client._subscriber.on("privmsg", function (privmsg) { commandParser.parse(privmsg); });

// And finally, the module system.
client._modules = new di.Modules(client._subscriber, client);
Expand Down
7 changes: 4 additions & 3 deletions lib/command-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ function CommandParser (config, nickname, logger) {

parser.after(function (err, toSay, type, command) {
if (err) {
logger.error("Error thrown in command handler: ", err);
logger.error("Error thrown in command handler!");
logger.error(err.stack);
return;
}
}

if (Array.isArray(toSay) || typeof toSay === "string") {
receiver.say(command.channel, toSay);;
command.receiver.say(command.channel, toSay);;
} else if (toSay !== undefined) {
logger.error("Listener returned with non-string/non-array value: ", toSay);
}
Expand Down
10 changes: 8 additions & 2 deletions lib/message-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ var util = require('util');
var EventEmitter = require('./event-emitter');
var Message = require('./message');

var MessageParser = function MP (receiver, socket, logger) {
var MessageParser = function MP (receiver, logger, socket) {
var parser = Object.create(EventEmitter());

parser.parse = function (raw) {
var message = new Message(raw, receiver);

if (message === null) {
logger.error("Raw message given was not a valid IRC message!", raw);
return null;
}

this.emit(message.command.toLowerCase(), message);
this.emit("*", message);
return message;
Expand All @@ -61,7 +67,7 @@ var MessageParser = function MP (receiver, socket, logger) {
if (err) {
logger.error("Error thrown in message handler: ", err);
return;
}
}

if (Array.isArray(toSay) || typeof toSay === "string") {
receiver.say(message.channel, toSay);;
Expand Down
10 changes: 4 additions & 6 deletions lib/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var extensions = {
},

kick: function (message) {
// :metalbot!metalbot@coldfront-9CEAA055.austin.res.rr.com KICK #hb Havvy-kickme :metalbot

message.channel = message.params[0].toLowerCase();
message.kicked = message.params[1];
message.kicker = message.params[2];
Expand Down Expand Up @@ -59,10 +57,10 @@ var Message = function (raw, receiver) {
message.receiver = receiver;
message.command = message.command.toLowerCase();

if (message.prefixIsHostmask()) {
message.hostmask = message.parseHostmaskFromPrefix();
message.nickname = message.hostmask.nickname;
}
// message.hostmask is either null or an object with
// nickname, username, hostname properties.
message.hostmask = message.parseHostmaskFromPrefix();
message.nickname = message.hostmask && message.hostmask.nickname;

if (extensions[message.command]) {
extensions[message.command](message);
Expand Down
42 changes: 25 additions & 17 deletions lib/output-socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This will be fixed in 0.8.x.
*/

var util = require('util');
var format = util.format;

var partition = function (array, length) {
var partitions = [];
Expand All @@ -33,48 +34,53 @@ var partition = function (array, length) {

var OutputSocket = function (socket, logger, nick) {
var raw = function (line) {
logger.info("->: " + Array.isArray(line) ? line.join(" ") : String(line));
if (Array.isArray(line)) { line = line.join(" "); }
logger.info("->: " + String(line));
socket.raw(line);
};

var rawf = function () {
raw(format.apply(null, arguments));
};

return {
say : function (location, message) {
say : function recur (location, message) {
if (util.isArray(message)) {
message.forEach(function (msg) {
say.call(this, location, msg);
recur.call(this, location, msg);
});

return;
}
raw(["PRIVMSG", location, ":" + message]);
rawf("PRIVMSG %s :%s", location, message);
},

ctcp : function (location, type, message) {
ctcp : function recur (location, type, message) {
if (util.isArray(message)) {
message.forEach(function (msg) {
ctcp.call(this, location, type, msg);
recur.call(this, location, type, msg);
});

return;
}
this.say(location, '\u0001' + type + " " + message + '\u0001');
this.say(location, format('\u0001%s %s\u0001', type, message));
},

act: function (location, message) {
this.ctcp(location, "ACTION", message);
},

join : function (channel) {
raw(["JOIN", channel]);
rawf("JOIN :%s", channel);
},

part : function (channel, reason) {
raw("PART "+ channel + (reason ? " :" + reason : ''));
raw("PART " + channel + (reason ? " :" + reason : ""));
},

nick : function (newNick) {
if (newNick) {
raw("NICK " + newNick);
rawf("NICK %s", newNick);
nick = newNick;
return;
} else {
Expand All @@ -83,6 +89,7 @@ var OutputSocket = function (socket, logger, nick) {
},

quit : function (reason) {
logger.notice(format("Quitting with reason: %s", reason));
raw("QUIT" + (reason ? " :" + reason : ""));
},

Expand All @@ -104,34 +111,35 @@ var OutputSocket = function (socket, logger, nick) {
raw(["MODE", target, args]);
},

userhost : function userhost (users) {
userhost : function recur (users) {
if (typeof users === 'string') {
raw("USERHOST " + users);
rawf("USERHOST :%s", users);
} else if (typeof users === 'array') {
partition(users, 5)
.map(function (hosts) { return hosts.join(' '); })
.map(userhost);
.map(recur);
} else {
throw new Error("Userhost command takes either a string (a single nick) or an array (of string nicks)");
}
},

whois : function whois (users, server) {
whois : function recur (users, server) {
if (typeof users === "array") {
if (users.length > 15) {
partition(users, 15)
.map(function (users) { return users.join(','); })
.map(function (users) { whois(users, server); });
.map(function (users) { recur(users, server); });
}
} else if (typeof users === 'string') {
raw("WHOIS " + server ? server + " " : "" + users);
raw("WHOIS " + (server ? server + " " : "") + users);
} else {
throw new Error("Whois command takes either a string (a single nick) or an array (of string nicks)");
}
},

_raw : raw,
toString : require('./make-toString')('OutputSocket')
_rawf : rawf,
toString : function () { return "[Object IrcOutputSocket]"; }
};
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tennu",
"version": "0.7.0",
"version": "0.7.1",
"description": "Tennu Node.js IRC Framework",
"maintainers": [
{
Expand Down
Loading

0 comments on commit 8726e3b

Please sign in to comment.