-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcensor-generator.py
More file actions
executable file
·67 lines (53 loc) · 2.47 KB
/
censor-generator.py
File metadata and controls
executable file
·67 lines (53 loc) · 2.47 KB
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
#!/usr/bin/env python
"""
Generate a censor configuration for InspIRCd
Copyright (c) 2024 Michael Hazell <michaelhazell@hotmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import argparse
import requests
import sys
parser = argparse.ArgumentParser(
description='Generate a censor.conf file for InspIRCd')
parser.add_argument('-o', '--output', type=str, default='',
help='Output file, where the configuration will be written. If not specified, output will be written to stdout.')
parser.add_argument('-r', '--replace', type=str, default='',
help='Replacement string for censored words. If not specified, replacement string will be asterisks.')
parser.add_argument('-u', '--url', type=str,
required=True, help='URL of the word list. It is expected to be a plaintext list.')
args = parser.parse_args()
def main():
# Fetch the specified URL
response = requests.get(args.url)
if response.status_code != 200:
print('URL request did not return OK status', file=sys.stderr)
return
# If no output file specified, print to stdout
use_file: bool = len(args.output) != 0
with open(args.output, 'w') if use_file else sys.stdout as f:
f.write('# censor.conf auto-generated by censor-generator.py\n')
# If a default replacement was specified, use it. Else, use stars.
use_replace: bool = len(args.replace) != 0
for word in response.iter_lines(decode_unicode=True):
replace: str = args.replace if use_replace else stars(len(word))
line: str = f'<badword text="{word}" replace="{replace}">\n'
f.write(line)
def stars(length: int) -> str:
"""Return a 'len'-sized string of asterisks"""
if length == 0:
return ''
elif length < 0:
raise ValueError('Length cannot be a negative value')
else:
return '*' * length
if __name__ == "__main__":
main()