# 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
                ip = line.split(":")[1].strip()
                # test whether this is an smtp flow
                smtp_flow_file_path = "/".join([path, ip])
                if not self.decode_SMTP(smtp_flow_file_path):
                    continue
                self.file_name_li.append(ip)
                src_ip, sport, dst_ip, dport = self.parse_aFile(ip)

                packet = None
                found = False
                for pcap in self.flow.pcaps:
                    # this line is required, otherwise pcap.pcakets is not returning the info
                    pcap = Pcap.objects.get(id = pcap.id)
                    for packet in pcap.packets:
                        if packet.src_ip == src_ip and packet.sport == sport and packet.dst_ip == dst_ip and packet.dport == dport:
                            packet = packet
                            found = True
                            break
                    break

                if found:
                    timestamp = packet.timestamp
                    result.append([src_ip, sport, dst_ip, dport, timestamp])
                    result.append([dst_ip, dport, src_ip, sport, timestamp])

        return result

    def parse_aFile(self, ip):
        ip_info = ip.split("-")
        src = ip_info[0].split(".")

# 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

                ip = line.split(":")[1].strip()
                # test whether this is an smtp flow
                smtp_flow_file_path = "/".join([path, ip])
                if not self.decode_SMTP(smtp_flow_file_path):
                    continue
                self.file_name_li.append(ip)
                src_ip, sport, dst_ip, dport = self.parse_aFile(ip)

                packet = None
                found = False
                for pcap in self.flow.pcaps:
                    # this line is required, otherwise pcap.pcakets is not returning the info
                    pcap = Pcap.objects.get(id = pcap.id)
                    for packet in pcap.packets:
                        if packet.src_ip == src_ip and packet.sport == sport and packet.dst_ip == dst_ip and packet.dport == dport:
                            packet = packet
                            found = True
                            break
                    break

                if found:
                    timestamp = packet.timestamp
                    result.append([src_ip, sport, dst_ip, dport, timestamp])
                    result.append([dst_ip, dport, src_ip, sport, timestamp])

        return result

    def parse_aFile(self, ip):
        ip_info = ip.split("-")
        src = ip_info[0].split(".")

        dst = ip_info[1].split(".")