Created
March 24, 2012 15:49
-
-
Save hadashiA/2184485 to your computer and use it in GitHub Desktop.
IRC protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class IRCNotifierError extends Exception {} | |
class IRCNotifier { | |
private $_socket; | |
function __construct($url, $options) { | |
$url_info = parse_url($url); | |
if ($url_info === false) { | |
throw new IRCNotifierError('invalid url: ' . $url); | |
} | |
$scheme = $url_info['scheme']; | |
$host = $url_info['host']; | |
$port = $url_info['port']; | |
if (!$port) $port = 6667; | |
$this->_socket = fsockopen($scheme.'://'.$host, $port, $errno, $errstr); | |
if (!$this->_socket) { | |
$error = "$errstr ($errno)"; | |
$msg = "coldn't connect to " . $url . | |
' reason: "' . $errstr . ' (' . $errno . ')"'; | |
$this->_socket = null; | |
throw new IRCNotifierError($msg); | |
} | |
if (isset($options['password'])) { | |
$this->send('PASS ' . $options['password']); | |
} | |
$nickname = $options['nickname']; | |
if (!$nickname) $nickname = 'notifier'; | |
$hostname = $options['hostname']; | |
if (!$hostname) $hostname = 'notifier.local'; | |
$servername = $options['servername']; | |
if (!$servername) $servername = $hostname; | |
if (!$servername) $servername = 'notifier.local'; | |
$this->send('NICK ' . $nickname); | |
$this->send("USER $nickname $hostname $servername :$nickname"); | |
$this->send(''); | |
fflush($this->_socket); | |
usleep(1000 * 1000); | |
$logged_in = false; | |
$buffer = ''; | |
while (($line = fgets($this->_socket)) !== false) { | |
$buffer .= $line; | |
if (strpos($line, 'Welcome to the EFnet Internet Relay Chat Network') !== false) { | |
$logged_in = true; | |
} else if (strpos($line, 'ERROR')) { | |
throw new IRCNotifierError("login faild. reason:\n" . $buffer); | |
} | |
if (preg_match('/:\+i\s*$/', $line)) { | |
break; | |
} | |
} | |
if (!$logged_in) { | |
throw new IRCNotifierError("login faild. reason:\n" . $buffer); | |
} | |
} | |
function __destruct() { | |
if ($this->_socket) { | |
$this->send("QUIT"); | |
@fflush($this->_socket); | |
@fclose($this->_socket); | |
} | |
} | |
function join($channel) { | |
$this->send("JOIN " . $channel); | |
fflush($this->_socket); | |
while ((($line = fgets($this->_socket)) !== false)) { | |
if (preg_match('/\s+JOIN\s+:/', $line)) { | |
break; | |
} | |
} | |
} | |
function names() { | |
$this->send('NAMES'); | |
fflush($this->_socket); | |
usleep(100 * 1000); | |
$result = array(); | |
while ((($line = fgets($this->_socket)) !== false)) { | |
if (preg_match('/End of \/NAMES List./i', $line)) { | |
break; | |
} | |
if (preg_match('/:.+?:(.+)/', $line, $m)) { | |
$result = array_merge($result, explode(' ', $m[1])); | |
} | |
} | |
return array_unique($result); | |
} | |
function privmsg($nickname, $message) { | |
$this->send("PRIVMSG $nickname $message"); | |
fflush($this->_socket); | |
} | |
// private | |
private function send($command) { | |
if (!$this->_socket) { | |
return false; | |
} | |
$result = fwrite($this->_socket, $command . "\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment