;MSChatCoffeeCupAway.mrc v1.1 (c) Woodsman, 3/30/2000.  This is an mIRC add-on script.
;To load into mIRC, type '/load -rs MSChatCoffeeCupAway11.mrc', w/o the quotes.
;
;MSChatCoffeeCupAway.mrc (Away.mrc, for short) simulates the MS Chat 'Coffee Cup' Away
;algorithms.  In MS Chat, when someone sets themselves Away, MS Chat sends an AWAY CTCP
;to others who are on the same channels.  Then, a coffee cup is displayed next to their
;name on channel nicklists, on their screen, and everyone else's who is using MS Chat.
;When they set themselves as Back, it sends an AWAY CTCP code again, and the coffee cup
;is removed from everyone's screens.
;
;Away.mrc simulates that for mIRC.  When you set yourself Away, it will send the correct
;codes to everyone on your channels, so that, for instance, if someone is using MS Chat,
;a coffee cup will appear next to your name on their screen.  Away.mrc also responds to the
;same codes from other users.  The difference between Away.mrc and MS Chat is that, since
;it's impossible to display a coffee cup in mIRC, when someone sets themselves Away,
;Away.mrc will instead change the color of their name on your channel nicklists.  When
;someone then sets themselves as Back, the color of their name will change back to it's
;original color.
;
;Before you use Away.mrc, just like with any new script, you should 1st compare it with
;other scripts that you have loaded, checking alias names, global variables, and remote
;CTCP's and Events, and modifying them if necessary, to ensure that there are no conflicts.
;As far as variables go, Away.mrc only uses 4 global variables, %JoinChannel, %AwayMsg,
;%AwayColor, and %BackColor.  The best way to test this script is to connect to Microsoft's
;own IRC network, at chat.msn.com, port 6667, in mIRC.  Many people there use MS Chat.
;
;This script is in 3 sections.  The 1st section initializes 2 variables.  The 2nd section
;sends the correct codes to put a coffee cup on others' screens when you set yourself as
;Away.  The 3rd section handles the same Away codes from other users, and changes the
;color of their name on your channel nicklists.  This script is (c) Woodsman, 3/30/2000.
;email: wbaldwin@netscape.net
;
;This event initializes the colors you want to use in the channel nicklists when people are
;Away or Back.  You can change them to suit your own preferences.  It is only run the 1st
;time you load this script.  14 = Gray, and 1 = Black.:
on 1:LOAD: {
  set %AwayColor 14
  set %BackColor 1
}
;
;This section will allow you to set yourself as Away, and put a coffee cup in the screens
;of those people in your channels who are using MS Chat.  Also, if there are other folks
;in the channel who are using mIRC and this script, it will be handled in the 3rd section,
;below, just as Away messages from MS Chat users are.  To set yourself as away, just
;type '/a awayreason', without the quotes.  This will put the coffee cup on other users'
;screens, and if people enter the channel while you're away, it will put a coffee cup up
;on their screens, too.  To return from away, and remove the coffee cup from other users'
;screens, just type '/b' without the quotes.
;This alias sets you as Away:
alias a {
  /away $$1- | %awaymsg = $1-
  sendallchan
}
;This alias sets you as Back:
alias b {
  unset %awaymsg
  /away
  sendallchan
}
;This alias is used to send the Away CTCP to the channel(s) you're on, and put a coffee
;cup on other users' screens, and also to remove the coffee cup when you set yourself as
;Back.  It is called by the previous 2 routines:
alias sendallchan {
  var %num1 = 1
  var %TotChans = $chan(0)

  :loopchans2
  var %currchan = $chan(%num1)
  if (%num1 > %TotChans) { goto end2 }
  /ctcp %currchan AWAY %awaymsg
  if ($away) { /cline %AwayColor %currchan $me }
  else { /cline %BackColor %currchan $me }
  inc %num1 1
  goto loopchans2
  :end2
}
;This event sends the ctcp to people who join the channel *while* you're Away:
on !1:join:#: {
  if ($away) /.ctcp $nick AWAY %awaymsg
}
;
;This section colors the name in the channel listbox on *your* screen, when *others*
;set themselves away.  It relies on dynamically created named user access levels, based
;on the channel that you are both on.  For instance, if someone sets themselves Away in the
;#GeneralChat channel, Away.mrc will create a #GeneralChat-Away access level.  When they
;set themselves as Back, the access level will be deleted.
;
;this event clears the channel level user list when you join a channel, if it exists.  It
;also sends the channel Away CTCP if you are set as Away:
on me:*:join:#: {
  /.rlevel # $+ -Away
  set %JoinChannel #
  if ($away) {
      /ctcp # AWAY %awaymsg
;the next line doesn't actually do anything in mIRC 5.7, because of an anomaly in
;mIRC, but I have left it here in case a future version of mIRC would support it.
      /cline %AwayColor # $me
  }
}
;this one responds to private Away msgs received when you join the channel, and changes
;the color of their name accordingly:
CTCP *:AWAY*:?: {
  /.auser -a %JoinChannel $+ -Away $nick
  /cline %AwayColor %JoinChannel $nick
}
;this one responds to channel Away msgs, and changes the color of their name accordingly:
CTCP *:AWAY*:#: {
  var %AccessLevel = $chan $+ -Away
  if $ulist($nick,%AccessLevel,1) == $null {
    /.auser -a %AccessLevel $nick
    /cline %AwayColor $chan $nick
  }
  else {
    /.ruser %AccessLevel $nick 
    /cline %BackColor $chan $nick
  }
}
;this one changes their user list entry, in case they change their nick while Away:
on !*:NICK: {
  var %num1 = 1
  var %TotChans = $chan(0)

  :loopchans3
  var %currchan = $chan(%num1)
  if (%num1 > %TotChans) { goto end3 }
  var %AccessLevel = %currchan $+ -Away
  if $ulist($nick,%AccessLevel,1) != $null {
    /.ruser %AccessLevel $nick
    /.auser -a %AccessLevel $newnick
    goto end3
  }
  inc %num1 1
  goto loopchans3
  :end3
}
;This one removes them from the user list if they /part the channel while their Away is set:
on !*:part:#: {
  var %AccessLevel = $chan $+ -Away
  if $ulist($nick,%AccessLevel,1) != $null {
    /.ruser %AccessLevel $nick
  }
}
;This one removes them from the user list if they /quit while their Away is set:
on !*:quit: {
  var %num1 = 1
  var %TotChans = $chan(0)

  :loopchans4
  var %currchan = $chan(%num1)
  var %AccessLevel = %currchan $+ -Away
  if (%num1 > %TotChans) { goto end4 }
  if $ulist($nick,%AccessLevel,1) != $null {
    /.ruser %AccessLevel $nick
    goto end4
  }
  inc %num1 1
  goto loopchans4
  :end4
}

