2021-06-04 12:03:03 -04:00
|
|
|
import * as irc from "./lib/irc.js";
|
2021-03-02 16:46:48 -05:00
|
|
|
import Client from "./lib/client.js";
|
2021-01-22 12:29:22 -05:00
|
|
|
|
2020-07-13 11:22:24 -04:00
|
|
|
export const SERVER_BUFFER = "*";
|
|
|
|
|
2020-06-26 04:35:38 -04:00
|
|
|
export const BufferType = {
|
|
|
|
SERVER: "server",
|
|
|
|
CHANNEL: "channel",
|
|
|
|
NICK: "nick",
|
|
|
|
};
|
2020-06-24 10:56:28 -04:00
|
|
|
|
2021-06-03 05:46:50 -04:00
|
|
|
export const ServerStatus = Client.Status;
|
2020-06-24 10:56:28 -04:00
|
|
|
|
|
|
|
export const Unread = {
|
|
|
|
NONE: "",
|
|
|
|
MESSAGE: "message",
|
2020-06-29 05:08:47 -04:00
|
|
|
HIGHLIGHT: "highlight",
|
2020-06-24 10:56:28 -04:00
|
|
|
|
2021-05-31 12:26:04 -04:00
|
|
|
compare(a, b) {
|
2020-06-24 10:56:28 -04:00
|
|
|
const priority = {
|
2021-05-27 16:35:41 -04:00
|
|
|
[Unread.NONE]: 0,
|
2020-06-24 10:56:28 -04:00
|
|
|
[Unread.MESSAGE]: 1,
|
2020-06-29 05:08:47 -04:00
|
|
|
[Unread.HIGHLIGHT]: 2,
|
2020-06-24 10:56:28 -04:00
|
|
|
};
|
2021-05-31 12:26:04 -04:00
|
|
|
return priority[a] - priority[b];
|
|
|
|
},
|
|
|
|
union(a, b) {
|
|
|
|
return (Unread.compare(a, b) > 0) ? a : b;
|
2020-06-24 10:56:28 -04:00
|
|
|
},
|
|
|
|
};
|
2020-07-15 12:47:33 -04:00
|
|
|
|
2020-07-23 03:58:05 -04:00
|
|
|
export const ReceiptType = {
|
|
|
|
DELIVERED: "delivered",
|
|
|
|
READ: "read",
|
|
|
|
};
|
|
|
|
|
2020-07-15 12:47:33 -04:00
|
|
|
export function getNickURL(nick) {
|
2021-05-25 06:42:24 -04:00
|
|
|
return "irc:///" + encodeURIComponent(nick) + ",isuser";
|
2020-07-15 12:47:33 -04:00
|
|
|
}
|
|
|
|
|
2021-06-03 05:04:32 -04:00
|
|
|
export function getChannelURL(channel) {
|
|
|
|
return "irc:///" + encodeURIComponent(channel);
|
|
|
|
}
|
|
|
|
|
2020-07-15 12:47:33 -04:00
|
|
|
export function getBufferURL(buf) {
|
|
|
|
switch (buf.type) {
|
|
|
|
case BufferType.SERVER:
|
|
|
|
return "irc:///";
|
|
|
|
case BufferType.CHANNEL:
|
2021-06-03 05:04:32 -04:00
|
|
|
return getChannelURL(buf.name);
|
2020-07-15 12:47:33 -04:00
|
|
|
case BufferType.NICK:
|
|
|
|
return getNickURL(buf.name);
|
|
|
|
}
|
|
|
|
throw new Error("Unknown buffer type: " + buf.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getMessageURL(buf, msg) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let bufURL = getBufferURL(buf);
|
2020-07-21 08:48:04 -04:00
|
|
|
if (msg.tags.msgid) {
|
2021-05-25 06:46:00 -04:00
|
|
|
return bufURL + "?msgid=" + encodeURIComponent(msg.tags.msgid);
|
2020-07-21 08:48:04 -04:00
|
|
|
} else {
|
2021-05-25 06:46:00 -04:00
|
|
|
return bufURL + "?timestamp=" + encodeURIComponent(msg.tags.time);
|
2020-07-21 08:48:04 -04:00
|
|
|
}
|
2020-07-15 12:47:33 -04:00
|
|
|
}
|
2021-05-31 11:39:37 -04:00
|
|
|
|
2021-12-07 07:16:07 -05:00
|
|
|
export function getServerName(server, bouncerNetwork) {
|
2021-12-07 06:09:10 -05:00
|
|
|
let netName = server.name;
|
2021-10-15 08:05:39 -04:00
|
|
|
|
2021-10-18 13:51:30 -04:00
|
|
|
if (bouncerNetwork && bouncerNetwork.name && bouncerNetwork.name !== bouncerNetwork.host) {
|
|
|
|
// User has picked a custom name for the network, use that
|
|
|
|
return bouncerNetwork.name;
|
2021-05-31 11:39:37 -04:00
|
|
|
}
|
2021-10-18 13:51:30 -04:00
|
|
|
|
|
|
|
if (netName) {
|
|
|
|
// Server has specified a name
|
|
|
|
return netName;
|
2021-05-31 11:39:37 -04:00
|
|
|
}
|
|
|
|
|
2021-10-18 13:51:30 -04:00
|
|
|
if (bouncerNetwork) {
|
|
|
|
return bouncerNetwork.name || bouncerNetwork.host || "server";
|
2021-12-07 07:16:07 -05:00
|
|
|
} else if (server.isBouncer) {
|
2021-10-18 13:51:30 -04:00
|
|
|
return "bouncer";
|
|
|
|
} else {
|
|
|
|
return "server";
|
|
|
|
}
|
2021-05-31 11:39:37 -04:00
|
|
|
}
|
2021-06-04 12:03:03 -04:00
|
|
|
|
2022-02-12 04:05:58 -05:00
|
|
|
export function receiptFromMessage(msg) {
|
|
|
|
// At this point all messages are supposed to have a time tag.
|
|
|
|
// App.addMessage ensures this is the case even if the server doesn't
|
|
|
|
// support server-time.
|
|
|
|
if (!msg.tags.time) {
|
|
|
|
throw new Error("Missing time message tag");
|
|
|
|
}
|
|
|
|
return { time: msg.tags.time };
|
|
|
|
}
|
|
|
|
|
2022-02-12 04:21:11 -05:00
|
|
|
export function isReceiptBefore(a, b) {
|
|
|
|
if (!b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!a) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!a.time || !b.time) {
|
|
|
|
throw new Error("Missing receipt time");
|
|
|
|
}
|
|
|
|
return a.time <= b.time;
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:37:58 -05:00
|
|
|
export function isMessageBeforeReceipt(msg, receipt) {
|
|
|
|
if (!receipt) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!msg.tags.time) {
|
|
|
|
throw new Error("Missing time message tag");
|
|
|
|
}
|
|
|
|
if (!receipt.time) {
|
|
|
|
throw new Error("Missing receipt time");
|
|
|
|
}
|
|
|
|
return msg.tags.time <= receipt.time;
|
|
|
|
}
|
|
|
|
|
2021-06-04 12:03:03 -04:00
|
|
|
function updateState(state, updater) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let updated;
|
2021-06-04 12:03:03 -04:00
|
|
|
if (typeof updater === "function") {
|
|
|
|
updated = updater(state, state);
|
|
|
|
} else {
|
|
|
|
updated = updater;
|
|
|
|
}
|
|
|
|
if (state === updated || !updated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return { ...state, ...updated };
|
|
|
|
}
|
|
|
|
|
2021-06-04 12:37:34 -04:00
|
|
|
function isServerBuffer(buf) {
|
|
|
|
return buf.type == BufferType.SERVER;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns 1 if a should appear after b, -1 if a should appear before b, or
|
|
|
|
* 0 otherwise. */
|
|
|
|
function compareBuffers(a, b) {
|
|
|
|
if (a.server != b.server) {
|
|
|
|
return a.server > b.server ? 1 : -1;
|
|
|
|
}
|
|
|
|
if (isServerBuffer(a) != isServerBuffer(b)) {
|
|
|
|
return isServerBuffer(b) ? 1 : -1;
|
|
|
|
}
|
|
|
|
if (a.name != b.name) {
|
|
|
|
return a.name > b.name ? 1 : -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-11 06:44:14 -04:00
|
|
|
function updateMembership(membership, letter, add, client) {
|
2021-12-07 06:09:10 -05:00
|
|
|
let prefix = client.isupport.prefix();
|
2021-06-11 06:44:14 -04:00
|
|
|
|
|
|
|
let prefixPrivs = new Map(irc.parseMembershipModes(prefix).map((membership, i) => {
|
|
|
|
return [membership.prefix, i];
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
let i = membership.indexOf(letter);
|
|
|
|
if (i < 0) {
|
|
|
|
membership += letter;
|
|
|
|
membership = Array.from(membership).sort((a, b) => {
|
|
|
|
return prefixPrivs.get(a) - prefixPrivs.get(b);
|
|
|
|
}).join("");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
membership = membership.replace(letter, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
return membership;
|
|
|
|
}
|
|
|
|
|
2021-06-04 13:07:14 -04:00
|
|
|
/* Insert a message in an immutable list of sorted messages. */
|
|
|
|
function insertMessage(list, msg) {
|
|
|
|
if (list.length == 0) {
|
|
|
|
return [msg];
|
2021-11-09 04:49:18 -05:00
|
|
|
} else if (!irc.findBatchByType(msg, "chathistory") || list[list.length - 1].tags.time <= msg.tags.time) {
|
2021-06-04 13:07:14 -04:00
|
|
|
return list.concat(msg);
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let insertBefore = -1;
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
let other = list[i];
|
2021-06-04 13:07:14 -04:00
|
|
|
if (msg.tags.time < other.tags.time) {
|
|
|
|
insertBefore = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.assert(insertBefore >= 0, "");
|
|
|
|
|
|
|
|
list = [ ...list ];
|
|
|
|
list.splice(insertBefore, 0, msg);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let lastServerID = 0;
|
|
|
|
let lastBufferID = 0;
|
2021-06-11 04:58:09 -04:00
|
|
|
let lastMessageKey = 0;
|
2021-06-04 12:37:34 -04:00
|
|
|
|
2021-06-04 12:03:03 -04:00
|
|
|
export const State = {
|
2021-09-21 06:33:22 -04:00
|
|
|
create() {
|
|
|
|
return {
|
|
|
|
servers: new Map(),
|
|
|
|
buffers: new Map(),
|
|
|
|
activeBuffer: null,
|
2022-02-04 08:22:50 -05:00
|
|
|
bouncerNetworks: new Map(),
|
2021-09-21 06:33:22 -04:00
|
|
|
};
|
|
|
|
},
|
2021-06-04 12:03:03 -04:00
|
|
|
updateServer(state, id, updater) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let server = state.servers.get(id);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!server) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let updated = updateState(server, updater);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!updated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let servers = new Map(state.servers);
|
2021-06-04 12:03:03 -04:00
|
|
|
servers.set(id, updated);
|
|
|
|
return { servers };
|
|
|
|
},
|
|
|
|
updateBuffer(state, id, updater) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let buf = State.getBuffer(state, id);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!buf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let updated = updateState(buf, updater);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!updated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let buffers = new Map(state.buffers);
|
2021-06-04 12:03:03 -04:00
|
|
|
buffers.set(buf.id, updated);
|
|
|
|
return { buffers };
|
|
|
|
},
|
|
|
|
getActiveServerID(state) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let buf = state.buffers.get(state.activeBuffer);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!buf) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return buf.server;
|
|
|
|
},
|
|
|
|
getBuffer(state, id) {
|
|
|
|
switch (typeof id) {
|
|
|
|
case "number":
|
|
|
|
return state.buffers.get(id);
|
|
|
|
case "object":
|
|
|
|
if (id.id) {
|
|
|
|
return state.buffers.get(id.id);
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let serverID = id.server, name = id.name;
|
2021-06-04 12:03:03 -04:00
|
|
|
if (!serverID) {
|
|
|
|
serverID = State.getActiveServerID(state);
|
|
|
|
}
|
2021-06-10 03:25:17 -04:00
|
|
|
if (!name) {
|
|
|
|
name = SERVER_BUFFER;
|
|
|
|
}
|
2021-06-04 12:03:03 -04:00
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let cm = irc.CaseMapping.RFC1459;
|
|
|
|
let server = state.servers.get(serverID);
|
2021-06-04 12:03:03 -04:00
|
|
|
if (server) {
|
2021-12-07 06:09:10 -05:00
|
|
|
cm = server.cm;
|
2021-06-04 12:03:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let nameCM = cm(name);
|
|
|
|
for (let buf of state.buffers.values()) {
|
2021-06-04 12:03:03 -04:00
|
|
|
if (buf.server === serverID && cm(buf.name) === nameCM) {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
default:
|
|
|
|
throw new Error("Invalid buffer ID type: " + (typeof id));
|
|
|
|
}
|
|
|
|
},
|
2021-06-10 04:54:33 -04:00
|
|
|
createServer(state) {
|
|
|
|
lastServerID++;
|
2021-06-10 12:11:11 -04:00
|
|
|
let id = lastServerID;
|
2021-06-10 04:54:33 -04:00
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let servers = new Map(state.servers);
|
2021-06-10 04:54:33 -04:00
|
|
|
servers.set(id, {
|
|
|
|
id,
|
2021-12-07 06:09:10 -05:00
|
|
|
name: null, // from ISUPPORT NETWORK
|
2021-06-10 04:54:33 -04:00
|
|
|
status: ServerStatus.DISCONNECTED,
|
2021-12-07 06:09:10 -05:00
|
|
|
cm: irc.CaseMapping.RFC1459,
|
2021-09-21 07:33:15 -04:00
|
|
|
users: new irc.CaseMapMap(null, irc.CaseMapping.RFC1459),
|
2021-11-21 06:13:44 -05:00
|
|
|
account: null,
|
2021-11-21 10:40:46 -05:00
|
|
|
supportsSASLPlain: false,
|
2021-11-16 07:32:54 -05:00
|
|
|
supportsAccountRegistration: false,
|
2021-12-07 06:09:10 -05:00
|
|
|
reliableUserAccounts: false,
|
|
|
|
statusMsg: null, // from ISUPPORT STATUSMSG
|
2021-12-07 07:16:07 -05:00
|
|
|
isBouncer: false,
|
2021-12-07 06:09:10 -05:00
|
|
|
bouncerNetID: null,
|
2021-06-10 04:54:33 -04:00
|
|
|
});
|
|
|
|
return [id, { servers }];
|
|
|
|
},
|
2021-06-04 12:37:34 -04:00
|
|
|
createBuffer(state, name, serverID, client) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let buf = State.getBuffer(state, { server: serverID, name });
|
2021-06-04 12:37:34 -04:00
|
|
|
if (buf) {
|
|
|
|
return [buf.id, null];
|
|
|
|
}
|
|
|
|
|
|
|
|
lastBufferID++;
|
2021-06-10 12:11:11 -04:00
|
|
|
let id = lastBufferID;
|
2021-06-04 12:37:34 -04:00
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let type;
|
2021-06-04 12:37:34 -04:00
|
|
|
if (name == SERVER_BUFFER) {
|
|
|
|
type = BufferType.SERVER;
|
|
|
|
} else if (client.isChannel(name)) {
|
|
|
|
type = BufferType.CHANNEL;
|
|
|
|
} else {
|
|
|
|
type = BufferType.NICK;
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:11:11 -04:00
|
|
|
let bufferList = Array.from(state.buffers.values());
|
2021-06-04 12:37:34 -04:00
|
|
|
bufferList.push({
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
type,
|
|
|
|
server: serverID,
|
|
|
|
serverInfo: null, // if server
|
2021-11-05 06:49:56 -04:00
|
|
|
joined: false, // if channel
|
2021-06-04 12:37:34 -04:00
|
|
|
topic: null, // if channel
|
|
|
|
members: new irc.CaseMapMap(null, client.cm), // if channel
|
|
|
|
messages: [],
|
|
|
|
unread: Unread.NONE,
|
2021-08-24 08:01:57 -04:00
|
|
|
prevReadReceipt: null,
|
2021-06-04 12:37:34 -04:00
|
|
|
});
|
|
|
|
bufferList = bufferList.sort(compareBuffers);
|
2021-06-10 12:11:11 -04:00
|
|
|
let buffers = new Map(bufferList.map((buf) => [buf.id, buf]));
|
2021-06-04 12:37:34 -04:00
|
|
|
return [id, { buffers }];
|
|
|
|
},
|
2022-02-04 08:22:50 -05:00
|
|
|
storeBouncerNetwork(state, id, attrs) {
|
|
|
|
let bouncerNetworks = new Map(state.bouncerNetworks);
|
|
|
|
bouncerNetworks.set(id, {
|
|
|
|
...bouncerNetworks.get(id),
|
|
|
|
...attrs,
|
|
|
|
});
|
|
|
|
return { bouncerNetworks };
|
|
|
|
},
|
|
|
|
deleteBouncerNetwork(state, id) {
|
|
|
|
let bouncerNetworks = new Map(state.bouncerNetworks);
|
|
|
|
bouncerNetworks.delete(id);
|
|
|
|
return { bouncerNetworks };
|
|
|
|
},
|
2021-06-04 12:27:21 -04:00
|
|
|
handleMessage(state, msg, serverID, client) {
|
|
|
|
function updateServer(updater) {
|
|
|
|
return State.updateServer(state, serverID, updater);
|
|
|
|
}
|
|
|
|
function updateBuffer(name, updater) {
|
|
|
|
return State.updateBuffer(state, { server: serverID, name }, updater);
|
|
|
|
}
|
2021-09-21 07:33:15 -04:00
|
|
|
function updateUser(name, updater) {
|
|
|
|
return updateServer((server) => {
|
|
|
|
let users = new irc.CaseMapMap(server.users);
|
|
|
|
let updated = updateState(users.get(name), updater);
|
|
|
|
if (!updated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
users.set(name, updated);
|
|
|
|
return { users };
|
|
|
|
});
|
|
|
|
}
|
2021-06-04 12:27:21 -04:00
|
|
|
|
2021-06-04 13:45:51 -04:00
|
|
|
// Don't update our internal state if it's a chat history message
|
|
|
|
if (irc.findBatchByType(msg, "chathistory")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-23 17:11:43 -04:00
|
|
|
let target, channel, topic, targets, who, update, buffers;
|
2021-06-04 12:27:21 -04:00
|
|
|
switch (msg.command) {
|
|
|
|
case irc.RPL_MYINFO:
|
|
|
|
// TODO: parse available modes
|
2021-06-10 12:11:11 -04:00
|
|
|
let serverInfo = {
|
2021-06-04 12:27:21 -04:00
|
|
|
name: msg.params[1],
|
|
|
|
version: msg.params[2],
|
|
|
|
};
|
|
|
|
return updateBuffer(SERVER_BUFFER, { serverInfo });
|
|
|
|
case irc.RPL_ISUPPORT:
|
2021-10-23 17:11:43 -04:00
|
|
|
buffers = new Map(state.buffers);
|
2021-06-04 12:27:21 -04:00
|
|
|
state.buffers.forEach((buf) => {
|
|
|
|
if (buf.server != serverID) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-10 12:11:11 -04:00
|
|
|
let members = new irc.CaseMapMap(buf.members, client.cm);
|
2021-06-04 12:27:21 -04:00
|
|
|
buffers.set(buf.id, { ...buf, members });
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
buffers,
|
2021-09-21 07:33:15 -04:00
|
|
|
...updateServer((server) => {
|
|
|
|
return {
|
2021-12-07 06:09:10 -05:00
|
|
|
name: client.isupport.network(),
|
|
|
|
cm: client.cm,
|
2021-09-21 07:33:15 -04:00
|
|
|
users: new irc.CaseMapMap(server.users, client.cm),
|
2021-12-07 06:09:10 -05:00
|
|
|
reliableUserAccounts: client.isupport.monitor() > 0 && client.isupport.whox(),
|
|
|
|
statusMsg: client.isupport.statusMsg(),
|
2021-12-07 07:37:14 -05:00
|
|
|
bouncerNetID: client.isupport.bouncerNetID(),
|
2021-09-21 07:33:15 -04:00
|
|
|
};
|
|
|
|
}),
|
2021-06-04 12:27:21 -04:00
|
|
|
};
|
2021-11-21 10:40:46 -05:00
|
|
|
case "CAP":
|
2021-11-16 07:32:54 -05:00
|
|
|
return updateServer({
|
|
|
|
supportsSASLPlain: client.supportsSASL("PLAIN"),
|
2021-12-10 09:34:51 -05:00
|
|
|
supportsAccountRegistration: client.caps.enabled.has("draft/account-registration"),
|
|
|
|
isBouncer: client.caps.enabled.has("soju.im/bouncer-networks"),
|
2021-11-16 07:32:54 -05:00
|
|
|
});
|
2021-11-21 06:13:44 -05:00
|
|
|
case irc.RPL_LOGGEDIN:
|
|
|
|
return updateServer({ account: msg.params[2] });
|
|
|
|
case irc.RPL_LOGGEDOUT:
|
|
|
|
return updateServer({ account: null });
|
2021-11-16 07:32:54 -05:00
|
|
|
case "REGISTER":
|
|
|
|
case "VERIFY":
|
|
|
|
if (msg.params[0] === "SUCCESS") {
|
|
|
|
return updateServer({ account: msg.params[1] });
|
|
|
|
}
|
|
|
|
break;
|
2021-06-04 12:27:21 -04:00
|
|
|
case irc.RPL_NOTOPIC:
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[1];
|
2021-06-04 12:27:21 -04:00
|
|
|
return updateBuffer(channel, { topic: null });
|
|
|
|
case irc.RPL_TOPIC:
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[1];
|
|
|
|
topic = msg.params[2];
|
2021-06-04 12:27:21 -04:00
|
|
|
return updateBuffer(channel, { topic });
|
|
|
|
case irc.RPL_TOPICWHOTIME:
|
|
|
|
// Ignore
|
|
|
|
break;
|
2021-10-23 14:05:07 -04:00
|
|
|
case irc.RPL_ENDOFNAMES:
|
|
|
|
channel = msg.params[1];
|
2021-06-04 12:27:21 -04:00
|
|
|
return updateBuffer(channel, (buf) => {
|
2021-10-23 14:05:07 -04:00
|
|
|
let members = new irc.CaseMapMap(null, buf.members.caseMap);
|
|
|
|
msg.list.forEach((namreply) => {
|
|
|
|
let membersList = namreply.params[3].split(" ");
|
|
|
|
membersList.forEach((s) => {
|
|
|
|
let member = irc.parseTargetPrefix(s);
|
|
|
|
members.set(member.name, member.prefix);
|
|
|
|
});
|
2021-06-04 12:27:21 -04:00
|
|
|
});
|
|
|
|
return { members };
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case irc.RPL_WHOREPLY:
|
2021-09-21 10:58:00 -04:00
|
|
|
case irc.RPL_WHOSPCRPL:
|
2021-09-21 11:24:13 -04:00
|
|
|
who = client.parseWhoReply(msg);
|
2021-09-21 10:58:00 -04:00
|
|
|
|
|
|
|
if (who.flags !== undefined) {
|
|
|
|
who.away = who.flags.indexOf("G") >= 0; // H for here, G for gone
|
2021-11-01 13:45:16 -04:00
|
|
|
who.operator = who.flags.indexOf("*") >= 0;
|
2021-09-21 10:58:00 -04:00
|
|
|
delete who.flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
who.offline = false;
|
|
|
|
|
2021-09-21 07:33:15 -04:00
|
|
|
return updateUser(who.nick, who);
|
2021-06-04 12:27:21 -04:00
|
|
|
case irc.RPL_ENDOFWHO:
|
2021-06-11 06:44:14 -04:00
|
|
|
target = msg.params[1];
|
2021-11-10 04:32:23 -05:00
|
|
|
if (msg.list.length == 0 && !client.isChannel(target) && target.indexOf("*") < 0) {
|
2021-06-04 12:27:21 -04:00
|
|
|
// Not a channel nor a mask, likely a nick
|
2021-09-21 07:33:15 -04:00
|
|
|
return updateUser(target, (user) => {
|
2021-06-04 12:27:21 -04:00
|
|
|
return { offline: true };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2021-06-04 12:53:06 -04:00
|
|
|
case "JOIN":
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[0];
|
2021-06-04 12:53:06 -04:00
|
|
|
|
|
|
|
if (client.isMyNick(msg.prefix.name)) {
|
2021-06-10 12:11:11 -04:00
|
|
|
let [id, update] = State.createBuffer(state, channel, serverID, client);
|
2021-06-04 12:53:06 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
}
|
|
|
|
|
2021-10-23 17:11:43 -04:00
|
|
|
update = updateBuffer(channel, (buf) => {
|
2021-06-10 12:11:11 -04:00
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
2021-06-04 12:53:06 -04:00
|
|
|
members.set(msg.prefix.name, "");
|
2021-11-05 06:49:56 -04:00
|
|
|
|
|
|
|
let joined = buf.joined || client.isMyNick(msg.prefix.name);
|
|
|
|
|
|
|
|
return { members, joined };
|
2021-06-04 12:53:06 -04:00
|
|
|
});
|
2021-09-21 08:12:07 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
|
|
|
|
who = { nick: msg.prefix.name, offline: false };
|
|
|
|
if (msg.prefix.user) {
|
|
|
|
who.username = msg.prefix.user;
|
|
|
|
}
|
|
|
|
if (msg.prefix.host) {
|
|
|
|
who.hostname = msg.prefix.host;
|
|
|
|
}
|
2021-09-21 08:29:31 -04:00
|
|
|
if (msg.params.length > 2) {
|
|
|
|
who.account = msg.params[1];
|
|
|
|
if (who.account === "*") {
|
|
|
|
who.account = null;
|
|
|
|
}
|
|
|
|
who.realname = msg.params[2];
|
|
|
|
}
|
2021-09-21 08:12:07 -04:00
|
|
|
update = updateUser(msg.prefix.name, who);
|
|
|
|
state = { ...state, ...update };
|
|
|
|
|
|
|
|
return state;
|
2021-06-04 12:53:06 -04:00
|
|
|
case "PART":
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[0];
|
2021-06-04 12:53:06 -04:00
|
|
|
|
|
|
|
return updateBuffer(channel, (buf) => {
|
2021-06-10 12:11:11 -04:00
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
2021-06-04 12:53:06 -04:00
|
|
|
members.delete(msg.prefix.name);
|
2021-11-05 06:49:56 -04:00
|
|
|
|
|
|
|
let joined = buf.joined && !client.isMyNick(msg.prefix.name);
|
|
|
|
|
|
|
|
return { members, joined };
|
2021-06-04 12:53:06 -04:00
|
|
|
});
|
2021-06-04 12:27:21 -04:00
|
|
|
case "KICK":
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[0];
|
|
|
|
let nick = msg.params[1];
|
2021-06-04 12:27:21 -04:00
|
|
|
|
|
|
|
return updateBuffer(channel, (buf) => {
|
2021-06-10 12:11:11 -04:00
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
2021-06-04 12:27:21 -04:00
|
|
|
members.delete(nick);
|
2021-12-04 10:52:38 -05:00
|
|
|
|
|
|
|
let joined = buf.joined && !client.isMyNick(nick);
|
|
|
|
|
|
|
|
return { members, joined };
|
2021-06-04 12:27:21 -04:00
|
|
|
});
|
2021-09-21 08:00:52 -04:00
|
|
|
case "QUIT":
|
2021-10-23 17:11:43 -04:00
|
|
|
buffers = new Map(state.buffers);
|
|
|
|
state.buffers.forEach((buf) => {
|
|
|
|
if (buf.server != serverID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!buf.members.has(msg.prefix.name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
|
|
|
members.delete(msg.prefix.name);
|
|
|
|
buffers.set(buf.id, { ...buf, members });
|
|
|
|
});
|
|
|
|
state = { ...state, buffers };
|
|
|
|
|
|
|
|
update = updateUser(msg.prefix.name, (user) => {
|
2021-09-21 08:00:52 -04:00
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return { offline: true };
|
|
|
|
});
|
2021-10-23 17:11:43 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
|
|
|
|
return state;
|
2021-09-21 08:00:52 -04:00
|
|
|
case "NICK":
|
|
|
|
let newNick = msg.params[0];
|
2021-10-23 17:11:43 -04:00
|
|
|
|
|
|
|
buffers = new Map(state.buffers);
|
|
|
|
state.buffers.forEach((buf) => {
|
|
|
|
if (buf.server != serverID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!buf.members.has(msg.prefix.name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
|
|
|
members.set(newNick, members.get(msg.prefix.name));
|
|
|
|
members.delete(msg.prefix.name);
|
|
|
|
buffers.set(buf.id, { ...buf, members });
|
|
|
|
});
|
|
|
|
state = { ...state, buffers };
|
|
|
|
|
|
|
|
update = updateServer((server) => {
|
2021-09-21 08:00:52 -04:00
|
|
|
let users = new irc.CaseMapMap(server.users);
|
|
|
|
let user = users.get(msg.prefix.name);
|
|
|
|
if (!user) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
users.set(newNick, user);
|
|
|
|
users.delete(msg.prefix.name);
|
|
|
|
return { users };
|
|
|
|
});
|
2021-10-23 17:11:43 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
|
|
|
|
return state;
|
2021-06-04 12:27:21 -04:00
|
|
|
case "SETNAME":
|
2021-09-21 07:33:15 -04:00
|
|
|
return updateUser(msg.prefix.name, { realname: msg.params[0] });
|
2021-09-06 05:20:33 -04:00
|
|
|
case "CHGHOST":
|
2021-09-21 07:33:15 -04:00
|
|
|
return updateUser(msg.prefix.name, {
|
|
|
|
username: msg.params[0],
|
|
|
|
hostname: msg.params[1],
|
2021-09-06 05:20:33 -04:00
|
|
|
});
|
2021-09-21 08:49:32 -04:00
|
|
|
case "ACCOUNT":
|
|
|
|
let account = msg.params[0];
|
|
|
|
if (account === "*") {
|
|
|
|
account = null;
|
|
|
|
}
|
|
|
|
return updateUser(msg.prefix.name, { account });
|
2021-06-04 12:27:21 -04:00
|
|
|
case "AWAY":
|
2021-06-10 12:11:11 -04:00
|
|
|
let awayMessage = msg.params[0];
|
2021-09-21 07:33:15 -04:00
|
|
|
return updateUser(msg.prefix.name, { away: !!awayMessage });
|
2021-06-04 12:57:02 -04:00
|
|
|
case "TOPIC":
|
2021-06-10 12:11:11 -04:00
|
|
|
channel = msg.params[0];
|
|
|
|
topic = msg.params[1];
|
2021-06-04 12:57:02 -04:00
|
|
|
return updateBuffer(channel, { topic });
|
2021-06-11 06:44:14 -04:00
|
|
|
case "MODE":
|
|
|
|
target = msg.params[0];
|
|
|
|
|
|
|
|
if (!client.isChannel(target)) {
|
|
|
|
return; // TODO: handle user mode changes too
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:37:33 -05:00
|
|
|
let prefix = client.isupport.prefix();
|
2021-06-11 06:44:14 -04:00
|
|
|
let prefixByMode = new Map(irc.parseMembershipModes(prefix).map((membership) => {
|
|
|
|
return [membership.mode, membership.prefix];
|
|
|
|
}));
|
|
|
|
|
|
|
|
return updateBuffer(target, (buf) => {
|
|
|
|
let members = new irc.CaseMapMap(buf.members);
|
|
|
|
|
2021-06-11 06:54:42 -04:00
|
|
|
irc.forEachChannelModeUpdate(msg, client.isupport, (mode, add, arg) => {
|
2021-06-11 06:44:14 -04:00
|
|
|
if (prefixByMode.has(mode)) {
|
|
|
|
let nick = arg;
|
|
|
|
let membership = members.get(nick);
|
|
|
|
let letter = prefixByMode.get(mode);
|
|
|
|
members.set(nick, updateMembership(membership, letter, add, client));
|
|
|
|
}
|
2021-06-11 06:54:42 -04:00
|
|
|
});
|
2021-06-11 06:44:14 -04:00
|
|
|
|
|
|
|
return { members };
|
|
|
|
});
|
2021-08-24 06:53:46 -04:00
|
|
|
case irc.RPL_MONONLINE:
|
|
|
|
targets = msg.params[1].split(",");
|
|
|
|
|
|
|
|
for (let target of targets) {
|
|
|
|
let prefix = irc.parsePrefix(target);
|
2021-09-21 07:33:15 -04:00
|
|
|
let update = updateUser(prefix.name, { offline: false });
|
2021-08-24 06:53:46 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
case irc.RPL_MONOFFLINE:
|
|
|
|
targets = msg.params[1].split(",");
|
|
|
|
|
|
|
|
for (let target of targets) {
|
|
|
|
let prefix = irc.parsePrefix(target);
|
2021-09-21 07:33:15 -04:00
|
|
|
let update = updateUser(prefix.name, { offline: true });
|
2021-08-24 06:53:46 -04:00
|
|
|
state = { ...state, ...update };
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2021-06-04 12:27:21 -04:00
|
|
|
}
|
|
|
|
},
|
2021-06-04 13:07:14 -04:00
|
|
|
addMessage(state, msg, bufID) {
|
2021-06-11 04:58:09 -04:00
|
|
|
lastMessageKey++;
|
|
|
|
msg.key = lastMessageKey;
|
|
|
|
|
2021-06-04 13:07:14 -04:00
|
|
|
return State.updateBuffer(state, bufID, (buf) => {
|
2021-06-10 12:11:11 -04:00
|
|
|
let messages = insertMessage(buf.messages, msg);
|
2021-06-04 13:07:14 -04:00
|
|
|
return { messages };
|
|
|
|
});
|
|
|
|
},
|
2021-06-04 12:03:03 -04:00
|
|
|
};
|