#by wiebe @ QuakeNet
#requires c_rule.tcl
#requires chase.tcl



#help warn pub
bind pubm lomn|lomn "% ${botnet-nick} help warn" c_warn:help:pub

proc c_warn:help:pub { nick uhost handle chan text } {
  lappend output "Usage: warn <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  lappend output "Warns 6 users at most, custom reason or one predefined from the rule command, the : prefixing the reason is required when using multiple nicks."
  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 warn msg
bind msgm lomn|lomn "help warn" c_warn:help:msg

proc c_warn:help:msg { nick uhost handle text } {
  lappend output "Usage: warn <channel> <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  lappend output "Warns 6 users at most, custom reason or one predefined from the rule command, the : prefixing the reason is required when using multiple nicks."
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  putcmdlog "($nick!$uhost) !$handle! $text"
}



#warn pub
bind pubm lomn|lomn "% ${botnet-nick} warn" c_warn:pub
bind pubm lomn|lomn "% ${botnet-nick} warn *" c_warn:pub

proc c_warn:pub { nick uhost handle chan text } {
  set targets [join [lrange [split $text] 2 end]]
  if { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"    
  } elseif { [string equal $targets ""] } {
    lappend output "Usage: warn <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  } else {
    set output [c_warn:warn $nick $chan $targets]
  }
  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]]"
}



#warn msg
bind msg lomn|lomn warn c_warn:msg

proc c_warn:msg { nick uhost handle text } {
  set chan [lindex [split $text] 0]
  set targets [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 $targets ""] } {
    lappend output "Usage: warn <channel> <nick1> \[<nick2> .. <nick6>\] \[:<reason>\]"
  } elseif { ![matchattr $handle lomn|lomn $chan] } {
    lappend output "No access or unknown channel $chan"
  } elseif { ![botisop $chan] && ![botishalfop $chan] } {
    lappend output "I am not a channel operator on $chan"
  } else {
    set output [c_warn:warn $nick $chan $targets]
  }
  if { [catch {set x [cnotice $nick $output]} error] || !$x } {
    foreach t $output { puthelp "NOTICE $nick :$t" }
  }
  return 1
}



#warn
proc c_warn:warn { nick chan targets } {
  set reason ""; set onchan ""; set offchan ""; set output ""
  set p [lsearch -glob $targets :*]

#found reason, adjust targets
  if { ![string equal $p -1] } {
    set reason [string range [join [lrange [split $targets] $p end]] 1 end]
    set targets [join [lrange [split $targets] 0 [expr $p -1]]]

#allow max 6 targets
    set targets [join [lrange [split $targets] 0 5]]
  } else {
    set reason [join [lrange [split $targets] 1 end]]
    set targets [lindex [split $targets] 0]
  }

  foreach t [split $targets] {

#chasenick
    if { ![onchan $t $chan] } {
      if { [catch {set ct [chase:chasenick $t]} error] } {
        putlog "ERROR using chase.tcl, perhaps you have not loaded it?"
      }
      if { ![string equal $ct -1] && [onchan $ct $chan] && ![onchansplit $ct] } { set t $ct }
    }

#dont warn myself or a bot, target not on chan
    if { [isbotnick $t] || [matchattr [nick2hand $t] b] || ![onchan $t $chan] } {
    if { [string equal [lsearch -exact [string tolower $onchan] [string tolower $t]] -1] } {
      lappend offchan $t
    }

#target on chan
    } elseif { [onchan $t $chan] } {
      if { [string equal [lsearch -exact [string tolower $onchan] [string tolower $t]] -1] } {
        lappend onchan $t
      }
    }
  }

#set reason
  set temp 0
  if { [catch {set temp [c_rule:rule $reason]} error] } {
    putlog "ERROR using c_rule.tcl, perhaps you have not loaded it?"
  }
  if { ![string equal $temp 0] } { set reason $temp }

#use default
  if { [string equal $reason ""] } {
    if { [catch {set reason [c_rule:rule \#0]} error] } {
      putlog "ERROR using c_rule.tcl, perhaps you have not loaded it?"
      set reason "You are violating channel rules."
    }    
  }

#one target, use cnotice if possible
  if { [string equal [llength $onchan] 1] } {
    set onchan [join $onchan]
    lappend output2 "\[$chan\] Warning: $reason"
    if { [catch {set x [cnotice $onchan $output2]} error] || !$x } {
      puthelp "NOTICE $onchan :$output2"
    }
    lappend output "Warning sent to $onchan ($reason) on $chan"

#multiple targets, use notice
  } elseif { ![string equal $onchan ""] } {
    set onchan [join $onchan ,]
    puthelp "NOTICE $onchan :\[$chan\] Warning: $reason"
    lappend output "Warning sent to [join [split $onchan ,]] ($reason) on $chan"
  }
  if { ![string equal $offchan ""] } { lappend output "Failed to warn on $chan $offchan" }
  if { [string equal $offchan$onchan ""] } { lappend output "Failed to find any valid targets to warn on $chan" }
  return $output
}

