Skip to content

Commit

Permalink
system/moderation: Fix capitals check with emotes
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Apr 4, 2018
1 parent 241403d commit 0207e97
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
12 changes: 9 additions & 3 deletions libs/configuration.js
Expand Up @@ -129,9 +129,15 @@ Configuration.prototype.listSets = function (self, sender, text) {

Configuration.prototype.getValue = async function (cfgName) {
let item = await global.db.engine.findOne('settings', { key: cfgName })
if (_.isEmpty(item)) return this.cfgL[cfgName].value // return default value if not saved
if (_.includes(['true', 'false'], item.value.toString().toLowerCase())) return item.value.toString().toLowerCase() === 'true'
else return item.value
try {
if (_.isEmpty(item)) return this.cfgL[cfgName].value // return default value if not saved
if (_.includes(['true', 'false'], item.value.toString().toLowerCase())) return item.value.toString().toLowerCase() === 'true'
else return item.value
} catch (e) {
global.log.error(`Error when loading ${cfgName} value`)
global.log.error(e.stack)
return null
}
}

module.exports = Configuration
19 changes: 13 additions & 6 deletions libs/systems/moderation.js
Expand Up @@ -317,27 +317,34 @@ class Moderation {
var msgLength = whitelisted.trim().length
var capsLength = 0

debug('moderation:caps')('emotes - %j', sender['emotes'])
debug('moderation:caps')('should check caps - %s', isEnabled)
debug('moderation:caps')('isOwner: %s', global.commons.isOwner(sender))
debug('moderation:caps')('isMod: %s', isMod)
debug('moderation:caps')('msgLength: %s', msgLength)
debug('moderation:caps')('triggerLength: %s', triggerLength)
if (global.commons.isOwner(sender) || isMod || msgLength < triggerLength || !isEnabled || (sender.subscriber && !isEnabledForSubs)) {
return true
}

const regexp = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-./:;<=>?@[\]^_`{|}~]/gi
whitelisted = whitelisted.trim()
for (let i = 0; i < whitelisted.length; i++) {
// if is emote or symbol - continue
if (_.includes(emotesCharList, i) || !_.isNull(whitelisted.charAt(i).match(regexp))) {
debug('moderation:caps')(`Emotes char at position ${i}, ${whitelisted.charAt(i)}`)
msgLength = parseInt(msgLength, 10) - 1
continue
} else if (!_.isFinite(parseInt(whitelisted.charAt(i), 10)) && whitelisted.charAt(i).toUpperCase() === whitelisted.charAt(i) && whitelisted.charAt(i) !== ' ') {
debug('moderation:caps')(`Capped char at position ${i}, ${whitelisted.charAt(i)}`)
capsLength += 1
}
if (!_.isFinite(parseInt(whitelisted.charAt(i), 10)) && whitelisted.charAt(i).toUpperCase() === whitelisted.charAt(i) && whitelisted.charAt(i) !== ' ') capsLength += 1
}

debug('moderation:caps')('msgLength: %s', msgLength)
debug('moderation:caps')('triggerLength: %s', triggerLength)
debug('moderation:caps')('capped chars: %i', capsLength)
debug('moderation:caps')('triggerPercent: %i%', maxCapsPercent)
debug('moderation:caps')('capped percent: %i%', Math.ceil(capsLength / (msgLength / 100)))

if (global.commons.isOwner(sender) || isMod || msgLength < triggerLength || !isEnabled || (sender.subscriber && !isEnabledForSubs)) {
return true
}
if (Math.ceil(capsLength / (msgLength / 100)) >= maxCapsPercent) {
log.info(sender.username + ' [caps] ' + timeout + 's timeout: ' + text)
self.timeoutUser(self, sender,
Expand Down
10 changes: 6 additions & 4 deletions test/tests/moderation/caps.js
Expand Up @@ -10,9 +10,11 @@ const tests = {
'timeout': [
'AAAAAAAAAAAAAAAAAAAAAA',
'ЙЦУЦЙУЙЦУЙЦУЙЦУЙЦУЙЦ',
'AAAAAAAAAAAAAaaaaaaaaaaaa'
'AAAAAAAAAAAAAaaaaaaaaaaaa',
'SomeMSG SomeMSG'
],
'ok': [
'SomeMSG SomeMSg',
'123123123213123123123123213123'
]
}
Expand Down Expand Up @@ -54,16 +56,16 @@ describe('systems/moderation - Caps()', () => {
})
}
})
describe('#884 - message length - 15, max caps 80%, message: FrankerZ FrankerZ', async () => {
describe('#884 - message length - 15, max caps 80%, message: BlessRNG BlessRNG with emotes', async () => {
before(async () => {
await db.cleanup()
await global.db.engine.insert('settings', { key: 'moderationCaps', value: 'true' })
await global.db.engine.insert('settings', { key: 'moderationCapsMaxPercent', value: 80 })
await global.db.engine.insert('settings', { key: 'moderationCapsTriggerLength', value: 15 })
})

it(`message 'FrankerZ FrankerZ' should not timeout`, async () => {
assert.isTrue(await global.systems.moderation.caps(global.systems.moderation, { username: 'testuser' }, 'FrankerZ FrankerZ'))
it(`message 'BlessRNG BlessRNG' with emotes should not timeout`, async () => {
assert.isTrue(await global.systems.moderation.caps(global.systems.moderation, { username: 'testuser', emotes: {'153556': ['0-7', '9-16']} }, 'BlessRNG BlessRNG'))
})
})
})

0 comments on commit 0207e97

Please sign in to comment.