#######################################
##   	  auto login on X or W  	 ##
##      _]NiX[_ <nix@valium.org>     ##
#######################################

#checktime
set uz(chktime) 3

#debug
set uz(debug) 0

bind dcc o +login +login
proc +login {hand idx text} {
	global uzc
	set text [string tolower $text]
	if {[llength $text] != 3} {
		putdcc $idx "Usage : .+login <x/w> <#channel> <password>"
		return 0
	} elseif {[lindex $text 0] != "x" && [lindex $text 0] != "w"} {
		putdcc $idx "you must specify the bot (X or W)"
		return 0
	} elseif {![string match #* [lindex $text 1]]} {
		putdcc $idx "[lindex $text 1] is not a valid channel"
		return 0
	} else {
		set uzc([lindex $text 1]) "$text"
		SaveXW
		return 1
	}
}
bind dcc o -login -login
proc -login {hand idx text} {
	global uzc
	set text [string tolower $text]
	if {[llength $text] != 1} {
		putdcc $idx "Usage: .-login <#channel>"
		return 0
	} elseif {![info exists uzc([lindex $text 0])]} {
		putdcc $idx "[lindex $text 0] is not a valid channel"
		return 0
	} else {
		catch {unset uzc([lindex $text 0])}
		SaveXW
		return 1
	}
}
bind dcc o login login
proc login {hand idx text} {
	global uz uzc
	putdcc $idx "      XW-login-undernet.v2.tcl      "
	putdcc $idx ""
	putdcc $idx ".+login <x/w> <#channel> <password>"
	putdcc $idx ".-login <#channel>"
	putdcc $idx ""
	putdcc $idx "login list :"
	foreach a [array name uzc] {
		putdcc $idx "bot: [string toupper [lindex $uzc($a) 0]] - chan: [lindex $uzc($a) 1]"
	}
	putdcc $idx ""
}
proc SaveXW {} {
	global uzc
	set f [open LoginUndernet.conf w]
	foreach c [array name uzc] {
		puts $f "$uzc($c)"
	}
	close $f
}
if {[file exists "LoginUndernet.conf"]} {
	set f [open LoginUndernet.conf r]
	while {[gets $f line] >= 0} {
		set uzc([lindex $line 1]) $line
	}
	close $f
}
proc :login: {} {
	global uz uzc
	foreach a [array name uzc] {
		if {[validchan [lindex $uzc($a) 1]]} {
			if {(![botisop [lindex $uzc($a) 1]]) && ([isop X [lindex $uzc($a) 1]] || [isop W [lindex $uzc($a) 1]])} {
				if {[lindex $uzc($a) 0] == "x"} {
					putserv "PRIVMSG X@channels.undernet.org :LOGIN [lrange $uzc($a) 1 end]"
					putserv "PRIVMSG X :OP [lindex $uzc($a) 1]"
					if {$uz(debug) == 1} {putlog "PRIVMSG X@channels.undernet.org :LOGIN [lindex $uzc($a) 1]"}
				} else {
					putserv "PRIVMSG W@channels2.undernet.org :LOGIN [lrange $uzc($a) 1 end]"
					putserv "PRIVMSG W :OP [lindex $uzc($a) 1]"
					if {$uz(debug) == 1} {putlog "PRIVMSG W@channels2.undernet.org :LOGIN [lindex $uzc($a) 1]"}
				}
			}
		} else {
			if {$uz(debug) == 1} {putlog "[lindex $uzc($a) 0] != validchan"}
		}
	}
	timer $uz(chktime) :login:
}
foreach t [timers] {
	if {[lindex $t 1] == ":login:"} {killtimer [lindex $t 2]}
}
:login:

putlog "XW-CserviceLogin.tcl - (.login for help - check time set to $uz(chktime) mins)"