mirror of
https://github.com/danog/MadelineProto.git
synced 2025-01-23 00:51:12 +01:00
Add two filter
This commit is contained in:
parent
2f0bec11da
commit
6ca86777a8
56
src/EventHandler/Filter/FilterCommandCaseInsensetive.php
Normal file
56
src/EventHandler/Filter/FilterCommandCaseInsensetive.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of MadelineProto.
|
||||
* MadelineProto 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.
|
||||
* MadelineProto 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 General Public License along with MadelineProto.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
|
||||
* @copyright 2016-2023 Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto\EventHandler\Filter;
|
||||
|
||||
use Attribute;
|
||||
use danog\MadelineProto\EventHandler\Message;
|
||||
use danog\MadelineProto\EventHandler\Story\Story;
|
||||
use danog\MadelineProto\EventHandler\Update;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
/**
|
||||
* Allow only messages containing the specified case-insensitive command.
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
final class FilterCommandCaseInsensetive extends Filter
|
||||
{
|
||||
/**
|
||||
* @var array<CommandType>
|
||||
*/
|
||||
public readonly array $commandTypes;
|
||||
/**
|
||||
* @param string $command Command
|
||||
* @param list<CommandType> $types Command types, if empty all command types are allowed.
|
||||
*/
|
||||
public function __construct(private readonly string $command, array $types = [CommandType::BANG, CommandType::DOT, CommandType::SLASH])
|
||||
{
|
||||
Assert::true(preg_match("/^\w+$/", $command) === 1, "An invalid command was specified!");
|
||||
Assert::notEmpty($types, 'No command types were specified!');
|
||||
$c = [];
|
||||
foreach ($types as $type) {
|
||||
if (isset($c[$type->value])) {
|
||||
throw new AssertionError($type->value." was already specified!");
|
||||
}
|
||||
$c[$type->value] = true;
|
||||
}
|
||||
$this->commandTypes = $types;
|
||||
}
|
||||
public function apply(Update $update): bool
|
||||
{
|
||||
return $update instanceof Message && strtolower($update->command) === $this->command && \in_array($update->commandType, $this->commandTypes, true);
|
||||
}
|
||||
}
|
47
src/EventHandler/Filter/FilterForwardedFrom.php
Normal file
47
src/EventHandler/Filter/FilterForwardedFrom.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* This file is part of MadelineProto.
|
||||
* MadelineProto 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.
|
||||
* MadelineProto 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 General Public License along with MadelineProto.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* @author Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
|
||||
* @copyright 2016-2023 Amir Hossein Jafari <amirhosseinjafari8228@gmail.com>
|
||||
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
|
||||
* @link https://docs.madelineproto.xyz MadelineProto documentation
|
||||
*/
|
||||
|
||||
namespace danog\MadelineProto\EventHandler\Filter;
|
||||
|
||||
use Attribute;
|
||||
use danog\MadelineProto\EventHandler;
|
||||
use danog\MadelineProto\EventHandler\Update;
|
||||
use danog\MadelineProto\EventHandler\Message;
|
||||
|
||||
/**
|
||||
* Allow only forwarded messages from a certain sender.
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
final class FilterForwardedFrom extends Filter
|
||||
{
|
||||
private readonly int $peerResolved;
|
||||
|
||||
public function __construct(private readonly string|int $peer)
|
||||
{
|
||||
}
|
||||
|
||||
public function initialize(EventHandler $API): Filter
|
||||
{
|
||||
/** @psalm-suppress InaccessibleProperty */
|
||||
$this->peerResolved = $API->getId($this->peer);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function apply(Update $update): bool
|
||||
{
|
||||
return $update instanceof Message && ($update?->fwdInfo?->fromId === $this->peerResolved || $update?->fwdInfo?->forwardedChannelMsgId === $this->peerResolved);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user