PHP IRC bot

By supafreky on Jan 24, 2009

Connects to an IRC server you specify and idles. Basically this is a frame for an IRC bot.

<?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();
?>

Comments

Sign in to comment.
edgy   -  May 04, 2010

@Scarecrow kill the process

 Respond  
Matt5150   -  Apr 20, 2010

Nice.
But how can I quit it?

 Respond  
^Neptune   -  Dec 03, 2009

Don't worry! The ping works absolutely fine. :)

I found out it was max_execution_time in my php.ini that was causing it to stop after 60 seconds. This works fine. :D

 Respond  
[Plornt]   -  Dec 03, 2009

Hmm ill have a look but im not great with regex so it may confuse me.

 Respond  
^Neptune   -  Dec 03, 2009

Of course this won't work in mIRC, it's PHP. :|

Okay, i've got this connected to a server and modified it according to this snippet so I can easily make it respond to commands: http://www.hawkee.com/snippet/6223/

However, the ping does not seem to be working. Can anyone help me on this? Code: http://pastebin.com/m44c64848

 Respond  
[jonathan]   -  Jun 15, 2009

how to use it in mirc script ?

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.