Pages

Sunday, August 28, 2011

hostspy.tcl


##############
## COMMANDS ##
##################################################
## DCC ## .hostspy <command> (Can be changed)    ##
######### See .hostspy help for a command list.    ##
##################################################

##########################################################
## Just load the script, edit the settings, and rehash. ##
##########################################################

###############################################
# Log and database files will be stored here. #
###############################################

set hostspy_setting(logdir) "./logs/"

#######################################################
# Set the main dcc command for using the script here. #
#######################################################

set hostspy_setting(cmd) "hostspy"

####################################################
# Set the flag required for using the script here. #
####################################################

set hostspy_setting(flag) "+H"

###################################
# Enable use of bold in DCC chat? #
###################################

set hostspy_setting(bold) 1

###########################################
# Prefix "HOSTSPY:" in DCC chat messages? #
###########################################

set hostspy_setting(HOSTSPY:) 1

####################
# Code begins here #
####################

if {![string match 1.6.* $version]} { putlog "\002HOSTSPY:\002 \002WARNING:\002 This script is intended to run on eggdrop 1.6.x or later." }
if {[info tclversion] < 8.2} { putlog "\002HOSTSPY:\002 \002WARNING:\002 This script is intended to run on Tcl Version 8.2 or later." }

foreach bt [list msgm pubm notc join part sign topc kick nick mode ctcp ctcr flud] { bind $bt - * hostspy_chk$bt }
foreach bt [list 600 601 604 605] { bind raw - $bt hostspy_watch }
bind dcc $hostspy_setting(flag) $hostspy_setting(cmd) hostspy_hostspy
bind evnt - init-server hostspy_connect_watch

proc hostspy_dopre {} {
    if {!$::hostspy_setting(HOSTSPY:)} { return "" }
    if {!$::hostspy_setting(bold)} { return "HOSTSPY: " }
    return "\002HOSTSPY:\002 "
}
proc hostspy_cm {pattern string} { return [string match $pattern $string] }
proc hostspy_ncm {pattern string} { return [hostspy_cm [string tolower $pattern] [string tolower $string]] }
proc hostspy_readarray {array file} {
    upvar $array ours
    if {[catch {open $file r} fd]} { return }
    if {[array exists ours]} { unset ours }
    foreach line [split [read $fd] \n] {
        if {[string range $line 0 2] != "%!%" || [scan [string range $line 3 end] {%[^!]!!!%s} name data] != 2} { continue }
        if {![info exists data]} { set data "" }
        set ours($name) $data
    }
    close $fd
}
proc hostspy_savearray {array file} {
    upvar $array ours
    if {![array exists ours] || [catch {open $file w} fd]} { return }
    foreach entry [array names ours] { puts $fd "%!%$entry!!!$ours($entry)" }
    close $fd
}
proc hostspy_putdcc {idx text} { putdcc $idx "[hostspy_dopre]$text" }
if {![array exists hostspy_spy] && [file exists ${hostspy_setting(logdir)}spy.db]} { hostspy_readarray hostspy_spy ${hostspy_setting(logdir)}spy.db }
if {![array exists hostspy_watch] && [file exists ${hostspy_setting(logdir)}watch.db]} { hostspy_readarray hostspy_watch ${hostspy_setting(logdir)}watch.db }
proc hostspy_hostspy {hand idx text} {
    global hostspy_watch hostspy_spy
    set cmd [lindex [split $text] 0]
    set arg1 [lindex [split $text] 1]
    set arg2 [lindex [split $text] 2]
    if {[string compare $cmd ""] == 0 || ([hostspy_ncm help $cmd] && [string compare $arg1 ""] == 0)} {
        hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) <command>"
        hostspy_putdcc $idx "Commands:"
        hostspy_putdcc $idx "AddSpy - Adds a host to the spy list."
        hostspy_putdcc $idx "AddWatch - Adds a nick to the watch list."
        hostspy_putdcc $idx "ClearSpy - Clears the spy list."
        hostspy_putdcc $idx "ClearWatch - Clears the watch list."
        hostspy_putdcc $idx "DelSpy - Deletes a channel from the database."
        hostspy_putdcc $idx "DelWatch - Deletes a nick from the database."
        hostspy_putdcc $idx "ListSpy - Displays the spy list."
        hostspy_putdcc $idx "ListWatch - Displays the watch list."
        hostspy_putdcc $idx "See .$::hostspy_setting(cmd) help <command> for more information."
    } elseif {[hostspy_ncm help $cmd]} {
        if {[hostspy_ncm addspy $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) AddSpy <nick!user@host>"
            hostspy_putdcc $idx "This command adds a host to the spy list."
        } elseif {[hostspy_ncm addwatch $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) AddWatch <nick>"
            hostspy_putdcc $idx "This command adds a nick to the watch list."
        } elseif {[hostspy_ncm clearspy $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) ClearSpy"
            hostspy_putdcc $idx "This command clears the spy list."
        } elseif {[hostspy_ncm clearwatch $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) ClearWatch"
            hostspy_putdcc $idx "This command clears the watch list."
        } elseif {[hostspy_ncm delspy $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) DelSpy <nick!user@host>"
            hostspy_putdcc $idx "This command deletes a host from the spy list."
        } elseif {[hostspy_ncm delwatch $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) DelWatch <nick>"
            hostspy_putdcc $idx "This command deletes a nick from the watch list."
        } elseif {[hostspy_ncm listspy $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) ListSpy"
            hostspy_putdcc $idx "This command displays the spy list."
        } elseif {[hostspy_ncm listwatch $arg1]} {
            hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) ListWatch"
            hostspy_putdcc $idx "This command displays the watch list."
        } {
            hostspy_putdcc $idx "No help is available for that command."
        }
    } elseif {[hostspy_ncm addspy $cmd]} {
        if {[string compare $arg1 ""] == 0} { hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) AddSpy <nick!user@host>" ; return }
        if {[info exists hostspy_spy([string tolower $arg1])]} { hostspy_putdcc $idx "$arg1 already exists on the spy list." ; return }
        set hostspy_spy([string tolower $arg1]) [string tolower $arg1]
        hostspy_savearray hostspy_spy ${::hostspy_setting(logdir)}spy.db
        hostspy_putdcc $idx "Now logging events from $arg1 to ${::hostspy_setting(logdir)}hostspy.log."
    } elseif {[hostspy_ncm addwatch $cmd]} {
        if {[string compare $arg1 ""] == 0} { hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) AddWatch <nick>" ; return }
        if {[info exists hostspy_watch([string tolower $arg1])]} { hostspy_putdcc $idx "$arg1 already exists on the watch list." ; return }
        putserv "WATCH +$arg1"
        set hostspy_watch([string tolower $arg1]) [string tolower $arg1]
        hostspy_savearray hostspy_watch ${::hostspy_setting(logdir)}watch.db
        hostspy_putdcc $idx "Now logging connects and disconnects from $arg1 to ${::hostspy_setting(logdir)}hostspy.log."
    } elseif {[hostspy_ncm clearspy $cmd]} {
        if {[array size hostspy_spy] == 0} { hostspy_putdcc $idx "There are no users on the spy list." ; return }
        foreach g [array names hostspy_spy] { hostspy_putdcc $idx "$hostspy_spy($g) has been removed from the spy list." ; unset hostspy_spy($g) }
    } elseif {[hostspy_ncm clearwatch $cmd]} {
        if {[array size hostspy_watch] == 0} { hostspy_putdcc $idx "There are no users on the watch list." ; return }
        foreach g [array names hostspy_watch] {
            putserv "WATCH -$hostspy_watch($g)"
            hostspy_putdcc $idx "$hostspy_watch($g) has been removed from the watch list."
            unset hostspy_watch($g)
        }
        hostspy_savearray hostspy_watch ${::hostspy_setting(logdir)}watch.db
    } elseif {[hostspy_ncm delspy $cmd]} {
        if {[string compare $arg1 ""] == 0} { hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) DelSpy <nick!user@host>" ; return }
        if {![info exists hostspy_spy([string tolower $arg1])]} { hostspy_putdcc $idx "$arg1 does not exist on the spy list." ; return }
        foreach g [array names hostspy_spy] {
            if {[hostspy_ncm $arg1 $hostspy_spy($g)]} { hostspy_putdcc $idx "$hostspy_spy($g) has been removed from the spy list." ; unset hostspy_spy($g) }
        }
        hostspy_savearray hostspy_spy ${::hostspy_setting(logdir)}spy.db
    } elseif {[hostspy_ncm delwatch $cmd]} {
        if {[string compare $arg1 ""] == 0} { hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) DelWatch <nick>" ; return }
        if {![info exists hostspy_watch($arg1)]} { hostspy_putdcc $idx "$arg1 does not exist on the watch list." ; return }
        foreach g [array names hostspy_watch] {
            if {![hostspy_ncm $arg1 $hostspy_watch($g)]} { continue }
            putquick "WATCH -$hostspy_watch($g)"
            hostspy_putdcc $idx "$hostspy_watch($g) has been removed from the watch list."
            unset hostspy_watch($g)
        }
        hostspy_savearray hostspy_watch ${::hostspy_setting(logdir)}watch.db
    } elseif {[hostspy_ncm listspy $cmd]} {
        if {[array size hostspy_spy] == 0 || ![array exists hostspy_spy]} { hostspy_putdcc $idx "There are no users on the spy list." ; return }
        hostspy_putdcc $idx "Spy list:"
        foreach g [array names hostspy_spy] { hostspy_putdcc $idx "Spy List: $hostspy_spy($g)" }
        hostspy_putdcc $idx "End of spy list."
    } elseif {[hostspy_ncm listwatch $cmd]} {
        if {[array size hostspy_watch] == 0 || ![array exists hostspy_spy]} { hostspy_putdcc $idx "There are no users on the watch list." ; return }
        hostspy_putdcc $idx "Watch list:"
        foreach g [array names hostspy_watch] { hostspy_putdcc $idx "Watch List: $hostspy_watch($g)" }
        hostspy_putdcc $idx "End of watch list."
    } {
        hostspy_putdcc $idx "Usage: .$::hostspy_setting(cmd) <command>"
        hostspy_putdcc $idx "See .$::hostspy_setting(cmd) help for a command list."
    }
}
proc hostspy_add_event {g text} {
    global hostspy_setting hostspy_spy
    if {[catch {open ${hostspy_setting(logdir)}hostspy.log a} fd]} {  putlog "[hostspy_dopre]Error: ${hostspy_setting(logdir)}hostspy.log could not be opened." ; return }
    puts $fd "\[[clock format [clock seconds] -format "%D"]\] \[[clock format [clock seconds] -format "%I:%M:%S %p"]\] $text"
    close $fd
}
proc hostspy_watch {from keyword text} {
    foreach g [array names ::hostspy_watch] {
        if {[hostspy_ncm $::hostspy_watch($g) [join [lrange [split $text] 1 1]]]} { hostspy_add_event $g "O[expr {([regexp "600|604" $keyword])?"n":"ff"}]line: [join [lrange [split $text] 1 1]]![join [lrange [split $text] 2 2]]@[join [lrange [split $text] 3 3]]" }
    }
}
proc hostspy_chkmsgm {nick uhost hand text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Private Message: ${nick}!$uhost Text: $text" }
    }
}
proc hostspy_chkpubm {nick uhost hand chan text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Public Text: ${nick}!$uhost Chan: $chan Text: $text" }
    }
}
proc hostspy_chknotc {nick uhost hand text dest} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Notice: ${nick}!$uhost Destination: $dest Text: $text" }
    }
}
proc hostspy_chkjoin {nick uhost hand chan} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Join: ${nick}!$uhost Chan: $chan" }
    }
}
proc hostspy_chkpart {nick uhost hand chan msg} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Part: ${nick}!$uhost Chan: $chan Part Message: $msg" }
    }
}
proc hostspy_chksign {nick uhost hand chan text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Quit: ${nick}!$uhost Chan: $chan Quit Message: $text" }
    }
}
proc hostspy_chktopc {nick uhost hand chan text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Topic Change: ${nick}!$uhost Chan: $chan New Topic: $text" }
    }
}
proc hostspy_chkkick {nick uhost hand chan target text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Kick: ${nick}!$uhost Chan: $chan Target: $target Kick Message: $text" }
    }
}
proc hostspy_chknick {nick uhost hand chan newnick} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Nick Change: ${nick}!$uhost Chan: $chan New Nick: $newnick" }
    }
}
proc hostspy_chkmode {nick uhost hand chan mode vict} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Mode Change: ${nick}!$uhost Chan: $chan Modes: $mode $vict" }
    }
}
proc hostspy_chkctcp {nick uhost hand dest keyword text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "CTCP: ${nick}!$uhost Destination: $dest Keyword: $keyword Text: $text" }
    }
}
proc hostspy_chkctcr {nick uhost hand dest keyword text} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "CTCR: ${nick}!$uhost Destination: $dest Keyword: $keyword Text: $text" }
    }
}
proc hostspy_chkflud {nick uhost hand type chan} {
    foreach g [array names ::hostspy_spy] {
        if {[hostspy_ncm $::hostspy_spy($g) $nick!$uhost]} { hostspy_add_event $g "Flood Trigger: ${nick}!$uhost Chan: $chan Type: $type" }
    }
}
proc hostspy_connect_watch {t} {
    foreach g [array names ::hostspy_watch] { puthelp "WATCH +$g" }
}
putlog "\002HOSTSPY:\002 HostSpy.tcl Version 1.8 by Wcc is loaded."

No comments:

Post a Comment