<commit_before># -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Name:         sfp_email
# Purpose:      SpiderFoot plug-in for scanning retreived content by other
#               modules (such as sfp_spider) and identifying e-mail addresses
#
# Author:      <NAME> <<EMAIL>>
#
# Created:     06/04/2012
# Copyright:   (c) <NAME> 2012
# Licence:     GPL
# -------------------------------------------------------------------------------

import re
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent


class sfp_email(SpiderFootPlugin):
    """E-Mail:Footprint,Investigate:Identify e-mail addresses in any obtained data."""

    # Default options
    opts = {
        # options specific to this module
        'includeexternal': False  # Include e-mail addrs on external domains
    }

    # Option descriptions
    optdescs = {
        'includeexternal': "Report e-mail addresses not on the target base domain-name?"
    }

    def setup(self, sfc, userOpts=dict()):
        self.sf = sfc

        for opt in userOpts.keys():
            self.opts[opt] = userOpts[opt]

    # What events is this module interested in for input
    def watchedEvents(self):
        return ["*"]

    # What events this module produces
    # This is to support the end user in selecting modules based on events
    # produced.
    def producedEvents(self):
        return ["EMAILADDR"]

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data
        parentEvent = event.sourceEvent

        if eventName == "EMAILADDR":
            return None

        self.sf.debug("Received event, " + eventName + ", from " + srcModuleName)

        if type(eventData) not in [str, unicode]:
            self.sf.debug("Unhandled type to find e-mails: " + str(type(eventData)))
            return None

        pat = re.compile("([\%a-zA-Z\.0-9_\-]+@[a-zA-Z\.0-9\-]+\.[a-zA-Z\.0-9\-]+)")
        matches = re.findall(pat, eventData)
        for match in matches:
            if len(match) < 4:
                self.sf.debug("Likely invalid address: " + match)
                continue

            # Handle messed up encodings
            if "%" in match:
                self.sf.debug("Skipped address: " + match)
                continue

            mailDom = match.lower().split('@')[1]
            if not self.opts['includeexternal'] and not \
                    self.getTarget().matches(mailDom):
                self.sf.debug("Ignoring e-mail address on an external domain: " + match)
                continue

            self.sf.info("Found e-mail address: " + match)
            mail = unicode(match, 'utf-8', errors='replace')
            evt = SpiderFootEvent("EMAILADDR", mail, self.__name__, parentEvent)
            self.notifyListeners(evt)

        return None

# End of sfp_email class
<commit_msg>Proper bug fix for handling unicode.<commit_after># -*- coding: utf-8 -*-
# -------------------------------------------------------------------------------
# Name:         sfp_email
# Purpose:      SpiderFoot plug-in for scanning retreived content by other
#               modules (such as sfp_spider) and identifying e-mail addresses
#
# Author:      <NAME> <<EMAIL>>
#
# Created:     06/04/2012
# Copyright:   (c) <NAME> 2012
# Licence:     GPL
# -------------------------------------------------------------------------------

import re
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent


class sfp_email(SpiderFootPlugin):
    """E-Mail:Footprint,Investigate:Identify e-mail addresses in any obtained data."""

    # Default options
    opts = {
        # options specific to this module
        'includeexternal': False  # Include e-mail addrs on external domains
    }

    # Option descriptions
    optdescs = {
        'includeexternal': "Report e-mail addresses not on the target base domain-name?"
    }

    def setup(self, sfc, userOpts=dict()):
        self.sf = sfc

        for opt in userOpts.keys():
            self.opts[opt] = userOpts[opt]

    # What events is this module interested in for input
    def watchedEvents(self):
        return ["*"]

    # What events this module produces
    # This is to support the end user in selecting modules based on events
    # produced.
    def producedEvents(self):
        return ["EMAILADDR"]

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data
        parentEvent = event.sourceEvent

        if eventName == "EMAILADDR":
            return None

        self.sf.debug("Received event, " + eventName + ", from " + srcModuleName)

        if type(eventData) not in [str, unicode]:
            self.sf.debug("Unhandled type to find e-mails: " + str(type(eventData)))
            return None

        pat = re.compile("([\%a-zA-Z\.0-9_\-]+@[a-zA-Z\.0-9\-]+\.[a-zA-Z\.0-9\-]+)")
        matches = re.findall(pat, eventData)
        for match in matches:
            if len(match) < 4:
                self.sf.debug("Likely invalid address: " + match)
                continue

            # Handle messed up encodings
            if "%" in match:
                self.sf.debug("Skipped address: " + match)
                continue

            mailDom = match.lower().split('@')[1]
            if not self.opts['includeexternal'] and not \
                    self.getTarget().matches(mailDom):
                self.sf.debug("Ignoring e-mail address on an external domain: " + match)
                continue

            self.sf.info("Found e-mail address: " + match)
            if type(match) == str:
                mail = unicode(match, 'utf-8', errors='replace')
            else:
                mail = match
            evt = SpiderFootEvent("EMAILADDR", mail, self.__name__, parentEvent)
            self.notifyListeners(evt)

        return None

# End of sfp_email class

{'fraction_non_alphanumeric': 0.15283562960589556, 'fraction_numerical': 0.007049022749118872, 'mean_word_length': 1.6253153910849454, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3177590', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1646, 'n_words': 475}