#by wiebe @ QuakeNet
#requires showtopic.tcl

set c_showtopic(len) "250"



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

proc c_showtopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: showtopic <channel>"
  lappend output "Shows the topic for the given channel."
  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 showtopic msg
bind msgm fvlomn|fvlomn "help showtopic" c_showtopic:help:msg

proc c_showtopic:help:msg { nick uhost handle text } {
  lappend output "Usage: showtopic <channel>"
  lappend output "Shows the topic for the given channel where the bot is."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#showtopic pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} showtopic" c_showtopic:pub
bind pubm fvlomn|fvlomn "% ${botnet-nick} showtopic *" c_showtopic:pub

proc c_showtopic:pub { nick uhost handle chan text } {
  set text2 [join [lrange [split $text] 2 end]]
  if { [catch {showtopic:pub $nick $uhost $handle $chan $text2} error] } {
    putlog "ERROR using showtopic.tcl, perhaps you have not loaded it?"
    error $error
  }
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#showtopic msg
bind msg fvlomn|fvlomn showtopic c_showtopic:msg

proc c_showtopic: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: showtopic <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 { [string equal [topic $chan] ""] } {
    lappend output "No topic set on $chan"
  } else {
    set topic [topic $chan]
    lappend output "Topic on $chan ([string length $topic]/$::c_showtopic(len) chars) is:"
    lappend output "$topic"  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

