#########################################
###   Natural Bot by nix@valium.org   ###
#########################################

bind pubm - * NaturalPubm
proc NaturalPubm {nick uhost hand chan text} {
	global natoff
	if {!$natoff} {Natural $nick $chan chan $text}
}
bind msgm - * NaturalMsg
proc NaturalMsg {nick uhost hand text} {
	global natoff
	if {!$natoff} {Natural $nick $nick nick $text}
}

proc Natural {nick target type text} {
	global nl botnick
	foreach n [array name nl] {
		if {[string match *%chan* [lindex $nl($n) 1]] || [string match *%ranick* [lindex $nl($n) 1]] && $type == "nick"} {return 0}
		regsub -all %bot [lindex $n 0] $botnick matchtxt
		if {[string match [string tolower $matchtxt] [string tolower $text]]} {
			if {[lindex $nl($n) 1] == "%random"} {
				set outmsg [lrange $nl($n) 2 end]
				set randnum [llength $outmsg]
				if {[llength $outmsg] > 1} {
					set tempmsg [lindex $outmsg [rand $randnum]]
					set outmsg $tempmsg
				}
			} else {set outmsg [lrange $nl($n) 1 end]}
			regsub -all %nick $outmsg $nick outmsg
			if {$type == "chan"} {
				regsub -all %ranick $outmsg [lindex [chanlist $target] [rand [llength [chanlist $target]]]] outmsg
				regsub -all %chan $outmsg $target outmsg
				regsub -all %chan $outmsg \\& outmsg
			}
			if {$outmsg != ""} {putserv "PRIVMSG $target :$outmsg"}
			break
		}
	}
}
proc random {range} {
	set _ran [clock second]
	set _ran [expr ($_ran * 9301 + 49297) % 233280]
	set rdnum expr int($range * ($_ran / double(233280)))]
}
bind dcc m natadd Nat:add
proc Nat:add {hand idx text} {
	global nl
	if {[lindex $text 1] == ""} {
		putdcc $idx "natadd 10<7*hello7*6%bot7*10> <Hi 6%nick!!! welcome on 6%chan :)10>"
		putdcc $idx "    or 10<7*hello7*6%bot7*10> <6%random10> <\{Hi 6%nick!!! welcome on 6%chan :)\} \{hello 6%nick!\} \{yo 6%nick\}10>"
		putdcc $idx	"    %ranick in a message select a random nick"
		return 0
	}
	set al 0
	foreach n [array name nl] {
		if {$n == "[string tolower [lindex $text 0]]"} {incr al}
	}
	if {$al == 0} {
		set nl([string tolower [lindex $text 0]]) "$text"
		NaturalSave
		return 1
	} else {
		putdcc $idx "an entry for '[lindex $text 0]' already exists"
	}
}
bind dcc o natdel Nat:del
proc Nat:del {hand idx text} {
	global nl
	if {[lindex $text 0] == ""} {putdcc $idx "Usage: .natdel 10<7*hello7*6%bot7*10>";return 0}
	set al 0
	foreach n [array name nl] {
		if {$n == "[string tolower [lindex $text 0]]"} {incr al}
	}
	if {$al == 1} {
		unset nl([string tolower [lindex $text 0]])
		NaturalSave
		return 1
	} else {
		putdcc $idx "no entry for '[lindex $text 0]'"
	}
}
bind dcc o natlist Nat:list
proc Nat:list {hand idx text} {
	global nl
	putdcc $idx "Natural List :"
	set i 0
	foreach n [array name nl] {
		regsub -all %nick [lrange $nl($n) 1 end] 10%nick displaym
		regsub -all %ranick $displaym 10%ranick displaym
		regsub -all %chan $displaym 10%chan displaym
		regsub -all %random $displaym 6%random displaym
		regsub -all \\* [lindex $nl($n) 0] 7* displayb
		putdcc $idx "14$i) $displayb -> $displaym"
		incr i
	}
	return 1
}
bind dcc o natlist2 Nat:list2
proc Nat:list2 {hand idx text} {
	global nl
	putdcc $idx "Natural List :"
	set i 0
	foreach n [array name nl] {
		regsub -all \\* [lindex $nl($n) 0] 7* displayb
		putdcc $idx "14$i) $displayb"
		incr i
	}
	return 1
}
bind dcc m natload Nat:load
proc Nat:load {hand idx text} {
	global nl
	foreach n [array name nl] {unset nl($n)}
	NaturalLoad
	return 1
}
bind dcc m natoff Nat:off
proc Nat:off {hand idx text} {
	global natoff
	set natoff 1
	return 1
}
bind dcc m naton Nat:on
proc Nat:on {hand idx text} {
	global natoff
	set natoff 0
	return 1
}
bind dcc o natural Nat:ural
proc Nat:ural {hand idx text} {
	global nl
	putdcc $idx "      Natural.v2.tcl      "
	putdcc $idx "natadd 10<7*hello7*6%bot7*10> <Hi 6%nick!!! welcome on 6%chan :)10>"
	putdcc $idx "    or 10<7*hello7*6%bot7*10> <6%random10> <\{Hi 6%nick!!! welcome on 6%chan :)\} \{hello 6%nick!\} \{yo 6%nick\}10>"
	putdcc $idx	"    %ranick in a message select a random nick"
	putdcc $idx "natdel 10<7*hello7*6%bot7*10>"
	putdcc $idx "natlist show configuration"
	putdcc $idx "natlist2 show configuration (short version)"
	putdcc $idx "natload reload Natural.conf"
	putdcc $idx "natoff  stop Natural.tcl"
	putdcc $idx "naton   start Natural.tcl"
	return 1
}
proc NaturalLoad {} {
	global nl natoff
	set natoff 0
	set f [open Natural.conf r]
	while {[gets $f line] >= 0} {set nl([string tolower [lindex $line 0]]) $line}
	close $f
}
proc NaturalSave {} {
	global nl
	set f [open Natural.conf w]
	foreach n [array name nl] {puts $f "$nl($n)"}
	close $f
}
if {![file exists "Natural.conf"]} {
	set f [open Natural.conf w]
	close $f
}
NaturalLoad

putlog "Natural.v2.tcl - (.Natural for help)"