# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Mobius Forensic Toolkit
# Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2019,2020 Eduardo Aguiar
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import os.path
import datetime
import pymobius
import pymobius.json_serializer
import pymobius.operating_system
import mobius
import chromium
import gecko
import internet_explorer

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# @brief Browser model
# @author Eduardo Aguiar
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class model (object):

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief initialize object
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def __init__ (self, item):
    self.__item = item
    self.form_history = []
    self.profiles = []

    self.__load_data ()

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief check if data is loaded
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def is_data_available (self):
    return self.__is_data_available

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief load data from .json file
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def __load_data (self):
    self.__is_data_available = False
    path = self.__item.get_data_path ('browser.json')

    if os.path.exists (path):
      fp = open (path)
      data = pymobius.json_serializer.unserialize (fp.read ())
      fp.close ()

      if data.mtime >= self.__item.datasource.mtime:
        self.form_history = data.form_history
        self.profiles = data.profiles
        self.__is_data_available = True

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief save data into .json file
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def __save_data (self):

    # create data model
    data = pymobius.Data ()
    data.form_history = self.form_history
    data.profiles = self.profiles
    data.mtime = datetime.datetime.utcnow ()

    # create file
    path = self.__item.create_data_path ('browser.json')
    fp = open (path, 'w')

    try:
      fp.write (pymobius.json_serializer.serialize (data))
      fp.close ()

    except Exception, e:
      print ('pymobius.browser.__save_data: Warning %s' % e)
      fp.close ()
      os.remove (path)

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief retrieve data
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def retrieve (self):

    # Retrieve activity data according to OS's found
    for opsys in pymobius.operating_system.scan (self.__item):
      try:
        chromium.retrieve (opsys, self.__item, self)
      except Exception, e:
        mobius.core.log ('WRN pymobius.browser: %s' % str (e))

      try:
        gecko.retrieve (opsys, self.__item, self)
      except Exception, e:
        mobius.core.log ('WRN pymobius.browser: %s' % str (e))

      try:
        internet_explorer.retrieve (opsys, self.__item, self)
      except Exception, e:
        mobius.core.log ('WRN pymobius.browser: %s' % str (e))

    # Save data model
    self.__save_data ()
    self.__is_data_available = True
