# REQS 1.2 TCL for Channel Request List by [-Scorp-] (aka : Scorpion`)

# Revision History:
#   1.1 (May 10, 1999) - Added main options as variables
#   1.2 (July 2, 1999) - Minor bug fixes & added email contact

# [-Scorp-] can be contacted at : bikini.team@sweden.com
# If you edit this tcl , i would like the proper recognition
# Please Email me with any bugs or comments
# Also , in  the next few days , im working on editing this tcl so that to remove
# a Request "!reqs del <request#>" your nick will have to match that of the Requestor

# This TCL is designed to maintain a "Channel Request List"
# For use in MP3, Warez, or other channels.  Please change
# the few variables below for channel & bot options.

# Additions, deletions, and the list are maintained through 
# a public !reqs command, with certain arguments (see below).

# The script will run on your bot, and will only recognize
# public messages in that channel.  If the user tries !reqs via
# /msg, the bot will respond that it must be done publicly.

# There is also a template file that will be sent with a
# !reqs send command.  It is sent appended to the top of the
# requests file, in a .doc format.  Please note that it MUST be
# edited with a *nix editor such as pico.

# The commands it recognizes are as follows:
#
#   !reqs help [to get a list of available commands]
#   !reqs view [to see the list via msg]
#   !reqs send [for a .doc of the list]
#   !reqs add <Song Title-Artist> [to add to the list] (Must be +v or +o in the channel, not on the bot)
#   !reqs del <request#> [to delete a request] (Must be +o in the channel, not on the bot)

# This is the channel you wish the tcl to run in
set reqschan "#mp3-lamers"

# This is the file the request list should be maintained in
set reqsfile "./#mp3-lamers_request_list"

# This is the number of requests you want the bot to
# suggest people to limit their requests to
set reqnub "3"

# This is the template file you need to edit or change.
# It will be sent at the top of the .doc for a "!reqs send"
set tfile "./template"

# Set this to the msg that you want
# the bot to advertise
set admsg "Please help us fill requests, type !reqs view or !reqs help for the channel requests list. Feel free to add a request or 2. Thank You!!"

# Set this to how many minute intervals you want the bot to advertise the above message
# 30 minutes is recommended
set adtime "30"
##########################################

set reqsver "1.2"
proc chanmsg1 {} {
global admsg reqschan adtime
 puthelp "PRIVMSG $reqschan :$admsg"
 timer $adtime chanmsg1
}

if {![info exists timerloaded]} {
  putlog "timer started"
  timer 1 chanmsg1
  set timerloaded 1
}

bind pub - !reqs pubm:req
proc pubm:req {nick uhost hand chan arg} {
global reqsver reqsfile time date tfile reqschan reqnub
 set command [string tolower [lindex $arg 0]]
 set ops [isop $nick $chan]
 set vops [isvoice $nick $chan]
 set channel [string tolower $chan]
 if {$channel == "$reqschan"} {
  if {$command == "help" || $command == ""} {
      puthelp "PRIVMSG $nick :reqs.tcl version $reqsver by Scorpion` 1999"
      puthelp "PRIVMSG $nick :-------------------------------------------"
      puthelp "PRIVMSG $nick :All Commands are via the channel"
      puthelp "PRIVMSG $nick :USAGE: !reqs add Song Title-Artist :You Must Be +v or +o to add requests"
      puthelp "PRIVMSG $nick :USAGE: !reqs del <number of the request> :You Must Be +o to delete requests"
      puthelp "PRIVMSG $nick :USAGE: !reqs view <to see the list via msg>"
      puthelp "PRIVMSG $nick :USAGE: !reqs help <this help msg>"
      puthelp "PRIVMSG $nick :USAGE: !reqs send <for a .doc of the list to keep>"
      puthelp "PRIVMSG $nick :All Commands are via the channel"
      puthelp "PRIVMSG $nick :-------------------------------------------"
      putlog "#$nick# asked for the help file"
      return 0
  }
  if {$command == "add" && ($ops || $vops)} {
    set reqs [lrange $arg 1 end]
    if {$reqs == ""} {
        puthelp "PRIVMSG $nick :USAGE: !reqs add Song Title-Artist"
        return 0
    }
    if {[file exist $reqsfile]} {set file [open $reqsfile a]} else {set file [open $reqsfile w]}
    set time1 [time]
    set date1 [date]
    puts $file "$reqs , was requested by $nick on $date1 at $time1"
    close $file
    puthelp "PRIVMSG $nick :$reqs added to the reqs file."
    puthelp "PRIVMSG $nick :Please Limit your Requests to $reqnub Request(s). Thank You"
    putcmdlog "#$nick#  added  the song  : $reqs ..."
    return 0
  }
  if {$command == "add" && !($ops || $vops)} {
    puthelp "PRIVMSG $nick :You must be either +o or +v to add a request, sorry"
    return 0
  }
  if {$command == "del" && ($ops)} {
      set numbofreqs [lindex $arg 1]
      catch {set poi [expr $numbofreqs + 2]}
      if {($numbofreqs == "" || [string tolower $numbofreqs] == "help") && [info exist poi]} {
          puthelp "PRIVMSG $nick :USAGE: !reqs del number of the request"
          return 0
      }
      putlog "#$nick# : is Removing $numbofreqs from $reqsfile"
      if {![file exist $reqsfile]} {
          puthelp "PRIVMSG $nick :$reqsfile doesnt exist!"
          return 0
      }
      set count 0
      set open [open $reqsfile r]
      set bah ""
      while {![eof $open] && $bah == ""} {
        set poi [gets $open]
        if {[lindex $poi 0] != $numbofreqs && $poi != ""} {
            incr count
        }
        if {$count == $numbofreqs} {
            puthelp "PRIVMSG $nick :removing : $poi"
            set bah "$poi"
        }
      }
      close $open
      exec cp $reqsfile $reqsfile.bak
      set doc [open $reqsfile.bak r]
      set khg [open $reqsfile w]
      set tribz ""
      set count 0
      while {![eof $doc]} {
        set poi [gets $doc]
        if {$poi != ""} {
            incr count
        }
        if {$count == $numbofreqs} {
          putlog "#$nick# removed $poi"
        }
        if {$count != $numbofreqs} {
            puts $khg "$poi"
        }
      }
      close $doc
      close $khg
      return 0
  }
  if {$command == "del" && !($ops)} {
    puthelp "PRIVMSG $nick :You must be +o to remove a request, sorry. ask an op to help you"
    return 0
  }
  if {$command == "view"} {
      if {![file exist $reqsfile]} {
          puthelp "PRIVMSG $nick :There are no requests stored, yet"
          return 0
      }
      set input [open $reqsfile r]
      puthelp "PRIVMSG $nick :Thank you for viewing the $reqschan request list"
      puthelp "PRIVMSG $nick :Please try and help us fill these requests"
      puthelp "PRIVMSG $nick :Use !reqs add Song name-Artist to add a request, you must be +o or +v to Add and Delete Requests"
      set count 1
      while {![eof $input]} {
        set poi [gets $input]
        if {[lindex $poi 0] != "" } {
            puthelp "PRIVMSG $nick :#$count, $poi"
            incr count
        }
      }
      if {$count == 1 } {
          puthelp "PRIVMSG $nick :There are no requests stored, yet"
      } else {
          puthelp "PRIVMSG $nick :End of file. If you can fill a request, please upload it to an op"
      }
      close $input
      putcmdlog "#$nick# asked for !reqs view and was sent [expr $count -1] requests"
      return 0
  }
  if {$command == "send"} {
      set tpl [open $tfile r]
      set doc [open $reqsfile r]
      set khg [open $reqsfile.doc w]
      set tribz ""
      while {![eof $tpl]} {
        set dnd [gets $tpl]
        if {$dnd != ""} {
            puts $khg "$dnd"
        }
      }
      close $tpl
      set tribz ""
      set count 1 
      while {![eof $doc]} {
        set poi [gets $doc]
        if {$poi != ""} {
            puts $khg "#$count , $poi"
            incr count
        }
      }
      close $doc
      close $khg
      putlog "#$nick# is getting $reqsfile.doc"
      dccsend $reqsfile.doc $nick
      return 0
  }
 }
}

bind msg - !reqs in_chan
proc in_chan {nick uhost hand arg} {
  puthelp "PRIVMSG $nick :You Must use the channel for the requests, msg will Not work. thank you"
}

putlog "!reqs.tcl by \[-Scorp-\] 1999 version $reqsver loaded."
