Improved description
[friendica.git/.git] / src / Console / Relay.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 [-h|--help|-?] [-v]
51         bin/console relay add <actor> [-h|--help|-?] [-v]
52         bin/console relay remove <actoor> [-h|--help|-?] [-v]
53
54 Description
55         bin/console relay
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     -h|--help|-? Show help information
66     -v           Show more debug information.
67 HELP;
68                 return $help;
69         }
70
71         public function __construct(\Friendica\Database\Database $dba, array $argv = null)
72         {
73                 parent::__construct($argv);
74
75                 $this->dba = $dba;
76         }
77
78         protected function doExecute()
79         {
80                 if ($this->getOption('v')) {
81                         $this->out('Executable: ' . $this->executable);
82                         $this->out('Class: ' . __CLASS__);
83                         $this->out('Arguments: ' . var_export($this->args, true));
84                         $this->out('Options: ' . var_export($this->options, true));
85                 }
86
87                 if (count($this->args) > 2) {
88                         throw new CommandArgsException('Too many arguments');
89                 }
90
91                 if (count($this->args) == 1) {
92                         throw new CommandArgsException('Too few arguments');
93                 }
94
95                 if (count($this->args) == 0) {
96                         $contacts = $this->dba->select('apcontact', ['url'],
97                         ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))",
98                                 'Application', 0, Contact::FOLLOWER, Contact::FRIEND]);
99                         while ($contact = $this->dba->fetch($contacts)) {
100                                 $this->out($contact['url']);
101                         }
102                         $this->dba->close($contacts);
103                 }
104
105                 if (count($this->args) == 2) {
106                         $mode = $this->getArgument(0);
107                         $actor = $this->getArgument(1);
108
109                         $apcontact = APContact::getByURL($actor);
110                         if (empty($apcontact) || ($apcontact['type'] != 'Application')) {
111                                 $this->out($actor . ' is no relay actor');
112                                 return 1;
113                         }
114
115                         if ($mode == 'add') {
116                                 if (Transmitter::sendRelayFollow($actor)) {
117                                         $this->out('Successfully added ' . $actor);
118                                 } else {
119                                         $this->out($actor . " couldn't be added");
120                                 }
121                         } elseif ($mode == 'remove') {
122                                 if (Transmitter::sendRelayUndoFollow($actor)) {
123                                         $this->out('Successfully removed ' . $actor);
124                                 } else {
125                                         $this->out($actor . " couldn't be removed");
126                                 }
127                         } else {
128                                 throw new CommandArgsException($mode . ' is no valid command');
129                         }
130                 }
131
132                 return 0;
133         }
134 }