Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

encoding support with documentation #113

Closed
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+51 −9
Split
View
@@ -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
@@ -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) {