Snippet Name: IRC Bot skeleton code
Description: Simple skeleton code for an IRC bot that connects to an IRC server you specify and idles.
Comment: (none)
Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009
|
<?PHP
CLASS Client{
VAR $server;
FUNCTION server($dstaddr=NULL){
IF (!$dstaddr)
{
RETURN $this->server;
}
$this->server = $dstaddr;
}
VAR $port;
FUNCTION port($dstport=NULL){
IF (!$dstport)
{
RETURN $this->port;
}
$this->port = $dstport;
}
VAR $nick;
FUNCTION nick($alias=NULL){
IF (!$alias)
{
RETURN $this->nick;
}
$this->nick = $alias;
}
VAR $ident;
FUNCTION ident($user=NULL){
IF (!$user)
{
RETURN $this->ident;
}
$this->ident = $user;
}
VAR $gecos;
FUNCTION gecos($name=NULL){
IF (!$name)
{
RETURN $this->gecos;
}
$this->gecos = $name;
}
VAR $fd;
VAR $BUFFER_SIZE = 512;
FUNCTION run(){
$canonical = GETHOSTBYNAME($this->server());
IF ($canonical == $this->server()){
RETURN;
}
$this->fd = SOCKET_CREATE(AF_INET, SOCK_STREAM, SOL_TCP);
IF (!$this->fd){
RETURN;
}
$err = SOCKET_CONNECT($this->fd, $this->server(), $this->port());
IF (!$err){
RETURN;
}
SOCKET_WRITE($this->fd, "NICK " . $this->nick() . "\r\n");
SOCKET_WRITE($this->fd, "USER " . $this->ident() . " \"\" \"\" :" . $this->gecos() . "\r\n");
WHILE (($in = SOCKET_READ($this->fd, $this->BUFFER_SIZE))){
IF (PREG_MATCH("/^PING (:[^ ]+)$/i", $in, $regex)){
SOCKET_WRITE($this->fd, "PONG " . $regex[0] . "\r\n");
}
}
}
}
?>
<?PHP
$client = NEW Client();
$client->server("irc.kingskrown.com");
$client->port(6667);
$client->nick("php");
$client->ident("svs");
$client->gecos("php");
$client->run();
?> |