# -*- coding: latin-1 -*-
'''
webOutput.py

Copyright 2007 Mariano Nuņez Di Croce @ CYBSEC

This file is part of sapyto, http://www.cybsec.com/EN/research/tools/sapyto.php

sapyto 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.

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

from core.controllers.basePlugin.baseOutputPlugin import baseOutputPlugin
import sys

class webOutput(baseOutputPlugin):
	'''
	This plugin prints all messages to the web GUI.
	
	@author: Mariano Nuņez Di Croce <mnunez@cybsec.com>
	'''
	
	def __init__(self):
		baseOutputPlugin.__init__(self)
		self.verbosity = 0
		self._msgCache = {}
		# Initialize cache per level
		self._msgCache['debug'] = []
		self._msgCache['info'] = []
		self._msgCache['error'] = []
		self._msgCache['vuln'] = []
		self._msgCache['console'] = []
		

	def debug(self, message, newLine = True ):
		'''
		This method is called from the output object. The output object was called from a plugin
		or from the framework. This method should take an action for debug messages.
		'''
	
		toPrint = str ( message )
		if newLine == True:
			toPrint += '\n'
		self._msgCache['debug'].append(toPrint)

	
	def information(self, message , newLine = True ):
		'''
		This method is called from the output object. The output object was called from a plugin
		or from the framework. This method should take an action for informational messages.
		'''	
		toPrint = str ( message )
		if newLine == True:
			toPrint += '\n'
		self._msgCache['info'].append(toPrint)


	def error(self, message , newLine = True ):
		'''
		This method is called from the output object. The output object was called from a plugin
		or from the framework. This method should take an action for error messages.
		'''		
		toPrint = str ( message )
		if newLine == True:
			toPrint += '\n'
		self._msgCache['error'].append(toPrint)

	def vulnerability(self, message , newLine = True ):
		'''
		This method is called from the output object. The output object was called from a plugin
		or from the framework. This method should take an action when a vulnerability is found.
		'''		
		toPrint = str ( message )
		if newLine == True:
			toPrint += '\n'
		self._msgCache['vuln'].append(toPrint)
		
	def console( self, message, newLine = True ):
		'''
		This method is used by the sapyto console to print messages to the outside.
		'''
		pass

	def logHttp( self, request, response):
		pass
	
	def getMessageCache(self, debug=False ):
		'''
		This method retrieves message cache and clears it afterwards
		'''
		res = {}
		
		if debug:
			res = self._msgCache.copy()
		else:
			res = self._msgCache.copy().pop('debug')
			
		# empty the cache.
		for it in self._msgCache:
			self._msgCache[it] = []
		return res

	def getLongDesc( self ):
		'''
		@return: A DETAILED description of the plugin functions and features.
		'''
		return '''
		This plugin writes the framework messages to a cache, where the webUI can then read the data
		to show to the user in the client browser. This plugin is the "glue" that joins w3af and the client browser.
		You should only enable it if you are running a webUI or testing something wierd. 
		
		Note: When you run w3af with the "-w" flag ( web ), this plugin is auto-enabled.
		'''
