import cryptobox.plugins.base
from cryptobox.core.exceptions import *


class volume_mount(cryptobox.plugins.base.CryptoBoxPlugin):

	pluginCapabilities = [ "volume" ]
	pluginVisibility = [ "volume" ]
	requestAuth = False
	rank = 0


	def doAction(self, action=None, pw=None):
		self.container = self.cbox.getContainer(self.device)
		if action == "mount_plain":
			return self.__doMountPlain()
		elif action == "mount_luks":
			return self.__doMountLuks(pw)
		elif action == "umount":
			return self.__doUmount()
		elif not action:
			return "volume_status"
		else:
			self.cbox.log.info("plugin 'volume_mount' - unknown action: %s" % action)
			return None
	

	def getStatus(self):
		container = self.cbox.getContainer(self.device)
		if not self.container:
			return "invalid device"
		if container.isMounted():
			return "active"
		else:
			return "passive"


	def __doMountPlain(self):
		if self.container.isMounted():
			self.hdf["Data.Warning"] = "Plugins.volume_mount.IsAlreadyMounted"
			self.cbox.log.info("the device (%s) is already mounted" % self.device)
			return "volume_status"
		if self.container.getType() != self.container.Types["plain"]:
			## not a plain container
			self.cbox.log.info("plugin 'volume_mount' - invalid container type")
			self.hdf["Data.Warning"] = "Plugins.volume_mount.InvalidContainerType"
			return "volume_status"
		try:
			self.container.mount()
		except CBMountError, errMsg:
			self.hdf["Data.Warning"] = "Plugins.volume_mount.MountFailed"
			self.cbox.log.warn("failed to mount the device (%s): %s" % (self.device, errMsg))
			return "volume_status"
		except CBContainerError, errMsg:
			self.hdf["Data.Warning"] = "Plugins.volume_mount.MountFailed"
			self.cbox.log.warn("failed to mount the device (%s): %s" % (self.device, errMsg))
			return "volume_status"
		self.cbox.log.info("successfully mounted the volume: %s" % self.device)
		self.hdf["Data.Success"] = "Plugins.volume_mount.MountDone"
		return "volume_status"


	def __doMountLuks(self, pw):
		if self.container.isMounted():
			self.hdf["Data.Warning"] = "Plugins.volume_mount.IsAlreadyMounted"
			self.cbox.log.info("the device (%s) is already mounted" % self.device)
			return "volume_status"
		if not pw:
			self.dataset["Data.Warning"] = "EmptyPassword"
			self.log.info("no password was supplied for mounting of device: '%s'" % self.device)
			return "volume_status"
		if self.container.getType() != self.container.Types["luks"]:
			## not a luks container - fail silently
			self.cbox.log.info("plugin 'volume_mount' - invalid container type")
			return "volume_status"
		try:
			self.container.mount(pw)
		except CBMountError, errMsg:
			self.hdf["Data.Warning"] = "Plugins.volume_mount.MountCryptoFailed"
			self.cbox.log.warn("failed to mount the device (%s): %s" % (self.device, errMsg))
			return "volume_status"
		except CBContainerError, errMsg:
			self.hdf["Data.Warning"] = "Plugins.volume_mount.MountCryptoFailed"
			self.cbox.log.warn("failed to mount the device (%s): %s" % (self.device, errMsg))
			return "volume_status"
		self.cbox.log.info("successfully mounted the volume: %s" % self.device)
		self.hdf["Data.Success"] = "Plugins.volume_mount.MountDone"
		return "volume_status"


	def __doUmount(self):
		if not self.container.isMounted():
			self.hdf["Data.Warning"] = "Plugins.volume_mount.IsNotMounted"
			self.cbox.log.info("the device (%s) is currently not mounted" % self.device)
			return "volume_status"
		try:
			self.container.umount()
		except CBUmountError, errMsg:
			self.hdf["Data.Warning"] = "UmountFailed"
			self.cbox.log.warn("could not umount the volume (%s): %s" % (self.device, errMsg))
			return "volume_status"
		self.cbox.log.info("successfully unmounted the container: %s" % self.device)
		self.hdf["Data.Success"] = "Plugins.volume_mount.UmountDone"
		return "volume_status"

