####################################################
# by wiebe @ QuakeNet
#
####################################################

####################################################
# trout:help:pubm
####################################################
bind pubm fvlomn|fvlomn "% ${botnet-nick} help trout" trout:help:pubm
proc trout:help:pubm { n u h c t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "trout: usage trout \[<chan>\] <nick>"
  lappend o "trout: slaps nick with a trout"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "trout: " } }
  putloglev c $c "help: $n $u $h $c trout"
  return 1
}


####################################################
# trout:help:msg
####################################################
bind msgm fvlomn|fvlomn "help trout" trout:help:msgm
proc trout:help:msgm { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  lappend o "trout: usage trout <chan> <nick>"
  lappend o "trout: slaps nick with a trout"
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "trout: " } }
  putcmdlog "($n!$u) !$h! help trout"
  return 1
}


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


####################################################
# trout:msg
####################################################
bind msg fvlomn|fvlomn trout trout:msg
proc trout:msg { n u h t } {
  if {[matchattr $h bkZ]} { return 0 }
  if {[string equal $t ""]} { lappend o "trout: usage trout <chan> <nick>"
  } else { set o [trout:trout $h $t "" $n] }
  if {[string equal [info procs cnotice] ""]} { foreach l $o { puthelp "NOTICE $n :$l" }
  } else { foreach l $o { cnotice $n $l puthelp "trout: " } }
  return 1
}


####################################################
# trout:dcc
####################################################
bind dcc -|- trout trout:dcc
proc trout:dcc { h i t } {
  if {![valididx $i]} { return 0 }
  set c [lindex [split [console $i]] 0]
  if {![validchan $c]} { set c "" }
  if {[string equal $t ""]} {
    lappend o "trout: usage trout \[<chan>\] <nick>"
    lappend o "trout: slaps nick with a trout"
  } else { set o [trout:trout $h $t $c] }
  foreach l $o { putidx $i $l }
  return 1
}


####################################################
# trout:trout
####################################################
proc trout:trout { h t {c ""} {n ""} } {
  set t [split $t]; set d [lindex $t 0]; set m ""; set o ""
  if {[string equal $c ""] || [string match {[#&]*} $d]} {
    set c $d; set t [join [lrange $t 1 end]]
  }
  if {![validchan $c] && ![string equal [info procs whichchan] ""]} { set c [whichchan $h $c] }
  set t [string trimright [join $t] " "]
  if {[validchan $c]} { set m [lindex [split [getchanmode $c]] 0] }
  if {[string equal $c ""] || [string equal $t ""]} {
    lappend o "trout: usage trout \[<chan>\] <nick>"
  } elseif {![validchan $c] || ![matchattr $h fvlomn|fvlomn $c]} {
    lappend o "trout: no access or unknown channel $c"
  } elseif {![botonchan $c]} {
    lappend o "trout: I am not on $c"
  } elseif {[string match *m* $m] && ![botisop $c] && ![botishalfop $c] && ![botisvoice $c]} {
    lappend o "trout: channel $c is moderated"
  } else {
    set a "slaps $t around a bit with a large trout"
    if {[string equal $n ""] || ![onchan $n $c] || [onchansplit $n $c]} {
      lappend o "trout: trouted '$t\017' on $c"
    }
    if {[string match *c* $m] } { set a [stripcodes bcru $a] }
    if {[string equal [info procs action] ""]} { puthelp "PRIVMSG $c :\001ACTION$a\001"
    } else { action $c $a puthelp "" }
  }
  return $o
}


set scriptdb(trout) {
  "provides trout command to trout users"
}

