-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
138 lines (129 loc) · 4.71 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
async function register ({
registerHook,
registerSetting,
settingsManager,
storageManager,
videoCategoryManager,
videoLicenceManager,
videoLanguageManager,
peertubeHelpers
}) {
registerSetting({
name: 'displayname_pattern',
label: 'Forbidden patterns in display name.',
descriptionHTML: 'One pattern per line. You can specify a case-insensitive substring that should not be allowed (for example «John»), or a regular expression («/forbidden\s*word/i»). You can start a line with a # to add a comment line.',
type: 'input-textarea',
private: true,
default: ''
})
registerSetting({
name: 'username_pattern',
label: 'Forbidden patterns in username.',
descriptionHTML: 'One pattern per line. You can specify a case-insensitive substring that should not be allowed (for example «John»), or a regular expression («/forbidden\s*word/i»). You can start a line with a # to add a comment line.',
type: 'input-textarea',
private: true,
default: ''
})
registerSetting({
name: 'email_pattern',
label: 'Forbidden patterns in email.',
descriptionHTML: 'One pattern per line. You can specify a case-insensitive substring that should not be allowed (for example «John»), or a regular expression («/forbidden\s*word/i»). You can start a line with a # to add a comment line.',
type: 'input-textarea',
private: true,
default: ''
})
registerSetting({
name: 'ip_pattern',
label: 'Forbidden patterns on user IP address.',
descriptionHTML: 'One pattern per line. You can specify a case-insensitive substring that should not be allowed (for example «192.168.0.2»), or a regular expression («/^192\./»). You can start a line with a # to add a comment line.',
type: 'input-textarea',
private: true,
default: ''
})
registerSetting({
name: 'error_message',
label: 'Error message',
descriptionHTML: 'Error message to display when the registration is forbidden.',
type: 'input',
private: true,
default: 'You cannot register on this instance. Please use the contact form.'
})
registerHook({
target: 'filter:api.user.signup.allowed.result',
handler: (result, params) => filterRegistration(result, params, settingsManager, peertubeHelpers)
})
}
async function unregister () {
return
}
async function filterRegistration(result, params, settingsManager, peertubeHelpers) {
const logger = peertubeHelpers.logger
logger.debug('Calling filterRegistration...')
if (!params || !params.body) {
logger.debug('This is not a registration form submit. Ignoring.')
return result
}
if (!result) {
logger.error('The result parameter is falsey. This is unexpected. Check the peertube-plugin-filterregistrations compatibility.')
return result
}
if (!result.allowed) {
logger.debug('The registration is already refused.')
return result
}
const settings = await settingsManager.getSettings([
'displayname_pattern',
'username_pattern',
'email_pattern',
'ip_pattern',
'error_message'
])
if (
!checkValue(logger, params.body.displayName ?? '', settings['displayname_pattern'])
|| !checkValue(logger, params.body.username ?? '', settings['username_pattern'])
|| !checkValue(logger, params.body.email ?? '', settings['email_pattern'])
|| !checkValue(logger, params.ip ?? '', settings['ip_pattern'])
) {
return { allowed: false, errorMessage: settings['error_message'] }
}
return result
}
function checkValue(logger, value, setting) {
// splitting the setting on new lines...
let tests = (setting ?? '').split('\n')
for (const test of tests) {
if (/^\s*$/.test(test)) {
// Ignoring empty lines...
continue
}
if (/^\s*#/.test(test)) {
// Ignoring comments (line starting with #)
continue
}
try {
// Checking if the line is a RegExp (/something/ or /something/i)
const testMatch = test.match(/^\s*\/(.*)\/(\w+)?\s*$/)
if (testMatch) {
logger.debug('We have a regular expression to test: '+test)
const regex = new RegExp(testMatch[1], testMatch[2] ? testMatch[2] : undefined)
if (regex.test(value)) {
logger.info('The value is matching «'+regex.toString()+'», registration is forbidden')
return false
}
} else {
logger.debug('We should check if value contains: '+test)
if (value.toLowerCase().includes(test.toLowerCase())) {
logger.info('Found «'+test+'» in the value, registration is forbidden')
return false
}
}
} catch (err) {
logger.error('Failed to test «'+test+"», please check the plugin filterregistrations configuration.")
}
}
return true
}
module.exports = {
register,
unregister
}