<commit_before>import socket
import ssl
import base64
import json
import time

#don't forget \r\n on commands otherwise they won't get submitted to the server (sent but no response will come back)
# these should probably be broken out in to a separate dictionary file but here for now


with open('mail_servers.json') as data_file:
    TEST_SERVERS = json.load(data_file)

POP_STANDARD = 110
POP_IMPLICIT_SSL = 995
POP_USER = b'USER '
POP_PASS = b'<PASSWORD> '

SMTP_STANDARD = 25
SMTP_IMPLICIT_SSL = 465
SMTP_STARTTLS_SSL = 587
SMTP_EHLO = b'EHLO gmail.com \r\n'
SMTP_AUTH = b'AUTH LOGIN \r\n'

IMAP_STANDARD = 143
IMAP_IMPLICIT_SSL = 993
IMAP_RAND_STRING = b'A1 '
IMAP_CAPABILITY = b'CAPABILITY \r\n'
IMAP_LOGIN = b'LOGIN '

STARTTLS_COMMAND = b'STARTTLS \r\n'

EMAIL_USERNAME = b'<EMAIL>'
EMAIL_PASSWORD = b'<PASSWORD>'



def insecure_connection(HOST,PORT,PROTOCOL):
    print('Connecting to host: {}  on Port Number {} using Plaintext \r\n'.format(HOST,PORT))
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
        client.connect((HOST, PORT))
        data = client.recv(1024)
        #print('Received', repr(data))

        client.send(SMTP_EHLO)
        data = client.recv(1024)
        #print('Received', repr(data))

        decide_protocol_handler(client,PROTOCOL)


# STARTTLS REFERENCE: https://www.fastmail.com/help/technical/ssltlsstarttls.html
# STARTTLS PYTHON WITH SOCKETS CODE: https://stackoverflow.com/questions/12593944/how-to-start-tls-on-an-active-connection-in-python
# THE code above is Python2 ssl so the code below uses SSL contexts compatible with Python3 Sockets and SSL

def starttls_connection(HOST,PORT,PROTOCOL):
    print('Connecting to host: {}  on Port Number {} using STARTTLS \r\n'.format(HOST,PORT))
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:

        client.connect((HOST, PORT))
        data = client.recv(1024)
        #print('Received', repr(data))

        # SMTP NEEDS A EHLO MESSAGE BEFORE STARTTLS COMMAND
        # IMAP AND POP DO NOT

        if PROTOCOL=="SMTP":
            client.send(SMTP_EHLO)
            data = client.recv(1024)
            #print('Received', repr(data))

        client.send(STARTTLS_COMMAND)
        data = client.recv(1024)
        #print('Received', repr(data))

        context = ssl.create_default_context()
        secure_client = context.wrap_socket(client,server_hostname=HOST)
        secure_client.send(SMTP_EHLO)
        data = secure_client.recv(1024)
        print('Received', repr(data),'\r\n')

        cert = secure_client.getpeercert()
        print("Certificate is Issued By: {} \r\n".format(cert["issuer"][2]))
        print("Certificate covers the following Domains: {}\r\n".format(cert["subjectAltName"]))

        #pass the secure client to the write protocol conversation handler

        decide_protocol_handler(secure_client,PROTOCOL)

# Standard SSL Python 3 Code (straight from documents)


def secure_connection(HOST,PORT,PROTOCOL):

    context = ssl.create_default_context()
    secure_client = context.wrap_socket(socket.socket(socket.AF_INET),server_hostname=HOST)
    secure_client.connect((HOST,PORT))
    data = secure_client.recv(1024)

    cert = secure_client.getpeercert()
    print("Certificate is Issued By: {} \r\n".format(cert["issuer"][2]))<commit_msg>FIXING PROTOCOL SPECIFIC ERRORS<commit_after>import socket
import ssl
import base64
import json
import time

#don't forget \r\n on commands otherwise they won't get submitted to the server (sent but no response will come back)
# these should probably be broken out in to a separate dictionary file but here for now


with open('mail_servers.json') as data_file:
    TEST_SERVERS = json.load(data_file)

POP_STANDARD = 110
POP_IMPLICIT_SSL = 995
POP_USER = b'USER '
POP_PASS = b'<PASSWORD> '

SMTP_STANDARD = 25
SMTP_IMPLICIT_SSL = 465
SMTP_STARTTLS_SSL = 587
SMTP_EHLO = b'EHLO gmail.com \r\n'
SMTP_AUTH = b'AUTH LOGIN \r\n'

IMAP_STANDARD = 143
IMAP_IMPLICIT_SSL = 993
IMAP_RAND_STRING = b'A1 '
IMAP_CAPABILITY = b'CAPABILITY \r\n'
IMAP_LOGIN = b'LOGIN '

STARTTLS_COMMAND = b'STARTTLS \r\n'

EMAIL_USERNAME = b'<EMAIL>'
EMAIL_PASSWORD = b'<PASSWORD>'



def insecure_connection(HOST,PORT,PROTOCOL):
    print('Connecting to host: {}  on Port Number {} using Plaintext \r\n'.format(HOST,PORT))
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
        client.connect((HOST, PORT))
        data = client.recv(1024)
        #print('Received', repr(data))

        client.send(SMTP_EHLO)
        data = client.recv(1024)
        #print('Received', repr(data))

        decide_protocol_handler(client,PROTOCOL)


# STARTTLS REFERENCE: https://www.fastmail.com/help/technical/ssltlsstarttls.html
# STARTTLS PYTHON WITH SOCKETS CODE: https://stackoverflow.com/questions/12593944/how-to-start-tls-on-an-active-connection-in-python
# THE code above is Python2 ssl so the code below uses SSL contexts compatible with Python3 Sockets and SSL

def starttls_connection(HOST,PORT,PROTOCOL):
    print('Connecting to host: {}  on Port Number {} using STARTTLS \r\n'.format(HOST,PORT))
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:

        client.connect((HOST, PORT))
        data = client.recv(1024)
        #print('Received', repr(data))

        # SMTP NEEDS A EHLO MESSAGE BEFORE STARTTLS AND OTHER COMMANDS
        # IMAP NEEDS A RANDOM STRING FOR THE STARTTLS COMMAND
        # POP3 - investigate


        if PROTOCOL=="SMTP":
            client.send(SMTP_EHLO)
            data = client.recv(1024)
            #print('Received', repr(data))
            client.send(STARTTLS_COMMAND)
            data = client.recv(1024)
            #print('Received', repr(data))

        if PROTOCOL=="IMAP":
            client.send(IMAP_RAND_STRING + STARTTLS_COMMAND)
            data = client.recv(1024)
            #print('Received', repr(data))



        context = ssl.create_default_context()
        secure_client = context.wrap_socket(client,server_hostname=HOST)
        secure_client.send(SMTP_EHLO)
        data = secure_client.recv(1024)
        print('Received', repr(data),'\r\n')

        cert = secure_client.getpeercert()
        print("Certificate is Issued By: {} \r\n".format(cert["issuer"][2]))
        print("Certificate covers the following Domains: {}\r\n".format(cert["subjectAltName"]))

        #pass the secure client to the write protocol conversation handler

        decide_protocol_handler(secure_client,PROTOCOL)

# Standard SSL Python 3 Code (straight from documents)


def secure_connection(HOST,PORT,PROTOCOL):

    context = ssl.create_default_context()
    secure_client = context.wrap_socket(socket.socket(socket.AF_INET),server_hostname=HOST)
    secure_client.connect((HOST,PORT))
    data = secure_client.recv(1024)

    # SMTP NEEDS A EHLO MESSAGE BEFORE OTHER COMMANDS
    # IMAP AND POP DO NOT

    if PROTOCOL=="SMTP":
        secure_client.send(SMTP_EHLO)
        data = secure_client.recv(1024)
        #print('SMTP EHLO RESPONSE: ', repr(data))


    cert = secure_client.getpeercert()
    print("Certificate is Issued By: {} \r\n".format(cert["issuer"][2]))
{'fraction_non_alphanumeric': 0.11073825503355705, 'fraction_numerical': 0.019155480984340046, 'mean_word_length': 3.5881975625400897, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3456608', 'n_tokens_mistral': 2508, 'n_tokens_neox': 2358, 'n_words': 544}