Commit 8a9b0d6f authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #580 from noplanman/private_only_commands

New command parameter to enforce usage in private chats only.
parents 69ed1e89 c186a45b
...@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c ...@@ -13,6 +13,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
- `Telegram::enableAdmin()` now handles duplicate additions properly. - `Telegram::enableAdmin()` now handles duplicate additions properly.
- `Request::getMe()` failure doesn't break cron execution any more. - `Request::getMe()` failure doesn't break cron execution any more.
### Security ### Security
- New command parameter `$private_only` to enforce usage in private chats only.
## [0.46.0] - 2017-07-15 ## [0.46.0] - 2017-07-15
### Added ### Added
......
...@@ -97,6 +97,13 @@ abstract class Command ...@@ -97,6 +97,13 @@ abstract class Command
*/ */
protected $need_mysql = false; protected $need_mysql = false;
/*
* Make sure this command only executes on a private chat.
*
* @var bool
*/
protected $private_only = false;
/** /**
* Command config * Command config
* *
...@@ -145,6 +152,24 @@ abstract class Command ...@@ -145,6 +152,24 @@ abstract class Command
return $this->executeNoDb(); return $this->executeNoDb();
} }
if ($this->isPrivateOnly() && $this->removeNonPrivateMessage()) {
$message = $this->getMessage();
if ($user = $message->getFrom()) {
return Request::sendMessage([
'chat_id' => $user->getId(),
'parse_mode' => 'Markdown',
'text' => sprintf(
"/%s command is only available in a private chat.\n(`%s`)",
$this->getName(),
$message->getText()
),
]);
}
return Request::emptyResponse();
}
return $this->execute(); return $this->execute();
} }
...@@ -296,6 +321,16 @@ abstract class Command ...@@ -296,6 +321,16 @@ abstract class Command
return $this->enabled; return $this->enabled;
} }
/**
* If this command is intended for private chats only.
*
* @return bool
*/
public function isPrivateOnly()
{
return $this->private_only;
}
/** /**
* If this is a SystemCommand * If this is a SystemCommand
* *
...@@ -325,4 +360,27 @@ abstract class Command ...@@ -325,4 +360,27 @@ abstract class Command
{ {
return ($this instanceof UserCommand); return ($this instanceof UserCommand);
} }
/**
* Delete the current message if it has been called in a non-private chat.
*
* @return bool
*/
protected function removeNonPrivateMessage()
{
$message = $this->getMessage();
$chat = $message->getChat();
if (!$chat->isPrivateChat()) {
// Delete the falsely called command message.
Request::deleteMessage([
'chat_id' => $chat->getId(),
'message_id' => $message->getMessageId(),
]);
return true;
}
return false;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment