####################################################
# by wiebe @ QuakeNet
#
# can use chase.tcl when available
# can use term.tcl when available
####################################################

####################################################
# warn:help:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} help warn" warn:help:pubm
proc warn:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "warn: usage warn <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "warn: warns users by notice, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "warn: " } }
  putloglev c $c "help: $n $u $h $c warn"
  return 1
}


####################################################
# warn:help:msgm
####################################################
bind msgm lomn|lomn "help warn" warn:help:msgm
proc warn:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "warn: usage warn <nick> \[<nick2> .. <nick6> :\]<reason>"
  lappend o "warn: warns users by notice, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "warn: " } }
  putcmdlog "($n!$u) !$h! help warn"
  return 1
}


####################################################
# warn:pubm
####################################################
bind pubm lomn|lomn "% ${botnet-nick} warn" warn:pubm
bind pubm lomn|lomn "% ${botnet-nick} warn *" warn:pubm
proc warn:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  set t [join [lrange [split $t] 2 end]]
  if {[string equal $t ""]} {
    lappend o "warn: usage warn <nick> \[<nick2> .. <nick6> :\]<reason>"
  } else { set o [warn:warn $h $t $c] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "warn: " } }
  putloglev c $c "warn: $n $u $h $c $t"
  return 1
}


####################################################
# warn:msg
####################################################
bind msg lomn|lomn warn warn:msg
proc warn:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} {
    lappend o "warn: usage warn <nick> \[<nick2> .. <nick6> :\]<reason>"
  } else { set o [warn:warn $h $t] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "warn: " } }
  return 1
}


####################################################
# warn:dcc
####################################################
bind dcc -|- warn warn:dcc
proc warn:dcc { h i t } {
  if {[string equal $t ""]} {
    lappend o "warn: usage warn <nick> \[<nick2> .. <nick6> :\]<reason>"
    lappend o "warn: warns users by notice, the reason can be a custom reason, one predefined from the rule command or a term from the term database (use ?? \[<chan>\] <term>). the ':' prefixing the reason is required when using multiple nicks."
  } else { set o [warn:warn $h $t] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# warn:warn
####################################################
proc warn:warn { h t {c ""} } {
  set t [split $t]; set p [lsearch -glob $t ":*"]
  if {[string equal $p "0"]} {
    set t ""; set r ""; lappend o "warn: no nicks were specified, only a reason"
  } elseif {$p > -1} {
    set r [string range [join [lrange $t $p end]] 1 end]
    if {$p > 6} { lappend o "warn: maximum is 6 nicks"; set p 6 }
    set t [lrange $t 0 [expr $p -1]]

  } else { set r [join [lrange $t 1 end]]; set t [split [lindex $t 0]] }
  if {[string match "\\?\\? ?*" $r] && ![string equal [info procs term:export] ""]} { 
    set r [term:export $c [join [lrange [split $r] 1 end]]]
  }
  if {![string equal [info procs rule:get] ""]} { set r [rule:get $r] }
  if {[string equal $r ""]} { set r "You are violating channel rules." }
  set s ""; set f ""; set b ""; set r "Warning: $r"
  foreach n $t {
    if {[string equal $n ""]} { continue }
    if {![onchan $n]} {
      if {![string equal [info procs chase] ""]} { set d [chase $n] }
      if {![string equal $d ""]} { set n $d }
    }
    if {![onchan $n] || [onchansplit $n]} { lappend f "'$n not found'"; continue }
    if {[isop $n] || [ishalfop $n]} { lappend f "'$n is op'"; continue }
    set g [nick2hand $n]
    foreach c [channels] {
      if {![matchattr $h lomn|lomn $c]} { continue }
      if {![matchattr $g bfvlomn|fvlomn $c] || ![matchattr $g kZ|k $c]} { continue }
      lappend f "'$n is friendly'"; break; continue
    }
    if {[string equal [lsearch -exact [string tolower $s] [string tolower $n]] "-1"]} {
      lappend s $n
    }
  }
  if {![string equal $s ""]} {
    if {![string equal [info procs privmsg] ""]} { privmsg [join $s ,] $r puthelp
    } else { puthelp "PRIVMSG [join $s ,] :$r" }
    set s [join $s "   "]; set g [md5 "ban $s"]
    lappend o "warn: warned '$s' :$r"
    if {![string equal [info procs ircconsole] ""]} {
      ircconsole * w WARN "$h '$s' :$r" $g
    }
  }
  if {![string equal $f ""]} { lappend o "warn: failed to warn [join $f "   "]" }
  return $o
}


set scriptdb(warn) {
  "provides warn command"
}

