gamja-old/components/app.js

1675 lines
42 KiB
JavaScript
Raw Normal View History

import * as irc from "../lib/irc.js";
import Client from "../lib/client.js";
import Buffer from "./buffer.js";
import BufferList from "./buffer-list.js";
import BufferHeader from "./buffer-header.js";
import MemberList from "./member-list.js";
import ConnectForm from "./connect-form.js";
import JoinForm from "./join-form.js";
import Help from "./help.js";
2021-03-08 12:15:04 -05:00
import NetworkForm from "./network-form.js";
import AuthForm from "./auth-form.js";
import Composer from "./composer.js";
import ScrollManager from "./scroll-manager.js";
2021-03-08 10:23:16 -05:00
import Dialog from "./dialog.js";
import { html, Component, createRef } from "../lib/index.js";
import { strip as stripANSI } from "../lib/ansi.js";
import { SERVER_BUFFER, BufferType, ReceiptType, ServerStatus, Unread, State, getServerName } from "../state.js";
import commands from "../commands.js";
import { setup as setupKeybindings } from "../keybindings.js";
import * as store from "../store.js";
2020-06-26 04:35:38 -04:00
const baseConfig = {
server: {},
};
const configPromise = fetch("./config.json")
.then((resp) => {
if (resp.ok) {
return resp.json();
}
if (resp.status !== 404) {
console.error("Failed to fetch config: HTTP error:", resp.status, resp.statusText);
}
return {};
})
.catch((err) => {
console.error("Failed to fetch config:", err);
return {};
})
.then((config) => {
return {
...baseConfig,
...config,
};
});
2020-07-15 12:21:09 -04:00
const CHATHISTORY_MAX_SIZE = 4000;
function parseQueryString() {
2021-06-10 12:11:11 -04:00
let query = window.location.search.substring(1);
let params = {};
query.split('&').forEach((s) => {
if (!s) {
return;
}
2021-06-10 12:11:11 -04:00
let pair = s.split('=');
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || "");
});
return params;
}
2021-11-16 05:52:38 -05:00
function splitHostPort(str) {
let host = str;
let port = null;
// Literal IPv6 addresses contain colons and are enclosed in square brackets
let i = str.lastIndexOf(":");
if (i > 0 && !str.endsWith("]")) {
host = str.slice(0, i);
port = parseInt(str.slice(i + 1), 10);
}
if (host.startsWith("[") && host.endsWith("]")) {
host = host.slice(1, host.length - 1);
}
return { host, port };
}
function fillConnectParams(params) {
2021-06-10 12:11:11 -04:00
let host = window.location.host || "localhost:8080";
let proto = "wss:";
if (window.location.protocol != "https:") {
proto = "ws:";
}
2021-06-10 12:11:11 -04:00
let path = window.location.pathname || "/";
if (!window.location.host) {
path = "/";
}
params = { ...params };
if (!params.url) {
params.url = proto + "//" + host + path + "socket";
}
if (params.url.startsWith("/")) {
params.url = proto + "//" + host + params.url;
}
if (params.url.indexOf("://") < 0) {
params.url = proto + "//" + params.url;
}
if (!params.username) {
params.username = params.nick;
}
if (!params.realname) {
params.realname = params.nick;
}
return params;
}
2020-07-15 12:21:09 -04:00
function debounce(f, delay) {
2021-06-10 12:11:11 -04:00
let timeout = null;
2020-07-15 12:21:09 -04:00
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
timeout = null;
f(...args);
}, delay);
};
}
function showNotification(title, options) {
if (!window.Notification || Notification.permission !== "granted") {
return new EventTarget();
}
// This can still fail due to:
// https://bugs.chromium.org/p/chromium/issues/detail?id=481856
try {
return new Notification(title, options);
} catch (err) {
console.error("Failed to show notification: ", err);
return new EventTarget();
}
}
let lastErrorID = 0;
export default class App extends Component {
state = {
2021-09-21 06:33:22 -04:00
...State.create(),
connectParams: {
2021-05-31 12:04:02 -04:00
url: null,
pass: null,
username: null,
realname: null,
nick: null,
saslPlain: null,
saslExternal: false,
autoconnect: false,
autojoin: [],
},
bouncerNetworks: new Map(),
connectForm: true,
loading: true,
2021-03-08 10:23:16 -05:00
dialog: null,
2021-07-04 15:41:36 -04:00
dialogData: null,
error: null,
openPanels: {
bufferList: false,
memberList: false,
},
};
config = { ...baseConfig };
2021-01-22 11:36:53 -05:00
clients = new Map();
2020-06-29 03:06:47 -04:00
endOfHistory = new Map();
2020-07-15 12:21:09 -04:00
receipts = new Map();
buffer = createRef();
composer = createRef();
2021-05-25 06:40:33 -04:00
switchToChannel = null;
/**
* Parsed irc:// URL to automatically open. The user will be prompted for
* confirmation for security reasons.
*/
autoOpenURL = null;
constructor(props) {
super(props);
this.handleConnectSubmit = this.handleConnectSubmit.bind(this);
2021-03-08 10:23:16 -05:00
this.handleJoinSubmit = this.handleJoinSubmit.bind(this);
this.handleBufferListClick = this.handleBufferListClick.bind(this);
2021-10-17 13:33:02 -04:00
this.handleBufferListClose = this.handleBufferListClose.bind(this);
this.toggleBufferList = this.toggleBufferList.bind(this);
this.toggleMemberList = this.toggleMemberList.bind(this);
this.handleComposerSubmit = this.handleComposerSubmit.bind(this);
2021-05-31 22:39:35 -04:00
this.handleChannelClick = this.handleChannelClick.bind(this);
2020-06-25 12:45:41 -04:00
this.handleNickClick = this.handleNickClick.bind(this);
2020-06-29 06:36:17 -04:00
this.autocomplete = this.autocomplete.bind(this);
2020-06-29 03:06:47 -04:00
this.handleBufferScrollTop = this.handleBufferScrollTop.bind(this);
2021-07-04 15:41:36 -04:00
this.dismissDialog = this.dismissDialog.bind(this);
2021-03-08 12:15:04 -05:00
this.handleAddNetworkClick = this.handleAddNetworkClick.bind(this);
2021-03-09 13:10:22 -05:00
this.handleNetworkSubmit = this.handleNetworkSubmit.bind(this);
this.handleNetworkRemove = this.handleNetworkRemove.bind(this);
this.handleDismissError = this.handleDismissError.bind(this);
this.handleAuthSubmit = this.handleAuthSubmit.bind(this);
2020-07-15 12:21:09 -04:00
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
this.receipts = store.receipts.load();
this.bufferStore = new store.Buffer();
2020-07-01 06:25:57 -04:00
configPromise.then((config) => {
this.handleConfig(config);
return config;
});
}
/**
* Handle configuration data and populate the connection parameters.
*
* The priority order is:
*
* - URL params
* - Saved parameters in local storage
* - Configuration data (fetched from the config.json file)
* - Default server URL constructed from the current URL location (this is
* done in fillConnectParams)
*/
handleConfig(config) {
this.setState({ loading: false });
let connectParams = { ...this.state.connectParams };
if (config.server) {
2021-10-09 03:57:54 -04:00
if (typeof config.server.url === "string") {
connectParams.url = config.server.url;
}
if (Array.isArray(config.server.autojoin)) {
connectParams.autojoin = config.server.autojoin;
2021-10-09 03:57:54 -04:00
} else if (typeof config.server.autojoin === "string") {
connectParams.autojoin = [config.server.autojoin];
}
2021-10-09 04:17:52 -04:00
if (typeof config.server.nick === "string") {
connectParams.nick = config.server.nick;
}
2021-10-09 04:14:27 -04:00
if (typeof config.server.autoconnect === "boolean") {
connectParams.autoconnect = config.server.autoconnect;
}
if (config.server.auth === "external") {
connectParams.saslExternal = true;
}
}
2021-06-10 12:11:11 -04:00
let autoconnect = store.autoconnect.load();
if (autoconnect) {
connectParams = {
...connectParams,
...autoconnect,
autoconnect: true,
autojoin: [], // handled by store.Buffer
};
}
2021-11-08 07:01:54 -05:00
let autojoin = [];
2021-06-10 12:11:11 -04:00
let queryParams = parseQueryString();
// Don't allow to silently override the server URL if there's one in
// config.json, because this has security implications. But still allow
// setting server to an empty string to reveal the server field in the
// connect form.
if (typeof queryParams.server === "string" && (!connectParams.url || !queryParams.server)) {
connectParams.url = queryParams.server;
// When using a custom server, some configuration options don't
// make sense anymore.
config.server.auth = null;
}
if (typeof queryParams.nick === "string") {
connectParams.nick = queryParams.nick;
}
if (typeof queryParams.channels === "string") {
2021-11-08 07:01:54 -05:00
autojoin = queryParams.channels.split(",");
2020-07-15 12:21:09 -04:00
}
if (typeof queryParams.open === "string") {
this.autoOpenURL = irc.parseURL(queryParams.open);
}
if (window.location.hash) {
2021-11-08 07:01:54 -05:00
autojoin = window.location.hash.split(",");
}
this.config = config;
2021-11-08 07:01:54 -05:00
if (autojoin.length > 0) {
if (connectParams.autoconnect) {
// Ask the user whether they want to join that new channel.
// TODO: support multiple channels here
this.autoOpenURL = { host: "", entity: autojoin[0] };
} else {
connectParams.autojoin = autojoin;
}
}
this.setState({ connectParams: connectParams });
if (connectParams.autoconnect) {
this.setState({ connectForm: false });
this.connect(connectParams);
}
}
showError(msg) {
this.setState({ error: msg });
lastErrorID++;
return lastErrorID;
}
dismissError(id) {
if (id && id !== lastErrorID) {
return;
}
this.setState({ error: null });
}
handleDismissError(event) {
event.preventDefault();
this.dismissError();
}
setServerState(id, updater, callback) {
2021-01-21 13:01:50 -05:00
this.setState((state) => {
2021-06-04 12:03:03 -04:00
return State.updateServer(state, id, updater);
2021-01-21 13:01:50 -05:00
}, callback);
}
2021-01-21 14:41:44 -05:00
setBufferState(id, updater, callback) {
this.setState((state) => {
2021-06-04 12:03:03 -04:00
return State.updateBuffer(state, id, updater);
}, callback);
}
syncBufferUnread(serverID, name) {
let client = this.clients.get(serverID);
let stored = this.bufferStore.get({ name, server: client.params });
if (client.enabledCaps["draft/chathistory"] && stored) {
this.setBufferState({ server: serverID, name }, { unread: stored.unread });
}
if (!stored) {
this.bufferStore.put({
name,
server: client.params,
unread: Unread.NONE,
});
}
}
createBuffer(serverID, name) {
let client = this.clients.get(serverID);
2021-06-10 12:11:11 -04:00
let id = null;
let isNew = false;
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let updated;
2021-06-04 12:37:34 -04:00
[id, updated] = State.createBuffer(state, name, serverID, client);
isNew = !!updated;
2021-06-04 12:37:34 -04:00
return updated;
});
if (isNew) {
this.syncBufferUnread(serverID, name);
}
return id;
}
2021-01-21 16:15:33 -05:00
switchBuffer(id) {
2021-06-10 12:11:11 -04:00
let buf;
2021-01-21 14:41:44 -05:00
this.setState((state) => {
2021-06-04 12:03:03 -04:00
buf = State.getBuffer(state, id);
2021-01-21 14:41:44 -05:00
if (!buf) {
return;
}
return { activeBuffer: buf.id };
}, () => {
2021-01-21 16:15:33 -05:00
if (!buf) {
return;
}
let prevReadReceipt = this.getReceipt(buf.name, ReceiptType.READ);
2021-01-21 16:15:33 -05:00
// TODO: only mark as read if user scrolled at the bottom
this.setBufferState(buf.id, {
unread: Unread.NONE,
prevReadReceipt,
2021-01-21 16:15:33 -05:00
});
if (this.buffer.current) {
this.buffer.current.focus();
}
2020-07-15 12:21:09 -04:00
if (buf.messages.length > 0) {
let lastMsg = buf.messages[buf.messages.length - 1];
this.setReceipt(buf.name, ReceiptType.READ, lastMsg);
let client = this.clients.get(buf.server);
this.bufferStore.put({
name: buf.name,
server: client.params,
unread: Unread.NONE,
});
2020-07-15 12:21:09 -04:00
}
let server = this.state.servers.get(buf.server);
if (buf.type === BufferType.NICK && !server.users.has(buf.name)) {
this.whoUserBuffer(buf.name, buf.server);
}
2020-07-15 12:21:09 -04:00
});
}
saveReceipts() {
store.receipts.put(this.receipts);
2020-07-15 12:21:09 -04:00
}
getReceipt(target, type) {
2021-06-10 12:11:11 -04:00
let receipts = this.receipts.get(target);
2020-07-15 12:21:09 -04:00
if (!receipts) {
return undefined;
}
return receipts[type];
}
hasReceipt(target, type, msg) {
2021-06-10 12:11:11 -04:00
let receipt = this.getReceipt(target, type);
2020-07-15 12:21:09 -04:00
return receipt && msg.tags.time <= receipt.time;
}
setReceipt(target, type, msg) {
2021-06-10 12:11:11 -04:00
let receipt = this.getReceipt(target, type);
2020-07-15 12:21:09 -04:00
if (this.hasReceipt(target, type, msg)) {
return;
}
// TODO: this doesn't trigger a redraw
2020-07-15 12:21:09 -04:00
this.receipts.set(target, {
...this.receipts.get(target),
[type]: { time: msg.tags.time },
});
2020-07-15 12:21:09 -04:00
this.saveReceipts();
}
latestReceipt(type) {
2021-06-10 12:11:11 -04:00
let last = null;
this.receipts.forEach((receipts, target) => {
if (target === "*") {
return;
}
2021-06-10 12:11:11 -04:00
let delivery = receipts[type];
if (!delivery || !delivery.time) {
return;
}
if (!last || delivery.time > last.time) {
last = delivery;
}
});
return last;
}
addMessage(serverID, bufName, msg) {
2021-06-10 12:11:11 -04:00
let client = this.clients.get(serverID);
2021-01-22 11:36:53 -05:00
2021-06-23 13:52:45 -04:00
// Treat server-wide broadcasts as highlights. They're sent by server
// operators and can contain important information.
msg.isHighlight = irc.isHighlight(msg, client.nick, client.cm) || irc.isServerBroadcast(msg);
if (!msg.tags) {
msg.tags = {};
}
2020-06-29 03:06:47 -04:00
if (!msg.tags.time) {
msg.tags.time = irc.formatDate(new Date());
}
2021-06-10 12:11:11 -04:00
let isDelivered = this.hasReceipt(bufName, ReceiptType.DELIVERED, msg);
let isRead = this.hasReceipt(bufName, ReceiptType.READ, msg);
2020-07-15 12:21:09 -04:00
// TODO: messages coming from infinite scroll shouldn't trigger notifications
2021-06-24 12:04:26 -04:00
if (client.isMyNick(msg.prefix.name)) {
isRead = true;
}
2021-06-10 12:11:11 -04:00
let msgUnread = Unread.NONE;
2020-07-15 12:21:09 -04:00
if ((msg.command == "PRIVMSG" || msg.command == "NOTICE") && !isRead) {
2021-06-10 12:57:57 -04:00
let target = msg.params[0];
2021-06-10 12:11:11 -04:00
let text = msg.params[1];
2020-06-29 05:50:42 -04:00
2021-06-10 12:11:11 -04:00
let kind;
if (msg.isHighlight) {
2020-06-29 05:08:47 -04:00
msgUnread = Unread.HIGHLIGHT;
2020-06-29 05:50:42 -04:00
kind = "highlight";
2021-06-10 12:57:57 -04:00
} else if (client.isMyNick(target)) {
2020-06-29 05:50:42 -04:00
msgUnread = Unread.HIGHLIGHT;
kind = "private message";
2020-06-29 05:08:47 -04:00
} else {
msgUnread = Unread.MESSAGE;
}
2020-06-29 05:50:42 -04:00
if (msgUnread == Unread.HIGHLIGHT && !isDelivered && !irc.parseCTCP(msg)) {
2021-06-10 12:11:11 -04:00
let title = "New " + kind + " from " + msg.prefix.name;
if (client.isChannel(bufName)) {
title += " in " + bufName;
2020-06-29 05:50:42 -04:00
}
2021-06-10 12:11:11 -04:00
let notif = showNotification(title, {
body: stripANSI(text),
2020-06-29 05:50:42 -04:00
requireInteraction: true,
tag: "msg," + msg.prefix.name + "," + bufName,
2020-06-29 05:50:42 -04:00
});
notif.addEventListener("click", () => {
// TODO: scroll to message
this.switchBuffer({ server: serverID, name: bufName });
2020-06-29 05:50:42 -04:00
});
}
2020-06-24 10:56:28 -04:00
}
if (msg.command === "INVITE" && client.isMyNick(msg.params[0])) {
msgUnread = Unread.HIGHLIGHT;
2021-06-10 12:11:11 -04:00
let channel = msg.params[1];
let notif = new Notification("Invitation to " + channel, {
body: msg.prefix.name + " has invited you to " + channel,
requireInteraction: true,
tag: "invite," + msg.prefix.name + "," + channel,
actions: [{
action: "accept",
title: "Accept",
}],
});
notif.addEventListener("click", (event) => {
if (event.action === "accept") {
this.setReceipt(bufName, ReceiptType.READ, msg);
this.open(channel, serverID);
} else {
// TODO: scroll to message
this.switchBuffer({ server: serverID, name: bufName });
}
});
}
2020-06-24 10:56:28 -04:00
// Open a new buffer if the message doesn't come from me or is a
// self-message
if ((!client.isMyNick(msg.prefix.name) || client.isMyNick(bufName)) && (msg.command != "PART" && msg.comand != "QUIT")) {
this.createBuffer(serverID, bufName);
}
2020-07-15 12:21:09 -04:00
this.setReceipt(bufName, ReceiptType.DELIVERED, msg);
2021-06-10 12:11:11 -04:00
let bufID = { server: serverID, name: bufName };
2021-06-04 13:07:14 -04:00
this.setState((state) => State.addMessage(state, msg, bufID));
this.setBufferState(bufID, (buf) => {
2020-07-15 12:21:09 -04:00
// TODO: set unread if scrolled up
2021-06-10 12:11:11 -04:00
let unread = buf.unread;
let prevReadReceipt = buf.prevReadReceipt;
if (this.state.activeBuffer !== buf.id) {
2020-06-24 11:46:43 -04:00
unread = Unread.union(unread, msgUnread);
2020-07-15 12:21:09 -04:00
} else {
this.setReceipt(bufName, ReceiptType.READ, msg);
2020-06-24 10:56:28 -04:00
}
// Don't show unread marker for my own messages
if (client.isMyNick(msg.prefix.name)) {
prevReadReceipt = { time: msg.tags.time };
}
this.bufferStore.put({
name: buf.name,
server: client.params,
unread,
});
return { unread, prevReadReceipt };
});
}
2021-01-22 12:44:06 -05:00
connect(params) {
2021-06-10 12:11:11 -04:00
let serverID = null;
2021-01-21 13:01:50 -05:00
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let update;
2021-06-10 04:54:33 -04:00
[serverID, update] = State.createServer(state);
return update;
2021-01-21 13:01:50 -05:00
});
this.setState({ connectParams: params });
2021-06-10 12:11:11 -04:00
let client = new Client(fillConnectParams(params));
this.clients.set(serverID, client);
2021-06-10 04:54:33 -04:00
this.setServerState(serverID, { status: client.status });
2021-01-22 11:36:53 -05:00
let errorID = null;
2021-01-22 12:29:22 -05:00
client.addEventListener("status", () => {
this.setServerState(serverID, { status: client.status });
if (client.status === Client.Status.REGISTERED) {
this.setState({ connectForm: false });
if (errorID) {
this.dismissError(errorID);
}
}
2021-01-21 13:01:50 -05:00
});
2021-01-22 11:36:53 -05:00
client.addEventListener("message", (event) => {
this.handleMessage(serverID, event.detail.message);
});
2021-01-22 11:36:53 -05:00
client.addEventListener("error", (event) => {
errorID = this.showError(event.detail);
});
this.createBuffer(serverID, SERVER_BUFFER);
if (!this.state.activeBuffer) {
this.switchBuffer({ server: serverID, name: SERVER_BUFFER });
}
2021-05-25 06:40:33 -04:00
if (params.autojoin.length > 0) {
this.switchToChannel = params.autojoin[0];
}
2021-05-27 12:17:41 -04:00
if (this.config.server && typeof this.config.server.ping !== "undefined") {
client.setPingInterval(this.config.server.ping);
2021-05-27 12:17:41 -04:00
}
}
disconnect(serverID) {
if (!serverID) {
2021-06-04 12:03:03 -04:00
serverID = State.getActiveServerID(this.state);
}
2021-06-10 12:11:11 -04:00
let client = this.clients.get(serverID);
2021-01-22 11:36:53 -05:00
if (client) {
this.clients.delete(serverID);
2021-01-22 12:29:22 -05:00
client.disconnect();
2021-01-12 04:35:38 -05:00
}
}
reconnect(serverID) {
if (!serverID) {
2021-06-04 12:03:03 -04:00
serverID = State.getActiveServerID(this.state);
}
2021-06-10 12:11:11 -04:00
let client = this.clients.get(serverID);
2021-01-22 12:44:06 -05:00
if (client) {
client.reconnect();
}
2021-01-12 04:35:38 -05:00
}
serverFromBouncerNetwork(bouncerNetworkID) {
2021-06-10 12:11:11 -04:00
for (let [id, client] of this.clients) {
if (client.params.bouncerNetwork === bouncerNetworkID) {
return id;
}
}
return null;
}
routeMessage(serverID, msg) {
2021-06-10 12:11:11 -04:00
let client = this.clients.get(serverID);
let chatHistoryBatch = irc.findBatchByType(msg, "chathistory");
let target, channel, affectedBuffers;
switch (msg.command) {
case "MODE":
target = msg.params[0];
if (client.isChannel(target)) {
return [target];
}
return [SERVER_BUFFER];
case "NOTICE":
case "PRIVMSG":
target = msg.params[0];
if (client.isMyNick(target)) {
if (client.cm(msg.prefix.name) === client.cm(client.serverPrefix.name)) {
target = SERVER_BUFFER;
} else {
target = msg.prefix.name;
}
}
if (msg.command === "NOTICE" && !State.getBuffer(this.state, { server: serverID, name: target })) {
// Don't open a new buffer if this is just a NOTICE
target = SERVER_BUFFER;
}
let allowedPrefixes = client.isupport.get("STATUSMSG");
if (allowedPrefixes) {
let parts = irc.parseTargetPrefix(target, allowedPrefixes);
if (client.isChannel(parts.name)) {
target = parts.name;
}
}
return [target];
case "JOIN":
channel = msg.params[0];
if (!client.isMyNick(msg.prefix.name)) {
return [channel];
}
return [];
case "PART":
channel = msg.params[0];
return [channel];
case "KICK":
channel = msg.params[0];
return [channel];
case "QUIT":
affectedBuffers = [];
if (chatHistoryBatch) {
affectedBuffers.push(chatHistoryBatch.params[0]);
} else {
this.state.buffers.forEach((buf) => {
if (buf.server != serverID) {
return;
}
if (!buf.members.has(msg.prefix.name) && client.cm(buf.name) !== client.cm(msg.prefix.name)) {
return;
}
affectedBuffers.push(buf.name);
});
}
return affectedBuffers;
case "NICK":
let newNick = msg.params[0];
affectedBuffers = [];
if (chatHistoryBatch) {
affectedBuffers.push(chatHistoryBatch.params[0]);
} else {
this.state.buffers.forEach((buf) => {
if (buf.server != serverID) {
return;
}
if (!buf.members.has(msg.prefix.name)) {
return;
}
affectedBuffers.push(buf.name);
});
if (client.isMyNick(newNick)) {
affectedBuffers.push(SERVER_BUFFER);
}
}
return affectedBuffers;
case "TOPIC":
channel = msg.params[0];
return [channel];
case "INVITE":
channel = msg.params[1];
// TODO: find a more reliable way to do this
let bufName = channel;
if (!State.getBuffer(this.state, { server: serverID, name: channel })) {
bufName = SERVER_BUFFER;
}
return [bufName];
case irc.RPL_CHANNELMODEIS:
case irc.RPL_CREATIONTIME:
case irc.RPL_INVITELIST:
case irc.RPL_ENDOFINVITELIST:
case irc.RPL_EXCEPTLIST:
case irc.RPL_ENDOFEXCEPTLIST:
case irc.RPL_BANLIST:
case irc.RPL_ENDOFBANLIST:
case irc.RPL_QUIETLIST:
case irc.RPL_ENDOFQUIETLIST:
channel = msg.params[1];
return [channel];
case irc.RPL_INVITING:
channel = msg.params[2];
return [channel];
case irc.RPL_YOURHOST:
case irc.RPL_MYINFO:
case irc.RPL_ISUPPORT:
case irc.RPL_ENDOFMOTD:
case irc.ERR_NOMOTD:
2021-11-23 11:58:49 -05:00
case irc.RPL_AWAY:
case irc.RPL_NOTOPIC:
case irc.RPL_TOPIC:
case irc.RPL_TOPICWHOTIME:
case irc.RPL_NAMREPLY:
case irc.RPL_ENDOFNAMES:
case irc.RPL_MONONLINE:
case irc.RPL_MONOFFLINE:
case irc.RPL_SASLSUCCESS:
case "AWAY":
case "SETNAME":
case "CHGHOST":
case "ACCOUNT":
case "CAP":
case "AUTHENTICATE":
case "PING":
case "PONG":
case "BATCH":
case "TAGMSG":
case "CHATHISTORY":
case "ACK":
case "BOUNCER":
// Ignore these
return [];
default:
return [SERVER_BUFFER];
}
}
handleMessage(serverID, msg) {
let client = this.clients.get(serverID);
2021-11-07 07:51:39 -05:00
let destBuffers = this.routeMessage(serverID, msg);
if (irc.findBatchByType(msg, "chathistory")) {
destBuffers.forEach((bufName) => {
this.addMessage(serverID, bufName, msg);
});
return;
}
this.setState((state) => State.handleMessage(state, msg, serverID, client));
let target, channel;
switch (msg.command) {
case irc.RPL_WELCOME:
let lastReceipt = this.latestReceipt(ReceiptType.DELIVERED);
if (lastReceipt && lastReceipt.time && client.enabledCaps["draft/chathistory"] && (!client.enabledCaps["soju.im/bouncer-networks"] || client.params.bouncerNetwork)) {
2021-06-10 12:11:11 -04:00
let now = irc.formatDate(new Date());
client.fetchHistoryTargets(now, lastReceipt.time).then((targets) => {
targets.forEach((target) => {
let from = lastReceipt;
2021-10-29 10:34:50 -04:00
let to = { time: msg.tags.time || now };
this.fetchBacklog(client, target.name, from, to);
});
});
}
break;
case irc.RPL_ENDOFMOTD:
case irc.ERR_NOMOTD:
// These messages are used to indicate the end of the ISUPPORT list
// Restore opened channel and user buffers
let join = [];
for (let buf of this.bufferStore.list(client.params)) {
if (buf.name === "*") {
continue;
}
if (client.isChannel(buf.name)) {
if (client.enabledCaps["soju.im/bouncer-networks"]) {
continue;
}
join.push(buf.name);
} else {
this.createBuffer(serverID, buf.name);
this.whoUserBuffer(buf.name, serverID);
}
}
// Auto-join channels given at connect-time
let server = this.state.servers.get(serverID);
let bouncerNetID = server.isupport.get("BOUNCER_NETID");
let bouncerNetwork = null;
if (bouncerNetID) {
bouncerNetwork = this.state.bouncerNetworks.get(bouncerNetID);
}
if (!bouncerNetwork || bouncerNetwork.state === "connected") {
join = join.concat(client.params.autojoin);
client.params.autojoin = [];
}
if (join.length > 0) {
client.send({
command: "JOIN",
params: [join.join(",")],
});
}
let serverHost = bouncerNetwork ? bouncerNetwork.host : "";
if (this.autoOpenURL && serverHost === this.autoOpenURL.host) {
this.openURL(this.autoOpenURL);
this.autoOpenURL = null;
}
case "JOIN":
2021-06-10 12:11:11 -04:00
channel = msg.params[0];
if (client.isMyNick(msg.prefix.name)) {
this.syncBufferUnread(serverID, channel);
}
2021-05-25 06:40:33 -04:00
if (channel == this.switchToChannel) {
this.switchBuffer({ server: serverID, name: channel });
2021-05-25 06:40:33 -04:00
this.switchToChannel = null;
}
break;
case "PART":
2021-06-10 12:11:11 -04:00
channel = msg.params[0];
if (client.isMyNick(msg.prefix.name)) {
this.receipts.delete(channel);
this.saveReceipts();
}
break;
case "BOUNCER":
if (msg.params[0] !== "NETWORK") {
break; // We're only interested in network updates
}
if (client.isupport.has("BOUNCER_NETID")) {
// This can happen if the user has specified a network to bind
// to via other means, e.g. "<username>/<network>".
break;
}
2021-06-10 12:11:11 -04:00
let id = msg.params[1];
let attrs = null;
if (msg.params[2] !== "*") {
attrs = irc.parseTags(msg.params[2]);
}
2021-06-10 12:11:11 -04:00
let isNew = false;
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let bouncerNetworks = new Map(state.bouncerNetworks);
if (!attrs) {
bouncerNetworks.delete(id);
} else {
2021-06-10 12:11:11 -04:00
let prev = bouncerNetworks.get(id);
isNew = prev === undefined;
attrs = { ...prev, ...attrs };
bouncerNetworks.set(id, attrs);
}
return { bouncerNetworks };
}, () => {
if (!attrs) {
2021-06-10 12:11:11 -04:00
let serverID = this.serverFromBouncerNetwork(id);
if (serverID) {
this.close({ server: serverID, name: SERVER_BUFFER });
}
} else if (isNew) {
this.connect({
...client.params,
bouncerNetwork: id,
});
}
if (attrs && attrs.state === "connected") {
let serverID = this.serverFromBouncerNetwork(id);
let client = this.clients.get(serverID);
if (client && client.status === Client.Status.REGISTERED && client.params.autojoin && client.params.autojoin.length > 0) {
client.send({
command: "JOIN",
params: [client.params.autojoin.join(",")],
});
client.params.autojoin = [];
}
}
});
break;
case "BATCH":
if (!msg.params[0].startsWith("-")) {
break;
}
let name = msg.params[0].slice(1);
let batch = client.batches.get(name);
if (!batch || batch.type !== "soju.im/bouncer-networks") {
break;
}
// We've received a BOUNCER NETWORK batch. If we have a URL to
// auto-open and no existing network matches it, ask the user to
// create a new network.
if (this.autoOpenURL && this.autoOpenURL.host && !this.findBouncerNetIDByHost(this.autoOpenURL.host)) {
this.openURL(this.autoOpenURL);
this.autoOpenURL = null;
}
break;
default:
if (irc.isError(msg.command) && msg.command != irc.ERR_NOMOTD) {
2021-06-10 12:11:11 -04:00
let description = msg.params[msg.params.length - 1];
this.showError(description);
}
}
destBuffers.forEach((bufName) => {
this.addMessage(serverID, bufName, msg);
});
}
handleConnectSubmit(connectParams) {
this.dismissError();
if (connectParams.autoconnect) {
store.autoconnect.put(connectParams);
} else {
store.autoconnect.put(null);
}
// Disconnect previous server, if any
let activeBuffer = this.state.buffers.get(this.state.activeBuffer);
if (activeBuffer) {
this.close(activeBuffer.server);
}
2021-01-22 12:44:06 -05:00
this.connect(connectParams);
}
handleChannelClick(event) {
let handled = this.openURL(event.target.href);
if (handled) {
event.preventDefault();
}
}
findBouncerNetIDByHost(host) {
for (let [id, bouncerNetwork] of this.state.bouncerNetworks) {
if (bouncerNetwork.host === host) {
return id;
}
}
return null;
}
openURL(url) {
if (typeof url === "string") {
url = irc.parseURL(url);
}
if (!url) {
return false;
}
2021-11-16 05:52:38 -05:00
let { host, port } = splitHostPort(url.host);
let serverID;
if (!url.host) {
serverID = State.getActiveServerID(this.state);
} else {
2021-11-16 05:52:38 -05:00
let bouncerNetID = this.findBouncerNetIDByHost(host);
if (!bouncerNetID) {
// Open dialog to create network if bouncer
let client = this.clients.values().next().value;
if (!client || !client.enabledCaps["soju.im/bouncer-networks"]) {
return false;
}
2021-11-16 05:52:38 -05:00
let params = { host };
if (typeof port === "number") {
params.port = port;
}
this.openDialog("network", { params, autojoin: url.entity });
return true;
}
for (let [id, server] of this.state.servers) {
if (server.isupport.get("BOUNCER_NETID") === bouncerNetID) {
serverID = id;
break;
}
}
}
if (!serverID) {
return false;
}
let buf = State.getBuffer(this.state, { server: serverID, name: url.entity || SERVER_BUFFER });
2021-05-31 22:39:35 -04:00
if (buf) {
this.switchBuffer(buf.id);
} else {
this.openDialog("join", { server: serverID, channel: url.entity });
2021-05-31 22:39:35 -04:00
}
return true;
2021-05-31 22:39:35 -04:00
}
2020-06-25 12:45:41 -04:00
handleNickClick(nick) {
this.open(nick);
}
fetchBacklog(client, target, after, before) {
client.fetchHistoryBetween(target, after, before, CHATHISTORY_MAX_SIZE).catch((err) => {
console.error("Failed to fetch backlog for '" + target + "': ", err);
this.showError("Failed to fetch backlog for '" + target + "'");
this.receipts.delete(target);
this.saveReceipts();
});
}
whoUserBuffer(target, serverID) {
let client = this.clients.get(serverID);
client.who(target, {
fields: ["flags", "hostname", "nick", "realname", "username", "account"],
});
client.monitor(target);
}
open(target, serverID) {
if (!serverID) {
2021-06-04 12:03:03 -04:00
serverID = State.getActiveServerID(this.state);
}
2021-06-10 12:11:11 -04:00
let client = this.clients.get(serverID);
if (client.isServer(target)) {
this.switchBuffer({ server: serverID });
} else if (client.isChannel(target)) {
this.switchToChannel = target;
2021-01-22 11:36:53 -05:00
client.send({ command: "JOIN", params: [target] });
} else {
this.whoUserBuffer(target, serverID);
this.createBuffer(serverID, target);
this.switchBuffer({ server: serverID, name: target });
2020-06-25 12:45:41 -04:00
}
}
2021-01-22 04:26:53 -05:00
close(id) {
2021-06-10 12:11:11 -04:00
let buf = State.getBuffer(this.state, id);
2021-01-22 04:26:53 -05:00
if (!buf) {
return;
}
2021-06-10 12:11:11 -04:00
let client = this.clients.get(buf.server);
2021-01-22 04:26:53 -05:00
switch (buf.type) {
case BufferType.SERVER:
2021-03-10 04:59:39 -05:00
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let buffers = new Map(state.buffers);
for (let [id, b] of state.buffers) {
if (b.server === buf.server) {
2021-03-10 04:59:39 -05:00
buffers.delete(id);
}
}
2021-06-10 12:11:11 -04:00
let activeBuffer = state.activeBuffer;
if (activeBuffer && state.buffers.get(activeBuffer).server === buf.server) {
2021-03-10 04:59:39 -05:00
if (buffers.size > 0) {
activeBuffer = buffers.keys().next().value;
} else {
activeBuffer = null;
}
}
return { buffers, activeBuffer };
2021-01-11 12:12:28 -05:00
});
2021-03-10 04:59:39 -05:00
2021-06-10 12:11:11 -04:00
let disconnectAll = client && !client.params.bouncerNetwork && client.enabledCaps["soju.im/bouncer-networks"];
2021-03-10 04:59:39 -05:00
this.disconnect(buf.server);
2021-03-10 04:59:39 -05:00
2021-01-22 04:41:28 -05:00
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let servers = new Map(state.servers);
servers.delete(buf.server);
2021-06-10 12:11:11 -04:00
let connectForm = state.connectForm;
if (servers.size == 0) {
connectForm = true;
}
return { servers, connectForm };
2021-01-22 04:41:28 -05:00
});
2021-03-10 04:59:39 -05:00
if (disconnectAll) {
2021-06-10 12:11:11 -04:00
for (let serverID of this.clients.keys()) {
this.close({ server: serverID, name: SERVER_BUFFER });
2021-03-10 04:59:39 -05:00
}
this.bufferStore.clear();
} else {
this.bufferStore.clear(client.params);
2021-03-10 04:59:39 -05:00
}
// TODO: only clear autoconnect if this server is stored there
if (buf.server == 1) {
store.autoconnect.put(null);
}
2021-01-22 04:26:53 -05:00
break;
case BufferType.CHANNEL:
if (buf.joined) {
client.send({ command: "PART", params: [buf.name] });
}
2021-01-22 04:26:53 -05:00
// fallthrough
case BufferType.NICK:
this.switchBuffer({ name: SERVER_BUFFER });
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let buffers = new Map(state.buffers);
2021-03-03 03:59:40 -05:00
buffers.delete(buf.id);
2021-01-22 04:26:53 -05:00
return { buffers };
});
2020-07-15 12:21:09 -04:00
2021-08-24 06:53:46 -04:00
client.unmonitor(buf.name);
2021-01-22 11:36:53 -05:00
this.receipts.delete(buf.name);
2021-01-22 04:26:53 -05:00
this.saveReceipts();
this.bufferStore.delete({ name: buf.name, server: client.params });
2021-01-22 04:26:53 -05:00
break;
}
}
executeCommand(s) {
2021-06-10 12:11:11 -04:00
let parts = s.split(" ");
let name = parts[0].toLowerCase().slice(1);
let args = parts.slice(1);
2020-07-13 11:22:24 -04:00
2021-06-10 12:11:11 -04:00
let cmd = commands[name];
2020-07-13 11:22:24 -04:00
if (!cmd) {
this.showError(`Unknown command "${name}" (run "/help" to get a command list)`);
2020-07-13 11:22:24 -04:00
return;
}
try {
2021-03-08 11:25:00 -05:00
cmd.execute(this, args);
} catch (error) {
console.error(`Failed to execute command "${name}":`, error);
this.showError(error.message);
}
}
2020-06-28 03:29:39 -04:00
privmsg(target, text) {
if (target == SERVER_BUFFER) {
this.showError("Cannot send message in server buffer");
2020-06-28 03:29:39 -04:00
return;
}
2021-06-10 12:11:11 -04:00
let serverID = State.getActiveServerID(this.state);
let client = this.clients.get(serverID);
2021-01-22 11:36:53 -05:00
2021-06-10 12:11:11 -04:00
let msg = { command: "PRIVMSG", params: [target, text] };
2021-01-22 11:36:53 -05:00
client.send(msg);
2020-06-28 03:29:39 -04:00
2021-01-22 11:36:53 -05:00
if (!client.enabledCaps["echo-message"]) {
msg.prefix = { name: client.nick };
this.addMessage(serverID, target, msg);
2020-06-28 03:29:39 -04:00
}
}
handleComposerSubmit(text) {
if (!text) {
return;
}
if (text.startsWith("//")) {
text = text.slice(1);
} else if (text.startsWith("/")) {
this.executeCommand(text);
return;
}
2021-06-10 12:11:11 -04:00
let buf = this.state.buffers.get(this.state.activeBuffer);
2021-01-21 14:41:44 -05:00
if (!buf) {
return;
}
2021-01-21 14:41:44 -05:00
this.privmsg(buf.name, text);
}
handleBufferListClick(id) {
this.switchBuffer(id);
this.closeBufferList();
}
2021-10-17 13:33:02 -04:00
handleBufferListClose(id) {
this.close(id);
this.closeBufferList();
}
toggleBufferList() {
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let openPanels = {
...state.openPanels,
bufferList: !state.openPanels.bufferList,
};
return { openPanels };
});
}
toggleMemberList() {
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let openPanels = {
...state.openPanels,
memberList: !state.openPanels.memberList,
};
return { openPanels };
});
}
closeBufferList() {
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let openPanels = {
...state.openPanels,
bufferList: false,
};
return { openPanels };
});
}
closeMemberList() {
this.setState((state) => {
2021-06-10 12:11:11 -04:00
let openPanels = {
...state.openPanels,
memberList: false,
};
return { openPanels };
});
}
handleJoinClick(buf) {
switch (buf.type) {
case BufferType.SERVER:
this.openDialog("join", { server: buf.server });
break;
case BufferType.CHANNEL:
let client = this.clients.get(buf.server);
client.send({ command: "JOIN", params: [buf.name] });
break;
}
2021-03-08 10:23:16 -05:00
}
2021-01-22 11:36:53 -05:00
2021-03-08 10:23:16 -05:00
handleJoinSubmit(data) {
this.open(data.channel, this.state.dialogData.server);
2021-07-04 15:41:36 -04:00
this.dismissDialog();
2020-06-29 04:12:46 -04:00
}
2020-06-29 06:36:17 -04:00
autocomplete(prefix) {
2020-07-13 11:28:49 -04:00
function fromList(l, prefix) {
prefix = prefix.toLowerCase();
let repl = [];
2021-06-10 12:11:11 -04:00
for (let item of l) {
2020-07-13 11:28:49 -04:00
if (item.toLowerCase().startsWith(prefix)) {
repl.push(item);
2020-07-13 11:28:49 -04:00
}
}
return repl;
2020-06-29 06:36:17 -04:00
}
2020-07-13 11:28:49 -04:00
if (prefix.startsWith("/")) {
2021-06-10 12:11:11 -04:00
let repl = fromList(Object.keys(commands), prefix.slice(1));
return repl.map(cmd => "/" + cmd);
2020-07-13 11:28:49 -04:00
}
// TODO: consider using the CHANTYPES ISUPPORT token here
if (prefix.startsWith("#")) {
let chanNames = [];
for (const buf of this.state.buffers.values()) {
if (buf.name.startsWith("#")) {
chanNames.push(buf.name);
}
}
return fromList(chanNames, prefix);
}
2021-06-10 12:11:11 -04:00
let buf = this.state.buffers.get(this.state.activeBuffer);
2021-01-21 14:41:44 -05:00
if (!buf || !buf.members) {
return [];
2020-06-29 06:36:17 -04:00
}
2020-07-13 11:28:49 -04:00
return fromList(buf.members.keys(), prefix);
2020-06-29 06:36:17 -04:00
}
openHelp() {
2021-07-04 15:41:36 -04:00
this.openDialog("help");
}
2020-06-29 03:06:47 -04:00
handleBufferScrollTop() {
2021-06-10 12:11:11 -04:00
let buf = this.state.buffers.get(this.state.activeBuffer);
2021-01-21 14:41:44 -05:00
if (!buf || buf.type == BufferType.SERVER) {
2020-06-29 03:06:47 -04:00
return;
}
2021-01-22 11:36:53 -05:00
2021-06-10 12:11:11 -04:00
let client = this.clients.get(buf.server);
2021-01-22 11:36:53 -05:00
if (!client || !client.enabledCaps["draft/chathistory"] || !client.enabledCaps["server-time"]) {
2020-06-29 03:06:47 -04:00
return;
}
2021-01-22 09:49:22 -05:00
if (this.endOfHistory.get(buf.id)) {
2020-06-29 03:06:47 -04:00
return;
}
2021-06-10 12:11:11 -04:00
let before;
2020-06-29 03:06:47 -04:00
if (buf.messages.length > 0) {
before = buf.messages[0].tags["time"];
} else {
before = irc.formatDate(new Date());
}
2020-07-15 12:21:09 -04:00
// Avoids sending multiple CHATHISTORY commands in parallel
2021-01-22 09:49:22 -05:00
this.endOfHistory.set(buf.id, true);
2020-07-15 12:21:09 -04:00
let limit = 100;
if (client.enabledCaps["draft/event-playback"]) {
limit = 200;
}
client.fetchHistoryBefore(buf.name, before, limit).then((result) => {
2021-01-23 06:16:57 -05:00
this.endOfHistory.set(buf.id, !result.more);
2020-07-15 12:21:09 -04:00
});
2020-06-29 03:06:47 -04:00
}
2021-07-04 15:41:36 -04:00
openDialog(name, data) {
this.setState({ dialog: name, dialogData: data });
}
dismissDialog() {
this.setState({ dialog: null, dialogData: null });
2021-03-08 10:23:16 -05:00
}
handleAuthClick(serverID) {
let client = this.clients.get(serverID);
this.openDialog("auth", { username: client.nick });
}
handleAuthSubmit(username, password) {
let serverID = State.getActiveServerID(this.state);
let client = this.clients.get(serverID);
client.authenticate("PLAIN", { username, password }).then(() => {
let firstClient = this.clients.values().next().value;
if (client !== firstClient) {
return;
}
let autoconnect = store.autoconnect.load();
if (!autoconnect) {
return;
}
console.log("Saving SASL PLAIN credentials");
autoconnect = {
...autoconnect,
saslPlain: { username, password },
};
store.autoconnect.put(autoconnect);
});
this.dismissDialog();
}
2021-03-08 12:15:04 -05:00
handleAddNetworkClick() {
2021-07-04 15:41:36 -04:00
this.openDialog("network");
2021-03-08 12:15:04 -05:00
}
handleManageNetworkClick(serverID) {
2021-06-10 12:11:11 -04:00
let server = this.state.servers.get(serverID);
let bouncerNetID = server.isupport.get("BOUNCER_NETID");
let bouncerNetwork = this.state.bouncerNetworks.get(bouncerNetID);
2021-07-04 15:41:36 -04:00
this.openDialog("network", {
id: bouncerNetID,
params: bouncerNetwork,
2021-03-09 13:10:22 -05:00
});
}
handleNetworkSubmit(attrs, autojoin) {
2021-06-10 12:11:11 -04:00
let client = this.clients.values().next().value;
2021-03-09 13:10:22 -05:00
2021-07-04 15:41:36 -04:00
if (this.state.dialogData && this.state.dialogData.id) {
2021-03-09 13:10:22 -05:00
if (Object.keys(attrs).length == 0) {
2021-07-04 15:41:36 -04:00
this.dismissDialog();
2021-03-09 13:10:22 -05:00
return;
}
client.send({
command: "BOUNCER",
2021-07-04 15:41:36 -04:00
params: ["CHANGENETWORK", this.state.dialogData.id, irc.formatTags(attrs)],
2021-03-09 13:10:22 -05:00
});
} else {
attrs = { ...attrs, tls: "1" };
client.createBouncerNetwork(attrs).then((id) => {
if (!autojoin) {
return;
}
// By this point, bouncer-networks-notify should've advertised
// the new network
let serverID = this.serverFromBouncerNetwork(id);
let client = this.clients.get(serverID);
client.params.autojoin = [autojoin];
this.switchToChannel = autojoin;
2021-03-09 13:10:22 -05:00
});
}
2021-07-04 15:41:36 -04:00
this.dismissDialog();
2021-03-09 13:10:22 -05:00
}
handleNetworkRemove() {
2021-06-10 12:11:11 -04:00
let client = this.clients.values().next().value;
2021-03-09 13:10:22 -05:00
2021-03-08 12:15:04 -05:00
client.send({
command: "BOUNCER",
2021-07-04 15:41:36 -04:00
params: ["DELNETWORK", this.state.dialogData.id],
2021-03-08 12:15:04 -05:00
});
2021-03-09 13:10:22 -05:00
2021-07-04 15:41:36 -04:00
this.dismissDialog();
2021-03-08 12:15:04 -05:00
}
componentDidMount() {
2020-07-23 03:58:05 -04:00
setupKeybindings(this);
}
render() {
if (this.state.loading) {
return html`<section id="connect"></section>`;
}
2021-06-10 12:11:11 -04:00
let activeBuffer = null, activeServer = null, activeBouncerNetwork = null;
let isBouncer = false;
if (this.state.buffers.get(this.state.activeBuffer)) {
activeBuffer = this.state.buffers.get(this.state.activeBuffer);
activeServer = this.state.servers.get(activeBuffer.server);
2021-03-08 12:15:04 -05:00
2021-06-10 12:11:11 -04:00
let activeClient = this.clients.get(activeBuffer.server);
2021-03-08 12:15:04 -05:00
isBouncer = activeClient && activeClient.enabledCaps["soju.im/bouncer-networks"];
2021-03-10 05:48:58 -05:00
2021-06-10 12:11:11 -04:00
let bouncerNetID = activeServer.isupport.get("BOUNCER_NETID");
2021-03-10 05:48:58 -05:00
if (bouncerNetID) {
activeBouncerNetwork = this.state.bouncerNetworks.get(bouncerNetID);
}
}
if (this.state.connectForm) {
2021-06-10 12:11:11 -04:00
let status = activeServer ? activeServer.status : ServerStatus.DISCONNECTED;
let connecting = status === ServerStatus.CONNECTING || status === ServerStatus.REGISTERING;
2021-01-21 13:01:50 -05:00
return html`
<section id="connect">
<${ConnectForm}
error=${this.state.error}
params=${this.state.connectParams}
auth=${this.config.server.auth}
connecting=${connecting}
onSubmit=${this.handleConnectSubmit}
/>
2021-01-21 13:01:50 -05:00
</section>
`;
}
2021-06-10 12:11:11 -04:00
let bufferHeader = null;
2020-06-26 06:08:14 -04:00
if (activeBuffer) {
let activeUser = null;
if (activeBuffer.type == BufferType.NICK) {
activeUser = activeServer.users.get(activeBuffer.name);
}
2020-06-26 09:16:07 -04:00
bufferHeader = html`
<section id="buffer-header">
<${BufferHeader}
buffer=${activeBuffer}
server=${activeServer}
user=${activeUser}
2021-03-08 12:15:04 -05:00
isBouncer=${isBouncer}
2021-03-10 05:48:58 -05:00
bouncerNetwork=${activeBouncerNetwork}
2021-05-31 22:39:35 -04:00
onChannelClick=${this.handleChannelClick}
onClose=${() => this.close(activeBuffer)}
onJoin=${() => this.handleJoinClick(activeBuffer)}
2021-03-08 12:15:04 -05:00
onAddNetwork=${this.handleAddNetworkClick}
onManageNetwork=${() => this.handleManageNetworkClick(activeBuffer.server)}
/>
</section>
`;
}
2021-06-10 12:11:11 -04:00
let memberList = null;
2020-06-26 08:32:56 -04:00
if (activeBuffer && activeBuffer.type == BufferType.CHANNEL) {
memberList = html`
<section
id="member-list"
class=${this.state.openPanels.memberList ? "expand" : ""}
>
<button
class="expander"
onClick=${this.toggleMemberList}
>
<span></span>
<span></span>
</button>
<section>
<section id="member-list-header">
${activeBuffer.members.size} users
</section>
<${MemberList}
members=${activeBuffer.members}
users=${activeServer.users}
onNickClick=${this.handleNickClick}
/>
</section>
2020-06-26 08:32:56 -04:00
</section>
`;
}
2021-06-10 12:11:11 -04:00
let dialog = null;
let dialogData = this.state.dialogData || {};
2021-03-08 10:23:16 -05:00
switch (this.state.dialog) {
2021-03-09 13:10:22 -05:00
case "network":
let isNew = !dialogData.id;
let title = isNew ? "Add network" : "Edit network";
2021-03-08 12:15:04 -05:00
dialog = html`
2021-07-04 15:41:36 -04:00
<${Dialog} title=${title} onDismiss=${this.dismissDialog}>
2021-03-09 13:10:22 -05:00
<${NetworkForm}
onSubmit=${this.handleNetworkSubmit}
onRemove=${this.handleNetworkRemove}
params=${dialogData.params}
autojoin=${dialogData.autojoin}
isNew=${isNew}
2021-03-09 13:10:22 -05:00
/>
2021-03-08 12:15:04 -05:00
</>
`;
break;
case "help":
dialog = html`
2021-07-04 15:41:36 -04:00
<${Dialog} title="Help" onDismiss=${this.dismissDialog}>
<${Help}/>
</>
`;
break;
2021-03-08 10:23:16 -05:00
case "join":
dialog = html`
2021-07-04 15:41:36 -04:00
<${Dialog} title="Join channel" onDismiss=${this.dismissDialog}>
<${JoinForm} channel=${dialogData.channel} onSubmit=${this.handleJoinSubmit}/>
2021-03-08 10:23:16 -05:00
</>
`;
break;
case "auth":
dialog = html`
<${Dialog} title="Login to ${getServerName(activeServer, activeBouncerNetwork, isBouncer)}" onDismiss=${this.dismissDialog}>
<${AuthForm} username=${dialogData.username} onSubmit=${this.handleAuthSubmit}/>
</>
`;
break;
2021-03-08 10:23:16 -05:00
}
2021-06-10 12:11:11 -04:00
let error = null;
2021-03-08 09:05:43 -05:00
if (this.state.error) {
error = html`
<div id="error-msg">
${this.state.error}
${" "}
<button onClick=${this.handleDismissError}>×</button>
</div>
2021-03-08 09:05:43 -05:00
`;
}
2021-06-10 12:11:11 -04:00
let composerReadOnly = false;
if (activeBuffer && activeBuffer.type === BufferType.SERVER) {
composerReadOnly = true;
}
if (activeServer && activeServer.status !== ServerStatus.REGISTERED) {
composerReadOnly = true;
}
return html`
<section
id="buffer-list"
class=${this.state.openPanels.bufferList ? "expand" : ""}
>
<${BufferList}
buffers=${this.state.buffers}
servers=${this.state.servers}
bouncerNetworks=${this.state.bouncerNetworks}
2021-03-08 12:15:04 -05:00
isBouncer=${isBouncer}
activeBuffer=${this.state.activeBuffer}
onBufferClick=${this.handleBufferListClick}
2021-10-17 13:33:02 -04:00
onBufferClose=${this.handleBufferListClose}
/>
<button
class="expander"
onClick=${this.toggleBufferList}
>
<span></span>
<span></span>
</button>
</section>
2020-06-26 09:16:07 -04:00
${bufferHeader}
<${ScrollManager}
target=${this.buffer}
stickTo=".logline"
scrollKey=${this.state.activeBuffer}
onScrollTop=${this.handleBufferScrollTop}
>
<section id="buffer" ref=${this.buffer} tabindex="-1">
2021-05-31 22:39:35 -04:00
<${Buffer}
buffer=${activeBuffer}
server=${activeServer}
2021-11-07 11:53:10 -05:00
isBouncer=${isBouncer}
2021-05-31 22:39:35 -04:00
onChannelClick=${this.handleChannelClick}
onNickClick=${this.handleNickClick}
onAuthClick=${() => this.handleAuthClick(activeBuffer.server)}
/>
</section>
</>
2020-06-26 08:32:56 -04:00
${memberList}
<${Composer}
ref=${this.composer}
readOnly=${composerReadOnly}
onSubmit=${this.handleComposerSubmit}
autocomplete=${this.autocomplete}
/>
2021-03-08 10:23:16 -05:00
${dialog}
2021-03-08 09:05:43 -05:00
${error}
`;
}
}