Let IRC use UTF-8. Fix some bugs along the way. ¯\_(ツ)_/¯

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3364 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2016-12-23 19:36:14 +00:00
parent 8a46fd4560
commit fe765ed834
2 changed files with 23 additions and 27 deletions

View File

@ -67,8 +67,8 @@ function addAccount() {
return database.get(kAccountsKey).then(function(data) { return database.get(kAccountsKey).then(function(data) {
let accounts = data ? JSON.parse(data) : []; let accounts = data ? JSON.parse(data) : [];
let id = 0; let id = 0;
for (var i in accounts) { while (accounts.some(x => x.id == id)) {
id = Math.max(id, accounts[i].id + 1); id++;
} }
accounts.push({name: "unnamed", id: id}); accounts.push({name: "unnamed", id: id});
return database.set(kAccountsKey, JSON.stringify(accounts)); return database.set(kAccountsKey, JSON.stringify(accounts));
@ -150,7 +150,7 @@ function deleteAccount(id) {
return database.get(kAccountsKey).then(function(data) { return database.get(kAccountsKey).then(function(data) {
let accounts = data ? JSON.parse(data) : []; let accounts = data ? JSON.parse(data) : [];
for (var i = 0; i < accounts.length; i++) { for (var i = 0; i < accounts.length; i++) {
if (accounts[i] && (!accounts[i].id || accounts[i].id == id)) { if (accounts[i] && (accounts[i].id == id)) {
accounts.splice(i, 1); accounts.splice(i, 1);
break; break;
} }
@ -164,11 +164,12 @@ function connect(id) {
let accounts = data ? JSON.parse(data) : []; let accounts = data ? JSON.parse(data) : [];
let account; let account;
for (var i = 0; i < accounts.length; i++) { for (var i = 0; i < accounts.length; i++) {
if (accounts[i] && (!accounts[i].id || accounts[i].id == id)) { if (accounts[i] && accounts[i].id == id) {
account = accounts[i]; account = accounts[i];
break; break;
} }
} }
print(id, account);
if (account) { if (account) {
let self = {account: account}; let self = {account: account};

View File

@ -18,11 +18,13 @@
//! ] //! ]
//! }, //! },
//! "require": [ //! "require": [
//! "libchat" //! "libchat",
//! "libencoding"
//! ] //! ]
//! } //! }
let ChatService = require("libchat").ChatService; let ChatService = require("libchat").ChatService;
require("libencoding");
class IrcService { class IrcService {
constructor(options) { constructor(options) {
@ -38,24 +40,13 @@ class IrcService {
}); });
} }
_decode(text) {
if (text) {
for (let i = 0; i < text.length; i++) {
if (text.charCodeAt(i) > 128) {
text = text.substring(0, i) + "?" + text.substring(i + 1);
}
}
}
return text;
}
_send(line) { _send(line) {
return this._socket.write(line + "\r\n"); return this._socket.write(line + "\r\n");
} }
_receivedLine(originalLine) { _receivedLine(originalLine) {
try { try {
let line = this._decode(originalLine); let line = originalLine;
let prefix; let prefix;
if (line.charAt(0) == ":") { if (line.charAt(0) == ":") {
let space = line.indexOf(" "); let space = line.indexOf(" ");
@ -134,19 +125,23 @@ class IrcService {
_connect(options) { _connect(options) {
let self = this; let self = this;
let readBuffer = ""; let kNewLine = '\n'.charCodeAt(0);
let kCarriageReturn = '\r'.charCodeAt(0);
let readBuffer = new Uint8Array(0);
self._socket.read(function(data) { self._socket.read(function(data) {
if (data) { if (data) {
readBuffer += data; let newBuffer = new Uint8Array(readBuffer.length + data.length);
let end = readBuffer.indexOf("\n"); newBuffer.set(readBuffer, 0);
newBuffer.set(data, readBuffer.length);
readBuffer = newBuffer;
let end = readBuffer.indexOf(kNewLine);
while (end != -1) { while (end != -1) {
let line = readBuffer.substring(0, end); let line = readBuffer.slice(0, (end > 0 && readBuffer[end - 1] == kCarriageReturn) ? end - 1 : end);
if (line.charAt(line.length - 1) == "\r") { readBuffer = readBuffer.slice(end + 1);
line = line.substring(0, line.length - 1); self._receivedLine(new TextDecoder("UTF-8").decode(line));
} end = readBuffer.indexOf(kNewLine);
readBuffer = readBuffer.substring(end + 1);
self._receivedLine(line);
end = readBuffer.indexOf("\n");
} }
} else { } else {
self._service.notifyStateChanged("disconnected"); self._service.notifyStateChanged("disconnected");