This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

onlineserver.tcl - best server times (make public command)

Support & discussion of released scripts, and announcements of new releases.
Post Reply
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

onlineserver.tcl - best server times (make public command)

Post by Thunderdome »

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."
My ideia was just to put a !bestserver public command available...
how can it be done?
greetz
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

try adding this to the script:

Code: Select all

bind pub - !bestserver pub:serveronline

proc pub:serveronline {nick uhost hand chan 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 } { 
      puthelp "PRIVMSG $chan :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 
   puthelp "PRIVMSG $chan :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" 
      puthelp "PRIVMSG $chan :$string"
   } 
   puthelp "PRIVMSG $chan :End of servers and the online times.\r" 
}
Edit: fixed code format.
Last edited by Sir_Fz on Fri May 06, 2005 8:21 pm, edited 1 time in total.
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

Code: Select all

Tcl error [pub:serveronline]: wrong # args: should be "clock format clockval ?-format string? ?-gmt boolean?"
this shows up .... the dcc still works, but not the pub... :\
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

This is probably caused by the uncorrect code format after you pasted it. Try it now (I edited it.).
User avatar
Thunderdome
Op
Posts: 187
Joined: Tue Mar 15, 2005 4:42 pm

Post by Thunderdome »

All okay! Thanks... I've been looking at the code... I get what you made... :)
You passed all dcc-stuff to pub-stuff... so you have a proc for dcc and another for pub... :)
Nice! 8)
Thanks for the work! :D
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Glad you understood it :D hopefully next time you'll be able to tweak such things by yourself :wink:
Post Reply