Exemplo n.º 1
0
def main():
    args = get_args()

    parser = MailParser()

    if args.file:
        parser.parse_from_file(args.file)
    elif args.string:
        parser.parse_from_string(args.string)

    if args.json:
        j = json.loads(parser.parsed_mail_json)
        safe_print(json.dumps(j, ensure_ascii=False, indent=4))

    if args.body:
        # safe_print(parser.body)
        safe_print(parser.body)

    if args.headers:
        safe_print(parser.headers)

    if args.to:
        safe_print(parser.to_)

    if args.from_:
        safe_print(parser.from_)

    if args.subject:
        safe_print(parser.subject)

    if args.defects:
        for i in parser.defects_category:
            safe_print(i)

    if args.anomalies:
        for i in parser.anomalies:
            safe_print(i)

    if args.senderip:
        r = parser.get_server_ipaddress(args.senderip)
        if r:
            safe_print(r)
        else:
            safe_print("Not Found")

    if args.attachments:
        for i in parser.attachments_list:
            safe_print(json.dumps(i, ensure_ascii=False, indent=4))
Exemplo n.º 2
0
def main():
    args = get_args()

    parser = MailParser()

    if args.file:
        parser.parse_from_file(args.file)
    elif args.string:
        parser.parse_from_string(args.string)

    if args.json:
        j = json.loads(parser.parsed_mail_json)
        print(json.dumps(j, ensure_ascii=False, indent=4).encode('utf-8'))

    if args.body:
        print(parser.body.encode('utf-8'))

    if args.headers:
        print(parser.headers.encode('utf-8'))

    if args.to:
        print(parser.to_.encode('utf-8'))

    if args.from_:
        print(parser.from_.encode('utf-8'))

    if args.subject:
        print(parser.subject.encode('utf-8'))

    if args.defects:
        for i in parser.defects_category:
            print(i.encode('utf-8'))

    if args.anomalies:
        for i in parser.anomalies:
            print(i.encode('utf-8'))

    if args.senderip:
        r = parser.get_server_ipaddress(args.senderip)
        if r:
            print(r.encode('utf-8'))
        else:
            print("Not Found")

    if args.attachments:
        for i in parser.attachments_list:
            print(json.dumps(i, ensure_ascii=False, indent=4).encode('utf-8'))
Exemplo n.º 3
0
class TestMailParser(unittest.TestCase):

    def setUp(self):
        # Init
        self.parser = MailParser()

    def test_ipaddress(self):
        self.parser.parse_from_file(mail_test_2)
        trust = "smtp.customers.net"

        ip = "217.76.210.112"
        result = self.parser.get_server_ipaddress(trust)
        self.assertEqual(result, ip)

        trust = ""
        result = self.parser.get_server_ipaddress(trust)
        self.assertEqual(result, None)

        trust = "   "
        result = self.parser.get_server_ipaddress(trust)
        self.assertEqual(result, None)

    def test_fingerprints_body(self):
        self.parser.parse_from_file(mail_test_1)
        md5, sha1, sha256, sha512 = fingerprints(
            self.parser.body.encode("utf-8"))
        self.assertEqual(md5, "1bbdb7dcf511113bbc0c1b214aeac392")
        self.assertEqual(sha1, "ce9e62b50fa4e2168278880b14460b905b24eb4b")
        self.assertEqual(sha256, ("1e9b96e3f1bc74702f9703391e8ba0715b849"
                                  "7127a7ff857013ab33385898574"))
        self.assertEqual(sha512, ("ad858f7b5ec5549e55650fd13df7683e403489"
                                  "77522995851fb6b625ac54744cf3a4bf652784"
                                  "dba971ef99afeec4e6caf2fdd10be72eabb730"
                                  "c312ffbe1c4de3"))

    def test_malformed_mail(self):
        self.parser.parse_from_file(mail_malformed_3)
        defects_category = self.parser.defects_category
        self.assertIn("StartBoundaryNotFoundDefect", defects_category)
        self.assertIn("MultipartInvariantViolationDefect", defects_category)

    def test_type_error(self):
        self.parser.parse_from_file(mail_test_5)
        self.assertEqual(len(self.parser.attachments_list), 5)
        for i in self.parser.attachments_list:
            self.assertIsInstance(i["filename"], six.text_type)

    def test_valid_mail(self):
        with self.assertRaises(InvalidMail):
            self.parser.parse_from_string("fake mail")

    def test_valid_date_mail(self):
        self.parser.parse_from_file(mail_test_1),
        self.assertIn("mail_without_date", self.parser.anomalies)

    def test_parsing_know_values(self):
        self.parser.parse_from_file(mail_test_2)
        trust = "smtp.customers.net"

        self.assertEqual(False, self.parser.has_defects)

        raw = "217.76.210.112"
        result = self.parser.get_server_ipaddress(trust)
        self.assertEqual(raw, result)

        raw = "<*****@*****.**>"
        result = self.parser.message_id
        self.assertEqual(raw, result)

        raw = "mporcile@server_mail.it"
        result = self.parser.to_
        self.assertEqual(raw, result)

        raw = "<*****@*****.**>"
        result = self.parser.from_
        self.assertEqual(raw, result)

        raw = "Bollettino Meteorologico del 29/11/2015"
        result = self.parser.subject
        self.assertEqual(raw, result)

        result = self.parser.has_defects
        self.assertEqual(False, result)

        result = len(self.parser.attachments_list)
        self.assertEqual(3, result)

        raw = "Sun, 29 Nov 2015 09:45:18 +0100"
        raw_utc = datetime.datetime(2015, 11, 29, 8, 45, 18, 0).isoformat()
        result = self.parser.date_mail.isoformat()
        self.assertEqual(raw_utc, result)

    def test_types(self):
        self.parser.parse_from_file(mail_test_2)
        trust = "smtp.customers.net"

        self.assertEqual(False, self.parser.has_defects)

        result = self.parser.parsed_mail_obj
        self.assertIsInstance(result, dict)
        self.assertNotIn("defects", result)
        self.assertNotIn("anomalies", result)
        self.assertIn("has_defects", result)
        self.assertIn("has_anomalies", result)

        result = self.parser.get_server_ipaddress(trust)
        self.assertIsInstance(result, six.text_type)

        result = self.parser.parsed_mail_json
        self.assertIsInstance(result, six.text_type)

        result = self.parser.headers
        self.assertIsInstance(result, six.text_type)

        result = self.parser.body
        self.assertIsInstance(result, six.text_type)

        result = self.parser.date_mail
        self.assertIsInstance(result, datetime.datetime)

        result = self.parser.from_
        self.assertIsInstance(result, six.text_type)

        result = self.parser.to_
        self.assertIsInstance(result, six.text_type)

        result = self.parser.subject
        self.assertIsInstance(result, six.text_type)

        result = self.parser.message_id
        self.assertIsInstance(result, six.text_type)

        result = self.parser.attachments_list
        self.assertIsInstance(result, list)

        result = self.parser.date_mail
        self.assertIsInstance(result, datetime.datetime)

        result = self.parser.defects
        self.assertIsInstance(result, list)

        result = self.parser.anomalies
        self.assertIsInstance(result, list)

    def test_defects_anomalies(self):
        self.parser.parse_from_file(mail_malformed_1)

        self.assertEqual(True, self.parser.has_defects)
        self.assertEqual(1, len(self.parser.defects))
        self.assertEqual(1, len(self.parser.defects_category))
        self.assertIn("defects", self.parser.parsed_mail_obj)
        self.assertIn("StartBoundaryNotFoundDefect",
                      self.parser.defects_category)
        self.assertIsInstance(self.parser.parsed_mail_json, six.text_type)

        result = len(self.parser.attachments_list)
        self.assertEqual(1, result)

        self.parser.parse_from_file(mail_test_1)
        if six.PY2:
            self.assertEqual(False, self.parser.has_defects)
            self.assertNotIn("defects", self.parser.parsed_mail_obj)
        elif six.PY3:
            self.assertEqual(True, self.parser.has_defects)
            self.assertEqual(1, len(self.parser.defects))
            self.assertEqual(1, len(self.parser.defects_category))
            self.assertIn("defects", self.parser.parsed_mail_obj)
            self.assertIn(
                "CloseBoundaryNotFoundDefect", self.parser.defects_category)

        self.assertEqual(True, self.parser.has_anomalies)
        self.assertEqual(2, len(self.parser.anomalies))
        self.assertIn("anomalies", self.parser.parsed_mail_obj)
        self.assertIn("has_anomalies", self.parser.parsed_mail_obj)

    def test_defects_bug(self):
        self.parser.parse_from_file(mail_malformed_2)

        self.assertEqual(True, self.parser.has_defects)
        self.assertEqual(1, len(self.parser.defects))
        self.assertEqual(1, len(self.parser.defects_category))
        self.assertIn("defects", self.parser.parsed_mail_obj)
        self.assertIn("StartBoundaryNotFoundDefect",
                      self.parser.defects_category)
        self.assertIsInstance(self.parser.parsed_mail_json, six.text_type)

        result = len(self.parser.attachments_list)
        self.assertEqual(0, result)

    def test_add_content_type(self):
        self.parser.parse_from_file(mail_test_3)

        self.assertEqual(False, self.parser.has_defects)

        result = self.parser.parsed_mail_obj

        self.assertEqual(len(result["attachments"]), 1)
        self.assertIsInstance(
            result["attachments"][0]["mail_content_type"], six.text_type)
        self.assertIsInstance(
            result["attachments"][0]["payload"], six.text_type)
        self.assertEqual(
            result["attachments"][0]["content_transfer_encoding"],
            "quoted-printable")
Exemplo n.º 4
0
        return rows

    except Error as e:
        #print(e)
        logger.error(e)

    finally:
        cursor.close()
        conn.close()


if __name__ == "__main__":
    try:
        content = sys.stdin.read()
        parser = MailParser()
        parser.parse_from_string(content)
    except:
        logger.error(traceback.format_exc())
        sys.exit()

    data = {}
    sfrom = email.utils.parseaddr(parser.from_)
    ssubject = parser.subject

    if sfrom[0]:
        sfrom = sfrom[0]

        if '=?utf-8?' in sfrom or '=?UTF-8?' in sfrom:
            sfrom = email.Header.decode_header(sfrom)[0][0]
    else:
        sfrom = sfrom[1]