3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\Model\APContact;
26 use Friendica\Model\Contact;
27 use Friendica\Protocol\ActivityPub\Transmitter;
30 * tool to control the list of ActivityPub relay servers from the CLI
32 * With this script you can access the relay servers of your node from
35 class Relay extends \Asika\SimpleConsole\Console
37 protected $helpOptions = ['h', 'help', '?'];
40 * @var $dba Friendica\Database\Database
45 protected function getHelp()
48 console relay - Manage ActivityPub relay configuration
50 bin/console relay list [-h|--help|-?] [-v]
51 bin/console relay add <actor> [-h|--help|-?] [-v]
52 bin/console relay remove <actor> [-f|--force] [-h|--help|-?] [-v]
55 bin/console relay list
56 Lists all active relay servers
58 bin/console relay add <actor>
59 Add a relay actor in the format https://relayserver.tld/actor
61 bin/console relay remove <actor>
62 Remove a relay actor in the format https://relayserver.tld/actor
65 -f|--force Change the relay status in the system even if the unsubscribe message failed
66 -h|--help|-? Show help information
67 -v Show more debug information.
72 public function __construct(\Friendica\Database\Database $dba, array $argv = null)
74 parent::__construct($argv);
79 protected function doExecute(): int
81 if ($this->getOption('v')) {
82 $this->out('Executable: ' . $this->executable);
83 $this->out('Class: ' . __CLASS__);
84 $this->out('Arguments: ' . var_export($this->args, true));
85 $this->out('Options: ' . var_export($this->options, true));
88 if (count($this->args) > 2) {
89 throw new CommandArgsException('Too many arguments');
92 if ((count($this->args) == 1) && ($this->getArgument(0) == 'list')) {
93 $contacts = $this->dba->select('apcontact', ['url'],
94 ["`type` IN (?, ?) AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)",
95 'Application', 'Service', 0, Contact::FRIEND]);
96 while ($contact = $this->dba->fetch($contacts)) {
97 $this->out($contact['url']);
99 $this->dba->close($contacts);
100 } elseif (count($this->args) == 0) {
101 throw new CommandArgsException('too few arguments');
102 } elseif (count($this->args) == 1) {
103 throw new CommandArgsException($this->getArgument(0) . ' is no valid command');
106 if (count($this->args) == 2) {
107 $mode = $this->getArgument(0);
108 $actor = $this->getArgument(1);
110 $apcontact = APContact::getByURL($actor);
111 if (empty($apcontact) || !in_array($apcontact['type'], ['Application', 'Service'])) {
112 $this->out($actor . ' is no relay actor');
116 if ($mode == 'add') {
117 if (Transmitter::sendRelayFollow($actor)) {
118 $this->out('Successfully added ' . $actor);
120 $this->out($actor . " couldn't be added");
122 } elseif ($mode == 'remove') {
123 $force = $this->getOption(['f', 'force'], false);
125 if (Transmitter::sendRelayUndoFollow($actor, $force)) {
126 $this->out('Successfully removed ' . $actor);
128 $this->out($actor . " couldn't be removed");
130 $this->out($actor . " is forcefully removed");
133 throw new CommandArgsException($mode . ' is no valid command');