author | Dan |
Sun, 14 Dec 2008 00:51:06 -0500 | |
changeset 35 | a76ae7eaa2eb |
parent 33 | c3179049f670 |
child 38 | e6a4b7f91e91 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
||
3 |
/** |
|
4 |
* PHP IRC Client library |
|
5 |
* Copyright (C) 2008 Dan Fuhry. All rights reserved. |
|
6 |
*/ |
|
7 |
||
8 |
/** |
|
9 |
* Version number |
|
10 |
* @const string |
|
11 |
*/ |
|
12 |
||
13 |
define('REQUEST_IRC_VERSION', '0.1'); |
|
14 |
||
15 |
/** |
|
16 |
* The base class for an IRC session. |
|
17 |
*/ |
|
18 |
||
19 |
class Request_IRC |
|
20 |
{ |
|
21 |
||
22 |
/** |
|
23 |
* Hostname |
|
24 |
* @var string |
|
25 |
*/ |
|
26 |
||
27 |
private $host = ''; |
|
28 |
||
29 |
/** |
|
30 |
* Port number |
|
31 |
* @var int |
|
32 |
*/ |
|
33 |
||
34 |
private $port = 6667; |
|
35 |
||
36 |
/** |
|
37 |
* The socket for the connection. |
|
38 |
* @var resource |
|
39 |
*/ |
|
40 |
||
41 |
public $sock = false; |
|
42 |
||
43 |
/** |
|
44 |
* Channel objects, associative array |
|
45 |
* @var array |
|
46 |
*/ |
|
47 |
||
48 |
public $channels = array(); |
|
49 |
||
50 |
/** |
|
51 |
* The function called when a private message is received. |
|
52 |
* @var string |
|
53 |
*/ |
|
54 |
||
55 |
private $privmsg_handler = false; |
|
56 |
||
57 |
/** |
|
30 | 58 |
* The function called when a timeout is suspected. |
59 |
* @var string |
|
60 |
*/ |
|
61 |
||
62 |
private $timeout_warning_handler = false; |
|
63 |
||
64 |
/** |
|
65 |
* The function called when a timeout is confirmed. |
|
66 |
* @var string |
|
67 |
*/ |
|
68 |
||
69 |
private $timeout_error_handler = false; |
|
70 |
||
71 |
/** |
|
0 | 72 |
* Switch to track if quitted or not. Helps avoid quitting the connection twice thus causing write errors. |
73 |
* @var bool |
|
74 |
* @access private |
|
75 |
*/ |
|
76 |
||
77 |
protected $quitted = false; |
|
78 |
||
79 |
/** |
|
80 |
* The nickname we're connected as. Not modified once connected. |
|
81 |
* @var string |
|
82 |
*/ |
|
83 |
||
84 |
public $nick = ''; |
|
85 |
||
86 |
/** |
|
87 |
* The username we're connected as. Not modified once connected. |
|
88 |
* @var string |
|
89 |
*/ |
|
90 |
||
91 |
public $user = ''; |
|
92 |
||
93 |
/** |
|
94 |
* Constructor. |
|
95 |
* @param string Hostname |
|
96 |
* @param int Port number, defaults to 6667 |
|
97 |
*/ |
|
98 |
||
99 |
public function __construct($host, $port = 6667) |
|
100 |
{ |
|
101 |
// Check hostname |
|
102 |
if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) ) |
|
103 |
die(__CLASS__ . ': Invalid hostname'); |
|
104 |
$this->host = $host; |
|
105 |
||
106 |
// Check port |
|
107 |
if ( is_int($port) && $port >= 1 && $port <= 65535 ) |
|
108 |
$this->port = $port; |
|
109 |
else |
|
110 |
die(__CLASS__ . ': Invalid port'); |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Sets parameters and opens the connection. |
|
115 |
* @param string Nick |
|
116 |
* @param string User |
|
117 |
* @param string Real name |
|
118 |
* @param string NickServ password |
|
119 |
* @param int Flags, defaults to 0. |
|
120 |
*/ |
|
121 |
||
30 | 122 |
public function connect($nick, $username, $realname, $pass = false, $flags = 0) |
0 | 123 |
{ |
124 |
// Init connection |
|
125 |
$this->sock = fsockopen($this->host, $this->port); |
|
126 |
if ( !$this->sock ) |
|
127 |
throw new Exception('Could not make socket connection to host.'); |
|
128 |
||
8 | 129 |
stream_set_timeout($this->sock, 1); |
0 | 130 |
|
131 |
// Send nick and username |
|
132 |
$this->put("NICK $nick\r\n"); |
|
133 |
$this->put("USER $username 0 * :$realname\r\n"); |
|
134 |
||
8 | 135 |
// wait for a mode +i or end of the motd |
136 |
while ( true ) |
|
0 | 137 |
{ |
8 | 138 |
$msg = $this->get(); |
139 |
if ( empty($msg) ) |
|
140 |
continue; |
|
141 |
if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') ) |
|
0 | 142 |
{ |
8 | 143 |
break; |
0 | 144 |
} |
8 | 145 |
if ( preg_match('/^PING :(.+?)$/', $msg, $match) ) |
146 |
{ |
|
147 |
$this->put("PONG :{$match[1]}\r\n"); |
|
148 |
} |
|
0 | 149 |
} |
150 |
||
8 | 151 |
// identify to nickserv |
30 | 152 |
if ( $pass ) |
153 |
$this->privmsg('NickServ', "IDENTIFY $pass"); |
|
8 | 154 |
|
0 | 155 |
$this->nick = $nick; |
156 |
$this->user = $username; |
|
30 | 157 |
$this->quitted = false; |
0 | 158 |
} |
159 |
||
160 |
/** |
|
161 |
* Writes some data to the socket, abstracted for debugging purposes. |
|
162 |
* @param string Message to send, this should include a CRLF. |
|
163 |
*/ |
|
164 |
||
165 |
public function put($message) |
|
166 |
{ |
|
167 |
if ( !$this->sock ) |
|
168 |
{ |
|
169 |
if ( defined('LIBIRC_DEBUG') ) |
|
170 |
echo ">>> WRITE FAILED: $message"; |
|
171 |
return false; |
|
172 |
} |
|
173 |
if ( defined('LIBIRC_DEBUG') ) |
|
174 |
echo ">>> $message"; |
|
175 |
fwrite($this->sock, $message); |
|
176 |
} |
|
177 |
||
178 |
/** |
|
179 |
* Reads from the socket... |
|
180 |
* @return string |
|
181 |
*/ |
|
182 |
||
30 | 183 |
public function get($timeout = 1) |
0 | 184 |
{ |
185 |
if ( !$this->sock ) |
|
186 |
{ |
|
187 |
if ( defined('LIBIRC_DEBUG') ) |
|
188 |
echo "<<< READ FAILED\n"; |
|
189 |
return false; |
|
190 |
} |
|
30 | 191 |
if ( ($c = stream_select($r = array($this->sock), $w = null, $e = null, $timeout)) !== false ) |
192 |
{ |
|
193 |
if ( $c > 0 ) |
|
194 |
{ |
|
195 |
$out = fgets($this->sock, 4096); |
|
196 |
if ( defined('LIBIRC_DEBUG') ) |
|
197 |
if ( !empty($out) ) |
|
198 |
echo "<<< $out"; |
|
199 |
return $out; |
|
200 |
} |
|
201 |
} |
|
202 |
return false; |
|
0 | 203 |
} |
204 |
||
205 |
/** |
|
206 |
* Sends a message to a nick or channel. |
|
207 |
* @param string Nick or channel |
|
208 |
* @param string Message |
|
209 |
*/ |
|
210 |
||
211 |
public function privmsg($nick, $message) |
|
212 |
{ |
|
213 |
$message = str_replace("\r\n", "\n", $message); |
|
214 |
$message = explode("\n", $message); |
|
215 |
foreach ( $message as $line ) |
|
216 |
{ |
|
32 | 217 |
$line = $this->filter_message($line); |
0 | 218 |
$this->put("PRIVMSG $nick :$line\r\n"); |
219 |
} |
|
220 |
} |
|
221 |
||
222 |
/** |
|
33 | 223 |
* Parse bold (<b>...</b>) tags and color tags in a text into IRC speak, and process /me commands. Colors are <cyan>...</cyan>, specify background with <fg:bg>...</fgcolor:bgcolor>. Valid colors are white, black, navy, green, red, maroon, purple, orange, yellow, lime, teal, aqua, cyan, blue, pink, grey, and silver |
32 | 224 |
* @param string Text to filter |
225 |
* @return string |
|
226 |
*/ |
|
227 |
||
228 |
function filter_message($text) |
|
229 |
{ |
|
230 |
$text = preg_replace('#<b>(.*?)</b>#is', "\x02$1\x02", $text); |
|
231 |
if ( preg_match('#^/me #i', $text) ) |
|
232 |
{ |
|
233 |
$text = "\x01ACTION " . preg_replace('#^/me #i', '', $text) . "\x01"; |
|
234 |
} |
|
235 |
$supportedcolors = array('white', 'black', 'navy', 'green', 'red', 'maroon', 'purple', 'orange', 'yellow', 'lime', 'teal', 'cyan', 'blue', 'pink', 'grey', 'silver'); |
|
236 |
$colors = implode('|', $supportedcolors); |
|
237 |
$supportedcolors = array_flip($supportedcolors); |
|
238 |
preg_match_all("#<((?:$colors)(?::(?:$colors))?)>(.*?)</\\1>#is", $text, $matches); |
|
239 |
foreach ( $matches[0] as $i => $match ) |
|
240 |
{ |
|
241 |
preg_match("#<(?:($colors)(?::($colors))?)>#i", $match, $colordata); |
|
242 |
$fgcolor = $supportedcolors[$colordata[1]]; |
|
243 |
$bgcolor = $colordata[1] ? $supportedcolors[$colordata[2]] : ''; |
|
244 |
$fgcolor = ( $fgcolor < 10 ) ? "0$fgcolor" : "$fgcolor"; |
|
245 |
if ( is_int($bgcolor) ) |
|
246 |
$bgcolor = ( $bgcolor < 10 ) ? ",0$bgcolor" : ",$bgcolor"; |
|
247 |
$text = str_replace($match, "\x03{$fgcolor}{$bgcolor}{$matches[2][$i]}\x03", $text); |
|
248 |
} |
|
249 |
||
250 |
return $text; |
|
251 |
} |
|
252 |
||
253 |
/** |
|
0 | 254 |
* The main event loop. |
255 |
*/ |
|
256 |
||
257 |
public function event_loop() |
|
258 |
{ |
|
30 | 259 |
$timeout = 180; |
260 |
$timeout_warn = false; |
|
261 |
while ( true ) |
|
0 | 262 |
{ |
30 | 263 |
$data = $this->get($timeout); |
264 |
$data_trim = $data ? trim($data) : false; |
|
265 |
if ( empty($data_trim) ) |
|
266 |
{ |
|
267 |
if ( $timeout_warn ) |
|
268 |
{ |
|
269 |
// timeout confirmed :-/ |
|
270 |
if ( $this->timeout_error_handler ) |
|
271 |
{ |
|
272 |
$result = @call_user_func($this->timeout_error_handler, $this); |
|
273 |
if ( $result == 'BREAK' ) |
|
274 |
break; |
|
275 |
} |
|
276 |
$timeout = 180; |
|
277 |
$timeout_warn = false; |
|
278 |
continue; |
|
279 |
} |
|
280 |
// timeout suspected |
|
281 |
if ( $this->timeout_warning_handler ) |
|
282 |
{ |
|
283 |
$result = @call_user_func($this->timeout_warning_handler, $this); |
|
284 |
if ( $result == 'BREAK' ) |
|
285 |
break; |
|
286 |
} |
|
287 |
// ping the server |
|
288 |
$this->put("PING :{$this->nick}\r\n"); |
|
289 |
// set timeout lower to make reconnecting work as fast as possible |
|
290 |
$timeout = 10; |
|
291 |
$timeout_warn = true; |
|
292 |
continue; |
|
293 |
} |
|
0 | 294 |
$match = self::parse_message($data_trim); |
295 |
if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) ) |
|
296 |
{ |
|
297 |
$this->put("PONG :{$pmatch[1]}\r\n"); |
|
8 | 298 |
eval(eb_fetch_hook('event_ping')); |
0 | 299 |
} |
30 | 300 |
else if ( preg_match('/^:((?:[a-z0-9-]+\.)*[a-z0-9-]+) PONG \\1 :' . preg_quote($this->nick) .'/', $data_trim) ) |
301 |
{ |
|
302 |
$timeout = 180; |
|
303 |
$timeout_warn = false; |
|
304 |
} |
|
0 | 305 |
else if ( $match ) |
306 |
{ |
|
307 |
// Received PRIVMSG or other mainstream action |
|
8 | 308 |
if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' ) |
0 | 309 |
$channel =& $match['message']; |
310 |
else |
|
311 |
$channel =& $match['target']; |
|
312 |
||
313 |
if ( !preg_match('/^[#!&\+]/', $channel) ) |
|
314 |
{ |
|
315 |
// Private message from user |
|
316 |
$result = $this->handle_privmsg($data); |
|
8 | 317 |
@stream_set_timeout($this->sock, 0xFFFFFFFE); |
0 | 318 |
} |
319 |
else if ( isset($this->channels[strtolower($channel)]) ) |
|
320 |
{ |
|
321 |
// Message into channel |
|
322 |
$chan =& $this->channels[strtolower($channel)]; |
|
323 |
$func = $chan->get_handler(); |
|
324 |
$result = @call_user_func($func, $data, $chan); |
|
8 | 325 |
@stream_set_timeout($this->sock, 0xFFFFFFFE); |
0 | 326 |
} |
327 |
if ( $result == 'BREAK' ) |
|
328 |
{ |
|
329 |
break; |
|
330 |
} |
|
331 |
} |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Processor for when a private message is received. |
|
337 |
* @access private |
|
338 |
*/ |
|
339 |
||
340 |
private function handle_privmsg($message) |
|
341 |
{ |
|
342 |
$message = self::parse_message($message); |
|
21 | 343 |
$message['message'] = preg_replace('/^(.+?):/', '', $message['message']); |
0 | 344 |
$ph = $this->privmsg_handler; |
345 |
if ( @function_exists($ph) ) |
|
346 |
return @call_user_func($ph, $message); |
|
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Changes the function called upon receipt of a private message. |
|
351 |
* @param string Function to call, will be passed a parsed message. |
|
352 |
*/ |
|
353 |
||
354 |
function set_privmsg_handler($func) |
|
355 |
{ |
|
356 |
if ( !function_exists($func) ) |
|
357 |
return false; |
|
358 |
$this->privmsg_handler = $func; |
|
359 |
return true; |
|
360 |
} |
|
361 |
||
362 |
/** |
|
30 | 363 |
* Changes the functions called when IRC connection timeouts occur. |
364 |
* @param string Function to call when a warning (no traffic within 3 minutes) occurs. If false, nothing will be called. |
|
365 |
* @param string Function to call if a ping timeout occurs. If false, nothing will be called. |
|
366 |
*/ |
|
367 |
||
368 |
function set_timeout_handlers($warn_func, $error_func) |
|
369 |
{ |
|
370 |
$this->timeout_warning_handler = false; |
|
371 |
$this->timeout_error_handler = false; |
|
372 |
if ( function_exists($warn_func) ) |
|
373 |
{ |
|
374 |
$this->timeout_warning_handler = $warn_func; |
|
375 |
} |
|
376 |
if ( function_exists($error_func) ) |
|
377 |
{ |
|
378 |
$this->timeout_error_handler = $error_func; |
|
379 |
} |
|
380 |
return true; |
|
381 |
} |
|
382 |
||
383 |
/** |
|
0 | 384 |
* Parses a message line. |
385 |
* @param string Message text |
|
386 |
* @return array Associative with keys: nick, user, host, action, target, message |
|
387 |
*/ |
|
388 |
||
389 |
public static function parse_message($message) |
|
390 |
{ |
|
391 |
// Indices: 12 3 4 5 67 8 |
|
392 |
$mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match); |
|
393 |
if ( !$mc ) |
|
394 |
{ |
|
395 |
return false; |
|
396 |
} |
|
397 |
// Indices: 0 1 2 3 4 5 6 7 8 |
|
398 |
list( , , $nick, $user, $host, $action, , $target, $message) = $match; |
|
399 |
return array( |
|
400 |
'nick' => $nick, |
|
401 |
'user' => $user, |
|
402 |
'host' => $host, |
|
403 |
'action' => $action, |
|
404 |
'target' => $target, |
|
405 |
'message' => trim($message) |
|
406 |
); |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Joins a channel, and returns a Request_IRC_Channel object. |
|
411 |
* @param string Channel name (remember # prefix) |
|
412 |
* @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object |
|
413 |
* @return object |
|
414 |
*/ |
|
415 |
||
416 |
function join($channel, $handler) |
|
417 |
{ |
|
418 |
$chan = new Request_IRC_Channel(strtolower($channel), $handler, $this); |
|
419 |
$this->channels[strtolower($channel)] = $chan; |
|
420 |
return $chan; |
|
421 |
} |
|
422 |
||
423 |
/** |
|
30 | 424 |
* Changes the current nick. |
425 |
* @param string New nick. |
|
426 |
* @param string Password to authenticate to NickServ if needed. |
|
427 |
*/ |
|
428 |
||
429 |
public function change_nick($nick, $pass = false) |
|
430 |
{ |
|
431 |
$this->put("NICK $nick\r\n"); |
|
432 |
$this->nick = $nick; |
|
433 |
if ( $pass ) |
|
434 |
{ |
|
435 |
while ( $data = $this->get(3) ) |
|
436 |
{ |
|
437 |
if ( strstr($data, 'NickServ') ) |
|
438 |
{ |
|
439 |
$this->privmsg('NickServ', "IDENTIFY $pass"); |
|
440 |
break; |
|
441 |
} |
|
442 |
} |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
/** |
|
0 | 447 |
* Closes the connection and quits. |
448 |
* @param string Optional part message |
|
449 |
*/ |
|
450 |
||
451 |
public function close($partmsg = false) |
|
452 |
{ |
|
453 |
if ( $this->quitted ) |
|
454 |
return true; |
|
455 |
||
456 |
$this->quitted = true; |
|
457 |
// Part all channels |
|
458 |
if ( !$partmsg ) |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
459 |
$partmsg = 'Closing connection (no reason given)'; |
0 | 460 |
|
461 |
foreach ( $this->channels as $channel ) |
|
462 |
{ |
|
463 |
$channel->part($partmsg); |
|
464 |
} |
|
465 |
||
466 |
$this->put("QUIT\r\n"); |
|
467 |
||
30 | 468 |
while ( $msg = $this->get(1) ) |
0 | 469 |
{ |
470 |
// Do nothing. |
|
471 |
} |
|
472 |
||
473 |
fclose($this->sock); |
|
474 |
} |
|
475 |
||
476 |
} |
|
477 |
||
478 |
/** |
|
479 |
* Wrapper for channels. |
|
480 |
*/ |
|
481 |
||
482 |
class Request_IRC_Channel extends Request_IRC |
|
483 |
{ |
|
484 |
||
485 |
/** |
|
486 |
* The name of the channel |
|
487 |
* @var string |
|
488 |
*/ |
|
489 |
||
490 |
private $channel_name = ''; |
|
491 |
||
492 |
/** |
|
493 |
* The event handler function. |
|
494 |
* @var string |
|
495 |
*/ |
|
496 |
||
497 |
private $handler = ''; |
|
498 |
||
499 |
/** |
|
500 |
* The parent connection. |
|
501 |
* @var object |
|
502 |
*/ |
|
503 |
||
504 |
public $parent = false; |
|
505 |
||
506 |
/** |
|
507 |
* Whether the channel has been parted or not, used to kill the destructor. |
|
508 |
* @var bool |
|
509 |
*/ |
|
510 |
||
511 |
protected $parted = false; |
|
512 |
||
513 |
/** |
|
514 |
* Constructor. |
|
515 |
* @param string Channel name |
|
516 |
* @param string Handler function |
|
517 |
* @param object IRC connection (Request_IRC object) |
|
518 |
*/ |
|
519 |
||
520 |
function __construct($channel, $handler, $parent) |
|
521 |
{ |
|
522 |
$this->parent = $parent; |
|
523 |
$this->parent->put("JOIN $channel\r\n"); |
|
8 | 524 |
// stream_set_timeout($this->parent->sock, 3); |
525 |
// while ( $msg = $this->parent->get() ) |
|
526 |
// { |
|
527 |
// // Do nothing |
|
528 |
// } |
|
0 | 529 |
$this->channel_name = $channel; |
530 |
$this->handler = $handler; |
|
8 | 531 |
eval(eb_fetch_hook('event_self_join')); |
0 | 532 |
} |
533 |
||
534 |
/** |
|
535 |
* Returns the channel name |
|
536 |
* @return string |
|
537 |
*/ |
|
538 |
||
539 |
function get_channel_name() |
|
540 |
{ |
|
541 |
return $this->channel_name; |
|
542 |
} |
|
543 |
||
544 |
/** |
|
545 |
* Returns the handler function |
|
546 |
* @return string |
|
547 |
*/ |
|
548 |
||
549 |
function get_handler() |
|
550 |
{ |
|
551 |
return $this->handler; |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* Sends a message. |
|
556 |
* @param string message |
|
557 |
* @param bool If true, will fire a message event when the message is sent. |
|
558 |
*/ |
|
559 |
||
560 |
function msg($msg, $fire_event = false) |
|
561 |
{ |
|
562 |
$this->parent->privmsg($this->channel_name, $msg); |
|
563 |
if ( $fire_event ) |
|
564 |
{ |
|
565 |
$func = $this->get_handler(); |
|
566 |
// format: :nick!user@host PRIVMSG #channel :msg. |
|
567 |
$lines = explode("\n", $msg); |
|
568 |
foreach ( $lines as $line ) |
|
569 |
{ |
|
570 |
$data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line"; |
|
571 |
$result = @call_user_func($func, $data, $this); |
|
572 |
stream_set_timeout($this->parent->sock, 0xFFFFFFFE); |
|
573 |
} |
|
574 |
} |
|
575 |
} |
|
576 |
||
577 |
/** |
|
578 |
* Destructor, automatically parts the channel. |
|
579 |
*/ |
|
580 |
||
581 |
function __destruct() |
|
582 |
{ |
|
583 |
if ( !$this->parted ) |
|
584 |
$this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION); |
|
585 |
} |
|
586 |
||
587 |
/** |
|
588 |
* Parts the channel. |
|
589 |
* @param string Optional message |
|
590 |
*/ |
|
591 |
||
592 |
function part($msg = '') |
|
593 |
{ |
|
594 |
$this->parent->put("PART {$this->channel_name} :$msg\r\n"); |
|
595 |
$this->parted = true; |
|
596 |
unset($this->parent->channels[$this->channel_name]); |
|
597 |
} |
|
598 |
||
599 |
} |
|
600 |
||
601 |
?> |