#
# $Id$
# $Revision$
#
require 'serialport'

module Msf

###
#
# This class hooks all session creation events and sends a signal to a serial port
#
###

  class Plugin::SessionAlarm < Msf::Plugin


    attr_accessor :blinks, :serial, :port, :baud_rate, :data_bits, :stop_bits, :parity

    include Msf::SessionEvent

    def on_session_open(session)
      print_status "opened session type: #{session.type}"
      blink_led
    end

    def on_session_close(session, reason='')
      print_status "closing session"
      sid = session.sid.to_s
    end

    def initialize(framework, opts)
      super

      # Subscribe to session events
      self.framework.events.add_session_subscriber(self)

      # Grab options
      self.blinks = opts['blinks'].to_i || 50
      self.port = opts['port'] || "/dev/ttyUSB0"
      self.baud_rate = opts['baud_rate'] || 9600
      self.data_bits = opts['data_bits'] || 8
      self.stop_bits = opts['stop_bits'] || 1
      self.parity = opts['parity'] || SerialPort::NONE

      self.serial = SerialPort.new(self.port, self.baud_rate, self.data_bits, self.stop_bits, self.parity)
    end

    def cleanup
      self.framework.events.remove_session_subscriber(self)
      self.serial.close
    end

    def name
      "session_alarm"
    end

    def desc
      "Automatically plays a sound when various framework events occur"
    end

    def blink_led()
      for x in 1..self.blinks
        self.serial.write "s"
      end
    end

  end
end

