# vim:ts=4:autoindent

require 'Log'

class WhosOn
    def initialize(dd)
        @dd = dd
        @dd.on_event( self, :type => :privmsg, :priv_to => :station, :message => /^who/ ){ |ev, md| do_whoson(ev, md) }
    end

    def do_whoson(ev, md)
        user = ev[:user]
        count = 0
        @dd.stations.each do |st_line, st|
            msg = [ "~--> On #{st.name}" ]
            empty = true
            st.users.each do |u|
                if !u.nil? and !u.is_remote
                    empty = false
                    count += 1
                    msg << "^#{u.lha}"
                    a = (Time.now - u.idle).to_i / 60
                    msg << " idle #{a}m" if a > 0
                end
            end
            msg << " -- Empty" if empty
            user.privmsg(msg.join + "\r")
        end
        user.privmsg(Time.now.strftime("~--> [#{count} user#{count > 1 ? "s" : ""}] %a %m/%d %I:%M %p\r"))
    end
end