################################################################################
###### Author: Khobbits ( #tcl on irc.swiftirc.net | tcl@khobbits.co.uk ) ######
################################################################################

# 'Greet set' system
# Can be used to log connection statistics
# Can be used to set random greets/tags on connect
# Can be used to set greets containing connection information

################################################################################
####### You will need probably not need to edit anything in this script. #######
####### Position of default messages are highlighted with some comments  #######
################################################################################


internalbind command khgreet:command
internalbind attach khgreet:attach
internalbind svrlogon khgreet:connect

proc khgreet:command { client params } {
	if { [string equal -nocase [lindex $params 0] "help"] } {
		bncaddcommand greet KHUser "enables the auto greet change system" "The main purpose of this function is to allow the bnc to update your ns greet with connection data.\nOpon connect the bnc will update your ns greet, replacing the variables.\nSyntax /sbnc greet <command> \[params\]\nCommands:\nenable <on/off> - enables or disabled the function\nset - this sets your greet\nreset - this resets the tracked statistics\nupdate - forces an early update\nadd - adds a %RandomMessage\ndel - removes a %RandomMessage (wilcard match)\nlist - lists current %RandomMessage's\nVariables:\n%client - number of reconnects to bnc\n%bnc - number of times bnc reconnected\n%days - days this information has been tracked\n%RandomMessage - a random message from a db"

	} elseif { [string equal -nocase [lindex $params 0] "jump"] } {
		if { [getbncuser $client tag greet.bnc] != "" } {
			setbncuser $client tag greet.bnc [expr [getbncuser $client tag greet.bnc] - 1 ]
		}
	} elseif { [string equal -nocase [lindex $params 0] "greet"] } {
		if { [string equal -nocase [lindex $params 1] "reset"] } {
			setbncuser $client tag greet.start [clock seconds]
			setbncuser $client tag greet.bnc 0
			setbncuser $client tag greet.client 0
			bncreply "Reset disconnect statistics, to disable function or change greet use /sbnc greet set"
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "update"] } {
			khgreet:update $client
			bncreply "Setting nickserv greet with current statistics and settings."
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "add"] } {
			setbncuser $client tag greet.rand "[getbncuser $client tag greet.rand]|[join [lrange $params 2 end]]"
			bncreply "Done, added random message '[join [lrange $params 2 end]]'. (If you are having problems with ':' try '::'.)"
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "list"] } {
			bncreply "%RandomMessage List:"
			foreach x [split [getbncuser $client tag greet.rand] |] {
				bncreply "$x"
			}
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "del"] } {
			set data [split [getbncuser $client tag greet.rand] |]
			set match [lsearch $data "[join [lrange $params 2 end]]"]
			if { $match == "-1" } {
				bncreply "Sorry match couldn't be found, search is case sensitive, you may use wildcards in match, only first match is removed."
			} else {
				setbncuser $client tag greet.rand "[join [lreplace $data $match $match] |]"
				bncreply "Done, removed random message: [lindex $data $match]"
			}
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "enable"] } {
			if { [string equal -nocase [lindex $params 2] "on"] } {
				setbncuser $client tag greet.on 1
				bncreply "Done."
			} elseif { [lindex $params 2] == 1 } {
				setbncuser $client tag greet.on 1
				bncreply "Done."
			} elseif { [string equal -nocase [lindex $params 2] "off"] } {
				setbncuser $client tag greet.on ""
				bncreply "Done."
			} elseif { [lindex $params 2] == 0 } {
				setbncuser $client tag greet.on ""
				bncreply "Done."
			} else {
				bncreply "Error: Vaild values are on/off or 1/0."
			}
			if { [getbncuser $client tag greet.start] == "" } {
				setbncuser $client tag greet.start [clock seconds]
			}
			if { [getbncuser $client tag greet.bnc] == "" } {
				setbncuser $client tag greet.bnc 0
			}
			if { [getbncuser $client tag greet.client] == "" } {
				setbncuser $client tag greet.client 0
			}
			if { [getbncuser $client tag greet.message] == "" } {
############################### Set innital greet message
				setbncuser $client tag greet.message {For %days days my bnc has saved me from %client quits but quit itself %bnc times. %RandomMessage}
			}
			if { [getbncuser $client tag greet.rand] == "" } {
############################### set innital random message
				setbncuser $client tag greet.rand "Pingouts ftl!"
			}
			if { [getbncuser $client tag greet.on] != "" } {
				khgreet:update $client
			}
			haltoutput
		} elseif { [string equal -nocase [lindex $params 1] "set"] } {
			if { [llength $params] < 2 } {
				bncreply "Error: You didnt set a value"
				haltoutput

			} else {
				set message "[join [lrange $params 2 end]]"
				setbncuser $client tag greet.message "$message"
				bncreply "Done, set greet as: [getbncuser $client tag greet.message]"
				bncreply "Only %days %client %bnc %BNC %randomessage or %RandomMessage will be replaced. (It is best not to use: \{ \} \[ \] or \$)"
				if { [getbncuser $client tag greet.on] != "" } {
					khgreet:update $client
				}
				haltoutput
			}
		} else {
			bncreply "You can use this command to modify your nickserv greet automatically, assuming you have nickserv set to auto identify or only want it to update after you log in."
			bncreply "Current settings:"
			if { [getbncuser $client tag greet.on] == "" } {
				bncreply "Enable: 0 (no)"
			} else {
				bncreply "Enable: 1 (yes)"
			}
			if { [getbncuser $client tag greet.method] == "" } {
				bncreply "Set method: ns set greet"
			} else {
				bncreply "Set method: [getbncuser $client tag greet.method] (default: ns set greet)"
			}
			if { [getbncuser $client tag greet.start] == "" } {
				bncreply "Logging: 0 days"
			} else {
				bncreply "Logging for: [expr ( [clock seconds] - [getbncuser $client tag greet.start] ) /86400 ] days"
			}
			if { [getbncuser $client tag greet.bnc] == "" } {
				bncreply "BNC Disconnects: 0"
			} else {
				bncreply "BNC Disconnects: [getbncuser $client tag greet.bnc]"
			}
			if { [getbncuser $client tag greet.client] == "" } {
				bncreply "Client Disconnects: 0"
			} else {
				bncreply "Client Disconnects: [getbncuser $client tag greet.client]"
			}
			if { [getbncuser $client tag greet.message] == "" } {
				bncreply "Greet: Default"
			} else {
				bncreply "Greet: [getbncuser $client tag greet.message]"
			}
			bncreply "Type '/sbnc help greet' for information on how to change settings."
			haltoutput
		}
	}
	return
}

proc khgreet:update { client } {
	global botnick botname

	setctx $client

	if { [getbncuser $client tag greet.method] == "" } {
		set method "ns set greet"
	} else {
		set method "[getbncuser $client tag greet.method]"
	}

	set gdays [expr ( [clock seconds] - [getbncuser $client tag greet.start] ) /86400 ]
	set gclient [getbncuser $client tag greet.client]
	set gbnc [getbncuser $client tag greet.bnc]
	set grandommessage "[khgreet:random $client]"

	set message "[getbncuser $client tag greet.message]"
	set message "[string map [list %RandomMessage $grandommessage %randommessage $grandommessage] $message]"
	set message "[string map [list %days $gdays %client $gclient %bnc $gbnc %BNC $gbnc] $message]"
	putserv "$method $message"
}


proc khgreet:attach { client } {
	global botnick botname

	if { [getbncuser $client tag greet.client] == "" } {
		setbncuser $client tag greet.client 0
	} else {
		setbncuser $client tag greet.client [expr [getbncuser $client tag greet.client] + 1 ]
	}

	if { [getbncuser $client tag greet.on] != "" } {
		khgreet:update $client
	}

}

proc khgreet:connect { client } {
	global botnick botname

	if { [getbncuser $client tag greet.bnc] == "" } {
		setbncuser $client tag greet.bnc 0
	} else {
		setbncuser $client tag greet.bnc [expr [getbncuser $client tag greet.bnc] + 1 ]
	}

	setctx $client
	after 5000

	if { [getbncuser $client tag greet.on] != "" } {
		khgreet:update $client
	}
}

proc khgreet:random { client } {
	set array "[getbncuser $client tag greet.rand]"
	set reply "[lindex [split $array |] [randx 0 [expr "[llength [split $array |]] - 1"]]]"
	return "$reply"
}

proc randx {x y} {
return [expr "round( ($x - 0.5) + (rand() * ($y - $x + 0.499)) )"]
}