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

Deduplicate verifyNickname into a single importable function

This commit is contained in:
MinusGix 2020-03-06 14:41:09 -06:00
parent da9cbe3dbb
commit eec99669c8
5 changed files with 19 additions and 12 deletions

View File

@ -2,8 +2,7 @@
Description: Allows calling client to change their current nickname Description: Allows calling client to change their current nickname
*/ */
// module support functions import * as UAC from "../utility/UAC/_info";
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
// module main // module main
export async function run(core, server, socket, data) { export async function run(core, server, socket, data) {
@ -21,7 +20,7 @@ export async function run(core, server, socket, data) {
// make sure requested nickname meets standards // make sure requested nickname meets standards
const newNick = data.nick.trim(); const newNick = data.nick.trim();
if (!verifyNickname(newNick)) { if (!UAC.verifyNickname(newNick)) {
return server.reply({ return server.reply({
cmd: 'warn', cmd: 'warn',
text: 'Nickname must consist of up to 24 letters, numbers, and underscores', text: 'Nickname must consist of up to 24 letters, numbers, and underscores',

View File

@ -2,8 +2,7 @@
Description: Generates a semi-unique channel name then broadcasts it to each client Description: Generates a semi-unique channel name then broadcasts it to each client
*/ */
// module support functions import * as UAC from "../utility/UAC/_info";
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
// module main // module main
export async function run(core, server, socket, data) { export async function run(core, server, socket, data) {
@ -16,7 +15,7 @@ export async function run(core, server, socket, data) {
} }
// verify user input // verify user input
if (typeof data.nick !== 'string' || !verifyNickname(data.nick)) { if (typeof data.nick !== 'string' || !UAC.verifyNickname(data.nick)) {
return true; return true;
} }

View File

@ -13,8 +13,6 @@ const hash = (password) => {
return sha.digest('base64').substr(0, 6); return sha.digest('base64').substr(0, 6);
}; };
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
// exposed "login" function to allow hooks to verify user join events // exposed "login" function to allow hooks to verify user join events
// returns object containing user info or string if error // returns object containing user info or string if error
export function parseNickname(core, data) { export function parseNickname(core, data) {
@ -29,7 +27,7 @@ export function parseNickname(core, data) {
const nickArray = data.nick.split('#', 2); const nickArray = data.nick.split('#', 2);
userInfo.nick = nickArray[0].trim(); userInfo.nick = nickArray[0].trim();
if (!verifyNickname(userInfo.nick)) { if (!UAC.verifyNickname(userInfo.nick)) {
// return error as string // return error as string
return 'Nickname must consist of up to 24 letters, numbers, and underscores'; return 'Nickname must consist of up to 24 letters, numbers, and underscores';
} }

View File

@ -2,8 +2,9 @@
Description: Display text on targets screen that only they can see Description: Display text on targets screen that only they can see
*/ */
import * as UAC from "../utility/UAC/_info";
// module support functions // module support functions
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
const parseText = (text) => { const parseText = (text) => {
// verifies user input is text // verifies user input is text
@ -41,7 +42,7 @@ export async function run(core, server, socket, payload) {
} }
const targetNick = payload.nick; const targetNick = payload.nick;
if (!verifyNickname(targetNick)) { if (!UAC.verifyNickname(targetNick)) {
return true; return true;
} }

View File

@ -1,6 +1,6 @@
/** /**
* User Account Control information containing level constants * User Account Control information containing level constants
* and simple helper functions used to verify permissions * and simple helper functions related to users
* @property {Object} levels - Defines labels for default permission ranges * @property {Object} levels - Defines labels for default permission ranges
* @author MinusGix ( https://github.com/MinusGix ) * @author MinusGix ( https://github.com/MinusGix )
* @version v1.0.0 * @version v1.0.0
@ -83,3 +83,13 @@ export function isChannelTrusted(level) {
export function isTrustedUser(level) { export function isTrustedUser(level) {
return level >= levels.trustedUser; return level >= levels.trustedUser;
} }
/**
* Returns true if the nickname is valid
* @public
* @param {String} nick
* @return {boolean}
*/
export function verifyNickname (nick) {
return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
}