Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: martynsmith/node-irc
...
head fork: noraesae/node-irc
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Oct 26, 2012
@rlidwka rlidwka encoding support
Rebased and documented by @noraesae
555ec66
HyeonJe Jun Process decoding after all the chunks have been received.
Prevent bugs that can occur when decode the large chunk with
multibyte encoding.
639432c
Showing with 51 additions and 9 deletions.
  1. +10 −1 docs/API.rst
  2. +41 −8 lib/irc.js
View
11 docs/API.rst
@@ -31,7 +31,8 @@ Client
floodProtectionDelay: 1000,
stripColors: false,
channelPrefixes: "&#",
- messageSplit: 512
+ messageSplit: 512,
+ encoding: false
}
`secure` (SSL connection) can be a true value or an object (the kind of object
@@ -55,6 +56,14 @@ Client
bold, 0x1f underline, 0x16 reverse, 0x0f reset) from the entire
message before parsing it and passing it along.
+ `encoding` can be any encoding acceptable to iconv, for example,
+ 'ASCII', 'CP1251//IGNORE', 'CP949', etc. You can also check acceptable
+ encodings with a console command, 'iconv -l'. Setting `encoding` to
+ false(default) means that you use default encoding, UTF-8. If you don't
+ use default encoding, you need to install node-iconv module::
+
+ npm install iconv
+
Setting `autoConnect` to false prevents the Client from connecting on
instantiation. You will need to call `connect()` on the client instance::
View
49 lib/irc.js
@@ -50,7 +50,8 @@ function Client(server, nick, opt) {
floodProtectionDelay: 1000,
stripColors: false,
channelPrefixes: "&#",
- messageSplit: 512
+ messageSplit: 512,
+ encoding: false
};
// Features supported by the server
@@ -86,6 +87,18 @@ function Client(server, nick, opt) {
self.activateFloodProtection();
}
+ if (self.opt.encoding) {
+ var Iconv = require('iconv').Iconv;
+ var from = new Iconv(self.opt.encoding, 'UTF-8');
+ var to = new Iconv('UTF-8', self.opt.encoding);
+ self.encode = function(str) {
+ return to.convert(str);
+ }
+ self.decode = function(str) {
+ return from.convert(str);
+ }
+ }
+
// TODO - fail if nick or server missing
// TODO - fail if username has a space in it
if (self.opt.autoConnect === true) {
@@ -589,7 +602,7 @@ 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.encoding) self.conn.setEncoding('utf-8');
if ( self.opt.certExpired &&
self.conn.authorizationError === 'CERT_HAS_EXPIRED' ) {
util.log('Connecting to server with expired certificate');
@@ -612,7 +625,7 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{
}
self.conn.requestedDisconnect = false;
self.conn.setTimeout(0);
- self.conn.setEncoding('utf8');
+ if (!self.opt.encoding) self.conn.setEncoding('utf-8');
self.conn.addListener("connect", function () {
if ( self.opt.password !== null ) {
self.send( "PASS", self.opt.password );
@@ -622,11 +635,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') {
+ buffer += chunk;
+ }
+ else {
+ buffer = Buffer.concat([buffer, chunk]);
+ }
+
+ var lines = self.decode(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 {
@@ -698,7 +725,7 @@ Client.prototype.send = function(command) { // {{{
util.log('SEND: ' + command + " " + args.join(" "));
if ( ! this.conn.requestedDisconnect ) {
- this.conn.write(command + " " + args.join(" ") + "\r\n");
+ this.conn.write(this.encode(command + " " + args.join(" ") + "\r\n"));
}
}; // }}}
Client.prototype.activateFloodProtection = function(interval) { // {{{
@@ -783,6 +810,12 @@ Client.prototype.action = function(channel, text) { // {{{
Client.prototype.notice = function(target, text) { // {{{
this.send('NOTICE', target, text);
} // }}}
+Client.prototype.encode = function(str) { // {{{
+ return str;
+} // }}}
+Client.prototype.decode = function(str) { // {{{
+ return str;
+} // }}}
Client.prototype.whois = function(nick, callback) { // {{{
if ( typeof callback === 'function' ) {
var callbackWrapper = function(info) {

No commit comments for this range

Something went wrong with that request. Please try again.