]> git.mxchange.org Git - friendica.git/blob - src/Console/Relay.php
Merge pull request #11524 from annando/cache-endpoints
[friendica.git] / src / Console / Relay.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Console;
23
24 use Asika\SimpleConsole\CommandArgsException;
25 use Friendica\Model\APContact;
26 use Friendica\Model\Contact;
27 use Friendica\Protocol\ActivityPub\Transmitter;
28
29 /**
30  * tool to control the list of ActivityPub relay servers from the CLI
31  *
32  * With this script you can access the relay servers of your node from
33  * the CLI.
34  */
35 class Relay extends \Asika\SimpleConsole\Console
36 {
37         protected $helpOptions = ['h', 'help', '?'];
38
39         /**
40          * @var $dba Friendica\Database\Database
41          */
42         private $dba;
43
44
45         protected function getHelp()
46         {
47                 $help = <<<HELP
48 console relay - Manage ActivityPub relay configuration
49 Synopsis
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]
53
54 Description
55         bin/console relay list
56                 Lists all active relay servers
57
58         bin/console relay add <actor>
59                 Add a relay actor in the format https://relayserver.tld/actor
60
61         bin/console relay remove <actor>
62                 Remove a relay actor in the format https://relayserver.tld/actor
63
64 Options
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.
68 HELP;
69                 return $help;
70         }
71
72         public function __construct(\Friendica\Database\Database $dba, array $argv = null)
73         {
74                 parent::__construct($argv);
75
76                 $this->dba = $dba;
77         }
78
79         protected function doExecute()
80         {
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));
86                 }
87
88                 if (count($this->args) > 2) {
89                         throw new CommandArgsException('Too many arguments');
90                 }
91
92                 if ((count($this->args) == 1) && ($this->getArgument(0) == 'list')) {
93                         $contacts = $this->dba->select('apcontact', ['url'],
94                         ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` = ?)",
95                                 'Application', 0, Contact::FRIEND]);
96                         while ($contact = $this->dba->fetch($contacts)) {
97                                 $this->out($contact['url']);
98                         }
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');
104                 }
105
106                 if (count($this->args) == 2) {
107                         $mode = $this->getArgument(0);
108                         $actor = $this->getArgument(1);
109
110                         $apcontact = APContact::getByURL($actor);
111                         if (empty($apcontact) || ($apcontact['type'] != 'Application')) {
112                                 $this->out($actor . ' is no relay actor');
113                                 return 1;
114                         }
115
116                         if ($mode == 'add') {
117                                 if (Transmitter::sendRelayFollow($actor)) {
118                                         $this->out('Successfully added ' . $actor);
119                                 } else {
120                                         $this->out($actor . " couldn't be added");
121                                 }
122                         } elseif ($mode == 'remove') {
123                                 $force = $this->getOption(['f', 'force'], false);
124
125                                 if (Transmitter::sendRelayUndoFollow($actor, $force)) {
126                                         $this->out('Successfully removed ' . $actor);
127                                 } elseif (!$force) {
128                                         $this->out($actor . " couldn't be removed");
129                                 } else {
130                                         $this->out($actor . " is forcefully removed");
131                                 }
132                         } else {
133                                 throw new CommandArgsException($mode . ' is no valid command');
134                         }
135                 }
136
137                 return 0;
138         }
139 }