########### XDCC THiNGiE 0.1 ############
# designed and programmed by FlyBoyUSA  #
# for original use in #emuroms Undernet #
#########################################
# Thanks for bass for helping me think the right way to approach
# getting rid of queueing files
# Also thanks to AshToreth for letting me use his XDCC script from
# Askrypt to get a idea of how the XDCC list and stuff should visualy look
#
# Any questions, comments, hate letters, etc goto flyboyus@modex.com
#
# Also available is PiNG THiNGiE, URL THiNGiE, XW LoGoN THiNGiE, and
# soon WoF(Wheel of Fortune) THiNGiE will be available.
#
# Features
#  - You use the eggdrops internal description command to set the
#    description of files for the xdcc script to use and you don't
#    have to make any changes to the script when you add files
#  - No queueing... so people don't keep xdcc getting files confusing
#    the bot
#    limit, instead of total dcc connections! I love this feature :-)
#  - Two different timer options for advertising the XDCC
#
# Tried getting it to allow linked files from other bots, but I can't
# figure out how to send w/o the person being on the filesystem
#
# One problem I did run into... I can get the gots in the file system for
# the eggdrop but couldn't figure a way to increase them effeciently
# If you have any idea (w/o including a modified module) that I can increase
# them (also w/o moving the files to the bot directory) then please e-mail me
# or contact me
#

#############
# Variables #
#############

# set this to a directory under files-path in the config
set xdccpath "xdcc/"

# set this to the maximum xdcc total gets and pending gets allowed
set xdcclimit 10

# if your config doesn't have the max-dloads variable set, set it
# below to designate how many gets per person
#set max-dloads 1

# set this variable to the channels you want the timer to notice
# seperate channels with a space
set xdccchans "#lamestchannel"

# for a full xdcc list every time the timer goes off, set this to 1 or
# set this to 0 or comment it outfor a two line timer advertising the 
# xdcc server
# (if you remove the binds below this then this command is useless)
set xdcctimer 0

# set the binds below or add your own depending on when you what the
# timer to notice the channels you have set
# syntax in the quotes is "min hour day month year"
# ex 20 * * * * would activate the notice in the channels every hour 20 past
#    *0 * * * * would activate the notice in the channels every 10 minutes
bind time - "20 * * * *" *xdcc:timer
bind time - "40 * * * *" *xdcc:timer
bind time - "00 * * * *" *xdcc:timer

# remove this line or your bot will die
die "Please edit your XDCC THiNGiE script!"

###################################################
# You do NOT have to edit anything below here and #
# should NOT unless you know what you are doing   #
###################################################

foreach item [getfiles $xdccpath] {
  if {[getlink $xdccpath $item] != ""} {
    if {[string match "* [string trimright [getlink $xdccpath $item] :] *" [bots]]} {putbot [string trimright [getlink $xdccpath $item] :] "getxdccsize [string trimleft [getlink $xdccpath $item]]"}
  }
}


bind bot - xdccsizeis *xdcc:sizeis
proc *xdcc:sizeis { bot com arg } {
  global xdcclink
  set xdcclink([lindex $arg 0]) "[lindex $arg 1]"
}

proc *xdcc:timer {min hour day month year} {
  global xdcclimit xdccpath botnick files-path xdccchans xdcctimer xdcclink
  if {$xdcctimer == 1} {
    set numb2 0
    while {[lindex $xdccchans $numb2] != ""} {
      puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002XDCC THiNGiE\002 - /CTCP $botnick XDCC SEND #<number>"
      set numb 0
      set totalsize 0
      while {[lindex [getfiles $xdccpath] $numb] != ""} {
        if {[getlink $xdccpath [lindex [getfiles $xdccpath] $numb]] != ""} {
          if {![info exists xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])]} {set xdccsize 0
          } {set xdccsize $xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])
            set totalsize [expr $totalsize + $xdccsize]
          }
        } {set xdccsize [string trimright [expr [file size ${files-path}$xdccpath[lindex [getfiles $xdccpath] $numb]] / 1024] .]
          set totalsize [expr $totalsize + $xdccsize]
        }
        if {$numb > 8} {puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002\[\002#[expr $numb + 1]\002\] \[ \002[getdesc $xdccpath [lindex [getfiles $xdccpath] $numb]] \002\] \[\002${xdccsize}k\002\]"
        } {puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002\[\002# [expr $numb + 1]\002\] \[ \002[getdesc $xdccpath [lindex [getfiles $xdccpath] $numb]] \002\] \[\002${xdccsize}k\002\]"
        }
        incr numb
      }
      append totalsize k
      puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002XDCC THiNGiE - \[\002[expr $numb] file(s) totaling $totalsize\002\] \[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\]"
      incr numb2
    }
    return 0
  } {set numb2 0
    while {[lindex $xdccchans $numb2] != ""} {
      puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002XDCC THiNGiE\002 - To get a list of available packets /CTCP $botnick XDCC LIST"
      set numb 0
      set totalsize 0
      while {[lindex [getfiles $xdccpath] $numb] != ""} {
        if {[getlink $xdccpath [lindex [getfiles $xdccpath] $numb]] != ""} {
          if {![info exists xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])]} {set xdccsize 0
          } {set xdccsize $xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])
            set totalsize [expr $totalsize + $xdccsize]
          }
        } {set xdccsize [string trimright [expr [file size ${files-path}$xdccpath[lindex [getfiles $xdccpath] $numb]] / 1024] .]
          set totalsize [expr $totalsize + $xdccsize]
        }
        incr numb
      }
      append totalsize k
      puthelp "PRIVMSG [lindex $xdccchans $numb2] :\002XDCC THiNGiE - \[\002[expr $numb] file(s) totaling $totalsize\002\] \[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\]"
      incr numb2
    }
    return 0
  }
}

bind ctcp - XDCC xdcc
proc xdcc { nick uhost hand dest keyword arg } {
  if {[string toupper [lindex $arg 0]] == "LIST"} {
    global xdcclimit xdccpath botnick files-path max-dloads
    puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - /CTCP $botnick XDCC SEND #<number>"
    set numb 0
    set totalsize 0
    while {[lindex [getfiles $xdccpath] $numb] != ""} {
      if {[getlink $xdccpath [lindex [getfiles $xdccpath] $numb]] != ""} {
        if {![info exists xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])]} {set xdccsize 0
        } {set xdccsize $xdcclink([string trimleft [getlink $xdccpath [lindex [getfiles $xdccpath] $numb]]])
          set totalsize [expr $totalsize + $xdccsize]
        }
      } {set xdccsize [string trimright [expr [file size ${files-path}$xdccpath[lindex [getfiles $xdccpath] $numb]] / 1024] .]
        set totalsize [expr $totalsize + $xdccsize]
      }
      if {$numb > 8} {puthelp "NOTICE $nick :\002\[\002#[expr $numb + 1]\002\] \[ \002[getdesc $xdccpath [lindex [getfiles $xdccpath] $numb]] \002\] \[\002${xdccsize}k\002\]"
      } {puthelp "NOTICE $nick :\002\[\002# [expr $numb + 1]\002\] \[ \002[getdesc $xdccpath [lindex [getfiles $xdccpath] $numb]] \002\] \[\002${xdccsize}k\002\]"
      }
      incr numb
    }
    append totalsize k
    set numb3 0
    foreach item [dcclist get] {
      if {[lindex $item 1] == $nick} {incr numb3}
    }
    foreach item [dcclist get_pending] {
      if {[lindex $item 1] == $nick} {incr numb3}
    }
    puthelp "NOTICE $nick :\002XDCC THiNGiE - \[\002 $numb file(s) totaling $totalsize \002\] \[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\] \[\002Your Gets $numb3/${max-dloads}\002\]"
    return 1
  } elseif {[string toupper [lindex $arg 0]] == "HELP"} {
    global botnick
    puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - XDCC File Server Commands:"
    puthelp "NOTICE $nick :\002/CTCP $botnick XDCC HELP\002 - This is it!"
    puthelp "NOTICE $nick :\002/CTCP $botnick XDCC LIST\002 - Lists the available files."
    puthelp "NOTICE $nick :\002/CTCP $botnick XDCC SEND #<number>\002 - Sends the specified packet."
    return 1
  } elseif {[string toupper [lindex $arg 0]] == "SEND"} {
    global xdcclimit xdccpath botnick files-path max-dloads admin
    if {$dest != "$botnick"} {return 1}
    set arg [expr [string trim [lindex $arg 1] #] - 1]
    if {[lindex [dcclist get] [expr $xdcclimit * 4]] == ""} {
      set numb 0
    foreach item [dcclist get] {
      if {[lindex $item 1] == $nick} {incr numb}
    }
    foreach item [dcclist get_pending] {
      if {[lindex $item 1] == $nick} {incr numb}
    }
      if {[expr [llength [dcclist get_pending]] + [llength [dcclist get]]] >= $xdcclimit} {
        puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - Too many total transfers going, try getting [getdesc $xdccpath [lindex [getfiles $xdccpath] $arg]] ([lindex [getfiles $xdccpath] $arg]) later... \002\[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\] \[\002Your Gets $numb/${max-dloads}\002\]"
        return 1
      } elseif {$numb >= ${max-dloads}} {
        puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - You have too many transfers going, try getting [getdesc $xdccpath [lindex [getfiles $xdccpath] $arg]] ([lindex [getfiles $xdccpath] $arg]) later... \002\[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\] \[\002Your Gets $numb/${max-dloads}\002\]"
        return 1
      } elseif {[lindex [getfiles $xdccpath] $arg] != ""} {
        switch [dccsend ${files-path}$xdccpath[lindex [getfiles $xdccpath] $arg] $nick] {
          0 {set numb 0
            foreach item [dcclist get] {
              if {[lindex $item 1] == $nick} {incr numb}
            }
            foreach item [dcclist get_pending] {
              if {[lindex $item 1] == $nick} {incr numb}
            }
            puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - Sending [getdesc $xdccpath [lindex [getfiles $xdccpath] $arg]] ([lindex [getfiles $xdccpath] $arg])... \002\[\002Send [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\] \[\002Your Gets $numb/${max-dloads}\002\]"
            return 1}
          1 {puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - DCC Table Full, unable to send!"
            putlog "DCC Table Full, unable to send file to $nick, contact $admin"
            return 1}
          2 {puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - Socket error!, contact $admin"
            putlog "Socket error in XDCC THiNGiE"
            return 1}
        }
      } {puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - Packet Number Invalid!  /ctcp $botnick LIST for a list"
      }
    }
  } {return 1
  }
}

bind sent - * *xdcc:dnld
proc *xdcc:dnld { hand nick path } {
  putlog "$path"
  global files-path xdccpath max-dloads xdcclimit
  set numb 0
  if {[string match "$path*" ${files-path}$xdccpath]} {
    foreach item [dcclist get] {
      if {[lindex $item 1] == $nick} {incr numb}
    }
    foreach item [dcclist get_pending] {
      if {[lindex $item 1] == $nick} {incr numb}
    }
    set xdccfile [trim ${files-path}$xdccpath $path]
    puthelp "NOTICE $nick :\002XDCC THiNGiE\002 - Enjoy [getdesc $xdccpath $xdccfile file] ($xdccfile):-) \002\[\002Sends [expr [llength [dcclist get_pending]] + [llength [dcclist get]]]/$xdcclimit\002\] \[\002Your Gets $numb/${max-dloads}\002\]"
    putlog "Successfuly sent packet [getdsec $xdccpath $xdccfile$xdccfile] ($xdccfile) to $nick"
  }
}

putlog "=__ XDCC THiNGiE 0.1 by FlyBoyUSA Loaded __="