#
# --------( ServerJump by MonkeyBrains v1.2 )----------
#
#  special special!! thanks to    - TymezUp - 
#
#  v1.2 --	now dcc off/on switch
#		dcc adjustable jump time interval  (5 min - 48 hrs)
#		and other various dcc chat commands.... " .sjump "
#
#  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	

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 jidx
	set jidx $idx
	set args [lindex $args 0]
	if {$args == ""} {set args "help"}
	switch $args {
		go	{kill_j_timer
			set jump_on "on"
			auto_jump
			return 0}
		stat	{next_jump
			return 0}
		1	{set jump_on "on"
			putdcc $idx "*** ServerJump - Enabled" 
			return 0}
		0	{set jump_on "off"
			putdcc $idx "*** ServerJump - Disabled"
			return 0}
		kill	{kill_j_timer
			set jump_on "off"
			putdcc $idx "*** ServerJump is OFF....type: '.sjump go' or '.sjump <time>' to restart."
			return 0}
		help	{putdcc $idx "*** usage:"
			putdcc $idx "***   .sjump <0/1>    - off/on"
			putdcc $idx "***   .sjump <time in min>     5 - 2880"
			putdcc $idx "***   .sjump go       - will instatly run ServerJump"
			putdcc $idx "***   .sjump kill     - turns off ServerJump and kills backround timers"
			putdcc $idx "***   .sjump stat     - displays amound of time until"
			putdcc $idx "                                  next ServerJump "
			putdcc $idx "***   ServerJump is currently ' $jump_on ' @ $jump_time min intervals."
			return 0}
	}
	if {[string trim $args 0123456789] != ""} {
		putdcc $idx "*** $args is a invaled sjump value."
		return 0}
	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
	set jump_on "on"
	timer $jump_time auto_jump
	putdcc $idx "*** ServerJump ' on ' @ $jump_time "
	return 0 }

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

proc next_jump {} {
	global jidx
	set if_jump 1
	foreach j [timers] {
	set time [lindex $j 1]
	if {[string trim $time auto_jump] == ""} {
	set time [lindex $j 0]
	putdcc $jidx "*** $time min untill next ServerJump"
	set if_jump 0
	return 0 }}
	if {$if_jump != 0} {putdcc $jidx "*** no ServerJump timers found"}
	return 0 }


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