mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
3.0 KiB
79 lines
3.0 KiB
4 years ago
|
'use strict';
|
||
|
|
||
4 years ago
|
const { getInsecureTrip, getSecureTrip } = require(__dirname+'/tripcode.js')
|
||
3 years ago
|
, Permissions = require(__dirname+'/../permissions.js')
|
||
3 years ago
|
, nameRegex = /^(?<name>[^#].*?)?(?:(?<tripcode>##(?<strip>[^ ].*?)|#(?<itrip>[^#].*?)))?(?<capcode>##(?<capcodetext> .*?)?)?$/
|
||
4 years ago
|
, staffLevels = ['Admin', 'Global Staff', 'Board Owner', 'Board Mod']
|
||
3 years ago
|
, staffLevelsRegex = new RegExp(`(${staffLevels.join('|')})+`, 'igm');
|
||
4 years ago
|
|
||
3 years ago
|
module.exports = async (inputName, permissions, boardSettings, boardOwner, boardStaff, username) => {
|
||
4 years ago
|
|
||
3 years ago
|
const { forceAnon, defaultName } = boardSettings;
|
||
|
const isBoardOwner = username === boardOwner; //why not just check staffboards and ownedboards?
|
||
|
const staffUsernames = Object.keys(boardStaff);
|
||
|
const isBoardMod = staffUsernames.includes(username);
|
||
|
const staffPermissions = [permissions.get(Permissions.ROOT),
|
||
|
permissions.get(Permissions.MANAGE_GLOBAL_GENERAL),
|
||
|
isBoardOwner, isBoardMod];
|
||
4 years ago
|
|
||
|
let name = defaultName;
|
||
|
let tripcode = null;
|
||
|
let capcode = null;
|
||
3 years ago
|
if ((permissions.get(Permissions.MANAGE_BOARD_GENERAL) || !forceAnon) && inputName && inputName.length > 0) {
|
||
4 years ago
|
// get matches with named groups for name, trip and capcode in 1 regex
|
||
|
const matches = inputName.match(nameRegex);
|
||
|
if (matches && matches.groups) {
|
||
3 years ago
|
|
||
4 years ago
|
const groups = matches.groups;
|
||
3 years ago
|
|
||
4 years ago
|
//name
|
||
|
if (groups.name) {
|
||
|
name = groups.name;
|
||
|
}
|
||
3 years ago
|
|
||
4 years ago
|
//tripcode
|
||
|
if (groups.tripcode) {
|
||
3 years ago
|
let tripcodeText = groups.strip || groups.itrip;
|
||
3 years ago
|
if (!permissions.get(Permissions.MANAGE_BOARD_GENERAL) && groups.capcode === '##' && !groups.capcodetext) {
|
||
3 years ago
|
//for the complaining non-staff troglodyte who puts the name as all #s
|
||
|
tripcodeText = tripcodeText.concat('##');
|
||
|
}
|
||
|
if (groups.strip) {
|
||
|
tripcode = `!!${(await getSecureTrip(tripcodeText))}`;
|
||
|
} else if (groups.itrip) {
|
||
|
tripcode = `!${getInsecureTrip(tripcodeText)}`;
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
4 years ago
|
//capcode
|
||
3 years ago
|
if (permissions.get(Permissions.MANAGE_BOARD_GENERAL) && groups.capcode) {
|
||
4 years ago
|
let capcodeInput = groups.capcodetext ? groups.capcodetext.trim() : '';
|
||
3 years ago
|
const { staffLevelDirect, staffLevelFallback } = staffPermissions.reduce((acc, sp, i) => {
|
||
|
if (sp === true) {
|
||
|
if (!acc.staffLevelFallback) {
|
||
|
acc.staffLevelFallback = staffLevels[i];
|
||
|
}
|
||
|
if (!acc.staffLevelDirect && capcodeInput.toLowerCase().startsWith(staffLevels[i].toLowerCase())) {
|
||
|
acc.staffLevelDirect = staffLevels[i];
|
||
|
}
|
||
|
}
|
||
|
return acc;
|
||
|
}, { 'staffLevelDirect': null, 'staffLevelFallback': null });
|
||
|
//we still get the same fallbacks as before, its just more annoying without the direct permlevel mapping
|
||
|
const staffLevel = staffLevelDirect ||
|
||
|
(isBoardOwner ? staffLevels[2]
|
||
|
: isBoardMod ? staffLevels[3]
|
||
|
: staffLevelFallback);
|
||
4 years ago
|
capcode = staffLevel;
|
||
|
if (capcodeInput && capcodeInput.toLowerCase() !== staffLevel.toLowerCase()) {
|
||
|
capcode = `${staffLevel} ${capcodeInput.replace(staffLevelsRegex, '').trim()}`;
|
||
4 years ago
|
}
|
||
2 years ago
|
capcode = `## ${capcode.trim()}`;
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return { name, tripcode, capcode };
|
||
|
|
||
|
}
|