]> git.mxchange.org Git - fba.git/blob - fba/boot.py
55e9fb4d786052cd5e0a966c5010085388c3c3f7
[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("--debug", action="store_const", dest="log_level", const=logging.DEBUG, help="Full debug output")
44
45     # Commands:
46     subparser_command = _PARSER.add_subparsers(
47         dest="command",
48         title="Commands to execute",
49         required=True,
50         help="Command to perform",
51     )
52
53     ### Check instance ###
54     parser = subparser_command.add_parser(
55         "check_instance",
56         help="Checks given instance if it exists and returns proper exit code"
57     )
58     parser.set_defaults(command=commands.check_instance)
59     parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
60
61     ### Fetch from bka.li ###
62     parser = subparser_command.add_parser(
63         "fetch_bkali",
64         help="Fetches domain names from bka.li API",
65     )
66     parser.set_defaults(command=commands.fetch_bkali)
67
68     ### Recheck instance's obfuscated peers ###
69     parser = subparser_command.add_parser(
70         "recheck_obfuscation",
71         help="Checks all instance's obfuscated peers if they can be de-obfuscated now.",
72     )
73     parser.set_defaults(command=commands.recheck_obfuscation)
74     parser.add_argument("--domain", help="Instance name (aka. domain)")
75     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
76     parser.add_argument("--force", action="store_true", help="Include also already existing instances, otherwise only new are checked")
77
78     ### Fetch blocks from registered instances or given ###
79     parser = subparser_command.add_parser(
80         "fetch_blocks",
81         help="Fetches blocks from registered instances (run command fetch_instances first!).",
82     )
83     parser.set_defaults(command=commands.fetch_blocks)
84     parser.add_argument("--domain", help="Instance name (aka. domain)")
85     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
86     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
87
88     ### Fetch blocks from chaos.social ###
89     parser = subparser_command.add_parser(
90         "fetch_cs",
91         help="Fetches blocks from chaos.social's meta sub domain.",
92     )
93     parser.set_defaults(command=commands.fetch_cs)
94
95     ### Fetch blocks from todon.eu wiki ###
96     parser = subparser_command.add_parser(
97         "fetch_todon_wiki",
98         help="Fetches blocks from todon.eu's wiki.",
99     )
100     parser.set_defaults(command=commands.fetch_todon_wiki)
101
102     ### Fetch blocks from a FBA-specific RSS feed  ###
103     parser = subparser_command.add_parser(
104         "fetch_fba_rss",
105         help="Fetches domains from a FBA-specific RSS feed.",
106     )
107     parser.set_defaults(command=commands.fetch_fba_rss)
108     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
109
110     ### Fetch blocks from FBA's bot account ###
111     parser = subparser_command.add_parser(
112         "fetch_fbabot_atom",
113         help="Fetches ATOM feed with domains from FBA's bot account.",
114     )
115     parser.set_defaults(command=commands.fetch_fbabot_atom)
116     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
117
118     ### Fetch blocks from oliphant's GIT repository ###
119     parser = subparser_command.add_parser(
120         "fetch_oliphant",
121         help="Fetches CSV files (block recommendations) for more possible instances to disover",
122     )
123     parser.set_defaults(command=commands.fetch_oliphant)
124     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
125
126     ### Fetch instances from given initial instance ###
127     parser = subparser_command.add_parser(
128         "fetch_instances",
129         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.",
130     )
131     parser.set_defaults(command=commands.fetch_instances)
132     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 .")
133     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
134
135     ### Fetch blocks from static text file(s) ###
136     parser = subparser_command.add_parser(
137         "fetch_txt",
138         help="Fetches text/plain files as simple domain lists",
139     )
140     parser.set_defaults(command=commands.fetch_txt)
141
142     ### Fetch blocks from joinfediverse.wiki ###
143     parser = subparser_command.add_parser(
144         "fetch_joinfediverse",
145         help="Fetches FediBlock page from joinfediverse.wiki",
146     )
147     parser.set_defaults(command=commands.fetch_joinfediverse)
148
149     ### Fetch instances JSON from instances.joinmobilizon.org
150     parser = subparser_command.add_parser(
151         "fetch_joinmobilizon",
152         help="Fetches instances from joinmobilizon",
153     )
154     parser.set_defaults(command=commands.fetch_joinmobilizon)
155
156     ### Fetch blocks from misskey.page ###
157     parser = subparser_command.add_parser(
158         "fetch_joinmisskey",
159         help="Fetches instances.json from misskey.page",
160     )
161     parser.set_defaults(command=commands.fetch_joinmisskey)
162
163     ### Fetch blocks from fediverse.observer ###
164     parser = subparser_command.add_parser(
165         "fetch_observer",
166         help="Fetches blocks from fediverse.observer.",
167     )
168     parser.set_defaults(command=commands.fetch_observer)
169     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
170
171     ### Fetch instances from fedipact.online ###
172     parser = subparser_command.add_parser(
173         "fetch_fedipact",
174         help="Fetches blocks from fedipact.online.",
175     )
176     parser.set_defaults(command=commands.fetch_fedipact)
177
178     ### Fetch from pixelfed.org's API ###
179     parser = subparser_command.add_parser(
180         "fetch_pixelfed_api",
181         help="Fetches domain names from pixelfed.org's API",
182     )
183     parser.set_defaults(command=commands.fetch_pixelfed_api)
184
185     ### Check nodeinfo ###
186     parser = subparser_command.add_parser(
187         "check_nodeinfo",
188         help="Checks if domain is part of nodeinfo.",
189     )
190     parser.set_defaults(command=commands.check_nodeinfo)
191
192     ### Fetch CSV from fedilist.com ###
193     parser = subparser_command.add_parser(
194         "fetch_fedilist",
195         help="Fetches CSV from fedilist.com",
196     )
197     parser.set_defaults(command=commands.fetch_fedilist)
198     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
199     parser.add_argument("--force", action="store_true", help="Include also already existing instances, otherwise only new are checked")
200
201     ### Update nodeinfo ###
202     parser = subparser_command.add_parser(
203         "update_nodeinfo",
204         help="Updates nodeinfo for all instances",
205     )
206     parser.set_defaults(command=commands.update_nodeinfo)
207     parser.add_argument("--domain", help="Instance name (aka. domain)")
208     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
209     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
210
211     ### Fetch instances from instances.social ###
212     parser = subparser_command.add_parser(
213         "fetch_instances_social",
214         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!",
215     )
216     parser.set_defaults(command=commands.fetch_instances_social)
217
218     ### Convert international domain names to punycode domains ###
219     parser = subparser_command.add_parser(
220         "convert_idna",
221         help="Convertes UTF-8 encoded international domain names into punycode (IDNA) domain names.",
222     )
223     parser.set_defaults(command=commands.convert_idna)
224
225     ### Fetch instances from ActivityPub relays ###
226     parser = subparser_command.add_parser(
227         "fetch_relays",
228         help="Fetches instances from ActivityPub relays",
229     )
230     parser.set_defaults(command=commands.fetch_relays)
231     parser.add_argument("--domain", help="Instance name (aka. 'relay')")
232     parser.add_argument("--force", action="store_true", help="Forces update of data, no matter what.")
233
234     logger.debug("EXIT!")
235
236 def run_command():
237     logger.debug("CALLED!")
238     args = _PARSER.parse_args()
239
240     if args.log_level is not None:
241         loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
242         for _logger in loggers:
243             _logger.setLevel(args.log_level)
244
245     logger.debug("args[%s]='%s'", type(args), args)
246     status = args.command(args)
247
248     logger.debug("status=%d - EXIT!", status)
249     return status
250
251 def shutdown():
252     logger.debug("Closing database connection ...")
253     database.connection.close()
254     locking.release()
255     logger.debug("Shutdown completed.")