# bxfaq.tcl 1.0-BETA (ehh, final too I guess)
# 
# 
# author : KeNsOn (kenson@dat95kjn.campus.mdh.se)
#
# create the faq-files like this "#channelname.bxfaq" in your ~/.BitchX -dir
#
# Don't use an editor that does linewrap when writing faq-files.
# 
# /TCL -file ~/.BitchX/bxfaq.tcl
# 
# example faq-file:  Just remove the #'s and save in a file named #bxfaq.bxfaq
#<begin #example.bxfaq> don't include this line
#tst1
#This is test-topic no.1
#tst2
#this is test-topic no.2
#<end #example.bxfaq>   don't include this line
#
bind pub - %faq pub_faq
bind pub - %pfaq pub_pfaq
bind msg - help bot_help

proc pub_faq {nick uhost hand channel args} {
   	set arrayname [string tolower $channel]
	append arrayname .bxfaq_array
	global ${arrayname}
	if [info exists $arrayname] {
	   	set subject [string tolower $args]		
		if [llength [array names ${arrayname} $subject]] {
			set the_info [set ${arrayname}($subject)]
			putserv "NOTICE $nick :FAQ on topic <\002$subject\002> requested by $nick"
        		putserv "NOTICE $nick :$the_info"
			
		} else {
			putserv "NOTICE $nick :Sorry $nick - no FAQ record for \002$subject\002. Try %faq index"
			}
	} else {
		putserv "NOTICE $nick :I have no FAQ for $channel"
		}
	}

proc pub_pfaq {nick uhost hand channel args} {
   	set arrayname [string tolower $channel]
	append arrayname .bxfaq_array
	global ${arrayname}
	if [isop $nick $channel] {
		if [info exists $arrayname] {
	   		set subject [string tolower $args]		
			if [llength [array names ${arrayname} $subject]] {
				set the_info [set ${arrayname}($subject)]
				putserv "PRIVMSG $channel :FAQ on topic <\002$subject\002> requested by $nick (non-ops use: %faq index)"
        			putserv "PRIVMSG $channel :$the_info"
			
			} else {
				putserv "PRIVMSG $channel :Sorry $nick - no FAQ record for \002$subject\002. Try %faq index"
				}
		} else {
			putserv "PRIVMSG $channel :I have no FAQ for $channel"
			}
		}
 	}
proc bot_help {nick uhost hand args} {
	putserv "PRIVMSG $nick :Type '%faq index' in a channel I'm in for a list of available help"
	putserv "PRIVMSG $nick :Channel operators may use '%pfaq <topic>' to display results in channel"
	if { [string length $args] > 2 } {
		set tok [string length $args]
		putserv "PRIVMSG $nick :btw, whats $args?"
		}
	}

if {![info exists faq_started]} {
   foreach faq_file [glob ~/.BitchX/*.bxfaq] {
	set input [open $faq_file r]
	set arrayname [append faq_file _array]
	set buffer [split [read -nonewline $input] \n]
	split $buffer
	set arrayname [split $arrayname /]
	set arrayname [lindex $arrayname [expr [llength $arrayname]-1]]
	array set ${arrayname} $buffer
	set ${arrayname}(index) [array name ${arrayname}]
	putlog "Array is $arrayname"
	close $input
	}
 
   set faq_started 1
}
