#by wiebe @ QuakeNet


set c_settopic(len) "250"



#help settopic pub
bind pubm omn|omn "% ${botnet-nick} help settopic" c_settopic:help:pub

proc c_settopic:help:pub { nick uhost handle chan text } {
  c_settopic:pub $nick $uhost $handle $chan ""
  putloglev c $chan "[lindex [split $text] 1]: $nick $uhost $handle $chan [join [lrange [split $text] 2 end]]"
}



#help settopic msg
bind msgm omn|omn "help settopic" c_settopic:help:msg

proc c_settopic:help:msg { nick uhost handle text } {
  c_settopic:msg $nick $uhost $handle ""
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#settopic pub
bind pubm omn|omn "% ${botnet-nick} settopic" c_settopic:pub
bind pubm omn|omn "% ${botnet-nick} settopic *" c_settopic:pub

proc c_settopic:pub { nick uhost handle chan text } {
  set topic [join [lrange [split $text] 2 end]]
  if { [string equal $topic ""] } {
    lappend output "Usage: settopic <newtopic>"
  } 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"
  } elseif { [string length $topic] > $::c_settopic(len) } {
    lappend output "The new topic for $chan exceeds the maximum topic length ([string length $topic]/$::c_settopic(len) chars)."
    lappend output "Topic: [string range $topic 0 [expr $::c_settopic(len) -1]]"
    lappend output "Cutoff: [string range $topic [expr $::c_settopic(len) -1] end]"
  } else {
    puthelp "TOPIC $chan :$topic"
    lappend output "New topic on $chan ([string length $topic]/$::c_settopic(len) chars):"
    lappend output "$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]]"
}



#settopic msg
bind msg omn|omn settopic c_settopic:msg

proc c_settopic:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set topic [join [lrange [split $text] 1 end]]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] || [string equal $topic ""] } {
    lappend output "Usage: settopic <channel> <newtopic>"
  } elseif { ![matchattr $handle lomn|lomn $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"
  } elseif { [string length $topic] > $::c_settopic(len) } {
    lappend output "The new topic for $chan exceeds the maximum topic length ([string length $topic]/$::c_settopic(len) chars)."
    lappend output "Topic: [string range $topic 0 [expr $::c_settopic(len) -1]]"
    lappend output "Cutoff: [string range $topic [expr $::c_settopic(len) -1] end]"
  } else {
    puthelp "TOPIC $chan :$topic"
    lappend output "New topic on $chan ([string length $topic]/$::c_settopic(len) chars):"
    lappend output "$topic"  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

