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

setudef flag commands

####################################################
# commands:pubm
####################################################
#bind pubm fvlomn|fvlomn "% -?*" commands:pubm
bind pubm -|- "% -?*" commands:pubm
proc commands:pubm { n u h c t } {
  if {![validchan $c]} { return 0 }
  if {![channel get $c commands]} { return 0 }
  if {[matchattr $h bkZ]} { return 0 }
  set t [split $t]; set d [string range [lindex $t 0] 1 end]; set s [join [lrange $t 1 end]]
  set r ""; set t [string range [join $t] 1 end]
  foreach b [binds pubm] {
    set f [lindex $b 1]; set m [lindex $b 2]; set p [lindex $b 4]
    if {![string match "% ${::botnet-nick} ?*" $m]} { continue }
    # allow commands bind for - / -|-
    #if {[string equal $f "-|-"]} { continue }
    #if {[string equal $f "-"]} { continue }
    set m [join [lrange [split $m] 2 end]]
    if {[string match -nocase $m $t]} {
      set r ""; lappend r "$f $p [lindex [split $m] 0]"; break
    }
    # disable unique match for -command
    continue
    if {![regexp {^[a-zA-Z]{3,}$} $d]} { continue }
    if {![string match -nocase "$d*" $m]} { continue }
    if {![string match -nocase [join [lrange [split $m] 1 end]] $s]} { continue }
    set m [lindex [split $m] 0]
    if {![string equal [lsearch -exact $r "$f $p $m"] -1]} { continue }
    lappend r "$f $p $m"
  }
  if {[llength $r] == 1} {
    set r [split [lindex $r 0]]; set f [lindex $r 0]; set p [lindex $r 1]; set m [lindex $r 2]
    set f [split $f |]; set g [lindex $f 0]; set f [lindex $f 1]; set y "-"; set x "-"
    if {[validuser $h]} {
      set x [split [chattr $h $c] |]; set y [lindex $x 0]; set x [lindex $x 1]
      if {![regexp \[$x\] $f] && ![regexp \[$y\] $g]} {
        if {![string equal $f$g "--"] && ![string equal $f$g "-"]} { return 0 }
      }
    } elseif {![string equal $f$g "--"] && ![string equal $f$g "-"]} { return 0 }
    # fail safe
    if {[string equal [info procs $p] ""]} { return 0 }
    if {[$p $n $u $h $c "${::botnet-nick} $m $s"]} { return 1 }
  }
  return 0
}


set scriptdb(commands) {
  "provides '-cmd' commands in channels where +commands is set"
}

