Skip to content

Commit 8726e3b

Browse files
committed
0.7.1 Bugfix release
1 parent 2578852 commit 8726e3b

File tree

12 files changed

+158
-79
lines changed

12 files changed

+158
-79
lines changed

bin/cli.js

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
var program = require('commander');
44
var Client = require('../lib/client.js');
55
var fs = require('fs');
6+
var inspect = require('util').inspect;
7+
var format = require('util').format;
68

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

16+
17+
// Get the configuration.
1318
var config_path = program.args[0];
1419

1520
if (!config_path) {
@@ -22,30 +27,47 @@ config_path = process.cwd() + '/' + config_path;
2227
try {
2328
var config = fs.readFileSync(config_path, {encoding: 'utf-8'});
2429
} catch (e) {
25-
console.log("Error detected!");
26-
console.log(e);
30+
console.log("Unknown Error detected!");
31+
console.log();
32+
console.log(e.stack);
2733
process.exit(2);
2834
}
2935

3036
try {
3137
config = JSON.parse(config)
3238
} catch (e) {
33-
console.log(e);
39+
console.log("Failed to parse configuration file.");
40+
console.log();
41+
console.log(e.stack);
3442
process.exit(3);
3543
}
3644

37-
var di = {};
45+
// Say what you're about to do (if -v)
46+
if (program.verbose) {
47+
console.log(format("Connecting to %s:%d", config.server, config.port));
48+
}
49+
50+
// Create the dependency management object.
51+
var parts = {};
3852

3953
if (program.verbose) {
4054
var log = function (level) {
41-
return function (line) {
42-
console.log(String(Date()), level, line);
55+
return function () {
56+
var args = Array.prototype.slice.call(arguments)
57+
.map(function (arg) {
58+
if (typeof arg === 'object') {
59+
return inspect(arg);
60+
} else {
61+
return String(arg);
62+
}
63+
});
64+
console.log(String(Date()), level, args.join(" "));
4365
};
4466
};
4567

4668
var Logger = function () {
4769
return {
48-
debug: function () {},
70+
debug: program.debug ? log('debug') : function () {},
4971
info: log('info'),
5072
notice: log('notice'),
5173
warn: log('warn'),
@@ -56,14 +78,27 @@ if (program.verbose) {
5678
};
5779
};
5880

59-
di.Logger = Logger;
81+
parts.Logger = Logger;
6082
}
6183

84+
// Try to connect, or print why it couldn't.
6285
try {
63-
var client = Client(config, di);
86+
var client = Client(config, parts);
6487
client.connect();
6588
} catch (e) {
6689
console.log("Error occurred creating and connecting to Tennu instance.");
67-
console.log(e);
90+
console.log();
91+
console.log(e.stack);
6892
process.exit(4);
69-
}
93+
}
94+
95+
// Register hangup functions
96+
var onabort = function () {
97+
client.quit("Bot terminated.");
98+
};
99+
100+
process.on('SIGHUP', onabort);
101+
process.on('SIGINT', onabort);
102+
process.on('SIGQUIT', onabort);
103+
process.on('SIGABRT', onabort);
104+
process.on('SIGTERM', onabort);

lib/client.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ var delegate = function (property, method) {
6868
client._config = config = Object.freeze(lodash.defaults({}, config, defaultClientConfiguration));
6969
di = lodash.defaults({}, dependencies || {}, defaultFactoryConfiguration);
7070

71-
console.log(config.server, config.port);
72-
7371
// Create a logger.
7472
// Default logger is a bunch of NOOPs.
7573
client._logger = new di.Logger();
@@ -83,7 +81,7 @@ var delegate = function (property, method) {
8381
// Create the listener to the socket.
8482
// This listener will parse the raw messages of the socket, and
8583
// emits specific events to listen to.
86-
var messageParser = new di.MessageParser(client, client._socket, client._logger);
84+
var messageParser = new di.MessageParser(client, client._logger, client._socket);
8785

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

10098
// And finally, the module system.
10199
client._modules = new di.Modules(client._subscriber, client);

lib/command-parser.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ function CommandParser (config, nickname, logger) {
5959

6060
parser.after(function (err, toSay, type, command) {
6161
if (err) {
62-
logger.error("Error thrown in command handler: ", err);
62+
logger.error("Error thrown in command handler!");
63+
logger.error(err.stack);
6364
return;
64-
}
65+
}
6566

6667
if (Array.isArray(toSay) || typeof toSay === "string") {
67-
receiver.say(command.channel, toSay);;
68+
command.receiver.say(command.channel, toSay);;
6869
} else if (toSay !== undefined) {
6970
logger.error("Listener returned with non-string/non-array value: ", toSay);
7071
}

lib/message-parser.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ var util = require('util');
3939
var EventEmitter = require('./event-emitter');
4040
var Message = require('./message');
4141

42-
var MessageParser = function MP (receiver, socket, logger) {
42+
var MessageParser = function MP (receiver, logger, socket) {
4343
var parser = Object.create(EventEmitter());
4444

4545
parser.parse = function (raw) {
4646
var message = new Message(raw, receiver);
47+
48+
if (message === null) {
49+
logger.error("Raw message given was not a valid IRC message!", raw);
50+
return null;
51+
}
52+
4753
this.emit(message.command.toLowerCase(), message);
4854
this.emit("*", message);
4955
return message;
@@ -61,7 +67,7 @@ var MessageParser = function MP (receiver, socket, logger) {
6167
if (err) {
6268
logger.error("Error thrown in message handler: ", err);
6369
return;
64-
}
70+
}
6571

6672
if (Array.isArray(toSay) || typeof toSay === "string") {
6773
receiver.say(message.channel, toSay);;

lib/message.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ var extensions = {
1212
},
1313

1414
kick: function (message) {
15-
// :metalbot!metalbot@coldfront-9CEAA055.austin.res.rr.com KICK #hb Havvy-kickme :metalbot
16-
1715
message.channel = message.params[0].toLowerCase();
1816
message.kicked = message.params[1];
1917
message.kicker = message.params[2];
@@ -59,10 +57,10 @@ var Message = function (raw, receiver) {
5957
message.receiver = receiver;
6058
message.command = message.command.toLowerCase();
6159

62-
if (message.prefixIsHostmask()) {
63-
message.hostmask = message.parseHostmaskFromPrefix();
64-
message.nickname = message.hostmask.nickname;
65-
}
60+
// message.hostmask is either null or an object with
61+
// nickname, username, hostname properties.
62+
message.hostmask = message.parseHostmaskFromPrefix();
63+
message.nickname = message.hostmask && message.hostmask.nickname;
6664

6765
if (extensions[message.command]) {
6866
extensions[message.command](message);

lib/output-socket.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This will be fixed in 0.8.x.
2222
*/
2323

2424
var util = require('util');
25+
var format = util.format;
2526

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

3435
var OutputSocket = function (socket, logger, nick) {
3536
var raw = function (line) {
36-
logger.info("->: " + Array.isArray(line) ? line.join(" ") : String(line));
37+
if (Array.isArray(line)) { line = line.join(" "); }
38+
logger.info("->: " + String(line));
3739
socket.raw(line);
3840
};
3941

42+
var rawf = function () {
43+
raw(format.apply(null, arguments));
44+
};
45+
4046
return {
41-
say : function (location, message) {
47+
say : function recur (location, message) {
4248
if (util.isArray(message)) {
4349
message.forEach(function (msg) {
44-
say.call(this, location, msg);
50+
recur.call(this, location, msg);
4551
});
4652

4753
return;
4854
}
49-
raw(["PRIVMSG", location, ":" + message]);
55+
rawf("PRIVMSG %s :%s", location, message);
5056
},
5157

52-
ctcp : function (location, type, message) {
58+
ctcp : function recur (location, type, message) {
5359
if (util.isArray(message)) {
5460
message.forEach(function (msg) {
55-
ctcp.call(this, location, type, msg);
61+
recur.call(this, location, type, msg);
5662
});
5763

5864
return;
5965
}
60-
this.say(location, '\u0001' + type + " " + message + '\u0001');
66+
this.say(location, format('\u0001%s %s\u0001', type, message));
6167
},
6268

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

6773
join : function (channel) {
68-
raw(["JOIN", channel]);
74+
rawf("JOIN :%s", channel);
6975
},
7076

7177
part : function (channel, reason) {
72-
raw("PART "+ channel + (reason ? " :" + reason : ''));
78+
raw("PART " + channel + (reason ? " :" + reason : ""));
7379
},
7480

7581
nick : function (newNick) {
7682
if (newNick) {
77-
raw("NICK " + newNick);
83+
rawf("NICK %s", newNick);
7884
nick = newNick;
7985
return;
8086
} else {
@@ -83,6 +89,7 @@ var OutputSocket = function (socket, logger, nick) {
8389
},
8490

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

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

107-
userhost : function userhost (users) {
114+
userhost : function recur (users) {
108115
if (typeof users === 'string') {
109-
raw("USERHOST " + users);
116+
rawf("USERHOST :%s", users);
110117
} else if (typeof users === 'array') {
111118
partition(users, 5)
112119
.map(function (hosts) { return hosts.join(' '); })
113-
.map(userhost);
120+
.map(recur);
114121
} else {
115122
throw new Error("Userhost command takes either a string (a single nick) or an array (of string nicks)");
116123
}
117124
},
118125

119-
whois : function whois (users, server) {
126+
whois : function recur (users, server) {
120127
if (typeof users === "array") {
121128
if (users.length > 15) {
122129
partition(users, 15)
123130
.map(function (users) { return users.join(','); })
124-
.map(function (users) { whois(users, server); });
131+
.map(function (users) { recur(users, server); });
125132
}
126133
} else if (typeof users === 'string') {
127-
raw("WHOIS " + server ? server + " " : "" + users);
134+
raw("WHOIS " + (server ? server + " " : "") + users);
128135
} else {
129136
throw new Error("Whois command takes either a string (a single nick) or an array (of string nicks)");
130137
}
131138
},
132139

133140
_raw : raw,
134-
toString : require('./make-toString')('OutputSocket')
141+
_rawf : rawf,
142+
toString : function () { return "[Object IrcOutputSocket]"; }
135143
};
136144
};
137145

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tennu",
3-
"version": "0.7.0",
3+
"version": "0.7.1",
44
"description": "Tennu Node.js IRC Framework",
55
"maintainers": [
66
{

0 commit comments

Comments
 (0)