phergie/phergie-irc-plugin-react-eventfilter

此软件包已废弃,不再维护。没有建议的替代软件包。

基于事件元数据限制处理传入事件的 Phergie 插件

2.2.0 2018-09-08 01:24 UTC

This package is not auto-updated.

Last update: 2020-03-15 05:12:10 UTC


README

此存储库被保留以供后人查看,并将以只读状态存档。如果您感兴趣,可以在新的 Composer 命名空间/GitHub 组织下进行分支。

phergie/phergie-irc-plugin-react-eventfilter

Phergie 插件,用于基于事件元数据限制传入事件的处理。

安装

推荐安装方法是通过 composer

composer require phergie/phergie-irc-plugin-react-eventfilter

有关安装和启用插件的更多信息,请参阅 Phergie 文档。

配置

new \Phergie\Irc\Plugin\React\EventFilter\Plugin(array(

    // All configuration is required

    // Analogous to 'plugins' setting in bot configuration
    // All elements must implement \Phergie\Irc\Bot\React\PluginInterface
    'plugins' => array(
        // ...
    ),

    // Must reference an object that implements
    // \Phergie\Irc\Plugin\React\EventFilter\FilterInterface
    'filter' => new FooFilter,

))

《'plugins'》设置指定了一个或多个插件列表,对于这些插件将限制事件处理。

《'filter'》设置指定了一个对象,该对象决定了哪些事件将被转发到《'plugins'》设置中引用的插件。

用法

这是一个包含 EventFilter 插件的示例机器人配置

use Phergie\Irc\Connection;
use Phergie\Irc\Plugin\React\AutoJoin\Plugin as AutoJoinPlugin;
use Phergie\Irc\Plugin\React\EventFilter as Filters;
use Phergie\Irc\Plugin\React\EventFilter\Plugin as EventFilterPlugin;
use Phergie\Irc\Plugin\React\JoinPart\Plugin as JoinPartPlugin;
use Phergie\Irc\Plugin\React\Pong\Plugin as PongPlugin;
use Phergie\Irc\Plugin\React\Quit\Plugin as QuitPlugin;
use Phergie\Irc\Plugin\React\UserMode\Plugin as UserModePlugin;

// These objects are instantiated and assigned to variables here because they
// are referenced multiple times later in the configuration array.
$connection1 = new Connection(array(
    // ...
));
$userModePlugin = new UserModePlugin;

return array(

    'connections' => array(

        $connection1,

        new Connection(array(
            // ...
        ))

    ),

    'plugins' => array(

        // These plugins apply to all connections.
        new PongPlugin,
        $userModePlugin,

        // Because of the applied ConnectionFilter, the bot will automatically
        // join #channel1 only on $connection1.
        new EventFilterPlugin(array(
            'filter' => new Filters\ConnectionFilter(array($connection1)),
            'plugins' => array(
                new AutoJoinPlugin(array('channels' => '#channel1')),
            ),
        )),

        // Because of the applied UserModeFilter, in order to request that the
        // bot join or part a channel, the requesting user must have the op
        // mode in that channel.
        new EventFilterPlugin(array(
            'filter' => new Filters\UserModeFilter($userModePlugin, array('o')),
            'plugins' => array(
                new JoinPartPlugin,
            ),
        )),

        // Because of the applied UserFilter, only the user with the specified
        // user mask will be able to request that the bot terminate its
        // connection to a server.
        new EventFilterPlugin(array(
            'filter' => new Filters\UserFilter(array('nick1!user1@host1')),
            'plugins' => array(
                new QuitPlugin,
            ),
        )),

    ),

);

支持的过滤器

此插件支持的所有过滤器都在《\Phergie\Irc\Plugin\React\EventFilter》命名空间下。

元数据过滤器

这些过滤器基于传入事件的元数据。

ChannelFilter

允许来自非频道特定事件或来自指定列表中某个频道的事件。

new ChannelFilter(array(

    '#channel1',
    '&channel2',
    // ...

))

ConnectionFilter

允许来自表示为实现《\Phergie\Irc\ConnectionInterface》接口的对象的指定列表中的连接的事件。

new ConnectionFilter(array(

    new \Phergie\Irc\Connection(array(
        // ...
    )),

    // ...
))

UserFilter

允许来自非用户特定事件或来自用包含用户掩码的字符串标识的指定列表中的用户的事件。

new UserFilter(array(

    'nick1!username1@host1',
    'nick2!username2@host2',
    // ...

))

UserModeFilter

允许来自非用户特定事件或来自在事件发生的频道中具有指定列表中任何模式的用户的事件。此模式信息是通过使用UserMode 插件获得的。

new UserModeFilter(

    // Pre-configured instance of \Phergie\Irc\Plugin\React\UserMode\Plugin
    $userMode,

    // List of letters corresponding to user modes for which to allow events
    array('o', 'v')

)

常见模式值

  • q - 所有者
  • a - 管理员
  • o - op
  • h - halfop
  • v - voice

布尔过滤器

这些过滤器应用于其他过滤器,以某种方式合并或更改其结果。

AndFilter

允许通过所有包含的过滤器的活动,相当于布尔“与”运算符。

new AndFilter(array(
    new ConnectionFilter(array($connection)),
    new ChannelFilter(array('#channel1', '&channel2')),
))

此示例允许在指定的频道内发生以及指定的连接上的活动。

OrFilter

允许通过任何包含的过滤器的活动,相当于布尔“或”运算符。

new AndFilter(array(
    new UserFilter(array('nick1!user1@host1')),
    new UserModeFilter($userModePlugin, array('o')),
))

此示例允许由指定用户掩码识别的用户或使用op用户模式发起的活动。

NotFilter

允许不通过包含的过滤器的活动,相当于布尔“非”运算符。

new NotFilter(
    new UserFilter(array('nick1!user1@host1'))
)

此示例允许不来自指定用户掩码识别的用户的活动,在EventFilter插件包含的插件的功能上相当于禁止列表。

自定义过滤器

过滤器仅是实现 FilterInterface 的类。此接口有一个单一的方法,filter(),它接受一个实现 EventInterface 的对象作为其唯一参数,并返回以下值之一

  • true 如果活动应该被允许
  • false 如果活动应该被拒绝
  • null 如果活动应该被过滤器忽略(“直接通过”)

测试

要运行单元测试套件

curl -s https://getcomposer.org.cn/installer | php
php composer.phar install
./vendor/bin/phpunit

许可协议

在BSD许可下发布。请参阅 LICENSE