#!/usr/local/bin/tclsh8.6

#
# Display inactive addresses
#
# History
#   2010/09/16 : jean     : design
#   2010/12/11 : pda      : i18n
#   2010/12/26 : pda      : use cgi-dispatch
#

#
# Template pages used by this script
#

set conf(page)          ipinact.html

#
# Next actions
#

set conf(next)		"ipinact"
set conf(nextmac)	"mac"
set conf(nextdel)	"del"

#
# Script parameters
#

set conf(occur)		{
			    {6 {More than 6 months}}
			    {9 {More than 9 months}}
			    {12 {More than one year}}
			}

set conf(sqldateformat) {'DD/MM/YYYY HH24:MI:SS'}

set conf(tableau) {
    global {
        chars {10 normal}
        columns {30 30 40}
        botbar {yes}
        align {left}
    }
    pattern Title {
        title {yes}
        topbar {yes}
        chars {bold}
        vbar {yes}
        column { }
        vbar {yes}
        column { }
        vbar {yes}
        column { }
        vbar {yes}
    }
    pattern Normal {
        vbar {yes}
        column {
            format {raw}
	}
        vbar {yes}
        column {
            format {raw}
	}
        vbar {yes}
        column { }
    }
}

#
# Netmagis general library
#

source /usr/local/lib/netmagis/libnetmagis.tcl

# ::webapp::cgidebug ; exit

##############################################################################
# Utility functions
##############################################################################

proc get-host-name {dbfd addr} {
    set sql "SELECT rr.name || '.' || domain.name AS name
			FROM dns.rr, dns.rr_ip, dns.domain
			WHERE rr.idrr = rr_ip.idrr
			    AND rr.iddom = domain.iddom
			    AND rr_ip.addr = '$addr'
			"
    set name ""
    pg_select $dbfd $sql tab {
	set name $tab(name)
    }
    return $name
}

##############################################################################
# Search for inactive IP addresses
##############################################################################

d cgi-register {} {
    {reseau	    0 1}
    {occurrence	    0 1}
} {
    global conf

    #
    # Open MAC database
    #

    set conninfo [get-conninfo "macdb"]
    if {[catch {set dbfdmac [pg_connect -conninfo $conninfo]} msg]} then {
        d error $msg
    }

    set tableau ""
    set selnet {}
    set seloccur {}
    set lnet [read-networks $dbfd $tabuid(idgrp) "consult"]

    set loccur {}
    foreach o $conf(occur) {
	lassign $o duration text
	lappend loccur [list $duration [mc $text]]
    }

    #
    # Is a network selected?
    #

    if {$reseau ne ""} then {
	#
	# Check permissions on network
	#

        set cidr [check-netid $dbfd $reseau $tabuid(idgrp) "consult" 4 msg]
        if {[llength $cidr] == 0} then {
              d error $msg
        }
	set selnet [lsearch -glob $lnet "$reseau *"]
	if {$selnet == -1} {
              d error [mc "You cannot read this network"]
	}

	#
	# Check occurrence
	#

	set seloccur [lsearch -exact -index 0 $loccur $occurrence]
	if {$seloccur == -1} {
              d error [mc "Invalid occurrence '%s'" $occurrence]
	}

	#
	# Build next action (for delete button)
	#

	set nextprog "ipinact"
	set nextargs "reseau=$reseau&occurrence=$occurrence"

	#
	# Build array
	#

	set lines {}
	lappend lines [list "Title" \
				[mc "IP address"] \
				[mc "Name"] \
				[mc "Last occurrence"] \
			    ]

	set datefmt $conf(sqldateformat)
	set sql "SELECT ip, to_char (most_recent_date,$datefmt) AS lastdate
		     FROM mac.ipmac2
		     WHERE ip <<= '$cidr' AND
			   most_recent_date < now() - interval '$occurrence month'
		     ORDER BY ip"
	set naddr 0
        pg_select $dbfdmac $sql tab {
	    set ip	    $tab(ip)
            set lastdate    $tab(lastdate)
	    set name [get-host-name $dbfd $ip]
	    #
	    # Don't look for host not in database
	    #
	    if {$name ne ""} then {
		d urlset "" $conf(nextmac) [list [list "recherche" $ip]]
		set url [d urlget ""]
		set addr [::webapp::helem "a" $ip "href" $url]

		set t [mc "Delete"]
		d urlset "" $conf(nextdel) [list \
						    [list "action" "del-ip"] \
						    [list "confirm" "no"] \
						    [list "addr" $ip] \
						]
		d urlsetnext "" $nextprog $nextargs
		set url [d urlget ""]
		set del [::webapp::helem "a" "\[$t\]" "href" $url]

		lappend lines [list Normal $addr "$name $del" $lastdate]
		incr naddr
	    }
	}

	set tableau "<BR>" 
	append tableau [mc {Inactive hosts in network %1$s since more than %2$s months: } $cidr $occurrence]
	if {$naddr >0 } then {
	    append tableau [mc "%s inactive hosts" $naddr]
	    append tableau "\n"
	    append tableau [::arrgen::output "html" $conf(tableau) $lines]
 	} else {
	    append tableau [mc "no inactive host found"]
	}
    }

    #
    # Display menus
    #

    set menureseau ""
    if {[llength $lnet] == 0} then {
        set menureseau [mc "No allowed network"]
    } else {
        set menureseau [::webapp::form-menu "reseau" 1 0 $lnet $selnet]
    }
    set menuoccur [::webapp::form-menu "occurrence" 1 0 $loccur $seloccur]

    #
    # Url du formulaire
    #

    d urlset "%URLFORM%" $conf(next) {}

    #
    # End of script: output page and close database
    #

    d result $conf(page) [list \
                                [list %LRES%      $menureseau] \
                                [list %LOCCUR%    $menuoccur] \
                                [list %TABLEAU%   $tableau] \
                            ]
}

###############################################################################
# Main procedure
###############################################################################

d cgi-dispatch "mac" "mac"
