^on ^353 * {
  push _chanicks.$encode($tolower($2)) $3-}
^on -raw_irc "% 366 *" {
  @ _chanicks[$encode($tolower($3))] = csort($_chanicks[$encode($tolower($3))])
  showusers users $3 $#pattern(@* $_chanicks[$encode($tolower($3))]) $#filter(@* $_chanicks[$encode($tolower($3))]) 0 $#pattern(+* $_chanicks[$encode($tolower($3))]) $_chanicks[$encode($tolower($3))]
  @ _chanicks[$encode($tolower($3))] = []}

^on #-channel_synch 23 * {reqops $0}

@ a.flags = [-o -v -n -i -a]
@ a.names = [ops voiced nops ircops users]
@ a.pats = [*@* *+* *.* \\** *]

alias chanstats {
  ^local aa $channel(${[$*]?[$*]:C})
  ^return $#pattern(*@* $aa) $#filter(*@* $aa) $#pattern(\\** $aa) $#pattern(*+* $aa)}

alias scanick {
  ^local _type
  switch ($*) {
    () {}
    (\\**) (@*) {@ _type = [opcolor]}
    (+*) {@ _type = [voicecolor]}
    (*) {@ _type = [nickcolor]}}
  ^return $fparse(format_names_$_type $[1]* $strip(*@+. $*))}

^on ^hook "scecho % 1 *" {
  xecho -win $1 $cparse("$names_echo" $(fls)$(de)$(fls)$(de)$scanick($3)$(de)$(fls)$(de)$(fls))}
^on ^hook "scecho % 2 *" {
  xecho -win $1 $cparse("$names_echo" $(fls)$(de)$scanick($3)$(de)$(fls)$(de)$scanick($4)$(de)$(fls))}
^on ^hook "scecho % 3 *" {
  xecho -win $1 $cparse("$names_echo" $(fls)$(de)$scanick($3)$(de)$scanick($4)$(de)$scanick($5)$(de)$(fls))}
^on ^hook "scecho % 4 *" {
  xecho -win $1 $cparse("$names_echo" $scanick($3)$(de)$scanick($4)$(de)$(fls)$(de)$scanick($5)$(de)$scanick($6))}
^on ^hook "scecho % 5 *" {
  xecho -win $1 $cparse("$names_echo" $scanick($3)$(de)$scanick($4)$(de)$scanick($5)$(de)$scanick($6)$(de)$scanick($7))}

alias showusers if ([$6]) {
  ^local n.
  ^local nicks $6-
  ^local len $#nicks
  ^local div ${len/5}
  ^local cw $chanwin($1)
  ^local pe $cparse("$names_echo" %t)
  @ de = cparse("$names_delimiter")
  @ fl = strlen($stripansicodes($scanick(@test)))
  @ fls = repeat($fl  )
  @ n.cst = [$2-5]
  ${names_header ? [xecho -win $cw $cparse("$names_header")] :}
  xecho -win $cw $fparse(format_names $0 $1 $n.cst)
  repeat $div {
    xecho -win $cw $sar(g/%t/$scanick($shift(nicks))$(de)$scanick($shift(nicks))$(de)$scanick($shift(nicks))$(de)$scanick($shift(nicks))$(de)$scanick($shift(nicks))/$pe)}
  ^hook scecho $cw $#nicks $nicks
  ${names_subtext ? [xecho -win $cw $cparse("$names_subtext" $0 $1 $n.cst)] : }
  ${format_names_footer ? [xecho -win $cw $fparse(format_names_footer $0 $1 $n.cst)] :}
  @ de = fl = fls = []}

alias sc scan
alias scan {
  ^local sc.
  @ sc.c = pattern(#* $*) ? pattern(#* $*) : C
  if (common($sc.c / $mychannels())) {
    @ sc.u = csort($channel($sc.c))
    @ sc.f = leftw(1 $pattern(-* $*)) ? leftw(1 $pattern(-* $*)) : [-a]
    @ sc.a = strip(. $pattern($copattern($sc.f a.flags a.pats) $sc.u))
    showusers $copattern($sc.f a.flags a.names) $sc.c $chanstats($sc.c) $sc.a} else {
    ge you are not on $(c.hk)\($(c.lc)$sc.c$(c.hk)\)$(cl).}}

alias scano scan -o $*
alias scann scan -n $*
alias scanv scan -v $*
alias scani scan -i $*
