1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/internal/disconnect.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-06-23 00:32:51 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
* @summary Disconnection handler
* @version 1.0.0
* @description The server invokes this module each time a websocket connection is disconnected
* @module disconnect
*/
2018-06-04 02:08:35 +08:00
2022-06-23 00:32:51 +08:00
import {
socketInChannel,
} from '../utility/_Channels.js';
/**
* Executes when invoked by a remote client
* @param {Object} env - Enviroment object with references to core, server, socket & payload
* @public
* @return {void}
*/
2020-09-17 13:44:32 +08:00
export async function run({ server, socket, payload }) {
if (payload.cmdKey !== server.cmdKey) {
2018-06-04 02:08:35 +08:00
// internal command attempt by client, increase rate limit chance and ignore
2019-11-07 15:35:23 +08:00
return server.police.frisk(socket.address, 20);
2018-06-04 02:08:35 +08:00
}
2018-06-04 15:07:24 +08:00
// send leave notice to client peers
2022-06-23 00:32:51 +08:00
// @todo Multichannel update
2018-06-04 02:08:35 +08:00
if (socket.channel) {
2022-06-23 00:32:51 +08:00
const isDuplicate = socketInChannel(server, socket.channel, socket);
if (isDuplicate === false) {
server.broadcast({
cmd: 'onlineRemove',
nick: socket.nick,
}, { channel: socket.channel });
}
2018-06-04 02:08:35 +08:00
}
2018-06-04 15:07:24 +08:00
// commit close just in case
2018-06-04 02:08:35 +08:00
socket.terminate();
2019-11-07 15:35:23 +08:00
return true;
}
2022-06-23 00:32:51 +08:00
/**
* The following payload properties are required to invoke this module:
* "cmdKey"
* @public
* @typedef {Array} disconnect/requiredData
*/
2019-11-07 15:35:23 +08:00
export const requiredData = ['cmdKey'];
2022-06-23 00:32:51 +08:00
/**
* Module meta information
* @public
* @typedef {Object} disconnect/info
* @property {string} name - Module command name
* @property {string} category - Module category name
* @property {string} description - Information about module
* @property {string} usage - Information about module usage
*/
2019-11-07 15:35:23 +08:00
export const info = {
2018-06-04 02:08:35 +08:00
name: 'disconnect',
2022-06-23 00:32:51 +08:00
category: 'internal',
description: 'Internally used to relay disconnect events to clients',
2018-06-04 02:08:35 +08:00
usage: 'Internal Use Only',
};