Code: Select all
# Time in minutes to check for server's uptime
#
set suptime(config:time) "30"
# Send a warning to all channels with +suptime about server's going down?
# Usage:
# 1 => enable (recommended)
# 0 => disable
#
set suptime(config:warn) "1"
# The warning message to display in channels when a server is lost
# (netsplit or unlinked from the irc network)
#
# %date => Date and Time
# %server => The name of the server
#
# <message-type> can be: PRIVMSG or NOTICE
#
set suptime(config:info) "PRIVMSG I've lost %server on %date"
proc suptime:dcc {handle idx text} {
global suptime
set indx 0
if {[llength $text] == 0} {
if {[llength [array get suptime "*:start"]] == 0} {
putidx $idx "server's list is empty."
return 1
} elseif {([llength [array get suptime "*:start"]] != 0) && ([llength [array get suptime "*:uptime"]] == 0)} {
putidx $idx "server's uptimes not available"
return 1
} else {
foreach {string rest} [array get suptime "*:uptime"] {
set indx [expr {$indx+1}]
set s_name [lindex [split "$string" ":"] 0]
set from $::suptime($s_name:start)
set to $rest
if {($from == "") && ([info exists suptime($s_name:info)])} {
putidx $idx "$indx. $s_name \[$::suptime($s_name:info)\] Last Uptime: [duration [expr {$::suptime($s_name:lastuptime) - $from}]]"
} elseif {($from != "") && ($to != "")} {
putidx $idx "$indx. $s_name Uptime: [duration [expr {$to-$from}]]"
} else {
if {[info exists suptime($s_name:info)]} {
putidx $idx "$indx. $s_name ($::suptime($s_name:info))"
} else {
putidx $idx "$indx. $s_name (unknown)"
}
}
}
return 1
}
}
set cmd [lindex [split $text] 0]
switch $cmd {
"list" {
if {[llength [array get suptime "*:start"]] == 0} {
putidx $idx "server's list is empty."
return 1
}
foreach {string rest} [array get suptime "*:start"] {
set indx [expr {$indx+1}]
putidx $idx "$indx. [lindex [split "$string" ":"] 0] Added [duration [expr {[clock seconds] - [lindex [split $rest] 0]}]] ago."
}; return 1
}
"del" {
set d_name [lindex [split $text] 1]
if {$d_name == ""} {
putidx $idx "Usage: .suptime <add/del/list> \[server-name\]"
} elseif {[info exists suptime($d_name:start)]} {
array unset suptime $d_name:start
array unset suptime $d_name:uptime
array unset suptime $d_name:info
array unset suptime $d_name:lastuptime
putidx $idx "removed '$d_name' from server list"
} else {
putidx $idx "unable to find '$d_name' in server list"
}; return 1
}
"add" {
set a_name [lindex [split $text] 1]
if {$a_name == ""} {
putidx $idx "Usage: .suptime <add/del/list> \[server-name\]"
} elseif {[info exists suptime($a_name:start)]} {
putidx $idx "server '$a_name' already exists!"
} else {
set suptime($a_name:start) [clock seconds]
putidx $idx "added '$a_name' to server's list"
}; return 1
}
}
}
proc suptime:351 {from keyword text} {
global suptime
set rtime [clock seconds]
set sname [string tolower [lindex [split $text] 2]]
if {[info exists suptime($sname:start)]} {
set suptime($sname:uptime) $rtime
return
}
}
proc suptime:402 {from keyword text} {
global suptime
set date [clock format [clock seconds]]
set sname [string tolower [lindex [split $text] 1]]
if {([info exists suptime($sname:start)]) && (![info exists suptime($sname:uptime)])} {
putloglev 1 * "recived \"bad server name\" error for server: $sname"
return
} elseif {([info exists suptime($sname:start)]) && ([info exists suptime($sname:uptime)])} {
set suptime($sname:lastuptime) $suptime($sname:uptime)
set suptime($sname:uptime) ""
set suptime($sname:info) "Offline"
if {$suptime(config:warn)} {
foreach dest [channels] {
if {[channel get $dest suptime]} {
regsub -all "%server" "[join [lrange [split $suptime(config:info)] 1 end]]" "$sname" msg
regsub -all "%date" "$msg" "$date" msg
puthelp "[lindex [split $suptime(config:info)] 0] $dest :$msg"
}
}
}; return
} else {
return
}
}
proc suptime:time {minute hour day month year} {
global suptime
set halt 3
if {[llength [array get suptime "*:start"]] == 0} {
return
}
foreach {string rest} [array get suptime "*:start"] {
timer $halt [list putserv "VERSION [lindex [split "$string" ":"] 0]"]
set halt [expr {$halt+1}]
}
putloglev 1 * "Checking server uptimes..."
return
}
setudef flag suptime
bind dcc mt|mt suptime suptime:dcc
bind time - "$suptime(config:time) * * * *" suptime:time
bind raw - 351 suptime:351
bind raw - 402 suptime:402
putlog "loaded suptime.tcl version 1.1 by TLC_no_TK"
return
Code: Select all
foreach {string_1 rest_1} [array get suptime "*:uptime"] {
foreach {string_2 rest_2} [array get suptime "*:start"] {
set indx [expr {$indx+1}]
set s_name [lindex [split "$string_1" ":"] 0]
set s_from [lindex [split $rest_2] 1]
set s_to [lindex [split $rest_1] 1]
if {($s_from == "") && ([info exists suptime($s_name:info)])} {
putidx $idx "$indx. $s_name \[$suptime($s_name:info)\] Last Uptime: [duration [expr {$s_from - $suptime($s_name:lastuptime)}]]"
} elseif {$s_from != ""} {
putidx $idx "$indx. $s_name Uptime: [duration [expr {$s_from - $s_to}]]"
} else {
putidx $idx "$indx. $s_name (unknown)"
}
}
};
Code: Select all
set counter -1
foreach {name value} [array get suptime "*:uptime"] {
set indx [expr {$indx+1}]
set s_name [lindex [split "$name" ":"] 0]
set s_from [lindex [split [lindex [array get suptime "*:start"] [expr {[incr counter 2}]]] 1]
set s_to [lindex [split $value] 1]
if {($s_from == "") && ([info exists suptime($s_name:info)])} {
putidx $idx "$indx. $s_name \[$suptime($s_name:info)\] Last Uptime: [duration [expr {$s_from - $suptime($s_name:lastuptime)}]]"
} elseif {$s_from != ""} {
putidx $idx "$indx. $s_name Uptime: [duration [expr {$s_from - $s_to}]]"
} else {
putidx $idx "$indx. $s_name (unknown)"
}
};
Of course it would've.. lmao.. But I was more puzzled at how he expected the nested foreach's to even work. So was giving really an alternative to times when those are required. Which may be never, haw. But certainly yes, referring directly to the values would be infinitely easier.. Having two arrays come out exactly parallel to each other is what the way I described requires. This may not be possible and using [lsort] on an inline list really isn't advisable. So forcing them to align isn't exactly simple either.nml375 wrote:speechles,
Wouldn't it simply be easier to use the s_name value and build the index of the array, than using that complex code with array get, counter, and praying we end up with the correct list offset?
Btw, what's the point of encapsulating the incr command with expr?
That's why there is a preview button.. And if you're so afraid of it saying "Edited", then perhaps you should put more thought into your posts before hitting the submit button.But it's there now because you never gave me enough time to ponder my post and silently correct it.
Code: Select all
bind pub mt|mt !suptime pub:suptime
proc pub:suptime {nick host handle channel text} {
global suptime
set indx 0
if {[llength $text] == 0} {
if {[llength [array get suptime "*:start"]] == 0} {
puthelp "NOTICE $nick :server's list is empty."
return 0
}
foreach {string_1 rest_1} [array get suptime "*:uptime"] {
set indx [expr {$indx+1}]
set s_name [lindex [split "$string_1" ":"] 0]
set s_from $suptime($s_name:start)
set s_to [lindex [split $rest_1] 1]
if {($s_from == "") && ([info exists suptime($s_name:info)])} {
puthelp "PRIVMSG $channel :$indx. $s_name \[$suptime($s_name:info)\] Last Uptime: [duration [expr {$s_from - $suptime($s_name:lastuptime)}]]"
} elseif {$s_from != ""} {
puthelp "PRIVMSG $channel :$indx. $s_name Uptime: [duration [expr {$s_from - $s_to}]]"
} else {
puthelp "PRIVMSG $channel :$indx. $s_name (unknown)"
}
}; return 1
}
set cmd [lindex [split $text] 0]
switch $cmd -- {
"list" {
if {[llength [array get suptime "*:start"]] == 0} {
puthelp "NOTICE $nick :server's list is empty."
return 1
}
foreach {string rest} [array get suptime "*:start"] {
set indx [expr {$indx+1}]
puthelp "PRIVMSG $channel :$indx. [lindex [split "$string" ":"] 0] Added [duration [expr {[clock seconds] - [lindex [split $rest] 0]}]] ago."
}
}
"del" {
set d_name [lindex [split $text] 1]
if {$d_name == ""} {
puthelp "NOTICE $nick :Usage: .suptime <add/del/list> \[server-name\]"
} elseif {[info exists suptime($d_name:start)]} {
array unset suptime $d_name:start
array unset suptime $d_name:uptime
array unset suptime $d_name:info
array unset suptime $d_name:lastuptime
puthelp "NOTICE $nick :removed '$d_name' from server list"
} else {
puthelp "NOTICE $nick :unable to find '$d_name' in server list"
}
}
"add" {
set a_name [lindex [split $text] 1]
if {$a_name == ""} {
puthelp "NOTICE $nick :Usage: .suptime <add/del/list> \[server-name\]"
} elseif {[info exists suptime($a_name:start)]} {
puthelp "NOTICE $nick :server '$a_name' already exists!"
} else {
set suptime($a_name:start) [clock seconds]
puthelp "NOTICE $nick :added '$a_name' to server's list."
}
}
}; return 1
}