Skip to content

Commit

Permalink
universal encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ile committed Jun 11, 2011
1 parent 9524edf commit 7e3862c
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ var net = require( 'net' )
, Compiler
, deps

var Buffer = require('buffer').Buffer,
Iconv = require('iconv').Iconv;

require.paths.unshift( path.join( __dirname, '..', 'node_modules' ) )
require.paths.unshift( path.join( __dirname ) )

Expand Down Expand Up @@ -96,15 +99,57 @@ function IRC( options ) {
emitter.emit( 'disconnected' )
}

function isUTF8(data)
{
var utf_to_iso = new Iconv('UTF-8', 'ISO-8859-1');
var iso_to_utf = new Iconv('ISO-8859-1', 'UTF-8');

try
{
var data2 = utf_to_iso.convert(data);
var data3 = iso_to_utf.convert(data2);
}
catch (e)
{
return false;
}

/*
// not needed?
if (data.toString('utf8') === data3.toString('utf8'))
{
return true;
}
else
{
return false;
}
*/

return true;
}

// Parse incoming messages
function parseMessage( data ) { var buffer, last, message, command, i
internal.last_message = +new Date


if (isUTF8(data))
{
data = data.toString('utf8');
}
else
{
var my_iconv = new Iconv('ISO-8859-1', 'UTF-8//TRANSLIT//IGNORE');
var my_buffer = my_iconv.convert(data);
data = my_buffer.toString('utf8');
}

// Apply previous buffer, split, re-buffer
if ( !!internal.buffer ) {
data = internal.buffer + data
internal.buffer = ''
}

buffer = data.split( /\r\n/ )
if ( last = buffer.pop() )
internal.buffer = last
Expand Down Expand Up @@ -164,7 +209,7 @@ function IRC( options ) {
}

stream = new net.Stream()
stream.setEncoding( this.options.encoding )
//stream.setEncoding( this.options.encoding )
stream.setTimeout( 0 )

// Forward network errors
Expand Down

0 comments on commit 7e3862c

Please sign in to comment.