;Lagbar Powered by X.a.S.H

menu @lag.win {
  &Color
  .&Red:{ lag.red }
  .&Blue:{ lag.blue }
  .&Green:{ lag.green }
  .&Yellow:{ lag.yellow }
  .&Orange:{ lag.orange }
  .-
  .&Custom:{ lag.custom }
  &Delay
  .&Current $+($lag.delay,s):{ lag.stime }
  -
  $iif($active ischan,&say lag):{ lag.msg }
  &Method
  .$iif($readini($lag.spc($script),lagstuff,method) = raw,$style(1) raw,raw):{ lag.method.raw }
  .$iif($readini($lag.spc($script),lagstuff,method) = ctcp,$style(1) ctcp,ctcp):{ lag.method.ctcp }
  .-
  .$iif($readini($lag.spc($script),lagstuff,method) = notice,$style(1) notice,notice):{ lag.method.notice }
  $iif($version >= 6.0,&network)
  .&current $lower($scid($timer(.lag.chk).cid).network):{ echo $color(info) -a *** Currently checking your lag on Network: $+($chr(2),$scid($timer(.lag.chk).cid).network,$chr(2)) Server: $+($chr(2),$scid($timer(.lag.chk).cid).server,$chr(2)) }
  -
  &Close:{ lag.stop | echo $color(info) -a *** Stopped all lag bar functions, to turn back on type /lag.start }
}
alias -l lag.bar {
  window -phB +d @lag.win 0 0 115 22
  tbwin Attach @lag.win
  var %z = $dll($_tbwin,GetTBInfo,NOT_USED),%w = $gettok(%z,1,32),%h = $gettok(%z,2,32)
  lagbar.size %w %h
  tbwin OnSize /lagbar.size
  lag.update
  window -a @lag.win
}
alias -l lag.update {
  if ($window(@lag.win)) {
    var %l = $hget(txlag,curlag),%t = Lag: $lag.isms($hget(txlag,curlag))
    drawrect -nrf @lag.win $rgb(face) 1 0 0 215 22
    drawtext -nro @lag.win $rgb(text) Tahoma -7 $calc(70 - $int($width(%t, Tahoma, -7))) 7 %t
    drawrect -nr @lag.win $rgb(text) 2 92 3 105 18
    drawrect -nrf @lag.win $lag.color 1 94 5 $iif(%l > 10,$iif(%l != ??,101,0),$int($calc(%l * 10.1))) 14
    drawdot @lag.win
  }
}
alias lagbar.size { window @lag.win $calc($1 - 205) $int($calc(($2 - 22) / 2)) 215 22 }
on ^*:notice:lag*:?: { 
  if ($nick = $me) && ($2 isnum) && ($hget(txlag)) { 
    hadd txlag curlag $calc($calc($ticks - $2) / 1000) 
    lag.update
    haltdef 
  }
  elseif (!$hget(txlag)) { 
    lag.stop 
    echo $color(info) -a *** Hash table error shutting down lag bar!
    haltdef
  }
}
ctcp *:lag*:?:{ 
  if ($nick = $me) && ($2 isnum) && ($hget(txlag)) {
    hadd txlag curlag $calc($calc($ticks - $2) / 1000)
    lag.update  
    halt 
  }
  elseif (!$hget(txlag)) { 
    lag.stop 
    echo $color(info) -a *** Hash table error shutting down lag bar!
    halt
  }
}
raw &421:*:{ 
  if ($1 = $me) && ($2 isnum) { 
    $iif($hget(txlag),hadd txlag curlag $calc($calc($ticks - $2) / 1000)) 
    lag.update 
  }
  halt
}
alias lag.start {
  if (!$readini($lag.spc($script),lagstuff,method)) { lag.method }
  if (!$hget(txlag)) { 
    hmake txlag 1 
    hadd txlag curlag ?? 
  } 
  lag.bar 
  lag.fill
  lag.chk
}
alias -l lag.method { 
  $iif($input(Would you like to use a notice for lag check if not press no then a ctcp will be used you can also use the raw method from popup!,8),writeini -n $lag.spc($script) lagstuff method notice,writeini $lag.spc($script) lagstuff method ctcp) 
  lag.stop
  $iif($server,lag.start)
}
alias -l _tbwin { 
  if ($isfile($+($scriptdir,tbwin.dll))) {
    return $lag.spc($+($scriptdir,tbwin.dll))
  }
  elseif ($findfile($mircdir,tbwin.dll,1)) {
    return $lag.spc($ifmatch)
  }
  else {
    lag.stop
    .timer.lag.error $iif($version >= 6.0,-i) 2 5 echo $color(info) -a *** Couldn't locate tbwin.dll in your mIRC or script directory, please put tbwin.dll in your mIRC or script directory so lag bar can function!
  }
}
alias -l lag.color { 
  if ($readini($lag.spc($script),lagstuff,color)) {
    return $ifmatch
  }
  else {
    return $rgb(255,0,0)
  }
}
alias -l lag.spc { return $+($chr(34),$1-,$chr(34)) }
alias -l lag.red { writeini -n $lag.spc($script) lagstuff color $rgb(255,0,0) | lag.fill }
alias -l lag.blue { writeini -n $lag.spc($script) lagstuff color $rgb(0,0,255) | lag.fill }
alias -l lag.green { writeini -n $lag.spc($script) lagstuff color $rgb(12,240,30) | lag.fill }
alias -l lag.yellow { writeini -n $lag.spc($script) lagstuff color $rgb(255,255,0) | lag.fill }
alias -l lag.orange { writeini -n $lag.spc($script) lagstuff color $rgb(252,127,0) | lag.fill }
alias -l lag.custom { writeini -n $lag.spc($script) lagstuff color $$input(Enter a custom rgb value which you can get by using $+($chr(36),rgb) identifier!,129) | lag.fill }
alias -l tbwin { dll $_tbwin $1- }
alias -l lag.chk { 
  if ($readini($lag.spc($script),lagstuff,method) != raw) {
    .timer.lag.chk $iif($version >= 6.0,-i) 0 $lag.delay $&
      $+(lag.,$readini($lag.spc($script),lagstuff,method)) | lag.ignore.chk
  } 
  else { 
    .timer.lag.chk $iif($version >= 6.0,-i) 0 $lag.delay $+(lag.,$readini($lag.spc($script),lagstuff,method))
  }
} 
alias -l lag.delay { return $iif($readini($lag.spc($script),lagstuff,delay),$ifmatch,20) }
alias lag.stime { 
  writeini -n $lag.spc($script) lagstuff delay $$input(Delay in seconds to check lag recommended 10 or more seconds!,129)
  lag.chk
}
alias -l lag.method.raw { writeini -n $lag.spc($script) lagstuff method raw | lag.stop | $iif($server,lag.start) }
alias -l lag.method.ctcp { writeini -n $lag.spc($script) lagstuff method ctcp | lag.stop | $iif($server,lag.start) }
alias -l lag.method.notice { writeini -n $lag.spc($script) lagstuff method notice | lag.stop | $iif($server,lag.start) }
alias -l lag.ctcp { $iif($server,.ctcp $me lag $ticks) }
alias -l lag.notice {  $iif($server,.notice $me lag $ticks) }
alias -l lag.raw {  $iif($server,.raw $ticks) }
alias -l lag.isms { if ($1 isnum) { return $+($1,s) } | else { return $1 } }
alias -l lag.msg { $iif($active ischan,msg # Currently my lag is: $lag.isms($hget(txlag,curlag))) }
alias lag.stop { 
  $iif($window(@lag.win),window -c @lag.win)
  .timer.lag.chk off 
  $iif($hget(txlag),hfree txlag)
}
on *:load: { 
  if ($version >= 5.91) { 
    lag.method 
    echo $color(info) -a *** Lagbar loaded successfully, right click on lagbar for options! 
  }
  else {
    echo $color(info) -a *** This needs mIRC version 5.91+ this is $version , this addon wont work unloading it!
    .unload -rs " $+ $script $+ "
  }
}
on *:connect:{ 
  if ($version >= 6.0) && (!$window(@lag.win)) { 
    lag.start
  }
  elseif ($version = 5.91) {
    lag.start
  }
}
on *:unload:{ lag.stop | remini $lag.spc($script) lagstuff }
on *:active:*:{ if ($version >= 6.0) && ($timer(.lag.chk)) && ($activecid != $timer(.lag.chk).cid) { scid $activecid lag.chk } }
on *:disconnect:{ 
  if ($version >= 6.0) { 
    var %s = 1,%t 
    while (%s <= $scon(0)) {
      $iif($scon(%s).status = connected,inc %t)
      inc %s
    }
    $iif(!%t,lag.stop)
  }
  elseif ($version = 5.91) {
    lag.stop 
  }
}
alias -l lag.fill {
  if ($hget(txlag)) {
    var %f = 1,%g = 50,%h = $hget(txlag,curlag)
    while (%f <= 10) {
      $+(.timer.lag.fill,$r(1,999)) -h 1 %g hadd txlag curlag %f  
      $+(.timer.lag.fill2,$r(1,9999)) -h 1 $calc(%g + 1) lag.update
      inc %f
      inc %g 50
    }
  }
  if ($hget(txlag)) {
    var %i = 10,%j = 500
    while (%i >= 0) {
      $+(.timer.lag.clr,$r(1,999)) -h 1 %j hadd txlag curlag %i 
      $+(.timer.lag.clr2,$r(1,9999)) -h 1 $calc(%j + 1) lag.update
      dec %i
      inc %j 50
    }
  }
  .timer.lag.restore -h 1 1000 hadd txlag curlag %h
  .timer.lag.restore2 -h 1 1050 lag.update
}
alias -l lag.ignore.chk { 
  if ($ignore($address($me,5))) && (notice isin $ignore($ifmatch).type) {   
    .timer.lag.error2 1 5 echo $color(info) -a *** You are ignoring notice's from yourself lagbar wont work,fix then type /lag.start
    lag.stop
  }
  elseif ($ignore($address($me,5))) && (ctcp isin $ignore($ifmatch).type) {
    .timer.lag.error3 1 5 echo $color(info) -a *** You are ignoring ctcp's from yourself lagbar wont work, fix then type /lag.start
    lag.stop
  }
  elseif ($ignore(*!*@*)) && (notice isin $ignore($ifmatch).type) {
    .timer.lag.error4 1 5 echo $color(info) -a *** You are ignoring notice's, your lagbar wont work, fix then type /lag.start
    lag.stop
  }
  elseif ($ignore(*!*@*)) && (ctcp isin $ignore($ifmatch).type) {
    .timer.lag.error5 1 5 echo $color(info) -a *** You are ignoring ctcps, your lagbar wont work, fix then type /lag.start
    lag.stop
  }
}


