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-07-13 11:22:24 -04:00
|
|
|
import { SERVER_BUFFER, BufferType, Status, Unread } from "/state.js";
|
|
|
|
import commands from "/commands.js";
|
2020-06-26 04:35:38 -04:00
|
|
|
|
2020-06-29 03:06:47 -04:00
|
|
|
const CHATHISTORY_PAGE_SIZE = 100;
|
2020-06-18 08:23:08 -04:00
|
|
|
|
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-06-18 08:23:08 -04:00
|
|
|
export default class App extends Component {
|
|
|
|
client = null;
|
|
|
|
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: [],
|
|
|
|
},
|
2020-06-24 10:56:28 -04:00
|
|
|
status: Status.DISCONNECTED,
|
2020-06-18 08:23:08 -04:00
|
|
|
buffers: new Map(),
|
|
|
|
activeBuffer: null,
|
|
|
|
};
|
2020-06-29 03:06:47 -04:00
|
|
|
endOfHistory = new Map();
|
2020-06-25 06:03:05 -04:00
|
|
|
buffer = createRef();
|
2020-06-18 08:23:08 -04:00
|
|
|
composer = createRef();
|
|
|
|
|
|
|
|
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-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-06-18 08:23:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setBufferState(name, updater, callback) {
|
|
|
|
this.setState((state) => {
|
|
|
|
var buf = state.buffers.get(name);
|
|
|
|
if (!buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-24 10:56:28 -04:00
|
|
|
var updated;
|
|
|
|
if (typeof updater === "function") {
|
|
|
|
updated = updater(buf, state);
|
|
|
|
} else {
|
|
|
|
updated = updater;
|
|
|
|
}
|
2020-06-24 10:02:56 -04:00
|
|
|
if (buf === updated || !updated) {
|
2020-06-18 08:23:08 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-06-24 10:02:56 -04:00
|
|
|
updated = { ...buf, ...updated };
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
var buffers = new Map(state.buffers);
|
2020-06-24 10:02:56 -04:00
|
|
|
buffers.set(name, updated);
|
2020-06-18 08:23:08 -04:00
|
|
|
return { buffers };
|
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
createBuffer(name) {
|
|
|
|
this.setState((state) => {
|
|
|
|
if (state.buffers.get(name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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-06-18 08:23:08 -04:00
|
|
|
var buffers = new Map(state.buffers);
|
|
|
|
buffers.set(name, {
|
2020-06-26 04:35:38 -04:00
|
|
|
name,
|
|
|
|
type,
|
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
|
|
|
});
|
|
|
|
return { buffers };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
switchBuffer(name) {
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setBufferState(name, { unread: Unread.NONE });
|
2020-06-18 08:23:08 -04:00
|
|
|
this.setState({ activeBuffer: name }, () => {
|
|
|
|
if (this.composer.current) {
|
|
|
|
this.composer.current.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessage(bufName, msg) {
|
2020-06-28 09:13:06 -04:00
|
|
|
msg.key = messagesCount;
|
|
|
|
messagesCount++;
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
var isHistory = false;
|
|
|
|
if (msg.tags.batch && this.client.batches.has(msg.tags.batch)) {
|
|
|
|
var batch = this.client.batches.get(msg.tags.batch);
|
|
|
|
isHistory = batch.type == "chathistory";
|
2020-06-24 11:16:49 -04:00
|
|
|
}
|
2020-06-18 08:23:08 -04:00
|
|
|
|
2020-06-24 11:46:43 -04:00
|
|
|
var msgUnread = Unread.NONE;
|
2020-06-29 03:06:47 -04:00
|
|
|
if ((msg.command == "PRIVMSG" || msg.command == "NOTICE") && !isHistory) {
|
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-06-29 05:08:47 -04:00
|
|
|
if (msg.prefix.name != this.client.nick && irc.isHighlight(text, this.client.nick)) {
|
|
|
|
msgUnread = Unread.HIGHLIGHT;
|
2020-06-29 05:50:42 -04:00
|
|
|
kind = "highlight";
|
|
|
|
} else if (target == this.client.nick) {
|
|
|
|
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 && window.Notification && Notification.permission === "granted") {
|
|
|
|
var title = "New " + kind + " from " + msg.prefix.name;
|
|
|
|
if (this.isChannel(target)) {
|
|
|
|
title += " in " + target;
|
|
|
|
}
|
|
|
|
var notif = new Notification(title, {
|
|
|
|
body: text,
|
|
|
|
requireInteraction: true,
|
|
|
|
});
|
|
|
|
notif.addEventListener("click", () => {
|
|
|
|
// TODO: scroll to message
|
|
|
|
this.switchBuffer(target);
|
|
|
|
});
|
|
|
|
}
|
2020-06-24 10:56:28 -04:00
|
|
|
}
|
|
|
|
|
2020-07-08 12:39:24 -04:00
|
|
|
if (msg.prefix.name != this.client.nick && (msg.command != "PART" && msg.comand != "QUIT")) {
|
2020-06-25 12:28:54 -04:00
|
|
|
this.createBuffer(bufName);
|
|
|
|
}
|
|
|
|
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setBufferState(bufName, (buf, state) => {
|
|
|
|
var unread = buf.unread;
|
|
|
|
if (state.activeBuffer != buf.name) {
|
2020-06-24 11:46:43 -04:00
|
|
|
unread = Unread.union(unread, msgUnread);
|
2020-06-24 10:56:28 -04:00
|
|
|
}
|
2020-06-29 03:06:47 -04:00
|
|
|
var messages = insertMessage(buf.messages, msg);
|
2020-06-24 10:56:28 -04:00
|
|
|
return {
|
2020-06-29 03:06:47 -04:00
|
|
|
messages,
|
2020-06-24 10:56:28 -04:00
|
|
|
unread,
|
|
|
|
};
|
2020-06-18 08:23:08 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(params) {
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setState({ status: Status.CONNECTING, connectParams: params });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
this.client = new Client({
|
|
|
|
url: params.serverURL,
|
|
|
|
pass: params.serverPass,
|
|
|
|
nick: params.nick,
|
|
|
|
username: params.username,
|
|
|
|
realname: params.realname,
|
|
|
|
saslPlain: params.saslPlain,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.client.addEventListener("close", () => {
|
2020-06-24 09:27:29 -04:00
|
|
|
this.setState({
|
2020-06-24 10:56:28 -04:00
|
|
|
status: Status.DISCONNECTED,
|
2020-06-24 09:27:29 -04:00
|
|
|
buffers: new Map(),
|
|
|
|
activeBuffer: null,
|
|
|
|
});
|
2020-06-18 08:23:08 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.client.addEventListener("message", (event) => {
|
|
|
|
this.handleMessage(event.detail.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.createBuffer(SERVER_BUFFER);
|
|
|
|
this.switchBuffer(SERVER_BUFFER);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMessage(msg) {
|
|
|
|
switch (msg.command) {
|
|
|
|
case irc.RPL_WELCOME:
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setState({ status: Status.REGISTERED });
|
2020-06-18 08:23:08 -04:00
|
|
|
|
|
|
|
if (this.state.connectParams.autojoin.length > 0) {
|
|
|
|
this.client.send({
|
|
|
|
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],
|
|
|
|
};
|
|
|
|
this.setBufferState(SERVER_BUFFER, { serverInfo });
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
case irc.RPL_TOPIC:
|
|
|
|
var channel = msg.params[1];
|
|
|
|
var topic = msg.params[2];
|
|
|
|
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setBufferState(channel, { topic });
|
2020-06-18 08:23:08 -04:00
|
|
|
break;
|
|
|
|
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
|
|
|
|
|
|
|
this.setBufferState(channel, (buf) => {
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
2020-07-13 06:51:09 -04:00
|
|
|
this.setBufferState(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
|
|
|
|
this.setBufferState(target, (buf) => {
|
|
|
|
// 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];
|
|
|
|
if (target == this.client.nick) {
|
|
|
|
target = msg.prefix.name;
|
|
|
|
}
|
|
|
|
this.addMessage(target, msg);
|
|
|
|
break;
|
|
|
|
case "JOIN":
|
|
|
|
var channel = msg.params[0];
|
|
|
|
|
|
|
|
this.createBuffer(channel);
|
|
|
|
this.setBufferState(channel, (buf) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
if (msg.prefix.name != this.client.nick) {
|
|
|
|
this.addMessage(channel, msg);
|
|
|
|
}
|
|
|
|
if (channel == this.state.connectParams.autojoin[0]) {
|
|
|
|
// TODO: only switch once right after connect
|
|
|
|
this.switchBuffer(channel);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "PART":
|
|
|
|
var channel = msg.params[0];
|
|
|
|
|
|
|
|
this.setBufferState(channel, (buf) => {
|
|
|
|
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
|
|
|
});
|
|
|
|
this.addMessage(channel, msg);
|
|
|
|
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;
|
|
|
|
buffers.set(buf.name, { ...buf, members, offline });
|
2020-07-08 12:39:24 -04:00
|
|
|
affectedBuffers.push(buf.name);
|
|
|
|
});
|
|
|
|
return { buffers };
|
|
|
|
});
|
|
|
|
affectedBuffers.forEach((name) => this.addMessage(name, msg));
|
|
|
|
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);
|
|
|
|
buffers.set(buf.name, { ...buf, members });
|
|
|
|
affectedBuffers.push(buf.name);
|
|
|
|
});
|
|
|
|
return { buffers };
|
|
|
|
});
|
|
|
|
affectedBuffers.forEach((name) => this.addMessage(name, msg));
|
|
|
|
break;
|
|
|
|
case "TOPIC":
|
|
|
|
var channel = msg.params[0];
|
|
|
|
var topic = msg.params[1];
|
|
|
|
|
2020-06-24 10:56:28 -04:00
|
|
|
this.setBufferState(channel, { topic });
|
2020-06-18 08:23:08 -04:00
|
|
|
this.addMessage(channel, msg);
|
|
|
|
break;
|
2020-06-26 06:45:27 -04:00
|
|
|
case "AWAY":
|
|
|
|
var awayMessage = msg.params[0];
|
|
|
|
|
|
|
|
this.setBufferState(msg.prefix.name, (buf) => {
|
|
|
|
var who = { ...buf.who, away: !!awayMessage };
|
|
|
|
return { who };
|
|
|
|
});
|
|
|
|
break;
|
2020-06-29 03:06:47 -04:00
|
|
|
case "BATCH":
|
|
|
|
var enter = msg.params[0].startsWith("+");
|
|
|
|
var name = msg.params[0].slice(1);
|
|
|
|
if (enter) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
var batch = this.client.batches.get(name);
|
|
|
|
if (batch.type == "chathistory" && batch.messages.length < CHATHISTORY_PAGE_SIZE) {
|
|
|
|
var target = batch.params[0];
|
|
|
|
this.endOfHistory.set(target, true);
|
|
|
|
}
|
|
|
|
break;
|
2020-06-25 06:16:42 -04:00
|
|
|
case "CAP":
|
|
|
|
case "AUTHENTICATE":
|
2020-07-01 06:12:56 -04:00
|
|
|
case "PING":
|
2020-06-25 06:16:42 -04:00
|
|
|
// Ignore these
|
|
|
|
break;
|
2020-06-18 08:23:08 -04:00
|
|
|
default:
|
|
|
|
this.addMessage(SERVER_BUFFER, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleConnectSubmit(connectParams) {
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.connect(connectParams);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (this.isChannel(target)) {
|
|
|
|
this.client.send({ command: "JOIN", params: [target] });
|
2020-06-26 06:00:10 -04:00
|
|
|
} else {
|
|
|
|
this.client.send({ command: "WHO", params: [target] });
|
2020-06-25 12:45:41 -04:00
|
|
|
}
|
|
|
|
this.createBuffer(target);
|
|
|
|
this.switchBuffer(target);
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:28:54 -04:00
|
|
|
close(target) {
|
|
|
|
if (target == SERVER_BUFFER) {
|
|
|
|
this.client.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.isChannel(target)) {
|
|
|
|
this.client.send({ command: "PART", params: [target] });
|
|
|
|
}
|
|
|
|
this.switchBuffer(SERVER_BUFFER);
|
|
|
|
this.setState((state) => {
|
|
|
|
var buffers = new Map(state.buffers);
|
|
|
|
buffers.delete(target);
|
|
|
|
return { buffers };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
console.error("Unknwon command '" + name + "'");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
cmd(this, args);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2020-06-18 08:23:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:29:39 -04:00
|
|
|
privmsg(target, text) {
|
|
|
|
if (target == SERVER_BUFFER) {
|
|
|
|
console.error("Cannot send message in server buffer");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg = { command: "PRIVMSG", params: [target, text] };
|
|
|
|
this.client.send(msg);
|
|
|
|
|
|
|
|
if (!this.client.enabledCaps["echo-message"]) {
|
|
|
|
msg.prefix = { name: this.client.nick };
|
|
|
|
this.addMessage(target, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var target = this.state.activeBuffer;
|
2020-06-28 03:29:39 -04:00
|
|
|
if (!target) {
|
2020-06-18 08:23:08 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:29:39 -04:00
|
|
|
this.privmsg(target, text);
|
2020-06-18 08:23:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
handleBufferListClick(name) {
|
|
|
|
this.switchBuffer(name);
|
|
|
|
}
|
|
|
|
|
2020-06-29 04:12:46 -04:00
|
|
|
handleJoinClick(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
var channel = prompt("Join channel:");
|
|
|
|
if (!channel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.client.send({ command: "JOIN", params: [channel] });
|
|
|
|
}
|
|
|
|
|
2020-06-29 06:36:17 -04:00
|
|
|
autocomplete(prefix) {
|
|
|
|
if (!this.state.activeBuffer) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var buf = this.state.buffers.get(this.state.activeBuffer);
|
|
|
|
|
|
|
|
prefix = prefix.toLowerCase();
|
|
|
|
|
|
|
|
var repl = null;
|
|
|
|
for (var nick of buf.members.keys()) {
|
|
|
|
if (nick.toLowerCase().startsWith(prefix)) {
|
|
|
|
if (repl) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
repl = nick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return repl;
|
|
|
|
}
|
|
|
|
|
2020-06-29 03:06:47 -04:00
|
|
|
handleBufferScrollTop() {
|
|
|
|
var target = this.state.activeBuffer;
|
|
|
|
if (!target || target == SERVER_BUFFER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!this.client.enabledCaps["draft/chathistory"] || !this.client.enabledCaps["server-time"]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.endOfHistory.has(target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var buf = this.state.buffers.get(target);
|
|
|
|
|
|
|
|
var before;
|
|
|
|
if (buf.messages.length > 0) {
|
|
|
|
before = buf.messages[0].tags["time"];
|
|
|
|
} else {
|
|
|
|
before = irc.formatDate(new Date());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.client.send({
|
|
|
|
command: "CHATHISTORY",
|
|
|
|
params: ["BEFORE", target, "timestamp=" + before, CHATHISTORY_PAGE_SIZE],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-18 08:23:08 -04:00
|
|
|
componentDidMount() {
|
2020-06-24 12:14:46 -04:00
|
|
|
if (this.state.connectParams.autoconnect) {
|
|
|
|
this.connect(this.state.connectParams);
|
2020-06-18 08:23:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-06-24 10:56:28 -04:00
|
|
|
if (this.state.status != Status.REGISTERED) {
|
2020-06-18 08:23:08 -04:00
|
|
|
return html`
|
|
|
|
<section id="connect">
|
2020-06-24 10:56:28 -04:00
|
|
|
<${Connect} params=${this.state.connectParams} disabled=${this.state.status != Status.DISCONNECTED} onSubmit=${this.handleConnectSubmit}/>
|
2020-06-18 08:23:08 -04:00
|
|
|
</section>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
var activeBuffer = null;
|
|
|
|
if (this.state.activeBuffer) {
|
|
|
|
activeBuffer = this.state.buffers.get(this.state.activeBuffer);
|
|
|
|
}
|
|
|
|
|
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">
|
2020-06-25 12:28:54 -04:00
|
|
|
<${BufferHeader} buffer=${activeBuffer} onClose=${() => this.close(activeBuffer.name)}/>
|
|
|
|
</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">
|
2020-06-18 08:23:08 -04:00
|
|
|
<${BufferList} buffers=${this.state.buffers} 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}
|
2020-06-29 06:36:17 -04:00
|
|
|
<${Composer} ref=${this.composer} readOnly=${this.state.activeBuffer == SERVER_BUFFER} onSubmit=${this.handleComposerSubmit} autocomplete=${this.autocomplete}/>
|
2020-06-18 08:23:08 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|