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

Merge pull request #90 from MinusGix/verifyNicknameMove

Deduplicate verifyNickname into a single importable function
This commit is contained in:
marzavec 2020-03-06 15:11:19 -06:00 committed by GitHub
commit 05436fa4dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 12 deletions

View File

@ -2,8 +2,7 @@
Description: Allows calling client to change their current nickname
*/
// module support functions
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
import * as UAC from "../utility/UAC/_info";
// module main
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
const newNick = data.nick.trim();
if (!verifyNickname(newNick)) {
if (!UAC.verifyNickname(newNick)) {
return server.reply({
cmd: 'warn',
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
*/
// module support functions
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
import * as UAC from "../utility/UAC/_info";
// module main
export async function run(core, server, socket, data) {
@ -16,7 +15,7 @@ export async function run(core, server, socket, data) {
}
// verify user input
if (typeof data.nick !== 'string' || !verifyNickname(data.nick)) {
if (typeof data.nick !== 'string' || !UAC.verifyNickname(data.nick)) {
return true;
}

View File

@ -13,8 +13,6 @@ const hash = (password) => {
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
// returns object containing user info or string if error
export function parseNickname(core, data) {
@ -29,7 +27,7 @@ export function parseNickname(core, data) {
const nickArray = data.nick.split('#', 2);
userInfo.nick = nickArray[0].trim();
if (!verifyNickname(userInfo.nick)) {
if (!UAC.verifyNickname(userInfo.nick)) {
// return error as string
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
*/
import * as UAC from "../utility/UAC/_info";
// module support functions
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
const parseText = (text) => {
// verifies user input is text
@ -41,7 +42,7 @@ export async function run(core, server, socket, payload) {
}
const targetNick = payload.nick;
if (!verifyNickname(targetNick)) {
if (!UAC.verifyNickname(targetNick)) {
return true;
}

View File

@ -1,6 +1,6 @@
/**
* 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
* @author MinusGix ( https://github.com/MinusGix )
* @version v1.0.0
@ -83,3 +83,13 @@ export function isChannelTrusted(level) {
export function isTrustedUser(level) {
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);
}