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 |
/**
|
|
58 |
* Switch to track if quitted or not. Helps avoid quitting the connection twice thus causing write errors.
|
|
59 |
* @var bool
|
|
60 |
* @access private
|
|
61 |
*/
|
|
62 |
|
|
63 |
protected $quitted = false;
|
|
64 |
|
|
65 |
/**
|
|
66 |
* The nickname we're connected as. Not modified once connected.
|
|
67 |
* @var string
|
|
68 |
*/
|
|
69 |
|
|
70 |
public $nick = '';
|
|
71 |
|
|
72 |
/**
|
|
73 |
* The username we're connected as. Not modified once connected.
|
|
74 |
* @var string
|
|
75 |
*/
|
|
76 |
|
|
77 |
public $user = '';
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Constructor.
|
|
81 |
* @param string Hostname
|
|
82 |
* @param int Port number, defaults to 6667
|
|
83 |
*/
|
|
84 |
|
|
85 |
public function __construct($host, $port = 6667)
|
|
86 |
{
|
|
87 |
// Check hostname
|
|
88 |
if ( !preg_match('/^(([a-z0-9-]+\.)*?)([a-z0-9-]+)$/', $host) )
|
|
89 |
die(__CLASS__ . ': Invalid hostname');
|
|
90 |
$this->host = $host;
|
|
91 |
|
|
92 |
// Check port
|
|
93 |
if ( is_int($port) && $port >= 1 && $port <= 65535 )
|
|
94 |
$this->port = $port;
|
|
95 |
else
|
|
96 |
die(__CLASS__ . ': Invalid port');
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Sets parameters and opens the connection.
|
|
101 |
* @param string Nick
|
|
102 |
* @param string User
|
|
103 |
* @param string Real name
|
|
104 |
* @param string NickServ password
|
|
105 |
* @param int Flags, defaults to 0.
|
|
106 |
*/
|
|
107 |
|
|
108 |
public function connect($nick, $username, $realname, $pass, $flags = 0)
|
|
109 |
{
|
|
110 |
// Init connection
|
|
111 |
$this->sock = fsockopen($this->host, $this->port);
|
|
112 |
if ( !$this->sock )
|
|
113 |
throw new Exception('Could not make socket connection to host.');
|
|
114 |
|
8
|
115 |
stream_set_timeout($this->sock, 1);
|
0
|
116 |
|
|
117 |
// Send nick and username
|
|
118 |
$this->put("NICK $nick\r\n");
|
|
119 |
$this->put("USER $username 0 * :$realname\r\n");
|
|
120 |
|
8
|
121 |
// wait for a mode +i or end of the motd
|
|
122 |
while ( true )
|
0
|
123 |
{
|
8
|
124 |
$msg = $this->get();
|
|
125 |
if ( empty($msg) )
|
|
126 |
continue;
|
|
127 |
if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') )
|
0
|
128 |
{
|
8
|
129 |
break;
|
0
|
130 |
}
|
8
|
131 |
if ( preg_match('/^PING :(.+?)$/', $msg, $match) )
|
|
132 |
{
|
|
133 |
$this->put("PONG :{$match[1]}\r\n");
|
|
134 |
}
|
0
|
135 |
}
|
|
136 |
|
8
|
137 |
// identify to nickserv
|
|
138 |
$this->privmsg('NickServ', "IDENTIFY $pass");
|
|
139 |
|
0
|
140 |
$this->nick = $nick;
|
|
141 |
$this->user = $username;
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
* Writes some data to the socket, abstracted for debugging purposes.
|
|
146 |
* @param string Message to send, this should include a CRLF.
|
|
147 |
*/
|
|
148 |
|
|
149 |
public function put($message)
|
|
150 |
{
|
|
151 |
if ( !$this->sock )
|
|
152 |
{
|
|
153 |
if ( defined('LIBIRC_DEBUG') )
|
|
154 |
echo ">>> WRITE FAILED: $message";
|
|
155 |
return false;
|
|
156 |
}
|
|
157 |
if ( defined('LIBIRC_DEBUG') )
|
|
158 |
echo ">>> $message";
|
|
159 |
fwrite($this->sock, $message);
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* Reads from the socket...
|
|
164 |
* @return string
|
|
165 |
*/
|
|
166 |
|
|
167 |
public function get()
|
|
168 |
{
|
|
169 |
if ( !$this->sock )
|
|
170 |
{
|
|
171 |
if ( defined('LIBIRC_DEBUG') )
|
|
172 |
echo "<<< READ FAILED\n";
|
|
173 |
return false;
|
|
174 |
}
|
|
175 |
$out = fgets($this->sock, 4096);
|
|
176 |
if ( defined('LIBIRC_DEBUG') )
|
|
177 |
if ( !empty($out) )
|
|
178 |
echo "<<< $out";
|
|
179 |
return $out;
|
|
180 |
}
|
|
181 |
|
|
182 |
/**
|
|
183 |
* Sends a message to a nick or channel.
|
|
184 |
* @param string Nick or channel
|
|
185 |
* @param string Message
|
|
186 |
*/
|
|
187 |
|
|
188 |
public function privmsg($nick, $message)
|
|
189 |
{
|
|
190 |
$message = str_replace("\r\n", "\n", $message);
|
|
191 |
$message = explode("\n", $message);
|
|
192 |
foreach ( $message as $line )
|
|
193 |
{
|
|
194 |
$this->put("PRIVMSG $nick :$line\r\n");
|
|
195 |
}
|
|
196 |
}
|
|
197 |
|
|
198 |
/**
|
|
199 |
* The main event loop.
|
|
200 |
*/
|
|
201 |
|
|
202 |
public function event_loop()
|
|
203 |
{
|
|
204 |
stream_set_timeout($this->sock, 0xFFFFFFFE);
|
|
205 |
while ( $data = $this->get() )
|
|
206 |
{
|
|
207 |
$data_trim = trim($data);
|
|
208 |
$match = self::parse_message($data_trim);
|
|
209 |
if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) )
|
|
210 |
{
|
|
211 |
$this->put("PONG :{$pmatch[1]}\r\n");
|
8
|
212 |
eval(eb_fetch_hook('event_ping'));
|
0
|
213 |
}
|
|
214 |
else if ( $match )
|
|
215 |
{
|
|
216 |
// Received PRIVMSG or other mainstream action
|
8
|
217 |
if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' )
|
0
|
218 |
$channel =& $match['message'];
|
|
219 |
else
|
|
220 |
$channel =& $match['target'];
|
|
221 |
|
|
222 |
if ( !preg_match('/^[#!&\+]/', $channel) )
|
|
223 |
{
|
|
224 |
// Private message from user
|
|
225 |
$result = $this->handle_privmsg($data);
|
8
|
226 |
@stream_set_timeout($this->sock, 0xFFFFFFFE);
|
0
|
227 |
}
|
|
228 |
else if ( isset($this->channels[strtolower($channel)]) )
|
|
229 |
{
|
|
230 |
// Message into channel
|
|
231 |
$chan =& $this->channels[strtolower($channel)];
|
|
232 |
$func = $chan->get_handler();
|
|
233 |
$result = @call_user_func($func, $data, $chan);
|
8
|
234 |
@stream_set_timeout($this->sock, 0xFFFFFFFE);
|
0
|
235 |
}
|
|
236 |
if ( $result == 'BREAK' )
|
|
237 |
{
|
|
238 |
break;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Processor for when a private message is received.
|
|
246 |
* @access private
|
|
247 |
*/
|
|
248 |
|
|
249 |
private function handle_privmsg($message)
|
|
250 |
{
|
|
251 |
$message = self::parse_message($message);
|
|
252 |
$ph = $this->privmsg_handler;
|
|
253 |
if ( @function_exists($ph) )
|
|
254 |
return @call_user_func($ph, $message);
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Changes the function called upon receipt of a private message.
|
|
259 |
* @param string Function to call, will be passed a parsed message.
|
|
260 |
*/
|
|
261 |
|
|
262 |
function set_privmsg_handler($func)
|
|
263 |
{
|
|
264 |
if ( !function_exists($func) )
|
|
265 |
return false;
|
|
266 |
$this->privmsg_handler = $func;
|
|
267 |
return true;
|
|
268 |
}
|
|
269 |
|
|
270 |
/**
|
|
271 |
* Parses a message line.
|
|
272 |
* @param string Message text
|
|
273 |
* @return array Associative with keys: nick, user, host, action, target, message
|
|
274 |
*/
|
|
275 |
|
|
276 |
public static function parse_message($message)
|
|
277 |
{
|
|
278 |
// Indices: 12 3 4 5 67 8
|
|
279 |
$mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
|
|
280 |
if ( !$mc )
|
|
281 |
{
|
|
282 |
return false;
|
|
283 |
}
|
|
284 |
// Indices: 0 1 2 3 4 5 6 7 8
|
|
285 |
list( , , $nick, $user, $host, $action, , $target, $message) = $match;
|
|
286 |
return array(
|
|
287 |
'nick' => $nick,
|
|
288 |
'user' => $user,
|
|
289 |
'host' => $host,
|
|
290 |
'action' => $action,
|
|
291 |
'target' => $target,
|
|
292 |
'message' => trim($message)
|
|
293 |
);
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Joins a channel, and returns a Request_IRC_Channel object.
|
|
298 |
* @param string Channel name (remember # prefix)
|
|
299 |
* @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object
|
|
300 |
* @return object
|
|
301 |
*/
|
|
302 |
|
|
303 |
function join($channel, $handler)
|
|
304 |
{
|
|
305 |
$chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
|
|
306 |
$this->channels[strtolower($channel)] = $chan;
|
|
307 |
return $chan;
|
|
308 |
}
|
|
309 |
|
|
310 |
/**
|
|
311 |
* Closes the connection and quits.
|
|
312 |
* @param string Optional part message
|
|
313 |
*/
|
|
314 |
|
|
315 |
public function close($partmsg = false)
|
|
316 |
{
|
|
317 |
if ( $this->quitted )
|
|
318 |
return true;
|
|
319 |
|
|
320 |
$this->quitted = true;
|
|
321 |
// Part all channels
|
|
322 |
if ( !$partmsg )
|
|
323 |
$partmsg = 'IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION;
|
|
324 |
|
|
325 |
foreach ( $this->channels as $channel )
|
|
326 |
{
|
|
327 |
$channel->part($partmsg);
|
|
328 |
}
|
|
329 |
|
|
330 |
$this->put("QUIT\r\n");
|
|
331 |
|
|
332 |
while ( $msg = $this->get() )
|
|
333 |
{
|
|
334 |
// Do nothing.
|
|
335 |
}
|
|
336 |
|
|
337 |
fclose($this->sock);
|
|
338 |
}
|
|
339 |
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Wrapper for channels.
|
|
344 |
*/
|
|
345 |
|
|
346 |
class Request_IRC_Channel extends Request_IRC
|
|
347 |
{
|
|
348 |
|
|
349 |
/**
|
|
350 |
* The name of the channel
|
|
351 |
* @var string
|
|
352 |
*/
|
|
353 |
|
|
354 |
private $channel_name = '';
|
|
355 |
|
|
356 |
/**
|
|
357 |
* The event handler function.
|
|
358 |
* @var string
|
|
359 |
*/
|
|
360 |
|
|
361 |
private $handler = '';
|
|
362 |
|
|
363 |
/**
|
|
364 |
* The parent connection.
|
|
365 |
* @var object
|
|
366 |
*/
|
|
367 |
|
|
368 |
public $parent = false;
|
|
369 |
|
|
370 |
/**
|
|
371 |
* Whether the channel has been parted or not, used to kill the destructor.
|
|
372 |
* @var bool
|
|
373 |
*/
|
|
374 |
|
|
375 |
protected $parted = false;
|
|
376 |
|
|
377 |
/**
|
|
378 |
* Constructor.
|
|
379 |
* @param string Channel name
|
|
380 |
* @param string Handler function
|
|
381 |
* @param object IRC connection (Request_IRC object)
|
|
382 |
*/
|
|
383 |
|
|
384 |
function __construct($channel, $handler, $parent)
|
|
385 |
{
|
|
386 |
$this->parent = $parent;
|
|
387 |
$this->parent->put("JOIN $channel\r\n");
|
8
|
388 |
// stream_set_timeout($this->parent->sock, 3);
|
|
389 |
// while ( $msg = $this->parent->get() )
|
|
390 |
// {
|
|
391 |
// // Do nothing
|
|
392 |
// }
|
0
|
393 |
$this->channel_name = $channel;
|
|
394 |
$this->handler = $handler;
|
8
|
395 |
eval(eb_fetch_hook('event_self_join'));
|
0
|
396 |
}
|
|
397 |
|
|
398 |
/**
|
|
399 |
* Returns the channel name
|
|
400 |
* @return string
|
|
401 |
*/
|
|
402 |
|
|
403 |
function get_channel_name()
|
|
404 |
{
|
|
405 |
return $this->channel_name;
|
|
406 |
}
|
|
407 |
|
|
408 |
/**
|
|
409 |
* Returns the handler function
|
|
410 |
* @return string
|
|
411 |
*/
|
|
412 |
|
|
413 |
function get_handler()
|
|
414 |
{
|
|
415 |
return $this->handler;
|
|
416 |
}
|
|
417 |
|
|
418 |
/**
|
|
419 |
* Sends a message.
|
|
420 |
* @param string message
|
|
421 |
* @param bool If true, will fire a message event when the message is sent.
|
|
422 |
*/
|
|
423 |
|
|
424 |
function msg($msg, $fire_event = false)
|
|
425 |
{
|
|
426 |
$this->parent->privmsg($this->channel_name, $msg);
|
|
427 |
if ( $fire_event )
|
|
428 |
{
|
|
429 |
$func = $this->get_handler();
|
|
430 |
// format: :nick!user@host PRIVMSG #channel :msg.
|
|
431 |
$lines = explode("\n", $msg);
|
|
432 |
foreach ( $lines as $line )
|
|
433 |
{
|
|
434 |
$data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
|
|
435 |
$result = @call_user_func($func, $data, $this);
|
|
436 |
stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
|
|
437 |
}
|
|
438 |
}
|
|
439 |
}
|
|
440 |
|
|
441 |
/**
|
|
442 |
* Destructor, automatically parts the channel.
|
|
443 |
*/
|
|
444 |
|
|
445 |
function __destruct()
|
|
446 |
{
|
|
447 |
if ( !$this->parted )
|
|
448 |
$this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
|
|
449 |
}
|
|
450 |
|
|
451 |
/**
|
|
452 |
* Parts the channel.
|
|
453 |
* @param string Optional message
|
|
454 |
*/
|
|
455 |
|
|
456 |
function part($msg = '')
|
|
457 |
{
|
|
458 |
$this->parent->put("PART {$this->channel_name} :$msg\r\n");
|
|
459 |
$this->parted = true;
|
|
460 |
unset($this->parent->channels[$this->channel_name]);
|
|
461 |
}
|
|
462 |
|
|
463 |
}
|
|
464 |
|
|
465 |
?>
|