From dca3ad9b79a2d67c10b38844338d9da6f88639c8 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Thu, 2 Jun 2016 23:01:55 +0000 Subject: [PATCH] Backlog of miscellaneous fixes. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3234 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 50 ++++++++++++++++++++++++++------ packages/cory/chat/chat.js | 7 ++++- packages/cory/index/index.js | 2 +- packages/cory/irc/irc.js | 26 +++++++++++++++-- packages/cory/libchat/libchat.js | 12 ++++++++ packages/cory/smtp/smtp.js | 4 +-- packages/cory/ui/ui.js | 21 +++++++------- 7 files changed, 96 insertions(+), 26 deletions(-) diff --git a/core/client.js b/core/client.js index 3dad049d..d6e4a102 100644 --- a/core/client.js +++ b/core/client.js @@ -5,6 +5,7 @@ var gSessionId; var gCredentials; var gErrorCount = 0; var gCommandHistory = []; +var gSendKeyEvents = false; var kMaxCommandHistory = 16; @@ -85,7 +86,8 @@ function split(container, children) { var grow = children[i].grow || "1"; var shrink = children[i].shrink || "1"; var basis = children[i].basis || "auto"; - node.setAttribute("style", "flex: " + grow + " " + shrink + " " + basis); + var style = children[i].style || ""; + node.setAttribute("style", style + "; flex: " + grow + " " + shrink + " " + basis); var classes = ["terminal"]; if (children[i].type == "vertical") { @@ -156,12 +158,36 @@ function receive(data) { if (iframe) { iframe.contentWindow.postMessage(line[0].message, "*"); } + } else if (line && line[0] && line[0].action == "setSendKeyEvents") { + var value = line[0].value; + if (value && !gSendKeyEvents) { + window.addEventListener("keydown", keyEvent); + window.addEventListener("keypress", keyEvent); + window.addEventListener("keyup", keyEvent); + } else if (!value && gSendKeyEvents) { + window.removeEventListener("keydown", keyEvent); + window.removeEventListener("keypress", keyEvent); + window.removeEventListener("keyup", keyEvent); + } + gSendKeyEvents = value; } else { print(document.getElementById(target), line); } } } +function keyEvent(event) { + send({ + event: "key", + type: event.type, + which: event.which, + keyCode: event.keyCode, + charCode: event.charCode, + character: String.fromCharCode(event.keyCode || event.which), + + }); +} + function autoNewLine(terminal) { terminal.appendChild(document.createElement("br")); } @@ -173,13 +199,18 @@ function print(terminal, data) { } function printSvg(container, data, name, namespace) { - var node = document.createElementNS("http://www.w3.org/2000/svg", name); - for (var i in data.attributes) { - node.setAttribute(i, data.attributes[i]); - } - if (data.children) { - for (var i in data.children) { - node.appendChild(printSvg(node, data.children[i], data.children[i].name)); + var node; + if (typeof data == "string") { + node = document.createTextNode(data); + } else { + node = document.createElementNS("http://www.w3.org/2000/svg", name); + for (var i in data.attributes) { + node.setAttribute(i, data.attributes[i]); + } + if (data.children) { + for (var i in data.children) { + node.appendChild(printSvg(node, data.children[i], data.children[i].name)); + } } } return node; @@ -201,7 +232,7 @@ function printStructured(container, data) { if (data.href) { node = document.createElement("a"); node.setAttribute("href", data.href); - node.setAttribute("target", "_blank"); + node.setAttribute("target", data.target || "_blank"); } else if (data.iframe) { node = document.createElement("iframe"); node.setAttribute("srcdoc", data.iframe); @@ -481,6 +512,7 @@ function connectSocket() { ['setPrompt', 'value'], ['setTitle', 'value'], ['split', 'options'], + ['setSendKeyEvents', 'value'], ], })); } diff --git a/packages/cory/chat/chat.js b/packages/cory/chat/chat.js index 94fbe138..77c9f1be 100644 --- a/packages/cory/chat/chat.js +++ b/packages/cory/chat/chat.js @@ -11,6 +11,7 @@ function updateTitle() { terminal.setTitle((gUnread ? "(" + gUnread.toString() + ") " : "") + "Chat"); } +let kMaxHistory = 32; let kAccountsKey = JSON.stringify(["accounts", core.user.name]); let kStateKey = JSON.stringify(["state", core.user.name]); @@ -218,6 +219,9 @@ function updateConversation() { let history = data[0]; let participants = data[1]; gCurrentConversation.messages = history || []; + if (gCurrentConversation.messages.length > kMaxHistory) { + gCurrentConversation.messages.splice(0, gCurrentConversation.messages.length - kMaxHistory); + } gCurrentConversation.participants = participants || []; terminal.cork(); terminal.select("terminal"); @@ -227,7 +231,7 @@ function updateConversation() { if (message.action == "message") { printMessage(message.message); } else { - terminal.print(message); + terminal.print(message.message); } } updateUsers(); @@ -283,6 +287,7 @@ function getConversation(session, conversationName) { sendMessage: function(message) { return session.sendMessage(key, message); }, + participants: [], }; updateWindows(); } diff --git a/packages/cory/index/index.js b/packages/cory/index/index.js index 0ff19944..8fa464c6 100644 --- a/packages/cory/index/index.js +++ b/packages/cory/index/index.js @@ -65,7 +65,7 @@ function index() { } terminal.print( "* ", - {href: "/~" + app.owner + "/" + app.name}, + {href: "/~" + app.owner + "/" + app.name, target: "_self"}, message, app.manifest && app.manifest.description ? " - " + app.manifest.description.toString() : ""); }); diff --git a/packages/cory/irc/irc.js b/packages/cory/irc/irc.js index cad0cdad..6c56411d 100644 --- a/packages/cory/irc/irc.js +++ b/packages/cory/irc/irc.js @@ -29,6 +29,7 @@ class IrcService { self._service = new ChatService(options.callback); self._name = options.name; self._nick = options.nick; + self._nameReplies = {}; network.newConnection().then(function(socket) { self._socket = socket; @@ -66,7 +67,7 @@ class IrcService { // Is it a channel type? if ("&#!+.".indexOf(parts[1].charAt(0)) != -1) { conversation = parts[1]; - } else { + } else if (prefix.indexOf('!') != -1) { conversation = prefix.split('!')[0]; } this._service.notifyMessageReceived(conversation, { @@ -81,10 +82,29 @@ class IrcService { let person = prefix.split('!')[0]; let conversation = parts[1]; this._service.notifyPresenceChanged(conversation, person, "present"); - } else if (parts[0] == "JOIN") { + } else if (parts[0] == "PART") { let person = prefix.split('!')[0]; let conversation = parts[1]; this._service.notifyPresenceChanged(conversation, person, "unavailable"); + } else if (parts[0] == "353") { // RPL_NAMREPLY + if (!this._nameReplies[parts[3]]) { + this._nameReplies[parts[3]] = []; + } + let users = parts[4].split(' '); + for (let i in users) { + let user = users[i]; + let state = "present"; + if ("@+".indexOf(user.charAt(0)) != -1) { + state = user.charAt(0); + user = user.substring(1); + } + this._nameReplies[parts[3]][user] = state; + } + } else if (parts[0] == "366") { // RPL_ENDOFNAMES + for (let conversation in this._nameReplies) { + this._service.notifyParticipantList(conversation, this._nameReplies[conversation]); + } + this._nameReplies = {}; } else { this._service.notifyMessageReceived("", {from: prefix, message: lineNoPrefix}); } @@ -127,7 +147,7 @@ class IrcService { } else { this._socket.write("PRIVMSG " + target + " :" + text + "\r\n"); } - this._service.notifyMessageReceived(target || "", {from: self._nick, message: text, timestamp: new Date().toString()}); + this._service.notifyMessageReceived(target || "", {from: this._nick, message: text, timestamp: new Date().toString()}); } disconnect() { diff --git a/packages/cory/libchat/libchat.js b/packages/cory/libchat/libchat.js index 508a0f31..e86e8d3a 100644 --- a/packages/cory/libchat/libchat.js +++ b/packages/cory/libchat/libchat.js @@ -86,6 +86,18 @@ exports.ChatService = class { }); } + notifyParticipantList(conversation, participants) { + let current = this._getConversation(conversation).participants; + for (let i in current) { + if (!participants[i]) { + this.notifyPresenceChanged(conversation, i, "unavailable"); + } + } + for (let i in participants) { + this.notifyPresenceChanged(conversation, i, participants[i]); + } + } + notifyStateChanged(state) { this._state = state; this._invokeCallback({action: state}); diff --git a/packages/cory/smtp/smtp.js b/packages/cory/smtp/smtp.js index 18a12029..69e5805b 100644 --- a/packages/cory/smtp/smtp.js +++ b/packages/cory/smtp/smtp.js @@ -4,7 +4,7 @@ terminal.print("Hello, world!"); -let kServer = "rowlf.unprompted.com"; +let kServer = "localhost"; class Smtp { constructor() { @@ -29,7 +29,7 @@ class Smtp { reject(error.message); } }); - socket.connect("localhost", 25).catch(reject); + socket.connect(kServer, 25).catch(reject); }); }); } diff --git a/packages/cory/ui/ui.js b/packages/cory/ui/ui.js index 5939756d..b88429e1 100644 --- a/packages/cory/ui/ui.js +++ b/packages/cory/ui/ui.js @@ -1,16 +1,6 @@ "use strict"; function fileList(settings) { - - terminal.setEcho(false); - terminal.clear(); - terminal.print(settings.title); - if (core.user.credentials - && core.user.credentials.permissions - && core.user.credentials.permissions.authenticated) { - terminal.print({command: "new"}); - } - let prefix = settings.prefix || ""; let makeSaveCallback = function(oldName, oldValue) { @@ -39,6 +29,16 @@ function fileList(settings) { core.register("hashChange", hashChange); return database.getAll().then(function(entries) { + terminal.cork(); + terminal.setEcho(false); + terminal.clear(); + terminal.print(settings.title); + if (core.user.credentials + && core.user.credentials.permissions + && core.user.credentials.permissions.authenticated) { + terminal.print({command: "new"}); + } + terminal.readLine().then(function(input) { if (input == "new") { terminal.setHash(name); @@ -97,6 +97,7 @@ function fileList(settings) { } } } + terminal.uncork(); }); }