# Title: channelsort.tcl
# Author: El_Rico
#
# Modified by KHobbits (khobbits.net/sbnc/)
#
# 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 (Channel order) - Not set"
			} else {
				set chansort "channelsort (Channel order) - [getbncuser $client channelsort]"
			}
##			if { [getbncuser $client tag channelsort.channellist] == "" } {
##				set chanlist "channellist (Custom channel order) - Not set"
##			} else {
##				set chanlist "channellist (Custom channel order) - [getbncuser $client tag channelsort.channellist]"
##			}
			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\" (alphabetical order), \"cts\" (order joined) and \"custom\" (Order set in channellist). To unset this setting use \"/sbnc unset channelsort\"."
				bncreply "Invalid value, valid values are \"alpha\" (alphabetical order), \"cts\" (order joined). To unset this setting use \"/sbnc unset channelsort\"."
				haltoutput
			}
##		} elseif { [string equal -nocase [lindex $params 1] "channellist"] } {
##			if { [lindex $params 2] != "" } {
##				setbncuser $client channelsort custom
##				setbncuser $client tag channelsort.channellist [lrange $params 2 end]
##				bncreply "Done."
##				haltoutput
##			} else {
##				bncreply "You can not use an empty value, list the order to display channels seperated by spaces, 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
		}
	}
}
