#by wiebe @ QuakeNet


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



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

proc c_instopic:help:pub { nick uhost handle chan text } {
  lappend output "Usage: instopic <n> <topic>"
  lappend output "Inserts topic in the Nth part in the channel topic. Parts are seperated by \"$::c_instopic(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 instopic msg
bind msgm omn|omn "help instopic" c_instopic:help:msg

proc c_instopic:help:msg { nick uhost handle text } {
  lappend output "Usage: instopic <channel> <n> <topic>"
  lappend output "Inserts topic in the Nth part in the channel topic on the given channel. Parts are seperated by \"$::c_instopic(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"
}



#instopic pub
bind pubm omn|omn "% ${botnet-nick} instopic" c_instopic:pub
bind pubm omn|omn "% ${botnet-nick} instopic *" c_instopic:pub

proc c_instopic: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_instopic(char) "\n" "search" "replace" "search" "replace"] $topic]
  set topic [split $topic \n]
  set t [llength $topic]
  if { [string equal $newtopic ""] } {
    lappend output "Usage: instopic <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"
  } 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 topic [linsert $topic $n $newtopic]
    set topic [join $topic $::c_instopic(char)]
    if { [string length $topic] > $::c_instopic(len) } {
      lappend output "The new topic for $chan exceeds the maximum topic length ($::c_instopic(len) chars)."
    } else {
      puthelp "TOPIC $chan :$topic"
      lappend output "Inserted in position [expr $n +1] in the topic of $chan: $newtopic"
      lappend output "New topic on $chan ([string length $topic]/$::c_instopic(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]]"
}



#instopic msg
bind msg omn|omn instopic c_instopic:msg

proc c_instopic: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_instopic(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: instopic <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 "No topic set, setting this as topic on $chan"
  } 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 topic [linsert $topic $n $newtopic]
    set topic [join $topic $::c_instopic(char)]
    if { [string length $topic] > $::c_instopic(len) } {
      lappend output "The new topic for $chan exceeds the maximum topic length ($::c_instopic(len) chars)."
    } else {
      puthelp "TOPIC $chan :$topic"
      lappend output "Inserted in position [expr $n +1] in the topic of $chan: $newtopic"
      lappend output "New topic on $chan ([string length $topic]/$::c_instopic(len) chars):"
      lappend output "$topic"
    }  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}

