PHP Classes

File: class.boboirc.php

Recommend this page to a friend!
  Classes of JImmy Bo   Bobo PHP IRC Class   class.boboirc.php   Download  
File: class.boboirc.php
Role: Class source
Content type: text/plain
Description: The class file for Bobo's PHP IRC Class
Class: Bobo PHP IRC Class
Client to access IRC servers
Author: By
Last change: fixed up commenting
Date: 10 years ago
Size: 5,014 bytes
 

Contents

Class file image Download
<?php

error_reporting
(E_ALL);

/* Please leave this comment in at the top of the script if you use this script
 * in it's entirety. That is my only request. Other then that have fun.
 *
 * Now you too can create irc bots too 8 )
 * or if you have any cool optimizations for it
 *
 * example usage:
 * $ircbot = new boboirc($nick, $ident, $realname, $host, $port);
 * $ircbot->joinChan('#phphelp'); // usually where u can find great help on undernet. #php is useless
 * $ircbot->loop(); // begin loop (so program doesn't die right away)
 *
 * to add new commands to your new bobot, scroll down to the following function
 * parse_command($command, $msg)
 * and follow the directions. It's easy. I'm easy.
 *
 * Also if you're looking for help from me directly you can usually find me
 * slumming it on #phphelp on the undernet irc network.
 * my nick on there is usually _P_H_P_
 *
 * Check out more useful classes:
 * http://www.phpclasses.org/browse/author/144301.html
 */
 

class boboirc
{
    private
$fp, $readbuffer, $line, $mcommands;
    public
$nick, $ident, $realname, $host, $port;
   
    function
boboirc($nick, $ident, $realname, $host, $port)
    {
       
$this->nick = $nick;
       
$this->ident = $ident;
       
$this->realname = $realname;
       
$this->host = $host;
       
$this->port = $port;

       
$this->fp = fsockopen($host, $port, $erno, $errstr, 30);
        if(!
$this->fp) die("Could not connect\r\n");
       
   
fwrite($this->fp, "NICK ".$nick."\r\n");
   
fwrite($this->fp, "USER ".$ident." ".$host." bla :".$realname."\r\n");

       
$this->flush();

    }
   
    function
loop()
    {
       
// now for program loop //
       
while (!feof($this->fp))
        {
           
$this->line = fgets($this->fp, 256); // wait for a message

           
if($this->is_ping($this->line)) $this->pong();
                       
            if(
strstr($this->line,"PRIVMSG"))
            {
                echo
"PRIVMSG... \r\n";
               
// incoming private message //
               
$msg = $this->msgToArray($this->line);
                               
               
// is this a command?
               
if($command = $this->get_command($msg['msg']))
                {
                    echo
"processing command ($command)... \r\n";
                   
// erase command from message array // array('from, 'chan', 'msg'); //
                   
$msg['msg'] = trim(str_replace($command,'',$msg['msg']));
                    echo
"parsing command ($command)... \r\n";
                   
$this->parse_command($command, $msg);
                }
            }
           
           
$this->line = "";
           
$this->flush();
           
$this->wait(); // time to next cycle
       
}

    }
   
   
// outgoing //
   
function out($msg) // raw message
   
{
        if(@empty(
$msg)) return false;
        if(!
strstr($msg, "\n")) $msg .= "\n";

       
fwrite($this->fp, $msg);
        return
true;
    }
   
    function
setNick($nick) { $this->out("NICK ".$nick."\r\n"); $this->nick = $nick; }
    function
joinChan($channel) { $this->out("JOIN :".$channel."\r\n"); }
    function
quitChan($channel) { $this->out("PART :".$channel."\r\n"); }

    function
listChans() { $this->out("LIST\r\n"); }
    function
getTopic($channel) { $this->out("TOPIC ".$channel."\r\n"); }
   
    function
msg($target, $msg) { $this->out("PRIVMSG $target :$msg\r\n"); }
    function
msgChan($channel, $msg) { $this->msg($channel, $msg); }
    function
msgUser($user, $msg) { $this->msg($user, $msg); }
   
    function
pong() { $this->out("PONG :".$this->host."\r\n"); }
    function
quit($msg="") { $this->out("QUIT :$msg\r\n"); }
   
   
// incoming processing //
   
function is_ping($line) { if(strstr($line, 'PING')) return true; }
    function
is_msg($line) { if(strstr($line, 'PRIVMSG')) return true; }

    function
msgToArray($line) // array('from, 'chan', 'msg');
   
{
       
$array = explode(":",$line);
               
       
$from = explode("!",$array[1]);
       
$from = trim($from[0]);
       
       
$fromchan = explode("#",$array[1]);
       
$fromchan = "#".trim($fromchan[1]);
       
       
$string = $array[2];
       
$string = trim($string);
       
       
$msg = array('from'=>$from, 'chan'=>$fromchan, 'msg'=>$string);
       
        return
$msg;
    }
   
   
// system
   
function flush() { @ob_flush; @flush(); }
    function
wait() { usleep(250000); }
    function
get_command($string)
    {
        if(!
strstr($string,"!")) return false;
        if(!
strstr($string, " "))
           
$command = $string;
        else
        {
           
$command = explode(" ", $string,2);
           
$command = $command[0];
        }
        return
$command;
    }
   
   
// misc useful functions //
   
function rem_xs_whitespace($string){ $string = trim(preg_replace('/\s+/', ' ', $string)); return $string; }



   
// command parser //
    // TELL THE PARSER WHAT COMMANDS ARE AVAILABLE
    // add commands in the next section below this function
   
function parse_command($command, $msg)
    {
       
// $command = "!command"; $msg = array('from, 'chan', 'msg')
       
switch($command)
        {
            case
'!sayrand': $this->command_sayrand($msg); break;
        }
    }
   
   
// now for commands //
    // ADD YOUR COMMANDS HERE //
   
function command_sayrand($msg)
    {
       
$number = rand(1,100);
       
$this->msgChan($msg['chan'], "random number:".$number);
        echo
"saying magic number...\r\n";
    }

}

?>