import core.controllers.outputManager as om

# options
from core.data.options.option import option
from core.data.options.optionList import optionList

from core.controllers.basePlugin.baseDiscoveryPlugin import baseDiscoveryPlugin
from core.controllers.w3afException import w3afException, w3afRunOnce

import core.data.kb.knowledgeBase as kb
import core.data.kb.vuln as vuln
import core.data.kb.info as info
import core.data.constants.severity as severity

from core.data.parsers.urlParser import url_object

class netcraft(baseDiscoveryPlugin):
    '''
    Find out "What's that site running?".
    
    @author: ino ( guerrino.dimassa@gmail.com )
    '''    
    def __init__(self):
        baseDiscoveryPlugin.__init__(self)

        # Internal variables
        self._exec = True
    
    def discover(self, fuzzableRequest ):
        '''
        Search netcraft and parse the output.
        
        @parameter fuzzableRequest: A fuzzableRequest instance that contains 
                                                    (among other things) the site to test.
        '''
        if not self._exec :
            # This will remove the plugin from the discovery plugins to be runned.
            raise w3afRunOnce()
        else:
            # Only run once
            self._exec = False                
            target_domain = fuzzableRequest.getURL().getRootDomain()
            
            # Example URL:
            # http://toolbar.netcraft.com/site_report?url=http://www.foo.org
            #
    
            netcraft_url_str = 'toolbar.netcraft.com/site_report?url=' + target_domain
            netcraft_url = url_object( netcraft_url_str )

            try:
               response = self._urlOpener.GET( netcraft_url )
            except w3afException, e:
                msg = 'An exception was raised while running netcraft plugin. Exception: ' + str(e)
                om.out.debug( msg )
    
        self._parse_netcraft_result( response )
    
    def _parse_netcraft_result(self, response):
        '''
        Parse the result from the netcraft site and create the corresponding info objects.
        
        @return: None
        '''

        if 'Netblock Owner' in response.getBody():
            i = info.info()
            i.setPluginName(self.getName())
            i.setName('Found')
            i.setURL( response.getURL() )
            msg = 'The target site found on netcraft. For more information'
            msg += ' please visit the following URL: "' + response.getURI() + '".'
            i.setDesc( msg )
            kb.kb.append( self, 'Target', i )
            om.out.information( i.getDesc() )
                
    def getOptions( self ):
        '''
        @return: A list of option objects for this plugin.
        '''    
        ol = optionList()
        return ol
        
    def setOptions( self, options ):
        '''
        This method sets all the options that are configured using the user interface 
        generated by the framework using the result of getOptions().
        
        @parameter OptionList: A dictionary with the options for the plugin.
        @return: No value is returned.
        ''' 
        pass
        
    def getPluginDeps( self ):
        '''
        @return: A list with the names of the plugins that should be runned before the
        current one.
        '''
        return []

    def getLongDesc( self ):
        return '''
        This plugin searches the netcraft database and parses the result.The information
        stored in that database is useful to know about Netblock Owner,IP address,OS,Web Server,Last changed
        of the site.
        '''


