Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions mailoney/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import threading
import logging
import json
import re
import sys
import argparse
from time import strftime
Expand Down Expand Up @@ -105,12 +106,25 @@ def _handle_client(self, client_socket: socket.socket, addr: Tuple[str, int]) ->

# Handle client commands
error_count = 0
receiving_mail = False

while error_count < 10:
try:
request = client_socket.recv(4096).decode().strip().lower()
raw_request = client_socket.recv(4096).decode()
request = raw_request.strip().lower()
if not request:
break

if receiving_mail:
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "in-body", "data": request})
if re.search(r'(?:\r?\n|^)\.(?:\r?\n|$)', raw_request):
response = "451 4.7.1 Try again later\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
receiving_mail = False

continue

session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "in", "data": request})
logger.debug(f"Client: {request}")

Expand Down Expand Up @@ -141,12 +155,20 @@ def _handle_client(self, client_socket: socket.socket, addr: Tuple[str, int]) ->
break

# Handle other SMTP commands (simplistic simulation)
elif request.startswith(('mail from:', 'rcpt to:', 'data')):
elif request.startswith(('mail from:', 'rcpt to:')):
response = "250 2.1.0 OK\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
error_count = 0


# Receive an e-mail
elif request.startswith('data'):
response = "354 End data with <CR><LF>.<CR><LF>\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
receiving_mail = True
error_count = 0

# Unknown command
else:
response = "502 5.5.2 Error: command not recognized\n"
Expand Down
28 changes: 25 additions & 3 deletions src/mailoney/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import threading
import logging
import json
import re
from time import strftime
from typing import Optional, Tuple, Dict, Any

Expand Down Expand Up @@ -102,12 +103,25 @@ def _handle_client(self, client_socket: socket.socket, addr: Tuple[str, int]) ->

# Handle client commands
error_count = 0
receiving_mail = False

while error_count < 10:
try:
request = client_socket.recv(4096).decode().strip().lower()
raw_request = client_socket.recv(4096).decode()
request = raw_request.strip().lower()
if not request:
break

if receiving_mail:
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "in-body", "data": request})
if re.search(r'(?:\r?\n|^)\.(?:\r?\n|$)', raw_request):
response = "451 4.7.1 Try again later\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
receiving_mail = False

continue

session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "in", "data": request})
logger.debug(f"Client: {request}")

Expand Down Expand Up @@ -138,12 +152,20 @@ def _handle_client(self, client_socket: socket.socket, addr: Tuple[str, int]) ->
break

# Handle other SMTP commands (simplistic simulation)
elif request.startswith(('mail from:', 'rcpt to:', 'data')):
elif request.startswith(('mail from:', 'rcpt to:')):
response = "250 2.1.0 OK\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
error_count = 0


# Receive an e-mail
elif request.startswith('data'):
response = "354 End data with <CR><LF>.<CR><LF>\n"
client_socket.send(response.encode())
session_log.append({"timestamp": strftime("%Y-%m-%d %H:%M:%S"), "direction": "out", "data": response})
receiving_mail = True
error_count = 0

# Unknown command
else:
response = "502 5.5.2 Error: command not recognized\n"
Expand Down