####################################################
# by wiebe @ QuakeNet
#
# tcl:tcl proc is from tcldebug.tcl by nevermore, #tcl on QuakeNet
# http://tclhelp.net/files/tcldebug.tcl.txt
####################################################


####################################################
# tcl:help:pubm
####################################################
bind pubm n "% ${botnet-nick} help tcl" tcl:help:pubm
proc tcl:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "tcl: usage tcl <command>"
  lappend o "tcl: executes the specified Tcl command. see doc/tcl-commands.doc for details on Tcl commands added to Eggdrop, and visit http://tcl.activestate.com/ for more information on Tcl."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "tcl: " } }
  putloglev c $c "help: $n $u $h $c tcl"
  return 1
}


####################################################
# tcl:help:msgm
####################################################
bind msgm n "help tcl" tcl:help:msgm
proc tcl:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "tcl: usage tcl <command>"
  lappend o "tcl: executes the specified Tcl command. see doc/tcl-commands.doc for details on Tcl commands added to Eggdrop, and visit http://tcl.activestate.com/ for more information on Tcl."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "tcl: " } }
  putcmdlog "($n!$u) !$h! help tcl"
  return 1
}


####################################################
# tcl:pubm
####################################################
bind pubm n "% ${botnet-nick} tcl" tcl:pubm
bind pubm n "% ${botnet-nick} tcl *" tcl:pubm
proc tcl:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {![validchan $c]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "tcl: usage tcl <command>"
  } elseif {[string equal [lsearch [string tolower $::owner] [string tolower $h]] -1]} {
    lappend o "tcl: you must be a permanent owner to access this command."
  } else {
    set m [lindex [split [getchanmode $c]] 0]; set o [tcl:tcl $n $u $h $c $t ""]
    if {[validchan $c] && (![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c])} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { putserv "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l putserv "tcl: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "tcl: " } }
  putloglev c $c "tcl: $n $u $h $c $t"
  return 1
}


####################################################
# tcl:pub
####################################################
bind pub n "*tcl" tcl:pub
proc tcl:pub { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} {
    lappend o "tcl: usage tcl <command>"
  } elseif {[string equal [lsearch [string tolower $::owner] [string tolower $h]] -1]} {
    lappend o "tcl: you must be a permanent owner to access this command."
  } else {
    set m [lindex [split [getchanmode $c]] 0]; set o [tcl:tcl $n $u $h $c $t ""]
    if {[validchan $c] && (![string match *m* $m] || [botisop $c] || [botishalfop $c] || [botisvoice $c])} {
      if {[string match *c* $m] } { set o [stripcodes bcru $o] }
      if {[string equal [info procs privmsg] ""]} { foreach l $o { putserv "PRIVMSG $c :$l" }
      } else { foreach l $o { privmsg $c $l putserv "tcl: " } }
      set o ""
    }
  }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "tcl: " } }
  return 1
}


####################################################
# tcl:msg
####################################################
bind msg n tcl tcl:msg
proc tcl:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} {
    lappend o "tcl: usage tcl <command>"
  } elseif {[string equal [lsearch [string tolower $::owner] [string tolower $h]] -1]} {
    lappend o "tcl: you must be a permanent owner to access this command."
  } else { set o [tcl:tcl $n $u $h "" $t ""] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "tcl: " } }
  return 1
}


####################################################
# tcl:dcc
####################################################
bind dcc n tcl tcl:dcc
proc tcl:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {[string equal $t ""]} {
    lappend o "tcl: usage tcl <command>"
    lappend o "tcl: executes the specified Tcl command. see doc/tcl-commands.doc for details on Tcl commands added to Eggdrop, and visit http://tcl.activestate.com/ for more information on Tcl."
  } elseif {[string equal [lsearch [string tolower $::owner] [string tolower $h]] -1]} {
    lappend o "tcl: you must be a permanent owner to access this command."
  } else { set o [tcl:tcl "" "" $h $c $t $i] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# tcl:tcl
# from http://tclhelp.net/files/tcldebug.tcl.txt
####################################################
proc tcl:tcl { nick host hand chan text idx } {
  set uhost $host; set handle $hand; set start [clock clicks]
  if {[string equal $nick ""]} { unset nick; unset host; unset uhost }
  if {[string equal $chan ""]} { unset chan }
  if {[string equal $idx ""]} { unset idx }
  set errnum [catch {eval $text} error]; set end [clock clicks]
  if {$error==""} {set error "<empty string>"}
  switch -- $errnum {
     0 {set error "ok: $error"}
     4 {set error "continue: $error"}
     3 {set error "break: $error"}
     2 {set error "return: $error"}
     1 {set error "error: $error"}
     default {set error "$errnum: $error"}
  }
  set error "$error - [expr ($end-$start)/1000.0] ms"
  set o [split $error "\n"]
  return $o
}


set scriptdb(tcl) {
  "provides tcl command for executing tcl commands"
}

