#by wiebe @ QuakeNet




#file to save topics in
set topicsync(file) "topicsync.txt"



#topic
bind topc -|- * topicsync:change

proc topicsync:change { nick uhost handle chan topic } {
  global topicsync
  set topic [join [lrange [split $topic] 0 end]]
  set topic [string map [list "[format %c 160]" " " "search" "replace" "search" "replace"] $topic]
  if { ![validchan $chan] } { return 0 }
  set rtopic ""
  if { [info exists topicsync(topic,$chan)] } { set rtopic [join [lrange [split $topicsync(topic,$chan)] 3 end]] }
  if { ![string equal $topic $rtopic] && ![string equal $nick *] } {
    if { ![string equal $topic ""] } {
      set topicsync(topic,$chan) "$nick!$uhost $handle [clock seconds] $topic"
    } else {
      set topicsync(topic,$chan) ""
    }
    topicsync:save
  }
}



#time
bind time - "00 * * * *" topicsync:time
bind time - "30 * * * *" topicsync:time

proc topicsync:time { mi ho da mo ye } {
  if { [string equal $::botname $::botnick] } { return 0 }
  foreach chan [channels] {
    topicsync:chan $chan
  }
}



#set the topic
proc topicsync:chan { chan } {
  global server topicsync
  if { [string equal $server ""] || ![validchan $chan] } { return 0 }
  if { ![botonchan $chan] } { return 0 }
  if { ![botisop $chan] && ![botishalfop $chan] && [string match *t* [lindex [split [getchanmode $chan]] 0]] } { return 0 }
  set topic [topic $chan]
  if { [string equal $topic ""] && [info exists topicsync(topic,$chan)] } {
    set topic [join [lrange [split $topicsync(topic,$chan)] 3 end]]
  }
  if { ![string equal $topic ""] } {
    puthelp "TOPIC $chan :$topic"
    return 1
  } else {
    #putallbots "topicsync $chan"
    return 0
  }
}



#raw
bind raw -|- "333" topicsync:raw

proc topicsync:raw { server raw rest } {
  global topicsync
  set chan [lindex [split $rest] 1]
  set nick [lindex [split $rest] 2]
  set ctime [lindex [split $rest] 3]
  if { [validchan $chan] } {
    set rtopic ""
    if { [info exists topicsync(topic,$chan)] } {
      set rtopic [join [lrange [split $topicsync(topic,$chan)] 3 end]]
    }
    set topic [topic $chan]
    set topic [string map [list "[format %c 160]" " " "search" "replace" "search" "replace"] $topic]
    if { ![string equal -nocase $rtopic $topic] } {
      set topicsync(topic,$chan) "$nick * $ctime [topic $chan]"
      topicsync:save
    }
  }
  return 0
}



#bot
bind bot - topicsync topicsync:bot

proc topicsync:bot { bot command chan } {
  global server
  if { [string equal $server ""] } { return 1 }
  if { [string equal -nocase $command topicsync] && [validchan $chan] } {
    topicsync:chan $chan
  }
}



#join
bind join -|- * topicsync:join

proc topicsync:join { nick uhost handle chan } {
  if { [isbotnick $nick] && [validchan $chan] } {
    timer 1 [list topicsync:chan $chan]
  }
}



#topic pub
#bind pub fvlomn|fvlomn !topic topicsync:pub

proc topicsync:pub { nick uhost handle chan text } {
  global topicsync
  if { ![validchan $chan] } { return 0 }
  if { ![onchan $nick $chan] } { return 0 }
  topicsync:chan $chan
  if { [info exists topicsync(topic,$chan)] && ![string equal $topicsync(topic,$chan) ""] } {
    set info $topicsync(topic,$chan)
    set by [lindex [split $info] 0]
    set bynick [lindex [split $by !] 0]
    set byuhost [lindex [split $by !] 1]
    set byhandle [lindex [split $info] 1]
    set time [lindex [split $info] 2]
    set ago [join [lrange [split [duration [expr [clock seconds] - $time]]] 0 3]]
    set time [ctime $time]
    set topic [join [lrange [split $info] 3 end]]
    set length [string length $topic]
    if { [string equal $byuhost ""] } {
      set msg "Topic set by $bynick on $time ($ago ago) ' $topic\017 ' ($length chars)"
      if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes c $msg] }
      puthelp "PRIVMSG $chan :$msg"
    } elseif { [string equal $byhandle *] } {
      set msg "Topic set by $bynick ($byuhost) on $time ($ago ago) ' $topic\017 ' ($length chars)"
      if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes c $msg] }
      puthelp "PRIVMSG $chan :$msg"
    } else {
      set msg "Topic set by $bynick ($byuhost) (account: $byhandle) on $time ($ago ago) ' $topic\017 ' ($length chars)"
      if { [string match *c* [lindex [split [getchanmode $chan]] 0]] } { set msg [stripcodes c $msg] }
      puthelp "PRIVMSG $chan :$msg"
    }
  } else {
    puthelp "PRIVMSG $chan :no topic set (here)"
  }
}



#save
proc topicsync:save { } {
  global topicsync
  set file $topicsync(file)
  set fs [open "$file" w]
  foreach name [array names topicsync] {
    if { [string match -nocase topic,* $name] } {
      set chan [lindex [split $name ,] 1]
      if { [validchan $chan] } {
        puts $fs "$chan $topicsync($name)"
      }
    }
  }
  close $fs
}



#load
proc topicsync:load { } {
  global topicsync
  set file $topicsync(file)
  if { ![file exists $file] } { return 0 }
  set fs [open "$file" r]
  set data [read $fs]
  close $fs
  foreach line [split $data \n] {
    if { ![string equal $line ""] } {
      set chan [lindex [split $line] 0]
      if { [validchan $chan] } {
        set rest [join [lrange [split $line] 1 end]]
        set topicsync(topic,$chan) "$rest"
      }
    }
  }
}


topicsync:load

