class Mybot::Command

Attributes

channel[RW]
exit[RW]

Public Class Methods

new() click to toggle source
# File lib/mybot/command.rb, line 7
def initialize
        @out = @stdout = @stderr = ""
        @handlers = {}
        @time = Time.now
        @exit = -1
end

Public Instance Methods

handle_close() click to toggle source
# File lib/mybot/command.rb, line 32
def handle_close
        @time = Time.now - @time
        print_cmd! "time", @time, :blue, :bold
        print_cmd! "exit", @exit, :blue, :bold

        unless @exit == 0
                print_cmd! "error", "invalid exit status", :red, :bold
                print_stdout @stdout, true
                print_stderr @stderr, true
                abort
        end
end
handle_stderr(data) click to toggle source
# File lib/mybot/command.rb, line 25
def handle_stderr(data)
        @out = data
        @stderr += data
        print_stderr data
        run_handlers
end
handle_stdout(data) click to toggle source
# File lib/mybot/command.rb, line 18
def handle_stdout(data)
        @out = data
        @stdout += data
        print_stdout data
        run_handlers
end
on(s, &block) click to toggle source
# File lib/mybot/command.rb, line 14
def on(s, &block)
        @handlers[s] = block
end
result() click to toggle source
# File lib/mybot/command.rb, line 55
def result
        { 
                :stdout => filter(@stdout),
                :stderr => filter(@stderr),
                :exit => @exit,
                :time => @time
        }
end
write(data) click to toggle source
# File lib/mybot/command.rb, line 45
def write(data)
        print_cmd! "write", data, :blue, :bold
        @channel.send_data "#{data}\n"
end
write!(data) click to toggle source
# File lib/mybot/command.rb, line 50
def write!(data)
        print_cmd! "write", asterisks(data), :blue, :bold
        @channel.send_data "#{data}\n"
end

Private Instance Methods

filter(data) click to toggle source
# File lib/mybot/command.rb, line 76
            def filter(data)
  data.gsub(/\[sudo\]\ password\ for\ [a-zA-Z0-9_].+\:/, "")
end
run_handlers() click to toggle source
# File lib/mybot/command.rb, line 66
def run_handlers
        @handlers.each do |s, block|
                if @out.include? s
                        puts if @out[-3..-1] == "\r\n" && $MYBOT_SHOW_OUTPUT
                        print_cmd! "handle", s, :blue, :bold
                        block.call
                end
        end
end