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