]> git.mxchange.org Git - fba.git/blob - fba/boot.py
d72c8e4c59279340d360378da00dc439834575f9
[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.",
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.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
59     parser.set_defaults(command=commands.check_instance)
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.add_argument("--domain", help="Instance name (aka. domain)")
74     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
75     parser.set_defaults(command=commands.recheck_obfuscation)
76
77     ### Fetch blocks from registered instances or given ###
78     parser = subparser_command.add_parser(
79         "fetch_blocks",
80         help="Fetches blocks from registered instances (run command fetch_instances first!).",
81     )
82     parser.add_argument("--domain", help="Instance name (aka. domain)")
83     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
84     parser.set_defaults(command=commands.fetch_blocks)
85
86     ### Fetch blocks from chaos.social ###
87     parser = subparser_command.add_parser(
88         "fetch_cs",
89         help="Fetches blocks from chaos.social's meta sub domain.",
90     )
91     parser.set_defaults(command=commands.fetch_cs)
92
93     ### Fetch blocks from todon.eu wiki ###
94     parser = subparser_command.add_parser(
95         "fetch_todon_wiki",
96         help="Fetches blocks from todon.eu's wiki.",
97     )
98     parser.set_defaults(command=commands.fetch_todon_wiki)
99
100     ### Fetch blocks from a FBA-specific RSS feed  ###
101     parser = subparser_command.add_parser(
102         "fetch_fba_rss",
103         help="Fetches domains from a FBA-specific RSS feed.",
104     )
105     parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
106     parser.set_defaults(command=commands.fetch_fba_rss)
107
108     ### Fetch blocks from FBA's bot account ###
109     parser = subparser_command.add_parser(
110         "fetch_fbabot_atom",
111         help="Fetches ATOM feed with domains from FBA's bot account.",
112     )
113     parser.set_defaults(command=commands.fetch_fbabot_atom)
114
115     ### Fetch blocks from oliphant's GIT repository ###
116     parser = subparser_command.add_parser(
117         "fetch_oliphant",
118         help="Fetches CSV files (block recommendations) for more possible instances to disover",
119     )
120     parser.add_argument("--domain", help="Instance name (aka. domain) to check")
121     parser.set_defaults(command=commands.fetch_oliphant)
122
123     ### Fetch instances from given initial instance ###
124     parser = subparser_command.add_parser(
125         "fetch_instances",
126         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.",
127     )
128     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 .")
129     parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
130     parser.set_defaults(command=commands.fetch_instances)
131
132     ### Fetch blocks from static text file(s) ###
133     parser = subparser_command.add_parser(
134         "fetch_txt",
135         help="Fetches text/plain files as simple domain lists",
136     )
137     parser.set_defaults(command=commands.fetch_txt)
138
139     ### Fetch blocks from joinfediverse.wiki ###
140     parser = subparser_command.add_parser(
141         "fetch_joinfediverse",
142         help="Fetches FediBlock page from joinfediverse.wiki",
143     )
144     parser.set_defaults(command=commands.fetch_joinfediverse)
145
146     ### Fetch blocks from fediverse.observer ###
147     parser = subparser_command.add_parser(
148         "fetch_observer",
149         help="Fetches blocks from fediverse.observer.",
150     )
151     parser.set_defaults(command=commands.fetch_observer)
152     parser.add_argument("--software", help="Name of software, e.g. 'lemmy'")
153
154     ### Fetch instances from fedipact.online ###
155     parser = subparser_command.add_parser(
156         "fetch_fedipact",
157         help="Fetches blocks from fedipact.online.",
158     )
159     parser.set_defaults(command=commands.fetch_fedipact)
160
161     ### Fetch from pixelfed.org's API ###
162     parser = subparser_command.add_parser(
163         "fetch_pixelfed_api",
164         help="Fetches domain names from pixelfed.org's API",
165     )
166     parser.set_defaults(command=commands.fetch_pixelfed_api)
167
168     logger.debug("EXIT!")
169
170 def run_command():
171     logger.debug("CALLED!")
172     args = _PARSER.parse_args()
173
174     if args.log_level is not None:
175         loggers = [logging.getLogger(name) for name in logging.root.manager.loggerDict]
176         for _logger in loggers:
177             _logger.setLevel(args.log_level)
178
179     logger.debug("args[%s]='%s'", type(args), args)
180     status = args.command(args)
181
182     logger.debug("status=%d - EXIT!", status)
183     return status
184
185 def shutdown():
186     logger.debug("Closing database connection ...")
187     database.connection.close()
188     locking.release()
189     logger.debug("Shutdown completed.")