BeBot - An Anarchy Online and Age Of Conan chat automaton

Archive => Anarchy Online Archive => 0.2.x Custom/Unofficial Modules => Topic started by: Sabkor on December 10, 2005, 04:11:15 am

Title: Quotes
Post by: Sabkor on December 10, 2005, 04:11:15 am
We all say stupid things, and everyone wants to immortalize them. The guild loves this one, so I thought I'd share with you all ;)

No helpfile, but it's easy to use:

!quotes - Show a random quote
!quotes <id> - Show a specific quote
!quotes add <text> - Add a new quote
!quotes del <id> - Delete a quote that you added (or if you're an admin, delete any quote)
Code: [Select]
<?php
  
/*
   * Quotes.php - Module template.
   *
   * BeBot - An Anarchy Online Chat Automaton
   * Copyright (C) 2004 Jonas Jax
   *
   * Developed by Sabkor (RK1)
   *
   * File last changed at $LastChangedDate: 2004-12-29 01:41:32 +0100 (Wed, 29 Dec 2004) $
   * Revision: $Id: _ClassName.php 8 2004-12-29 00:41:32Z blondengy $
   */
   
  /*
    Add a "_" at the beginning of the file (_quotes.php) if you do not want it to be loaded.
  */

  
$db -> query("CREATE TABLE IF NOT EXISTS quotes
              (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, quote VARCHAR(2000), contributor VARCHAR(15))"
);
  
  
$quotes = new Quotes($bot);

  
$commands["tell"]["quotes"] = &$quotes;
  
$commands["pgmsg"]["quotes"] = &$quotes;
  
$commands["gc"]["quotes"] = &$quotes;


  
/*
    The Class itself...
  */
  
class Quotes
  
{
    var 
$bot;
    
    

    
/*
      Constructor:
        Hands over a referance to the "Bot" class.
    */
    
function Quotes (&$bot)
    {
      
$this -> bot = &$bot;
    }



    
/*
      This gets called on a tell with the command
    */
    
function tell($name$msg)
    {
   
  if (preg_match("/^" $this -> bot -> commpre "quotes ([0-9]+)$/i"$msg$info))
$this -> bot ->send_tell($name$this -> send_quote($info[1]));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes add (.+)$/i"$msg$info))
   
   $this -> bot ->send_tell($name$this -> add_quote($info[1], $name));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes (remove|del|rem|delete) ([0-9]+)$/i"$msg$info))
   
   $this -> bot ->send_tell($name$this -> del_quote($info[2], $name));      
   
  else
$this -> bot ->send_tell($name$this -> send_quote(-1));
    }



    
/*
      This gets called on a msg in the privgroup with the command
    */
    
function pgmsg($name$msg)
    {
   
  if (preg_match("/^" $this -> bot -> commpre "quotes ([0-9]+)$/i"$msg$info))
$this -> bot ->send_pgroup($this -> send_quote($info[1]));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes add (.+)$/i"$msg$info))
   
   $this -> bot ->send_pgroup($this -> add_quote($info[1], $name));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes (remove|del|rem|delete) ([0-9]+)$/i"$msg$info))
   
   $this -> bot ->send_pgroup($this -> del_quote($info[2], $name));      
   
  else
$this -> bot ->send_pgroup($this -> send_quote(-1));
    }



    
/*
      This gets called on a msg in the guildchat with the command
    */
    
function gc($name$msg)
    {
   
  if (preg_match("/^" $this -> bot -> commpre "quotes ([0-9]+)$/i"$msg$info))
$this -> bot ->send_gc($this -> send_quote($info[1]));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes add (.+)$/i"$msg$info))
   
   $this -> bot ->send_gc($this -> add_quote($info[1], $name));
   
  else if (preg_match("/^" $this -> bot -> commpre "quotes (remove|del|rem|delete) ([0-9]+)$/i"$msg$info))
   
   $this -> bot ->send_gc($this -> del_quote($info[2], $name));
   
  else
$this -> bot ->send_gc($this -> send_quote(-1));
    }

function add_quote($strquote$name)
{
    $this -> bot -> db -> query("INSERT INTO quotes (quote, contributor) VALUES ('" str_replace("'""''"$strquote) . "', '" $name "')");
    $num $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
    $strmsg "Thank you, your quote has been added as id #" $num[0][0];
   
   
   
    return $strmsg;        
}

function del_quote($qnum$name)
{
   
    $result $this -> bot -> db -> select("SELECT * FROM quotes WHERE id=" $qnum);
    if (!empty($result)) {
    $candel false;
if($this -> bot -> admin -> in_group($name"superadmin")) $candel true;
if($this -> bot -> admin -> in_group($name"admin")) $candel true;
if($result[0][2]=$name$candel true;

if ($candel=true) {
$this -> bot -> db -> query("DELETE FROM quotes WHERE id="$qnum);
$reply "Quote removed.";
}
else
$reply "Unable to delete quote with ID of " $qnum ". You must be an admin, or the contributor of the quote to delete it.";

} else {
$num $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
$reply "Quote with id of " $qnum " not found. (Highest quote ID is " $num[0][0] . ".)";
}

return $reply;
}

function send_quote($qnum)
{
$strquote "";
if($qnum == -1) {
$num $this -> bot -> db -> select("SELECT count(*) FROM quotes");
   
   $result $this -> bot -> db -> select("SELECT * FROM quotes");
   
  
   
   if($num[0][0] > 0) {
   
   $row rand(0,$num[0][0]);
   
   $strquote "#" $result[$row][0] . " - " $result[$row][1] . " [Contributed by: " $result[$row][2] . "]";
   
   } else {
   
   $strquote "No quotes exist. Add some!";
   
   }
} else {
   
   $result $this -> bot -> db -> select("SELECT * FROM quotes WHERE id=" $qnum);
       if (!empty($result)) {
       $strquote "#" $result[0][0] . " - " $result[0][1] . " [Contributed by: " $result[0][2] . "]";
    } else {
    $num $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
    $strquote "Quote with id of " $qnum " not found. (Highest quote ID is " $num[0][0] . ".)";
    }
}

return $strquote;
}

  }
?>

Title: Quotes
Post by: Akarah on December 10, 2005, 04:30:12 am
haha! i just wrote one of these too :)

slightly different, but similar enough that i'm not going to post mine now. good work :)
Title: Quotes
Post by: Xenixa on December 10, 2005, 08:31:02 am
hheh, like "Pinks" on Gridbot. :D
Title: Quotes
Post by: mookie on December 10, 2005, 08:54:28 am
Here is a help file for Quotes

Code: [Select]

<font color=CCInfoHeader><pre>quotes</font>
<font color=CCInfoText>Shows a random quote.</font>
<font color=CCInfoHeader><pre>quotes &lt;ID&gt;</font>
<font color=CCInfoText>Shows a specific quote.</font>
<font color=CCInfoHeader><pre>quotes add &lt;text&gt;</font>
<font color=CCInfoText>Add a new quote.</font>
<font color=CCInfoHeader><pre>quotes del &lt;ID&gt;</font>
<font color=CCInfoText>Delete a quote that you added (or if you're an admin, delete any quote).</font>
Title: Quotes
Post by: Wanuarmi on December 10, 2005, 09:20:05 am
great module, one of my members found a bug, it seems that deleted quotes ids are added to the random picked ones, so it shows an empty quote sometimes

btw my mysql is configured so it goes on with index numbers, it doesnt use the first available one
Title: Quotes
Post by: Wanuarmi on December 10, 2005, 09:32:49 am
temp fix

Code: [Select]
<?
  /*
   * Quotes.php - Module template.
   *
   * BeBot - An Anarchy Online Chat Automaton
   * Copyright (C) 2004 Jonas Jax
   *
   * Developed by Sabkor (RK1)
   *
   * File last changed at $LastChangedDate: 2004-12-29 01:41:32 +0100 (Wed, 29 Dec 2004) $
   * Revision: $Id: _ClassName.php 8 2004-12-29 00:41:32Z blondengy $
   */
   
  /*
    Add a "_" at the beginning of the file (_quotes.php) if you do not want it to be loaded.
  */

  $db -> query("CREATE TABLE IF NOT EXISTS quotes
              (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, quote VARCHAR(2000), contributor VARCHAR(15))");
 
  $quotes = new Quotes($bot);

  $commands["tell"]["quotes"] = &$quotes;
  $commands["pgmsg"]["quotes"] = &$quotes;
  $commands["gc"]["quotes"] = &$quotes;


  /*
    The Class itself...
  */
  class Quotes
  {
    var $bot;
   
   

    /*
      Constructor:
        Hands over a referance to the "Bot" class.
    */
    function Quotes (&$bot)
    {
      $this -> bot = &$bot;
    }



    /*
      This gets called on a tell with the command
    */
    function tell($name, $msg)
    {
        if (preg_match("/^" . $this -> bot -> commpre . "quotes ([0-9]+)$/i", $msg, $info))
      $this -> bot ->send_tell($name, $this -> send_quote($info[1]));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes add (.+)$/i", $msg, $info))
           $this -> bot ->send_tell($name, $this -> add_quote($info[1], $name));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes (remove|del|rem|delete) ([0-9]+)$/i", $msg, $info))
           $this -> bot ->send_tell($name, $this -> del_quote($info[2], $name));          
        else
      $this -> bot ->send_tell($name, $this -> send_quote(-1));
    }



    /*
      This gets called on a msg in the privgroup with the command
    */
    function pgmsg($name, $msg)
    {
        if (preg_match("/^" . $this -> bot -> commpre . "quotes ([0-9]+)$/i", $msg, $info))
      $this -> bot ->send_pgroup($this -> send_quote($info[1]));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes add (.+)$/i", $msg, $info))
           $this -> bot ->send_pgroup($this -> add_quote($info[1], $name));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes (remove|del|rem|delete) ([0-9]+)$/i", $msg, $info))
           $this -> bot ->send_pgroup($this -> del_quote($info[2], $name));          
        else
      $this -> bot ->send_pgroup($this -> send_quote(-1));
    }



    /*
      This gets called on a msg in the guildchat with the command
    */
    function gc($name, $msg)
    {
        if (preg_match("/^" . $this -> bot -> commpre . "quotes ([0-9]+)$/i", $msg, $info))
      $this -> bot ->send_gc($this -> send_quote($info[1]));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes add (.+)$/i", $msg, $info))
           $this -> bot ->send_gc($this -> add_quote($info[1], $name));
        else if (preg_match("/^" . $this -> bot -> commpre . "quotes (remove|del|rem|delete) ([0-9]+)$/i", $msg, $info))
           $this -> bot ->send_gc($this -> del_quote($info[2], $name));
        else
      $this -> bot ->send_gc($this -> send_quote(-1));
    }

   function add_quote($strquote, $name)
   {
       $this -> bot -> db -> query("INSERT INTO quotes (quote, contributor) VALUES ('" . str_replace("'", "''", $strquote) . "', '" . $name . "')");
       $num = $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
       $strmsg = "Thank you, your quote has been added as id #" . $num[0][0];
         
          return $strmsg;          
   }
   
   function del_quote($qnum, $name)
   {
          $result = $this -> bot -> db -> select("SELECT * FROM quotes WHERE id=" . $qnum);
         if (!empty($result)) {
            $candel = false;
         if($this -> bot -> admin -> in_group($name, "superadmin")) $candel = true;
         if($this -> bot -> admin -> in_group($name, "admin")) $candel = true;
         if($result[0][2]=$name) $candel = true;
         
         if ($candel=true) {
            $this -> bot -> db -> query("DELETE FROM quotes WHERE id=". $qnum);
            $reply = "Quote removed.";
         }
         else
            $reply = "Unable to delete quote with ID of " . $qnum . ". You must be an admin, or the contributor of the quote to delete it.";
         
      } else {
         $num = $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
         $reply = "Quote with id of " . $qnum . " not found. (Highest quote ID is " . $num[0][0] . ".)";
      }
     
      return $reply;
   }

   function send_quote($qnum)
   {
      $strquote = "";
      if($qnum == -1) {
         $num = $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
              $result = $this -> bot -> db -> select("SELECT * FROM quotes");
           
              if($num[0][0] > 0) {
    $found = false;
while ($found == false)
{
                $row = rand(0,$num[0][0]);
if (!empty($result[$row][0]))
{
$strquote = "#" . $result[$row][0] . " - " . $result[$row][1] . " [Contributed by: " . $result[$row][2] . "]";
$found = true;
}
}
              } else {
                 $strquote = "No quotes exist. Add some!";
              }
      } else {
              $result = $this -> bot -> db -> select("SELECT * FROM quotes WHERE id=" . $qnum);
              if (!empty($result)) {
                 $strquote = "#" . $result[0][0] . " - " . $result[0][1] . " [Contributed by: " . $result[0][2] . "]";
            } else {
               $num = $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
               $strquote = "Quote with id of " . $qnum . " not found. (Highest quote ID is " . $num[0][0] . ".)";
            }        
      }

      return $strquote;
   }

  }
?>
Title: Re: Quotes
Post by: Sabkor on December 11, 2005, 09:26:24 am
Good fix, Wanuarmi, thanks :)

I had just added the remove feature and hadn't even really used it much, never even crossed my mind about that happening...
Title: Re: Quotes
Post by: Shelnaria on December 13, 2005, 06:01:21 am
Code: [Select]
function del_quote($qnum, $name)
   {
          $result = $this -> bot -> db -> select("SELECT * FROM quotes WHERE id=" . $qnum);
         if (!empty($result)) {
            $candel = false;
         if($this -> bot -> admin -> in_group($name, "superadmin")) $candel = true;
         if($this -> bot -> admin -> in_group($name, "admin")) $candel = true;
         if($result[0][2]=$name) $candel = true;
         
         if ($candel=true) {
            $this -> bot -> db -> query("DELETE FROM quotes WHERE id=". $qnum);
            $reply = "Quote removed.";
         }
         else
            $reply = "Unable to delete quote with ID of " . $qnum . ". You must be an admin, or the contributor of the quote to delete it.";
         
      } else {
         $num = $this -> bot -> db -> select("SELECT id FROM quotes ORDER BY id DESC");
         $reply = "Quote with id of " . $qnum . " not found. (Highest quote ID is " . $num[0][0] . ".)";
      }
     
      return $reply;
   }

Need to change two lines so only those who are supposed to be able to CAN delete the quotes. Thanks to Shadowmaster and Naturalistic on their help in solving this for me.

Code needed to change:
Code: [Select]
if($result[0][2]=$name) $candel = true;
         
         if ($candel=true) {
To
Code: [Select]
if($result[0][2]==$name) $candel = true;
         
         if ($candel==true) {
Title: Re: Quotes
Post by: Sabkor on December 17, 2005, 09:58:48 pm
ha, you know what... I always forget to do that when I'm coding PHP ;)  Thanks
Title: Re: Quotes
Post by: Glarawyn on January 08, 2006, 11:06:01 pm
I was getting a database error when adding quotes. Changing the quote column from VARCHAR to BLOB fixed.

Code: [Select]
$db -> query("CREATE TABLE IF NOT EXISTS quotes
              (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, quote VARCHAR(2000), contributor VARCHAR(15))");

Code: [Select]
$db -> query("CREATE TABLE IF NOT EXISTS quotes
              (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, quote BLOB, contributor VARCHAR(15))");
Title: Re: Quotes
Post by: Akarah on January 09, 2006, 12:29:47 am
might want to addslashes() some of the input stuff too   :o
Title: Re: Quotes
Post by: jjones666 on January 25, 2006, 02:26:04 am
Great module!  We have been using this a lot and have filled up about 50 quotes so far.  I wondered if there was a possibility to add a command so that all quotes could be displayed in a blob (kind of like how !news outputs)?

Cheers,

-jj-
Title: Re: Quotes
Post by: Glarawyn on January 25, 2006, 09:54:50 pm
might want to addslashes() some of the input stuff too   :o

I think what Akarah is trying to say is that more characters than single quote need extra handling when inserting into a MySQL database. Single quote ('), double quote ("), backslash (\) and NUL (the NULL byte) need to be escaped by adding a \ character.

See addslashes (http://us3.php.net/manual/en/function.addslashes.php) and stripslashes (http://us3.php.net/manual/en/function.stripslashes.php).

While what you have seems to work ok with " and ', if someone does something like
Quote
/tell bot !quotes add 1\2
Bot: Thank you, your quote has been added as id #73

You get:
Quote
/tell bot !quotes 73
Bot: #73 - 12 [Contributed by: Glarawyn]


For example:

Line 99:

Find
Code: [Select]
$this -> bot -> db -> query("INSERT INTO quotes (quote, contributor) VALUES ('" . str_replace("'", "''", $strquote) . "', '" . $name . "')");

Replace with
Code: [Select]
$this -> bot -> db -> query("INSERT INTO quotes (quote, contributor) VALUES ('" . addslashes($strquote) . "', '" . $name . "')");


I tried the change, and I didn't really notice any difference. I really wasn't able to trigger any errors, but that could be due to a PHP or MySQL configuration on my end.
SimplePortal 2.3.7 © 2008-2025, SimplePortal