############################
### ClanAvail.tcl        ###
### Version 1.3          ###
### By Wcc               ###
### wcc@techmonkeys.org  ###
### http://dawg.dynu.com ###
### EFnet #|DAWG|Tcl     ###
############################

############################################################################
### Copyright © 2000 - 2001 |DAWG| Scripting Group. All rights reserved. ###
############################################################################

############################################################
## This script stores availability details for clan wars. ##
## Users must be authorized to use the commands. The bot  ##
## will also automatically change your channel's topic to ##
## the availability details of the next war.              ##
############################################################

##############
## COMMANDS ##
############################################
## PUB ## !avail (Can be changed)         ##
######### Sets you as available for the   ##
######### next war.                       ##
######### ------------------------------  ##
######### !navail (Can be changed)        ##
######### Sets you as not available for   ##
######### the next war.                   ##
######### ------------------------------  ##
######### !maybe (Can be changed)         ##
######### Sets you as maybe available     ##
######### for the next war.               ##
######### ------------------------------  ##
######### !wardetails [details]           ##
######### Shows the details for the       ##
######### next war. Availability is also  ##
######### shown, than cleared. If details ## 
######### are given, the war details will ##
######### be set to the specified         ##
######### information.                    ##
############################################
## MSG ## /msg <bot> auth <password>	  ##
######### Authorizes you to use commands. ##
######### ------------------------------  ##
######### /msg <bot> deauth		  ##
######### De-authorizes you from using    ##
######### commands.			  ##
############################################
## DCC ## .chanset <channel> +clantopic   ##
######### Makes the bot set the topic of  ##
######### the specified channel to the    ##
######### details of the next war when    ##
######### information changes.            ##
############################################

##########################################################
## Just load the script, edit the settings, and rehash. ##
##########################################################

#################################################
# Set the flag required to use the script here. #
#################################################

set clanavail_setting(flag) "o|o"

#################################################################
# Set the command for setting yourself as maybe available here. #
#################################################################

set clanavail_setting(maybe) "!maybe"

###########################################################
# Set the command for setting yourself as available here. #
###########################################################

set clanavail_setting(avail) "!avail"

###############################################################
# Set the command for setting yourself as not available here. #
###############################################################

set clanavail_setting(navail) "!navail"

####################################################################
# When adding to the topic, should the war details be added to the #
# end of the topic or the beginning? (0 for beginning, 1 for end)  #
####################################################################

set clanavail_setting(end) 1

###################################
# Set the database filename here. #
###################################

set clanavail_setting(database) "./scripts/clanavail.db"

###################################################################
# Enable bold in topic and !wardetails? (Makes it easier to read) #
###################################################################

set clanavail_setting(bold) 1

####################
# Code begins here #
####################

if {![string match 1.6.* $version]} {
	putlog "\002CLANAVAIL:\002 \002CRITICAL ERROR\002 ClanAvail.tcl requires eggdrop 1.6.x to run."
	die "\002CLANAVAIL:\002 \002CRITICAL ERROR\002 ClanAvail.tcl requires eggdrop 1.6.x to run."
}

bind pub $clanavail_setting(flag) !wardetails clanavail_showwar
bind pub $clanavail_setting(flag) $clanavail_setting(maybe) clanavail_maybe
bind pub $clanavail_setting(flag) $clanavail_setting(avail) clanavail_avail
bind pub $clanavail_setting(flag) $clanavail_setting(navail) clanavail_navail
bind msg - auth clanavail_auth
bind msg - deauth clanavail_deauth
bind part - * clanavail_deauth_part
bind sign - * clanavail_deauth_sign
bind splt - * clanavail_deauth_splt
setudef flag clantopic

proc clanavail_auth {nick uhost hand text} {
	global botnick
	if {![validuser $hand]} {
		putserv "NOTICE $nick :You are not a bot user."
	} elseif {[lindex [split $text] 0] == ""} {
		putserv "NOTICE $nick :Usage: /msg $botnick auth <password>"
	} elseif {![passwdok $hand [lindex [split $text] 0]]} {
		putserv "NOTICE $nick :Invalid password."
	} elseif {[matchattr $hand +Q]} {
		putserv "NOTICE $nick :You are already authorized."
	} else {
		chattr $hand +Q
		putserv "NOTICE $nick :You are now authorized."
	}
}
proc clanavail_deauth {nick uhost hand text} {
	if {![validuser $hand]} {
		putserv "NOTICE $nick :You are not a bot user."
	} elseif {![matchattr $hand +Q]} {
		putserv "NOTICE $nick :You are not authorized."
	} else {
		chattr $hand -Q
		putserv "NOTICE $nick :You are no longer authorized."
	}
}
proc clanavail_deauth_sign {nick uhost hand chan text} {
	if {([validuser $hand]) && ([matchattr $hand +Q])} { chattr $hand -Q }
}
proc clanavail_deauth_part {nick uhost hand chan text} {
	set on 0
	foreach chan [channels] {
		if {[onchan $nick $chan]} {
			set on 1
			break
		}
	}
	if {(!$on) && ([validuser $hand]) && ([matchattr $hand +Q])} { chattr $hand -Q }
}
proc clanavail_deauth_splt {nick uhost hand chan} {
	if {([validuser $hand]) && ([matchattr $hand +Q])} { chattr $hand -Q }
}
proc clanavail_readarray {array file} {
	upvar $array ours
	if {![catch {open $file r} fd]} {
		set arraydata [split [read $fd] \n]
		close $fd
		if {[array exists ours]} { unset ours }
		foreach line $arraydata {
			if {[string range $line 0 2] == "%!%"} {
				set line [string range $line 3 end]
				if {[scan $line "%\[^!!!\]!!!%s" name data] > 0} {
					if {![info exists data]} { set data "" }
					set ours($name) $data
				}
			}
		}
		
	}
}
proc clanavail_savearray {array file} {
	upvar $array ours
	if {(![catch {open $file w} fd]) && ([array exists ours])} {
		foreach entry [array names ours] { puts $fd "%!%$entry!!!$ours($entry)" }
		close $fd
	}
}
if {(![array exists clanavail_users]) && ([file exists $clanavail_setting(database)])} { clanavail_readarray clanavail_users $clanavail_setting(database) }

proc clanavail_showwar {nick uhost hand chan text} {
	global clanavail_users botnick
	if {[matchattr $hand +Q]} {
		if {$text == ""} {
			if {![info exists clanavail_users(/warinfo)]} {	set clanavail_users(/warinfo) "None" }
			set users [clanavail_getavail]
			set avail [lindex $users 0]
			set navail [lindex $users 1]
			set maybe [lindex $users 2]
			putserv "PRIVMSG $chan :[clanavail_bold "Next War: $clanavail_users(/warinfo) Available: $avail Not Available: $navail Maybe Available: $maybe."]"
			if {[info exists clanavail_users]} { 
				foreach i [array names clanavail_users] {
					if {![string match $i /warinfo]} { unset clanavail_users($i) }
				}
			}
		} else {
			set clanavail_users(/warinfo) $text
			clanavail_do_topic
			putserv "NOTICE $nick :[clanavail_bold "Next War: $clanavail_users(/warinfo)"]"
		}
	} else {
		putserv "NOTICE $nick :You are not authorized. Please type \"/msg $botnick auth <password>\" to authorize yourself."
	}
}
proc clanavail_avail {nick uhost hand chan text} {
	global clanavail_users botnick clanavail_setting
	if {[matchattr $hand +Q]} {
		if {([info exists clanavail_users($nick)]) && ([string match $clanavail_users($nick) avail])} {
			putserv "NOTICE $nick :You are already marked as available."
		} else {
			set clanavail_users($nick) avail
			clanavail_savearray clanavail_users $clanavail_setting(database)
			clanavail_do_topic
			putserv "NOTICE $nick :You are now marked as available."
		}
	} else {
		putserv "NOTICE $nick :You are not authorized. Please type \"/msg $botnick auth <password>\" to authorize yourself."
	}
}
proc clanavail_navail {nick uhost hand chan text} {
	global clanavail_users botnick clanavail_setting
	if {[matchattr $hand +Q]} {
		if {([info exists clanavail_users($nick)]) && ([string match $clanavail_users($nick) navail])} {
			putserv "NOTICE $nick :You are already marked as not available."
		} else {
			set clanavail_users($nick) navail
			clanavail_savearray clanavail_users $clanavail_setting(database)
			clanavail_do_topic
			putserv "NOTICE $nick :You are now marked as not available."
		}
	} else {
		putserv "NOTICE $nick :You are not authorized. Please type \"/msg $botnick auth <password>\" to authorize yourself."
	}
}
proc clanavail_maybe {nick uhost hand chan text} {
	global clanavail_users botnick clanavail_setting
	if {[matchattr $hand +Q]} {
		if {([info exists clanavail_users($nick)]) && ([string match $clanavail_users($nick) maybe])} {
			putserv "NOTICE $nick :You are already marked as maybe available."
		} else {
			set clanavail_users($nick) maybe
			clanavail_savearray clanavail_users $clanavail_setting(database)
			clanavail_do_topic
			putserv "NOTICE $nick :You are now marked as maybe available."
		}
	} else {
		putserv "NOTICE $nick :You are not authorized. Please type \"/msg $botnick auth <password>\" to authorize yourself."
	}
}
proc clanavail_do_topic {} {
	global clanavail_users clanavail_setting
	foreach c [channels] {
		foreach i [channel info $c] {
			if {([string match "+clantopic" $i]) && ([botisop $c])} {
				lappend tchans $c
				break
			}
		}
	}
	set users [clanavail_getavail]
	set avail [lindex $users 0]
	set navail [lindex $users 1]
	set maybe [lindex $users 2]
	if {![info exists clanavail_users(/warinfo)]} {	set clanavail_users(/warinfo) "None" }
	if {[info exists tchans]} {
		foreach chan $tchans {
			if {!$clanavail_setting(end)} {
				if {[string match "*Next War:*Available:*Not Available:*Maybe Available:*. |*" [topic $chan]]} {
					set topic [string range [topic $chan] [expr [string first ". |" [topic $chan]] + 4] end]
				} else {
					set topic [topic $chan]
				}
				set topic "[clanavail_bold "Next War: $clanavail_users(/warinfo) Available: $avail Not Available: $navail Maybe Available: $maybe."] | $topic"
			} else {
				if {[string match "*| *Next War:*Available:*Not Available:*Maybe Available:*" [topic $chan]]} {
					if {$clanavail_setting(bold)} {
						set topic [string range [topic $chan] 0 [expr [string first "| \002Next War:" [topic $chan]] - 2]]
					} else {
						set topic [string range [topic $chan] 0 [expr [string first "| Next War:" [topic $chan]] - 2]]
					}
				} else {
					set topic [topic $chan]
				}
				set topic "$topic | [clanavail_bold "Next War: $clanavail_users(/warinfo) Available: $avail Not Available: $navail Maybe Available: $maybe."]"
			}
			if {[topic $chan] != $topic} { putserv "TOPIC $chan :$topic" }
		}
	}
}
proc clanavail_bold {text} {
	global clanavail_setting
	if {$clanavail_setting(bold)} {
		regsub -all -- " Not Available: " "$text" " \002Not Available:\002 " text
		regsub -all -- " Maybe Available: " "$text" " \002Maybe Available:\002 " text
		regsub -all -- " Available: " "$text" " \002Available:\002 " text
		regsub -all -- "Next War: " "$text" "\002Next War:\002 " text
	}
	return $text
}
proc clanavail_getavail {} {
	global clanavail_users
	foreach a [array names clanavail_users] {
		if {![string match $a /warinfo]} {
			switch -- $clanavail_users($a) {
				"avail" { lappend avail $a, }
				"navail" { lappend navail $a, }
				"maybe" { lappend maybe $a, }
			}
		}
	}
	if {![info exists avail]} { set avail None }
	if {![info exists navail]} { set navail None }
	if {![info exists maybe]} { set maybe None }
	regsub -all -- "," "[lindex $avail end]" "" end
	regsub -all -- "," "[lindex $navail end]" "" end2
	regsub -all -- "," "[lindex $maybe end]" "" end3
	set avail [lreplace $avail end end $end]
	set navail [lreplace $navail end end $end2]
	set maybe [lreplace $maybe end end $end3]
	return "[list $avail $navail $maybe]"
}
putlog "\002CLANAVAIL:\002 ClanAvail.tcl Version 1.3 by Wcc is loaded."