]> git.mxchange.org Git - fba.git/blob - fba/boot.py
9e8e91c84c4b2890bd473a4a9f1b3904a67096d9
[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("--force", action="store_true", help="Forces update of data, no matter what.")
93
94     ### Fetch blocks from chaos.social ###
95     parser = subparser_command.add_parser(
96         "fetch_cs",
97         help="Fetches blocks from chaos.social's meta sub domain.",
98     )
99     parser.set_defaults(command=commands.fetch_cs)
100
101     ### Fetch blocks from todon.eu wiki ###
102     parser = subparser_command.add_parser(
103         "fetch_todon_wiki",
104         help="Fetches blocks from todon.eu's wiki.",
105     )
106     parser.set_defaults(command=commands.fetch_todon_wiki)
107
108     ### Fetch blocks from a FBA-specific RSS feed  ###
109     parser = subparser_command.add_parser(
110         "fetch_fba_rss",
111         help="Fetches domains from a FBA-specific RSS feed.",
112     )
113     parser.set_defaults(command=commands.fetch_fba_rss)
114     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
115
116     ### Fetch blocks from FBA's bot account ###
117     parser = subparser_command.add_parser(
118         "fetch_fbabot_atom",
119         help="Fetches ATOM feed with domains from FBA's bot account.",
120     )
121     parser.set_defaults(command=commands.fetch_fbabot_atom)
122     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
123
124     ### Fetch blocks from oliphant's GIT repository ###
125     parser = subparser_command.add_parser(
126         "fetch_oliphant",
127         help="Fetches CSV files from GIT generated by Oliphant 'member instances'.",
128     )
129     parser.set_defaults(command=commands.fetch_oliphant)
130     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
131
132     ### Fetch blocks from other CSV files
133     parser = subparser_command.add_parser(
134         "fetch_csv",
135         help="Fetches CSV files (block recommendations) for more possible instances to disover",
136     )
137     parser.set_defaults(command=commands.fetch_csv)
138     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
139
140     ### Fetch instances from given initial instance ###
141     parser = subparser_command.add_parser(
142         "fetch_instances",
143         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.",
144     )
145     parser.set_defaults(command=commands.fetch_instances)
146     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to fetch further instances from. Start with a large instance, e.g. mastodon.social .")
147     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
148
149     ### Fetch blocks from static text file(s) ###
150     parser = subparser_command.add_parser(
151         "fetch_txt",
152         help="Fetches text/plain files as simple domain lists",
153     )
154     parser.set_defaults(command=commands.fetch_txt)
155
156     ### Fetch blocks from joinfediverse.wiki ###
157     #parser = subparser_command.add_parser(
158     #    "fetch_joinfediverse",
159     #    help="Fetches FediBlock page from joinfediverse.wiki",
160     #)
161     #parser.set_defaults(command=commands.fetch_joinfediverse)
162
163     ### Fetch instances JSON from instances.joinmobilizon.org
164     parser = subparser_command.add_parser(
165         "fetch_joinmobilizon",
166         help="Fetches instances from joinmobilizon",
167     )
168     parser.set_defaults(command=commands.fetch_joinmobilizon)
169
170     ### Fetch blocks from misskey.page ###
171     parser = subparser_command.add_parser(
172         "fetch_joinmisskey",
173         help="Fetches instances.json from misskey.page",
174     )
175     parser.set_defaults(command=commands.fetch_joinmisskey)
176
177     ### Fetch blocks from fediverse.observer ###
178     parser = subparser_command.add_parser(
179         "fetch_observer",
180         help="Fetches blocks from fediverse.observer.",
181     )
182     parser.set_defaults(command=commands.fetch_observer)
183     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
184
185     ### Fetch instances from fedipact.online ###
186     parser = subparser_command.add_parser(
187         "fetch_fedipact",
188         help="Fetches blocks from fedipact.online.",
189     )
190     parser.set_defaults(command=commands.fetch_fedipact)
191
192     ### Fetch from pixelfed.org's API ###
193     parser = subparser_command.add_parser(
194         "fetch_pixelfed_api",
195         help="Fetches domain names from pixelfed.org's API",
196     )
197     parser.set_defaults(command=commands.fetch_pixelfed_api)
198
199     ### Check nodeinfo ###
200     parser = subparser_command.add_parser(
201         "check_nodeinfo",
202         help="Checks if domain is part of nodeinfo.",
203     )
204     parser.set_defaults(command=commands.check_nodeinfo)
205
206     ### Fetch CSV from fedilist.com ###
207     parser = subparser_command.add_parser(
208         "fetch_fedilist",
209         help="Fetches CSV from fedilist.com",
210     )
211     parser.set_defaults(command=commands.fetch_fedilist)
212     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
213     parser.add_argument("--force", action="store_true", help="Include also already existing instances, otherwise only new are checked")
214
215     ### Update nodeinfo ###
216     parser = subparser_command.add_parser(
217         "update_nodeinfo",
218         help="Updates nodeinfo for all instances",
219     )
220     parser.set_defaults(command=commands.update_nodeinfo)
221     parser.add_argument("--domain", help="Instance name (aka. domain)")
222     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
223     parser.add_argument("--mode", help="Name of detection mode, e.g. 'auto_discovery'")
224     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
225     parser.add_argument("--no-software", action="store_true", help="Checks only entries with no software type detected.")
226     parser.add_argument("--no-auto", action="store_true", help="Checks only entries with other than AUTO_DISCOVERY as detection mode.")
227     parser.add_argument("--no-detection", action="store_true", help="Checks only entries with no detection mode set.")
228
229     ### Fetch instances from instances.social ###
230     parser = subparser_command.add_parser(
231         "fetch_instances_social",
232         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!",
233     )
234     parser.set_defaults(command=commands.fetch_instances_social)
235
236     ### Convert international domain names to punycode domains ###
237     parser = subparser_command.add_parser(
238         "convert_idna",
239         help="Convertes UTF-8 encoded international domain names into punycode (IDNA) domain names.",
240     )
241     parser.set_defaults(command=commands.convert_idna)
242
243     ### Fetch instances from ActivityPub relays ###
244     parser = subparser_command.add_parser(
245         "fetch_relays",
246         help="Fetches instances from ActivityPub relays",
247     )
248     parser.set_defaults(command=commands.fetch_relays)
249     parser.add_argument("--domain", help="Instance name (aka. 'relay')")
250     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
251     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
252
253     ### Fetches relay list from relaylist.com
254     parser = subparser_command.add_parser(
255         "fetch_relaylist",
256         help="Fetches relay list from relaylist.com",
257     )
258     parser.set_defaults(command=commands.fetch_relaylist)
259
260     ### Remove invalid domains ###
261     parser = subparser_command.add_parser(
262         "remove_invalid",
263         help="Removes invalid domains.",
264     )
265     parser.set_defaults(command=commands.remove_invalid)
266
267     logger.debug("EXIT!")
268
269 def run_command():
270     logger.debug("CALLED!")
271     args = _PARSER.parse_args()
272
273     if args.log_level is not None:
274         loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
275         for _logger in loggers:
276             _logger.setLevel(args.log_level)
277
278     logger.debug("args[%s]='%s'", type(args), args)
279     status = args.command(args)
280
281     logger.debug("status=%d - EXIT!", status)
282     return status
283
284 def shutdown():
285     logger.debug("Closing database connection ...")
286     database.connection.close()
287     locking.release()
288     logger.debug("Shutdown completed.")