# Title: channelsort.tcl
# Author: El_Rico
# Version: 1 - initial release
# License: GPL 2 (http://www.gnu.org/licenses/gpl.html)
# Contact: El_Rico in #sbnc on irc.quakenet.org
# Purpose: Adding control over the channel join order when the user connects to sBNC
# Getting started: load the script in sBNC and adjust the settings "channelsort" and "channellist" via "/sbnc set" according to your needs

internalbind command channelsort:command

setchannelsorthandler "channelsort"

proc channelsort {channel1 channel2} {
	set channel1 [string tolower $channel1]
	set channel2 [string tolower $channel2]
	set channellist [split [string tolower [join [getbncuser [getctx] tag channelsort.channellist] " "]] " "]
	set returnvalue [expr [lsearch -exact $channellist $channel1] - [lsearch -exact $channellist $channel2]]
	set returnvalue [expr {($returnvalue>0) - ($returnvalue<0)}]
	if { [lsearch -exact $channellist $channel1] == -1 } {
		set returnvalue 1
	} elseif { [lsearch -exact $channellist $channel2] == -1 } { 
		set returnvalue -1 
	}
	return $returnvalue
}

proc channelsort:command { client params } {
	if { [string equal -nocase [lindex $params 0] "set"] } {
		if { [llength $params] < 2 } {
			if { [getbncuser $client channelsort] == "" } {
				set chansort "channelsort - Not set"
			} else {
				set chansort "channelsort - [getbncuser $client channelsort]"
			}
			if { [getbncuser $client tag channelsort.channellist] == "" } {
				set chanlist "channellist - Not set"
			} else {
				set chanlist "channellist - [getbncuser $client tag channelsort.channellist]"
			}
			setctx $client
			internaltimer 0 0 bncreply $chansort
			internaltimer 0 0 bncreply $chanlist
		} elseif { [string equal -nocase [lindex $params 1] "channelsort"] } {
			if { [string equal -nocase [lindex $params 2] "alpha"] } {
				setbncuser $client channelsort alpha
				bncreply "Done."
				haltoutput
			} elseif { [string equal -nocase [lindex $params 2] "cts"] } {
				setbncuser $client channelsort cts
				bncreply "Done."
				haltoutput
			} elseif { [string equal -nocase [lindex $params 2] "custom"] } {
				setbncuser $client channelsort custom
				bncreply "Done."
				haltoutput				
			} else {
				bncreply "Invalid value, valid values are \"alpha\", \"cts\" and \"custom\". To unset this setting use \"/sbnc unset channelsort\"."
				haltoutput
			}
		} elseif { [string equal -nocase [lindex $params 1] "channellist"] } {
			if { [lindex $params 2] != "" } {
				setbncuser $client tag channelsort.channellist [lrange $params 2 end]
				bncreply "Done."
				haltoutput
			} else {
				bncreply "You can not use an empty value, to unset this setting use \"/sbnc unset channellist\"."
				haltoutput
			}
		}
	} elseif { [string equal -nocase [lindex $params 0] "unset"] } {
		if { [string equal -nocase [lindex $params 1] "channelsort"] } {
			setbncuser $client channelsort ""
			bncreply "Done."
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "channellist"] } {
			setbncuser $client tag channelsort.channellist ""
			bncreply "Done."
			haltoutput
		}
	}
}