'''
miscSettings.py

Copyright 2006 Andres Riancho

This file is part of w3af, w3af.sourceforge.net .

w3af 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 version 2 of the License.

w3af 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 w3af; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

'''

from core.controllers.configurable import configurable
import core.data.kb.config as cf
from core.controllers.misc.parseOptions import parseOptions

class miscSettings(configurable):
	'''
	A class that acts as an interface for the user interfaces, so they can configure w3af settings using getOptionsXML and SetOptions.
	'''
	
	def __init__( self ):
		
		# User configured variables
		self._fuzzCookie = False
		self._fuzzFileContent = True
		self._fuzzFileContentExtension = 'txt'
		self._autoDependencies = True
		self._maxDepth = 10
		self._maxThreads = 0
		self._fuzzableHeaders = []
		self._maxDiscoveryLoops = 500
		
		cf.cf.save('fuzzableCookie', self._fuzzCookie )
		cf.cf.save('fuzzFileContent', self._fuzzFileContent )
		cf.cf.save('fuzzFCExt', self._fuzzFileContentExtension )
		cf.cf.save('autoDependencies', self._autoDependencies )
		cf.cf.save('maxDepth', self._maxDepth )
		cf.cf.save('maxThreads', self._maxThreads )
		cf.cf.save('fuzzableHeaders', self._fuzzableHeaders )
		cf.cf.save('maxDiscoveryLoops', self._maxDiscoveryLoops )
	
	def getOptionsXML(self):
		'''
		This method returns a XML containing the Options that the plugin has.
		Using this XML the framework will build a window, a menu, or some other input method to retrieve
		the info from the user. The XML has to validate against the xml schema file located at :
		w3af/core/ui/userInterface.dtd
		
		@return: XML with the plugin options.
		'''	
		return	'<?xml version="1.0" encoding="ISO-8859-1"?>\
		<OptionList>\
			<Option name="fuzzCookie">\
				<default>'+str(self._fuzzCookie)+'</default>\
				<desc>Indicates if w3af plugins will use cookies as a fuzzable parameter</desc>\
				<type>boolean</type>\
			</Option>\
			<Option name="fuzzFileContent">\
				<default>'+str(self._fuzzFileContent)+'</default>\
				<desc>Indicates if w3af plugins will send the fuzzed payload to the file forms</desc>\
				<type>boolean</type>\
			</Option>\
			<Option name="fuzzFCExt">\
				<default>'+str(self._fuzzFileContentExtension)+'</default>\
				<desc>Indicates the extension to use when fuzzing file content</desc>\
				<type>string</type>\
			</Option>\
			<Option name="fuzzableHeaders">\
				<default>'+','.join(self._fuzzableHeaders)+'</default>\
				<desc>A list with all fuzzable header names</desc>\
				<type>list</type>\
			</Option>\
			<Option name="autoDependencies">\
				<default>'+str(self._autoDependencies)+'</default>\
				<desc>Automatic dependency enabling for plugins</desc>\
				<type>boolean</type>\
			</Option>\
			<Option name="maxDepth">\
				<default>'+str(self._maxDepth)+'</default>\
				<desc>Maximum depth of the discovery phase</desc>\
				<help>For example, if set to 10, the webSpider plugin will only follow 10 links while spidering the site</help>\
				<type>integer</type>\
			</Option>\
			<Option name="maxThreads">\
				<default>'+str(self._maxThreads)+'</default>\
				<desc>Maximum number of threads that the w3af process will spawn</desc>\
				<type>integer</type>\
			</Option>\
			<Option name="maxDiscoveryLoops">\
				<default>'+str(self._maxDiscoveryLoops)+'</default>\
				<desc>Maximum number of times the discovery function is called.</desc>\
				<type>integer</type>\
			</Option>\
		</OptionList>\
		'
		
	def setOptions( self, OptionMap ):
		'''
		This method sets all the options that are configured using the user interface 
		generated by the framework using the result of getOptionsXML().
		
		@parameter OptionMap: A dictionary with the options for the plugin.
		@return: No value is returned.
		'''	
		f00, OptionMap = parseOptions( 'misc-settings', OptionMap )
		cf.cf.save('fuzzableCookie', OptionMap['fuzzCookie'] )
		cf.cf.save('fuzzFileContent', OptionMap['fuzzFileContent'] )
		cf.cf.save('fuzzFCExt', OptionMap['fuzzFCExt'] )
		cf.cf.save('autoDependencies', OptionMap['autoDependencies'] )
		cf.cf.save('maxDepth', OptionMap['maxDepth'] )
		cf.cf.save('maxThreads', OptionMap['maxThreads'] )
		cf.cf.save('fuzzableHeaders', OptionMap['fuzzableHeaders'] )
		cf.cf.save('maxDiscoveryLoops', OptionMap['maxDiscoveryLoops'] )
		
# This is an undercover call to __init__ :) , so I can set all default parameters.
miscSettings()
