# Crazy ass external command handler for eggdrop bots v2
# By The Almighty Pegasus Epsilon <pegasus@pimpninjas.org>
#
# Distribute Unmodified.
# http://pegasus.pimpninjas.org/license
#
# WARNING WARNING WARNING
# vvvvvvv vvvvvvv vvvvvvv
# This version uses a new API, which allows the users to send RAW IRC
# COMMANDS. Trust your users, or patch the multiplexer.
# ^^^^^^^ ^^^^^^^ ^^^^^^^
# WARNING WARNING WARNING
#
# loads, binds, and handles a directory full of other scripts, as
# filename.ext -> !filename
namespace eval external2 {
set users {pegasus sev lagshot}
set scripts {}
set user {}
foreach user $users {
if ![catch {glob /home/$user/$::username/external/*} temp] {
set scripts "$scripts $temp"
}
}
unset user
foreach script $scripts {
if ![file isdirectory $script] {
set command [string range $script [expr [string last "/" $script]+1] [expr [string first "." $script]-1]]
proc $command-public {
nick uhost hand chan arg
} "multiplex $command \$nick \$uhost \$hand \$chan \$arg"
bind pub - !$command [ namespace code $command-public ]
proc $command-private {
nick uhost hand arg
} "multiplex $command \$nick \$uhost \$hand \$nick \$arg"
bind msg - $command [ namespace code $command-private ]
putlog "Enabled !$command."
}
}
proc external {hand dest arg} {
variable scripts
variable users
set user {}
foreach user $users {
if {$hand == $user} {
set admin {}
}
}
set cmd [lindex $arg 0]
set arg [lrange $arg 1 end]
switch $cmd {
reload {
unset user
if [info exists admin] {
unset admin
namespace eval [namespace parent] "
namespace delete [ namespace current ]
source external2.tcl
"
puthelp "PRIVMSG $dest :Right-half of my brain reloaded."
} {
puthelp "PRIVMSG $dest :Sorry, only pegasus, sev, and lagshot are allowed to mess with my head."
}
}
unload {
if [info exists admin] {
unbind pub - !ext [ namespace code public ]
namespace delete [ namespace current ]
puthelp "PRIVMSG $dest :I have become less interesting."
}
}
list {
set commands {}
foreach command $scripts {
if ![file isdirectory $command] {
set commands "$commands ![file rootname [file tail $command]]"
}
}
puthelp "PRIVMSG $dest :External commands available: [lsort -unique $commands]"
unset commands
}
owner {
set command [string trimleft $arg "!"]
foreach user $users {
if ![catch {glob /home/$user/$::username/external/$command.*} external] {
puthelp "PRIVMSG $dest :$arg is owned by $user."
break
}
}
}
default { puthelp "PRIVMSG $dest :Valid !x2 commands are reload, unload, list, and owner." }
}
}
proc public { nick uhost hand chan arg } { external $hand $chan $arg }
bind pub - !x2 [ namespace code public ]
proc multiplex { command nick uhost hand dest arg } {
variable users
set external {}
foreach user $users {
if ![catch {glob /home/$user/$::username/external/$command.*} external] {
break
}
}
set ::env(TCLAPI_NICK) $nick
set ::env(TCLAPI_UHOST) $uhost
set ::env(TCLAPI_HAND) $hand
set ::env(TCLAPI_DEST) $dest
set ::env(TCLAPI_ARG) $arg
if ![catch {exec $external} result] {
foreach line [split $result "\n"] {
# old api
putlog "$line"
# puthelp "PRIVMSG $dest :$line"
puthelp "$line"
}
} {
puthelp "PRIVMSG $dest :External script error. Disabling !$command."
putlog "Disabling $command: $result"
unbind pub - !$command [ namespace code "$command-handler" ]
rename $command-handler {}
}
unset ::env(TCLAPI_NICK)
unset ::env(TCLAPI_UHOST)
unset ::env(TCLAPI_HAND)
unset ::env(TCLAPI_DEST)
unset ::env(TCLAPI_ARG)
}
}
putlog "External command handler v2 loaded."