forked from jerodev/php-irc-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.php
104 lines (84 loc) · 2.76 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
require_once './vendor/autoload.php';
use Jerodev\PhpIrcClient\IrcChannel;
use Jerodev\PhpIrcClient\IrcClient;
use Jerodev\PhpIrcClient\Options\ClientOptions;
// Set the IRC network to connect to and the port if you're not connecting to
// Freenode.
$server = 'chat.freenode.net';
$port = '6667';
// Give your bot a memorable name.
$nickname = 'PHP_IRC_Bot';
// If you add any channels (like ['#php-is-neat']), the bot will automatically
// join them when you run `php test.php`.
$autojoinChannels = [];
$options = new ClientOptions(nickname: $nickname, channels: $autojoinChannels);
$client = new IrcClient(\sprintf('%s:%s', $server, $port), $options);
$signal_handler = function (int $signo, mixed $siginfo) use ($client, $server): void {
if (\SIGHUP === $signo) {
echo 'Caught signal to re-read config', \PHP_EOL;
return;
}
if (\SIGTSTP === $signo) {
echo 'Caught sleep signal', \PHP_EOL;
// Restore original handler.
pcntl_signal(\SIGTSTP, \SIG_DFL);
posix_kill(posix_getpid(), \SIGTSTP);
return;
}
if (\SIGCONT === $signo) {
echo 'Caught continue signal', \PHP_EOL;
return;
}
if (\SIGINT !== $signo && \SIGTERM !== $signo) {
echo 'Caught unknown signal (', $signo, ')', \PHP_EOL;
return;
}
// Handle shutdown tasks.
echo 'Disconnecting from ', $server, \PHP_EOL;
foreach ($client->getChannels() as $name => $channel) {
$client->part($name);
}
$client->disconnect();
exit();
};
pcntl_signal(\SIGHUP, $signal_handler); // kill -HUP <pid>
pcntl_signal(\SIGINT, $signal_handler); // CTRL-C
pcntl_signal(\SIGTERM, $signal_handler); // kill <pid>
pcntl_signal(\SIGTSTP, $signal_handler); // CTRL-Z
pcntl_signal(\SIGCONT, $signal_handler); // fg after a CTRL-Z
$client->on('registered', function () use ($server, $port): void {
echo \sprintf('Connected to %s, port %s', $server, $port), \PHP_EOL;
});
$client->on(
'message',
function (
string $from,
IrcChannel $channel,
string $message
) use ($client, $nickname): void {
echo \sprintf(
' . %10s - %10s: %s',
$channel->getName(),
$from,
$message
), \PHP_EOL;
if ($nickname === $from) {
// Ignore messages from the bot.
return;
}
if (false === str_contains($message, $nickname)) {
// Ignore messages that aren't to the bot.
return;
}
echo \sprintf(
' . %10s - %10s: %s',
$channel->getName(),
$nickname,
'I am not a bot!',
), \PHP_EOL;
$client->say($channel->getName(), 'I am not a bot!');
}
);
$client->connect();