# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
# Mobius Forensic Toolkit
# Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 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 chrome
import firefox

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

  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  # @brief initialize object
  # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  def __init__ (self, item):
    self.__item = item
    self.applications = []
    self.cookies = []
    self.form_history = []
    self.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.applications = data.applications
        self.cookies = data.cookies
        self.form_history = data.form_history
        self.history = data.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.applications = self.applications
    data.cookies = self.cookies
    data.form_history = self.form_history
    data.history = self.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):

    # Clear ignored KFF, if any
    pymobius.remove_kff_ignore (self.__item, 'browser')

    # Retrieve activity data according to OS's found
    for opsys in pymobius.operating_system.scan (self.__item):
      chrome.retrieve (opsys, self.__item, self)
      firefox.retrieve (opsys, self.__item, self)

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