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) {
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};

View File

@ -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");