# QUOTE.TCL v0.1 `2000 by sat0r (sat0r@oddity.de) or nick sat0r on EFnet

#let u add text to the bot which will be shown
#in the channel every x minutes. It picks a
#quote from the database and displays it in the
#chan. Other features: stats, delquote and a
#backup function that sends the quote file to a
#ftp server every x minutes (I wrote this because
#I've lost a shell include a 102kb quotefile
#and I dont want to let this happen again :)
#
# For eggdrop1.4.x or above
#
# Known bugs: if u add a quote with a "?" inside, change
# the "?" to "\?" otherwise the stats script won't work.
# I will correct this in a future version.
##########################################################
# USAGE:
# !quote                   shows a randomly picked quote
#                          from the quotefile
# !addquote blahblehblah     adds the quote "blahblehblah"
#                          to the quotefile
# !delquote blahblehblah     deletes the quote "blahblehblah"
#                          from the quotefile
# !quotestats              shows database statistics in
#                          the quote channel
# !whoadded <quote>        shows u the name of the person
#                          who added the quote.
# /msg <botnick> !qsend    sends ya the whole quotefile
#
##########################################################

# Trigger character of your public commands
set qtrig "!"

# In this channel, the quotes will be shown. Only one channel is allowed. 
set quotechannel "#HaLL"

# This is the file where the quotes are stored (default: quotes)
set quotefile "/IRC/windrop/text/quotes.txt"

# This variable is the time (in minutes) after a random quote is shown. (default: 30 minutes)
set quotetime 10

# Do you want the script to automatically backup the quotefile ? 1 = yes 0 = no (default: 1)
set quotebackup 0

# Backup the quotefile every x minutes (default: 60)
set quotebackuptime 3600

# Now enter the hostnameof the ftp server where to backup the quotefile.
set quoteftp "localhost"

# Username and Password for the ftp account.
set quoteusername "anonymous"
set quotepassword "a@a.com"

# Path of your eggdrop dir on your shell
set eggpath "/IRC/windrop"

# Absolute path of the backup dir on your backup FTP
set backuppath "/quotes"

# Now the bindings: default flag is f
# That means u have to .chattr username +f

bind pub f-f ${qtrig}addquote add_quote
bind pub f-f ${qtrig}delquote del_quote
bind pub f-f ${qtrig}quote pub_randquote
bind pub f-f ${qtrig}quotestats pub_quotestats
bind pub f-f ${qtrig}quotehelp pub_quotehelp
bind pub f-f ${qtrig}whoadded whoadded_quote
#bind msg f-f quotehelp pub_quotehelp
bind msg f-f qsend msg_sendquotes


######### Please do not edit anything below unless you know what you are doing ;) #########

proc add_quote {nick uhost hand chan arg} {
    global quotechannel quotefile quotetime quotearray
    set quote_fid [open $quotefile a]
    putserv "PRIVMSG $chan :\[4x]: quote has been added."
    putlog "QUOTE \<\<$nick\>\> $arg"
    puts $quote_fid "$hand $arg"
    close $quote_fid
    return 1
}

proc del_quote {nick uhost hand chan arg} {
    global quotechannel quotefile quotetime quotearray
    set quote_fid [open $quotefile r]
    set temp_fid [open tempquote w]
    putserv "PRIVMSG $chan :\[4x]: searching for $arg"
    set anzahl 0
    set qfound 0
    while {(![eof $quote_fid])} {
      set ficken [gets $quote_fid]
      if {[string trim [lrange $ficken 1 end]] != [string trim $arg]} {
        puts $temp_fid "$ficken"
      } else { 
	set qfound 1
      }
    }
    if {$qfound != 1} {
      putserv "PRIVMSG $chan :\[4x]: error: quote is not in database."
    } else {
      putserv "PRIVMSG $chan :\[4x]: quote found and deleted from database."
    }
    close $quote_fid
    close $temp_fid
    exec mv tempquote $quotefile
    return 1
}

proc whoadded_quote {nick uhost hand chan arg} {
    global quotechannel quotefile quotetime quotearray
    set quote_fid [open $quotefile r]
    putserv "PRIVMSG $chan :\[4x]: searching for $arg"
    set anzahl 0
    set qfound 0
    while {(![eof $quote_fid])} {
      set ficken [gets $quote_fid]
      if {[string trim [lrange $ficken 1 end]] == [string trim $arg]} {
        putserv "PRIVMSG $chan :\[4x]: quote was added by [lindex $ficken 0]"
        close $quote_fid
        return 1
      }
    }
    putserv "PRIVMSG $chan :\[4x]: quote wasn't found in database."
    close $quote_fid
    return 1
}

proc pub_randquote {nick uhost hand chan arg} {
    global quotechannel quotefile quotetime quotearray
    set file [open $quotefile r]
    set anzahl 0
    set ficken [string trim [gets $file]]
    while {(![eof $file])} {
      set quotearray($anzahl) "$ficken"
       set ficken [string trim [gets $file]]
       incr anzahl
    }
    close $file
    putserv "PRIVMSG $quotechannel :[lrange $quotearray([rand $anzahl]) 1 end]"
    return 1
}

proc pub_quotehelp {nick uhost hand chan arg} {
    global botnick qtrig
    putserv "NOTICE $nick :\[4x]: Utility, Quote v0.1 Help"
    putserv "NOTICE $nick : "
    putserv "NOTICE $nick :Public & MSG Commands:"
    putserv "NOTICE $nick : ${qtrig}addquote <args> - adds a quote."
    putserv "NOTICE $nick : ${qtrig}delquote <args> - removes a quote."
    putserv "NOTICE $nick : ${qtrig}quotehelp - shows you this help."
    putserv "NOTICE $nick : ${qtrig}quotestats - shows stats from the quotefile."
    putserv "NOTICE $nick : ${qtrig}whoadded <args> - shows you the handle of the user who added the quote."
    putserv "NOTICE $nick : ${qtrig}quote - picks a random quote from the database and sends it to the quotechannel."
    putserv "NOTICE $nick : /msg $botnick qsend - send you the quotefile via DCC."
    putserv "NOTICE $nick : "
    putserv "NOTICE $nick :\[4x]: End of Help, Utility Quote v0.1"
    return 1
}

proc pub_quotestats {nick uhost hand chan arg} {
    global quotechannel quotefile quotetime quotearray
    set file [open $quotefile r]
    set anzahl 2
    set quoteadders 0
    set ficken [string trim [gets $file]]
    set i 0
    set qadder(0) [lindex $ficken 0]
    set qadded(0) 1
    while {(![eof $file])} {
      set quotearray($anzahl) "$ficken"
      set ficken [string trim [gets $file]]
      set founded 0
      set i 0
      while {$i <= $quoteadders} {
          if {[lindex $ficken 0] == $qadder($i)} {
	     incr qadded($i)
	     set founded 1
	  }
	  incr i
      }
      if {$founded != 1} {
         incr quoteadders
	 set qadder($quoteadders) [lindex $ficken 0]
	 set qadded($quoteadders) 1
      }
      if {$ficken != ""} {incr anzahl
}
    }
    close $file
    putserv "PRIVMSG $quotechannel :\[4x]: Quote Database Stats\: "
    putserv "PRIVMSG $quotechannel :\[4x]: added quotes\: [expr $anzahl-1]"
    set quoteaddrs 0
    while {$quoteaddrs < $quoteadders} {
        putserv "PRIVMSG $quotechannel : $qadder($quoteaddrs) added $qadded($quoteaddrs) quotes"
        incr quoteaddrs
    }
    return 1
}

proc msg_sendquotes {nick uhost hand arg} {
    global quotechannel quotefile quotetime quotearray
    putserv "NOTICE $nick :\[4x]: sending the quote file..."
    dccsend $quotefile $nick
    putserv "NOTICE $nick :\[4x]: you should be recieving $quotefile."
    putlog "\[4x]: sending $quotefile to $nick"
    return 1
}
proc pub_timequote {} {
    global quotechannel quotefile quotetime quotearray
    set file [open $quotefile r]
    set anzahl 0
    set ficken [string trim [gets $file]]
    while {(![eof $file])} {
      set quotearray($anzahl) "$ficken"
       set ficken [string trim [gets $file]]
       incr anzahl
    }
    close $file
    putserv "PRIVMSG $quotechannel :[lrange $quotearray([rand $anzahl]) 1 end]"
    timer $quotetime pub_timequote
    return 1
}

proc quotelaunch {} {
  global quotechannel quotefile quotetime quotearray quotebackuptime
  putlog "[timers]"
  if {[string match *pub_timequote* [timers]]} {
    return 0
  } else {
  timer $quotetime pub_timequote
  }
  if {[string match *quotefilebackup* [timers]]} {
    return 0
  } else {
    timer $quotebackuptime quotefilebackup
  }
}

proc quotefilebackup { } {
	global quotebackup quoteftp quoteusername quotepassword quotefile quotebackuptime eggpath backuppath
	if {$quotebackup != 1} {return 0}
	putlog "\[4x]: backing up quote file"
	set ftpclient [exec which ftp]
	set wurstbaum [open "|$ftpclient -n $quoteftp" w]
	puts $wurstbaum "user $quoteusername $quotepassword"
	puts $wurstbaum "bin"
	puts $wurstbaum "put $eggpath/$quotefile $backuppath/$quotefile[unixtime]"
	puts $wurstbaum "quit"
	close $wurstbaum
        timer $quotebackuptime quotefilebackup
	return 1
}

quotelaunch

putlog "\[4x]: Utility, Quote v0.1 Loaded."
putlog "Quotes will be shown in $quotechannel every $quotetime minutes."
if {$quotebackup == 1} {putlog "A backup of the file $quotefile will be made every $quotebackuptime minutes on $quoteftp."}