Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

encoding support with documentation #113

Closed
wants to merge 2 commits into from

4 participants

@noraesae

Hi!

I rebased and documented the pull request #85.
Please check the patch and documentation,
and if there's something wrong or i can do further, please let me know.

Thanks!

Regards.

@martynsmith
Owner

This looks great. Just a quick thought, do you think it'd be better to decode the buffer after all the chunks have been received (then you could get rid of that comment about potential bugs on multi-byte characters spanning chunks).

It seems like it'd be fairly easy to do.

Either way, If I don't hear back from you I'll merge this :-)

@noraesae

Thank you for the reply.
I'll hack into it.

Regards.

@noraesae

I commited a patch to decode the buffer after all the chunks've been received.

Plz check the patch, and if there's something wrong, let me know.

Thanks!

Regards.

rlidwka and others added some commits
@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
@noraesae

ping

This was referenced
@sigkell sigkell closed this
@tarlepp tarlepp referenced this pull request
Closed

Added message auto encoding #269

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 26, 2012
  1. @rlidwka

    encoding support

    rlidwka authored HyeonJe Jun committed
    Rebased and documented by @noraesae
  2. Process decoding after all the chunks have been received.

    HyeonJe Jun authored
    Prevent bugs that can occur when decode the large chunk with
    multibyte encoding.
This page is out of date. Refresh to see the latest.
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) {
Something went wrong with that request. Please try again.