Skip to content

Instantly share code, notes, and snippets.

@H7-25
Created February 16, 2021 22:08
Show Gist options
  • Save H7-25/f19e194caefe9de473d4c23fbc690ba3 to your computer and use it in GitHub Desktop.
Save H7-25/f19e194caefe9de473d4c23fbc690ba3 to your computer and use it in GitHub Desktop.
module.exports.init = async function init(hooks, app) {
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
hooks.on('client_disconnected', async (event) => {
const con = event.client;
const user = await app.db.factories.User.query().where('id', con.state.authUserId).first();
const networks = await con.userDb.getUserNetworks(con.state.authUserId);
networks.forEach((net) => {
if (app.cons.findAllUsersClients(user.id).length === 1) {
let upstream = null;
upstream = con.conDict.findUsersOutgoingConnection(con.state.authUserId, net.id);
// wait 10 minutes before close bouncer connection to IRC
await sleep(600000);
// check if meanwhile any client has connected to the bouncer
if (app.cons.findAllUsersClients(user.id).length === 0) {
if (upstream && upstream.state.connected) {
let quitMessage = 'Bye Bye';
upstream.writeLine('QUIT', quitMessage);
upstream.close();
}
} else {
return;
}
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment