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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
diff --git a/bot.php b/bot.php
index 78ce627..0268a4e 100644
--- a/bot.php
+++ b/bot.php
@@ -9,7 +9,7 @@ $nickname = $_ENV["NICKNAME"];
$hostname = $_ENV["HOSTNAME"];
$password = $_ENV["PASSWORD"];
$server = $_ENV["SERVER"];
-$port = $_ENV["PORT"];
+$port = $_ENV["IRCPORT"];
// In order to update the bot's code while it's connected to IRC, we need a separate "functions" script.
// This script will receive a stream of the IRC activity, and then it'll "do stuff" and send the response
@@ -26,27 +26,27 @@ set_time_limit(0);
// Now we'll set up our connection
$context = stream_context_create([
- 'ssl' => [
- 'verify_peer' => true,
- 'verify_peer_name' => true,
- 'allow_self_signed' => false,
- 'SNI_enabled' => true,
- 'peer_name' => $server,
- ],
+ 'ssl' => [
+ 'verify_peer' => true,
+ 'verify_peer_name' => true,
+ 'allow_self_signed' => false,
+ 'SNI_enabled' => true,
+ 'peer_name' => $server,
+ ],
]);
// And actually connect
$socket = stream_socket_client(
- "tls://{$server}:{$port}",
- $errno,
- $errstr,
- 30,
- STREAM_CLIENT_CONNECT,
- $context
+ "tls://{$server}:{$port}",
+ $errno,
+ $errstr,
+ 30,
+ STREAM_CLIENT_CONNECT,
+ $context
);
-if(!$socket) {
- die("Connection failed: $errstr ($errno)\n");
+if (! $socket) {
+ die("Connection failed: $errstr ($errno)\n");
}
// If our code is still running at this point, we're connected to the server!
@@ -58,47 +58,45 @@ fputs($socket, "NICK " . $nickname . "\n");
fputs($socket, "USER " . $nickname . " 0 * :$nickname@$hostname\n");
// Send avatar metadata? does this even work? lol
-if($avatar_url) {
- fputs($socket, "METADATA * SET avatar $avatar_url\n");
+if ($avatar_url) {
+ fputs($socket, "METADATA * SET avatar $avatar_url\n");
}
-
// Loopy loop forever and ever and ever
-while(1) {
- while ($data = fgets($socket, 512)) {
- echo $data;
- flush();
-
- // Take the line of data that we've received and snap it into separate pieces wherever there's a space
- $pieces = explode(' ', $data);
-
- // At this point, $pieces[0] will contain the sender, and $pieces[1] will contain the command that was sent
-
- // Send a PONG back to the server whenever we receive a PING
- if($pieces[0] == "PING") {
- fputs($socket, "PONG " . $pieces[1] . "\n");
- }
-
- // Otherwise, we're going to send everything to our separate functions script for handling
- else {
-
- // Execute the functions script, passing the $data to it as an argument
- $response = shell_exec($functions_script_command.' '.escapeshellarg($data));
-
- // Since we're receiving JSON in response, we'll decode that
- $response = json_decode($response);
-
- // Process responses
- if(isset($response->response)) {
- if(is_countable($response->response)) {
- foreach($response->response as $response_item) {
- fputs($socket, $response_item."\r\n");
- }
+while (1) {
+ while ($data = fgets($socket, 512)) {
+ echo $data;
+ flush();
+
+ // Take the line of data that we've received and snap it into separate pieces wherever there's a space
+ $pieces = explode(' ', $data);
+
+ // At this point, $pieces[0] will contain the sender, and $pieces[1] will contain the command that was sent
+
+ // Send a PONG back to the server whenever we receive a PING
+ if ($pieces[0] == "PING") {
+ fputs($socket, "PONG " . $pieces[1] . "\n");
}
+
+ // Otherwise, we're going to send everything to our separate functions script for handling
else {
- fputs($socket, $response->response."\r\n");
+
+ // Execute the functions script, passing the $data to it as an argument
+ $response = shell_exec($functions_script_command . ' ' . escapeshellarg($data));
+
+ // Since we're receiving JSON in response, we'll decode that
+ $response = json_decode($response);
+
+ // Process responses
+ if (isset($response->response)) {
+ if (is_countable($response->response)) {
+ foreach ($response->response as $response_item) {
+ fputs($socket, $response_item . "\r\n");
+ }
+ } else {
+ fputs($socket, $response->response . "\r\n");
+ }
+ }
}
- }
}
- }
}
|