2020-06-18 08:23:08 -04:00
|
|
|
|
import * as irc from "/lib/irc.js";
|
|
|
|
|
import Client from "/lib/client.js";
|
|
|
|
|
import Buffer from "/components/buffer.js";
|
|
|
|
|
import BufferList from "/components/buffer-list.js";
|
2020-06-25 12:28:54 -04:00
|
|
|
|
import BufferHeader from "/components/buffer-header.js";
|
2020-06-26 08:32:56 -04:00
|
|
|
|
import MemberList from "/components/member-list.js";
|
2020-06-18 08:23:08 -04:00
|
|
|
|
import Connect from "/components/connect.js";
|
|
|
|
|
import Composer from "/components/composer.js";
|
2020-06-25 06:03:05 -04:00
|
|
|
|
import ScrollManager from "/components/scroll-manager.js";
|
2020-06-18 08:23:08 -04:00
|
|
|
|
import { html, Component, createRef } from "/lib/index.js";
|
2020-08-13 09:38:12 -04:00
|
|
|
|
import { strip as stripANSI } from "/lib/ansi.js";
|
2021-01-22 12:29:22 -05:00
|
|
|
|
import { SERVER_BUFFER, BufferType, ReceiptType, NetworkStatus, Unread } from "/state.js";
|
2020-07-13 11:22:24 -04:00
|
|
|
|
import commands from "/commands.js";
|
2020-07-23 03:58:05 -04:00
|
|
|
|
import { setup as setupKeybindings } from "/keybindings.js";
|
2020-06-26 04:35:38 -04:00
|
|
|
|
|
2020-07-15 12:21:09 -04:00
|
|
|
|
const CHATHISTORY_MAX_SIZE = 4000;
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
const DEFAULT_NETWORK = "network"; // TODO: remove this global
|
|
|
|
|
|
2020-06-28 09:13:06 -04:00
|
|
|
|
var messagesCount = 0;
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
function parseQueryString() {
|
|
|
|
|
var query = window.location.search.substring(1);
|
|
|
|
|
var params = {};
|
|
|
|
|
query.split('&').forEach((s) => {
|
|
|
|
|
if (!s) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var pair = s.split('=');
|
|
|
|
|
params[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || "");
|
|
|
|
|
});
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 03:06:47 -04:00
|
|
|
|
/* Insert a message in an immutable list of sorted messages. */
|
|
|
|
|
function insertMessage(list, msg) {
|
|
|
|
|
if (list.length == 0) {
|
|
|
|
|
return [msg];
|
|
|
|
|
} else if (list[list.length - 1].tags.time <= msg.tags.time) {
|
|
|
|
|
return list.concat(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var insertBefore = -1;
|
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
|
var other = list[i];
|
|
|
|
|
if (msg.tags.time < other.tags.time) {
|
|
|
|
|
insertBefore = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.assert(insertBefore >= 0, "");
|
|
|
|
|
|
|
|
|
|
list = [ ...list ];
|
|
|
|
|
list.splice(insertBefore, 0, msg);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 12:21:09 -04:00
|
|
|
|
function debounce(f, delay) {
|
|
|
|
|
var timeout = null;
|
|
|
|
|
return (...args) => {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
|
timeout = null;
|
|
|
|
|
f(...args);
|
|
|
|
|
}, delay);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 09:43:20 -04:00
|
|
|
|
function compareBuffers(a, b) {
|
2021-01-22 04:37:58 -05:00
|
|
|
|
if (a.network < b.network) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (a.network > b.network) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 09:43:20 -04:00
|
|
|
|
if (a.type == BufferType.SERVER) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (b.type == BufferType.SERVER) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 07:47:27 -04:00
|
|
|
|
if (a.name < b.name) {
|
2020-08-03 09:43:20 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-08-04 07:47:27 -04:00
|
|
|
|
if (a.name > b.name) {
|
2020-08-03 09:43:20 -04:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
function updateState(state, updater) {
|
|
|
|
|
var updated;
|
|
|
|
|
if (typeof updater === "function") {
|
|
|
|
|
updated = updater(state, state);
|
|
|
|
|
} else {
|
|
|
|
|
updated = updater;
|
|
|
|
|
}
|
|
|
|
|
if (state === updated || !updated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return { ...state, ...updated };
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 04:38:07 -05:00
|
|
|
|
function getActiveNetworkID(state) {
|
|
|
|
|
var buf = state.buffers.get(state.activeBuffer);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return buf.network;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 16:02:53 -05:00
|
|
|
|
function getBuffer(state, id) {
|
|
|
|
|
switch (typeof id) {
|
|
|
|
|
case "number":
|
|
|
|
|
return state.buffers.get(id);
|
|
|
|
|
case "object":
|
2021-01-21 16:15:33 -05:00
|
|
|
|
if (id.id) {
|
|
|
|
|
return state.buffers.get(id.id);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 16:02:53 -05:00
|
|
|
|
var network = id.network, name = id.name;
|
|
|
|
|
if (!network) {
|
2021-01-22 04:38:07 -05:00
|
|
|
|
network = getActiveNetworkID(state);
|
2021-01-21 16:02:53 -05:00
|
|
|
|
}
|
|
|
|
|
for (var buf of state.buffers.values()) {
|
|
|
|
|
if (buf.network === network && buf.name === name) {
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
2021-01-21 14:41:44 -05:00
|
|
|
|
}
|
2021-01-21 16:02:53 -05:00
|
|
|
|
return null;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error("Invalid buffer ID type: " + (typeof id));
|
2021-01-21 14:41:44 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
export default class App extends Component {
|
|
|
|
|
state = {
|
|
|
|
|
connectParams: {
|
|
|
|
|
serverURL: null,
|
|
|
|
|
serverPass: null,
|
|
|
|
|
username: null,
|
|
|
|
|
realname: null,
|
|
|
|
|
nick: null,
|
|
|
|
|
saslPlain: null,
|
2020-06-24 12:14:46 -04:00
|
|
|
|
autoconnect: false,
|
2020-06-18 08:23:08 -04:00
|
|
|
|
autojoin: [],
|
|
|
|
|
},
|
2021-01-21 13:01:50 -05:00
|
|
|
|
networks: new Map(),
|
2020-06-18 08:23:08 -04:00
|
|
|
|
buffers: new Map(),
|
|
|
|
|
activeBuffer: null,
|
2020-08-08 00:08:51 -04:00
|
|
|
|
error: null,
|
2020-06-18 08:23:08 -04:00
|
|
|
|
};
|
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();
|
2020-06-25 06:03:05 -04:00
|
|
|
|
buffer = createRef();
|
2020-06-18 08:23:08 -04:00
|
|
|
|
composer = createRef();
|
2021-01-22 04:49:08 -05:00
|
|
|
|
lastNetworkID = 0;
|
2021-01-21 14:41:44 -05:00
|
|
|
|
lastBufferID = 0;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.handleConnectSubmit = this.handleConnectSubmit.bind(this);
|
|
|
|
|
this.handleBufferListClick = this.handleBufferListClick.bind(this);
|
|
|
|
|
this.handleComposerSubmit = this.handleComposerSubmit.bind(this);
|
2020-06-25 12:45:41 -04:00
|
|
|
|
this.handleNickClick = this.handleNickClick.bind(this);
|
2020-06-29 04:12:46 -04:00
|
|
|
|
this.handleJoinClick = this.handleJoinClick.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);
|
2020-08-08 00:08:51 -04:00
|
|
|
|
this.dismissError = this.dismissError.bind(this);
|
2020-06-24 12:14:46 -04:00
|
|
|
|
|
2020-07-15 12:21:09 -04:00
|
|
|
|
this.saveReceipts = debounce(this.saveReceipts.bind(this), 500);
|
|
|
|
|
|
2020-06-24 12:14:46 -04:00
|
|
|
|
if (window.localStorage && localStorage.getItem("autoconnect")) {
|
|
|
|
|
var connectParams = JSON.parse(localStorage.getItem("autoconnect"));
|
|
|
|
|
this.state.connectParams = {
|
|
|
|
|
...this.state.connectParams,
|
|
|
|
|
...connectParams,
|
|
|
|
|
autoconnect: true,
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
var params = parseQueryString();
|
|
|
|
|
|
2020-07-01 06:25:57 -04:00
|
|
|
|
var host = window.location.host || "localhost:8080";
|
|
|
|
|
var proto = "wss:";
|
|
|
|
|
if (window.location.protocol != "https:") {
|
|
|
|
|
proto = "ws:";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var serverURL;
|
2020-06-24 12:14:46 -04:00
|
|
|
|
if (params.server) {
|
2020-07-01 06:25:57 -04:00
|
|
|
|
if (params.server.startsWith("/")) {
|
|
|
|
|
serverURL = proto + "//" + host + params.server;
|
|
|
|
|
} else {
|
|
|
|
|
serverURL = params.server;
|
2020-06-24 12:14:46 -04:00
|
|
|
|
}
|
2020-07-01 06:25:57 -04:00
|
|
|
|
} else {
|
|
|
|
|
serverURL = proto + "//" + host + "/socket";
|
2020-06-24 12:14:46 -04:00
|
|
|
|
}
|
2020-07-01 06:25:57 -04:00
|
|
|
|
this.state.connectParams.serverURL = serverURL;
|
2020-06-24 12:14:46 -04:00
|
|
|
|
|
|
|
|
|
if (params.channels) {
|
|
|
|
|
this.state.connectParams.autojoin = params.channels.split(",");
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-15 12:21:09 -04:00
|
|
|
|
|
|
|
|
|
if (window.localStorage && localStorage.getItem("receipts")) {
|
|
|
|
|
var obj = JSON.parse(localStorage.getItem("receipts"));
|
|
|
|
|
this.receipts = new Map(Object.entries(obj));
|
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 00:08:51 -04:00
|
|
|
|
dismissError(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
this.setState({ error: null });
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
setNetworkState(id, updater, callback) {
|
|
|
|
|
this.setState((state) => {
|
|
|
|
|
var net = state.networks.get(id);
|
|
|
|
|
if (!net) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updated = updateState(net, updater);
|
2021-01-21 14:41:44 -05:00
|
|
|
|
if (!updated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-21 13:01:50 -05:00
|
|
|
|
|
|
|
|
|
var networks = new Map(state.networks);
|
|
|
|
|
networks.set(id, updated);
|
|
|
|
|
return { networks };
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
setBufferState(id, updater, callback) {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
this.setState((state) => {
|
2021-01-21 16:02:53 -05:00
|
|
|
|
var buf = getBuffer(state, id);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
if (!buf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
var updated = updateState(buf, updater);
|
2021-01-21 14:41:44 -05:00
|
|
|
|
if (!updated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
|
|
var buffers = new Map(state.buffers);
|
2021-01-21 14:41:44 -05:00
|
|
|
|
buffers.set(buf.id, updated);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
return { buffers };
|
|
|
|
|
}, callback);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
createBuffer(netID, name, callback) {
|
|
|
|
|
var id = null;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
this.setState((state) => {
|
2021-01-21 16:02:53 -05:00
|
|
|
|
if (getBuffer(state, { network: netID, name })) {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.lastBufferID++;
|
|
|
|
|
id = this.lastBufferID;
|
|
|
|
|
|
2020-06-26 04:35:38 -04:00
|
|
|
|
var type;
|
|
|
|
|
if (name == SERVER_BUFFER) {
|
|
|
|
|
type = BufferType.SERVER;
|
|
|
|
|
} else if (this.isChannel(name)) {
|
|
|
|
|
type = BufferType.CHANNEL;
|
|
|
|
|
} else {
|
|
|
|
|
type = BufferType.NICK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-03 09:43:20 -04:00
|
|
|
|
var bufferList = Array.from(state.buffers.values());
|
|
|
|
|
bufferList.push({
|
2021-01-21 14:41:44 -05:00
|
|
|
|
id,
|
2020-06-26 04:35:38 -04:00
|
|
|
|
name,
|
|
|
|
|
type,
|
2021-01-21 13:27:04 -05:00
|
|
|
|
network: netID,
|
2020-06-26 06:08:14 -04:00
|
|
|
|
serverInfo: null, // if server
|
2020-06-26 06:00:10 -04:00
|
|
|
|
topic: null, // if channel
|
2020-07-13 06:51:09 -04:00
|
|
|
|
members: new Map(), // if channel
|
2020-06-26 06:00:10 -04:00
|
|
|
|
who: null, // if nick
|
2020-07-13 06:51:09 -04:00
|
|
|
|
offline: false, // if nick
|
2020-06-18 08:23:08 -04:00
|
|
|
|
messages: [],
|
2020-06-24 10:56:28 -04:00
|
|
|
|
unread: Unread.NONE,
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
2020-08-03 09:43:20 -04:00
|
|
|
|
bufferList = bufferList.sort(compareBuffers);
|
2021-01-21 14:41:44 -05:00
|
|
|
|
var buffers = new Map(bufferList.map((buf) => [buf.id, buf]));
|
2020-06-18 08:23:08 -04:00
|
|
|
|
return { buffers };
|
2021-01-21 14:41:44 -05:00
|
|
|
|
}, () => {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback(id);
|
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 16:15:33 -05:00
|
|
|
|
switchBuffer(id) {
|
2021-01-21 14:41:44 -05:00
|
|
|
|
var buf;
|
|
|
|
|
this.setState((state) => {
|
2021-01-21 16:15:33 -05:00
|
|
|
|
buf = 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var lastReadReceipt = this.getReceipt(buf.name, ReceiptType.READ);
|
|
|
|
|
// TODO: only mark as read if user scrolled at the bottom
|
|
|
|
|
this.setBufferState(buf.id, {
|
|
|
|
|
unread: Unread.NONE,
|
|
|
|
|
lastReadReceipt,
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
if (this.composer.current) {
|
|
|
|
|
this.composer.current.focus();
|
|
|
|
|
}
|
2020-07-15 12:21:09 -04:00
|
|
|
|
|
2021-01-21 16:15:33 -05:00
|
|
|
|
if (buf.messages.length == 0) {
|
2020-07-15 12:21:09 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var lastMsg = buf.messages[buf.messages.length - 1];
|
2021-01-21 16:15:33 -05:00
|
|
|
|
this.setReceipt(buf.name, ReceiptType.READ, lastMsg);
|
2020-07-15 12:21:09 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveReceipts() {
|
|
|
|
|
if (window.localStorage) {
|
|
|
|
|
var obj = Object.fromEntries(this.receipts);
|
|
|
|
|
localStorage.setItem("receipts", JSON.stringify(obj));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getReceipt(target, type) {
|
|
|
|
|
var receipts = this.receipts.get(target);
|
|
|
|
|
if (!receipts) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
return receipts[type];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasReceipt(target, type, msg) {
|
|
|
|
|
var receipt = this.getReceipt(target, type);
|
|
|
|
|
return receipt && msg.tags.time <= receipt.time;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setReceipt(target, type, msg) {
|
|
|
|
|
var receipt = this.getReceipt(target, type);
|
|
|
|
|
if (this.hasReceipt(target, type, msg)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.receipts.set(target, {
|
|
|
|
|
...this.receipts.get(target),
|
|
|
|
|
[type]: { time: msg.tags.time },
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
2020-07-15 12:21:09 -04:00
|
|
|
|
this.saveReceipts();
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:27:04 -05:00
|
|
|
|
addMessage(netID, bufName, msg) {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
|
2020-06-28 09:13:06 -04:00
|
|
|
|
msg.key = messagesCount;
|
|
|
|
|
messagesCount++;
|
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
msg.isHighlight = irc.isHighlight(msg, client.nick);
|
2020-08-13 05:58:36 -04:00
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
if (!msg.tags) {
|
|
|
|
|
msg.tags = {};
|
|
|
|
|
}
|
2020-06-29 03:06:47 -04:00
|
|
|
|
if (!msg.tags.time) {
|
|
|
|
|
msg.tags.time = irc.formatDate(new Date());
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 12:21:09 -04:00
|
|
|
|
var isDelivered = this.hasReceipt(bufName, ReceiptType.DELIVERED, msg);
|
|
|
|
|
var isRead = this.hasReceipt(bufName, ReceiptType.READ, msg);
|
|
|
|
|
// TODO: messages coming from infinite scroll shouldn't trigger notifications
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
2020-06-24 11:46:43 -04:00
|
|
|
|
var msgUnread = Unread.NONE;
|
2020-07-15 12:21:09 -04:00
|
|
|
|
if ((msg.command == "PRIVMSG" || msg.command == "NOTICE") && !isRead) {
|
2020-06-29 05:50:42 -04:00
|
|
|
|
var target = msg.params[0];
|
2020-06-29 05:08:47 -04:00
|
|
|
|
var text = msg.params[1];
|
2020-06-29 05:50:42 -04:00
|
|
|
|
|
|
|
|
|
var kind;
|
2020-08-13 05:58:36 -04:00
|
|
|
|
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-01-22 11:36:53 -05:00
|
|
|
|
} else if (target == client.nick) {
|
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
|
|
|
|
|
2020-08-13 10:04:39 -04:00
|
|
|
|
if (msgUnread == Unread.HIGHLIGHT && window.Notification && Notification.permission === "granted" && !isDelivered && !irc.parseCTCP(msg)) {
|
2020-06-29 05:50:42 -04:00
|
|
|
|
var title = "New " + kind + " from " + msg.prefix.name;
|
|
|
|
|
if (this.isChannel(target)) {
|
|
|
|
|
title += " in " + target;
|
|
|
|
|
}
|
|
|
|
|
var notif = new Notification(title, {
|
2020-08-13 09:38:12 -04:00
|
|
|
|
body: stripANSI(text),
|
2020-06-29 05:50:42 -04:00
|
|
|
|
requireInteraction: true,
|
|
|
|
|
});
|
|
|
|
|
notif.addEventListener("click", () => {
|
|
|
|
|
// TODO: scroll to message
|
2021-01-21 16:15:33 -05:00
|
|
|
|
this.switchBuffer({ network: netID, name: target });
|
2020-06-29 05:50:42 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-24 10:56:28 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
if (msg.prefix.name != client.nick && (msg.command != "PART" && msg.comand != "QUIT")) {
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.createBuffer(netID, bufName);
|
2020-06-25 12:28:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-15 12:21:09 -04:00
|
|
|
|
this.setReceipt(bufName, ReceiptType.DELIVERED, msg);
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: bufName}, (buf, state) => {
|
2020-07-15 12:21:09 -04:00
|
|
|
|
// TODO: set unread if scrolled up
|
2020-06-24 10:56:28 -04:00
|
|
|
|
var unread = buf.unread;
|
2020-09-03 05:51:52 -04:00
|
|
|
|
var lastReadReceipt = buf.lastReadReceipt;
|
2021-01-21 14:41:44 -05:00
|
|
|
|
if (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-09-03 05:51:52 -04:00
|
|
|
|
lastReadReceipt = this.getReceipt(bufName, ReceiptType.READ);
|
2020-06-24 10:56:28 -04:00
|
|
|
|
}
|
2020-06-29 03:06:47 -04:00
|
|
|
|
var messages = insertMessage(buf.messages, msg);
|
2020-09-03 05:51:52 -04:00
|
|
|
|
return { messages, unread, lastReadReceipt };
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 12:44:06 -05:00
|
|
|
|
connect(params) {
|
|
|
|
|
this.lastNetworkID++;
|
|
|
|
|
var netID = this.lastNetworkID;
|
2021-01-12 04:35:38 -05:00
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
this.setState((state) => {
|
|
|
|
|
var networks = new Map(state.networks);
|
|
|
|
|
networks.set(netID, {
|
|
|
|
|
id: netID,
|
2021-01-22 12:29:22 -05:00
|
|
|
|
status: NetworkStatus.CONNECTING,
|
2021-01-22 05:34:04 -05:00
|
|
|
|
isupport: new Map(),
|
2021-01-21 13:01:50 -05:00
|
|
|
|
});
|
|
|
|
|
return { networks };
|
|
|
|
|
});
|
|
|
|
|
this.setState({ connectParams: params });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
2021-01-22 15:00:06 -05:00
|
|
|
|
var client = new Client(params);
|
2021-01-22 11:36:53 -05:00
|
|
|
|
this.clients.set(netID, client);
|
|
|
|
|
|
2021-01-22 12:29:22 -05:00
|
|
|
|
client.addEventListener("status", () => {
|
2021-01-22 12:44:06 -05:00
|
|
|
|
this.setNetworkState(netID, { status: client.status });
|
2021-01-21 13:01:50 -05:00
|
|
|
|
});
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
client.addEventListener("message", (event) => {
|
2021-01-21 13:01:50 -05:00
|
|
|
|
this.handleMessage(netID, event.detail.message);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
client.addEventListener("error", (event) => {
|
2020-08-08 00:08:51 -04:00
|
|
|
|
this.setState({
|
|
|
|
|
error: event.detail,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.createBuffer(netID, SERVER_BUFFER);
|
2021-01-21 16:15:33 -05:00
|
|
|
|
this.switchBuffer({ network: netID, name: SERVER_BUFFER });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
disconnect(netID) {
|
2021-01-22 04:38:07 -05:00
|
|
|
|
if (!netID) {
|
|
|
|
|
netID = getActiveNetworkID(this.state);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
if (client) {
|
2021-01-22 12:29:22 -05:00
|
|
|
|
client.disconnect();
|
2021-01-12 04:35:38 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
reconnect(netID) {
|
2021-01-22 04:38:07 -05:00
|
|
|
|
if (!netID) {
|
|
|
|
|
netID = getActiveNetworkID(this.state);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 12:44:06 -05:00
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
if (client) {
|
|
|
|
|
client.reconnect();
|
|
|
|
|
}
|
2021-01-12 04:35:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 13:01:50 -05:00
|
|
|
|
handleMessage(netID, msg) {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var client = this.clients.get(netID);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
switch (msg.command) {
|
|
|
|
|
case irc.RPL_WELCOME:
|
|
|
|
|
if (this.state.connectParams.autojoin.length > 0) {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
client.send({
|
2020-06-18 08:23:08 -04:00
|
|
|
|
command: "JOIN",
|
|
|
|
|
params: [this.state.connectParams.autojoin.join(",")],
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
2020-06-26 06:08:14 -04:00
|
|
|
|
case irc.RPL_MYINFO:
|
|
|
|
|
// TODO: parse available modes
|
|
|
|
|
var serverInfo = {
|
|
|
|
|
name: msg.params[1],
|
|
|
|
|
version: msg.params[2],
|
|
|
|
|
};
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: SERVER_BUFFER}, { serverInfo });
|
2020-06-26 06:08:14 -04:00
|
|
|
|
break;
|
2021-01-22 05:34:04 -05:00
|
|
|
|
case irc.RPL_ISUPPORT:
|
|
|
|
|
var tokens = msg.params.slice(1, -1);
|
|
|
|
|
this.setNetworkState(netID, (network) => {
|
|
|
|
|
var isupport = new Map(network.isupport);
|
|
|
|
|
irc.parseISUPPORT(tokens, isupport);
|
|
|
|
|
return { isupport };
|
|
|
|
|
});
|
|
|
|
|
break;
|
2020-08-03 12:59:54 -04:00
|
|
|
|
case irc.RPL_NOTOPIC:
|
|
|
|
|
var channel = msg.params[1];
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, { topic: null });
|
2020-08-03 12:59:54 -04:00
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
case irc.RPL_TOPIC:
|
|
|
|
|
var channel = msg.params[1];
|
|
|
|
|
var topic = msg.params[2];
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, { topic });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
2020-09-03 05:36:08 -04:00
|
|
|
|
case irc.RPL_TOPICWHOTIME:
|
|
|
|
|
// Ignore
|
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
case irc.RPL_NAMREPLY:
|
|
|
|
|
var channel = msg.params[2];
|
2020-06-30 04:27:24 -04:00
|
|
|
|
var membersList = msg.params[3].split(" ");
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, (buf) => {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
var members = new Map(buf.members);
|
|
|
|
|
membersList.forEach((s) => {
|
|
|
|
|
var member = irc.parseMembership(s);
|
|
|
|
|
members.set(member.nick, member.prefix);
|
|
|
|
|
});
|
|
|
|
|
|
2020-06-24 10:02:56 -04:00
|
|
|
|
return { members };
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case irc.RPL_ENDOFNAMES:
|
|
|
|
|
break;
|
2020-06-26 06:00:10 -04:00
|
|
|
|
case irc.RPL_WHOREPLY:
|
|
|
|
|
var last = msg.params[msg.params.length - 1];
|
|
|
|
|
var who = {
|
|
|
|
|
username: msg.params[2],
|
|
|
|
|
hostname: msg.params[3],
|
|
|
|
|
server: msg.params[4],
|
|
|
|
|
nick: msg.params[5],
|
|
|
|
|
away: msg.params[6] == 'G', // H for here, G for gone
|
|
|
|
|
realname: last.slice(last.indexOf(" ") + 1),
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: who.nick}, { who, offline: false });
|
2020-06-26 06:00:10 -04:00
|
|
|
|
break;
|
|
|
|
|
case irc.RPL_ENDOFWHO:
|
2020-07-13 06:51:09 -04:00
|
|
|
|
var target = msg.params[1];
|
|
|
|
|
if (!this.isChannel(target) && target.indexOf("*") < 0) {
|
|
|
|
|
// Not a channel nor a mask, likely a nick
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: target}, (buf) => {
|
2020-07-13 06:51:09 -04:00
|
|
|
|
// TODO: mark user offline if we have old WHO info but this
|
|
|
|
|
// WHO reply is empty
|
|
|
|
|
if (buf.who) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return { offline: true };
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-26 06:00:10 -04:00
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
case "NOTICE":
|
|
|
|
|
case "PRIVMSG":
|
|
|
|
|
var target = msg.params[0];
|
2021-01-22 11:36:53 -05:00
|
|
|
|
if (target == client.nick) {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
target = msg.prefix.name;
|
|
|
|
|
}
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.addMessage(netID, target, msg);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
|
|
|
|
case "JOIN":
|
|
|
|
|
var channel = msg.params[0];
|
|
|
|
|
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.createBuffer(netID, channel);
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, (buf) => {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
var members = new Map(buf.members);
|
|
|
|
|
members.set(msg.prefix.name, null);
|
2020-06-24 10:02:56 -04:00
|
|
|
|
return { members };
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
2021-01-22 11:36:53 -05:00
|
|
|
|
if (msg.prefix.name != client.nick) {
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.addMessage(netID, channel, msg);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
if (channel == this.state.connectParams.autojoin[0]) {
|
|
|
|
|
// TODO: only switch once right after connect
|
2021-01-21 16:15:33 -05:00
|
|
|
|
this.switchBuffer({ network: netID, name: channel });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
2020-07-15 12:21:09 -04:00
|
|
|
|
|
|
|
|
|
var receipt = this.getReceipt(channel, ReceiptType.READ);
|
2021-01-22 11:36:53 -05:00
|
|
|
|
if (msg.prefix.name == client.nick && receipt && client.enabledCaps["draft/chathistory"] && client.enabledCaps["server-time"]) {
|
2020-07-15 12:21:09 -04:00
|
|
|
|
var after = receipt;
|
|
|
|
|
var before = { time: msg.tags.time || irc.formatDate(new Date()) };
|
2021-01-22 12:51:38 -05:00
|
|
|
|
client.fetchHistoryBetween(channel, after, before, CHATHISTORY_MAX_SIZE).catch((err) => {
|
2020-08-08 00:08:51 -04:00
|
|
|
|
this.setState({ error: "Failed to fetch history: " + err });
|
2020-07-15 12:21:09 -04:00
|
|
|
|
this.receipts.delete(channel);
|
|
|
|
|
this.saveReceipts();
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
|
|
|
|
case "PART":
|
|
|
|
|
var channel = msg.params[0];
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, (buf) => {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
var members = new Map(buf.members);
|
|
|
|
|
members.delete(msg.prefix.name);
|
2020-06-24 10:02:56 -04:00
|
|
|
|
return { members };
|
2020-06-18 08:23:08 -04:00
|
|
|
|
});
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.addMessage(netID, channel, msg);
|
2020-07-15 12:21:09 -04:00
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
if (msg.prefix.name == client.nick) {
|
2020-07-15 12:21:09 -04:00
|
|
|
|
this.receipts.delete(channel);
|
|
|
|
|
this.saveReceipts();
|
|
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
2020-07-08 12:39:24 -04:00
|
|
|
|
case "QUIT":
|
|
|
|
|
var affectedBuffers = [];
|
|
|
|
|
this.setState((state) => {
|
|
|
|
|
var buffers = new Map(state.buffers);
|
|
|
|
|
state.buffers.forEach((buf) => {
|
|
|
|
|
if (!buf.members.has(msg.prefix.name) && buf.name != msg.prefix.name) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var members = new Map(buf.members);
|
|
|
|
|
members.delete(msg.prefix.name);
|
2020-07-13 06:51:09 -04:00
|
|
|
|
var offline = buf.name == msg.prefix.name;
|
2021-01-21 16:15:33 -05:00
|
|
|
|
buffers.set(buf.id, { ...buf, members, offline });
|
2020-07-08 12:39:24 -04:00
|
|
|
|
affectedBuffers.push(buf.name);
|
|
|
|
|
});
|
|
|
|
|
return { buffers };
|
|
|
|
|
});
|
2021-01-21 13:27:04 -05:00
|
|
|
|
affectedBuffers.forEach((name) => this.addMessage(netID, name, msg));
|
2020-07-08 12:39:24 -04:00
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
case "NICK":
|
|
|
|
|
var newNick = msg.params[0];
|
|
|
|
|
|
|
|
|
|
var affectedBuffers = [];
|
|
|
|
|
this.setState((state) => {
|
|
|
|
|
var buffers = new Map(state.buffers);
|
|
|
|
|
state.buffers.forEach((buf) => {
|
|
|
|
|
if (!buf.members.has(msg.prefix.name)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var members = new Map(buf.members);
|
|
|
|
|
members.set(newNick, members.get(msg.prefix.name));
|
|
|
|
|
members.delete(msg.prefix.name);
|
2021-01-21 16:15:33 -05:00
|
|
|
|
buffers.set(buf.id, { ...buf, members });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
affectedBuffers.push(buf.name);
|
|
|
|
|
});
|
|
|
|
|
return { buffers };
|
|
|
|
|
});
|
2021-01-21 13:27:04 -05:00
|
|
|
|
affectedBuffers.forEach((name) => this.addMessage(netID, name, msg));
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
|
|
|
|
case "TOPIC":
|
|
|
|
|
var channel = msg.params[0];
|
|
|
|
|
var topic = msg.params[1];
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: channel}, { topic });
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.addMessage(netID, channel, msg);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
break;
|
2020-06-26 06:45:27 -04:00
|
|
|
|
case "AWAY":
|
|
|
|
|
var awayMessage = msg.params[0];
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.setBufferState({ network: netID, name: msg.prefix.name}, (buf) => {
|
2020-06-26 06:45:27 -04:00
|
|
|
|
var who = { ...buf.who, away: !!awayMessage };
|
|
|
|
|
return { who };
|
|
|
|
|
});
|
|
|
|
|
break;
|
2020-06-25 06:16:42 -04:00
|
|
|
|
case "CAP":
|
|
|
|
|
case "AUTHENTICATE":
|
2020-07-01 06:12:56 -04:00
|
|
|
|
case "PING":
|
2020-07-15 12:21:09 -04:00
|
|
|
|
case "BATCH":
|
2020-06-25 06:16:42 -04:00
|
|
|
|
// Ignore these
|
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
default:
|
2021-01-21 13:27:04 -05:00
|
|
|
|
this.addMessage(netID, SERVER_BUFFER, msg);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleConnectSubmit(connectParams) {
|
2020-08-10 08:57:54 -04:00
|
|
|
|
this.setState({ error: null });
|
|
|
|
|
|
2020-06-24 12:14:46 -04:00
|
|
|
|
if (window.localStorage) {
|
|
|
|
|
if (connectParams.autoconnect) {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
localStorage.setItem("autoconnect", JSON.stringify(connectParams));
|
|
|
|
|
} else {
|
|
|
|
|
localStorage.removeItem("autoconnect");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 12:44:06 -05:00
|
|
|
|
this.connect(connectParams);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:45:41 -04:00
|
|
|
|
handleNickClick(nick) {
|
|
|
|
|
this.open(nick);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 09:52:33 -04:00
|
|
|
|
isChannel(name) {
|
|
|
|
|
// TODO: use the ISUPPORT token if available
|
|
|
|
|
return irc.STD_CHANNEL_TYPES.indexOf(name[0]) >= 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:45:41 -04:00
|
|
|
|
open(target) {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var netID = getActiveNetworkID(this.state);
|
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
|
2020-06-25 12:45:41 -04:00
|
|
|
|
if (this.isChannel(target)) {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
client.send({ command: "JOIN", params: [target] });
|
2020-06-26 06:00:10 -04:00
|
|
|
|
} else {
|
2021-01-22 11:36:53 -05:00
|
|
|
|
client.send({ command: "WHO", params: [target] });
|
2020-06-25 12:45:41 -04:00
|
|
|
|
}
|
2021-01-22 11:36:53 -05:00
|
|
|
|
this.createBuffer(netID, target);
|
|
|
|
|
this.switchBuffer({ network: netID, name: target });
|
2020-06-25 12:45:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 04:26:53 -05:00
|
|
|
|
close(id) {
|
|
|
|
|
var buf = getBuffer(this.state, id);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (buf.type) {
|
|
|
|
|
case BufferType.SERVER:
|
2021-01-11 12:12:28 -05:00
|
|
|
|
this.setState({
|
|
|
|
|
buffers: new Map(),
|
|
|
|
|
activeBuffer: null,
|
|
|
|
|
});
|
2021-01-22 04:26:53 -05:00
|
|
|
|
this.disconnect(buf.network);
|
2021-01-22 04:41:28 -05:00
|
|
|
|
this.setState((state) => {
|
|
|
|
|
var networks = new Map(state.networks);
|
|
|
|
|
networks.delete(buf.network);
|
|
|
|
|
return { networks };
|
|
|
|
|
});
|
2021-01-22 04:26:53 -05:00
|
|
|
|
break;
|
|
|
|
|
case BufferType.CHANNEL:
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var client = this.clients.get(buf.network);
|
|
|
|
|
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) => {
|
|
|
|
|
var buffers = new Map(state.buffers);
|
2021-01-22 11:36:53 -05:00
|
|
|
|
buffers.delete(buf.name);
|
2021-01-22 04:26:53 -05:00
|
|
|
|
return { buffers };
|
|
|
|
|
});
|
2020-07-15 12:21:09 -04:00
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
this.receipts.delete(buf.name);
|
2021-01-22 04:26:53 -05:00
|
|
|
|
this.saveReceipts();
|
|
|
|
|
break;
|
2020-06-25 12:28:54 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
executeCommand(s) {
|
|
|
|
|
var parts = s.split(" ");
|
2020-07-13 11:22:24 -04:00
|
|
|
|
var name = parts[0].toLowerCase().slice(1);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
var args = parts.slice(1);
|
2020-07-13 11:22:24 -04:00
|
|
|
|
|
|
|
|
|
var cmd = commands[name];
|
|
|
|
|
if (!cmd) {
|
2020-08-08 00:08:51 -04:00
|
|
|
|
this.setState({ error: "Unknown command '" + name + "'" });
|
2020-07-13 11:22:24 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
cmd(this, args);
|
2020-08-08 00:08:51 -04:00
|
|
|
|
} catch (error) {
|
|
|
|
|
this.setState({ error });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 03:29:39 -04:00
|
|
|
|
privmsg(target, text) {
|
|
|
|
|
if (target == SERVER_BUFFER) {
|
2020-08-08 00:08:51 -04:00
|
|
|
|
this.setState({ error: "Cannot send message in server buffer" });
|
2020-06-28 03:29:39 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 11:36:53 -05:00
|
|
|
|
var netID = getActiveNetworkID(this.state);
|
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
|
2020-06-28 03:29:39 -04:00
|
|
|
|
var 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(netID, target, msg);
|
2020-06-28 03:29:39 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
handleComposerSubmit(text) {
|
|
|
|
|
if (!text) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (text.startsWith("//")) {
|
|
|
|
|
text = text.slice(1);
|
|
|
|
|
} else if (text.startsWith("/")) {
|
|
|
|
|
this.executeCommand(text);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
var buf = this.state.buffers.get(this.state.activeBuffer);
|
|
|
|
|
if (!buf) {
|
2020-06-18 08:23:08 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
this.privmsg(buf.name, text);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 15:43:58 -05:00
|
|
|
|
handleBufferListClick(id) {
|
|
|
|
|
this.switchBuffer(id);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 04:12:46 -04:00
|
|
|
|
handleJoinClick(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
|
|
var channel = prompt("Join channel:");
|
|
|
|
|
if (!channel) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-22 11:36:53 -05:00
|
|
|
|
|
|
|
|
|
var netID = getActiveNetworkID(this.state);
|
|
|
|
|
var client = this.clients.get(netID);
|
|
|
|
|
|
|
|
|
|
client.send({ command: "JOIN", params: [channel] });
|
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();
|
|
|
|
|
var repl = null;
|
|
|
|
|
for (var item of l) {
|
|
|
|
|
if (item.toLowerCase().startsWith(prefix)) {
|
|
|
|
|
if (repl) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
repl = item;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return repl;
|
2020-06-29 06:36:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 11:28:49 -04:00
|
|
|
|
if (prefix.startsWith("/")) {
|
|
|
|
|
var repl = fromList(Object.keys(commands), prefix.slice(1));
|
|
|
|
|
if (repl) {
|
|
|
|
|
repl = "/" + repl;
|
2020-06-29 06:36:17 -04:00
|
|
|
|
}
|
2020-07-13 11:28:49 -04:00
|
|
|
|
return repl;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-21 14:41:44 -05:00
|
|
|
|
var buf = this.state.buffers.get(this.state.activeBuffer);
|
|
|
|
|
if (!buf || !buf.members) {
|
2020-07-13 11:28:49 -04:00
|
|
|
|
return null;
|
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
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 03:06:47 -04:00
|
|
|
|
handleBufferScrollTop() {
|
2021-01-21 14:41:44 -05:00
|
|
|
|
var buf = this.state.buffers.get(this.state.activeBuffer);
|
|
|
|
|
if (!buf || buf.type == BufferType.SERVER) {
|
2020-06-29 03:06:47 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-22 11:36:53 -05:00
|
|
|
|
|
|
|
|
|
var client = this.clients.get(buf.network);
|
|
|
|
|
|
|
|
|
|
if (!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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var before;
|
|
|
|
|
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
|
|
|
|
|
2021-01-23 06:16:57 -05:00
|
|
|
|
client.fetchHistoryBefore(buf.name, before).then((result) => {
|
|
|
|
|
this.endOfHistory.set(buf.id, !result.more);
|
2020-07-15 12:21:09 -04:00
|
|
|
|
});
|
2020-06-29 03:06:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
componentDidMount() {
|
2020-06-24 12:14:46 -04:00
|
|
|
|
if (this.state.connectParams.autoconnect) {
|
2021-01-22 12:44:06 -05:00
|
|
|
|
this.connect(this.state.connectParams);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
2020-07-23 03:58:05 -04:00
|
|
|
|
|
|
|
|
|
setupKeybindings(this);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2021-01-22 04:38:07 -05:00
|
|
|
|
var activeBuffer = null, activeNetwork = null;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
if (this.state.activeBuffer) {
|
|
|
|
|
activeBuffer = this.state.buffers.get(this.state.activeBuffer);
|
2021-01-22 04:38:07 -05:00
|
|
|
|
activeNetwork = this.state.networks.get(activeBuffer.network);
|
2020-06-18 08:23:08 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 12:29:22 -05:00
|
|
|
|
if (!activeNetwork || (activeNetwork.status !== NetworkStatus.REGISTERED && !activeBuffer)) {
|
2021-01-21 13:01:50 -05:00
|
|
|
|
return html`
|
|
|
|
|
<section id="connect">
|
2021-01-22 12:29:22 -05:00
|
|
|
|
<${Connect} error=${this.state.error} params=${this.state.connectParams} disabled=${activeNetwork} onSubmit=${this.handleConnectSubmit}/>
|
2021-01-21 13:01:50 -05:00
|
|
|
|
</section>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 09:16:07 -04:00
|
|
|
|
var bufferHeader = null;
|
2020-06-26 06:08:14 -04:00
|
|
|
|
if (activeBuffer) {
|
2020-06-26 09:16:07 -04:00
|
|
|
|
bufferHeader = html`
|
|
|
|
|
<section id="buffer-header">
|
2021-01-22 05:53:17 -05:00
|
|
|
|
<${BufferHeader} buffer=${activeBuffer} network=${activeNetwork} onClose=${() => this.close(activeBuffer)}/>
|
2020-06-25 12:28:54 -04:00
|
|
|
|
</section>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-26 08:32:56 -04:00
|
|
|
|
var memberList = null;
|
|
|
|
|
if (activeBuffer && activeBuffer.type == BufferType.CHANNEL) {
|
|
|
|
|
memberList = html`
|
|
|
|
|
<section id="member-list-header">
|
|
|
|
|
${activeBuffer.members.size} users
|
|
|
|
|
</section>
|
|
|
|
|
<section id="member-list">
|
|
|
|
|
<${MemberList} members=${activeBuffer.members} onNickClick=${this.handleNickClick}/>
|
|
|
|
|
</section>
|
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
|
return html`
|
2020-06-26 09:16:07 -04:00
|
|
|
|
<section id="buffer-list">
|
2021-01-22 05:43:47 -05:00
|
|
|
|
<${BufferList} buffers=${this.state.buffers} networks=${this.state.networks} activeBuffer=${this.state.activeBuffer} onBufferClick=${this.handleBufferListClick}/>
|
2020-06-29 04:12:46 -04:00
|
|
|
|
<div class="actions">
|
|
|
|
|
<a href="#" onClick=${this.handleJoinClick}>Join channel</a>
|
|
|
|
|
</div>
|
2020-06-18 08:23:08 -04:00
|
|
|
|
</section>
|
2020-06-26 09:16:07 -04:00
|
|
|
|
${bufferHeader}
|
2020-06-29 03:06:47 -04:00
|
|
|
|
<${ScrollManager} target=${this.buffer} scrollKey=${this.state.activeBuffer} onScrollTop=${this.handleBufferScrollTop}>
|
2020-06-25 06:03:05 -04:00
|
|
|
|
<section id="buffer" ref=${this.buffer}>
|
2020-06-25 12:45:41 -04:00
|
|
|
|
<${Buffer} buffer=${activeBuffer} onNickClick=${this.handleNickClick}/>
|
2020-06-25 06:03:05 -04:00
|
|
|
|
</section>
|
|
|
|
|
</>
|
2020-06-26 08:32:56 -04:00
|
|
|
|
${memberList}
|
2021-01-22 06:05:45 -05:00
|
|
|
|
<${Composer} ref=${this.composer} readOnly=${activeBuffer && activeBuffer.type == BufferType.SERVER} onSubmit=${this.handleComposerSubmit} autocomplete=${this.autocomplete}/>
|
2020-08-08 00:08:51 -04:00
|
|
|
|
${this.state.error ? html`
|
|
|
|
|
<p id="error-msg">${this.state.error} <a href="#" onClick=${this.dismissError}>×</a></p>
|
|
|
|
|
` : null}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|