]> git.mxchange.org Git - fba.git/blob - fba/boot.py
0fb55ec5796751973811f249467add5d5546e00d
[fba.git] / fba / boot.py
1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published
6 # by the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17 import logging
18
19 import argparse
20
21 from fba import commands
22 from fba import database
23
24 from fba.helpers import locking
25
26 logging.basicConfig(level=logging.INFO)
27 logger = logging.getLogger(__name__)
28
29 # Argument parser
30 _PARSER = None
31
32 def init_parser():
33     logger.debug("CALLED!")
34     global _PARSER
35
36     logger.debug("Initializing parser ...")
37     _PARSER = argparse.ArgumentParser(
38         description="Fetches block reasons from the fediverse",
39         epilog="Please note that some commands have optional arguments, you may want to try fba.py <command> --help to find them out. Please DO NOT overdose requests that are not limited by themselves. Typically parameters like --domain, --software and --force are unlimited. \"Unlimited\" here means that there is no \"is recently accessed?\" limitation.",
40     )
41
42     # Generic:
43     _PARSER.add_argument(
44         "--debug",
45         action="store_const",
46         dest="log_level",
47         const=logging.DEBUG,
48         help="Full debug output"
49     )
50
51     # Commands:
52     subparser_command = _PARSER.add_subparsers(
53         dest="command",
54         title="Commands to execute",
55         required=True,
56         help="Command to perform",
57     )
58
59     ### Check instance ###
60     parser = subparser_command.add_parser(
61         "check_instance",
62         help="Checks given instance if it exists and returns proper exit code"
63     )
64     parser.set_defaults(command=commands.check_instance)
65     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
66
67     ### Fetch from bka.li ###
68     parser = subparser_command.add_parser(
69         "fetch_bkali",
70         help="Fetches domain names from bka.li API",
71     )
72     parser.set_defaults(command=commands.fetch_bkali)
73
74     ### Recheck instance's obfuscated peers ###
75     parser = subparser_command.add_parser(
76         "recheck_obfuscation",
77         help="Checks all instance's obfuscated peers if they can be de-obfuscated now.",
78     )
79     parser.set_defaults(command=commands.recheck_obfuscation)
80     parser.add_argument("--domain", help="Instance name (aka. domain)")
81     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
82     parser.add_argument("--force", action="store_true", help="Include also already existing instances, otherwise only new are checked")
83
84     ### Fetch blocks from registered instances or given ###
85     parser = subparser_command.add_parser(
86         "fetch_blocks",
87         help="Fetches blocks from registered instances (run command fetch_instances first!).",
88     )
89     parser.set_defaults(command=commands.fetch_blocks)
90     parser.add_argument("--domain", help="Instance name (aka. domain)")
91     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
92     parser.add_argument("--only-none", action="store_true", help="Checks only entries which has never been checked.")
93     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
94
95     ### Fetch blocks from chaos.social ###
96     parser = subparser_command.add_parser(
97         "fetch_cs",
98         help="Fetches blocks from chaos.social's meta sub domain.",
99     )
100     parser.set_defaults(command=commands.fetch_cs)
101
102     ### Fetch blocks from todon.eu wiki ###
103     parser = subparser_command.add_parser(
104         "fetch_todon_wiki",
105         help="Fetches blocks from todon.eu's wiki.",
106     )
107     parser.set_defaults(command=commands.fetch_todon_wiki)
108
109     ### Fetch blocks from a FBA-specific RSS feed  ###
110     parser = subparser_command.add_parser(
111         "fetch_fba_rss",
112         help="Fetches domains from a FBA-specific RSS feed.",
113     )
114     parser.set_defaults(command=commands.fetch_fba_rss)
115     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
116
117     ### Fetch blocks from FBA's bot account ###
118     parser = subparser_command.add_parser(
119         "fetch_fbabot_atom",
120         help="Fetches ATOM feed with domains from FBA's bot account.",
121     )
122     parser.set_defaults(command=commands.fetch_fbabot_atom)
123     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
124
125     ### Fetch blocks from oliphant's GIT repository ###
126     parser = subparser_command.add_parser(
127         "fetch_oliphant",
128         help="Fetches CSV files from GIT generated by Oliphant 'member instances'.",
129     )
130     parser.set_defaults(command=commands.fetch_oliphant)
131     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
132
133     ### Fetch blocks from other CSV files
134     parser = subparser_command.add_parser(
135         "fetch_csv",
136         help="Fetches CSV files (block recommendations) for more possible instances to disover",
137     )
138     parser.set_defaults(command=commands.fetch_csv)
139     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
140
141     ### Fetch instances from given initial instance ###
142     parser = subparser_command.add_parser(
143         "fetch_instances",
144         help="Fetches instances (aka. \"domains\") from an initial instance. You may want to re-run this command several times (at least 3 with big instances) to have a decent amount of valid instances.",
145     )
146     parser.set_defaults(command=commands.fetch_instances)
147     parser.add_argument("--domain", help="Instance name (aka. domain) to fetch further instances from. Start with a large instance, e.g. mastodon.social .")
148     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
149     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
150
151     ### Fetch blocks from static text file(s) ###
152     parser = subparser_command.add_parser(
153         "fetch_txt",
154         help="Fetches text/plain files as simple domain lists",
155     )
156     parser.set_defaults(command=commands.fetch_txt)
157     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
158
159     ### Fetch blocks from joinfediverse.wiki ###
160     #parser = subparser_command.add_parser(
161     #    "fetch_joinfediverse",
162     #    help="Fetches FediBlock page from joinfediverse.wiki",
163     #)
164     #parser.set_defaults(command=commands.fetch_joinfediverse)
165
166     ### Fetch instances JSON from instances.joinmobilizon.org
167     parser = subparser_command.add_parser(
168         "fetch_joinmobilizon",
169         help="Fetches instances from joinmobilizon",
170     )
171     parser.set_defaults(command=commands.fetch_joinmobilizon)
172
173     ### Fetch blocks from misskey.page ###
174     parser = subparser_command.add_parser(
175         "fetch_joinmisskey",
176         help="Fetches instances.json from misskey.page",
177     )
178     parser.set_defaults(command=commands.fetch_joinmisskey)
179
180     ### Fetch blocks from fediverse.observer ###
181     parser = subparser_command.add_parser(
182         "fetch_observer",
183         help="Fetches blocks from fediverse.observer.",
184     )
185     parser.set_defaults(command=commands.fetch_observer)
186     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
187
188     ### Fetch instances from fedipact.online ###
189     parser = subparser_command.add_parser(
190         "fetch_fedipact",
191         help="Fetches blocks from fedipact.online.",
192     )
193     parser.set_defaults(command=commands.fetch_fedipact)
194
195     ### Fetch from pixelfed.org's API ###
196     parser = subparser_command.add_parser(
197         "fetch_pixelfed_api",
198         help="Fetches domain names from pixelfed.org's API",
199     )
200     parser.set_defaults(command=commands.fetch_pixelfed_api)
201
202     ### Check nodeinfo ###
203     parser = subparser_command.add_parser(
204         "check_nodeinfo",
205         help="Checks if domain is part of nodeinfo.",
206     )
207     parser.set_defaults(command=commands.check_nodeinfo)
208
209     ### Fetch CSV from fedilist.com ###
210     parser = subparser_command.add_parser(
211         "fetch_fedilist",
212         help="Fetches CSV from fedilist.com",
213     )
214     parser.set_defaults(command=commands.fetch_fedilist)
215     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
216     parser.add_argument("--force", action="store_true", help="Include also already existing instances, otherwise only new are checked")
217
218     ### Update nodeinfo ###
219     parser = subparser_command.add_parser(
220         "update_nodeinfo",
221         help="Updates nodeinfo for all instances",
222     )
223     parser.set_defaults(command=commands.update_nodeinfo)
224     parser.add_argument("--domain", help="Instance name (aka. domain)")
225     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
226     parser.add_argument("--mode", help="Name of detection mode, e.g. 'auto_discovery'")
227     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
228     parser.add_argument("--no-software", action="store_true", help="Checks only entries with no software type detected.")
229     parser.add_argument("--no-auto", action="store_true", help="Checks only entries with other than AUTO_DISCOVERY as detection mode.")
230     parser.add_argument("--no-detection", action="store_true", help="Checks only entries with no detection mode set.")
231     parser.add_argument("--with-software", action="store_true", help="Checks only entries with any software type detected.")
232
233     ### Fetch instances from instances.social ###
234     parser = subparser_command.add_parser(
235         "fetch_instances_social",
236         help="Fetch instances from instances.social, you need an API key to access the API. Please consider donating to them when you want to more frequent use their API!",
237     )
238     parser.set_defaults(command=commands.fetch_instances_social)
239
240     ### Convert international domain names to punycode domains ###
241     parser = subparser_command.add_parser(
242         "convert_idna",
243         help="Convertes UTF-8 encoded international domain names into punycode (IDNA) domain names.",
244     )
245     parser.set_defaults(command=commands.convert_idna)
246
247     ### Fetch instances from ActivityPub relays ###
248     parser = subparser_command.add_parser(
249         "fetch_relays",
250         help="Fetches instances from ActivityPub relays",
251     )
252     parser.set_defaults(command=commands.fetch_relays)
253     parser.add_argument("--domain", help="Instance name (aka. 'relay')")
254     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
255     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
256
257     ### Fetches relay list from relaylist.com
258     parser = subparser_command.add_parser(
259         "fetch_relaylist",
260         help="Fetches relay list from relaylist.com",
261     )
262     parser.set_defaults(command=commands.fetch_relaylist)
263
264     ### Remove invalid domains ###
265     parser = subparser_command.add_parser(
266         "remove_invalid",
267         help="Removes invalid domains.",
268     )
269     parser.set_defaults(command=commands.remove_invalid)
270
271     logger.debug("EXIT!")
272
273 def run_command():
274     logger.debug("CALLED!")
275     args = _PARSER.parse_args()
276
277     if args.log_level is not None:
278         loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
279         for _logger in loggers:
280             _logger.setLevel(args.log_level)
281
282     logger.debug("args[%s]='%s'", type(args), args)
283     status = args.command(args)
284
285     logger.debug("status=%d - EXIT!", status)
286     return status
287
288 def shutdown():
289     logger.debug("Closing database connection ...")
290     database.connection.close()
291     locking.release()
292     logger.debug("Shutdown completed.")