#by wiebe @ QuakeNet


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



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

proc c_reptopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: reptopic <n> <topic>"
  lappend output "Replaces the Nth part in the channel topic with the given topic. Parts are seperated by \"$::c_reptopic(char)\". Count starts from 1, -N can be used to refer to the Nth part from the end, starting at -1."
  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 reptopic msg
bind msgm omn|omn "help reptopic" c_reptopic:help:msg

proc c_reptopic:help:msg { nick uhost handle text } {
  lappend output "Usage: reptopic <channel> <n> <topic>"
  lappend output "Replaces the Nth part in the channel topic with the given topic on the specified channel. Parts are seperated by \"$::c_reptopic(char)\". Count starts from 1, -N can be used to refer to the Nth part from the end, starting at -1."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#reptopic pub
bind pubm omn|omn "% ${botnet-nick} reptopic" c_reptopic:pub
bind pubm omn|omn "% ${botnet-nick} reptopic *" c_reptopic:pub

proc c_reptopic:pub { nick uhost handle chan text } {
  set n [lindex [split $text] 2]
  set newtopic [join [lrange [split $text] 3 end]]
  set topic [topic $chan]
  set topic [string map [list $::c_reptopic(char) "\n" "search" "replace" "search" "replace"] $topic]
  set topic [split $topic \n]
  set t [llength $topic]
  if { [string equal $newtopic ""] } {
    lappend output "Usage: reptopic <n> <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 equal [topic $chan] ""] } {
    puthelp "TOPIC $chan :$newtopic"
    lappend output "No topic set, setting this as topic on $chan"
    lappend output "New topic on $chan ([string length $topic]/$::c_reptopic(len) chars):"
    lappend output "$topic"
  } elseif { ![string is integer $n] } {
    lappend output "$n is not a valid number."
  } elseif { ($n > "0" && $n > $t) || ($n < "0" && $n < -$t) } {
    lappend output "No topic part with number $n found on $chan"
  } else {
    if { $n > "0" } { incr n -1 } else { incr n $t }
    set oldtopic [lindex $topic $n]
    set l $::c_reptopic(len)
    set l [expr $l - [string length [join $topic $::c_reptopic(char)]]]
    set l [expr $l + [string length $oldtopic]]
    set topic [lreplace $topic $n $n $newtopic]
    set topic [join $topic $::c_reptopic(char)]
    if { [string length $newtopic] > $l } {
      lappend output "The new topic exceeds the maximum topic length ([string length $topic]/$::c_reptopic(len) chars)."
      lappend output "New topic: [string range $newtopic 0 [expr $l -1]]"
      lappend output "Cutoff: [string range $newtopic [expr $l -1] end]"
    } else {
      puthelp "TOPIC $chan :$topic"
      lappend output "Replaced position [expr $n +1] in topic of $chan: $oldtopic"
      lappend output "With: $newtopic"
      lappend output "New topic on $chan ([string length $topic]/$::c_reptopic(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]]"
}



#reptopic msg
bind msg omn|omn reptopic c_reptopic:msg

proc c_reptopic:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set n [lindex [split $text] 1]
  set newtopic [join [lrange [split $text] 2 end]]
  set topic [topic $chan]
  set topic [string map [list $::c_reptopic(char) "\n" "search" "replace" "search" "replace"] $topic]
  set topic [split $topic \n]
  set t [llength $topic]
  if { ![validchan $chan] && ![string equal $chan ""] } {
    lappend output "No access or unknown channel $chan"
  } elseif { [string equal $chan ""] || [string equal $newtopic ""] } {
    lappend output "Usage: reptopic <channel> <n> <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 equal [topic $chan] ""] } {
    puthelp "TOPIC $chan :$newtopic"
    lappend output "$chan\017 No topic set, setting this as topic on $chan"
    lappend output "New topic on $chan ([string length $topic]/$::c_reptopic(len) chars):"
    lappend output "$topic"
  } elseif { ![string is integer $n] } {
    lappend output "$n is not a valid number."
  } elseif { ($n > "0" && $n > $t) || ($n < "0" && $n < -$t) } {
    lappend output "No topic part with number $n found on $chan"
  } else {
    if { $n > "0" } { incr n -1 } else { incr n $t }
    set oldtopic [lindex $topic $n]
    set l $::c_reptopic(len)
    set l [expr $l - [string length [join $topic $::c_reptopic(char)]]]
    set l [expr $l + [string length $oldtopic]]
    set topic [lreplace $topic $n $n $newtopic]
    set topic [join $topic $::c_reptopic(char)]
    if { [string length $newtopic] > $l } {
      lappend output "The new topic exceeds the maximum topic length ([string length $topic]/$::c_reptopic(len) chars)."
      lappend output "New topic: [string range $newtopic 0 [expr $l -1]]"
      lappend output "Cutoff: [string range $newtopic [expr $l -1] end]"
    } else {
      puthelp "TOPIC $chan :$topic"
      lappend output "Replaced position [expr $n +1] in topic of $chan: $oldtopic"
      lappend output "With: $newtopic"
      lappend output "New topic on $chan ([string length $topic]/$::c_reptopic(len) chars):"
      lappend output "$topic"
    }  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

