-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added message auto encoding #269
Closed
+55
−9
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,8 @@ function Client(server, nick, opt) { | |
sasl: false, | ||
stripColors: false, | ||
channelPrefixes: "&#", | ||
messageSplit: 512 | ||
messageSplit: 512, | ||
autoEncoding: false | ||
}; | ||
|
||
// Features supported by the server | ||
|
@@ -93,6 +94,23 @@ function Client(server, nick, opt) { | |
self.connect(); | ||
} | ||
|
||
if (self.opt.autoEncoding) { | ||
var charsetDetector = require('node-icu-charset-detector'); | ||
var Iconv = require('iconv').Iconv; | ||
|
||
self.convertEncoding = function(str) { | ||
var charset = charsetDetector.detectCharset(str).toString(); | ||
|
||
if (charset == 'UTF-8') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: Strict equality |
||
return str; | ||
} else { | ||
var to = new Iconv(charset, 'UTF-8'); | ||
|
||
return to.convert(str); | ||
} | ||
}; | ||
} | ||
|
||
self.addListener("raw", function (message) { // {{{ | ||
switch ( message.command ) { | ||
case "rpl_welcome": | ||
|
@@ -621,7 +639,9 @@ 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.autoEncoding) { | ||
self.conn.setEncoding('utf-8'); | ||
} | ||
if ( self.opt.certExpired && | ||
self.conn.authorizationError === 'CERT_HAS_EXPIRED' ) { | ||
util.log('Connecting to server with expired certificate'); | ||
|
@@ -645,7 +665,11 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{ | |
} | ||
self.conn.requestedDisconnect = false; | ||
self.conn.setTimeout(0); | ||
self.conn.setEncoding('utf8'); | ||
|
||
if (!self.opt.autoEncoding) { | ||
self.conn.setEncoding('utf8'); | ||
} | ||
|
||
self.conn.addListener("connect", function () { | ||
if ( self.opt.sasl ) { | ||
// see http://ircv3.atheme.org/extensions/sasl-3.1 | ||
|
@@ -658,11 +682,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') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: strict equality is better |
||
buffer += chunk; | ||
} else { | ||
buffer = Buffer.concat([buffer, chunk]); | ||
} | ||
|
||
var lines = self.convertEncoding(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 { | ||
|
@@ -888,6 +926,9 @@ Client.prototype._handleCTCP = function(from, to, text, type, message) { | |
Client.prototype.ctcp = function(to, type, text) { | ||
return this[type === 'privmsg' ? 'say' : 'notice'](to, '\1'+text+'\1'); | ||
} | ||
Client.prototype.convertEncoding = function(str) { | ||
return str; | ||
} | ||
|
||
/* | ||
* parseMessage(line, stripColors) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about stubbing out
Client.prototype.convertEncoding
to be a no-op in case it is every called andClient.opt.autoEncoding
is false?