From 61cd2af6e702fc21ee8786bf6d948411735c4223 Mon Sep 17 00:00:00 2001 From: marzavec Date: Fri, 5 Jan 2024 10:38:54 -0800 Subject: [PATCH] added unclaimchannel --- commands/channels/claimchannel.js | 14 +-- commands/channels/setlevel.js | 14 +-- commands/channels/unclaimchannel.js | 147 ++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 18 deletions(-) create mode 100644 commands/channels/unclaimchannel.js diff --git a/commands/channels/claimchannel.js b/commands/channels/claimchannel.js index cab7e19..6dc2454 100644 --- a/commands/channels/claimchannel.js +++ b/commands/channels/claimchannel.js @@ -28,7 +28,7 @@ import { * @return {void} */ export async function run({ - core, server, socket, payload, + core, server, socket, }) { // must be in a channel to run this command if (typeof socket.channel === 'undefined') { @@ -38,7 +38,7 @@ export async function run({ if (!socket.trip) { return server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` - text: 'Failed to take ownership: Invalid trip. Refer to `/help claimchannel` for instructions on how to use this command.', + text: 'Failed to take ownership: Missing trip code.', channel: socket.channel, // @todo Multichannel }, socket); } @@ -46,7 +46,7 @@ export async function run({ if (isModerator(socket.level)) { return server.reply({ cmd: 'info', // @todo Add numeric error code as `id` - text: "You're already a global moderator; it's free real estate. . .", + text: "Failed to take ownership: You're already a global moderator; it's free real estate. . .", channel: socket.channel, // @todo Multichannel }, socket); } @@ -56,7 +56,7 @@ export async function run({ if (channelSettings.owned) { return server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` - text: `This channel is already owned by the trip "${channelSettings.ownerTrip}", until ${channelSettings.claimExpires}`, + text: `Failed to take ownership: This channel is already owned by the trip "${channelSettings.ownerTrip}", until ${channelSettings.claimExpires}`, channel: socket.channel, // @todo Multichannel }, socket); } @@ -68,13 +68,13 @@ export async function run({ server.reply({ cmd: 'warn', text: 'Enter the following to take ownership (case-sensitive):', - channel: payload.channel, // @todo Multichannel + channel: socket.channel, // @todo Multichannel }, socket); server.reply({ cmd: 'captcha', text: captcha.word2Transformedstr(socket.claimCaptcha.solution), - channel: payload.channel, // @todo Multichannel + channel: socket.channel, // @todo Multichannel }, socket); return true; @@ -112,7 +112,7 @@ export function chatHook({ if (channelSettings.owned) { return server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` - text: `This channel is already owned by the trip "${channelSettings.ownerTrip}", until ${channelSettings.claimExpires}`, + text: `Failed to take ownership: This channel is already owned by the trip "${channelSettings.ownerTrip}", until ${channelSettings.claimExpires}`, channel: socket.channel, // @todo Multichannel }, socket); } diff --git a/commands/channels/setlevel.js b/commands/channels/setlevel.js index 9b12057..999249c 100644 --- a/commands/channels/setlevel.js +++ b/commands/channels/setlevel.js @@ -47,15 +47,7 @@ export async function run({ }, socket); } - if (typeof payload.level !== 'string') { - return server.reply({ - cmd: 'warn', // @todo Add numeric error code as `id` - text: 'Failed to set level: Invalid level label. Refer to `/help setlevel` for instructions on how to use this command.', - channel: socket.channel, // @todo Multichannel - }, socket); - } - - if (core.levelLabels.indexOf(payload.level) === -1) { + if (typeof payload.level !== 'string' || core.levelLabels.indexOf(payload.level) === -1) { return server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` text: `Failed to set level: Invalid level label; choices are case sensitive: ${core.levelLabels.join(', ')}`, @@ -141,7 +133,7 @@ export function setlevelCheck({ if (!input[1]) { server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` - text: 'Failed to set level: Invalid trip. Refer to `/help setlevel` for instructions on how to use this command.', + text: 'Failed to set level: Missing trip. Refer to `/help setlevel` for instructions on how to use this command.', channel: socket.channel, // @todo Multichannel }, socket); @@ -152,7 +144,7 @@ export function setlevelCheck({ if (!input[2]) { server.reply({ cmd: 'warn', // @todo Add numeric error code as `id` - text: 'Failed to set level: Invalid level label. Refer to `/help setlevel` for instructions on how to use this command.', + text: 'Failed to set level: Missing level label. Refer to `/help setlevel` for instructions on how to use this command.', channel: socket.channel, // @todo Multichannel }, socket); diff --git a/commands/channels/unclaimchannel.js b/commands/channels/unclaimchannel.js new file mode 100644 index 0000000..4e2c0f8 --- /dev/null +++ b/commands/channels/unclaimchannel.js @@ -0,0 +1,147 @@ +/** + * @author Marzavec + * @summary Release channel ownership + * @version 1.0.0 + * @description Clear ownership info and channel settings + * @module unclaimchannel + */ + +import { + isModerator, + getUserDetails, + levels, +} from '../utility/_UAC.js'; +import { + // Errors, + DefaultChannelSettings, +} from '../utility/_Constants.js'; +import { + getChannelSettings, + updateChannelSettings, +} from '../utility/_Channels.js'; + +/** + * Executes when invoked by a remote client + * @param {Object} env - Environment object with references to core, server, socket & payload + * @public + * @return {void} + */ +export async function run({ + core, server, socket, +}) { + // must be in a channel to run this command + if (typeof socket.channel === 'undefined') { + return server.police.frisk(socket, 10); + } + + if (!socket.trip) { + return server.reply({ + cmd: 'warn', // @todo Add numeric error code as `id` + text: 'Failed to release ownership: Missing trip code.', + channel: socket.channel, // @todo Multichannel + }, socket); + } + + const channelSettings = getChannelSettings(core.appConfig.data, socket.channel); + + if (channelSettings.owned === false) { + return server.reply({ + cmd: 'info', // @todo Add numeric error code as `id` + text: 'Failed to release ownership: That which is not owned may not be unowned, and with strange aeons. . .', + channel: socket.channel, // @todo Multichannel + }, socket); + } + + if (channelSettings.ownerTrip !== socket.trip && !isModerator(socket.level)) { + return server.reply({ + cmd: 'warn', // @todo Add numeric error code as `id` + text: 'Failed to release ownership: Wrong trip code.', + channel: socket.channel, // @todo Multichannel + }, socket); + } + + updateChannelSettings(core.appConfig.data, socket.channel, DefaultChannelSettings); + + server.broadcast({ + cmd: 'info', + text: 'Channel ownership has been removed and the channel settings have been reset', + channel: socket.channel, + }, { channel: socket.channel }); + + const targetClients = server.findSockets({ + channel: socket.channel, + }); + + for (let i = 0, j = targetClients.length; i < j; i += 1) { + if (!isModerator(targetClients[i].level)) { + targetClients[i].level = levels.default; + + server.broadcast({ + ...getUserDetails(targetClients[i]), + ...{ + cmd: 'updateUser', + channel: socket.channel, + }, + }, { channel: socket.channel }); + } + } + + return true; +} + +/** + * Automatically executes once after server is ready to register this modules hooks + * @param {Object} server - Reference to server environment object + * @public + * @return {void} + */ +export function initHooks(server) { + server.registerHook('in', 'chat', this.chatHook.bind(this), 26); +} + +/** + * Executes every time an incoming chat command is invoked + * @param {Object} env - Environment object with references to core, server, socket & payload + * @public + * @return {{Object|boolean|string}} Object = same/new payload, false = suppress, string = error + */ +export function chatHook({ + core, server, socket, payload, +}) { + if (typeof payload.text !== 'string') { + return false; + } + + if (payload.text.startsWith('/unclaimchannel')) { + this.run({ + core, + server, + socket, + payload: { + cmd: 'unclaimchannel', + }, + }); + + return false; + } + + return payload; +} + +/** + * Module meta information + * @public + * @typedef {Object} unclaimchannel/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 + */ +export const info = { + name: 'unclaimchannel', + category: 'channels', + description: 'Clear ownership info and channel settings', + usage: ` + API: { cmd: 'unclaimchannel' } + Text: /unclaimchannel`, +};