#by wiebe @ QuakeNet


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



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

proc c_deltopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: deltopic <n1> \[<n2> .. <nN>\]"
  lappend output "Deletes the Nth part in the channel topic. Parts are seperated by \"$::c_deltopic(char)\". Count starts from 1, -N can be used to refer to the Nth part from the end, starting at -1. Multiple part numbers can be specified."
  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 deltopic msg
bind msgm omn|omn "help deltopic" c_deltopic:help:msg

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



#deltopic pub
bind pubm omn|omn "% ${botnet-nick} deltopic" c_deltopic:pub
bind pubm omn|omn "% ${botnet-nick} deltopic *" c_deltopic:pub

proc c_deltopic:pub { nick uhost handle chan text } {
  set numbers [join [lrange [split $text] 2 end]]
  set topic [topic $chan]
  set topic [string map [list $::c_deltopic(char) "\n" "search" "replace" "search" "replace"] $topic]
  set topic [split $topic \n]
  set t [llength $topic]
  if { [string equal $numbers ""] } {
    lappend output "Usage: deltopic <n1> \[<n2> .. <nN>\]"
  } 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] ""] } {
    lappend output "No topic set on $chan"
  } else {
    set validnum ""
    set invalidnum ""
    set notfound ""
    foreach n [split $numbers] {
      if { ![string is integer $n] } {
        lappend invalidnum $n
      } elseif { ($n > "0" && $n > $t) || ($n < "0" && $n < -$t) } {
        lappend notfound $n
      } else {
        if { $n > "0" } { incr n -1 } else { incr n $t }
        if { [string equal [lsearch $validnum $n] -1] } { lappend validnum $n }
      }
    }
    if { ![string equal $invalidnum ""] } {
      lappend output "[join $invalidnum] is not a valid number."
    }
    if { ![string equal $notfound ""] } {
      lappend output "No topic part with number [join $notfound] found on $chan"
    }
    if { ![string equal $validnum ""] } {
      set validnum [lsort -decreasing $validnum]
      lappend output "Removed from topic on $chan:"
      foreach n $validnum {
        lappend output "[lindex $topic $n]"
        set topic [lreplace $topic $n $n]
      }
      set topic [join $topic $::c_deltopic(char)]
      puthelp "TOPIC $chan :$topic"
      lappend output "New topic on $chan ([string length $topic]/$::c_deltopic(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]]"
}



#deltopic msg
bind msg omn|omn deltopic c_deltopic:msg

proc c_deltopic:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set numbers [join [lrange [split $text] 1 end]]
  set topic [topic $chan]
  set topic [string map [list $::c_deltopic(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 $numbers ""] } {
    lappend output "usage: deltopic <channel> <n1> \[<n2> .. <nN>\]"
  } 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] ""] } {
    lappend output "No topic is set on $chan"
  } else {
    set validnum ""
    set invalidnum ""
    set notfound ""
    foreach n [split $numbers] {
      if { ![string is integer $n] } {
        lappend invalidnum $n
      } elseif { ($n > "0" && $n > $t) || ($n < "0" && $n < -$t) } {
        lappend notfound $n
      } else {
        if { $n > "0" } { incr n -1 } else { incr n $t }
        if { [string equal [lsearch $validnum $n] -1] } { lappend validnum $n }
      }
    }
    if { ![string equal $invalidnum ""] } {
      lappend output "[join $invalidnum] is not a valid number."
    }
    if { ![string equal $notfound ""] } {
      lappend output "No topic part with number [join $notfound] found on $chan"
    }
    if { ![string equal $validnum ""] } {
      set validnum [lsort -decreasing $validnum]
      lappend output "Removed from topic on $chan:"
      foreach n $validnum {
        lappend output "[lindex $topic $n]"
        set topic [lreplace $topic $n $n]
      }
      set topic [join $topic $::c_deltopic(char)]
      puthelp "TOPIC $chan :$topic"
      lappend output "New topic on $chan ([string length $topic]/$::c_deltopic(len) chars):"
      lappend output "$topic"
    }  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

