Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2019-01-23 19:24:44 +0300
committerJoas Schilling <coding@schilljs.com>2019-02-20 14:17:35 +0300
commitdda4b0d4eb4dbb65717d0227a4c737bf6d245d9f (patch)
treebc0b1d3c980ce0a1d76389ac9d91569af72fd800 /lib/Chat/Command/Executor.php
parent8ad3535ba93a943152b240e971375b6a70d0f679 (diff)
Cleanup execution and trigger an event for apps
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Chat/Command/Executor.php')
-rw-r--r--lib/Chat/Command/Executor.php101
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/Chat/Command/Executor.php b/lib/Chat/Command/Executor.php
new file mode 100644
index 000000000..aa6a2f74e
--- /dev/null
+++ b/lib/Chat/Command/Executor.php
@@ -0,0 +1,101 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Spreed\Chat\Command;
+
+
+use OCA\Spreed\Model\Command;
+use OCA\Spreed\Room;
+use OCP\Comments\IComment;
+use Symfony\Component\EventDispatcher\EventDispatcherInterface;
+use Symfony\Component\EventDispatcher\GenericEvent;
+
+class Executor {
+
+ public const PLACEHOLDER_ROOM = '{ROOM}';
+ public const PLACEHOLDER_USER = '{USER}';
+ public const PLACEHOLDER_ARGUMENTS = '{ARGUMENTS}';
+
+
+ /** @var EventDispatcherInterface */
+ protected $dispatcher;
+
+ public function __construct(EventDispatcherInterface $dispatcher) {
+ $this->dispatcher = $dispatcher;
+ }
+
+ public function exec(Room $room, IComment $message, Command $command, string $arguments): void {
+ if ($command->getApp() !== '') {
+ $output = $this->execApp($room, $message, $command, $arguments);
+ } else if ($command->getCommand() === 'help') {
+ $output = $this->execHelp($room, $message, $command, $arguments);
+ } else {
+ $output = $this->execShell($room, $message, $command, $arguments);
+ }
+
+ $user = $message->getActorType() === 'users' ? $message->getActorId() : '';
+ $message->setMessage(json_encode([
+ 'user' => $user,
+ 'visibility' => $command->getResponse(),
+ 'output' => $output,
+ ]));
+ $message->setActor('bots', $command->getName());
+ $message->setVerb('command');
+ }
+
+ protected function execApp(Room $room, IComment $message, Command $command, string $arguments): string {
+ $output = '';
+
+ $this->dispatcher->dispatch(self::class . '::execApp', new GenericEvent($command, [
+ 'room' => $room,
+ 'message' => $message,
+ 'arguments' => $arguments,
+ 'output' => $output,
+ ]));
+
+ return $output;
+ }
+
+ protected function execHelp(Room $room, IComment $message, Command $command, string $arguments): string {
+ // FIXME Implement a useful help
+ return $arguments;
+ }
+
+ protected function execShell(Room $room, IComment $message, Command $command, string $arguments): string {
+ $user = $message->getActorType() === 'users' ? $message->getActorId() : '';
+
+ $cmd = str_replace([
+ self::PLACEHOLDER_ROOM,
+ self::PLACEHOLDER_USER,
+ self::PLACEHOLDER_ARGUMENTS,
+ ], [
+ $room->getToken(),
+ $user,
+ $arguments,
+ ], $command->getScript());
+
+ $output = [];
+ exec($cmd, $output);
+
+ return implode("\n", $output);
+ }
+}