#by wiebe @ QuakeNet


set c_addtopic(char) " | "
set c_addtopic(len) "250"



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

proc c_addtopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: addtopic <topic>"
  lappend output "Adds the given topic to the end of 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 addtopic msg
bind msgm omn|omn "help addtopic" c_addtopic:help:msg

proc c_addtopic:help:msg { nick uhost handle text } {
  lappend output "Usage: addtopic <channel> <topic>"
  lappend output "Adds the given topic to the end of the channel 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"
}



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

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

