; StatusColor version 1.6c
; a mIRC Remote Script that colors IRC Opers and Away Users in the Nicklist
; 1`2004 by basti "ibt|bla" raff
; contact me in #ibt or #tatooine @ quakenet
; please distribute the complete zip-file only!

menu menubar {
  StatusColor Config:/statuscolor

}



on *:LOAD: {
  set %stc.Version 1.6c
  stc.initCNick
  if (!$exists($scriptdirstcicons\0.jpg)) {
    echo 4 -s Error Loading StatusColor: jpg-icons or stcicons-dir missing
    unload -rs $scriptdir $+ statuscolor.mrc
    return
  }
  if (!$exists(statuscolor.ini)) {
    writeini statuscolor.ini colors oper 8
    writeini statuscolor.ini colors away 12
    writeini statuscolor.ini colors igno 14
    writeini statuscolor.ini colors deaf 3
    writeini statuscolor.ini colors blnk 5
    writeini statuscolor.ini blink enable 1
  }

  set %stc.AwayColor $readini(statuscolor.ini,colors,away)
  set %stc.IgnoredColor $readini(statuscolor.ini,colors,igno)
  set %stc.OperColor $readini(statuscolor.ini,colors,oper)
  set %stc.DeafColor $readini(statuscolor.ini,colors,deaf)
  set %stc.BlinkColor $readini(statuscolor.ini,colors,blnk)
  set %stc.Blink $readini(statuscolor.ini,blink,enable)


  echo 4 -s StatusColor %stc.Version by ibt|bla loaded.
  echo 4 -s To change the Colors type "/statuscolor" or go to Menu "Commands" -> "StatusColor Config"
  echo 4 -s To uninstall type "/unload -rs statuscolor.mrc" and delete the statuscolor.mrc, statuscolor.ini and the stcicons dir.
}

on *:UNLOAD: {
  stc.unsetCNick
  hfree -w stc.chanlist*
  unset %stc.*
  echo 4 -s Unloading StatusColor %stc.Version
  echo 4 -s To uninstall delete the statuscolor.mrc, statuscolor.ini and the stcicons dir.
}

on *:CONNECT: {
  set %stc.AwayColor $readini(statuscolor.ini,colors,away)
  set %stc.IgnoredColor $readini(statuscolor.ini,colors,igno)
  set %stc.OperColor $readini(statuscolor.ini,colors,oper)
  set %stc.DeafColor $readini(statuscolor.ini,colors,deaf)
  set %stc.BlinkColor $readini(statuscolor.ini,colors,blnk)
  set %stc.Blink $readini(statuscolor.ini,blink,enable)

  stc.initCNick
}

on *:DISCONNECT: {
  stc.initCNick
  writeini statuscolor.ini colors oper %stc.OperColor
  writeini statuscolor.ini colors away %stc.AwayColor
  writeini statuscolor.ini colors igno %stc.IgnoredColor
  writeini statuscolor.ini colors deaf %stc.DeafColor
  writeini statuscolor.ini colors blnk %stc.BlinkColor
  writeini statuscolor.ini blink enable %stc.Blink
}

on *:ACTIVE:*: {
  if ($active ischan ) {
    if ($server != $null) {
      stc.doWhoCNick $active
    }
  }
  if ($lactive ischan ) {
    .timer $+ $lactive off
  }
}

on *:NICK: {
  if (($cnick($nick).color == %stc.AwayColor) || ($cnick($nick).color == %stc.DeafColor)) {
    .timer 1 2 stc.doWhoCNickSingle $nick
  }
  else {
    if ((off isin $nick) || (away isin $nick) || (bnc isin $nick)) {
      .timer 1 2 stc.doWhoCNickSingle $nick
    }
  }
}

on *:PART:#: {
  if ($cnick($nick).color == %stc.AwayColor) {
    .cnick -r $nick
  }
  if ($me == $nick) {
    .timerrefreshCNick 1 30 stc.refreshCNick
  }
}

on *:QUIT: {
  if ($cnick($nick).color == %stc.AwayColor || $cnick($nick).color == %stc.IgnoredColor) {
    .cnick -r $nick
  }
}

RAW 301:*: {
  unset %stc.doneWhois
  if ($cnick($2).color == $color(normal)) {
    stc.setaway $2
    ;.cnick -m2 $2 %stc.AwayColor
  }
}

RAW 311:*: {
  set %stc.doneWhois 1
}

RAW 315:*: {
  if (%stc.hideWho. [ $+ [ $2 ] ] == 1) {
    unset %stc.hideWho. [ $+ [ $2 ] ]
    .halt
  }
}

RAW 318:*: {
  if (%stc.doneWhois) {
    if ($cnick($2).color == %stc.AwayColor) {
      stc.removeaway $2
      ;.cnick -r $2
    }
    unset %stc.doneWhois
  }
}

alias stc.removeaway {
  .cnick -r $1
  if (%stc.Blink) {
    .timer -m 1 500 stc.blinkaway $1 16
  }
}

alias -l stc.blinkaway {
  if (($timer(0) > 24) && ($int($calc($2 / 2)) == $calc($2 / 2))) {
    return
  }

  var %blinkcycle
  if ($cnick($1).color == %stc.BlinkColor) {
    if ($3) {
      .cnick -m2 $1 $3
    }
    else {
      .cnick -r $1
    }
  }
  else {
    .cnick -m2 $1 %stc.BlinkColor
  }
  set %blinkcycle $calc($2 - 1)
  if (%blinkcycle > 0) {
    .timer -m 1 500 stc.blinkaway $1 %blinkcycle $3
  }  
} 

alias -l stc.setaway {
  .cnick -m2 $1 %stc.AwayColor
  if (%stc.Blink) {
    .timer -m 1 500 stc.blinkaway $1 16 %stc.AwayColor
  }
}



RAW 352:*: {

  if (* isin $7) {
    .cnick -m2 $6 %stc.OperColor
  } 
  else {
    if (channel isin $ignore($+($6,!,$3,@,$4)).type) {       
      .cnick -m2 $6 %stc.IgnoredColor
    }
    else {
      if (d isin $7) {
        .cnick -m2 $6 %stc.DeafColor
      }
      else {
        if ($left($7,1) == H) {
          if (($cnick($6).color == %stc.AwayColor) || ($cnick($6).color == %stc.IgnoredColor)) {
            stc.removeaway $6
          }
        }
        else {
          if (($cnick($6).color == $color(normal)) || ($cnick($6).color == %stc.IgnoredColor)) {
            stc.setaway $6
          }
        }
      }
    }
  }
  if ((%stc.hideWho. [ $+ [ $2 ] ] == 1) || (%stc.hideWho. [ $+ [ $3 ] ] == 1)) {
    .halt
  }
}

alias -l stc.initCNick {
  unset %stc.doneWhoCNick
  hfree -w stc.chanlist*
  stc.unsetCNick
  set %stc.AwayColor $readini(statuscolor.ini,colors,away)
  set %stc.IgnoredColor $readini(statuscolor.ini,colors,igno)
  set %stc.OperColor $readini(statuscolor.ini,colors,oper)
  set %stc.DeafColor $readini(statuscolor.ini,colors,deaf)
  set %stc.BlinkColor $readini(statuscolor.ini,colors,blnk)
  set %stc.Blink $readini(statuscolor.ini,blink,enable)

}

alias -l stc.doWhoCNick {
  var %chanCNick $1
  if (!%stc.doneWhoCNick) {
    if (!$hget(stc.chanlist, %chanCNick)) {  
      set -u5 %stc.doneWhoCNick 1
      hadd -mu900 stc.chanlist %chanCNick 1
      set %stc.hideWho. [ $+ [ %chanCNick ] ] 1
      who %chanCNick
    } 
  }
  else {
    if (!$hget(stc.chanlist, %chanCNick)) {
      .timer $+ %chanCNick 1 5 /stc.doWhoCNick %chanCNick
    }
  }
}

alias -l stc.doWhoCNickSingle {
  set %stc.hideWho. [ $+ [ $1 ] ] 1 
  /who $1
}

alias -l stc.unsetCNick {
  var %counterCNick 1
  var %emptyCNick 0
  while (%emptyCNick < 16)  {
    if (($cnick(%counterCNick).color == $color(normal)) && (!$cnick(%counterCNick))) {
      inc %emptyCNick 1
      inc %counterCNick
    }
    elseif (($cnick(%counterCNick).color == %stc.IgnoredColor) || ($cnick(%counterCNick).color == %stc.AwayColor) || ($cnick(%counterCNick).color == %stc.DeafColor) || ($cnick(%counterCNick).color == %stc.OperColor)) {
      .cnick -r %counterCNick
    }
    else {
      inc %counterCNick
    }
  }
}

alias -l stc.refreshCNick {
  var %counterCNick 1
  var %emptyCNick 0
  while (%emptyCNick < 16)  {
    if (!$cnick(%counterCNick)) {
      inc %emptyCNick 1
      inc %counterCNick
    } 
    if (($ial($cnick(%counterCNick)) == $null) && (($cnick(%counterCNick).color == %stc.DeafColor) || ($cnick(%counterCNick).color == %stc.AwayColor) || ($cnick(%counterCNick).color == %stc.OperColor) || ($cnick(%counterCNick).color == %stc.IgnoredColor))) {
      .cnick -r %counterCNick
    } 
    else {
      inc %counterCNick 1
    }
  }
}


alias statuscolor {
  /dialog -m stc.ConfigDialog stc.ConfigDialogTable
  did -g stc.ConfigDialog 2 " $+ $scriptdirstcicons\ $+ %stc.OperColor $+ .jpg $+ "
  did -g stc.ConfigDialog 3 " $+ $scriptdirstcicons\ $+ %stc.AwayColor $+ .jpg $+ " 
  did -g stc.ConfigDialog 4 " $+ $scriptdirstcicons\ $+ %stc.IgnoredColor $+ .jpg $+ " 
  did -g stc.ConfigDialog 11 " $+ $scriptdirstcicons\ $+ %stc.DeafColor $+ .jpg $+ " 
  did -g stc.ConfigDialog 14 " $+ $scriptdirstcicons\ $+ %stc.BlinkColor $+ .jpg $+ " 
  if (%stc.Blink) { 
    did -c stc.ConfigDialog 16 
    set %stc.dialog.blinkenable 1
  }
  else {
    did -u stc.ConfigDialog 16
  }
}

alias -l stc.ShowChooser { 
  if ( !$dialog(stc.ChooseDialog) ) {
    dialog -m stc.ChooseDialog stc.ChooseDialogTable
  }
  else {
    dialog -o stc.ChooseDialog
  }
  dialog -t stc.ChooseDialog %stc.DialogChooseTitle Color
}

on *:dialog:stc.ChooseDialog:sclick:*:{
  if (($did != $null) && ($did < 17)) {
    var %col
    set %col $calc($did - 1)
    if (%stc.dialogchange == oper) {
      if ((%stc.dialog.away != %col) && (%stc.dialog.igno != %col)) {
        set %stc.dialog. [ $+ [ %stc.dialogchange ] ] %col
        did -g stc.ConfigDialog %stc.DialogTemp " $+ $scriptdirstcicons\ $+ %col $+ .jpg $+ " | dialog -x stc.ChooseDialog

      }
    }
    if (%stc.dialogchange == away) {
      if ((%stc.dialog.oper != %col) && (%stc.dialog.igno != %col)) {
        set %stc.dialog. [ $+ [ %stc.dialogchange ] ] %col
        did -g stc.ConfigDialog %stc.DialogTemp " $+ $scriptdirstcicons\ $+ %col $+ .jpg $+ " | dialog -x stc.ChooseDialog

      }

    }
    if (%stc.dialogchange == igno) {
      if ((%stc.dialog.away != %col) && (%stc.dialog.oper != %col)) {
        set %stc.dialog. [ $+ [ %stc.dialogchange ] ] %col
        did -g stc.ConfigDialog %stc.DialogTemp " $+ $scriptdirstcicons\ $+ %col $+ .jpg $+ " | dialog -x stc.ChooseDialog

      }
    }
    if (%stc.dialogchange == deaf) {
      set %stc.dialog. [ $+ [ %stc.dialogchange ] ] %col
      did -g stc.ConfigDialog %stc.DialogTemp " $+ $scriptdirstcicons\ $+ %col $+ .jpg $+ " | dialog -x stc.ChooseDialog

    }
    if (%stc.dialogchange == blnk) {
      set %stc.dialog. [ $+ [ %stc.dialogchange ] ] %col
      did -g stc.ConfigDialog %stc.DialogTemp " $+ $scriptdirstcicons\ $+ %col $+ .jpg $+ " | dialog -x stc.ChooseDialog

    }



  }
}


on *:dialog:stc.ConfigDialog:sclick:*:{
  if ($did == 2) { .set %stc.dialogchange oper | .set %stc.DialogChooseTitle Opers | .set %stc.DialogTemp $did | stc.ShowChooser }
  if ($did == 3) { .set %stc.dialogchange away | .set %stc.DialogChooseTitle Away Users | .set %stc.DialogTemp $did | stc.ShowChooser }
  if ($did == 4) { .set %stc.dialogchange igno | .set %stc.DialogChooseTitle Ignored Users | .set %stc.DialogTemp $did | stc.ShowChooser }
  if ($did == 11) { .set %stc.dialogchange deaf | .set %stc.DialogChooseTitle Deaf Users | .set %stc.DialogTemp $did | stc.ShowChooser }
  if ($did == 14) { .set %stc.dialogchange blnk | .set %stc.DialogChooseTitle Blink Color | .set %stc.DialogTemp $did | stc.ShowChooser }
  if ($did == 16) { 
    if ($did(stc.ConfigDialog,16).state) {
      set %stc.dialog.blinkenable 1    
    } 
    else {
      unset %stc.dialog.blinkenable
    }
  }
  if ($did == 18) {

  }
  if ($did == 1) {
    stc.ApplyChange
    if ($dialog(stc.ChooseDialog)) {
      dialog -x stc.ChooseDialog
    }

  }
  if ($did == 7) {
    stc.ApplyChange
  }
  if ($did == 8) {
    if ($dialog(stc.ChooseDialog)) {
      dialog -x stc.ChooseDialog
    }

  }
}

alias -l stc.ApplyChange {
  var %counterCNick 1
  var %emptyCNick 0
  var %nick
  if (((%stc.dialog.deaf) && (%stc.dialog.deaf != %stc.DeafColor)) || ((%stc.dialog.oper) && (%stc.dialog.oper != %stc.OperColor)) || ((%stc.dialog.away) && (%stc.dialog.away != %stc.AwayColor)) || ((%stc.dialog.igno) && (%stc.dialog.igno != %stc.IgnoredColor))) {

    while ((%emptyCNick < 16) && (%counterCNick < 5000))  {
      if (!$cnick(%counterCNick)) {
        inc %emptyCNick 1
        inc %counterCNick 1
      }
      else {
        set %nick $cnick(%counterCNick)
        if (($cnick(%counterCNick).color == %stc.AwayColor) && ((%stc.dialog.away) && (%stc.dialog.away != %stc.AwayColor))) {
          .cnick -r %counterCNick
          .cnick -m2 %nick %stc.dialog.away
        } 
        elseif (($cnick(%counterCNick).color == %stc.OperColor) && ((%stc.dialog.oper) && (%stc.dialog.oper != %stc.OperColor))) {
          .cnick -r %counterCNick
          .cnick -m2 %nick %stc.dialog.oper
        } 
        elseif (($cnick(%counterCNick).color == %stc.IgnoredColor) && ((%stc.dialog.igno) && (%stc.dialog.igno != %stc.IgnoredColor))) {
          .cnick -r %counterCNick
          .cnick -m2 %nick %stc.dialog.igno
        } 
        elseif (($cnick(%counterCNick).color == %stc.DeafColor) && ((%stc.dialog.deaf) && (%stc.dialog.deaf != %stc.DeafColor))) {
          .cnick -r %counterCNick
          .cnick -m2 %nick %stc.dialog.deaf
        } 
        else {
          inc %counterCNick 1
        }
      }



    }

  }

  if (%stc.dialog.oper != $null) { set %stc.OperColor %stc.dialog.oper }
  if (%stc.dialog.away != $null) { set %stc.AwayColor %stc.dialog.away }
  if (%stc.dialog.igno != $null) { set %stc.IgnoredColor %stc.dialog.igno }
  if (%stc.dialog.deaf != $null) { set %stc.DeafColor %stc.dialog.deaf }
  if (%stc.dialog.blnk != $null) { set %stc.BlinkColor %stc.dialog.blnk }
  set %stc.Blink %stc.dialog.blinkenable
  writeini statuscolor.ini colors oper %stc.OperColor
  writeini statuscolor.ini colors away %stc.AwayColor
  writeini statuscolor.ini colors igno %stc.IgnoredColor
  writeini statuscolor.ini colors deaf %stc.DeafColor
  writeini statuscolor.ini colors blnk %stc.BlinkColor
  if (%stc.Blink == $null) { set %stc.Blink 0 }
  writeini statuscolor.ini blink enable %stc.Blink
  unset %stc.dialog.*

}


dialog stc.ConfigDialogTable {
  title "StatusColor Config"
  size -1 -1 128 136
  option dbu
  button "&Ok", 1, 11 114 30 12, default ok
  icon 2, 21 13 12 12,  " $+ $scriptdirstcicons\1.jpg $+ ", 0
  icon 3, 93 13 12 12,  " $+ $scriptdirstcicons\2.jpg $+ ", 0
  icon 4, 45 13 12 12,  " $+ $scriptdirstcicons\3.jpg $+ ", 0
  button "&Apply", 7, 50 114 30 12, default
  button "&Cancel", 8, 88 114 30 12, default cancel
  box "Blink Nickname on Away Status Change", 9, 8 56 113 48
  icon 11, 69 13 12 12,  " $+ $scriptdirstcicons\3.jpg $+ ", 0
  text "Opers", 5, 18 29 20 8, center
  text "Away Users", 6, 88 29 22 15, center
  text "Ignored Users", 10, 39 29 23 15, center
  text "Deaf Users", 12, 65 29 20 15, center
  box "Colors", 13, 8 4 113 46
  icon 14, 95 64 12 12,  " $+ $scriptdirstcicons\2.jpg $+ ", 0
  text "Blink Color", 15, 90 80 22 15, center
  check "Enable Blinking", 16, 15 69 50 10
  ;  edit "", 17, 52 86 20 10
  ;  text "Blink duration", 18, 14 87 37 8
  ;  text "s", 19, 74 87 9 8
}

dialog stc.ChooseDialogTable {
  size -1 -1 166 46
  icon 1, 3 3 20 20, " $+  $scriptdirstcicons\0.jpg $+ "
  icon 2, 23 3 20 20, " $+ $scriptdirstcicons\1.jpg $+ "
  icon 3, 43 3 20 20, " $+ $scriptdirstcicons\2.jpg $+ "
  icon 4, 63 3 20 20, " $+ $scriptdirstcicons\3.jpg $+ "
  icon 5, 83 3 20 20, " $+ $scriptdirstcicons\4.jpg $+ "
  icon 6, 103 3 20 20, " $+ $scriptdirstcicons\5.jpg $+ "
  icon 7, 123 3 20 20, " $+ $scriptdirstcicons\6.jpg $+ "
  icon 8, 143 3 20 20, " $+ $scriptdirstcicons\7.jpg $+ "
  icon 9, 3 23 20 20, " $+ $scriptdirstcicons\8.jpg $+ "
  icon 10, 23 23 20 20, " $+ $scriptdirstcicons\9.jpg $+ "
  icon 11, 43 23 20 20, " $+ $scriptdirstcicons\10.jpg $+ "
  icon 12, 63 23 20 20, " $+ $scriptdirstcicons\11.jpg $+ "
  icon 13, 83 23 20 20, " $+ $scriptdirstcicons\12.jpg $+ "
  icon 14, 103 23 20 20, " $+ $scriptdirstcicons\13.jpg $+ "
  icon 15, 123 23 20 20, " $+ $scriptdirstcicons\14.jpg $+ "
  icon 16, 143 23 20 20, " $+ $scriptdirstcicons\15.jpg $+ "
  button "ok", 200, 10 10 10 10, ok hide
}
