author | Dan |
Tue, 20 Jan 2009 22:08:17 -0500 | |
changeset 52 | a8f0e99883d1 |
parent 51 | 508400fc5282 |
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 |
||
38 | 135 |
$need_deghost = false; |
136 |
||
8 | 137 |
// wait for a mode +i or end of the motd |
138 |
while ( true ) |
|
0 | 139 |
{ |
8 | 140 |
$msg = $this->get(); |
141 |
if ( empty($msg) ) |
|
142 |
continue; |
|
38 | 143 |
if ( strstr($msg, "433") ) |
144 |
{ |
|
145 |
// nick already in use - try to ghost |
|
146 |
$this->change_nick("{$nick}|de-ghosting"); |
|
147 |
$need_deghost = true; |
|
148 |
} |
|
8 | 149 |
if ( ( strstr($msg, 'MODE') && strstr($msg, '+i') ) || strstr(strtolower($msg), 'end of /motd') ) |
0 | 150 |
{ |
8 | 151 |
break; |
0 | 152 |
} |
8 | 153 |
if ( preg_match('/^PING :(.+?)$/', $msg, $match) ) |
154 |
{ |
|
155 |
$this->put("PONG :{$match[1]}\r\n"); |
|
156 |
} |
|
0 | 157 |
} |
158 |
||
38 | 159 |
if ( $need_deghost ) |
160 |
{ |
|
161 |
$this->privmsg('NickServ', "GHOST $nick $pass"); |
|
162 |
while ( $msg = $this->get(10) ) |
|
163 |
{ |
|
164 |
if ( stristr($msg, 'nickserv') && stristr($msg, 'ghost') ) |
|
165 |
break; |
|
166 |
} |
|
167 |
$this->change_nick($nick); |
|
168 |
} |
|
169 |
||
8 | 170 |
// identify to nickserv |
30 | 171 |
if ( $pass ) |
172 |
$this->privmsg('NickServ', "IDENTIFY $pass"); |
|
8 | 173 |
|
0 | 174 |
$this->nick = $nick; |
175 |
$this->user = $username; |
|
30 | 176 |
$this->quitted = false; |
0 | 177 |
} |
178 |
||
179 |
/** |
|
180 |
* Writes some data to the socket, abstracted for debugging purposes. |
|
181 |
* @param string Message to send, this should include a CRLF. |
|
182 |
*/ |
|
183 |
||
184 |
public function put($message) |
|
185 |
{ |
|
186 |
if ( !$this->sock ) |
|
187 |
{ |
|
188 |
if ( defined('LIBIRC_DEBUG') ) |
|
189 |
echo ">>> WRITE FAILED: $message"; |
|
190 |
return false; |
|
191 |
} |
|
192 |
if ( defined('LIBIRC_DEBUG') ) |
|
193 |
echo ">>> $message"; |
|
194 |
fwrite($this->sock, $message); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* Reads from the socket... |
|
199 |
* @return string |
|
200 |
*/ |
|
201 |
||
30 | 202 |
public function get($timeout = 1) |
0 | 203 |
{ |
204 |
if ( !$this->sock ) |
|
205 |
{ |
|
206 |
if ( defined('LIBIRC_DEBUG') ) |
|
207 |
echo "<<< READ FAILED\n"; |
|
208 |
return false; |
|
209 |
} |
|
30 | 210 |
if ( ($c = stream_select($r = array($this->sock), $w = null, $e = null, $timeout)) !== false ) |
211 |
{ |
|
212 |
if ( $c > 0 ) |
|
213 |
{ |
|
214 |
$out = fgets($this->sock, 4096); |
|
215 |
if ( defined('LIBIRC_DEBUG') ) |
|
216 |
if ( !empty($out) ) |
|
217 |
echo "<<< $out"; |
|
218 |
return $out; |
|
219 |
} |
|
220 |
} |
|
221 |
return false; |
|
0 | 222 |
} |
223 |
||
224 |
/** |
|
225 |
* Sends a message to a nick or channel. |
|
226 |
* @param string Nick or channel |
|
227 |
* @param string Message |
|
228 |
*/ |
|
229 |
||
230 |
public function privmsg($nick, $message) |
|
231 |
{ |
|
232 |
$message = str_replace("\r\n", "\n", $message); |
|
233 |
$message = explode("\n", $message); |
|
234 |
foreach ( $message as $line ) |
|
235 |
{ |
|
32 | 236 |
$line = $this->filter_message($line); |
0 | 237 |
$this->put("PRIVMSG $nick :$line\r\n"); |
238 |
} |
|
239 |
} |
|
240 |
||
241 |
/** |
|
40 | 242 |
* Sends a notice. |
243 |
* @param string Nick or channel |
|
244 |
* @param string Message |
|
245 |
*/ |
|
246 |
||
247 |
public function notice($nick, $message) |
|
248 |
{ |
|
249 |
$message = str_replace("\r\n", "\n", $message); |
|
250 |
$message = explode("\n", $message); |
|
251 |
foreach ( $message as $line ) |
|
252 |
{ |
|
253 |
$line = $this->filter_message($line); |
|
254 |
$this->put("NOTICE $nick :$line\r\n"); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
/** |
|
51
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
259 |
* Returns WHOIS information about a nick. |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
260 |
* @param string Nick |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
261 |
* @return array |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
262 |
*/ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
263 |
|
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
264 |
public function whois($nick) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
265 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
266 |
static $cache = array(); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
267 |
if ( isset($cache[$nick]) ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
268 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
269 |
if ( $cache[$nick]['time'] + 20 >= time() ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
270 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
271 |
return $cache[$nick]; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
272 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
273 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
274 |
$this->put("WHOIS $nick\r\n"); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
275 |
$lines = array(); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
276 |
$return = array( |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
277 |
'identified' => false, |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
278 |
'time' => time() |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
279 |
); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
280 |
while ( $line = $this->get(3) ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
281 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
282 |
$lines[] = $line; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
283 |
list(, $code) = explode(' ', $line); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
284 |
$code = intval($code); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
285 |
switch($code) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
286 |
{ |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
287 |
case 401: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
288 |
return false; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
289 |
case 311: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
290 |
list(, , , $return['nick'], $return['user'], $return['host']) = explode(' ', $line); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
291 |
$return['ircname'] = substr($line, strpos(substr($line, 1), ':') + 2); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
292 |
break; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
293 |
case 319: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
294 |
$return['channels'] = explode(' ', substr($line, strpos(substr($line, 1), ':') + 2)); |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
295 |
break; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
296 |
case 307: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
297 |
case 320: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
298 |
if ( strstr($line, 'identified') ) |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
299 |
$return['identified'] = true; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
300 |
break; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
301 |
case 318: |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
302 |
$cache[$nick] = $return; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
303 |
return $return; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
304 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
305 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
306 |
$cache[$nick] = $return; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
307 |
return $return; |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
308 |
} |
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
309 |
|
508400fc5282
Major change to permissions backend - performs whois check (only supported blitzed and freenode right now) and advanced permissions supported.
Dan
parents:
40
diff
changeset
|
310 |
/** |
33 | 311 |
* 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 | 312 |
* @param string Text to filter |
313 |
* @return string |
|
314 |
*/ |
|
315 |
||
316 |
function filter_message($text) |
|
317 |
{ |
|
318 |
$text = preg_replace('#<b>(.*?)</b>#is', "\x02$1\x02", $text); |
|
319 |
if ( preg_match('#^/me #i', $text) ) |
|
320 |
{ |
|
321 |
$text = "\x01ACTION " . preg_replace('#^/me #i', '', $text) . "\x01"; |
|
322 |
} |
|
323 |
$supportedcolors = array('white', 'black', 'navy', 'green', 'red', 'maroon', 'purple', 'orange', 'yellow', 'lime', 'teal', 'cyan', 'blue', 'pink', 'grey', 'silver'); |
|
324 |
$colors = implode('|', $supportedcolors); |
|
325 |
$supportedcolors = array_flip($supportedcolors); |
|
326 |
preg_match_all("#<((?:$colors)(?::(?:$colors))?)>(.*?)</\\1>#is", $text, $matches); |
|
327 |
foreach ( $matches[0] as $i => $match ) |
|
328 |
{ |
|
329 |
preg_match("#<(?:($colors)(?::($colors))?)>#i", $match, $colordata); |
|
330 |
$fgcolor = $supportedcolors[$colordata[1]]; |
|
331 |
$bgcolor = $colordata[1] ? $supportedcolors[$colordata[2]] : ''; |
|
332 |
$fgcolor = ( $fgcolor < 10 ) ? "0$fgcolor" : "$fgcolor"; |
|
333 |
if ( is_int($bgcolor) ) |
|
334 |
$bgcolor = ( $bgcolor < 10 ) ? ",0$bgcolor" : ",$bgcolor"; |
|
335 |
$text = str_replace($match, "\x03{$fgcolor}{$bgcolor}{$matches[2][$i]}\x03", $text); |
|
336 |
} |
|
337 |
||
338 |
return $text; |
|
339 |
} |
|
340 |
||
341 |
/** |
|
0 | 342 |
* The main event loop. |
343 |
*/ |
|
344 |
||
345 |
public function event_loop() |
|
346 |
{ |
|
30 | 347 |
$timeout = 180; |
348 |
$timeout_warn = false; |
|
349 |
while ( true ) |
|
0 | 350 |
{ |
30 | 351 |
$data = $this->get($timeout); |
352 |
$data_trim = $data ? trim($data) : false; |
|
353 |
if ( empty($data_trim) ) |
|
354 |
{ |
|
355 |
if ( $timeout_warn ) |
|
356 |
{ |
|
357 |
// timeout confirmed :-/ |
|
358 |
if ( $this->timeout_error_handler ) |
|
359 |
{ |
|
360 |
$result = @call_user_func($this->timeout_error_handler, $this); |
|
361 |
if ( $result == 'BREAK' ) |
|
362 |
break; |
|
363 |
} |
|
364 |
$timeout = 180; |
|
365 |
$timeout_warn = false; |
|
366 |
continue; |
|
367 |
} |
|
368 |
// timeout suspected |
|
369 |
if ( $this->timeout_warning_handler ) |
|
370 |
{ |
|
371 |
$result = @call_user_func($this->timeout_warning_handler, $this); |
|
372 |
if ( $result == 'BREAK' ) |
|
373 |
break; |
|
374 |
} |
|
375 |
// ping the server |
|
376 |
$this->put("PING :{$this->nick}\r\n"); |
|
377 |
// set timeout lower to make reconnecting work as fast as possible |
|
378 |
$timeout = 10; |
|
379 |
$timeout_warn = true; |
|
380 |
continue; |
|
381 |
} |
|
0 | 382 |
$match = self::parse_message($data_trim); |
383 |
if ( preg_match('/^PING :(.+?)$/', $data_trim, $pmatch) ) |
|
384 |
{ |
|
385 |
$this->put("PONG :{$pmatch[1]}\r\n"); |
|
8 | 386 |
eval(eb_fetch_hook('event_ping')); |
0 | 387 |
} |
30 | 388 |
else if ( preg_match('/^:((?:[a-z0-9-]+\.)*[a-z0-9-]+) PONG \\1 :' . preg_quote($this->nick) .'/', $data_trim) ) |
389 |
{ |
|
390 |
$timeout = 180; |
|
391 |
$timeout_warn = false; |
|
392 |
} |
|
0 | 393 |
else if ( $match ) |
394 |
{ |
|
38 | 395 |
if ( $match['action'] == 'KILL' ) |
396 |
{ |
|
397 |
// be ethical here and die |
|
398 |
return 'killed'; |
|
399 |
} |
|
0 | 400 |
// Received PRIVMSG or other mainstream action |
8 | 401 |
if ( $match['action'] == 'JOIN' || $match['action'] == 'PART' ) |
0 | 402 |
$channel =& $match['message']; |
403 |
else |
|
404 |
$channel =& $match['target']; |
|
405 |
||
406 |
if ( !preg_match('/^[#!&\+]/', $channel) ) |
|
407 |
{ |
|
408 |
// Private message from user |
|
409 |
$result = $this->handle_privmsg($data); |
|
8 | 410 |
@stream_set_timeout($this->sock, 0xFFFFFFFE); |
0 | 411 |
} |
412 |
else if ( isset($this->channels[strtolower($channel)]) ) |
|
413 |
{ |
|
414 |
// Message into channel |
|
415 |
$chan =& $this->channels[strtolower($channel)]; |
|
416 |
$func = $chan->get_handler(); |
|
417 |
$result = @call_user_func($func, $data, $chan); |
|
8 | 418 |
@stream_set_timeout($this->sock, 0xFFFFFFFE); |
0 | 419 |
} |
420 |
if ( $result == 'BREAK' ) |
|
421 |
{ |
|
422 |
break; |
|
423 |
} |
|
424 |
} |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
/** |
|
429 |
* Processor for when a private message is received. |
|
430 |
* @access private |
|
431 |
*/ |
|
432 |
||
433 |
private function handle_privmsg($message) |
|
434 |
{ |
|
435 |
$message = self::parse_message($message); |
|
21 | 436 |
$message['message'] = preg_replace('/^(.+?):/', '', $message['message']); |
0 | 437 |
$ph = $this->privmsg_handler; |
438 |
if ( @function_exists($ph) ) |
|
439 |
return @call_user_func($ph, $message); |
|
440 |
} |
|
441 |
||
442 |
/** |
|
443 |
* Changes the function called upon receipt of a private message. |
|
444 |
* @param string Function to call, will be passed a parsed message. |
|
445 |
*/ |
|
446 |
||
447 |
function set_privmsg_handler($func) |
|
448 |
{ |
|
449 |
if ( !function_exists($func) ) |
|
450 |
return false; |
|
451 |
$this->privmsg_handler = $func; |
|
452 |
return true; |
|
453 |
} |
|
454 |
||
455 |
/** |
|
30 | 456 |
* Changes the functions called when IRC connection timeouts occur. |
457 |
* @param string Function to call when a warning (no traffic within 3 minutes) occurs. If false, nothing will be called. |
|
458 |
* @param string Function to call if a ping timeout occurs. If false, nothing will be called. |
|
459 |
*/ |
|
460 |
||
461 |
function set_timeout_handlers($warn_func, $error_func) |
|
462 |
{ |
|
463 |
$this->timeout_warning_handler = false; |
|
464 |
$this->timeout_error_handler = false; |
|
465 |
if ( function_exists($warn_func) ) |
|
466 |
{ |
|
467 |
$this->timeout_warning_handler = $warn_func; |
|
468 |
} |
|
469 |
if ( function_exists($error_func) ) |
|
470 |
{ |
|
471 |
$this->timeout_error_handler = $error_func; |
|
472 |
} |
|
473 |
return true; |
|
474 |
} |
|
475 |
||
476 |
/** |
|
0 | 477 |
* Parses a message line. |
478 |
* @param string Message text |
|
479 |
* @return array Associative with keys: nick, user, host, action, target, message |
|
480 |
*/ |
|
481 |
||
482 |
public static function parse_message($message) |
|
483 |
{ |
|
484 |
// Indices: 12 3 4 5 67 8 |
|
485 |
$mc = preg_match('/^:(([^ ]+)!([^ ]+)@([^ ]+)) ([A-Z]+) (([#!&\+]*[A-z0-9_-]+) )?:?(.*?)$/', $message, $match); |
|
486 |
if ( !$mc ) |
|
487 |
{ |
|
488 |
return false; |
|
489 |
} |
|
490 |
// Indices: 0 1 2 3 4 5 6 7 8 |
|
491 |
list( , , $nick, $user, $host, $action, , $target, $message) = $match; |
|
492 |
return array( |
|
493 |
'nick' => $nick, |
|
494 |
'user' => $user, |
|
495 |
'host' => $host, |
|
496 |
'action' => $action, |
|
497 |
'target' => $target, |
|
498 |
'message' => trim($message) |
|
499 |
); |
|
500 |
} |
|
501 |
||
502 |
/** |
|
503 |
* Joins a channel, and returns a Request_IRC_Channel object. |
|
504 |
* @param string Channel name (remember # prefix) |
|
505 |
* @param string Event handler function, will be called with param 0 = socket output and param 1 = channel object |
|
506 |
* @return object |
|
507 |
*/ |
|
508 |
||
509 |
function join($channel, $handler) |
|
510 |
{ |
|
511 |
$chan = new Request_IRC_Channel(strtolower($channel), $handler, $this); |
|
512 |
$this->channels[strtolower($channel)] = $chan; |
|
513 |
return $chan; |
|
514 |
} |
|
515 |
||
516 |
/** |
|
30 | 517 |
* Changes the current nick. |
518 |
* @param string New nick. |
|
519 |
* @param string Password to authenticate to NickServ if needed. |
|
520 |
*/ |
|
521 |
||
522 |
public function change_nick($nick, $pass = false) |
|
523 |
{ |
|
524 |
$this->put("NICK $nick\r\n"); |
|
525 |
$this->nick = $nick; |
|
526 |
if ( $pass ) |
|
527 |
{ |
|
528 |
while ( $data = $this->get(3) ) |
|
529 |
{ |
|
530 |
if ( strstr($data, 'NickServ') ) |
|
531 |
{ |
|
532 |
$this->privmsg('NickServ', "IDENTIFY $pass"); |
|
533 |
break; |
|
534 |
} |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
||
539 |
/** |
|
0 | 540 |
* Closes the connection and quits. |
541 |
* @param string Optional part message |
|
542 |
*/ |
|
543 |
||
544 |
public function close($partmsg = false) |
|
545 |
{ |
|
546 |
if ( $this->quitted ) |
|
547 |
return true; |
|
548 |
||
549 |
$this->quitted = true; |
|
550 |
// Part all channels |
|
551 |
if ( !$partmsg ) |
|
31
d75124700259
Timeout recovery should avoid getting the bot throttled now :)
Dan
parents:
30
diff
changeset
|
552 |
$partmsg = 'Closing connection (no reason given)'; |
0 | 553 |
|
554 |
foreach ( $this->channels as $channel ) |
|
555 |
{ |
|
556 |
$channel->part($partmsg); |
|
557 |
} |
|
558 |
||
559 |
$this->put("QUIT\r\n"); |
|
560 |
||
30 | 561 |
while ( $msg = $this->get(1) ) |
0 | 562 |
{ |
563 |
// Do nothing. |
|
564 |
} |
|
565 |
||
566 |
fclose($this->sock); |
|
567 |
} |
|
568 |
||
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Wrapper for channels. |
|
573 |
*/ |
|
574 |
||
575 |
class Request_IRC_Channel extends Request_IRC |
|
576 |
{ |
|
577 |
||
578 |
/** |
|
579 |
* The name of the channel |
|
580 |
* @var string |
|
581 |
*/ |
|
582 |
||
583 |
private $channel_name = ''; |
|
584 |
||
585 |
/** |
|
586 |
* The event handler function. |
|
587 |
* @var string |
|
588 |
*/ |
|
589 |
||
590 |
private $handler = ''; |
|
591 |
||
592 |
/** |
|
593 |
* The parent connection. |
|
594 |
* @var object |
|
595 |
*/ |
|
596 |
||
597 |
public $parent = false; |
|
598 |
||
599 |
/** |
|
600 |
* Whether the channel has been parted or not, used to kill the destructor. |
|
601 |
* @var bool |
|
602 |
*/ |
|
603 |
||
604 |
protected $parted = false; |
|
605 |
||
606 |
/** |
|
607 |
* Constructor. |
|
608 |
* @param string Channel name |
|
609 |
* @param string Handler function |
|
610 |
* @param object IRC connection (Request_IRC object) |
|
611 |
*/ |
|
612 |
||
613 |
function __construct($channel, $handler, $parent) |
|
614 |
{ |
|
615 |
$this->parent = $parent; |
|
616 |
$this->parent->put("JOIN $channel\r\n"); |
|
8 | 617 |
// stream_set_timeout($this->parent->sock, 3); |
618 |
// while ( $msg = $this->parent->get() ) |
|
619 |
// { |
|
620 |
// // Do nothing |
|
621 |
// } |
|
0 | 622 |
$this->channel_name = $channel; |
623 |
$this->handler = $handler; |
|
8 | 624 |
eval(eb_fetch_hook('event_self_join')); |
0 | 625 |
} |
626 |
||
627 |
/** |
|
628 |
* Returns the channel name |
|
629 |
* @return string |
|
630 |
*/ |
|
631 |
||
632 |
function get_channel_name() |
|
633 |
{ |
|
634 |
return $this->channel_name; |
|
635 |
} |
|
636 |
||
637 |
/** |
|
638 |
* Returns the handler function |
|
639 |
* @return string |
|
640 |
*/ |
|
641 |
||
642 |
function get_handler() |
|
643 |
{ |
|
644 |
return $this->handler; |
|
645 |
} |
|
646 |
||
647 |
/** |
|
648 |
* Sends a message. |
|
649 |
* @param string message |
|
650 |
* @param bool If true, will fire a message event when the message is sent. |
|
651 |
*/ |
|
652 |
||
653 |
function msg($msg, $fire_event = false) |
|
654 |
{ |
|
655 |
$this->parent->privmsg($this->channel_name, $msg); |
|
656 |
if ( $fire_event ) |
|
657 |
{ |
|
658 |
$func = $this->get_handler(); |
|
659 |
// format: :nick!user@host PRIVMSG #channel :msg. |
|
660 |
$lines = explode("\n", $msg); |
|
661 |
foreach ( $lines as $line ) |
|
662 |
{ |
|
663 |
$data = ":{$this->parent->nick}!{$this->parent->user}@localhost PRIVMSG {$this->channel_name} :$line"; |
|
664 |
$result = @call_user_func($func, $data, $this); |
|
665 |
stream_set_timeout($this->parent->sock, 0xFFFFFFFE); |
|
666 |
} |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
/** |
|
671 |
* Destructor, automatically parts the channel. |
|
672 |
*/ |
|
673 |
||
674 |
function __destruct() |
|
675 |
{ |
|
676 |
if ( !$this->parted ) |
|
677 |
$this->part('IRC bot powered by PHP/' . PHP_VERSION . ' libirc/' . REQUEST_IRC_VERSION); |
|
678 |
} |
|
679 |
||
680 |
/** |
|
681 |
* Parts the channel. |
|
682 |
* @param string Optional message |
|
683 |
*/ |
|
684 |
||
685 |
function part($msg = '') |
|
686 |
{ |
|
687 |
$this->parent->put("PART {$this->channel_name} :$msg\r\n"); |
|
688 |
$this->parted = true; |
|
689 |
unset($this->parent->channels[$this->channel_name]); |
|
690 |
} |
|
691 |
||
692 |
} |
|
693 |
||
694 |
?> |