# Path: lib/objects.py
# class AccountBreachAlertTemplate:
#     def __init__(self, alert):
#         self.alert = alert
#         self.subject = "A " + alert[6] + " alert was reported for account " + alert[7]
#         self.indicators = alert[8].split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert:\r\n\r\n" \
#                     "Account at risk: " + self.alert[7] + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "This is the first time this incident is reported.\r\n" \
#                     "We recommend to change immediately the password for this account to prevent further misuse by" \
#                     " malicious hackers."
# 
# class HostAlertTemplate:
#     def __init__(self, homenet, alert):
#         self.homenet = homenet
#         self.alert = alert
#         self.subject = "A " + alert[6] + " alert was reported for host " + alert[7]
#         self.indicators = alert[8].replace('.', '[.]').split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert for the device below:\r\n\r\n" \
#                     "IP address: " + self.alert[7] + "\r\n" \
#                     "Hostname: " + str(self.homenet.hosts[self.alert[7]].hostname) + "\r\n" \
#                     "MAC address: " + str(self.homenet.hosts[self.alert[7]].mac) + "\r\n" \
#                     "MAC vendor: " + str(self.homenet.hosts[self.alert[7]].vendor) + "\r\n" \
#                     "Operating system family: " + "\r\n".join(self.homenet.hosts[self.alert[7]].os_family) + "\r\n" \
#                     "Device family: " + str("\r\n".join(self.homenet.hosts[self.alert[7]].device_family)) + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "This is the first time this incident is reported.\r\n" \
#                     "We recommend to investigate this issue as soon as possible."
# 
# class DefaultCredsAlertTemplate:
#     def __init__(self, homenet, alert):
#         self.homenet = homenet
#         self.alert = alert
#         self.subject = "An account with default vendor credentials was found on host " + alert[7]
#         self.indicators = alert[8].replace('.', '[.]').split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert for the device below:\r\n\r\n" \
#                     "IP address: " + self.alert[7] + "\r\n" \
#                     "Hostname: " + str(self.homenet.hosts[self.alert[7]].hostname) + "\r\n" \
#                     "MAC address: " + str(self.homenet.hosts[self.alert[7]].mac) + "\r\n" \
#                     "MAC vendor: " + str(self.homenet.hosts[self.alert[7]].vendor) + "\r\n" \
#                     "Operating system family: " + "\r\n".join(self.homenet.hosts[self.alert[7]].os_family) + "\r\n" \
#                     "Device family: " + str("\r\n".join(self.homenet.hosts[self.alert[7]].device_family)) + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "We recommend you to fix this issue as soon as possible."
# 
# Path: lib/settings.py
# def get_top_domains(dbname):


import threading
import smtplib
import time
import requests
import json
import uuid
import base64
from lib.logger import *
from lib.config import *
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from lib.objects import AccountBreachAlertTemplate, HostAlertTemplate, DefaultCredsAlertTemplate
from lib.settings import homenet, lock


class AlertReporter(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ctime = int(time.time())

    def run(self):

        while 1:
            self.ctime = int(time.time())
            if homenet.dst_emails:
                self.report_new_alerts()
            else:
                pass
            time.sleep(15)

    def report_new_alerts(self):
        alerts = utils.get_not_reported_alerts()
        with lock:
            for alert in alerts:
                email = {}

                if alert[1] == 'data_breach':
                    t = AccountBreachAlertTemplate(alert)
                    t.create_body()
                elif alert[1] == 'default_creds':

# Path: lib/objects.py
# class AccountBreachAlertTemplate:
#     def __init__(self, alert):
#         self.alert = alert
#         self.subject = "A " + alert[6] + " alert was reported for account " + alert[7]
#         self.indicators = alert[8].split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert:\r\n\r\n" \
#                     "Account at risk: " + self.alert[7] + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "This is the first time this incident is reported.\r\n" \
#                     "We recommend to change immediately the password for this account to prevent further misuse by" \
#                     " malicious hackers."
# 
# class HostAlertTemplate:
#     def __init__(self, homenet, alert):
#         self.homenet = homenet
#         self.alert = alert
#         self.subject = "A " + alert[6] + " alert was reported for host " + alert[7]
#         self.indicators = alert[8].replace('.', '[.]').split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert for the device below:\r\n\r\n" \
#                     "IP address: " + self.alert[7] + "\r\n" \
#                     "Hostname: " + str(self.homenet.hosts[self.alert[7]].hostname) + "\r\n" \
#                     "MAC address: " + str(self.homenet.hosts[self.alert[7]].mac) + "\r\n" \
#                     "MAC vendor: " + str(self.homenet.hosts[self.alert[7]].vendor) + "\r\n" \
#                     "Operating system family: " + "\r\n".join(self.homenet.hosts[self.alert[7]].os_family) + "\r\n" \
#                     "Device family: " + str("\r\n".join(self.homenet.hosts[self.alert[7]].device_family)) + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "This is the first time this incident is reported.\r\n" \
#                     "We recommend to investigate this issue as soon as possible."
# 
# class DefaultCredsAlertTemplate:
#     def __init__(self, homenet, alert):
#         self.homenet = homenet
#         self.alert = alert
#         self.subject = "An account with default vendor credentials was found on host " + alert[7]
#         self.indicators = alert[8].replace('.', '[.]').split('|')
#         self.references = alert[11].split('|')
#         self.body = ''
# 
#     def create_body(self):
#         self.body = "Falcongate has reported a " + self.alert[6] + " alert for the device below:\r\n\r\n" \
#                     "IP address: " + self.alert[7] + "\r\n" \
#                     "Hostname: " + str(self.homenet.hosts[self.alert[7]].hostname) + "\r\n" \
#                     "MAC address: " + str(self.homenet.hosts[self.alert[7]].mac) + "\r\n" \
#                     "MAC vendor: " + str(self.homenet.hosts[self.alert[7]].vendor) + "\r\n" \
#                     "Operating system family: " + "\r\n".join(self.homenet.hosts[self.alert[7]].os_family) + "\r\n" \
#                     "Device family: " + str("\r\n".join(self.homenet.hosts[self.alert[7]].device_family)) + "\r\n\r\n" \
#                     "Description: " + self.alert[10] + "\r\n\r\n" \
#                     "The following indicators were detected:\r\n" + str("\r\n".join(self.indicators)) + "\r\n\r\n" \
#                     "References:\r\n" + str("\r\n".join(self.references)) + "\r\n\r\n" \
#                     "We recommend you to fix this issue as soon as possible."
# 
# Path: lib/settings.py
# def get_top_domains(dbname):


# Path: lib/reporter.py
import threading
import smtplib
import time
import requests
import json
import uuid
import base64
from lib.logger import *
from lib.config import *
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from lib.objects import AccountBreachAlertTemplate, HostAlertTemplate, DefaultCredsAlertTemplate
from lib.settings import homenet, lock



class AlertReporter(threading.Thread):
    def __init__(self, threadID):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ctime = int(time.time())

    def run(self):

        while 1:
            self.ctime = int(time.time())
            if homenet.dst_emails:
                self.report_new_alerts()
            else:
                pass
            time.sleep(15)

    def report_new_alerts(self):
        alerts = utils.get_not_reported_alerts()
        with lock:
            for alert in alerts:
                email = {}

                if alert[1] == 'data_breach':
                    t = AccountBreachAlertTemplate(alert)
                    t.create_body()
                elif alert[1] == 'default_creds':

                    t = DefaultCredsAlertTemplate(homenet, alert)