import subprocess
import os
import cryptobox.plugins.base


## specify (in seconds), how long we should wait before redirecting and ip change
REDIRECT_DELAY=20
CHANGE_IP_DELAY=1

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

	pluginCapabilities = [ "system" ]
	pluginVisibility = [ "preferences" ]
	requestAuth = True
	rank = 30

	def doAction(self, store=None, redirected="", ip1="", ip2="", ip3="", ip4=""):
		## if we were redirected, then we should display the default page
		self.cbox.log.debug("executing network plugin")
		if redirected == "1":
			self.cbox.log.debug("network plugin: redirected")
			return None
		## request for IP change?
		if store:
			self.cbox.log.debug("network plugin: changing IP")
			try:
				for ip_in in (ip1, ip2, ip3, ip4):
					if (int(ip_in) < 0) or (int(ip_in) > 255):
						self.cbox.log.info("invalid IP supplied: %s" % str((ip1,ip2,ip3,ip4)))
						raise ValueError
				ip = "%d.%d.%d.%d" % (int(ip1), int(ip2), int(ip3), int(ip4))
			except ValueError:
				self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
				self.__prepareFormData()
				return "form_network"
			if self.__setIP(ip):
				self.cbox.log.info("the IP was successfully changed: %s" % ip)
				self.hdf["Data.Success"] = "Plugins.network.IPChanged"
				self.hdf["Data.Redirect.URL"] = self.__getRedirectDestination(ip)
				self.hdf["Data.Redirect.Delay"] = REDIRECT_DELAY
				return None
			else:
				self.cbox.log.warn("failed to change IP address to: %s" % ip)
				self.hdf["Data.Warning"] = "Plugins.network.InvalidIP"
				self.__prepareFormData()
				return "form_network"
		else:
			self.cbox.log.debug("network plugin: show form")
			## just show the form
			self.__prepareFormData()
			return "form_network"


	def getStatus(self):
		return "%d.%d.%d.%d" % self.__getCurrentIP()


	def __getRedirectDestination(self, ip):
		import cherrypy
		req = cherrypy.request
		base_parts = req.base.split(":")
		dest = "%s:%s" % (base_parts[0], ip)
		if len(base_parts) == 3:
			dest += ":%s" % base_parts[2]
		return dest


	def __prepareFormData(self):
		(oc1, oc2, oc3, oc4) = self.__getCurrentIP()
		self.hdf[self.hdf_prefix + "ip.oc1"] = oc1
		self.hdf[self.hdf_prefix + "ip.oc2"] = oc2
		self.hdf[self.hdf_prefix + "ip.oc3"] = oc3
		self.hdf[self.hdf_prefix + "ip.oc4"] = oc4


	def __getCurrentIP(self):
		import re
		import imp
		## load some values from the root_action.py script
		root_action_plug = imp.load_source("root_action", os.path.join(self.pluginDir, "root_action.py"))
		## get the current IP of the network interface
		proc = subprocess.Popen(
			shell = False,
			stdout = subprocess.PIPE,
			args = [
				root_action_plug.IFCONFIG_BIN,
				root_action_plug.IFACE])
		(stdout, stderr) = proc.communicate()
		if proc.returncode != 0: return (0,0,0,0)
		## this regex matches the four numbers of the IP
		match = re.search(u'inet [\w]+:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\s', stdout)
		if match:
			## use the previously matched numbers
			return tuple([int(e) for e in match.groups()])
		else:
			return (0,0,0,0)


	def __setIP(self, ip):
		import threading
		## call the root_action script after some seconds - so we can deliver the page before
		def delayedIPchange():
			import time
			time.sleep(CHANGE_IP_DELAY)
			proc = subprocess.Popen(
				shell = False,
				stderr = subprocess.PIPE,
				args = [
					self.cbox.prefs["Programs"]["super"],
					self.cbox.prefs["Programs"]["CryptoBoxRootActions"],
					"plugin",
					os.path.join(self.pluginDir, "root_action.py"),
					ip])
			proc.wait()
			if proc.returncode != 0:
				self.cbox.log.warn("failed to change IP address: %s" % ip)
				self.cbox.log.warn("error output: %s" % str(proc.stderr.read()))
			return
		thread = threading.Thread()
		thread.run = delayedIPchange
		thread.setDaemon(True)
		thread.start()
		# TODO: how could we guess, if it failed?
		return True



