Old chat tweaks.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3360 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2016-12-22 18:24:19 +00:00
parent 4313cfbc4f
commit a1e2c2abae
4 changed files with 25 additions and 21 deletions

View File

@ -328,6 +328,7 @@ function getConversation(session, conversationName) {
} }
async function printToConversation(conversation, message, notify) { async function printToConversation(conversation, message, notify) {
print(message);
if (conversation == gCurrentConversation) { if (conversation == gCurrentConversation) {
if (message.action == "message") { if (message.action == "message") {
await printMessage(message.message); await printMessage(message.message);
@ -409,26 +410,26 @@ function niceTime(lastTime, thisTime) {
async function formatMessage(message) { async function formatMessage(message) {
var result; var result;
if (typeof message == "string") { if (typeof message == "string") {
for (let i = 0; i < message.length; i++) { for (let i = message.length - 1; i >= 0; i--) {
if (message.charCodeAt(i) >= 128 /*&& message.charCodeAt(i) < 256*/) { if (message.charCodeAt(i) >= 128 || message.charCodeAt(i) < 32) {
message = message.substring(0, i) + "?" + message.substring(i + 1); message = message.substring(0, i) + "\\" + message.charCodeAt(i).toString() + message.substring(i + 1);
} }
} }
result = []; result = [];
var regex = /(\w+:\/*\S+?)(?=(?:[\.!?])?(?:$|\s))/gi; var regex = /(\w+:\/*\S+?)(?=(?:[\.!?])?(?:$|\s))/gi;
var match; var match;
var lastIndex = 0; var lastIndex = 0;
let libunfurl; // let libunfurl;
while ((match = regex.exec(message)) !== null) { while ((match = regex.exec(message)) !== null) {
if (!libunfurl) { // if (!libunfurl) {
libunfurl = await core.getService("libunfurl", "libunfurl"); // libunfurl = await core.getService("libunfurl", "libunfurl");
} // }
result.push({class: "base1", value: message.substring(lastIndex, match.index)}); result.push({class: "base1", value: message.substring(lastIndex, match.index)});
if (!libunfurl) { // if (!libunfurl) {
result.push({href: match[0]}); result.push({href: match[0]});
} else { // } else {
result.push(await libunfurl.postMessage(match[0])); // result.push(await libunfurl.postMessage(match[0]));
} // }
lastIndex = regex.lastIndex; lastIndex = regex.lastIndex;
} }
result.push({class: "base1", value: message.substring(lastIndex)}); result.push({class: "base1", value: message.substring(lastIndex)});

View File

@ -5,12 +5,14 @@
function parseUrl(url) { function parseUrl(url) {
// XXX: Hack. // XXX: Hack.
var match = url.match(new RegExp("(\\w+)://([^/]+)?(.*)")); var match = url.match(new RegExp("(\\w+)://([^/]+)?(.*)"));
if (match) {
return { return {
protocol: match[1], protocol: match[1],
host: match[2], host: match[2],
path: match[3], path: match[3],
port: match[1] == "http" ? 80 : 443, port: match[1] == "http" ? 80 : 443,
}; };
}
} }
function parseResponse(data) { function parseResponse(data) {
@ -37,6 +39,9 @@ function get(url) {
return new Promise(async function(resolve, reject) { return new Promise(async function(resolve, reject) {
try { try {
let parsed = parseUrl(url); let parsed = parseUrl(url);
if (!parsed) {
throw new Error("Failed to parse: " + url);
}
let buffer = ""; let buffer = "";
let socket = await network.newConnection(); let socket = await network.newConnection();

View File

@ -172,6 +172,7 @@ class IrcService {
this._send("QUIT"); this._send("QUIT");
this._socket.close(); this._socket.close();
this._service.notifyStateChanged("disconnected"); this._service.notifyStateChanged("disconnected");
exit(0);
} }
}; };

View File

@ -65,13 +65,10 @@ core.register("onMessage", async function(sender, message) {
if (gCache[message] && new Date().valueOf() < gCache[message].expires) { if (gCache[message] && new Date().valueOf() < gCache[message].expires) {
result = gCache[message].result; result = gCache[message].result;
} else { } else {
if (gCache[message]) {
print("Didn't use", gCache[message]);
}
try { try {
result = await unfurl(message); result = await unfurl(message);
} catch (error) { } catch (error) {
result = [message, "(error retrieving: ", error, ")"]; result = [{href: message}, " (error retrieving: ", error, ")"];
} }
gCache[message] = { gCache[message] = {
expires: new Date().valueOf() + 7 * 24 * 60 * 60 * 1000, expires: new Date().valueOf() + 7 * 24 * 60 * 60 * 1000,