Add support for the soju.im/bouncer-networks extension

This commit is contained in:
Simon Ser 2021-01-22 21:01:03 +01:00
parent 982a945932
commit b429243573
3 changed files with 69 additions and 15 deletions

View File

@ -160,6 +160,7 @@ export default class App extends Component {
}, },
networks: new Map(), networks: new Map(),
buffers: new Map(), buffers: new Map(),
bouncerNetworks: new Map(),
activeBuffer: null, activeBuffer: null,
dialog: null, dialog: null,
error: null, error: null,
@ -541,6 +542,15 @@ export default class App extends Component {
var client = this.clients.get(netID); var client = this.clients.get(netID);
switch (msg.command) { switch (msg.command) {
case irc.RPL_WELCOME: case irc.RPL_WELCOME:
if (client.enabledCaps["soju.im/bouncer-networks"] && !client.params.bouncerNetwork) {
client.listBouncerNetworks().then((bouncerNetworks) => {
this.setState((state) => {
return { bouncerNetworks };
});
this.openSecondaryClients(client, bouncerNetworks);
});
}
if (this.state.connectParams.autojoin.length > 0) { if (this.state.connectParams.autojoin.length > 0) {
client.send({ client.send({
command: "JOIN", command: "JOIN",
@ -735,6 +745,7 @@ export default class App extends Component {
case "AUTHENTICATE": case "AUTHENTICATE":
case "PING": case "PING":
case "BATCH": case "BATCH":
case "BOUNCER":
// Ignore these // Ignore these
break; break;
default: default:
@ -742,6 +753,15 @@ export default class App extends Component {
} }
} }
openSecondaryClients(client, bouncerNetworks) {
bouncerNetworks.forEach((attrs, id) => {
this.connect({
...client.params,
bouncerNetwork: id,
});
});
}
handleConnectSubmit(connectParams) { handleConnectSubmit(connectParams) {
this.setState({ error: null }); this.setState({ error: null });
@ -1053,6 +1073,7 @@ export default class App extends Component {
<${BufferList} <${BufferList}
buffers=${this.state.buffers} buffers=${this.state.buffers}
networks=${this.state.networks} networks=${this.state.networks}
bouncerNetworks=${this.state.bouncerNetworks}
activeBuffer=${this.state.activeBuffer} activeBuffer=${this.state.activeBuffer}
onBufferClick=${this.handleBufferListClick} onBufferClick=${this.handleBufferListClick}
/> />

View File

@ -2,13 +2,12 @@ import * as irc from "../lib/irc.js";
import { html, Component } from "../lib/index.js"; import { html, Component } from "../lib/index.js";
import { BufferType, Unread, getBufferURL } from "../state.js"; import { BufferType, Unread, getBufferURL } from "../state.js";
function getNetworkName(network) { function getNetworkName(network, bouncerNetwork, bouncer) {
var bouncerStr = network.isupport.get("BOUNCER"); if (bouncerNetwork && bouncerNetwork.name) {
if (bouncerStr) { return bouncerNetwork.name;
var bouncerProps = irc.parseTags(bouncerStr); }
if (bouncerProps["network"]) { if (bouncer) {
return bouncerProps["network"]; return "bouncer";
}
} }
var netName = network.isupport.get("NETWORK"); var netName = network.isupport.get("NETWORK");
@ -27,7 +26,7 @@ function BufferItem(props) {
var name = props.buffer.name; var name = props.buffer.name;
if (props.buffer.type == BufferType.SERVER) { if (props.buffer.type == BufferType.SERVER) {
name = getNetworkName(props.network); name = getNetworkName(props.network, props.bouncerNetwork, props.bouncer);
} }
var activeClass = props.active ? "active" : ""; var activeClass = props.active ? "active" : "";
@ -46,11 +45,22 @@ function BufferItem(props) {
export default function BufferList(props) { export default function BufferList(props) {
return html` // TODO: check bouncer-networks cap instead
<ul> var bouncer = props.bouncerNetworks.size > 0;
${Array.from(props.buffers.values()).map((buf) => html`
<${BufferItem} key=${buf.id} buffer=${buf} network=${props.networks.get(buf.network)} onClick=${() => props.onBufferClick(buf)} active=${props.activeBuffer == buf.id}/> var items = Array.from(props.buffers.values()).map((buf) => {
`)} var network = props.networks.get(buf.network);
</ul>
`; var bouncerNetwork = null;
var bouncerNetID = network.isupport.get("BOUNCER_NETID");
if (bouncerNetID) {
bouncerNetwork = props.bouncerNetworks.get(bouncerNetID);
}
return html`
<${BufferItem} key=${buf.id} buffer=${buf} network=${network} bouncer=${bouncer} bouncerNetwork=${bouncerNetwork} onClick=${() => props.onBufferClick(buf)} active=${props.activeBuffer == buf.id}/>
`;
});
return html`<ul>${items}</ul>`;
} }

View File

@ -10,6 +10,7 @@ const permanentCaps = [
"message-tags", "message-tags",
"multi-prefix", "multi-prefix",
"server-time", "server-time",
"soju.im/bouncer-networks",
]; ];
const RECONNECT_DELAY_SEC = 10; const RECONNECT_DELAY_SEC = 10;
@ -37,6 +38,7 @@ export default class Client extends EventTarget {
nick: null, nick: null,
pass: null, pass: null,
saslPlain: null, saslPlain: null,
bouncerNetwork: null,
}; };
batches = new Map(); batches = new Map();
autoReconnect = true; autoReconnect = true;
@ -178,6 +180,9 @@ export default class Client extends EventTarget {
case irc.RPL_SASLSUCCESS: case irc.RPL_SASLSUCCESS:
console.log("SASL authentication success"); console.log("SASL authentication success");
if (this.status != Client.Status.REGISTERED) { if (this.status != Client.Status.REGISTERED) {
if (this.enabledCaps["soju.im/bouncer-networks"] && this.params.bouncerNetwork) {
this.send({ command: "BOUNCER", params: ["BIND", this.params.bouncerNetwork] });
}
this.send({ command: "CAP", params: ["END"] }); this.send({ command: "CAP", params: ["END"] });
} }
break; break;
@ -454,4 +459,22 @@ export default class Client extends EventTarget {
return null; return null;
}); });
} }
listBouncerNetworks() {
if (!this.enabledCaps["soju.im/bouncer-networks"]) {
return Promise.reject(new Error("Server doesn't support the BOUNCER extension"));
}
var req = { command: "BOUNCER", params: ["LISTNETWORKS"] };
return this.fetchBatch(req, "bouncer-networks").then((batch) => {
var networks = new Map();
for (var msg of batch.messages) {
console.assert(msg.command === "BOUNCER" && msg.params[0] === "NETWORK");
var id = msg.params[1];
var params = irc.parseTags(msg.params[2]);
networks.set(id, params);
}
return networks;
});
}
} }