Your IP : 216.73.216.224


Current Path : /home/mudbot/eggdrop/
Upload File :
Current File : //home/mudbot/eggdrop/QuoteEngine.tcl

# $Id: QuoteEngine.tcl,v 1.9 2003/07/27 21:56:16 James Exp $

###############################################################################

# QuoteEngine for eggdrop bots
# Copyright (C) James Michael Seward 2003
#

# This program is covered by the GPL, please refer the to LICENCE file in the

# distribution.
###############################################################################


# load the extension
# CHANGE ME: My main bot's server has broken TCL, so I need the line below:
load /usr/lib/x86_64-linux-gnu/libmysqlclient.so
# BUT most people should get away with this if their server's setup right:
#package require mysqltcl

# Use only one or other of the above!

# connect to database
# CHANGE ME
set db_handle [mysqlconnect -host localhost -user root -password "wssx34x!" -db newquotes]

# url to site
# CHANGE ME (use blank if you're not using the PHP script)
set php_page "http://icq.ex.ac.uk/~notopic/quotes"

# bind commands
# CHANGE as needed (default is people with local +ov in bot)
bind pub "|ov" !addquote quote_add
bind pub "|ov" !randquote quote_rand
bind pub "|ov" !fetchquote quote_fetch
bind pub "|ov" !getquote quote_fetch
bind pub "|ov" !findquote quote_search
bind pub "|ov" !searchquote quote_search
bind pub "|ov" !urlquote quote_url
bind pub "|ov" !quoteurl quote_url
bind pub "|ov" !delquote quote_delete
bind pub "|ov" !deletequote quote_delete
bind pub "|ov" !quotestats quote_stats
bind pub "|ov" !quoteversion quote_version
bind pub "|ov" !quotehelp quote_help

### code starts here
set quote_version "1.00"

proc quote_add { nick host handle channel text } {
  global db_handle

  if {($handle == "") || ($handle == "*")} {
    set handle $nick
  }

  set sql "INSERT INTO quotes VALUES(null, "
  append sql "'$handle', "
  append sql "'$nick!$host', "
  set text [mysqlescape $text]
  append sql "'$text', "
  append sql "'$channel', "
  append sql "'[clock seconds]')"

  putlog "QuoteEngine: executing $sql"

  set result [mysqlexec $db_handle $sql]
  if {$result != 1} {
    putlog "An error occurred with the sql :("
  } else {
    set id [mysqlinsertid $db_handle]
    puthelp "PRIVMSG $channel :Quote $id added"
  }
}

proc quote_rand { nick host handle channel text } {
  global db_handle

  set sql "SELECT * FROM quotes WHERE channel='$channel' ORDER BY RAND() LIMIT 1"
  putlog "QuoteEngine: executing $sql"

  set result [mysqlquery $db_handle $sql]

  if {[set row [mysqlnext $result]] != ""} {
    set id [lindex $row 0]
    set quote [lindex $row 3]
    set by [lindex $row 1]
    set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]

    puthelp "PRIVMSG $channel :\002$id\002: $quote \[Added: $by at $when\]"
  } else {
    putlog "PRIVMSG $channel :Couldn't find a quote :("
  }
  mysqlendquery $result
}

proc quote_fetch { nick host handle channel text } {
  global db_handle

  if {![regexp {[0-9]+} $text]} {
    puthelp "PRIVMSG $channel: Use: !getquote <id>"
    return 0
  }

  set sql "SELECT * FROM quotes WHERE id='$text'"
  putlog "QuoteEngine: executing $sql"

  set result [mysqlquery $db_handle $sql]

  if {[set row [mysqlnext $result]] != ""} {
    set id [lindex $row 0]
    set quote [lindex $row 3]
    set by [lindex $row 1]
    set when [clock format [lindex $row 5] -format "%Y/%m/%d %H:%M"]
    set chan [lindex $row 4]
    if {$chan != $channel} {
      set out "\002$id\002: ($chan) $quote \[Added $by at $when\]"
    } else {
      set out "\002$id\002: $quote \[Added $by at $when\]"
    }
  } else {
    set out "Couldn't find quote $text"
  }

  mysqlendquery $result

  if {$out != ""} {
    puthelp "PRIVMSG $channel :$out"
  }
}

proc quote_search { nick host handle channel text } {
  global db_handle php_page

  if {$text == ""} {
    puthelp "PRIVMSG $channel :Use: !findquote <text>"
    return 0
  }
  
  set sql "SELECT * FROM quotes WHERE quote LIKE '%[mysqlescape $text]%' AND channel='$channel' ORDER BY RAND()"

  putlog "QuoteEngine: executing $sql"

  if {[mysqlsel $db_handle $sql] > 0} {

    set count 0
    mysqlmap $db_handle {id qnick qhost quote qchannel qts} {
      if {$count == 5} {
        break
      }

      set when [clock format $qts -format "%Y/%m/%d %H:%M"]
      puthelp "PRIVMSG $channel :\002$id\002\:$qnick: $quote"
      incr count
    }
    
    set remaining [mysqlresult $db_handle rows?]
    if {$remaining > 0} {
      regsub "#" $channel "" chan
      if {$php_page != ""} {
        puthelp "PRIVMSG $channel :(Plus $remaining more matches: $php_page?filter=${text}&channel=${chan}&search=search)"
      } else {
        puthelp "PRIVMSG $channel :Plus $remaining other matches"
      }
    } else {
      if {$count == 1} {
        puthelp "PRIVMSG $channel :(All of 1 match)"
      } else {
        puthelp "PRIVMSG $channel :(All of $count matches)"
      }
    }
  } else {
    puthelp "PRIVMSG $channel :No matches"
  }
}

proc quote_url { nick host handle channel text } {
  global php_page
  if {$php_page != ""} {
    puthelp "PRIVMSG $channel :$php_page"
  } else {
    puthelp "PRIVMSG $channel :Not available."
  }
}

proc quote_stats { nick host handle channel text } {
  global db_handle

  set sql "SELECT COUNT(*) AS total FROM quotes WHERE channel='$channel'"
  putlog "QuoteEngine: executing $sql"

  set result [mysqlquery $db_handle $sql]
  set total 0
  set chan 0

  if {[set row [mysqlnext $result]] != ""} {
    set total [lindex $row 0]
  }

  mysqlendquery $result

  set sql "SELECT COUNT(*) AS total FROM quotes"
  putlog "QuoteEngine: executing $sql"

  set result [mysqlquery $db_handle $sql]

  if {[set row [mysqlnext $result]] != ""} {
    set chan [lindex $row 0]
  }

  mysqlendquery $result

  set sql "SELECT COUNT(*) AS total FROM quotes WHERE nick='$handle' AND channel='$channel'"
  putlog "QuoteEngine: executing $sql"

  set result [mysqlquery $db_handle $sql]

  if {[set row [mysqlnext $result]] != ""} {
    set by_handle [lindex $row 0]
  }

  mysqlendquery $result

  puthelp "PRIVMSG $channel :Quotes for $channel: \002$total\002 (total: $chan). You have added \002$by_handle\002 quotes in this channel."
}

proc quote_delete  { nick host handle channel text } {
  global db_handle

  set id [mysqlescape $text]
  if {![matchattr $handle m|m $channel]} {
    set sql "SELECT nick FROM quotes WHERE id='$text'"
    putlog "QuoteEngine: executing $sql"

    set result [mysqlquery $db_handle $sql]
    set owner [lindex [mysqlnext $result] 0]
    mysqlendquery $result
    if {$owner != $handle} {
      puthelp "NOTICE $nick :You cannot delete that quote."
      return 0
    }
  }

  set sql "DELETE FROM quotes WHERE id='$text'"
  putlog "QuoteEngine: executing $sql..."
  
  set result [mysqlexec $db_handle $sql]
  if {$result != 1} {
    puthelp "PRIVMSG $channel :An error occurred deleting the quote :("
    return 0
  } else {
    puthelp "PRIVMSG $channel :Deleted quote $text"
  }
}

proc quote_version { nick host handle channel text } {
  global quote_version
  puthelp "PRIVMSG $channel :This is the QuoteEngine version $quote_version by JamesOff (http://www.jamesoff.net/go/quoteengine)"
  return 0
}

proc quote_help { nick host handle channel text } {
  puthelp "PRIVMSG $nick :Commands for the QuoteEngine script:"
  puthelp "PRIVMSG $nick :  !addquote <quote text> - adds a quote to the database"
  puthelp "PRIVMSG $nick :  !delquote <id> - deletes a quote. You must be either a bot/channel master or the person who added the quote to delete it."
  puthelp "PRIVMSG $nick :  !randquote - fetches a random quote from the current channel"
  puthelp "PRIVMSG $nick :  !getquote <id> - fetches the quote with number <id>"
  puthelp "PRIVMSG $nick :  !findquote <text> - finds up to 5 quotes containing 'text'"
  puthelp "PRIVMSG $nick :  !quoteurl - get the URL for the web interface to the quotes"
  puthelp "PRIVMSG $nick :  !quotestats - get some information"
  puthelp "PRIVMSG $nick :  !quoteversion - get the version of the script"
  puthelp "PRIVMSG $nick :  Some commands have synonyms: !deletequote, !fetchquote, !urlquote, and !searchquote."
  puthelp "PRIVMSG $nick :  (End of help)"
  return 0
}