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 |
{
|
|
217 |
$this->put("PRIVMSG $nick :$line\r\n");
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
/**
|
|
222 |
* The main event loop.
|
|
223 |
*/
|
|
224 |
|
|
225 |
public function event_loop()
|
|
226 |
{
|
30
|
227 |
$timeout = 180;
|
|
228 |
$timeout_warn = false;
|
|
229 |
while ( true )
|
0
|
230 |
{
|
30
|
231 |
$data = $this->get($timeout);
|
|
232 |
$data_trim = $data ? trim($data) : false;
|
|
233 |
if ( empty($data_trim) )
|
|
234 |
{
|
|
235 |
if ( $timeout_warn )
|
|
236 |
{
|
|
237 |
// timeout confirmed :-/
|
|
238 |
if ( $this->timeout_error_handler )
|
|
239 |
{
|
|
240 |
$result = @call_user_func($this->timeout_error_handler, $this);
|
|
241 |
if ( $result == 'BREAK' )
|
|
242 |
break;
|
|
243 |
}
|
|
244 |
$timeout = 180;
|
|
245 |
$timeout_warn = false;
|
|
246 |
continue;
|
|
247 |
}
|
|
248 |
// timeout suspected
|
|
249 |
if ( $this->timeout_warning_handler )
|
|
250 |
{
|
|
251 |
$result = @call_user_func($this->timeout_warning_handler, $this);
|
|
252 |
if ( $result == 'BREAK' )
|
|
253 |
break;
|
|
254 |
}
|
|
255 |
// ping the server
|
|
256 |
$this->put("PING :{$this->nick}\r\n");
|
|
257 |
// set timeout lower to make reconnecting work as fast as possible
|
|
258 |
$timeout = 10;
|
|
259 |
$timeout_warn = true;
|
|
260 |
continue;
|
|
261 |
}
|
0
|
262 |
$match = self::parse_message($data_trim);
|
|
263 |
if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
|
|
264 |
{
|
|
265 |
$this->put("PONG :{$pmatch[1]}\r\n");
|
8
|
266 |
eval(eb_fetch_hook('event_ping'));
|
0
|
267 |
}
|
30
|
268 |
else if ( preg_match('/^:((?:[a-z0-9-]+\.)*[a-z0-9-]+) PONG \\1 :' . preg_quote($this->nick) .'/', $data_trim) )
|
|
269 |
{
|
|
270 |
$timeout = 180;
|
|
271 |
$timeout_warn = false;
|
|
272 |
}
|
0
|
273 |
else if ( $match )
|
|
274 |
{
|
|
275 |
// Received PRIVMSG or other mainstream action
|
8
|
276 |
if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' )
|
0
|
277 |
$channel =& $match['message'];
|
|
278 |
else
|
|
279 |
$channel =& $match['target'];
|
|
280 |
|
|
281 |
if ( !preg_match('/^[#!&\+]/', $channel) )
|
|
282 |
{
|
|
283 |
// Private message from user
|
|
284 |
$result = $this->handle_privmsg($data);
|
8
|
285 |
@stream_set_timeout($this->sock, 0xFFFFFFFE);
|
0
|
286 |
}
|
|
287 |
else if ( isset($this->channels[strtolower($channel)]) )
|
|
288 |
{
|
|
289 |
// Message into channel
|
|
290 |
$chan =& $this->channels[strtolower($channel)];
|
|
291 |
$func = $chan->get_handler();
|
|
292 |
$result = @call_user_func($func, $data, $chan);
|
8
|
293 |
@stream_set_timeout($this->sock, 0xFFFFFFFE);
|
0
|
294 |
}
|
|
295 |
if ( $result == 'BREAK' )
|
|
296 |
{
|
|
297 |
break;
|
|
298 |
}
|
|
299 |
}
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
/**
|
|
304 |
* Processor for when a private message is received.
|
|
305 |
* @access private
|
|
306 |
*/
|
|
307 |
|
|
308 |
private function handle_privmsg($message)
|
|
309 |
{
|
|
310 |
$message = self::parse_message($message);
|
21
|
311 |
$message['message'] = preg_replace('/^(.+?):/', '', $message['message']);
|
0
|
312 |
$ph = $this->privmsg_handler;
|
|
313 |
if ( @function_exists($ph) )
|
|
314 |
return @call_user_func($ph, $message);
|
|
315 |
}
|
|
316 |
|
|
317 |
/**
|
|
318 |
* Changes the function called upon receipt of a private message.
|
|
319 |
* @param string Function to call, will be passed a parsed message.
|
|
320 |
*/
|
|
321 |
|
|
322 |
function set_privmsg_handler($func)
|
|
323 |
{
|
|
324 |
if ( !function_exists($func) )
|
|
325 |
return false;
|
|
326 |
$this->privmsg_handler = $func;
|
|
327 |
return true;
|
|
328 |
}
|
|
329 |
|
|
330 |
/**
|
30
|
331 |
* Changes the functions called when IRC connection timeouts occur.
|
|
332 |
* @param string Function to call when a warning (no traffic within 3 minutes) occurs. If false, nothing will be called.
|
|
333 |
* @param string Function to call if a ping timeout occurs. If false, nothing will be called.
|
|
334 |
*/
|
|
335 |
|
|
336 |
function set_timeout_handlers($warn_func, $error_func)
|
|
337 |
{
|
|
338 |
$this->timeout_warning_handler = false;
|
|
339 |
$this->timeout_error_handler = false;
|
|
340 |
if ( function_exists($warn_func) )
|
|
341 |
{
|
|
342 |
$this->timeout_warning_handler = $warn_func;
|
|
343 |
}
|
|
344 |
if ( function_exists($error_func) )
|
|
345 |
{
|
|
346 |
$this->timeout_error_handler = $error_func;
|
|
347 |
}
|
|
348 |
return true;
|
|
349 |
}
|
|
350 |
|
|
351 |
/**
|
0
|
352 |
* Parses a message line.
|
|
353 |
* @param string Message text
|
|
354 |
* @return array Associative with keys: nick, user, host, action, target, message
|
|
355 |
*/
|
|
356 |
|
|
357 |
public static function parse_message($message)
|
|
358 |
{
|
|
359 |
// Indices: 12 3 4 5 67 8
|
|
360 |
$mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
|
|
361 |
if ( !$mc )
|
|
362 |
{
|
|
363 |
return false;
|
|
364 |
}
|
|
365 |
// Indices: 0 1 2 3 4 5 6 7 8
|
|
366 |
list( , , $nick, $user, $host, $action, , $target, $message) = $match;
|
|
367 |
return array(
|
|
368 |
'nick' => $nick,
|
|
369 |
'user' => $user,
|
|
370 |
'host' => $host,
|
|
371 |
'action' => $action,
|
|
372 |
'target' => $target,
|
|
373 |
'message' => trim($message)
|
|
374 |
);
|
|
375 |
}
|
|
376 |
|
|
377 |
/**
|
|
378 |
* Joins a channel, and returns a Request_IRC_Channel object.
|
|
379 |
* @param string Channel name (remember # prefix)
|
|
380 |
* @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object
|
|
381 |
* @return object
|
|
382 |
*/
|
|
383 |
|
|
384 |
function join($channel, $handler)
|
|
385 |
{
|
|
386 |
$chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
|
|
387 |
$this->channels[strtolower($channel)] = $chan;
|
|
388 |
return $chan;
|
|
389 |
}
|
|
390 |
|
|
391 |
/**
|
30
|
392 |
* Changes the current nick.
|
|
393 |
* @param string New nick.
|
|
394 |
* @param string Password to authenticate to NickServ if needed.
|
|
395 |
*/
|
|
396 |
|
|
397 |
public function change_nick($nick, $pass = false)
|
|
398 |
{
|
|
399 |
$this->put("NICK $nick\r\n");
|
|
400 |
$this->nick = $nick;
|
|
401 |
if ( $pass )
|
|
402 |
{
|
|
403 |
while ( $data = $this->get(3) )
|
|
404 |
{
|
|
405 |
if ( strstr($data, 'NickServ') )
|
|
406 |
{
|
|
407 |
$this->privmsg('NickServ', "IDENTIFY $pass");
|
|
408 |
break;
|
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|
|
412 |
}
|
|
413 |
|
|
414 |
/**
|
0
|
415 |
* Closes the connection and quits.
|
|
416 |
* @param string Optional part message
|
|
417 |
*/
|
|
418 |
|
|
419 |
public function close($partmsg = false)
|
|
420 |
{
|
|
421 |
if ( $this->quitted )
|
|
422 |
return true;
|
|
423 |
|
|
424 |
$this->quitted = true;
|
|
425 |
// Part all channels
|
|
426 |
if ( !$partmsg )
|
|
427 |
$partmsg = 'IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION;
|
|
428 |
|
|
429 |
foreach ( $this->channels as $channel )
|
|
430 |
{
|
|
431 |
$channel->part($partmsg);
|
|
432 |
}
|
|
433 |
|
|
434 |
$this->put("QUIT\r\n");
|
|
435 |
|
30
|
436 |
while ( $msg = $this->get(1) )
|
0
|
437 |
{
|
|
438 |
// Do nothing.
|
|
439 |
}
|
|
440 |
|
|
441 |
fclose($this->sock);
|
|
442 |
}
|
|
443 |
|
|
444 |
}
|
|
445 |
|
|
446 |
/**
|
|
447 |
* Wrapper for channels.
|
|
448 |
*/
|
|
449 |
|
|
450 |
class Request_IRC_Channel extends Request_IRC
|
|
451 |
{
|
|
452 |
|
|
453 |
/**
|
|
454 |
* The name of the channel
|
|
455 |
* @var string
|
|
456 |
*/
|
|
457 |
|
|
458 |
private $channel_name = '';
|
|
459 |
|
|
460 |
/**
|
|
461 |
* The event handler function.
|
|
462 |
* @var string
|
|
463 |
*/
|
|
464 |
|
|
465 |
private $handler = '';
|
|
466 |
|
|
467 |
/**
|
|
468 |
* The parent connection.
|
|
469 |
* @var object
|
|
470 |
*/
|
|
471 |
|
|
472 |
public $parent = false;
|
|
473 |
|
|
474 |
/**
|
|
475 |
* Whether the channel has been parted or not, used to kill the destructor.
|
|
476 |
* @var bool
|
|
477 |
*/
|
|
478 |
|
|
479 |
protected $parted = false;
|
|
480 |
|
|
481 |
/**
|
|
482 |
* Constructor.
|
|
483 |
* @param string Channel name
|
|
484 |
* @param string Handler function
|
|
485 |
* @param object IRC connection (Request_IRC object)
|
|
486 |
*/
|
|
487 |
|
|
488 |
function __construct($channel, $handler, $parent)
|
|
489 |
{
|
|
490 |
$this->parent = $parent;
|
|
491 |
$this->parent->put("JOIN $channel\r\n");
|
8
|
492 |
// stream_set_timeout($this->parent->sock, 3);
|
|
493 |
// while ( $msg = $this->parent->get() )
|
|
494 |
// {
|
|
495 |
// // Do nothing
|
|
496 |
// }
|
0
|
497 |
$this->channel_name = $channel;
|
|
498 |
$this->handler = $handler;
|
8
|
499 |
eval(eb_fetch_hook('event_self_join'));
|
0
|
500 |
}
|
|
501 |
|
|
502 |
/**
|
|
503 |
* Returns the channel name
|
|
504 |
* @return string
|
|
505 |
*/
|
|
506 |
|
|
507 |
function get_channel_name()
|
|
508 |
{
|
|
509 |
return $this->channel_name;
|
|
510 |
}
|
|
511 |
|
|
512 |
/**
|
|
513 |
* Returns the handler function
|
|
514 |
* @return string
|
|
515 |
*/
|
|
516 |
|
|
517 |
function get_handler()
|
|
518 |
{
|
|
519 |
return $this->handler;
|
|
520 |
}
|
|
521 |
|
|
522 |
/**
|
|
523 |
* Sends a message.
|
|
524 |
* @param string message
|
|
525 |
* @param bool If true, will fire a message event when the message is sent.
|
|
526 |
*/
|
|
527 |
|
|
528 |
function msg($msg, $fire_event = false)
|
|
529 |
{
|
|
530 |
$this->parent->privmsg($this->channel_name, $msg);
|
|
531 |
if ( $fire_event )
|
|
532 |
{
|
|
533 |
$func = $this->get_handler();
|
|
534 |
// format: :nick!user@host PRIVMSG #channel :msg.
|
|
535 |
$lines = explode("\n", $msg);
|
|
536 |
foreach ( $lines as $line )
|
|
537 |
{
|
|
538 |
$data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
|
|
539 |
$result = @call_user_func($func, $data, $this);
|
|
540 |
stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
|
|
541 |
}
|
|
542 |
}
|
|
543 |
}
|
|
544 |
|
|
545 |
/**
|
|
546 |
* Destructor, automatically parts the channel.
|
|
547 |
*/
|
|
548 |
|
|
549 |
function __destruct()
|
|
550 |
{
|
|
551 |
if ( !$this->parted )
|
|
552 |
$this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
|
|
553 |
}
|
|
554 |
|
|
555 |
/**
|
|
556 |
* Parts the channel.
|
|
557 |
* @param string Optional message
|
|
558 |
*/
|
|
559 |
|
|
560 |
function part($msg = '')
|
|
561 |
{
|
|
562 |
$this->parent->put("PART {$this->channel_name} :$msg\r\n");
|
|
563 |
$this->parted = true;
|
|
564 |
unset($this->parent->channels[$this->channel_name]);
|
|
565 |
}
|
|
566 |
|
|
567 |
}
|
|
568 |
|
|
569 |
?>
|