diff --git a/packages/cory/chat/chat.js b/packages/cory/chat/chat.js index c55e3d1e..6e62fb78 100644 --- a/packages/cory/chat/chat.js +++ b/packages/cory/chat/chat.js @@ -67,8 +67,8 @@ function addAccount() { return database.get(kAccountsKey).then(function(data) { let accounts = data ? JSON.parse(data) : []; let id = 0; - for (var i in accounts) { - id = Math.max(id, accounts[i].id + 1); + while (accounts.some(x => x.id == id)) { + id++; } accounts.push({name: "unnamed", id: id}); return database.set(kAccountsKey, JSON.stringify(accounts)); @@ -150,7 +150,7 @@ function deleteAccount(id) { return database.get(kAccountsKey).then(function(data) { let accounts = data ? JSON.parse(data) : []; 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); break; } @@ -164,11 +164,12 @@ function connect(id) { let accounts = data ? JSON.parse(data) : []; let account; 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]; break; } } + print(id, account); if (account) { let self = {account: account}; diff --git a/packages/cory/libirc/libirc.js b/packages/cory/libirc/libirc.js index bd580d34..f478ef3d 100644 --- a/packages/cory/libirc/libirc.js +++ b/packages/cory/libirc/libirc.js @@ -18,11 +18,13 @@ //! ] //! }, //! "require": [ -//! "libchat" +//! "libchat", +//! "libencoding" //! ] //! } let ChatService = require("libchat").ChatService; +require("libencoding"); class IrcService { 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) { return this._socket.write(line + "\r\n"); } _receivedLine(originalLine) { try { - let line = this._decode(originalLine); + let line = originalLine; let prefix; if (line.charAt(0) == ":") { let space = line.indexOf(" "); @@ -134,19 +125,23 @@ class IrcService { _connect(options) { 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) { if (data) { - readBuffer += data; - let end = readBuffer.indexOf("\n"); + let newBuffer = new Uint8Array(readBuffer.length + data.length); + newBuffer.set(readBuffer, 0); + newBuffer.set(data, readBuffer.length); + readBuffer = newBuffer; + + let end = readBuffer.indexOf(kNewLine); while (end != -1) { - let line = readBuffer.substring(0, end); - if (line.charAt(line.length - 1) == "\r") { - line = line.substring(0, line.length - 1); - } - readBuffer = readBuffer.substring(end + 1); - self._receivedLine(line); - end = readBuffer.indexOf("\n"); + let line = readBuffer.slice(0, (end > 0 && readBuffer[end - 1] == kCarriageReturn) ? end - 1 : end); + readBuffer = readBuffer.slice(end + 1); + self._receivedLine(new TextDecoder("UTF-8").decode(line)); + end = readBuffer.indexOf(kNewLine); } } else { self._service.notifyStateChanged("disconnected");