# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/pcap/models.py
# class Pcap(models.Model):
#     hash_value = models.CharField(max_length=100)
#     file_name = models.FileField(upload_to="uploads", null=True, blank=True)
#     path = models.FilePathField()
#     packets = ListField(EmbeddedModelField('PacketDetails', null=True, blank=True))
# 
#     def __unicode__(self):
#         return u'%s/%s' % (self.path, self.file_name)
# 
#     def get_upload_path(self):
#         hash_dir = os.path.basename(self.path)
#         root = os.path.basename(os.path.dirname(self.path))
#         return os.path.join(root, hash_dir)
# 
# class SMTPDetails(models.Model):
#     login_data = ListField(null=True, blank=True)
#     msg_from = models.CharField(max_length=100, null=True, blank=True)
#     rcpt_to = models.CharField(max_length=100, null=True, blank=True)
#     raw = models.TextField(null=True, blank=True)
#     msgdata = models.TextField(null=True, blank=True)
#     attachment_path = ListField(null=True, blank=True)
#     flow_details = EmbeddedModelField('FlowDetails', null=True, blank=True)
# 
#     objects = MongoDBManager()
# 
#     def get_path_dict(self):
#         #/home/oguz/git/ovizart/ovizart/uploads/16-06-12/a6a6defb7253043a55281d01aa66538a/smtp-messages/1/part-001.ksh
#         result = []
#         for path in self.attachment_path:
#             tmp = dict()
#             r = path.split("uploads")
#             file_name = os.path.basename(r[1])
#             tmp['file_name'] = file_name
#             tmp['path'] = r[1]
#             result.append(tmp)
# 
#         return result
# 
# class FlowDetails(models.Model):
#     parent_hash_value = models.CharField(max_length=50)
#     user_id = models.CharField(max_length=100)
#     src_ip = models.IPAddressField()
#     dst_ip = models.IPAddressField()
#     sport = models.IntegerField()
#     dport = models.IntegerField()
#     protocol = models.CharField(max_length=10)
#     timestamp = models.DateTimeField()
# 
#     objects = MongoDBManager()


import base64
import hashlib
import os.path, os
import subprocess
import email, mimetypes
import zipfile
from ovizart.modules.traffic.log.logger import Logger
from ovizart.pcap.models import Pcap, SMTPDetails, FlowDetails
            self.report_SMTP(f, smtp_details)

    def process_SMTP(self, aFile):
        a = False
        b = False
        for i in self.toProcess[aFile]['raw']:

            if a and i.startswith("MAIL FROM"):
                a = False
            if b and i == ".":
                b = False

            if a:
                self.toProcess[aFile]['logindata'].append(i)
            if b:
                self.toProcess[aFile]['msgdata'].append(i)

            if i == "AUTH LOGIN":
                a = True
                self.toProcess[aFile]['logindata'] = []
            if i == "DATA":
                b = True
                self.toProcess[aFile]['msgdata'] = []
            if i.startswith("MAIL FROM:"):
                self.toProcess[aFile]['msg_from'] = i[11:]
            if i.startswith("RCPT TO:"):
                self.toProcess[aFile]['rcpt_to'] = i[9:]
        ip = aFile
        src_ip, sport, dst_ip, dport = self.parse_aFile(ip)
        flow_details = FlowDetails.objects.get(src_ip=src_ip, sport=sport, dst_ip=dst_ip, dport=dport)

# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/pcap/models.py
# class Pcap(models.Model):
#     hash_value = models.CharField(max_length=100)
#     file_name = models.FileField(upload_to="uploads", null=True, blank=True)
#     path = models.FilePathField()
#     packets = ListField(EmbeddedModelField('PacketDetails', null=True, blank=True))
# 
#     def __unicode__(self):
#         return u'%s/%s' % (self.path, self.file_name)
# 
#     def get_upload_path(self):
#         hash_dir = os.path.basename(self.path)
#         root = os.path.basename(os.path.dirname(self.path))
#         return os.path.join(root, hash_dir)
# 
# class SMTPDetails(models.Model):
#     login_data = ListField(null=True, blank=True)
#     msg_from = models.CharField(max_length=100, null=True, blank=True)
#     rcpt_to = models.CharField(max_length=100, null=True, blank=True)
#     raw = models.TextField(null=True, blank=True)
#     msgdata = models.TextField(null=True, blank=True)
#     attachment_path = ListField(null=True, blank=True)
#     flow_details = EmbeddedModelField('FlowDetails', null=True, blank=True)
# 
#     objects = MongoDBManager()
# 
#     def get_path_dict(self):
#         #/home/oguz/git/ovizart/ovizart/uploads/16-06-12/a6a6defb7253043a55281d01aa66538a/smtp-messages/1/part-001.ksh
#         result = []
#         for path in self.attachment_path:
#             tmp = dict()
#             r = path.split("uploads")
#             file_name = os.path.basename(r[1])
#             tmp['file_name'] = file_name
#             tmp['path'] = r[1]
#             result.append(tmp)
# 
#         return result
# 
# class FlowDetails(models.Model):
#     parent_hash_value = models.CharField(max_length=50)
#     user_id = models.CharField(max_length=100)
#     src_ip = models.IPAddressField()
#     dst_ip = models.IPAddressField()
#     sport = models.IntegerField()
#     dport = models.IntegerField()
#     protocol = models.CharField(max_length=10)
#     timestamp = models.DateTimeField()
# 
#     objects = MongoDBManager()


# Path: ovizart/modules/traffic/parser/tcp/smtp/handler.py
import base64
import hashlib
import os.path, os
import subprocess
import email, mimetypes
import zipfile
from ovizart.modules.traffic.log.logger import Logger
from ovizart.pcap.models import Pcap, SMTPDetails, FlowDetails

            self.report_SMTP(f, smtp_details)

    def process_SMTP(self, aFile):
        a = False
        b = False
        for i in self.toProcess[aFile]['raw']:

            if a and i.startswith("MAIL FROM"):
                a = False
            if b and i == ".":
                b = False

            if a:
                self.toProcess[aFile]['logindata'].append(i)
            if b:
                self.toProcess[aFile]['msgdata'].append(i)

            if i == "AUTH LOGIN":
                a = True
                self.toProcess[aFile]['logindata'] = []
            if i == "DATA":
                b = True
                self.toProcess[aFile]['msgdata'] = []
            if i.startswith("MAIL FROM:"):
                self.toProcess[aFile]['msg_from'] = i[11:]
            if i.startswith("RCPT TO:"):
                self.toProcess[aFile]['rcpt_to'] = i[9:]
        ip = aFile
        src_ip, sport, dst_ip, dport = self.parse_aFile(ip)
        flow_details = FlowDetails.objects.get(src_ip=src_ip, sport=sport, dst_ip=dst_ip, dport=dport)

        smtp_details = SMTPDetails.objects.create(login_data=self.toProcess[aFile]['logindata'],