Code: Select all
#---------------------------------------------------------------------
# serveronline.tcl
# Tcl script for IRC bot eggdrop
#
# A ".bestserver" on the partyline displays a ranking of the servers
# with the longest online times (duration of connection between
# server and bot).
# Upon each disconnect from the ircserver the bot writes the
# duration of the connection to a file.
#
# v0: 23-Sep-2003
# v1: 27-Sep-2003
# v2: 01-Oct-2003
# + include current server
#---------------------------------------------------------------------
package require Tcl 8.4
package require eggdrop 1.6.15
#---------------------------------------------------------------------
# bindings
# dcc binding to display result
# event binding for logging online time when disconnecting
#---------------------------------------------------------------------
bind DCC n bestserver dcc:serveronline
bind EVNT - disconnect-server evnt:serveronline
#---------------------------------------------------------------------
# Upon a disconnect write time and online time to file
#---------------------------------------------------------------------
proc evnt:serveronline { type } {
global server-online serveraddress
set currenttime [clock seconds]
set fileid [open serveronline.dat a+]
puts $fileid "$serveraddress $currenttime ${server-online}"
close $fileid
}
#---------------------------------------------------------------------
# Analyse the data file and report the respective online times.
#---------------------------------------------------------------------
proc dcc:serveronline { hand idx text } {
global server-online serveraddress
# initialise serverlist and serverdata
set serverlist [list]
# if data file exists, read data from file and analyse the data
if { [file exists serveronline.dat] } {
# read data from file
set fileid [open serveronline.dat r]
set serverdata [read $fileid]
close $fileid
# analyse data
set serverdata [split $serverdata "\n"]
foreach line $serverdata {
if { [scan $line "%s %d %d" name distime contime] != 3 } {
continue
}
set servertime [expr $distime - $contime]
if { $servertime < 0 } { continue }
set date [clock format $distime -format\
"%d-%b-%y %H:%M" -gmt YES]
lappend serverlist [list $name $date $servertime ]
}
}
# if bot is connected add current server
set name $serveraddress
if { $name != "" } {
set curtime [clock seconds]
set servertime [ expr $curtime - ${server-online} ]
set date "<- I am here"
lappend serverlist [list $name $date $servertime]
}
# no valid data?
if { [llength $serverlist] < 1 } {
putdcc $idx "No server online data available."
return 1
}
# sort the list: longest connection time comes first
set serverlist [lsort -index 2 -integer -decreasing $serverlist]
# only take first 20 entries
set serverlist [lrange $serverlist 0 19]
# formatting: determine maximum string length of servername
set namelen 0
foreach serverinfo $serverlist {
set servername [lindex $serverinfo 0]
set serverlen [string length $servername]
if { $serverlen > $namelen } { set namelen $serverlen }
}
incr namelen 3
# formatting: rank, servername, onlinetime
append fmtstring %-3d % - $namelen s %-18s %-s
# output the servernames and the respective online times
set count 0
putdcc $idx "Servers (host:port), date and time of disconnection\
and the online times:\r"
foreach serverinfo $serverlist {
incr count
set servername [lindex $serverinfo 0]
set serverdate [lindex $serverinfo 1]
set servertime [duration [lindex $serverinfo 2] ]
set string [format $fmtstring $count $servername\
$serverdate $servertime]
# seems my telnet client is bugged?
append string "\r"
putdcc $idx $string
}
putdcc $idx "End of servers and the online times.\r"
}
putlog "Loaded (version 2): Serveronline."
how can it be done?
greetz