1 # Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
2 # Copyright (C) 2023 Free Software Foundation
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.
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.
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/>.
23 from fba import commands
27 lockfile = tempfile.gettempdir() + '/fba.lock'
34 print(f"DEBUG: Acquiring lock: '{lockfile}'")
35 LOCK = zc.lockfile.LockFile(lockfile)
36 print("DEBUG: Lock obtained.")
38 except zc.lockfile.LockError:
39 print(f"ERROR: Cannot aquire lock: '{lockfile}'")
43 # DEBUG: print("DEBUG: init_parser(): CALLED!")
46 # DEBUG: print("DEBUG: Initializing parser ...")
47 _PARSER = argparse.ArgumentParser(
48 description="Fetches block reasons from the fediverse",
49 epilog="Please note that some commands have optional arguments, you may want to try fba.py <command> --help to find them out.",
51 subparser_command = _PARSER.add_subparsers(
53 title="Commands to execute",
55 help="Command to perform",
58 ### Check instance ###
59 parser = subparser_command.add_parser(
61 help="Checks given instance if it exists and returns proper exit code"
63 parser.add_argument("--domain", required=True, help="Instance name (aka. domain) to check")
64 parser.set_defaults(command=commands.check_instance)
66 ### Fetch from bka.li ###
67 parser = subparser_command.add_parser(
69 help="Fetches domain names from bka.li API",
71 parser.set_defaults(command=commands.fetch_bkali)
73 ### Fetch blocks from registered instances or given ###
74 parser = subparser_command.add_parser(
76 help="Fetches blocks from registered instances (run command fetch_instances first!).",
78 parser.add_argument("--domain", help="Instance name (aka. domain) to fetch blocks from")
79 parser.set_defaults(command=commands.fetch_blocks)
81 ### Fetch blocks from chaos.social ###
82 parser = subparser_command.add_parser(
84 help="Fetches blocks from chaos.social's meta sub domain.",
86 parser.set_defaults(command=commands.fetch_cs)
88 ### Fetch blocks from a FBA-specific RSS feed ###
89 parser = subparser_command.add_parser(
91 help="Fetches domains from a FBA-specific RSS feed.",
93 parser.add_argument("--feed", required=True, help="RSS feed to fetch domains from (e.g. https://fba.ryoma.agency/rss?domain=foo.bar).")
94 parser.set_defaults(command=commands.fetch_fba_rss)
96 ### Fetch blocks from FBA's bot account ###
97 parser = subparser_command.add_parser(
99 help="Fetches ATOM feed with domains from FBA's bot account. You may wish to re-run this command several times (at least 3 with big instances) to have a decent amount of valid instances.",
101 parser.set_defaults(command=commands.fetch_fbabot_atom)
103 ### Fetch blocks from federater ###
104 parser = subparser_command.add_parser(
106 help="Fetches CSV file (block recommendations) for more possible instances to disover",
108 parser.set_defaults(command=commands.fetch_federater)
110 ### Fetch instances from given initial instance ###
111 parser = subparser_command.add_parser(
113 help="Fetches instances (aka. \"domains\") from an initial instance.",
115 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 .")
116 parser.add_argument("--single", action="store_true", help="Only fetch given instance.")
117 parser.set_defaults(command=commands.fetch_instances)
119 # DEBUG: print("DEBUG: init_parser(): EXIT!")
122 # DEBUG: print("DEBUG: run_command(): CALLED!")
123 args = _PARSER.parse_args()
124 # DEBUG: print(f"DEBUG: args[{type(args)}]={args}")
125 status = args.command(args)
126 # DEBUG: print("DEBUG: status={status} - EXIT!")
127 return status if isinstance(status, int) else 0
130 print("DEBUG: Closing database connection ...")
131 fba.connection.close()
134 print("DEBUG: Releasing lock ...")
136 print(f"DEBUG: Deleting lockfile='{lockfile}' ...")
139 print("DEBUG: Shutdown completed.")