on *:load: {
  if ($version < 6.16) { echo -a This addon requires at least mIRC 6.16 to use. Unloading... | .unload -rs chanscan.mrc }
  else { echo -a ChanScan 2.5 has been successfully loaded! Type /chanscan to open the Channel Scanner. | hmake array 1 }
}
alias -l mdx { dll $+(",$scriptdir\dll\mdx.dll,") $1- }
menu channel,status {
  Channel Scanner: dialog -m scan scan
}
dialog scan {
  title Channel Scanner [/chanscan]
  size -1 -1 450 260
  option pixels
  list 1, 5 10 440 215, size
  edit "", 2, 5 233 100 21, autohs
  button "Cerca", 3, 371 233 75 21, flat
  text "Cerca:",4, 115 236 50 21
  combo 5, 163 233 150 21, drop
}

on *:dialog:scan:*:*:{
  if ($devent == init) {
    if (!$array(array).exists) { hmake array 1 }
    mdx SetMircVersion $version
    mdx MarkDialog $dname
    mdx SetControlMDX $dname 1 treeview haslines hasbuttons showsel rowselect insertdroptarget linesatroot > [ $mdx.vdll ]
    mdx SetFont $dname 1 15 400 arial
    did -a $dname 5 Clones
    did -a $dname 5 IRCops
    did -a $dname 5 Aways
    did -a $dname 5 Bots
    did -a $dname 5 Nick Registrati
    did -a $dname 5 Nick non Registrati
    did -a $dname 5 Tutti gli Inattivi
    did -a $dname 2 $active
  }
  if ($devent == sclick) {
    if ($did == 3) { 
      if (($left($did($dname,2),1) == $chr(35)) && ($me ison $did($dname,2))) {
        if ($did($dname,5) == clones) { 
          if ($did($dname,2) == Status Window) { .enable #scan | who $did($dname,2) }
          else { scanclone $did($dname,2) }
        }
        if ($did($dname,5) == ircops) { .enable #scan | who $did($dname,2) } 
        if ($did($dname,5) == aways) { .enable #scan | who $did($dname,2) }
        if ($did($dname,5) == Everybody's Idle) { scanidle $did($dname,2) }
        if ($did($dname,5) == bots) { .enable #scan | who $did($dname,2) }
        if ($did($dname,5) == registered users) { .enable #scan | who $did($dname,2) }
        if ($did($dname,5) == unregistered users) { .enable #scan | who $did($dname,2) }
      }
      elseif ($did($dname,2) == Status Window) { .enable #scan | who }
      elseif (($left($did($dname,2),1) == $chr(35)) && ($me !ison $did($dname,2))) { var %n = $input(Warning! $crlf $+ $crlf $+ Not on Channel: $did($dname,2),dwao,Warning!) }
      else { var %n = $input(Warning! $crlf $+ $crlf $+ Not a Channel: $did($dname,2),dwao,Warning!) }
    }
  }
}
alias -l scanclone {
  did -r scan 1
  did -a $dname 1 +bce 1 2 0 2 0 Clone Scan Risultati per $chan($did(scan,2)) $+ .
  did -i $dname 1 1 cb 2
  var %i 1,%nicks,%anicks,%o
  while ($nick($1,%i)) {
    var %x 1,%nick
    while (%x <= $nick($1,%x)) {
      if (%x == %i) { goto next }
      if ($address($nick($1,%i),2) == $address($nick($1,%x),2)) && ($address($nick($1,%i),2)) && ($nick($1,%i) !isin %nicks) {
        if (!%nick) { var %nick = $nick($1,%i) }
        var %nicks = $addtok(%nicks,%nick,44)
        var %anicks = $addtok(%anicks,$address($nick($1,%i),2),44)
      }
      :next
      inc %x
    }
    inc %i
  }
  if (%nicks) {
    var %o = 1
    while (%o <= $numtok(%anicks,44)) {
      var %l = 1
      did -a $dname 1 +e 1 2 $gettok(%anicks,%o,44)
      did -i $dname 1 1 cb $calc(2 + (%o -1))
      while ($gettok(%nicks,%l,44)) {
        if ($gettok(%anicks,%o,44) == $address($gettok(%nicks,%l,44),2)) { did -a scan 1 $gettok(%nicks,%l,44) $chr(9) $address($gettok(%nicks,%l,44),5) }
        inc %l
      }
      did -i $dname 1 1 cb root 2
      inc %o
    }
  }
  else { did -a scan 1 Clones Found: 0 }
}
Alias -l scanidle {
  did -r scan 1
  did -a $dname 1 +bce 1 2 0 2 0 Scan per $did(scan,5) on $chan($did(scan,2)) ha avuto questi risultati.
  did -i $dname 1 1 cb 2
  var %i 1
  while ($nick($did(scan,2),%i)) {
    did -a scan 1 $nick($did(scan,2),%i) $+ : $duration($nick($did(scan,2),%i).idle,3) $chr(9) $address($nick($did(scan,2),%i),5)
    inc %i
  }
}
#scan off
raw 352:*:{
  if (($did(scan,5) == clones) || ((G isin $7) && ($did(scan,5) == aways)) || ((* isin $7) && ($did(scan,5) == ircops)) || ((B isin $7) && ($did(scan,5) == bots)) || ((r isin $7) && ($did(scan,5) == registered users)) || ((r !isin $7) && ($did(scan,5) == unregistered users))) { 
    inc %copcount 
    array cop $6
    array copanicks $+($3,@,$4)
  }
  halt
}
raw 315:*:{
  did -r scan 1
  did -a scan 1 +bce 1 2 0 2 0 Scan per $did(scan,5) in $chan($did(scan,2)) ha avuto i seguenti risultati.
  did -i scan 1 1 cb 2
  var %i 0,%nicks,%anicks,%o
  if ($array(cop).count == 1) { 
    array nicks $array(cop,0)
    array anicks $+(*!*,@,$gettok($array(copanicks,0),2,$asc(@)))
    goto skiploop 
  }
  while ($array(cop,%i)) {
    var %x 0,%nick
    while (%x <= $array(cop).count) {
      if (($did(scan,5) == Clones) && (%x == %i)) { echo $array(cop,%i) | goto next }
      if ($address($array(cop,%i),2) == $address($array(cop,%x),2)) && (!$array(nicks,$array(cop,%i)).find)  {
        array nicks $array(cop,%i)
        array anicks $+(*!*,@,$gettok($array(copanicks,%i),2,$asc(@)))
      }
      :next
      inc %x
    }
    inc %i
  }
  :skiploop
  if ($array(nicks,0)) {
    var %o = 0,%i = 0
    while (%o <= $array(anicks).count) {
      var %l = 0
      :return
      if ($array(addedanicks,$array(anicks,%o)).find) { inc %o | goto return }
      if ($array(anicks,%o)) { did -a scan 1 +e 1 2 $array(anicks,%o) }
      did -i scan 1 1 cb $calc(2 + %i) 
      while ($array(nicks,%l)) {
        if ($array(anicks,%o) == $array(anicks,%l)) { 
          did -a scan 1 $array(nicks,%l) $chr(9) $array(anicks,%l) 
          array addedanicks $array(anicks,%o)
        }
        inc %l
      }
      did -i scan 1 1 cb root 2 
      inc %o
      inc %i
    }
  }
  else { did -a scan 1 $did(scan,5) Found: 0 }
  .disable #scan
  array -c cop,copanicks,nicks,anicks,addedanicks
  halt
}
#scan end
on *:join:*: { if (($nick == $me) && ($dialog(scan))) { loadscan $chan } }
alias -l loadscan { enable #scan | .who $1 }
alias -l mdx.vdll { return $scriptdir\dll\views.mdx }
alias chanscan { dialog -m scan scan }
;################
;#### Arrays ####
;################

alias array {
  if ($isid) {
    if ((!$prop) || ($prop == get)) { return $hget(array,$+($1,$chr(91),$2,$chr(93))) }
    if ($prop == find) { 
      var %get_array = $hfind(array,$+($1,$chr(91),*,$chr(93)),1,w)
      var %i = 0
      while ($hget(array,$+($gettok(%get_array,1,91),$chr(91),%i,$chr(93)))) {
        if ($hfind(array,$2-,0,n).data >= 1) {
          if ($2- == $hget(array,$+($gettok(%get_array,1,91),$chr(91),%i,$chr(93)))) { return $true }
        }
        else { return $false }
        inc %i
      }
    }
    if ($prop == filter) {
      ;##########################
      ;### Reconstruct Arrays ###
      ;##########################
      var %i = 1
      hmake array1 1
      while ($hget(array,$hget(array,%i).item)) {
        if ($gettok($hget(array,%i).item,1,91) == $1) {
          var %count = $hfind(array,$gettok($hget(array,%i).item,1,91),0,w)
          var %o = 1
          while ($hget(array,%o).item) {
            hadd array1 $hget(array,%o).item $hget(array,$hget(array,%o).item)
            hdel array $hget(array,%o).item
            inc %o
          }
          while (%count >= 0) {
            hadd array $hget(array1,%count).item $hget(array1,$hget(array1,%count).item)
            dec %count
          }
        }
        inc %i
      }
      hfree array1
    }
    if ($prop == exists) { 
      if ($hget(array)) { return $true }
      else { return $false }
    }
    if ($prop == count) { $iif($hfind(array,$+($1,$chr(91),*,$chr(93)),0,w),return $ifmatch,return 0) }
    if ($prop == print) {
      var %i = 1
      while ($hget(array,$hget(array,%i).item)) {
        echo $hget(array,%i).item $ifmatch
        inc %i
      }
    }
  }
  else {
    if (-* iswm $1) { 
      if (*c* iswm $1) {
        if (*a* iswm $1) { hfree array | hmake array 1 }
        else { 
          if ($numtok($2,44) > 1) { 
            var %i = 1
            while ($gettok($2,%i,44)) {
              hdel -w array $+($ifmatch,,$chr(91),*,$chr(93))
              inc %i
            }
          }
          else { hdel -w array $+($2,,$chr(91),*,$chr(93)) }
        }
      }
    }
    else { 
      var %array_find = $hfind(array,$+($1,$chr(91),*,$chr(93)),0,w)
      if (%array_find >= 1) { hadd array $+($1,$chr(91),%array_find,$chr(93)) $2- }
      else { hadd array $+($1,$chr(91),0,$chr(93)) $2- } 
    }
  }
}
