#by wiebe @ QuakeNet
#requires topicsync.tcl



#help reftopic pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} help reftopic" c_reftopic:help:pub

proc c_reftopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: reftopic"
  lappend output "Attemps to refresh the channel topic."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help reftopic msg
bind msgm fvlomn|fvlomn "help reftopic" c_reftopic:help:msg

proc c_reftopic:help:msg { nick uhost handle text } {
  lappend output "Usage: reftopic <channel>"
  lappend output "Attempts to refresh the topic on the given channel."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#reftopic pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} reftopic" c_reftopic:pub

proc c_reftopic:pub { nick uhost handle chan text } {
  if { ![botisop $chan] && ![botishalfop $chan] && [string match *t* [lindex [split [string tolower [getchanmode $chan]]] 0]] } {
    lappend output "I am not a channel operator and channel mode +t is set on $chan"
  } else {
    if { [catch {set result [topicsync:chan $chan]} error] } {
      putlog "ERROR using topicsync.tcl, perhaps you have not loaded it?"
      error $error
    }
    if { $result } {
     lappend output "Refreshed topic on $chan"
    } else {
     lappend output "Failed to refresh the topic on $chan"
    }  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#reftopic msg
bind msg fvlomn|fvlomn reftopic c_reftopic:msg

proc c_reftopic:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] } {
    lappend output "Usage: reftopic <channel>"
  } elseif { ![matchattr $handle fvlomn|fvlomn $chan] } {
    lappend output "No access or unknown channel $chan"
  } elseif { ![botonchan $chan] } {
    lappend output "I am not on channel $chan"
  } elseif { ![botisop $chan] && ![botishalfop $chan] && [string match *t* [lindex [split [string tolower [getchanmode $chan]]] 0]] } {
    lappend output "I am not a channel operator and channel mode +t is set on $chan"
  } else {
    if { [catch {set result [topicsync:chan $chan]} error] } {
      putlog "ERROR using topicsync.tcl, perhaps you have not loaded it?"
      error $error
    }
    if { $result } {
     lappend output "Refreshed topic on $chan"
    } else {
     lappend output "Failed to refresh the topic on $chan"
    }  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

