thirdplace / irc
dev-main
2023-07-06 07:07 UTC
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-posix: *
- ext-sockets: *
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is not auto-updated.
Last update: 2024-09-27 09:36:39 UTC
README
一个非常基本的 IRC 库和客户端(机器人)。
仅用于业余目的,尚未准备好用于生产。
不清楚网络代码是否有效。
教程
安装
composer require thirdplace/irc:dev-main
示例
// non-tls
$config = [
'server' => 'irc.libera.chat',
'protocol' => 'tcp',
'port' => 6667,
'nick' => 'botson45',
'channels' => ['#test'],
];
$client = new \Thirdplace\Irc\Client($config);
$client->start();
// tls
$libera = [
'server' => 'irc.libera.chat',
'protocol' => 'tls',
'port' => 6697,
'nick' => 'botson45',
'channels' => ['#test'],
];
$client = new \Thirdplace\Irc\Client($config);
$client->start();
// tls with SASL authentication
$libera2 = [
'server' => 'irc.libera.chat',
'protocol' => 'tls',
'port' => 6697,
'nick' => 'botson45',
'user' => 'botson45',
'auth' => 'sasl',
'password' => 'REPLACE ME',
'channels' => ['#test'],
];
$client = new \Thirdplace\Irc\Client($config);
$client->start();
// tls with twitch authentication (oauth)
$twitch = [
'server' => 'irc.chat.twitch.tv',
'protocol' => 'tls',
'port' => 6697,
'nick' => 'botson45',
'auth' => 'twitch',
'token' => 'REPLACE ME',
'channels' => ['#hasanbi'],
];
$client = new \Thirdplace\Irc\Client($config);
$client->start();
运行单元测试
./vendor/bin/phpunit --bootstrap=vendor/autoload.php ./test
如何
如何创建命令
// Respond to !hello command with 'world'
$client->addHandler(function(Client $client, Message $message) {
if (
$message->isChannelMessage()
&& preg_match('/^!hello/', $message->getMessageParameter())
) {
$client->write($message->reply('world'));
}
});
如何每10秒运行处理器
// Write a tick message to #test3 each 10 seconds
$client->addTickHandler(10, function(Client $client) {
$client->write(Message::privmsg('#test3', 'tick!'));
});
如何向服务器发送原始文本
使用 pipe_file
配置创建客户端
$config = [
'server' => 'irc.libera.chat',
'protocol' => 'tcp',
'port' => 6667,
'nick' => 'botson45',
'channels' => ['#test3'],
'pipe_file' => '/tmp/irc',
];
$client = new \Thirdplace\Irc\Client($config);
$client->start();
从shell向它写入
echo "PRIVMSG #test3 :hello world" > /tmp/irc
说明
代码库很小。从 src/Client.php
开始,以获得感觉。
大多数 IRC 逻辑在 Message.php
中。客户端在 Client.php
中。 src/handlers
文件夹包含根据传入的 IRC 消息调用的代码。
参考
客户端配置
$defaults = [
'server' => 'irc.libera.chat',
'protocol' => 'tls', // ['tcp', 'tls']
'port' => 6697,
'auth' => null, // [null, 'nickserv', 'sasl', 'twitch']
'nick' => null,
'user' => null,
'password' => null, // for NickServer or SASL
'token' => null, // oauth auth token (twitch.tv et al)
'channels' => [], // List of channels to join
'pipe_file' => null, // Write raw text to server
];
处理器接口
interface Handler
{
public function __invoke(Client $client, Message $message): void;
}
基本异常
final class IrcException extends \Exception
{
}