smtp_node/SMTPClient.gd

244 lines
5.6 KiB
GDScript3
Raw Normal View History

2020-07-25 19:56:17 +02:00
extends Node
2023-09-18 12:27:06 +02:00
export(String) var server : String = "smtp.gmail.com"
export(int) var port : int = 465
export(String) var user : String = ""
export(String) var password : String = ""
2023-09-18 12:37:45 +02:00
export(String) var email_address : String = "mail.smtp.localhost"
export(String) var client_address : String = "client.example.com"
2023-09-18 12:27:06 +02:00
export(int) var max_retries : int = 5
export(int) var delay_time : int = 250
2023-09-18 12:24:40 +02:00
2023-09-18 12:27:06 +02:00
var _socket_original : StreamPeer = null
var _socket : StreamPeer = null
var _packet_in : String = ""
var _packet_out : String = ""
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
enum SMTPStatus {
OK,
WAITING,
NO_RESPONSE,
UNHANDLED_REPONSE
}
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
var _current_status : int = 0
2020-07-25 19:56:17 +02:00
2023-09-18 12:37:45 +02:00
var _thread : Thread = null
2020-07-25 20:30:42 +02:00
2023-09-18 12:37:45 +02:00
var _auth_login_base64 : String = ""
var _auth_pass_base64 : String = ""
2023-09-18 12:14:24 +02:00
2020-07-25 19:56:17 +02:00
func _ready():
2023-09-18 12:14:24 +02:00
if user != "":
2023-09-18 12:37:45 +02:00
_auth_login_base64 = Marshalls.raw_to_base64(user.to_ascii())
2023-09-18 12:24:40 +02:00
2023-09-18 12:14:24 +02:00
if password != "":
2023-09-18 12:37:45 +02:00
_auth_pass_base64 = Marshalls.raw_to_base64(password.to_ascii())
2020-07-25 19:56:17 +02:00
2023-09-18 12:37:45 +02:00
func send_mail(address, subject, data):
_thread = Thread.new()
_thread.start(self, "_thread_deliver", [address, subject, data])
func _thread_deliver(user_data):
var address : String = user_data[0]
var subject : String = user_data[1]
var data : String = user_data[2]
2023-09-18 12:27:06 +02:00
var r_code : int
2023-09-18 12:37:45 +02:00
r_code = open_socket()
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:24:40 +02:00
r_code = wait_answer()
2023-09-18 12:37:45 +02:00
# if r_code == OK:
# emit_signal("SMTP_connected")
# r_code = send("ciao") # needed because some SMTP servers return error each first command
2023-09-18 12:37:45 +02:00
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:24:40 +02:00
r_code = mail_hello()
2020-07-25 19:56:17 +02:00
if r_code == OK:
print("SMTP_working")
2023-09-18 12:24:40 +02:00
close_socket()
2023-09-18 12:14:24 +02:00
return
2023-09-18 12:24:40 +02:00
r_code = mail_auth()
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:37:45 +02:00
r_code = mail_from(address)
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:37:45 +02:00
r_code = mail_to(address)
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:37:45 +02:00
r_code = mail_data(data, address, subject)
2020-07-25 19:56:17 +02:00
if r_code == OK:
print("process OK")
if r_code == OK:
2023-09-18 12:24:40 +02:00
r_code = mail_quit()
close_socket()
2020-07-25 19:56:17 +02:00
if r_code == OK:
2020-07-25 20:56:15 +02:00
display("All done")
2020-07-25 19:56:17 +02:00
else:
2023-09-18 12:14:24 +02:00
display("ERROR " + str(r_code))
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:37:45 +02:00
func open_socket():
2023-09-18 12:24:40 +02:00
var error : int
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
if _socket_original == null:
_socket_original = StreamPeerTCP.new()
error = _socket_original.connect_to_host(server,port)
_socket = StreamPeerSSL.new()
_socket.connect_to_stream(_socket_original, true, server)
2020-07-25 19:56:17 +02:00
display(["connecting server...",server,error])
if error > 0:
2023-09-18 12:37:45 +02:00
var ip = IP.resolve_hostname(server)
2023-09-18 12:24:40 +02:00
error=_socket.connect_to_host(ip,port)
2020-07-25 19:56:17 +02:00
display(["trying IP ...",ip,error])
2023-09-18 12:24:40 +02:00
for i in range(1,max_retries):
print("RETRIES" + str(_socket.get_status()))
2023-09-18 12:24:40 +02:00
# if _socket.get_status() == _socket.STATUS_ERROR:
# d.display("Error while requesting connection")
# break
2023-09-18 12:24:40 +02:00
# elif _socket.get_status() == _socket.STATUS_CONNECTING:
# d.display("connecting...")
# break
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
if _socket.get_status() == _socket.STATUS_CONNECTED:
2020-07-25 19:56:17 +02:00
display("connection up")
2023-09-18 12:14:24 +02:00
print("CONNECTED")
2020-07-25 19:56:17 +02:00
break
2023-09-18 12:24:40 +02:00
OS.delay_msec(delay_time)
2020-07-25 19:56:17 +02:00
return error
2023-09-18 12:24:40 +02:00
func close_socket():
_socket_original.disconnect_from_host()
2020-07-25 19:56:17 +02:00
func send(data1,data2=null,data3=null):
2023-09-18 12:27:06 +02:00
return send_only(data1,data2,data3)
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
func send_only(data1,data2=null,data3=null):
2020-07-25 19:56:17 +02:00
var error = 0
2023-09-18 12:24:40 +02:00
_packet_out = data1
2020-07-25 19:56:17 +02:00
if data2 != null:
2023-09-18 12:24:40 +02:00
_packet_out = _packet_out + " " + data2
2020-07-25 19:56:17 +02:00
if data3 != null:
2023-09-18 12:24:40 +02:00
_packet_out = _packet_out + " " + data3
display(["send",_packet_out])
_packet_out = _packet_out + "\n"
error=_socket.put_data(_packet_out.to_utf8())
if error == null:
error = "NULL"
2020-07-25 19:56:17 +02:00
display(["send","r_code",error])
2023-09-18 12:24:40 +02:00
2020-07-25 19:56:17 +02:00
return error
2023-09-18 12:24:40 +02:00
func wait_answer(succesful=""):
_current_status= SMTPStatus.WAITING
2020-07-25 19:56:17 +02:00
display(["waiting response from server..."])
2023-09-18 12:24:40 +02:00
_packet_in = ""
OS.delay_msec(delay_time)
for i in range(1,max_retries):
_socket.poll()
var bufLen = _socket.get_available_bytes()
if bufLen > 0:
display(["bytes buffered",String(bufLen)])
_packet_in=_packet_in + _socket.get_utf8_string(bufLen)
display(["receive",_packet_in])
break
2020-07-25 19:56:17 +02:00
else:
2023-09-18 12:24:40 +02:00
OS.delay_msec(delay_time)
if _packet_in != "":
_current_status= SMTPStatus.OK
if parse_packet_in(succesful) != OK:
_current_status=SMTPStatus.UNHANDLED_REPONSE
2020-07-25 19:56:17 +02:00
else:
2023-09-18 12:24:40 +02:00
_current_status = SMTPStatus.NO_RESPONSE
return _current_status
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
func parse_packet_in(strcompare):
2020-07-25 19:56:17 +02:00
if strcompare == "":
return OK
2023-09-18 12:24:40 +02:00
if _packet_in.left(strcompare.length())==strcompare:
2020-07-25 19:56:17 +02:00
return OK
else:
return FAILED
2023-09-18 12:24:40 +02:00
func mail_hello():
2023-09-18 12:37:45 +02:00
var r_code : int = send("HELO", client_address)
2023-09-18 12:24:40 +02:00
wait_answer()
2023-09-18 12:37:45 +02:00
r_code= send("EHLO", client_address)
2023-09-18 12:24:40 +02:00
r_code= wait_answer("250")
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:24:40 +02:00
func mail_auth():
2023-09-18 12:27:06 +02:00
var r_code : int =send("AUTH LOGIN")
2023-09-18 12:24:40 +02:00
r_code=wait_answer("334")
2020-07-25 19:56:17 +02:00
2023-09-18 12:24:40 +02:00
#print("mail_auth() , AUTH LOGIN ", r_code)
2020-07-25 20:29:05 +02:00
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:37:45 +02:00
r_code=send(_auth_login_base64)
2023-09-18 12:24:40 +02:00
r_code = wait_answer("334")
2023-09-18 12:37:45 +02:00
2023-09-18 12:24:40 +02:00
#print("mail_auth() , username ", r_code)
2023-09-18 12:37:45 +02:00
2020-07-25 19:56:17 +02:00
if r_code == OK:
2023-09-18 12:37:45 +02:00
r_code=send(_auth_pass_base64)
2023-09-18 12:24:40 +02:00
r_code = wait_answer("235")
#print("mail_auth() , password ", r_code)
2020-07-25 19:56:17 +02:00
display(["r_code auth:", r_code])
2023-09-18 12:37:45 +02:00
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:24:40 +02:00
func mail_from(data):
2020-07-25 19:56:17 +02:00
var r_code=send("MAIL FROM:",bracket(data))
2023-09-18 12:24:40 +02:00
r_code = wait_answer("250")
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:24:40 +02:00
func mail_to(data):
2020-07-25 19:56:17 +02:00
var r_code=send("RCPT TO:",bracket(data))
2023-09-18 12:24:40 +02:00
r_code = wait_answer("250")
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:24:40 +02:00
func mail_data(data=null,from=null,subject=null):
2023-09-18 12:27:06 +02:00
var corpo : String = ""
2020-07-25 19:56:17 +02:00
for i in data:
corpo = corpo + i + "\r\n"
corpo=corpo + "."
2020-07-25 19:56:17 +02:00
var r_code=send("DATA")
2023-09-18 12:24:40 +02:00
r_code=wait_answer("354")
# if r_code == OK and from != null:
# r_code=send("FROM: ",bracket(from))
2020-07-25 19:56:17 +02:00
if r_code == OK and subject != null:
r_code=send("SUBJECT: ",subject)
if r_code == OK and data != null:
r_code=send(corpo)
2023-09-18 12:24:40 +02:00
r_code =wait_answer("250")
2020-07-25 19:56:17 +02:00
return r_code
2023-09-18 12:24:40 +02:00
func mail_quit():
2020-07-25 19:56:17 +02:00
return send("QUIT")
func bracket(data):
return "<"+data+">"
2023-09-18 12:14:24 +02:00
func _on_Button_pressed() -> void:
2023-09-18 12:37:45 +02:00
send_mail("", "TEST SUBJECT", "TEST MSG!")
2023-09-18 12:14:24 +02:00
var debug = true
func display(data):
if debug == true:
print("debug: ",data)