QuaridBot
Results 1 to 5 of 5

Thread: QuaridBot

  1. #1
    Developer Extraordinar
    Join Date
    Jul 2002
    Location
    Internets
    Posts
    571

    QuaridBot

    As some of your know I'm well on my way developing an IRC bot for the ANewEraIRC Network, anyways, here's what I have so far. (It uses the Net_SmartIRC PEAR class)

    The actual main program
    PHP Code:
    <?php
    ini_set
    ("max_execution_time","0");
    /**
     *  Quarid, a basic (at the moment) bot for op functions, and some "neat" features
     *  such as stats logging, and "last seen". Configuration in config.php
     *  Build using SmartIRC PEAR class
     *
     *  Author: Daniel J. Enman
     *  Creation Date: December 14th 2005
     */
    /**
     * Include what we need to.
     */
    require_once("SmartIRC.php");
    require_once(
    "QuaridConfig.php");
    class 
    Quarid extends QuaridConfig {
         
    /**
          *  The Net_SmartIRC object
          */
      
    var $irc;
      
      
    /**
       *  The debug level
       */
      
    var $debug;
      
      
    /**
       *    Constructors... yippie!
       */
       
       
    public function __construct() {
         
    /**
          * We're going to need the Net_SmartIRC class for some
          * fancy pantsin'.
          */
         
    $this->irc = new Net_SmartIRC();
         
    $botff "botf";
         
    $this->{$botff} = new botf();
       }
      
    /**
       * Let's start with some debugging
       */
      
    public function _setDebug() {
        
    $debug $this->bot['debuglev'];
        switch(
    $debug){
          case 
    0:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_NONE);
            break;
          case 
    1:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_NOTICE);
            break;
          case 
    2:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_SOCKET);
            break;
          case 
    3:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_TIMEHANDLER);
            break;
          case 
    4:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_USERSYNCING);
            break;
          case 
    5:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MODULES);
            break;
          case 
    6:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGETYPES);
            break;
          case 
    7:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGEPARSER);
            break;
          case 
    8:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGEHANDLER);
            break;
          case 
    9:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_IRCMESSAGES);
            break;
          case 
    10:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_CONNECTION);
            break;
          case 
    11:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_CHANNELSYNCING);
            break;
          case 
    12:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_ACTIONHANDLER);
            break;
          case 
    13:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_ALL);
            break;
          default:
            die(
    "Error, please set a value on debug between 0 and 13. See manual for more infromation.");
            break;
        }
        return 
    $debug;
      }
      public function 
    _startLog() {
        
    $logto $this->bot['logto'];
        switch(
    $logto){
          case 
    0:
            
    $this->irc->setLogdestination(SMARTIRC_FILE);
            
    $file "./log/".$this->bot['logfile'];
            if(!
    file_exists($file)){
              
    touch($file);
              
    chmod($file0777);
            }
            
    $logf fopen($file'a');
            if(!
    $logf){
                echo 
    "Could not open file ".$file.", or create log file. Does log/ exist, and is it writeable?\n";
             exit;
            }
            
    $this->irc->log(SMARTIRC_DEBUG_NOTICE"QuaridBot loaded.""Quarid""0");
            
    $this->irc->setLogFile($file);
            break;
          case 
    1:
            
    $this->irc->setLogDestination(SMARTIRC_STDOUT);
            break;
          case 
    2:
            
    $this->irc->setLogDestination(SMARTIRC_BROWSEROUT);
            break;
          case 
    3:
            
    $this->irc->setLogDestination(SMARTIRC_SYSLOG);
            break;
          case 
    4:
            
    $this->irc->setLogDestination(SMARTIRC_NONE);
          default:
            die(
    "Error, please set a value between 0 and 4. See the manual for more information");
        }
        return 
    $logto;
      }
      
    /**
       *    CTCP version
       */
      
    protected function _setVersion() {
        
    $this->irc->setCTCPVersion("QuaridBot 0.01");
      }
      
      public function 
    actions() {
        
    $this->irc->registerActionhandler(SMARTIRC_TYPE_JOIN'.*'$this->botf'onjoin_greeting');
      }
      
      public function 
    main() {
        
    /**
         * Set up some settings set in QuaridConfig.php
         */
        
    $this->_setDebug();
        
    $this->irc->setUseSockets($this->bot['sockets']);
        
    $this->irc->setBenchmark($this->bot['bmark']);
        
    $this->irc->setChannelSyncing($this->bot['chansync']);
        
    $this->_startLog(); // Let's start logging now!
        
    $this->irc->showBenchmark();
        
    /**
         * Do the botf stuff!
         */
        
    $this->irc->connect('irc.unerror.com'6667);
        
    $this->irc->login($this->bot['nickname'], $this->bot['realname'], 0$this->bot['username'], null);
        
    $this->irc->join($this->channels);
        
    $this->actions();
        
    print_r($this->botf);
        
    $this->irc->listen();
        
    $this->irc->disconnect();
      }
    }
    $Quarid = new Quarid();
    $Quarid->main();
    ?&
    gt
    The configuration:
    PHP Code:
    <?php
    /**
     * The configuration class for Quarid
     */
     
    class QuaridConfig {
      var 
    $db = array("host" =&gt"localhost",
                      
    "user" =&gt"root",
                      
    "pass" =&gt"AReallyStrongPassWord",
                      
    "dbse" =&gt"QuaridIRC",
                      
    "tblp" =&gt"quarid_");
      var 
    $bot = array("nickname" =&gt"Quarid",
                 
    "username" =&gt"quarid",
                 
    "realname" =&gt"Quarid Bot",
                 
    "servername" =&gt"ANewEraIRC",
                       
    "serveraddy" =&gt"unerror.anewerairc.net",
                 
    "serverport" =&gt6667,
                       
    "SSL" =&gtfalse,
                       
    "debuglev" =&gt13,
                       
    "sockets" =&gttrue,
                       
    "bmark" =&gttrue,
                       
    "chansync" =&gttrue,
                       
    "logfile" =&gt"Quarid.log",
                       
    "logto" =&gt0);
      var 
    $channels = array ("#network");
    }
    /**
     * Let's do some modules
     */
    $modnames = array("botf");
    for(
    $i=0;$i<count($modnames);$i++){
      include(
    "modules/".$modnames[$i].".php");
    }
    ?&
    gt
    And an example of a module:
    PHP Code:
    <?php
    /**
     * Some basic bot functions
     */
    class botf {
        function 
    onjoin_greeting(&$irc, &$data)
        {
            
    // if _we_ join, don't greet ourself, just jump out via return
            
    if ($data->nick == $irc->_nick)
                return;
            
    // now check if this is the right channel
            
    if ($data->channel == '#network')
                
    // it is, lets greet the joint user
                
    $irc->message(SMARTIRC_TYPE_CHANNEL'#network''Hey, '.$data->nick);
                
    print_r($data);
        }
    }
    ?&
    gt
    Although as you can probably tell (given you know PHP, and have read the source) dynamic module loading isn't working, but I think I'm on my way for getting the bot up and running sometime soon, and hopefully addind some neat features into ANewEraIRC, it's services, and it's website.

    Comments and suggestions are welcome

    -enmand
    Come to UnError.com

  2. #2
    Now, RFC Compliant! Noia's Avatar
    Join Date
    Jan 2002
    Posts
    1,210
    This is the same design as Cygnus, I already have the module loading down though, if you want details I can supply you with the source and modules to boot
    With all the subtlety of an artillery barrage / Follow blindly, for the true path is sketchy at best. .:Bring OS X to x86!:.
    Og ingen kan minnast dei linne drag i dronningas andlet den fagre dag D landet her kvilte i heilag fred og alle hadde kjrleik elske med.

  3. #3
    Developer Extraordinar
    Join Date
    Jul 2002
    Location
    Internets
    Posts
    571
    Cool, I remember that bot. I think I have the module loading under control, I was just to tired to do it last night. Anyways, if I need some help, I'll be sure to ask. :-)

    enmand
    Come to UnError.com

  4. #4
    Developer Extraordinar
    Join Date
    Jul 2002
    Location
    Internets
    Posts
    571
    Here's an updated version of the bot (complete with dynamic module loading, and some example functions in a module)

    quarid.php
    PHP Code:
    <?php
    ini_set
    ("max_execution_time","0");
    /**
     *  Quarid, a basic (at the moment) bot for op functions, and some "neat" features
     *  such as stats logging, and "last seen". Configuration in config.php
     *  Build using SmartIRC PEAR class
     *
     *  Author: Daniel J. Enman
     *  Creation Date: December 14th 2005
     */
    /**
     * Include what we need to.
     */
    require_once("SmartIRC.php");
    require_once(
    "QuaridConfig.php");

    class 
    Quarid extends QuaridConfig {
      
    /**
       *  The Net_SmartIRC object
       */
      
    var $irc;
      
      
    /**
       *    Constructors... yippie!
       */
       
       
    public function __construct() {
         
    /**
          * We're going to need the Net_SmartIRC class for some
          * fancy pantsin'.
          */
         
    $this->irc = new Net_SmartIRC();
         
    /**
          * Now that we have the $this->irc object, we need to get the module
          * objects so we can do some more fancy pantsin'
          */
         
    $modulename $this->modulenames;
         for(
    $i=0;$i<count($modulename);$i++){ // Loop throuhg all the objects
           
    ${$modulename[$i]} = $modulename[$i]; // Dynaimically set the modulename
           
    $mod = ${$modulename[$i]};   // Make it easier to work with.
           
    $this->$mod = new $mod();
         }
         
    //$botff = "botf";
         //$this->{$botff} = new botf();
       
    }

      
    /**
       * Let's start with some debugging
       */
      
    public function _setDebug() {
        
    $debug $this->bot['debuglev'];
        switch(
    $debug){
          case 
    0:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_NONE);
            break;
          case 
    1:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_NOTICE);
            break;
          case 
    2:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_SOCKET);
            break;
          case 
    3:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_TIMEHANDLER);
            break;
          case 
    4:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_USERSYNCING);
            break;
          case 
    5:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MODULES);
            break;
          case 
    6:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGETYPES);
            break;
          case 
    7:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGEPARSER);
            break;
          case 
    8:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_MESSAGEHANDLER);
            break;
          case 
    9:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_IRCMESSAGES);
            break;
          case 
    10:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_CONNECTION);
            break;
          case 
    11:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_CHANNELSYNCING);
            break;
          case 
    12:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_ACTIONHANDLER);
            break;
          case 
    13:
            
    $this->irc->setDebug(SMARTIRC_DEBUG_ALL);
            break;
          default:
            die(
    "Error, please set a value on debug between 0 and 13. See manual for more infromation.");
            break;
        }
        return 
    $debug;
      }

      public function 
    _startLog() {
        
    $logto $this->bot['logto'];
        switch(
    $logto){
          case 
    0:
            
    $this->irc->setLogdestination(SMARTIRC_FILE);
            
    $file "./log/".$this->bot['logfile'];
            if(!
    file_exists($file)){
              
    touch($file);
              
    chmod($file0777);
            }
            
    $logf fopen($file'a');
            if(!
    $logf){
                echo 
    "Could not open file ".$file.", or create log file. Does log/ exist, and is it writeable?\n";
             exit;
            }
            
    $this->irc->log(SMARTIRC_DEBUG_NOTICE"QuaridBot loaded.""Quarid""0");
            
    $this->irc->setLogFile($file);
            break;
          case 
    1:
            
    $this->irc->setLogDestination(SMARTIRC_STDOUT);
            break;
          case 
    2:
            
    $this->irc->setLogDestination(SMARTIRC_BROWSEROUT);
            break;
          case 
    3:
            
    $this->irc->setLogDestination(SMARTIRC_SYSLOG);
            break;
          case 
    4:
            
    $this->irc->setLogDestination(SMARTIRC_NONE);
          default:
            die(
    "Error, please set a value between 0 and 4. See the manual for more information");
        }
        return 
    $logto;
      }
      
    /**
       *    CTCP version
       */
      
    protected function _setVersion() {
        
    $this->irc->setCTCPVersion("QuaridBot 0.01");
      }
      
      public function 
    actions() {
        
    $modulename $this->modulenames;
        for(
    $i=0;$i<count($modulename);$i++){
            ${
    $modulename[$i]} = $modulename[$i]; // Dynaimically set the modulename
            
    $mod = ${$modulename[$i]};   // Make it easier to work with.
            /**
             * Next we have to loop through the functions
             */
                
    for($j=0;$j<count($this->$mod->funcname);$j++){
                 
    $this->irc->registerActionhandler($this->$mod->functype[$j], $this->$mod->funccase[$j], $this->$mod$this->$mod->funcname[$j]);
             }
        }
      }
      
      public function 
    main() {
        
    /**
         * Set up some settings set in QuaridConfig.php
         */
        
    $this->_setDebug();
        
    $this->irc->setUseSockets($this->bot['sockets']);
        
    $this->irc->setBenchmark($this->bot['bmark']);
        
    $this->irc->setChannelSyncing($this->bot['chansync']);
        
    $this->_startLog(); // Let's start logging now!
        
    $this->irc->showBenchmark();
        
    /**
         * Do the botf stuff!
         */
        
    $this->irc->connect($this->bot['serveraddy'], $this->bot['serverport']);
        
    $this->irc->login($this->bot['nickname'], $this->bot['realname'], 0$this->bot['username'], null);
        
    $this->irc->join($this->channels);
        
    $this->actions();
        
    $this->irc->listen();
        
    $this->irc->disconnect();
      }
    }
    $Quarid = new Quarid();
    $Quarid->main();
    ?&
    gt
    QuaridConfig.php:
    PHP Code:
    <?php
    /**
     * The configuration class for Quarid
     */
     
    class QuaridConfig {
      var 
    $db = array("host" =&gt"localhost",
                      
    "user" =&gt"root",
                      
    "pass" =&gt"AReallyStrongPassWord",
                      
    "dbse" =&gt"QuaridIRC",
                      
    "tblp" =&gt"quarid_");
      var 
    $bot = array("nickname" =&gt"Quarid",
                       
    "username" =&gt"quarid",
                       
    "realname" =&gt"Quarid Bot",
                       
    "servername" =&gt"ANewEraIRC",
                       
    "serveraddy" =&gt"unerror.anewerairc.net",
                       
    "serverport" =&gt6667,
                       
    "SSL" =&gtfalse,
                       
    "debuglev" =&gt13,
                       
    "sockets" =&gttrue,
                       
    "bmark" =&gttrue,
                       
    "chansync" =&gttrue,
                       
    "logfile" =&gt"Quarid.log",
                       
    "logto" =&gt0);
      var 
    $channels = array ("#network");
      var 
    $modulenames = array("botf");
      public function 
    __construct(){
        for(
    $i=0;$i<count($this->modulenames);$i++){
            include(
    "modules/".$this->modulenames[$i].".php");
        }
      }
    }

    /**
     * Let's use __construct() to load the the modules
     */
    $qc = new QuaridConfig();
    ?&
    gt
    botf.php (basic module)
    PHP Code:
    <?php
    /**
     * Some basic bot functions
     */
    class botf {
      public function 
    printr(&$irc, &$ircdata){
        
    $message "I, $irc->_realname, just messaged $ircdata->channel because $ircdata->nick told me too";
        
    $irc->message(SMARTIRC_TYPE_CHANNEL$ircdata->channel$message);
      }
      public function 
    motd(&$irc, &$ircdata){
        for(
    $i=0;$i<count($irc->_motd);$i++){
          
    $irc->message(SMARTIRC_TYPE_CHANNEL$ircdata->channel$irc->_motd[$i]);
        }
      }
      public function 
    users(&$irc, &$data){
        foreach(
    $irc->channel["$data->channel"]->users as $key =&gt$value){
            
    $irc->message(SMARTIRC_TYPE_CHANNEL$data->channel$key);
      }
      
    //$irc->message(SMARTIRC_TYPE_CHANNEL, $data->channel, $irc->channel['#network']->users['quarid']->nick);
      
    }
      
    /**
       * We're going to need a list of the functions this class holds
       */
      
    var $functype = array(SMARTIRC_TYPE_CHANNEL,
                            
    SMARTIRC_TYPE_CHANNEL,
                            
    SMARTIRC_TYPE_CHANNEL); // Action Type, see manual for more information
      
    var $funccase = array("^!printr",
                            
    "^!motd",
                            
    "^!users");  // What case should this function activate (in relation to $functype)
      
    var $funcname = array("printr",
                            
    "motd",
                            
    "users"); // What's the functions name in the class?
    }
    ?&
    gt
    It's written for use with PHP5, and it probably won't work with PHP4, though I may release a PHP4 version at some time. Feel free to write some module (take a look at the Net_SmartIRC PEAR class, it should give you a rough idea on how to) and send them in to me :-)

    enmand
    Come to UnError.com

  5. #5
    Ninja Code Monkey
    Join Date
    Nov 2001
    Location
    Washington State
    Posts
    1,027
    Why does Quarid inherit from it's config class?
    "When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
    "There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
    "Mischief my ass, you are an unethical moron." - chsh
    Blog of X

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides