Skip to content

Commit

Permalink
Merge pull request #347 from aivot-on/master
Browse files Browse the repository at this point in the history
Fix charset conversion for invalid charsets
  • Loading branch information
jirwin committed Apr 10, 2015
2 parents 1e40e51 + e423bcb commit a29e052
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
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 <xandy@hackerspace-bamberg.de>",
"Mischa Spiegelmock <revmischa@cpan.org>",
"Justin Gallardo <justin.gallardo@gmail.com>",
"Chris Nehren <cnehren@pobox.com>"
"Chris Nehren <cnehren@pobox.com>",
"Henri Niemeläinen <aivot-on@iki.fi>"
],
"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",
":TijG!~TijG@null.1ago.be 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) {
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();
});

0 comments on commit a29e052

Please sign in to comment.