We can now manage relay servers and can send content to them
[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\App;
26 use Friendica\Database\DBA;
27 use Friendica\Model\APContact;
28 use Friendica\Model\Contact;
29 use Friendica\Protocol\ActivityPub\Transmitter;
30
31 /**
32  * tool to access the system config from the CLI
33  *
34  * With this script you can access the system configuration of your node from
35  * the CLI. You can do both, reading current values stored in the database and
36  * set new values to config variables.
37  *
38  * Usage:
39  *   If you specify no parameters at the CLI, the script will list all config
40  *   variables defined.
41  *
42  *   If you specify one parameter, the script will list all config variables
43  *   defined in this section of the configuration (e.g. "system").
44  *
45  *   If you specify two parameters, the script will show you the current value
46  *   of the named configuration setting. (e.g. "system loglevel")
47  *
48  *   If you specify three parameters, the named configuration setting will be
49  *   set to the value of the last parameter. (e.g. "system loglevel 0" will
50  *   disable logging)
51  */
52 class Relay extends \Asika\SimpleConsole\Console
53 {
54         protected $helpOptions = ['h', 'help', '?'];
55
56         /**
57          * @var App\Mode
58          */
59         private $appMode;
60
61         protected function getHelp()
62         {
63                 $help = <<<HELP
64 console relay - Manage ActivityPub relay configuration
65 Synopsis
66         bin/console relay [-h|--help|-?] [-v]
67         bin/console relay add <actor> [-h|--help|-?] [-v]
68         bin/console relay remove <actoor> [-h|--help|-?] [-v]
69
70 Description
71         bin/console relay
72                 Lists all active relais
73
74         bin/console relay add <actor>
75                 Add a relay actor
76
77         bin/console relay remove <actoor>
78                 Remove a relay actor
79
80 Options
81     -h|--help|-? Show help information
82     -v           Show more debug information.
83 HELP;
84                 return $help;
85         }
86
87         public function __construct(App\Mode $appMode, array $argv = null)
88         {
89                 parent::__construct($argv);
90
91                 $this->appMode = $appMode;
92         }
93
94         protected function doExecute()
95         {
96                 if ($this->getOption('v')) {
97                         $this->out('Executable: ' . $this->executable);
98                         $this->out('Class: ' . __CLASS__);
99                         $this->out('Arguments: ' . var_export($this->args, true));
100                         $this->out('Options: ' . var_export($this->options, true));
101                 }
102
103                 if (count($this->args) > 2) {
104                         throw new CommandArgsException('Too many arguments');
105                 }
106
107                 if (count($this->args) == 1) {
108                         throw new CommandArgsException('Too few arguments');
109                 }
110
111                 if (count($this->args) == 0) {
112                         $contacts = DBA::select('apcontact', ['url'],
113                         ["`type` = ? AND `url` IN (SELECT `url` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?))",
114                                 'Application', 0, Contact::FOLLOWER, Contact::FRIEND]);
115                         while ($contact = DBA::fetch($contacts)) {
116                                 $this->out($contact['url']);
117                         }
118                         DBA::close($contacts);
119                 }
120
121                 if (count($this->args) == 2) {
122                         $mode = $this->getArgument(0);
123                         $actor = $this->getArgument(1);
124
125                         $apcontact = APContact::getByURL($actor);
126                         if (empty($apcontact) || ($apcontact['type'] != 'Application')) {
127                                 $this->out($actor . ' is no relay actor');
128                                 return 1;
129                         }
130
131                         if ($mode == 'add') {
132                                 if (Transmitter::sendRelayFollow($actor)) {
133                                         $this->out('Successfully added ' . $actor);
134                                 } else {
135                                         $this->out($actor . " couldn't be added");
136                                 }
137                         } elseif ($mode == 'remove') {
138                                 if (Transmitter::sendRelayUndoFollow($actor)) {
139                                         $this->out('Successfully removed ' . $actor);
140                                 } else {
141                                         $this->out($actor . " couldn't be removed");
142                                 }
143                         } else {
144                                 throw new CommandArgsException($mode . ' is no valid command');
145                         }
146                 }
147
148                 return 0;
149         }
150 }