]> git.mxchange.org Git - friendica.git/blob - src/Console/Contact.php
Issue 10747: Improved check for blocked domains
[friendica.git] / src / Console / Contact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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 Console_Table;
25 use Friendica\App;
26 use Friendica\Model\Contact as ContactModel;
27 use Friendica\Model\User as UserModel;
28 use Friendica\Network\Probe;
29 use Friendica\Util\Temporal;
30 use RuntimeException;
31 use Seld\CliPrompt\CliPrompt;
32
33 /**
34  * tool to manage contacts of users of the current node
35  */
36 class Contact extends \Asika\SimpleConsole\Console
37 {
38         protected $helpOptions = ['h', 'help', '?'];
39
40         /**
41          * @var App\Mode
42          */
43         private $appMode;
44         /**
45          * @var IPConfig
46          */
47         private $pConfig;
48
49         protected function getHelp()
50         {
51                 $help = <<<HELP
52 console contact - Modify contact settings per console commands.
53 Usage
54         bin/console contact add <user nick> <URL> [<network>] [-h|--help|-?] [-v]
55         bin/console contact remove <CID> [-h|--help|-?] [-v]
56         bin/console contact search id <CID> [-h|--help|-?] [-v]
57         bin/console contact search url <user nick> <URL> [-h|--help|-?] [-v]
58         bin/console contact terminate <CID> [-h|--help|-?] [-v]
59
60 Description
61         Modify contact settings per console commands.
62
63 Options
64     -h|--help|-? Show help information
65     -v           Show more debug information
66     -y           Non-interactive mode, assume "yes" as answer to the user deletion prompt
67 HELP;
68                 return $help;
69         }
70
71         public function __construct(App\Mode $appMode, array $argv = null)
72         {
73                 parent::__construct($argv);
74
75                 $this->appMode = $appMode;
76         }
77
78         protected function doExecute()
79         {
80                 if ($this->getOption('v')) {
81                         $this->out('Class: ' . __CLASS__);
82                         $this->out('Arguments: ' . var_export($this->args, true));
83                         $this->out('Options: ' . var_export($this->options, true));
84                 }
85
86                 if (count($this->args) == 0) {
87                         $this->out($this->getHelp());
88                         return 0;
89                 }
90
91                 if ($this->appMode->isInstall()) {
92                         throw new RuntimeException('Database isn\'t ready or populated yet');
93                 }
94
95                 $command = $this->getArgument(0);
96
97                 switch ($command) {
98                         case 'add':
99                                 return $this->addContact();
100                         case 'remove':
101                                 return $this->removeContact();
102                         case 'search':
103                                 return $this->searchContact();
104                         case 'terminate':
105                                 return $this->terminateContact();
106                         default:
107                                 throw new \Asika\SimpleConsole\CommandArgsException('Wrong command.');
108                 }
109         }
110
111         /**
112          * Retrieves the user from a nick supplied as an argument or from a prompt
113          *
114          * @param int $arg_index Index of the nick argument in the arguments list
115          *
116          * @return array|boolean User record with uid field, or false if user is not found
117          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
118          */
119         private function getUserByNick($arg_index)
120         {
121                 $nick = $this->getArgument($arg_index);
122
123                 if (empty($nick)) {
124                         $this->out('Enter user nickname: ');
125                         $nick = CliPrompt::prompt();
126                         if (empty($nick)) {
127                                 throw new RuntimeException('A user nickname must be specified.');
128                         }
129                 }
130
131                 $user = UserModel::getByNickname($nick, ['uid', 'nickname']);
132                 if (empty($user)) {
133                         throw new RuntimeException('User not found');
134                 }
135
136                 return $user;
137         }
138
139         /**
140          * Adds a contact to a user from a URL
141          *
142          * @return bool True, if the command was successful
143          */
144         private function addContact()
145         {
146                 $user = $this->getUserByNick(1);
147
148                 $url = $this->getArgument(2);
149                 if (empty($url)) {
150                         $this->out('Enter contact URL: ');
151                         $url = CliPrompt::prompt();
152                         if (empty($url)) {
153                                 throw new RuntimeException('A contact URL must be specified.');
154                         }
155                 }
156
157                 $url = Probe::cleanURI($url);
158
159                 $contact = ContactModel::getByURLForUser($url, $user['uid']);
160                 if (!empty($contact)) {
161                         throw new RuntimeException('Contact already exists');
162                 }
163
164                 $network = $this->getArgument(3);
165                 if ($network === null) {
166                         $this->out('Enter network, or leave blank: ');
167                         $network = CliPrompt::prompt();
168                 }
169
170                 $result = ContactModel::createFromProbeForUser($user['uid'], $url, $network);
171
172                 if ($result['success']) {
173                         $this->out('User ' . $user['nickname'] . ' now connected to ' . $url . ', contact ID ' . $result['cid']);
174                 } else {
175                         throw new RuntimeException($result['message']);
176                 }
177         }
178
179         /**
180          * Sends an unfriend message.  Does not remove the contact
181          *
182          * @return bool True, if the command was successful
183          */
184         private function terminateContact()
185         {
186                 $cid = $this->getArgument(1);
187                 if (empty($cid)) {
188                         $this->out('Enter contact ID: ');
189                         $cid = CliPrompt::prompt();
190                         if (empty($cid)) {
191                                 throw new RuntimeException('A contact ID must be specified.');
192                         }
193                 }
194
195                 $contact = ContactModel::getById($cid);
196                 if (empty($contact)) {
197                         throw new RuntimeException('Contact not found');
198                 }
199
200                 $user = UserModel::getById($contact['uid']);
201
202                 $result = ContactModel::terminateFriendship($user, $contact);
203         }
204
205         /**
206          * Marks a contact for removal
207          *
208          * @return bool True, if the command was successful
209          */
210         private function removeContact()
211         {
212                 $cid = $this->getArgument(1);
213                 if (empty($cid)) {
214                         $this->out('Enter contact ID: ');
215                         $cid = CliPrompt::prompt();
216                         if (empty($cid)) {
217                                 throw new RuntimeException('A contact ID must be specified.');
218                         }
219                 }
220
221                 $result = ContactModel::remove($cid);
222         }
223
224         /**
225          * Returns a contact based on search parameter
226          *
227          * @return bool True, if the command was successful
228          */
229         private function searchContact()
230         {
231                 $fields = [
232                         'id',
233                         'uid',
234                         'network',
235                         'name',
236                         'nick',
237                         'url',
238                         'addr',
239                         'created',
240                         'updated',
241                         'blocked',
242                         'deleted',
243                 ];
244
245                 $subCmd = $this->getArgument(1);
246
247                 $table = new Console_Table();
248                 $table->setHeaders(['ID', 'UID', 'Network', 'Name', 'Nick', 'URL', 'E-Mail', 'Created', 'Updated', 'Blocked', 'Deleted']);
249
250                 $addRow = function ($row) use (&$table) {
251                         $table->addRow([
252                                 $row['id'],
253                                 $row['uid'],
254                                 $row['network'],
255                                 $row['name'],
256                                 $row['nick'],
257                                 $row['url'],
258                                 $row['addr'],
259                                 Temporal::getRelativeDate($row['created']),
260                                 Temporal::getRelativeDate($row['updated']),
261                                 $row['blocked'],
262                                 $row['deleted'],
263                         ]);
264                 };
265                 switch ($subCmd) {
266                         case 'id':
267                                 $cid     = $this->getArgument(2);
268                                 $contact = ContactModel::getById($cid, $fields);
269                                 if (!empty($contact)) {
270                                         $addRow($contact);
271                                 }
272                                 break;
273                         case 'url':
274                                 $user    = $this->getUserByNick(2);
275                                 $url     = $this->getArgument(3);
276                                 $contact = ContactModel::getByURLForUser($url, $user['uid'], false, $fields);
277                                 if (!empty($contact)) {
278                                         $addRow($contact);
279                                 }
280                                 break;
281                         default:
282                                 $this->out($this->getHelp());
283                                 return false;
284                 }
285
286                 $this->out($table->getTable());
287
288                 return true;
289         }
290 }