# peak1.5.tcl - FireEgl@EFNet <FireEgl@LinuxFan.com> - 1/1/00

### Description:
# Keeps up with the peak number of people in the
# channel and announces it when a new record is set.

# Set peak log file name and path
set peakfile "logs/peak.$chan.txt"

# Answer public queries via PRIVMSG or NOTICE?
set peak:out "privmsg"

######### Please do not edit anything below unless you know what you are doing ;) #########
bind join - * join:peak
proc join:peak {nick host hand chan} {
   set curnum [llength [chanlist $chan]]
   set peak [getpeak $chan]
   set lastmax [lindex $peak 0]
   if {$curnum > $lastmax} {
		if {$lastmax} { puthelp "PRIVMSG $chan :\[4x]: new channel peak! ($curnum)  last peak was [timeago [lindex $peak 1]] ago."
		putlog "\[4x]: new channel peak in $chan! ($curnum)  last peak was [timeago [lindex $peak 1]] ago."}
      return [setpeak $chan $curnum [unixtime]]
   }
}

# Loads the peak data from file if not already in memory and returns the data:
proc getpeak {chan} { global peak
   set chan $chan
   if {[info exists peak($chan)]} {
      set lastmax [lindex $peak($chan) 0]
      set lastdate [lindex $peak($chan) 1]
   } else {
      set fid [open "logs/peak.$chan.txt" "RDONLY CREAT"]
      set lastmax "[gets $fid]"
      if {$lastmax == ""} { set lastmax 0 }
      set lastdate "[gets $fid]"
      if {$lastdate == ""} { set lastdate [unixtime] }
      set peak($chan) "$lastmax $lastdate"
      close $fid
   }
   return "$lastmax $lastdate"
}

# Sets peak data to file:
proc setpeak {chan curnum unixtime} { global peak
   set chan $chan
   set peak($chan) "$curnum $unixtime"
   set fid [open "logs/peak.$chan.txt" "WRONLY CREAT"]
   puts $fid $curnum
   puts $fid $unixtime
   close $fid
}

# provides the dcc and public peak command:
bind dcc o|o peak dcc:peak
bind pub o|o !peak pub:peak

proc dcc:peak {hand idx rest} {
   if {[lindex $rest 0] == ""} {set chan [lindex [console $idx] 0]
   } else {
   set chan [lindex $rest 0]
   }
   if {![validchan $chan]} {putdcc $idx "\[4x]: I'm not on $chan, check out my channel list." ; return 0}
   set peak [getpeak $chan]
   putcmdlog "#$hand# peak $chan"
   putdcc $idx "\[4x]: $chan peak record: [lindex $peak 0] ([timeago [lindex $peak 1]] ago)."
}

# (the bot answer peak request for other channels now)
proc pub:peak {nick hand host chan rest} {
   if {[lindex $rest 0] == ""} {set peak [getpeak $chan]
   } else {
   set peak [getpeak [lindex $rest 0]]
   if {![validchan [lindex $rest 0]]} {puthelp "NOTICE $nick :\[4x]: I'm not on [lindex $rest 0], check out my channel list." ; return 0}
   }
   puthelp "PRIVMSG $chan :\[4x]: channel peak record: [lindex $peak 0] ([timeago [lindex $peak 1]] ago)."
   putcmdlog "<$nick@$chan> peak $rest"
}

proc timeago {lasttime} {
  set totalyear [expr [unixtime] - $lasttime]
  if {$totalyear >= 31536000} {
    set yearsfull [expr $totalyear/31536000]
    set years [expr int($yearsfull)]
    set yearssub [expr 31536000*$years]
    set totalday [expr $totalyear - $yearssub]
  }
  if {$totalyear < 31536000} {
    set totalday $totalyear
    set years 0
  }
  if {$totalday >= 86400} {
    set daysfull [expr $totalday/86400]
    set days [expr int($daysfull)]
    set dayssub [expr 86400*$days]
    set totalhour 0
  }
  if {$totalday < 86400} {
    set totalhour $totalday
    set days 0
  }
  if {$totalhour >= 3600} {
    set hoursfull [expr $totalhour/3600]
    set hours [expr int($hoursfull)]
    set hourssub [expr 3600*$hours]
    set totalmin [expr $totalhour - $hourssub]
     if {$totalhour >= 14400} { set totalmin 0 }
   }
  if {$totalhour < 3600} {
    set totalmin $totalhour
    set hours 0
  }
  if {$totalmin > 60} {
    set minsfull [expr $totalmin/60]
    set mins [expr int($minsfull)]
    set minssub [expr 60*$mins]
    set secs 0
  }
  if {$totalmin < 60} {
    set secs $totalmin
    set mins 0
  }
  if {$years < 1} {set yearstext ""} elseif {$years == 1} {set yearstext "$years year, "} {set yearstext "$years years, "}
  if {$days < 1} {set daystext ""} elseif {$days == 1} {set daystext "$days day, "} {set daystext "$days days, "}
  if {$hours < 1} {set hourstext ""} elseif {$hours == 1} {set hourstext "$hours hour, "} {set hourstext "$hours hours, "}
  if {$mins < 1} {set minstext ""} elseif {$mins == 1} {set minstext "$mins minute"} {set minstext "$mins minutes"}
  if {$secs < 1} {set secstext ""} elseif {$secs == 1} {set secstext "$secs second"} {set secstext "$secs seconds"}
  set output $yearstext$daystext$hourstext$minstext$secstext
  set output [string trimright $output ", "]
  return $output
}

putlog "\[4x]: Utility, Channel Peak Record. Loaded."