cvs: pearbot / PEARbot.php

[email protected] ("Mirco 'meebey' Bauer") Fri, 25 Jul 2003 14:59:06 -0000
Newsgroups php.pear.bot
Message-ID <cvsmeebey1059145146@cvsserver>
meebey		Fri Jul 25 10:59:06 2003 EDT

  Modified files:              
    /pearbot	PEARbot.php 
  Log:
  - removed dbquery() using now PEAR::MDB.
  - added CPEARbot::mdbError() method.
  - updated reverseverfiy().
  - added isAuthorized().
  
  
Index: pearbot/PEARbot.php
diff -u pearbot/PEARbot.php:1.7 pearbot/PEARbot.php:1.8
--- pearbot/PEARbot.php:1.7	Fri Jul 25 07:38:13 2003
+++ pearbot/PEARbot.php	Fri Jul 25 10:59:06 2003
@@ -1,7 +1,7 @@
 #!/home/meebey/php
 <?php
 /**
- * $Id: PEARbot.php,v 1.7 2003/07/25 11:38:13 meebey Exp $
+ * $Id: PEARbot.php,v 1.8 2003/07/25 14:59:06 meebey Exp $
  * 
  * PEARbot
  * this is an IRC service bot for #pear written in PHP
@@ -28,27 +28,19 @@
  *
  */
 
-include_once('config.php');
-include_once('../SmartIRC/SmartIRC.php');
-
-function dbquery($query)
-{
-    global $mysql_db;
-    global $mysql_server;
-    global $mysql_username;
-    global $mysql_password;
-    global $mysql_link;
-    global $irc;
-
-    if(!is_resource($mysql_link))
-    $mysql_link = mysql_connect( $mysql_server, $mysql_username, $mysql_password );
-
-    $result = mysql_db_query( $mysql_db, $query, $mysql_link );
-    return $result;
-}
+include_once 'config.php';
+include_once '../pear/SmartIRC/SmartIRC.php'; // using bleeding edge CVS versions
+include_once 'MDB.php';
 
 class CPEARbot
 {
+    function mdbError(&$irc, &$errorobj)
+    {
+        $error = $errorobj->getUserinfo();
+        $irc->log(SMARTIRC_DEBUG_NOTICE, 'DEBUG_NOTICE: DB error: '.$error);
+        return $error;
+    }
+    
     function nick2pearusername(&$irc, $nickname)
     {
         $query = "SELECT pearusername FROM dnsentries WHERE nickname='".$nickname."'";
@@ -57,25 +49,30 @@
         return $row['pearusername'];
     }
     
-    function reverseverify(&$irc, $host, $nick)
+    function reverseverify(&$irc, &$data)
     {
+        global $mdb;
+        
         $query = "SELECT nickname,dnsalias FROM dnsentries";
-        $result = dbquery($query);
+        $result = $mdb->query($query);
+        if (MDB::isError($result)) {
+            mdbError($result);
+            return;
+        }
         
         $list = array();
         $foundnick = false;
-        $userip = gethostbyname($host);
-        while ($row = mysql_fetch_array($result)) {
-            $dbip = gethostbyname($row['dnsalias']);
+        $userip = gethostbyname($data->host);
+        while ($row = $mdb->fetchInto($result)) {
+            $dbip = gethostbyname($row['host']);
             $dbnickname = $row['nickname'];
             
             if ($userip == $dbip) {
-                if ($dbnickname != $nick) {
+                if ($dbnickname != $data->nick) {
                     $foundnick = $dbnickname;
                 } else {
-                    $foundnick = $nick;
+                    $foundnick = $data->nick;
                 }
-                
                 break;
             }
         }
@@ -87,39 +84,29 @@
         }
     }
     
-    function verify(&$irc, $nickname, $ircnickname = null)
+    function isAuthorized(&$irc, &$data, $nick = null)
     {
-        $who = $nickname;
-        $loweredwho = strtolower($who);
+        global $mdb;
         
-        if ($ircnickname !== null) {
-            $dbwho = $nickname;
-            $who = $ircnickname;
-            $loweredwho = strtolower($who);
-        } else {
-            $dbwho = $who;
+        if ($nick === null) {
+            $nick = $data->nick;
         }
         
-        if (isset($irc->channel['#pear']->users[$loweredwho])) {
-            $query = "SELECT nickname,dnsalias FROM dnsentries WHERE nickname='".$dbwho."'";
-            $result = dbquery($query);
-            $numrows = mysql_num_rows($result);
-            
-            if($numrows > 0) {
-                $host = $irc->channel['#pear']->users[$loweredwho]->host;
-                $ip = gethostbyname($host);
-                
-                $found = false;
-                while ($row = mysql_fetch_array($result)) {
-                    $dnsaliasip = gethostbyname($row['dnsalias']);
-                    
-                    if ($dnsaliasip == $ip) {
-                        return $row['nickname'];
-                    }
-                }
-                
-            }
-            
+        if (!$irc->isJoined($data->channel, $nick)) {
+            return false;
+        }
+        
+        $user = &$irc->getUser($data->channel, $nick);
+        
+        // check if the user is recognized and has right host, ident etc..
+        $data->nick = $user->nick;
+        $data->host = $user->host;
+        $data->ident = $user->ident;
+        
+        $result = $this->reverseverify($irc, $data);
+        if ($result !== false) {
+            return true;
+        } else {
             return false;
         }
     }
@@ -130,13 +117,12 @@
         if ($data->nick == $irc->_nick) {
             return;
         }
+        
+        // autoop only for the pear channel
+        if (strtolower($data->channel) == '#pear' &&
+            $this->isAuthorized($irc, $data)) {
             
-        if (strtolower($data->channel) == '#pear') {
-            $result = $this->reverseverify($irc, $data->host, $data->nick);
-            
-            if ($result !== false) {
-                $irc->op('#pear', $data->nick);
-            }
+            $irc->op('#pear', $data->nick);
         }
     }
     
@@ -148,16 +134,17 @@
     {
         $requester = $data->nick;
         $lookupfor = $data->messageex[1];
-        $lowerlookupfor = strtolower($data->messageex[1]);
         
-        if (isset($irc->channel['#pear']->users[$lowerlookupfor])) {
-            $host = $irc->channel['#pear']->users[$lowerlookupfor]->host;
+        if ($irc->isJoined('#pear', $lookupfor)) {
+            $host = $irc->getUser('#pear', $lookupfor);
         } else {
             $irc->message(SMARTIRC_TYPE_CHANNEL, '#pear', $requester.": ".$lookupfor." is not in this channel"); 
             return;
         }
-     
-        $result = $this->reverseverify($irc, $host, $lookupfor);
+        
+        $data->host  = $host;
+        $data->nick = $lookupfor;
+        $result = $this->reverseverify($irc, $data);
         
         if ($result !== false) {
             $irc->message(SMARTIRC_TYPE_CHANNEL, '#pear', $requester.": ".$lookupfor." is ".$result);
@@ -167,9 +154,17 @@
 
 $bot = &new CPEARbot();
 $irc = &new Net_SmartIRC();
+
+$mdb = &MDB::connect($config['db_dsn']);
+if (MDB::isError($mdb)) {
+    mdbError($mdb);
+    die('DB error: '.$mdb->getUserinfo());
+}
+$mdb->setFetchMode(MDB_FETCHMODE_ASSOC);
+
 $irc->setLogdestination(SMARTIRC_FILE);
 $irc->setLogfile('./PEARbot.log');
-$irc->setChannelSynching(true);
+$irc->setChannelSyncing(true);
 $irc->setDebug(SMARTIRC_DEBUG_ALL);
 $irc->setUseSockets(true);
 $irc->setAutoRetry(true);