Skip to content

Commit 749a119

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 25ebcf0 + 1b21217 commit 749a119

10 files changed

Lines changed: 133 additions & 106 deletions

File tree

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
Copyright (c) 2013-2017, Davis Wainaina <davisjwn@gmail.com>
1+
Copyright (c) 2013-2019, Davis Wainaina <davisjwn@gmail.com>
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
55

66
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
77
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
var winston = require.main.require('winston');
44
var meta = require.main.require('./src/meta');
55

6+
var translator = require.main.require('./src/translator');
7+
68
var toRegExp = require('./lib/toRegExp');
79
var parseContent = require('./lib/parseContent');
810

@@ -24,7 +26,7 @@ var Beep = {
2426

2527
parseContent: function (content, symbol) {
2628
var nil = '^(?!x)x';
27-
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, symbol || '*');
29+
return parseContent(content, Beep.banned_words || nil, Beep.banned_urls || nil, Beep.censorWholeWord, symbol || '&ast;');
2830
},
2931
toRegExp: toRegExp,
3032
loadList: function (callback) {
@@ -84,7 +86,7 @@ var Beep = {
8486
return callback(null, config);
8587
}
8688
config.beep = {
87-
censorWholeWord: censorWholeWord === 'on'
89+
censorWholeWord: censorWholeWord === 'on',
8890
};
8991
callback(err, config);
9092
});
@@ -95,11 +97,15 @@ var Beep = {
9597

9698
var titleMatch = postTitle && postTitle.match(Beep.illegal_words);
9799
if (titleMatch) {
98-
return callback(new Error('You may not use the word "' + titleMatch[0] + '" in your title.'));
100+
return translator.translate('[[beep:titleMatch.error, ' + titleMatch[0] + ']]', function (translated) {
101+
callback(new Error(translated));
102+
});
99103
}
100104
var contentMatch = postContent && postContent.match(Beep.illegal_words);
101105
if (contentMatch) {
102-
return callback(new Error('You may not use the word "' + contentMatch[0] + '" in your post.'));
106+
return translator.translate('[[beep:contentMatch.error, ' + contentMatch[0] + ']]', function (translated) {
107+
callback(new Error(translated));
108+
});
103109
}
104110

105111
callback(null, data);
@@ -133,20 +139,13 @@ var Beep = {
133139
data.userData.signature = Beep.parseContent(data.userData.signature);
134140
callback(null, data);
135141
},
136-
onTopicsGet: function (data, callback) {
137-
data.topics.forEach(Beep.parseTopic);
138-
callback(null, data);
139-
},
140-
onTopicGet: function (data, callback) {
141-
Beep.parseTopic(data.topic);
142-
callback(null, data);
143-
},
144-
onGetPostSummaries: function (data, callback) {
145-
data.posts.forEach(function (post) {
146-
if (post) {
147-
Beep.parseTopic(post.topic);
148-
}
149-
});
142+
parseTopic: function (data, callback) {
143+
// from http://htmlarrows.com/symbols/
144+
var starHTML = '*';
145+
data.topic.title = Beep.parseContent(data.topic.title, starHTML);
146+
data.topic.slug = Beep.parseContent(data.topic.slug, starHTML);
147+
data.topic.titleRaw = Beep.parseContent(data.topic.titleRaw, starHTML);
148+
150149
callback(null, data);
151150
},
152151
parseTopic: function (topic) {
@@ -166,7 +165,9 @@ var Beep = {
166165
});
167166

168167
if (match) {
169-
return callback(new Error('You may not use the word "' + match[0] + '" in your tags.'));
168+
return translator.translate('[[beep:tagMatch.error, ' + match[0] + ']]', function (translated) {
169+
callback(new Error(translated));
170+
});
170171
}
171172

172173
data.tags = data.tags.map(function (tag) {
@@ -178,12 +179,12 @@ var Beep = {
178179
admin: {
179180
menu: function (custom_header, callback) {
180181
custom_header.plugins.push({
181-
'route': '/plugins/beep',
182-
'icon': 'fa-microphone-slash',
183-
'name': 'Censor Curse Words'
182+
route: '/plugins/beep',
183+
icon: 'fa-microphone-slash',
184+
name: 'Censor Curse Words',
184185
});
185186
callback(null, custom_header);
186-
}
187+
},
187188
},
188189
post: {
189190
getFields: function (data, callback) {
@@ -193,14 +194,14 @@ var Beep = {
193194
});
194195
}
195196
callback(null, data);
196-
}
197+
},
197198
},
198199
messaging: {
199200
getTeaser: function (data, callback) {
200201
data.teaser.content = Beep.parseContent(data.teaser.content);
201202
callback(null, data);
202-
}
203-
}
203+
},
204+
},
204205
};
205206

206207
module.exports = Beep;

languages/en_GB/beep.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"titleMatch.error": "You may not use the word \"%1\" in your title.",
3+
"contentMatch.error": "You may not use the word \"%1\" in your post.",
4+
"tagMatch.error": "You may not use the word \"%1\" in your tags."
5+
}

languages/it/beep.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"titleMatch.error": "Non puoi usare la parola \"%1\" nel titolo.",
3+
"contentMatch.error": "Non puoi usare la parola \"%1\" ne post.",
4+
"tagMatch.error": "Non puoi usare la parola \"%1\" nei tags."
5+
}

lib/parseContent.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,34 @@
33
var isLatin = /^\w+$/;
44

55
function parseContent(content, banned_words, banned_urls, censorWholeWord, symbol) {
6-
if (!content) {
7-
return content;
8-
}
9-
10-
symbol = symbol || '*';
11-
12-
function censor(match) {
13-
if (!isLatin.test(match)) {
14-
return '[censored]';
15-
}
16-
17-
var l = match.length;
18-
var out = match[0];
19-
20-
var i = l - 2;
21-
while (i) {
22-
out += symbol;
23-
i--;
24-
}
25-
26-
return out + match[l - 1];
27-
}
28-
29-
var replacement = censorWholeWord ? '[censored]' : censor;
30-
return content
31-
.replace(banned_words, replacement)
32-
.replace(banned_urls, '[link removed]');
6+
if (!content) {
7+
return content;
8+
}
9+
10+
symbol = symbol || '*';
11+
12+
function censor(match) {
13+
if (!isLatin.test(match)) {
14+
return '[censored]';
15+
}
16+
17+
var l = match.length;
18+
var out = match[0];
19+
20+
var i = l - 2;
21+
while (i) {
22+
out += symbol;
23+
// eslint-disable-next-line no-plusplus
24+
i--;
25+
}
26+
27+
return out + match[l - 1];
28+
}
29+
30+
var replacement = censorWholeWord ? '[censored]' : censor;
31+
return content
32+
.replace(banned_words, replacement)
33+
.replace(banned_urls, '[link removed]');
3334
}
3435

3536
module.exports = parseContent;

lib/toRegExp.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,39 @@
33
var isLatin = /^\w+$/;
44

55
function toRegExp(arr, fullWord) {
6-
if (!Array.isArray(arr)) {
7-
arr = (arr || '').toString().split(',');
8-
}
9-
arr = arr.filter(Boolean);
6+
if (!Array.isArray(arr)) {
7+
arr = (arr || '').toString().split(',');
8+
}
9+
arr = arr.filter(Boolean);
1010

11-
var str;
12-
if (fullWord) {
13-
var latin = arr.filter(function (word) {
14-
return isLatin.test(word);
15-
}).map(function (word) {
16-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
17-
}).join('|');
11+
var str;
12+
if (fullWord) {
13+
var latin = arr.filter(function (word) {
14+
return isLatin.test(word);
15+
}).map(function (word) {
16+
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
17+
}).join('|');
1818

19-
var notLatin = arr.filter(function (word) {
20-
return !isLatin.test(word);
21-
}).map(function (word) {
22-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
23-
}).join('|');
19+
var notLatin = arr.filter(function (word) {
20+
return !isLatin.test(word);
21+
}).map(function (word) {
22+
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
23+
}).join('|');
2424

25-
if (latin && notLatin) {
26-
str = '\\b(?:' + latin + ')\\b|(?:' + notLatin + ')';
27-
} else if (latin) {
28-
str = '\\b(?:' + latin + ')\\b';
29-
} else if (notLatin) {
30-
str = notLatin;
31-
}
32-
} else {
33-
str = arr.filter(Boolean).map(function (word) {
34-
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
35-
}).join('|');
36-
}
25+
if (latin && notLatin) {
26+
str = '\\b(?:' + latin + ')\\b|(?:' + notLatin + ')';
27+
} else if (latin) {
28+
str = '\\b(?:' + latin + ')\\b';
29+
} else if (notLatin) {
30+
str = notLatin;
31+
}
32+
} else {
33+
str = arr.filter(Boolean).map(function (word) {
34+
return word.trim().replace(/([-[\]{}()*+?.,\\^$|#\s])/g, '\\$1');
35+
}).join('|');
36+
}
3737

38-
return new RegExp(str || '^(?!x)x', 'ig');
38+
return new RegExp(str || '^(?!x)x', 'ig');
3939
}
4040

4141
module.exports = toRegExp;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nodebb-plugin-beep",
3-
"version": "0.4.1",
3+
"version": "0.4.2",
44
"description": "NodeBB Censor Curse Words Plugin",
55
"main": "index.js",
66
"scripts": {

plugin.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@
2929
{ "hook": "filter:config.get", "method": "appendConfig" },
3030

3131
{ "hook": "filter:messaging.getTeaser", "method": "messaging.getTeaser" }
32-
]
32+
],
33+
"languages": "languages",
34+
"defaultLang": "en_GB"
3335
}

tests/parseContent.spec.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ var nil = '^(?!x)x';
1111
var symbol = '&#8270;';
1212

1313
assert.strictEqual(parseContent(
14-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
15-
bannedWords,
16-
nil
14+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
15+
bannedWords,
16+
nil
1717
), 'A whole lot of p**p causes a ton of s**t, shitzu, repoopulate');
1818

1919
assert.strictEqual(parseContent(
20-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
21-
bannedWords,
22-
nil,
23-
false,
24-
symbol
20+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
21+
bannedWords,
22+
nil,
23+
false,
24+
symbol
2525
), 'A whole lot of p&#8270;&#8270;p causes a ton of s&#8270;&#8270;t, shitzu, repoopulate');
2626

2727
assert.strictEqual(parseContent(
28-
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
29-
bannedWords,
30-
nil,
31-
true
28+
'A whole lot of poop causes a ton of shit, shitzu, repoopulate',
29+
bannedWords,
30+
nil,
31+
true
3232
), 'A whole lot of [censored] causes a ton of [censored], shitzu, repoopulate');
3333

3434
assert.strictEqual(parseContent(
35-
'My favorite website is http://example.com. I also love http://foo.bar.',
36-
nil,
37-
bannedUrls,
38-
false
35+
'My favorite website is http://example.com. I also love http://foo.bar.',
36+
nil,
37+
bannedUrls,
38+
false
3939
), 'My favorite website is [link removed]. I also love [link removed].');
4040

4141
var unicodeBannedWords = toRegExp(['今', '野'], true);
4242
assert.strictEqual(parseContent(
43-
'載点代示早面通今就焼初哲野質',
44-
unicodeBannedWords,
45-
nil,
46-
false
43+
'載点代示早面通今就焼初哲野質',
44+
unicodeBannedWords,
45+
nil,
46+
false
4747
), '載点代示早面通[censored]就焼初哲[censored]質');
4848

4949
console.log('parseContent passed');

0 commit comments

Comments
 (0)