############################
### ChanPeak.tcl	 ###
### Version 1.1		 ###
### By Wcc		 ###
### wcc@techmonkeys.org  ###
### http://dawg.dynu.com ###
### EFnet #|DAWG|Tcl     ###
############################

############################################################################
### Copyright © 2000 - 2002 |DAWG| Scripting Group. All rights reserved. ###
############################################################################

##########################################################################################################
## This script keeps a record of the peak amount of users in a channel. The channel peak can be viewed  ##
## from dcc chat or via pub command. It can also optionally announce to the channel or broadcast a      ##
## partyline notice when a channel's peak record is broken.						##
##########################################################################################################

##############
## COMMANDS ##
##############################################
## DCC ## .chanset <channel> +/-peak        ##
######### Enables or disables Peak-Tracking ##
######### for a channel.		    ##
######### --------------------------------- ##
######### .peak <channel> (Can be changed)  ##
######### Shows the peak for a channel.     ##
##############################################
## PUB ## !peak [channel] (Can be changed)  ##
######### Shows the peak for the specified  ##
######### channel. If no channel is	    ##
######### specified, the current channel's  ##
######### peak will be displayed.	    ##
##############################################

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

###########################################################
# Set the flag required to use the pub peak command here. #
###########################################################

set chanpeak_setting(pflag) "-|-"

###########################################################
# Set the flag required to use the dcc peak command here. #
###########################################################

set chanpeak_setting(dflag) "-|-"

##################################
# Set the pub peak command here. #
##################################

set chanpeak_setting(cmd_pub) "!peak"

##################################
# Set the dcc peak command here. #
##################################

set chanpeak_setting(cmd_dcc) "peak"

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

set chanpeak_setting(bold) 1

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

set chanpeak_setting(CHANPEAK:) 1

######################################
# Announce new peaks to the channel? #
######################################

set chanpeak_setting(announce) 1

#########################################
# Broadcast new peaks to the partyline? #
#########################################

set chanpeak_setting(cast) 1

###################################
# Set the database filename here. #
###################################

set chanpeak_setting(db) "./scripts/peak.db"

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

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

setudef flag peak
bind join - * chanpeak_join
bind dcc $chanpeak_setting(dflag) $chanpeak_setting(cmd_dcc) chanpeak_dcc
bind pub $chanpeak_setting(pflag) $chanpeak_setting(cmd_pub) chanpeak_pub

proc chanpeak_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] <= 0} { continue }
		if {![info exists data]} { set data "" }
		set ours($name) $data
	}
	close $fd
}
proc chanpeak_savearray {array file} {
	upvar $array ours
	if {![catch {open $file w} fd] && [array exists ours]} {
		foreach entry [array names ours] { puts $fd "%!%$entry!!!$ours($entry)" }
		close $fd
	}
}
if {![array exists chanpeak_peak] && [file exists $chanpeak_setting(db)]} { chanpeak_readarray chanpeak_peak $chanpeak_setting(db) }
proc chanpeak_dopre {} {
	global chanpeak_setting
	if {!$chanpeak_setting(CHANPEAK:)} { return "" }
	if {!$chanpeak_setting(bold)} { return "CHANPEAK: " }
	return "\002CHANPEAK:\002 "
}
proc chanpeak_dcc {hand idx text} {
	global chanpeak_setting chanpeak_peak
	if {$text == ""} { putdcc $idx "[chanpeak_dopre]Usage: .$chanpeak_setting(cmd_dcc) <channel>" ; return }
	if {![info exists chanpeak_peak([string tolower [set chan [lindex [split $text] 0]]])]} { putdcc $idx "[chanpeak_dopre]The peak record for $chan does not exist." ; return }
	putdcc $idx "[chanpeak_dopre]The peak for $chan is [lindex $chanpeak_peak([string tolower $chan]) 0] users. It was set on [lindex $chanpeak_peak([string tolower $chan]) 1] at [lindex $chanpeak_peak([string tolower $chan]) 2] [lindex $chanpeak_peak([string tolower $chan]) 3]."
}
proc chanpeak_pub {nick uhost hand chan text} {
	global chanpeak_setting chanpeak_peak
	if {[set chan2 [lindex [split $text] 0]] == ""} { set chan2 $chan }
	if {![info exists chanpeak_peak([string tolower $chan2])]} { puthelp "PRIVMSG $chan :The peak record for $chan2 does not exist." ; return }
	puthelp "PRIVMSG $chan :The peak for $chan2 is [lindex $chanpeak_peak([string tolower $chan2]) 0] users. It was set on [lindex $chanpeak_peak([string tolower $chan2]) 1] at [lindex $chanpeak_peak([string tolower $chan2]) 2] [lindex $chanpeak_peak([string tolower $chan2]) 3]."
}
proc chanpeak_join {nick uhost hand chan} {
	global chanpeak_setting chanpeak_peak
	if {[lsearch -exact [channel info $chan] +peak] == -1} { return }
	if {[info exists chanpeak_peak([string tolower $chan])]} {
		set peak $chanpeak_peak([string tolower $chan])
	} {
		set peak "0 [clock format [clock seconds] -format %D] [clock format [clock seconds] -format "%I:%M"] [clock format [clock seconds] -format "%p"]"
	}
	if {[lindex $peak 0] >= [set users [llength [chanlist $chan]]]} { return }
	if {$chanpeak_setting(announce)} { puthelp "PRIVMSG $chan :New $chan peak: $users users.  Last peak was on [lindex $peak 1] at [lindex $peak 2] [lindex $peak 3]." }
	if {$chanpeak_setting(cast)} { dccbroadcast "New $chan peak: $users users.  Last peak was on [lindex $peak 1] at [lindex $peak 2] [lindex $peak 3]." }
	set chanpeak_peak([string tolower $chan]) "$users [clock format [clock seconds] -format %D] [clock format [clock seconds] -format "%I:%M"] [clock format [clock seconds] -format "%p"]"
	chanpeak_savearray chanpeak_peak $chanpeak_setting(db)
}
putlog "\002CHANPEAK:\002 ChanPeak.tcl Version 1.1 by Wcc is loaded."