#################################################################
# Meltdowns Topic-Recover V0.2 (09-01-2004)                     #
#                                                               #
# http://scripts.udp.at                                         #
#                                                               #
# Stores every chantopic into a file to recover the last topic, #
# if none is set. (can be forced with !recovertopic)		#
#								#
# Topics can be viewed directly in the chat as well.		#
# With !rollback you can rollback to the first topic 		#
# set on specific day (!rollback m/d/y with leading zeros)	#
#                                                               #
# Contact: meltdown@gmx.at | #lamer or #meltdown on quakenet    #
#                                                               #
#################################################################
#
# New in 0.2 :
#
# Added a cleanup-proc which will be executed twice a month
#  to cleanup the topics.log file (searches for double-entries and deletes them)
#  If you already use 0.1...DONT KILL THE BOT while it cleans up the topicfile(may take a while)
# Fixed error with tcl < 8.4
#
#################################################################

bind TOPC - * proc:rectopic

bind PUB o|o !searchtopic proc:searchtopic
bind PUB o|o !recovertopic proc:forcerecover
bind PUB o|o !rollback proc:rollbacktopic

bind TIME - "00 00 01 * *" topic:cleanup
bind time - "00 00 15 * *" topic:cleanup

set melttop(file) "$tgpath/txt/topics.log"
# File, where topics wil be stored

set melttop(max) 5
# Maximum output when searching for a specific topic

proc proc:forcerecover {nick uhost hand chan text } {
	proc:rectopic $nick $uhost $hand $chan ""
}

proc proc:rectopic {nick uhost hand chan topic} {
global melttop
if {![file exists $melttop(file)]} { exec /bin/touch $melttop(file) }
if {![string equal $topic ""]} { 
	set topicfile [open $melttop(file) r]
	while {![eof $topicfile]} {
		set input [split [gets $topicfile] ¦]
		if {[string equal -nocase [lindex $input 3] $topic]} { close $topicfile ; return 0 }
	}
	close $topicfile
	set topicfile [open $melttop(file) a]
	if {$nick != $::botnick} {puts $topicfile "[clock seconds]¦$chan¦$nick¦[string trim $topic]"}
	close $topicfile
 } else {
	set topicfile [open $melttop(file) r]
	while {![eof $topicfile]} {
		set input [split [gets $topicfile] ¦]
		if {[string equal -nocase [string trim [lindex $input 1]] $chan]} {
			if {![info exists aktuell]} {
				set aktuell [string trim [lindex $input 3]]
				set aktuellzeit [string trim [lindex $input 0]]
				set aktuellnick [string trim [lindex $input 2]]
			} else {
				if {$aktuellzeit < [lindex $input 0]} {
	                                set aktuell [string trim [lindex $input 3]]
	                                set aktuellzeit [string trim [lindex $input 0]]
					set aktuellnick [string trim [lindex $input 2]]
				}
			}
		}
	}		
	if {$aktuell != ""} {
		if {$aktuellnick == "*"} { set aktuellnick "unknown" }
		putserv "TOPIC $chan :$aktuell"
		putserv "PRIVMSG $chan :Recovering last topic. Topic was set by $aktuellnick on [clock format $aktuellzeit -format %D]"
	}
	close $topicfile
 }
}

proc proc:searchtopic { nick uhost hand chan text } {
global melttop
                        set topicfile [open $melttop(file) r]
                        set i 0
                        set found 0
                        while {![eof $topicfile]} {
				set input [split [gets $topicfile] ¦]
				set thistopic [lindex $input 3]
                                if {([string trim [lindex $input 1]] == $chan) && ( [string match -nocase *$text* $thistopic])} {
				if {$i >= $melttop(max)} { 
					putserv "NOTICE $nick :Further topics found, but maximum output reached"
					return 0
				}
				incr i
                                        putserv "NOTICE $nick :Topic found for channel [lindex $input 1] set by [lindex $input 2] at [clock format [lindex $input 0] -format %D]($thistopic)"
                                set found 1
                                }
                        }
                        if {$found == 0} { putserv "NOTICE $nick :Topic not found" }
	close $topicfile
}
proc proc:randomtopic {} {
	global melttop
	if {![file exists $melttop(file)]} {
		return [RandS remind]
	} else {
		set topicfile [open $melttop(file) r]
		set topics [split [read -nonewline $topicfile] "\n"]
		set ctopic [lindex $topics [rand [llength $topics]]]
		set ctopic [split $ctopic "¦"]
	#	return "([lindex $ctopic 2] [clock format [lindex $ctopic 0] -format %D]) \002\"[lindex $ctopic 3]\"\002"
		close $topicfile
		return [lindex $ctopic 3]
	}
}


proc proc:rollbacktopic { nick uhost hand chan text } {
global melttop
                        set topicfile [open $melttop(file) r]
                        set i 0
                        set found 0
                        while {![eof $topicfile]} {
       		                set input [split [gets $topicfile] ¦]
				if {[lindex $input 0] !=""} {
       	        	                set topictime [clock format [string trim [lindex $input 0]] -format %D]
						if {([string equal $topictime $text]) && ([string equal -nocase $chan [string trim [lindex $input 1]]])} {
						putserv "TOPIC $chan :[lindex $input 3]"
						putserv "PRIVMSG $chan :Topic found and set"
						close $topicfile
						return 0
					}
				}
			}
putserv "PRIVMSG $chan :No Topic found for $chan on $text"

}

proc topic:cleanup { min hour day month year } {
global melttop
set multi_top 0
putlog "Reading file..."
set topicfile [open $melttop(file) r]
set i 0
 while {![eof $topicfile]} {
	set checkline($i) [split [gets $topicfile] ¦]
	set vsline($i) $checkline($i)
	incr i
 }
putlog "File reading complete..."
close $topicfile
set x 0
set topicfile [open $melttop(file) r]
putlog "Beginning compare...This may take a while...dont kill the bot"
for {set x 0} {$x < $i} {incr x} {
			for {set y 0} {$y < $i} {incr y} {
				if {([string equal -nocase [lindex $checkline($y) 3] [lindex $vsline($x) 3]]) && ($y != $x)} {
					incr multi_top
					set vsline($y) [lreplace $vsline($y) 3 3 "BAD"]
				}
			}
}
close $topicfile
putlog "Writing new file..."
	set topicfile [open topictemp w]
	for {set x 0} {$x < $i} {incr x} {
		if {([lindex $vsline($x) 3] != "BAD") && ([lindex $vsline($x) 3] != "")} { 
			puts $topicfile [join $vsline($x) ¦]
		}
	}
	close $topicfile
	putlog "Found $multi_top multiple strings and deleted them"
	file copy -force topictemp $melttop(file)
	file delete topictemp

}
#putlog "Melts Topicrecover loaded... 0.2"
