#
# --------( ServerJump by MonkeyBrains v1.1 )----------
#
#  special special!! thanks to    - TymezUp - 
#
#  v1.1 --	now dcc off/on switch
#		and variable dcc jump time interval  (5 min - 48 hrs)
#
#  this program will jump servers at user set time intervals
#  to keep k-lines at a minimum
#  it ensures that the bot will regain ops before it changes servers
#  if it can't ensure a safe arival back on you channel(s)
#  it will check every 5 min until it can....

# ---------------(on with the script)------------------  
    
#  this value sets the time intervals that you want
#  the bot to change servers at
#  note: default is '480' = 8 hours	
#  you can NOW also change this value in dcc
#  type ' .sjump ' in dcc for help....
set jump_time 480	

#---------------------( DON'T TOUCH FROM HERE DOWN :) -----------------------

#  this command sets flag #5 to 'j'
#  the 'j' flag indicates users that you trust
#  to give ops back to the bot when it rejoins the channel(s)
set flag5 j	

set jump_on "on"
timer $jump_time auto_jump
	

proc auto_jump {} {
	global chan_ok bchan jump_on
	set chan_ok yes
	set botchan [channels]
	foreach bchan $botchan {
		if {[botisop $bchan]} {
		is_ok } 
	}
	if {$chan_ok != "no"} {
		putlog "jump timer reset"
		timer 480 auto_jump
		if {$jump_on == "on"} {
		jump }
	}
	if {$chan_ok != "yes"} {
		timer 5 auto_jump 
	}
}

proc is_ok {} {
	global bchan
	set jumpuser [userlist j]
	foreach handle $jumpuser {
	if { ([isop [hand2nick $handle $bchan] $bchan]) } {
		return 1 } }
	not_safe
}

proc not_safe {} {
	global chan_ok
	set chan_ok no
}

bind dcc m sjump sjump_set

proc sjump_set {handle idx args} {
	global jump_on jump_time server
	set args [lindex $args 0]
	if {[string trim $args 0123456789] != ""} {
		putdcc $idx "*** $args is a invaled sjump value."}
	if {($args != 1) && ($args != 0) && ($args != "")} {
		if {($args < 5) || ($args > 1440)} {
			putdcc $idx "*** sjump interval values must be between 5 - 2880 min."
			return 0 }
		kill_j_timer
		set jump_time $args
		timer $jump_time auto_jump
		putdcc $idx "*** set @ $jump_time "
		return 0 }
	if {$args == "1"} {
		set jump_on "on"
		putdcc $idx "*** ServerJump - Enabled" 
		return 0 }
	if {$args == "0"} {
		set jump_on "off"
		putdcc $idx "*** ServerJump - Disabled"
		return 0 }
	putdcc $idx "*** usage:"
	putdcc $idx "***   .sjump <0/1>    - off/on"
	putdcc $idx "***   .sjump <time in min>     5 - 2880"
	putdcc $idx "***   ServerJump is currently ' $jump_on ' @ $jump_time min intervals."
}

proc kill_j_timer {} {
	foreach j [timers] {
	set time [lindex $j 1]
	if {[string trim $time auto_jump] != ""} {return 1}
	set time [lindex $j 2]
	killtimer $time 
	return 2 } }

putlog "Y -- ServerJump -- Y v1.1"