# Path: sslscan.py

# 
# Path: sslscan/exception.py
# class StartTLSError(Exception):
#     def __init__(self, *args):
#         Exception.__init__(self, "There was an error during the STARTTLS command", *args)
# 
# Path: sslscan/module/handler/tcp.py
# class TCP(BaseHandler):
#     """
#     Handle raw TCP-connections.
#     """
# 
#     name = "tcp"
# 
#     config_options = BaseHandler.config_options + [
#         (
#             "delay", {
#                 "default": 0.1,
#                 "help": "Time to wait between connection attempts. This helps to avoid DoS detection. Default: 0.1s = 10 connections/second.",
#                 "type": "float",
#             }
#         )
#     ]
# 
#     def __init__(self, **kwargs):
#         if getattr(self, "port", None) is None:
#             self.port = 443
#         self.time_last_connect = None
#         BaseHandler.__init__(self, **kwargs)
# 
#     def connect(self):
#         time_delay = self.config.get_value("delay")
# 
#         if self.time_last_connect is not None:
#             while True:
#                 tmp = datetime.now() - self.time_last_connect
#                 time_delta = tmp.total_seconds()
#                 time_sleep = time_delay - time_delta
#                 logger.debug(
#                     "Time delta: {0:.2f}s -> Time to sleep: {1:.2f}s".format(
#                         time_delta,
#                         time_sleep
#                     )
#                 )
#                 if time_delta > time_delay:
#                     break
#                 sleep(time_sleep)
# 
#         self.time_last_connect = datetime.now()
# 
#         sock = socket()
#         sock.connect((self.host, self.port))
#         return Connection(
#             handler=self,
#             sock=sock
#         )


import re
from socket import socket
from sslscan import modules
from sslscan.exception import StartTLSError
from sslscan.module.handler.tcp import TCP
    def __init__(self, **kwargs):
        self.port = 143
        TCP.__init__(self, **kwargs)

    def _connect(self, conn):
        server_info = {}
        buf = conn.recv(4096)
        try:
            buf = buf.decode("ASCII")
            buf = buf.strip()
        except:
            pass

        # ToDo: improve parsing
        server_info["banner"] = buf
        if self._server_info is None:
            self._server_info = server_info

    def connect(self):
        # ToDo: raise exception
        conn = TCP.connect(self)
        if not self.config.get_value("starttls"):
            return conn

        self._connect(conn)

        conn.send(b". STARTTLS\r\n")
        buf = conn.recv(4096)
        buf = buf.strip()
        if not buf.startswith(b". OK"):

# Path: sslscan.py

# 
# Path: sslscan/exception.py
# class StartTLSError(Exception):
#     def __init__(self, *args):
#         Exception.__init__(self, "There was an error during the STARTTLS command", *args)
# 
# Path: sslscan/module/handler/tcp.py
# class TCP(BaseHandler):
#     """
#     Handle raw TCP-connections.
#     """
# 
#     name = "tcp"
# 
#     config_options = BaseHandler.config_options + [
#         (
#             "delay", {
#                 "default": 0.1,
#                 "help": "Time to wait between connection attempts. This helps to avoid DoS detection. Default: 0.1s = 10 connections/second.",
#                 "type": "float",
#             }
#         )
#     ]
# 
#     def __init__(self, **kwargs):
#         if getattr(self, "port", None) is None:
#             self.port = 443
#         self.time_last_connect = None
#         BaseHandler.__init__(self, **kwargs)
# 
#     def connect(self):
#         time_delay = self.config.get_value("delay")
# 
#         if self.time_last_connect is not None:
#             while True:
#                 tmp = datetime.now() - self.time_last_connect
#                 time_delta = tmp.total_seconds()
#                 time_sleep = time_delay - time_delta
#                 logger.debug(
#                     "Time delta: {0:.2f}s -> Time to sleep: {1:.2f}s".format(
#                         time_delta,
#                         time_sleep
#                     )
#                 )
#                 if time_delta > time_delay:
#                     break
#                 sleep(time_sleep)
# 
#         self.time_last_connect = datetime.now()
# 
#         sock = socket()
#         sock.connect((self.host, self.port))
#         return Connection(
#             handler=self,
#             sock=sock
#         )


# Path: sslscan/module/handler/imap.py
import re
from socket import socket
from sslscan import modules
from sslscan.exception import StartTLSError
from sslscan.module.handler.tcp import TCP

    def __init__(self, **kwargs):
        self.port = 143
        TCP.__init__(self, **kwargs)

    def _connect(self, conn):
        server_info = {}
        buf = conn.recv(4096)
        try:
            buf = buf.decode("ASCII")
            buf = buf.strip()
        except:
            pass

        # ToDo: improve parsing
        server_info["banner"] = buf
        if self._server_info is None:
            self._server_info = server_info

    def connect(self):
        # ToDo: raise exception
        conn = TCP.connect(self)
        if not self.config.get_value("starttls"):
            return conn

        self._connect(conn)

        conn.send(b". STARTTLS\r\n")
        buf = conn.recv(4096)
        buf = buf.strip()
        if not buf.startswith(b". OK"):

            raise StartTLSError()