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.
84 lines
2.2 KiB
84 lines
2.2 KiB
// text.js
|
|
// Copyright (C) 2024 DTP Technologies, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import mongoose from 'mongoose';
|
|
const ChatFilter = mongoose.model('ChatFilter');
|
|
|
|
import striptags from 'striptags';
|
|
import unzalgo from 'unzalgo';
|
|
import shoetest from 'shoetest';
|
|
import diacritics from 'diacritics';
|
|
import DtpTextFilter from './lib/edit-with-vi.js';
|
|
|
|
import { SiteService, SiteError } from '../../lib/site-lib.js';
|
|
|
|
export default class TextService extends SiteService {
|
|
|
|
static get slug () { return 'text'; }
|
|
static get name ( ) { return 'TextService'; }
|
|
|
|
constructor (dtp) {
|
|
super(dtp, TextService);
|
|
}
|
|
|
|
async start ( ) {
|
|
await this.loadChatFilters();
|
|
}
|
|
|
|
/**
|
|
* Basic text cleaning function to remove Zalgo and tags.
|
|
* @param {String} text The text to be cleaned
|
|
* @returns The cleaned text
|
|
*/
|
|
clean (text) {
|
|
text = unzalgo.clean(text);
|
|
text = striptags(text.trim());
|
|
return text;
|
|
}
|
|
|
|
/**
|
|
* The heavy hammer of text filtering that removes all malicious and annoying
|
|
* things I know about as of this writing. Zalgo, tags, shoetest, diacritics,
|
|
* and our own custom nonsense UTF-8 and Unicode filters.
|
|
*
|
|
* This filter is very heavy-handed and merciless.
|
|
*
|
|
* @param {String} text The text to be filtered
|
|
* @returns The filtered text
|
|
*/
|
|
filter (text) {
|
|
if (!text || (typeof text !== 'string') || (text.length < 1)) {
|
|
return text;
|
|
}
|
|
|
|
text = DtpTextFilter.filterNonsense(text);
|
|
text = DtpTextFilter.filterGuff(text);
|
|
text = DtpTextFilter.filterHtml(text);
|
|
|
|
text = shoetest.simplify(text);
|
|
text = diacritics.remove(text);
|
|
|
|
for (const filter of this.chatFilters) {
|
|
const regex = new RegExp(filter, 'gi');
|
|
if (text.match(regex)) {
|
|
this.log.alert('chat filter text match', { filter });
|
|
throw new SiteError(403, 'Text input rejected');
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Once all the stupidity has been stripped, strip the HTML
|
|
* tags that might remain.
|
|
*/
|
|
return this.clean(text);
|
|
}
|
|
|
|
async loadChatFilters ( ) {
|
|
this.chatFilters = await ChatFilter.find().lean();
|
|
this.chatFilters = this.chatFilters.map((filter) => filter.filter);
|
|
this.log.debug('loading chat filters', { count: this.chatFilters.length });
|
|
}
|
|
}
|