import FakeNet

#Sample python code for imitating an SMTP server
def FN_Init():
	pass

def FN_NewConnection(context):
	incomingPacket = ""
	FakeNet.sendData(context, "220 PracticalMalwareAnalysis.COM STMP Service Ready\r\n")
	while True:
		incomingPacket = FakeNet.recvData(context, 4096)
		if (incomingPacket == ""):
			break
		incomingPacket = incomingPacket[:4].upper()
		if (incomingPacket == "HELO"):
			FakeNet.sendData(context, "250 PracticalMalwareAnalysis.com\r\n")
			continue
		elif (incomingPacket == "MAIL" or incomingPacket == "RCPT" or incomingPacket == "NOOP" or incomingPacket == "RSET"):
			FakeNet.sendData(context, "250 OK\r\n")
		elif (incomingPacket == "QUIT"):
			FakeNet.sendData(context, "221 PracticalMalwareAnalysis.com bye\r\n")
			break
		elif (incomingPacket == "DATA"):
			FakeNet.sendData(context, "354 start mail input, end with <CRLF>.<CRLF>\r\n")
			dataBuffer = ""
			while True:
				incomingPacket = FakeNet.recvData(context, 4096)
				if (incomingPacket == ""):
					break
				dataBuffer = dataBuffer + incomingPacket
				ending = dataBuffer[-5:]
				if (dataBuffer[-5:] == '\r\n.\r\n'):
					break
			FakeNet.sendData(context, "250 OK\r\n");
		else:
			FakeNet.sendData(context, "503 Command not supported\r\n")

