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

Fix charset conversion for invalid charsets #347

Merged
merged 1 commit into from
Apr 10, 2015
Merged
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
25 changes: 16 additions & 9 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1026,18 +1026,25 @@ Client.prototype.ctcp = function(to, type, text) {
};

Client.prototype.convertEncoding = function(str) {
var self = this;
var self = this, out = str;

if (self.opt.encoding) {
var charsetDetector = require('node-icu-charset-detector');
var Iconv = require('iconv').Iconv;
var charset = charsetDetector.detectCharset(str).toString();
var to = new Iconv(charset, self.opt.encoding);

return to.convert(str);
} else {
return str;
try {
var charsetDetector = require('node-icu-charset-detector');
var Iconv = require('iconv').Iconv;
var charset = charsetDetector.detectCharset(str);
var converter = new Iconv(charset.toString(), self.opt.encoding);

out = converter.convert(str);
} catch (err) {
if (self.opt.debug) {
util.log('\u001b[01;31mERROR: ' + err + '\u001b[0m');
util.inspect({ str: str, charset: charset });
}
}
}

return out;
};
// blatantly stolen from irssi's splitlong.pl. Thanks, Bjoern Krombholz!
Client.prototype._updateMaxLineLength = function() {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"xAndy <[email protected]>",
"Mischa Spiegelmock <[email protected]>",
"Justin Gallardo <[email protected]>",
"Chris Nehren <[email protected]>"
"Chris Nehren <[email protected]>",
"Henri Niemeläinen <[email protected]>"
],
"repository": {
"type": "git",
Expand Down
10 changes: 9 additions & 1 deletion test/data/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,13 @@
"nick is as expected after 433",
"maxLineLength is as expected after 433"
]
}
},
"convert-encoding": {
"causesException": [
":ubottu!ubottu@ubuntu/bot/ubottu MODE #ubuntu -bo *!~Brian@* ubottu\r\n",
"Elizabeth",
":sblack1!~sblack1@unaffiliated/sblack1 NICK :sblack\r\n",
":[email protected] PRIVMSG #ubuntu :ThinkPad\r\n"
]
}
}
53 changes: 53 additions & 0 deletions test/test-convert-encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var irc = require('../lib/irc');
var test = require('tape');
var testHelpers = require('./helpers');
var checks = testHelpers.getFixtures('convert-encoding');
var bindTo = { opt: { encoding: 'utf-8' } };

test('irc.Client.convertEncoding old', function(assert) {
var convertEncoding = function(str) {
var self = this;

if (self.opt.encoding) {
var charsetDetector = require('node-icu-charset-detector');
var Iconv = require('iconv').Iconv;
var charset = charsetDetector.detectCharset(str).toString();
var to = new Iconv(charset, self.opt.encoding);

return to.convert(str);
} else {
return str;
}
}.bind(bindTo);

checks.causesException.forEach(function iterate(line) {
var causedException = false;
try {
convertEncoding(line);
} catch (e) {
causedException = true;
}

assert.equal(causedException, true, line + ' caused exception');
});

assert.end();
});

test('irc.Client.convertEncoding', function(assert) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you ever setting Client.opt.encoding to true so that your new code is being executed?

var convertEncoding = irc.Client.prototype.convertEncoding.bind(bindTo);

checks.causesException.forEach(function iterate(line) {
var causedException = false;

try {
convertEncoding(line);
} catch (e) {
causedException = true;
}

assert.equal(causedException, false, line + ' didn\'t cause exception');
});

assert.end();
});