#	$eterna: listidle,v 1.2 2001/08/12 15:57:11 mrg Exp $
#
# tristan+irc@ethereal.net 31jul2000
#
# bugs/lacking features:
# - doesn't work on servers that don't support multiple-nick /whois (parts
#   of EFnet) -- this could be automatically detected and worked around
# - only works on current channel
# - if you do a /whois while /listidle is running, it will get confused

# for $notword(), $lformat(), $format(), $sort()
load functions

alias fixdiff {
  if ([$2]) {
    @ function_return = [$0$left(1 $1)$fixdiff($2-)]
  }
  {
    @ function_return = [$0$left(1 $1)]
  }
}

alias listidle {
  if (left(2 $0) == [-s]) {
    @ LI.SORTED = 1
  }
  whois $fixsend($chanusers($C))
  ^on ^raw_irc "% 311 *" {
    @ LI.USERHOST[$encode($3)] = [$4@$5]
  }
  ^on ^raw_irc "% 319 *" #
  ^on ^raw_irc "% 313 *" #might want to do something here (IRCop)
  ^on ^raw_irc "% 301 *" #might want to do something here (away)
  ^on ^raw_irc "% 317 *" {
    @ LI.IDLE[$encode($3)] = [$4]
    @ LI.SIGNON[$encode($3)] = [$5]
  }
  ^on ^raw_irc "% 312 *" {
    if ([$0]!=[$4]) {
      @ LI.SERVERS[$encode($4)] = LI.SERVERS[$encode($4)] ## [ $3]
    }
  }
  ^on ^raw_irc "% 318 *" {
    ^on raw_irc -"% 311 *"
    ^on raw_irc -"% 319 *"
    ^on raw_irc -"% 313 *"
    ^on raw_irc -"% 301 *"
    ^on raw_irc -"% 317 *"
    ^on raw_irc -"% 312 *"
    ^on raw_irc -"% 318 *"
    li.rwhois
  }
}

alias li.rwhois {
  @ LI.COUNT = 0
  foreach li.servers serv {
    whois $decode($serv) $fixsend($li.servers[$serv])
    @ LI.COUNT = LI.COUNT + 1
    ^assign -li.servers.$serv
  }
  ^assign -serv
  ^on ^raw_irc "% 311 *" #
  ^on ^raw_irc "% 319 *" #
  ^on ^raw_irc "% 313 *" #might want to do something here (IRCop)
  ^on ^raw_irc "% 301 *" #might want to do something here (away)
  ^on ^raw_irc "% 317 *" {
    @ LI.IDLE[$encode($3)] = [$4]
    @ LI.SIGNON[$encode($3)] = [$5]
  }
  ^on ^raw_irc "% 312 *" #
  ^on ^raw_irc "% 318 *" {
    @ LI.COUNT = LI.COUNT - 1
    if (!LI.COUNT) {
      ^on raw_irc -"% 311 *"
      ^on raw_irc -"% 319 *"
      ^on raw_irc -"% 313 *"
      ^on raw_irc -"% 301 *"
      ^on raw_irc -"% 317 *"
      ^on raw_irc -"% 312 *"
      ^on raw_irc -"% 318 *"
      if (li.sorted) {
        li.sortedlist
      } {
        li.list
      }
      ^assign -li.sorted
    }
  }
}

alias li.list {
  echo ***  Nickname        Idle Sign-On      Account
  foreach li.idle li.nick {
    echo ***  $lformat(9 $decode($li.nick)) $format(10 $fixdiff($li.prettydiff($tdiff($li.idle[$li.nick])))) $strftime($li.signon[$li.nick] %b %e %H:%M) $li.userhost[$li.nick]
    ^assign -li.idle[$li.nick]
    ^assign -li.signon[$li.nick]
    ^assign -li.userhost[$li.nick]
  }
  ^assign -li.nick
}

alias li.prettydiff {
  @ li.args = [$*]
  if ([$[6]word(${#li.args-1} $*)] == [second] && #li.args != 2) {
    @ li.args = notword(${#li.args-1} $notword($#li.args $*))
  }
  if (#li.args > 3) {
    @ function_return = [$word(0 $li.args) $word(1 $li.args) $word(2 $li.args) $word(3 $li.args)]
  } {
    @ function_return = [$word(0 $li.args) $word(1 $li.args)]
  }
  ^assign -li.args
}

alias li.sortedlist {
  @ li.curtime = time()
  foreach li.idle nick {
    @ li.since = li.curtime - li.idle[$nick]
    while (@li.zeroes < 10 - @li.since) {
      @ li.zeroes = li.zeroes ## [0]
    }
    @ li.tosort = [$encode($li.zeroes$li.since).$nick ] ## li.tosort
    ^assign -li.zeroes
  }
  @ li.sorted = sort($li.tosort)
  @ li.count = 0
  echo ***  Nickname        Idle Sign-On      Account
  while (word($li.count $li.sorted)) {
    @ li.code = word($li.count $li.sorted)
    @ li.nick = mid(21 999 $li.code)
    echo ***  $lformat(9 $decode($li.nick)) $format(10 $fixdiff($li.prettydiff($tdiff(${li.curtime - decode($left(20 $li.code))})))) $strftime($li.signon[$li.nick] %b %e %H:%M) $li.userhost[$li.nick]
    ^assign -li.idle[$li.nick]
    ^assign -li.signon[$li.nick]
    ^assign -li.userhost[$li.nick]
    @ li.count = li.count + 1
  }
  ^assign -li.sorted
  ^assign -li.tosort
  ^assign -li.curtime
  ^assign -li.count
  ^assign -li.code
  ^assign -li.since
  ^assign -li.nick
  ^assign -li.diff
}
