Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added message auto encoding #269

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Client
sasl: false,
stripColors: false,
channelPrefixes: "&#",
messageSplit: 512
messageSplit: 512,
autoEncoding: false
}

`secure` (SSL connection) can be a true value or an object (the kind of object
Expand All @@ -59,6 +60,8 @@ Client
`messageSplit` will split up large messages sent with the `say` method
into multiple messages of length fewer than `messageSplit` characters.

Setting `autoEncoding` to true will convert all messages to UTF-8 encoding.

Setting `debug` to true will emit timestamped messages to the console
using `util.log` when certain events are fired.

Expand Down
55 changes: 48 additions & 7 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function Client(server, nick, opt) {
sasl: false,
stripColors: false,
channelPrefixes: "&#",
messageSplit: 512
messageSplit: 512,
autoEncoding: false
};

// Features supported by the server
Expand Down Expand Up @@ -93,6 +94,23 @@ function Client(server, nick, opt) {
self.connect();
}

if (self.opt.autoEncoding) {
var charsetDetector = require('node-icu-charset-detector');
var Iconv = require('iconv').Iconv;

self.convertEncoding = function(str) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about stubbing out Client.prototype.convertEncoding to be a no-op in case it is every called and Client.opt.autoEncoding is false?

var charset = charsetDetector.detectCharset(str).toString();

if (charset == 'UTF-8') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: Strict equality

return str;
} else {
var to = new Iconv(charset, 'UTF-8');

return to.convert(str);
}
};
}

self.addListener("raw", function (message) { // {{{
switch ( message.command ) {
case "rpl_welcome":
Expand Down Expand Up @@ -621,7 +639,9 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{
(self.opt.certExpired &&
self.conn.authorizationError === 'CERT_HAS_EXPIRED')) {
// authorization successful
self.conn.setEncoding('utf-8');
if (!self.opt.autoEncoding) {
self.conn.setEncoding('utf-8');
}
if ( self.opt.certExpired &&
self.conn.authorizationError === 'CERT_HAS_EXPIRED' ) {
util.log('Connecting to server with expired certificate');
Expand All @@ -645,7 +665,11 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{
}
self.conn.requestedDisconnect = false;
self.conn.setTimeout(0);
self.conn.setEncoding('utf8');

if (!self.opt.autoEncoding) {
self.conn.setEncoding('utf8');
}

self.conn.addListener("connect", function () {
if ( self.opt.sasl ) {
// see http://ircv3.atheme.org/extensions/sasl-3.1
Expand All @@ -658,11 +682,25 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{
self.send("USER", self.opt.userName, 8, "*", self.opt.realName);
self.emit("connect");
});
var buffer = '';
var buffer = new Buffer('');

self.conn.addListener("data", function (chunk) {
buffer += chunk;
var lines = buffer.split("\r\n");
buffer = lines.pop();
if (typeof(chunk) == 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: strict equality is better

buffer += chunk;
} else {
buffer = Buffer.concat([buffer, chunk]);
}

var lines = self.convertEncoding(buffer).toString().split("\r\n");

if (lines.pop()) {
// if buffer is not ended with \r\n, there's more chunks.
return;
} else {
// else, initialize the buffer.
buffer = new Buffer('');
}

lines.forEach(function (line) {
var message = parseMessage(line, self.opt.stripColors);
try {
Expand Down Expand Up @@ -888,6 +926,9 @@ Client.prototype._handleCTCP = function(from, to, text, type, message) {
Client.prototype.ctcp = function(to, type, text) {
return this[type === 'privmsg' ? 'say' : 'notice'](to, '\1'+text+'\1');
}
Client.prototype.convertEncoding = function(str) {
return str;
}

/*
* parseMessage(line, stripColors)
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
}
],
"dependencies": {
"ansi-color": "0.2.1"
"ansi-color": "0.2.1",
"iconv": "~2.1.4",
"node-icu-charset-detector": "0.0.7"
},
"devDependencies": {
"mocha": "^1.20.0",
Expand Down