#
# dZSbot.tcl
#

#################################################################################
# MAIN LOOP - PARSES DATA FROM GLFTPD.LOG                                       #
#################################################################################
proc readlog {} {
    global location lastoct disable defaultsection variables msgtypes chanlist dZStimer

    set dZStimer [utimer 1 "readlog"]

    set glftpdlogsize [file size $location(GLLOG)]

    if {$glftpdlogsize == $lastoct} { return 0 }
    if {$glftpdlogsize  < $lastoct} { set lastoct 0 }
    if {[catch {set of [open $location(GLLOG) r]} ]} { return 0 }

    seek $of $lastoct
    while {![eof $of]} {
        set line [gets $of]
        if {$line == ""} {continue}
        set msgtype [string trim [lindex $line 5] ":"]
        set path [lindex $line 6]

        if {![string compare $msgtype "INVITE"]} {
            set nick [lindex $line 6]
            foreach channel $chanlist(INVITE) { puthelp "INVITE $nick $channel" }
        }

        set section [getsection $path $msgtype]
        if {[denycheck "$path"] == 0} {
            if {[string compare "$section" "$defaultsection"]} {
                if {$disable($msgtype) == 0 || $disable(DEFAULT) == 0} {
                    if {[info exists variables($msgtype)]} {
                        set echoline [parse $msgtype [lrange $line 6 end] $section]
                        sndall $section $echoline
                        postcmd $msgtype $section $path
                    } else {
                        set echoline [parse DEFAULT [lrange $line 6 end] $section]
                        sndall $section $echoline
                        postcmd $msgtype $section $path
                    }
                }
            } else {
                if {[lsearch -glob $msgtypes(DEFAULT) $msgtype] != -1} {
                    if {$disable($msgtype) == 0} {
                        set echoline [parse $msgtype [lrange $line 6 end] "DEFAULT"]
                        sndall "DEFAULT" $echoline
                        postcmd $msgtype "DEFAULT" $path
                    }
                } else {
                    if {$disable(DEFAULT) == 0} {
                        set echoline [parse $msgtype [lrange $line 6 end] "DEFAULT"]
                        sndall "DEFAULT" $echoline
                        postcmd $msgtype "DEFAULT" $path
                    }
                }
            }
        }
    }

    close $of
    set lastoct [file size $location(GLLOG)]
    launchnuke
    return 0
}
#################################################################################

#################################################################################
# POST COMMAND                                                                  #
#################################################################################
proc postcmd {msgtype section path} {
    global postcommand

    if {[info exists postcommand($msgtype)]} {
        foreach cmd $postcommand($msgtype) {
            if {[catch {$cmd $msgtype $section $path} result] != 0} {
                putlog "dZSbot error: $cmd caused an error - $result"
            }
        }
    }
}










#
# dZSbconf.tcl
#

## RUNNING POST COMMANDS
#
# Example:
#  set postcommand(NEWDIR)       "imdb:check some:other:cmd"
#   TYPE -------------^
#             COMMANDS -------------^
#
# Notes:
#  Any process you list here must be waiting to accept 3 parameter
#
#  1st - message type, eg: NEWDIR
#  2nd - section name, eg: 0DAY
#  3rd - path to release, eg: /site/incoming/0day

set postcommand(NEWDIR)        "misc:gfaq:search"
set postcommand(DELDIR)        ""
set postcommand(NUKE)          ""
set postcommand(UNNUKE)        ""
set postcommand(DEFAULT)       ""
set postcommand(COMPLETE)      "misc:rominfo"
