^on ^305 * #
^on ^306 * #

^on ^set "aw_logpage *" {
  @ aw.logpage = [$1-]}

^on ^set "aw_page *" {
  @ aw.page = [$1-]}

^on ^set "aw_log *" {
  @ aw.log = [$1-]}

^on ^set "aw_none *" {
  @ aw.none = [$1-]}

^on ^set "aw_public *" {
  @ aw.public = [$1-]}

^on ^set "aw_away *" {
  @ aw.away = [$1-]}

^on ^set "aw_back *" {
  @ aw.back = [$1-]}

^on #-idle 23 * {
  if ([$0] == a.aawtime && !flag(away) && flag(a.autoaway)) {
    away $sar(g/%m/$(a.aawtime)/$a.aawmsg)}}

alias mme {
  ^local ii
  fe ($mychannels()) ii {
    ^quote privmsg $(ii) :ACTION $*}
  fe ($winrefs()) ii {
    xecho -win $(ii) $fparse(format_send_action_other $tolower($Z) $N * $*)}}

alias a.ltag {
  if (flag(a.msglog) && flag(a.pager)) {
    @ function_return = aw.logpage} elsif (flag(a.msglog) && !flag(a.pager)) {
    @ function_return = aw.log} elsif (!flag(a.msglog) && flag(a.pager)) {
    @ function_return = aw.page} else {
    @ function_return = aw.none}}

alias a.gt {
  @ function_return = time() - a.gtime}

alias a.upd {
  if (flag(away)) {
    ^quote away :$stripansicodes($cparse("$aw.away" $a.ltag() $a.gt() $a.r))
    ^timer -refnum a 18 a.upd} else {
    ^quote away}}

alias gone away

alias away {
  @ a.gtime = time()
  @ a.r = [$*] ? [$*] : a.awaymsg
  @ away = 1
  ^timer -delete a
  ^hook dlog startlog opened message log.
  if (flag(a.pubaway) && mychannels()) {
    mme $stripansicodes($cparse("$aw.public" $a.ltag() $a.r))} else {
    ge you have now been set away$(c.hk):$(cl) $a.r}
  a.upd}

alias back {
  ^local b.
  if (flag(away)) {
    @ b.r = [$*] ? [$*] : a.backmsg
    ^timer -delete a
    ^quote away
    ^hook dlog closelog closed message log.
    if (flag(a.pubaway) && mychannels()) {
      mme $stripansicodes($cparse("$aw.back" $a.gt() $b.r))} else {
      ge you have now been set back$(c.hk):$(cl) $b.r}
    @ a.gtime = a.r = away = []
    if (_a.msgs) {
      switch ($flag(a.logprom)) {
        (1) {
          input_char "read awaylog now? (y/n) " if ([$0]==[y]) {readlog}}
        (0) {
          ge you have messages. \($_a.msgs\)  use /readlog to read your log, /remlog to delete it.}}}} else {
    ge you aren't away!}}

^on #-msg 551 * {
  if (!match($0 $mnicks) && flag(away) && flag(a.awaynot)) {
    push mnicks $0
    a.not $0
    expire ${a.awaynotime} mnicks $0}}

alias a.not {
  if (flag(away) && flag(a.awaynot)) {
    ^notice $0 $tr(/ //$a.ltag()) ${a.email ? [\($(a.email)\) ] :}\(left${strftime(%m/%d) != strftime($a.gtime %m/%d) ? [ on $strftime($a.gtime %m/%d)] : }@$tolower($strftime($a.gtime %I:%M%p))\)}}
set msglog off
set auto_away off

^on ^ctcp "% % PAGE *" {
  if (flag(a.pager) && !ischannel($1) && flag(away)) {
    beep
    ge $(c.hk)[$(c.lc)page$(c.lm)/$(cl)$0$(c.hk)] [$(c.lc)$userhost()$(c.hk)]$(cl) $3-
    notice $0 \(ar\) your request has been logged.}}

^on ^hook "dlog % *" {
  if (flag(away) && flag(a.msglog) && a.loglevels =~ [*$1*]) {
    if ([$1-]!=_lastlogline) {
      @ _lastlogline=[$1-]
      @ dlf = open($a.msglogfile W T)
      if (_l.rn) {
        @ write($dlf $datetime($time())$(c.hk):$(cl) last message repeated ${_l.rn+1} times.)
        @ _l.rn=0} 
      if (match($1 closelog startlog)) {
        @ write($dlf $datetime($time())$(c.hk):$(cl) $2-)} else {
        @ _a.msgs++
        @ write($dlf $datetime($time())$(c.hk):$(cl) $fparse(format_$1 $1-))}
      @ close($dlf)} else {
      @ _l.rn++}}}

alias play readlog
alias readlog {
  if (fexist($a.msglogfile)==1) {
    @ readinglog = 1
    ^local rl.
    @ rl.c = pattern(-r $*) ? [tac] : [cat]
    @ rl.g = remw(-r $*) ? [| grep $remw(-r $*)] :
    less_pipe blah $rl.c $a.msglogfile $rl.g} else {
    ge the message log is empty.}}

on ^hook "dellog" {
  if (flag(a.logprom)) {
    input_char "delete this awaylog? (y/n) " if ([$0]==[y]) {remlog}}}

alias remlog {
  @ _a.msgs = 0
  @ unlink($a.msglogfile)
  ge $a.msglogfile has been removed.}
#sjk/psy'97
