This repository has been archived by the owner on Mar 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
protoirc.php
480 lines (395 loc) · 13 KB
/
protoirc.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?php
/**
* ProtoIRC IRC bot framework.
*
* @author Zane Ashby <zane.a@demonastery.org>
*/
/**
* ProtoIRC entry-point.
*
* @param string $conn_string A string describing the connection to attempt.
* @param callable $conn_func An optional callback function that is called after the connection is ready.
*
* @return ProtoIRC A new instance of the ProtoIRC class, to allow easy chaining.
*/
function ProtoIRC($conn_string, $conn_func = null) {
return new ProtoIRC($conn_string, $conn_func);
}
/**
* ProtoIRC class.
*/
class ProtoIRC {
/** @var string Hostname of the IRC server. */
var $host;
/** @var int Port of the IRC server. */
var $port;
/** @var string Nickname. */
var $nick;
/** @var string Last destination that a message was sent to. Either a nickname or a channel. */
var $last;
/** @var array List of channels to connect to. */
var $channels;
/** @var resource IRC socket. */
var $socket;
/** @var array Array of child PID's and their return values (if any). */
var $child;
/** @var array Array of event handlers. */
var $handlers = array();
/** @var array Array of event handlers that should return early (on first match). */
var $bhandlers = array('stdin');
/** @var object Object containing color values. */
var $ansi;
/**
* Constructor.
*
* @param string $conn_string A string describing the connection to attempt.
* @param callable $conn_func A callback function that is called after the connection is ready.
*/
function __construct($conn_string, $conn_func = null) {
$defaults = array(
'user' => 'ProtoBot',
'host' => '127.0.0.1',
'port' => '6667',
'path' => '',
'pass' => false,
'fragment' => '',
);
$url = (object)array_merge($defaults, parse_url($conn_string));
$this->nick = $url->user;
$this->host = $url->host;
$this->port = $url->port;
$channels = trim($url->path, '/');
$auth = $url->pass;
$key = $url->fragment;
foreach ($this->genIRCColors() as $color => $value)
$this->$color = $value;
$this->ansi = $this->genANSIColors();
// Set up some built in handlers for various messages
if (!empty($channels)) {
$this->in(
'/^.* (?:422|376)(?#builtincb)/',
function ($irc) use ($channels, $auth, $key) {
if ($auth)
$irc->send('NickServ', "IDENTIFY {$auth}");
foreach (explode(',', $channels) as $channel)
$irc->join("#{$channel} {$key}");
}
);
}
$this->in('/^.* (?:422|376)(?#usercb)/', $conn_func);
$this->in(
'/^PING (.*)(?#builtin)/',
function ($irc, $args) {
$irc->pong($args);
}
);
$this->in(
'/^:(.*?)!~?.* PRIVMSG (.*?) :(.*)(?#builtin)/',
function ($irc, $nick, $dest, $msg) {
$irc->last = ($dest == $irc->nick) ? $nick : $dest;
$irc->msg($msg, $nick, $dest);
}
);
$this->in(
'/^:(.*?)!~?.*? JOIN :(.*)(?#builtin)/',
function ($irc, $nick, $channel) {
if ($nick == $irc->nick)
$irc->channels[] = $channel;
}
);
$this->in(
'/^:(.*?)!~?.*? PART (.*)(?#builtin)/',
function ($irc, $nick, $channel) {
if ($nick == $irc->nick && ($key = array_search($channel, $irc->channels)) !== false)
unset($irc->channels[$key]);
}
);
$this->out(
array(
'/^(?:JOIN) (#.*)(?#builtin)/',
'/^(?:PRIVMSG|NOTICE) (.*) :(?#builtin)/',
'/^NICK (.*)(?#builtin)/',
),
function ($irc, $dest) {
$irc->last = $dest;
}
);
}
/**
* Generate an object containing IRC colors.
*
* @return object An object with color names as keys.
*/
function genIRCColors() {
$colors = array_flip(explode(' ', '_white black blue green _red red purple yellow _yellow _green cyan _cyan _blue _purple _black white'));
foreach ($colors as &$v)
$v = sprintf("\3%02s", $v);
return (object)($colors + array('default' => "\3"));
}
/**
* Generate an object containing ANSI colors (for use on terminals).
*
* @return object An object with color names as keys.
*/
function genANSIColors() {
$colors = array_flip(explode(' ', 'black red green yellow blue purple cyan white'));
foreach ($colors as &$v)
$v = "\033[0;" . (30 + $v) . "m";
$bcolors = array_flip(explode(' ', '_black _red _green _yellow _blue _purple _cyan _white'));
foreach ($bcolors as &$v)
$v = "\033[1;" . (30 + $v) . "m";
return (object)($colors + $bcolors + array('default' => "\033[0m"));
}
/**
* Write a line to STDOUT.
*
* @param string Some text to output to STDOUT.
* @param string The name of a color to output this text in.
*/
function stdout($line, $color = 'default') {
echo "{$this->ansi->$color}{$line}{$this->ansi->default}";
}
/**
* Send "stuff" to the IRC socket, in a few different ways.
*
* This method does different things depending on the number of arguments that it receives.
*
* @param string $data|$dest Either a valid string to send to an IRC server, or a destination.
* @param string $msg Message to send to destination. Optional if sending data directly.
* @param string $color Color to use for message. Optional.
*
* @return \ProtoIRC The instance.
*/
function send() {
if (!$this->socket)
return;
switch (func_num_args()) {
case 1:
$data = func_get_arg(0);
break;
case 2:
case 3:
$dest = func_get_arg(0);
$msg = func_get_arg(1);
if (empty($dest) || empty($msg))
return;
$color = (func_num_args() == 3) ? func_get_arg(2) : false;
// Send to multiple destinations..
if (is_array($dest)) {
foreach ($dest as $sdest)
$this->send($sdest, $msg, $color);
return;
}
// Print stuff containing newlines as expected..
if (!is_array($msg) && strpos($msg, "\n") !== false)
$msg = explode("\n", $msg);
if (is_array($msg)) {
foreach ($msg as $line)
$this->send($dest, $line, $color);
return;
}
if ($color) {
$data = "PRIVMSG {$dest} :{$this->$color}{$msg}{$this->default}";
} else {
$data = "PRIVMSG {$dest} :{$msg}";
}
break;
default:
return;
}
if ($this->out($data) === false)
return;
fwrite($this->socket, "{$data}\r\n");
usleep(200000);
return $this;
}
/**
* Do something asynchronously.
*
* This neat little method sets up a UNIX socket for IPC, and then forks the current process.
* The child is then able to talk to the parent, by way of a return value.
*
* @param callable A callback that is running in the forked instance. Anything returned will be captured by the main process.
*
* @return int The PID of the child process, for keeping track of your children.
*/
function async($function) {
socket_create_pair(AF_UNIX, SOCK_STREAM, 0, $sockets); // IPC
list($parent, $child) = $sockets;
if (($pid = pcntl_fork()) == 0) { // In child process now
socket_close($child);
socket_write($parent, serialize(call_user_func($function, $this)));
socket_close($parent);
exit;
}
socket_close($parent);
$this->child[$pid] = $child;
return $pid;
}
/**
* Read the value returned by a child process.
*
* @param int $pid The PID of the child process to read.
*
* @return mixed The stored value is returned if the child has exited, otherwise false.
*/
function read($pid) {
if (isset($this->child[$pid])) {
if (!is_resource($this->child[$pid])) {
$output = $this->child[$pid];
unset($this->child[$pid]);
return $output;
}
}
return false;
}
/**
* Wait for a child process to exit and return its value.
*
* Similar to the read function but will wait for the child to exit if necessary.
*
* @see \ProtoIRC::read()
*
* @param int The PID of the child process to read.
*
* @return mixed The returned value of the child process.
*/
function wait($pid) {
if (isset($this->child[$pid])) {
pcntl_waitpid($pid, $status);
if (is_resource($this->child[$pid])) {
$output = unserialize(socket_read($this->child[$pid], 4096));
socket_close($this->child[$pid]);
} else {
$output = $this->child[$pid];
}
unset($this->child[$pid]);
return $output;
}
}
/**
* Bind a callback to an event category, matched by regular expression.
*
* This is essentially a small event system used throughout the framework for dispatching methods.
* This can be thought of as "on". In fact it may end up being renamed to that effect.
*
* @param string $type The event category of event this is related to (such as "stdin", "out", and "in").
* @param string $regex A regular expression that is matched against the event.
* @param callable $function The callback that is called upon a successfull match.
*
* @return \ProtoIRC The instance.
*/
function bind($type, $regex, $function) {
if (is_callable($function)) {
foreach ((array)$regex as $_regex) {
$this->handlers[$type][$_regex] = $function;
// Sort by regex length, rough approximation of regex
// "wideness", since we want catch-all's to come last.
uksort($this->handlers[$type], function ($a, $b) {
return (strlen($b) - strlen($a));
});
}
}
return $this;
}
/**
* Magic method invoked when an unknown method is called.
*
* This is used as a shortcut for the event system (bind/call).
*
* @param string $type The event category, as seen in ProtoIRC::bind().
* @param array $args The provided arguments.
*
* @return mixed Returns what the call/bind methods do.
*/
function __call($type, $args) {
array_unshift($args, $type);
if (count($args) == 2 || !is_callable($args[2])) {
return call_user_func_array(array($this, 'call'), $args);
} else {
return call_user_func_array(array($this, 'bind'), $args);
}
}
/**
* Call an event.
*
* This can be thought of as "emit". In fact it may end up being renamed to that effect.
*
* @param string $type The event category.
* @param string $data The data that is matched against regular expressions.
*
* @return mixed Returns different values based on the matches.
* Of note, when there are no handlers registered for a type, this method will send to IRC directly.
* For example, a call of `$protoirc->notice($data)` is transformed into an IRC NOTICE.
*/
function call($type, $data) {
if (!isset($this->handlers[$type]))
return $this->send(strtoupper($type) . " {$data}");
foreach ($this->handlers[$type] as $regex => $func) {
if (preg_match($regex, $data, $matches)) {
array_shift($matches); // Remove full match from array
for ($i = func_num_args() - 1; $i > 1; $i--)
array_unshift($matches, func_get_arg($i));
array_unshift($matches, $this); // Add $irc parameter
if (($r = call_user_func_array($func, $matches)))
return $r;
// By default 'stdin' handlers should return as soon as
// a regex has matched. More intuitive this way.
if (in_array($type, $this->bhandlers))
return $this;
}
}
return $this;
}
/**
* Start the bot.
*
* This function handles connecting to IRC and acting as a mainloop.
*/
function go() {
while (true) {
if (!($this->socket = @fsockopen($this->host, $this->port)))
continue; // Keep reconnecting until it succeeds
$lastmsg = time();
$this->nick($this->nick);
$this->user("{$this->nick} * * :{$this->nick}");
do {
$r = array($this->socket, STDIN);
$w = null;
$x = null;
if (stream_select($r, $w, $x, 1)) {
foreach ($r as $stream) {
$buffer = trim(fgets($stream, 4096));
if (stream_is_local($stream)) {
$this->stdin($buffer);
} else {
$lastmsg = time();
$this->in($buffer);
}
}
}
// Garbage Collect, Clean up any waiting child processes
if (($pid = pcntl_waitpid(-1, $status, WNOHANG)) > 0) {
$output = unserialize(socket_read($this->child[$pid], 4096));
socket_close($this->child[$pid]);
if (!empty($output)) {
$this->child[$pid] = $output;
} else {
unset($this->child[$pid]);
}
}
if (isset($this->handlers['timer'])) {
$now = time();
foreach ($this->handlers['timer'] as $time => $func)
if (($now % $time) == 0)
$func($this);
}
if (time() > $lastmsg + (60 * 5)) { // No activity for 5 minutes?
fclose($this->socket);
$this->socket = null;
}
} while ($this->socket && !feof($this->socket));
sleep(30); // Wait half a minute before reconnecting
}
}
}