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);
|
21
|
252 |
$message['message'] = preg_replace('/^(.+?):/', '', $message['message']);
|
0
|
253 |
$ph = $this->privmsg_handler;
|
|
254 |
if ( @function_exists($ph) )
|
|
255 |
return @call_user_func($ph, $message);
|
|
256 |
}
|
|
257 |
|
|
258 |
/**
|
|
259 |
* Changes the function called upon receipt of a private message.
|
|
260 |
* @param string Function to call, will be passed a parsed message.
|
|
261 |
*/
|
|
262 |
|
|
263 |
function set_privmsg_handler($func)
|
|
264 |
{
|
|
265 |
if ( !function_exists($func) )
|
|
266 |
return false;
|
|
267 |
$this->privmsg_handler = $func;
|
|
268 |
return true;
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* Parses a message line.
|
|
273 |
* @param string Message text
|
|
274 |
* @return array Associative with keys: nick, user, host, action, target, message
|
|
275 |
*/
|
|
276 |
|
|
277 |
public static function parse_message($message)
|
|
278 |
{
|
|
279 |
// Indices: 12 3 4 5 67 8
|
|
280 |
$mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match);
|
|
281 |
if ( !$mc )
|
|
282 |
{
|
|
283 |
return false;
|
|
284 |
}
|
|
285 |
// Indices: 0 1 2 3 4 5 6 7 8
|
|
286 |
list( , , $nick, $user, $host, $action, , $target, $message) = $match;
|
|
287 |
return array(
|
|
288 |
'nick' => $nick,
|
|
289 |
'user' => $user,
|
|
290 |
'host' => $host,
|
|
291 |
'action' => $action,
|
|
292 |
'target' => $target,
|
|
293 |
'message' => trim($message)
|
|
294 |
);
|
|
295 |
}
|
|
296 |
|
|
297 |
/**
|
|
298 |
* Joins a channel, and returns a Request_IRC_Channel object.
|
|
299 |
* @param string Channel name (remember # prefix)
|
|
300 |
* @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object
|
|
301 |
* @return object
|
|
302 |
*/
|
|
303 |
|
|
304 |
function join($channel, $handler)
|
|
305 |
{
|
|
306 |
$chan = new Request_IRC_Channel(strtolower($channel), $handler, $this);
|
|
307 |
$this->channels[strtolower($channel)] = $chan;
|
|
308 |
return $chan;
|
|
309 |
}
|
|
310 |
|
|
311 |
/**
|
|
312 |
* Closes the connection and quits.
|
|
313 |
* @param string Optional part message
|
|
314 |
*/
|
|
315 |
|
|
316 |
public function close($partmsg = false)
|
|
317 |
{
|
|
318 |
if ( $this->quitted )
|
|
319 |
return true;
|
|
320 |
|
|
321 |
$this->quitted = true;
|
|
322 |
// Part all channels
|
|
323 |
if ( !$partmsg )
|
|
324 |
$partmsg = 'IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION;
|
|
325 |
|
|
326 |
foreach ( $this->channels as $channel )
|
|
327 |
{
|
|
328 |
$channel->part($partmsg);
|
|
329 |
}
|
|
330 |
|
|
331 |
$this->put("QUIT\r\n");
|
|
332 |
|
|
333 |
while ( $msg = $this->get() )
|
|
334 |
{
|
|
335 |
// Do nothing.
|
|
336 |
}
|
|
337 |
|
|
338 |
fclose($this->sock);
|
|
339 |
}
|
|
340 |
|
|
341 |
}
|
|
342 |
|
|
343 |
/**
|
|
344 |
* Wrapper for channels.
|
|
345 |
*/
|
|
346 |
|
|
347 |
class Request_IRC_Channel extends Request_IRC
|
|
348 |
{
|
|
349 |
|
|
350 |
/**
|
|
351 |
* The name of the channel
|
|
352 |
* @var string
|
|
353 |
*/
|
|
354 |
|
|
355 |
private $channel_name = '';
|
|
356 |
|
|
357 |
/**
|
|
358 |
* The event handler function.
|
|
359 |
* @var string
|
|
360 |
*/
|
|
361 |
|
|
362 |
private $handler = '';
|
|
363 |
|
|
364 |
/**
|
|
365 |
* The parent connection.
|
|
366 |
* @var object
|
|
367 |
*/
|
|
368 |
|
|
369 |
public $parent = false;
|
|
370 |
|
|
371 |
/**
|
|
372 |
* Whether the channel has been parted or not, used to kill the destructor.
|
|
373 |
* @var bool
|
|
374 |
*/
|
|
375 |
|
|
376 |
protected $parted = false;
|
|
377 |
|
|
378 |
/**
|
|
379 |
* Constructor.
|
|
380 |
* @param string Channel name
|
|
381 |
* @param string Handler function
|
|
382 |
* @param object IRC connection (Request_IRC object)
|
|
383 |
*/
|
|
384 |
|
|
385 |
function __construct($channel, $handler, $parent)
|
|
386 |
{
|
|
387 |
$this->parent = $parent;
|
|
388 |
$this->parent->put("JOIN $channel\r\n");
|
8
|
389 |
// stream_set_timeout($this->parent->sock, 3);
|
|
390 |
// while ( $msg = $this->parent->get() )
|
|
391 |
// {
|
|
392 |
// // Do nothing
|
|
393 |
// }
|
0
|
394 |
$this->channel_name = $channel;
|
|
395 |
$this->handler = $handler;
|
8
|
396 |
eval(eb_fetch_hook('event_self_join'));
|
0
|
397 |
}
|
|
398 |
|
|
399 |
/**
|
|
400 |
* Returns the channel name
|
|
401 |
* @return string
|
|
402 |
*/
|
|
403 |
|
|
404 |
function get_channel_name()
|
|
405 |
{
|
|
406 |
return $this->channel_name;
|
|
407 |
}
|
|
408 |
|
|
409 |
/**
|
|
410 |
* Returns the handler function
|
|
411 |
* @return string
|
|
412 |
*/
|
|
413 |
|
|
414 |
function get_handler()
|
|
415 |
{
|
|
416 |
return $this->handler;
|
|
417 |
}
|
|
418 |
|
|
419 |
/**
|
|
420 |
* Sends a message.
|
|
421 |
* @param string message
|
|
422 |
* @param bool If true, will fire a message event when the message is sent.
|
|
423 |
*/
|
|
424 |
|
|
425 |
function msg($msg, $fire_event = false)
|
|
426 |
{
|
|
427 |
$this->parent->privmsg($this->channel_name, $msg);
|
|
428 |
if ( $fire_event )
|
|
429 |
{
|
|
430 |
$func = $this->get_handler();
|
|
431 |
// format: :nick!user@host PRIVMSG #channel :msg.
|
|
432 |
$lines = explode("\n", $msg);
|
|
433 |
foreach ( $lines as $line )
|
|
434 |
{
|
|
435 |
$data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line";
|
|
436 |
$result = @call_user_func($func, $data, $this);
|
|
437 |
stream_set_timeout($this->parent->sock, 0xFFFFFFFE);
|
|
438 |
}
|
|
439 |
}
|
|
440 |
}
|
|
441 |
|
|
442 |
/**
|
|
443 |
* Destructor, automatically parts the channel.
|
|
444 |
*/
|
|
445 |
|
|
446 |
function __destruct()
|
|
447 |
{
|
|
448 |
if ( !$this->parted )
|
|
449 |
$this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION);
|
|
450 |
}
|
|
451 |
|
|
452 |
/**
|
|
453 |
* Parts the channel.
|
|
454 |
* @param string Optional message
|
|
455 |
*/
|
|
456 |
|
|
457 |
function part($msg = '')
|
|
458 |
{
|
|
459 |
$this->parent->put("PART {$this->channel_name} :$msg\r\n");
|
|
460 |
$this->parted = true;
|
|
461 |
unset($this->parent->channels[$this->channel_name]);
|
|
462 |
}
|
|
463 |
|
|
464 |
}
|
|
465 |
|
|
466 |
?>
|