2020-09-15 17:45:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2022-01-02 07:27:47 +00:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-09-15 17:45:19 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Console;
|
|
|
|
|
|
|
|
use Asika\SimpleConsole\CommandArgsException;
|
|
|
|
use Friendica\Model\APContact;
|
|
|
|
use Friendica\Model\Contact;
|
|
|
|
use Friendica\Protocol\ActivityPub\Transmitter;
|
|
|
|
|
|
|
|
/**
|
2020-09-16 18:38:36 +00:00
|
|
|
* tool to control the list of ActivityPub relay servers from the CLI
|
2020-09-15 17:45:19 +00:00
|
|
|
*
|
2020-09-16 18:38:36 +00:00
|
|
|
* With this script you can access the relay servers of your node from
|
|
|
|
* the CLI.
|
2020-09-15 17:45:19 +00:00
|
|
|
*/
|
|
|
|
class Relay extends \Asika\SimpleConsole\Console
|
|
|
|
{
|
|
|
|
protected $helpOptions = ['h', 'help', '?'];
|
|
|
|
|
|
|
|
/**
|
2020-09-16 18:38:36 +00:00
|
|
|
* @var $dba Friendica\Database\Database
|
2020-09-15 17:45:19 +00:00
|
|
|
*/
|
2020-09-16 18:38:36 +00:00
|
|
|
private $dba;
|
|
|
|
|
2020-09-15 17:45:19 +00:00
|
|
|
|
|
|
|
protected function getHelp()
|
|
|
|
{
|
|
|
|
$help = <<<HELP
|
|
|
|
console relay - Manage ActivityPub relay configuration
|
|
|
|
Synopsis
|
2020-09-21 07:42:53 +00:00
|
|
|
bin/console relay list [-h|--help|-?] [-v]
|
2020-09-15 17:45:19 +00:00
|
|
|
bin/console relay add <actor> [-h|--help|-?] [-v]
|
2020-09-29 05:06:37 +00:00
|
|
|
bin/console relay remove <actor> [-f|--force] [-h|--help|-?] [-v]
|
2020-09-15 17:45:19 +00:00
|
|
|
|
|
|
|
Description
|
2020-09-21 07:42:53 +00:00
|
|
|
bin/console relay list
|
2020-09-16 18:23:27 +00:00
|
|
|
Lists all active relay servers
|
2020-09-15 17:45:19 +00:00
|
|
|
|
|
|
|
bin/console relay add <actor>
|
2020-09-17 04:36:32 +00:00
|
|
|
Add a relay actor in the format https://relayserver.tld/actor
|
2020-09-15 17:45:19 +00:00
|
|
|
|
2020-09-16 18:23:27 +00:00
|
|
|
bin/console relay remove <actor>
|
2020-09-17 04:36:32 +00:00
|
|
|
Remove a relay actor in the format https://relayserver.tld/actor
|
2020-09-15 17:45:19 +00:00
|
|
|
|
|
|
|
Options
|
2020-09-29 05:06:37 +00:00
|
|
|
-f|--force Change the relay status in the system even if the unsubscribe message failed
|
2020-09-15 17:45:19 +00:00
|
|
|
-h|--help|-? Show help information
|
|
|
|
-v Show more debug information.
|
|
|
|
HELP;
|
|
|
|
return $help;
|
|
|
|
}
|
|
|
|
|
2020-09-16 18:38:36 +00:00
|
|
|
public function __construct(\Friendica\Database\Database $dba, array $argv = null)
|
2020-09-15 17:45:19 +00:00
|
|
|
{
|
|
|
|
parent::__construct($argv);
|
|
|
|
|
2020-09-16 18:23:27 +00:00
|
|
|
$this->dba = $dba;
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function doExecute()
|
|
|
|
{
|
|
|
|
if ($this->getOption('v')) {
|
|
|
|
$this->out('Executable: ' . $this->executable);
|
|
|
|
$this->out('Class: ' . __CLASS__);
|
|
|
|
$this->out('Arguments: ' . var_export($this->args, true));
|
|
|
|
$this->out('Options: ' . var_export($this->options, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) > 2) {
|
|
|
|
throw new CommandArgsException('Too many arguments');
|
|
|
|
}
|
|
|
|
|
2020-09-21 07:42:53 +00:00
|
|
|
if ((count($this->args) == 1) && ($this->getArgument(0) == 'list')) {
|
2020-09-16 18:23:27 +00:00
|
|
|
$contacts = $this->dba->select('apcontact', ['url'],
|
2021-01-09 19:19:20 +00:00
|
|
|
["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)",
|
|
|
|
'Application', 0, Contact::FRIEND]);
|
2020-09-16 18:23:27 +00:00
|
|
|
while ($contact = $this->dba->fetch($contacts)) {
|
2020-09-15 17:45:19 +00:00
|
|
|
$this->out($contact['url']);
|
|
|
|
}
|
2020-09-16 18:23:27 +00:00
|
|
|
$this->dba->close($contacts);
|
2020-09-21 07:42:53 +00:00
|
|
|
} elseif (count($this->args) == 0) {
|
|
|
|
throw new CommandArgsException('too few arguments');
|
|
|
|
} elseif (count($this->args) == 1) {
|
|
|
|
throw new CommandArgsException($this->getArgument(0) . ' is no valid command');
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->args) == 2) {
|
|
|
|
$mode = $this->getArgument(0);
|
|
|
|
$actor = $this->getArgument(1);
|
|
|
|
|
|
|
|
$apcontact = APContact::getByURL($actor);
|
|
|
|
if (empty($apcontact) || ($apcontact['type'] != 'Application')) {
|
|
|
|
$this->out($actor . ' is no relay actor');
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($mode == 'add') {
|
|
|
|
if (Transmitter::sendRelayFollow($actor)) {
|
|
|
|
$this->out('Successfully added ' . $actor);
|
|
|
|
} else {
|
|
|
|
$this->out($actor . " couldn't be added");
|
|
|
|
}
|
|
|
|
} elseif ($mode == 'remove') {
|
2020-09-29 05:06:37 +00:00
|
|
|
$force = $this->getOption(['f', 'force'], false);
|
|
|
|
|
|
|
|
if (Transmitter::sendRelayUndoFollow($actor, $force)) {
|
2020-09-15 17:45:19 +00:00
|
|
|
$this->out('Successfully removed ' . $actor);
|
2020-09-29 05:06:37 +00:00
|
|
|
} elseif (!$force) {
|
2020-09-15 17:45:19 +00:00
|
|
|
$this->out($actor . " couldn't be removed");
|
2020-09-29 05:06:37 +00:00
|
|
|
} else {
|
|
|
|
$this->out($actor . " is forcefully removed");
|
2020-09-15 17:45:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new CommandArgsException($mode . ' is no valid command');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|