channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
/* eslint import/no-cycle: [0, { ignoreExternal: true }] */
|
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
/**
|
|
|
|
* User Account Control information containing level constants
|
|
|
|
* and simple helper functions related to users
|
|
|
|
* @property {Object} levels - Defines labels for default permission ranges
|
|
|
|
* @author MinusGix ( https://github.com/MinusGix )
|
|
|
|
* @version v1.0.0
|
2022-06-23 00:32:51 +08:00
|
|
|
* @module UAC
|
2020-09-22 13:34:30 +08:00
|
|
|
*/
|
|
|
|
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
import {
|
|
|
|
createHash,
|
|
|
|
} from 'node:crypto';
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
getChannelSettings,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from './_Channels.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Object defining labels for default permission ranges
|
|
|
|
* @typedef {Object} levels
|
|
|
|
* @property {number} admin Global administrator range
|
|
|
|
* @property {number} moderator Global moderator range
|
|
|
|
* @property {number} channelOwner Local administrator range
|
|
|
|
* @property {number} channelModerator Local moderator range
|
|
|
|
* @property {number} channelTrusted Local (non-public) channel trusted
|
|
|
|
* @property {number} trustedUser Public channel trusted
|
|
|
|
* @property {number} default Default user level
|
|
|
|
*/
|
|
|
|
export const levels = {
|
|
|
|
admin: 9999999,
|
|
|
|
moderator: 999999,
|
|
|
|
|
|
|
|
channelOwner: 99999,
|
|
|
|
channelModerator: 9999,
|
|
|
|
channelTrusted: 8999,
|
|
|
|
|
|
|
|
trustedUser: 500,
|
|
|
|
default: 100,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than the global admin level
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isAdmin(level) {
|
|
|
|
return level >= levels.admin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than the global moderator level
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isModerator(level) {
|
|
|
|
return level >= levels.moderator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than the channel owner level
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isChannelOwner(level) {
|
|
|
|
return level >= levels.channelOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than the channel moderator level
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isChannelModerator(level) {
|
|
|
|
return level >= levels.channelModerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than the channel trust level
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isChannelTrusted(level) {
|
|
|
|
return level >= levels.channelTrusted;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if target level is equal or greater than a trusted user
|
|
|
|
* @public
|
|
|
|
* @param {number} level Level to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function isTrustedUser(level) {
|
|
|
|
return level >= levels.trustedUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an object containing public information about the socket
|
|
|
|
* @public
|
|
|
|
* @param {WebSocket} socket Target client
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
export function getUserDetails(socket) {
|
|
|
|
return {
|
|
|
|
nick: socket.nick,
|
2020-11-07 05:16:43 +08:00
|
|
|
trip: socket.trip || '',
|
2023-12-22 15:14:03 +08:00
|
|
|
uType: socket.uType,
|
2020-09-22 13:34:30 +08:00
|
|
|
hash: socket.hash,
|
|
|
|
level: socket.level,
|
|
|
|
userid: socket.userid,
|
2020-11-07 05:16:43 +08:00
|
|
|
isBot: socket.isBot,
|
|
|
|
color: socket.color,
|
2022-06-23 00:32:51 +08:00
|
|
|
online: true,
|
2020-09-22 13:34:30 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the nickname is valid
|
|
|
|
* @public
|
|
|
|
* @param {string} nick Nickname to verify
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
export function verifyNickname(nick) {
|
|
|
|
if (typeof nick === 'undefined') return false;
|
|
|
|
|
|
|
|
return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashes a user's password, returning a trip code
|
|
|
|
* or a blank string
|
|
|
|
* @public
|
|
|
|
* @param {string} pass User's password
|
2022-06-23 00:32:51 +08:00
|
|
|
* @param {buffer} salt Server salt data
|
2020-09-22 13:34:30 +08:00
|
|
|
* @param {string} config Server config object
|
|
|
|
* @param {string} channel Channel-level permissions check
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2022-06-23 00:32:51 +08:00
|
|
|
export function getUserPerms(pass, salt, config, channel) {
|
2020-09-22 13:34:30 +08:00
|
|
|
if (!pass) {
|
|
|
|
return {
|
|
|
|
trip: '',
|
|
|
|
level: levels.default,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
const trip = createHash('sha256').update(pass + salt, 'utf8').digest('base64').substr(0, 6);
|
2020-09-22 13:34:30 +08:00
|
|
|
|
|
|
|
// check if user is global admin
|
|
|
|
if (trip === config.adminTrip) {
|
|
|
|
return {
|
|
|
|
trip: 'Admin',
|
|
|
|
level: levels.admin,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
let level = levels.default;
|
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
// check if user is global mod
|
2022-06-23 00:32:51 +08:00
|
|
|
config.globalMods.forEach((mod) => { // eslint-disable-line consistent-return
|
2020-09-22 13:34:30 +08:00
|
|
|
if (trip === mod.trip) {
|
2020-11-07 05:16:43 +08:00
|
|
|
level = levels.moderator;
|
2020-09-22 13:34:30 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const channelSettings = getChannelSettings(config, channel);
|
|
|
|
if (channelSettings.owned) {
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
if (channelSettings.ownerTrip === trip) {
|
|
|
|
level = levels.channelOwner;
|
|
|
|
} else if (typeof channelSettings.tripLevels[trip] !== 'undefined') {
|
|
|
|
level = channelSettings.tripLevels[trip];
|
|
|
|
}
|
2020-09-22 13:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
trip,
|
2020-11-07 05:16:43 +08:00
|
|
|
level,
|
2020-09-22 13:34:30 +08:00
|
|
|
};
|
|
|
|
}
|