unbind dcc m match *dcc:match
bind dcc m match match_users

proc match_users { hand idx args } {
  regsub -nocase -all \[{}] $args "" args
	if {$args == ""} {
		putdcc $idx "Usage: match +<flags> [channel] [limit] | match <hostmask> [channel] [limit]"
		return 0
	}
	if {([lindex $args 1] != "") && ([string match #* [lindex $args 1]])} {
		if {[lindex $args 2] == ""} { set maxmatch 20 } else { set maxmatch [lindex $args 2] }
		set matches 0
		set whochan [lindex $args 1]
		if {[string first + [lindex $args 0]] == 0} {
			set flags [string trimleft [lindex $args 0] +]
			foreach f [split $flags {}] {
				if [string match *$f* "ofdk12345"] {
					append flag $f
				}
			}
			if ![info exists flag] {
				putdcc $idx "No valid channel flags specified"
				return 0
			}
			putdcc $idx "*** Matching '$flag' for $whochan"
			putdcc $idx "HANDLE    PASS NOTES FLAGS                     LAST"
			foreach user [userlist] {
				if [matchchanattr $user $flags $whochan] {
					incr matches
					set handle "$user          "
					if [passwdok $user ""] { set pass "no   " } else { set pass "yes  " }
					global notefile
					set numnotes 0
					if [file exists $notefile] {
						set f [open $notefile r]
						while {![eof $f]} {
							set note [gets $f]
							if {[lindex $note 0] == $user} { incr numnotes }
						}
						close $f
					}
					set numnotes "$numnotes     "
					putdcc $idx "[string range $handle 0 9]$pass[string range $numnotes 0 5][chattr $user]"
					foreach chan [channels] {
						if {[chattr $user + $chan] == "-"} { continue }
						set c "$chan                   "
						set f "[chattr $user + $chan]                          "
						if {[expr {[unixtime] - [getlaston $user $chan]}] < 86400} {
							set last [clock format [getlaston $user $chan] -format %R]
						} elseif {[getlaston $user $chan] == 0} {
							set last "never"
						} else { set last [clock format [getlaston $user $chan] -format "%e %h"] }
						putdcc $idx "  [string range $c 0 18][string range $f 0 26]$last"
					}
					set hl 1
					foreach host [gethosts $user] {
						if ![info exists hosts($hl)] {
							set hosts($hl) $host
							continue
						}
						if {[string length "$hosts($hl)$host"] < 43} {
							set hosts($hl) "$hosts($hl), $host"
						} else {
							incr hl
							set hosts($hl) "$host"
						}
					}
					if [array exists hosts] {
						foreach n [array names hosts] {
							if {$n == 1} { putdcc $idx "  HOSTS: $hosts($n)" } { putdcc $idx "         $hosts($n)" }
						}
					} else { putdcc $idx "  HOSTS: none" }
					catch { unset hosts }
					catch { unset hl }
					if {[getinfo $user] != ""} { putdcc $idx "  INFO: [getinfo $user]" }
					if {[getemail $user] != ""} { putdcc $idx "  EMAIL: [getemail $user]" }
					global {whois-fields}
					foreach field ${whois-fields} {
						if {[set i [user-get $user $field]] != ""} { putdcc $idx "[string toupper $field]: $i" }
					}
					if {$matches >= $maxmatch} {
						putdcc $idx "(More than $maxmatch matches found, truncating list)"
						break
					}
				}
			}
			putdcc $idx "--- Listed $matches matches."
		}
		return 1
	}
	*dcc:match $hand $idx $args
}

