]> git.mxchange.org Git - friendica.git/blob - src/Console/Contact.php
Add console contact command
[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\Content\Pager;
27 use Friendica\Model\Contact as ContactModel;
28 use Friendica\Model\User as UserModel;
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                 $contact = ContactModel::getByURLForUser($url, $user['uid']);
158                 if (!empty($contact)) {
159                         throw new RuntimeException('Contact already exists');
160                 }
161
162                 $network = $this->getArgument(3);
163                 if (empty($network) && $network !== '') {
164                         $this->out('Enter network, or leave blank: ');
165                         $network = CliPrompt::prompt();
166                 }
167
168                 $result = ContactModel::createFromProbe($user, $url, false, $network);
169
170                 if ($result['success']) {
171                         $this->out('User ' . $user['nickname'] . ' now connected to ' . $url . ', contact ID ' . $result['cid']);
172                 }
173                 else {
174                         throw new RuntimeException($result['message']);
175                 }
176         }
177
178         /**
179          * Sends an unfriend message.  Does not remove the contact
180          *
181          * @return bool True, if the command was successful
182          */
183         private function terminateContact()
184         {
185                 $cid = $this->getArgument(1);
186                 if (empty($cid)) {
187                         $this->out('Enter contact ID: ');
188                         $cid = CliPrompt::prompt();
189                         if (empty($cid)) {
190                                 throw new RuntimeException('A contact ID must be specified.');
191                         }
192                 }
193
194                 $contact = ContactModel::getById($cid);
195                 if (empty($contact)) {
196                         throw new RuntimeException('Contact not found');
197                 }
198
199                 $user = UserModel::getById($contact['uid']);
200
201                 $result = ContactModel::terminateFriendship($user, $contact);
202         }
203
204         /**
205          * Marks a contact for removal
206          *
207          * @return bool True, if the command was successful
208          */
209         private function removeContact()
210         {
211                 $cid = $this->getArgument(1);
212                 if (empty($cid)) {
213                         $this->out('Enter contact ID: ');
214                         $cid = CliPrompt::prompt();
215                         if (empty($cid)) {
216                                 throw new RuntimeException('A contact ID must be specified.');
217                         }
218                 }
219
220                 $result = ContactModel::remove($cid);
221         }
222
223         /**
224          * Returns a contact based on search parameter
225          *
226          * @return bool True, if the command was successful
227          */
228         private function searchContact()
229         {
230                 $fields = [
231                         'id',
232                         'uid',
233                         'network',
234                         'name',
235                         'nick',
236                         'url',
237                         'addr',
238                         'created',
239                         'updated',
240                         'blocked',
241                         'deleted',
242                 ];
243
244                 $subCmd = $this->getArgument(1);
245
246                 $table = new Console_Table();
247                 $table->setHeaders(['ID', 'UID', 'Network', 'Name', 'Nick', 'URL', 'E-Mail', 'Created', 'Updated', 'Blocked', 'Deleted']);
248
249                 $addRow = function($row) use (&$table) {
250                         $table->addRow([
251                                 $row['id'],
252                                 $row['uid'],
253                                 $row['network'],
254                                 $row['name'],
255                                 $row['nick'],
256                                 $row['url'],
257                                 $row['addr'],
258                                 Temporal::getRelativeDate($row['created']),
259                                 Temporal::getRelativeDate($row['updated']),
260                                 $row['blocked'],
261                                 $row['deleted'],
262                         ]);
263                 };
264                 switch ($subCmd) {
265                         case 'id':
266                                 $cid = $this->getArgument(2);
267                                 $contact = ContactModel::getById($cid, $fields);
268                                 if (!empty($contact)) {
269                                         $addRow($contact);
270                                 }
271                                 break;
272                         case 'url':
273                                 $user = $this->getUserByNick(2);
274                                 $url = $this->getArgument(3);
275                                 $contact = ContactModel::getByURLForUser($url, $user['uid'], false, $fields);
276                                 if (!empty($contact)) {
277                                         $addRow($contact);
278                                 }
279                                 break;
280                         default:
281                                 $this->out($this->getHelp());
282                                 return false;
283                 }
284
285                 $this->out($table->getTable());
286
287                 return true;
288         }
289 }