From a1e2c2abaefae172a68718ef0476452974f0c5de Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 22 Dec 2016 18:24:19 +0000 Subject: [PATCH] Old chat tweaks. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3360 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- packages/cory/chat/chat.js | 23 ++++++++++++----------- packages/cory/libhttp/libhttp.js | 17 +++++++++++------ packages/cory/libirc/libirc.js | 1 + packages/cory/libunfurl/libunfurl.js | 5 +---- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/cory/chat/chat.js b/packages/cory/chat/chat.js index 19d5c95d..5bb60cd3 100644 --- a/packages/cory/chat/chat.js +++ b/packages/cory/chat/chat.js @@ -328,6 +328,7 @@ function getConversation(session, conversationName) { } async function printToConversation(conversation, message, notify) { + print(message); if (conversation == gCurrentConversation) { if (message.action == "message") { await printMessage(message.message); @@ -409,26 +410,26 @@ function niceTime(lastTime, thisTime) { async function formatMessage(message) { var result; if (typeof message == "string") { - for (let i = 0; i < message.length; i++) { - if (message.charCodeAt(i) >= 128 /*&& message.charCodeAt(i) < 256*/) { - message = message.substring(0, i) + "?" + message.substring(i + 1); + for (let i = message.length - 1; i >= 0; i--) { + if (message.charCodeAt(i) >= 128 || message.charCodeAt(i) < 32) { + message = message.substring(0, i) + "\\" + message.charCodeAt(i).toString() + message.substring(i + 1); } } result = []; var regex = /(\w+:\/*\S+?)(?=(?:[\.!?])?(?:$|\s))/gi; var match; var lastIndex = 0; - let libunfurl; +// let libunfurl; while ((match = regex.exec(message)) !== null) { - if (!libunfurl) { - libunfurl = await core.getService("libunfurl", "libunfurl"); - } +// if (!libunfurl) { +// libunfurl = await core.getService("libunfurl", "libunfurl"); +// } result.push({class: "base1", value: message.substring(lastIndex, match.index)}); - if (!libunfurl) { +// if (!libunfurl) { result.push({href: match[0]}); - } else { - result.push(await libunfurl.postMessage(match[0])); - } +// } else { +// result.push(await libunfurl.postMessage(match[0])); +// } lastIndex = regex.lastIndex; } result.push({class: "base1", value: message.substring(lastIndex)}); diff --git a/packages/cory/libhttp/libhttp.js b/packages/cory/libhttp/libhttp.js index cac80b93..7c17bd83 100644 --- a/packages/cory/libhttp/libhttp.js +++ b/packages/cory/libhttp/libhttp.js @@ -5,12 +5,14 @@ function parseUrl(url) { // XXX: Hack. var match = url.match(new RegExp("(\\w+)://([^/]+)?(.*)")); - return { - protocol: match[1], - host: match[2], - path: match[3], - port: match[1] == "http" ? 80 : 443, - }; + if (match) { + return { + protocol: match[1], + host: match[2], + path: match[3], + port: match[1] == "http" ? 80 : 443, + }; + } } function parseResponse(data) { @@ -37,6 +39,9 @@ function get(url) { return new Promise(async function(resolve, reject) { try { let parsed = parseUrl(url); + if (!parsed) { + throw new Error("Failed to parse: " + url); + } let buffer = ""; let socket = await network.newConnection(); diff --git a/packages/cory/libirc/libirc.js b/packages/cory/libirc/libirc.js index 7a7d11bb..bd580d34 100644 --- a/packages/cory/libirc/libirc.js +++ b/packages/cory/libirc/libirc.js @@ -172,6 +172,7 @@ class IrcService { this._send("QUIT"); this._socket.close(); this._service.notifyStateChanged("disconnected"); + exit(0); } }; diff --git a/packages/cory/libunfurl/libunfurl.js b/packages/cory/libunfurl/libunfurl.js index 7ead793f..b0b9f898 100644 --- a/packages/cory/libunfurl/libunfurl.js +++ b/packages/cory/libunfurl/libunfurl.js @@ -65,13 +65,10 @@ core.register("onMessage", async function(sender, message) { if (gCache[message] && new Date().valueOf() < gCache[message].expires) { result = gCache[message].result; } else { - if (gCache[message]) { - print("Didn't use", gCache[message]); - } try { result = await unfurl(message); } catch (error) { - result = [message, "(error retrieving: ", error, ")"]; + result = [{href: message}, " (error retrieving: ", error, ")"]; } gCache[message] = { expires: new Date().valueOf() + 7 * 24 * 60 * 60 * 1000,