diff --git a/modules/smtp/email.cpp b/modules/smtp/email.cpp index 33d75b812..f4ea4e4b5 100644 --- a/modules/smtp/email.cpp +++ b/modules/smtp/email.cpp @@ -30,13 +30,344 @@ #include "email.h" +String EMail::get_sender_address() const { + return sender_address; +} +void EMail::set_sender_address(const String &p_value) { + sender_address = p_value; +} + +String EMail::get_sender_personal() const { + return sender_personal; +} +void EMail::set_sender_personal(const String &p_value) { + sender_personal = p_value; +} + +void EMail::set_sender(String p_address, String p_personal) { + sender_address = p_address; + sender_personal = p_personal; +} + +String EMail::get_recipient_address(const int p_index) const { + ERR_FAIL_INDEX_V(p_index, to.size(), ""); + + return to[p_index].address; +} +void EMail::set_recipient_address(const int p_index, const String &p_value) { + ERR_FAIL_INDEX(p_index, to.size()); + + to.write[p_index].address = p_value; +} + +String EMail::get_recipient_personal(const int p_index) const { + ERR_FAIL_INDEX_V(p_index, to.size(), ""); + + return to[p_index].personal; +} +void EMail::set_recipient_personal(const int p_index, const String &p_value) { + ERR_FAIL_INDEX(p_index, to.size()); + + to.write[p_index].personal = p_value; +} + +void EMail::add_recipient(String p_address, String p_personal) { + Address a; + + a.address = p_address; + a.personal = p_personal; + + to.push_back(a); +} + +int EMail::get_recipient_count() const { + return to.size(); +} + +void EMail::remove_recipient(const int p_index) { + ERR_FAIL_INDEX(p_index, to.size()); + + to.remove(p_index); +} + +Array EMail::get_recipients() { + Array r; + for (int i = 0; i < to.size(); i++) { + const Address &a = to[i]; + + Array ra; + ra.push_back(a.address); + ra.push_back(a.personal); + } + return r; +} +void EMail::set_recipients(const Array &p_recipients) { + to.clear(); + + for (int i = 0; i < p_recipients.size(); i++) { + Variant v = p_recipients[i]; + + ERR_CONTINUE(v.get_type() != Variant::ARRAY); + + Array varr = v; + + ERR_CONTINUE(varr.size() != 2); + + Address a; + a.address = varr[0]; + a.personal = varr[1]; + + to.push_back(a); + } +} + +String EMail::get_cc_address(const int p_index) const { + ERR_FAIL_INDEX_V(p_index, cc.size(), ""); + + return cc[p_index].address; +} +void EMail::set_cc_address(const int p_index, const String &p_value) { + ERR_FAIL_INDEX(p_index, cc.size()); + + cc.write[p_index].address = p_value; +} + +String EMail::get_cc_personal(const int p_index) const { + ERR_FAIL_INDEX_V(p_index, cc.size(), ""); + + return cc[p_index].personal; +} +void EMail::set_cc_personal(const int p_index, const String &p_value) { + ERR_FAIL_INDEX(p_index, cc.size()); + + cc.write[p_index].personal = p_value; +} + +void EMail::add_cc(String p_address, String p_personal) { + Address a; + + a.address = p_address; + a.personal = p_personal; + + cc.push_back(a); +} + +int EMail::get_cc_count() const { + return cc.size(); +} + +void EMail::remove_cc(const int p_index) { + ERR_FAIL_INDEX(p_index, cc.size()); + + cc.remove(p_index); +} + +Array EMail::get_ccs() { + Array r; + for (int i = 0; i < cc.size(); i++) { + const Address &a = cc[i]; + + Array ra; + ra.push_back(a.address); + ra.push_back(a.personal); + } + return r; +} +void EMail::set_ccs(const Array &p_recipients) { + cc.clear(); + + for (int i = 0; i < p_recipients.size(); i++) { + Variant v = p_recipients[i]; + + ERR_CONTINUE(v.get_type() != Variant::ARRAY); + + Array varr = v; + + ERR_CONTINUE(varr.size() != 2); + + Address a; + a.address = varr[0]; + a.personal = varr[1]; + + cc.push_back(a); + } +} + +String EMail::get_subject() const { + return subject; +} + +void EMail::set_subject(String p_subject) { + subject = p_subject; +} + +String EMail::get_body() const { + return body; +} + +void EMail::set_body(String p_body) { + body = p_body; +} + +String EMail::get_to_data_string() { + String ret; + + for (int i = 0; i < to.size(); ++i) { + Address t = to[i]; + + if (ret.size() != 0) { + ret += ", "; + } + + ret += t.get_address_data_string(); + } + + return ret; +} + +String EMail::get_cc_data_string() { + String ret; + + for (int i = 0; i < to.size(); ++i) { + Address t = to[i]; + + if (ret.size() != 0) { + ret += ", "; + } + + ret += t.get_address_data_string(); + } + + return ret; +} + +String EMail::get_email_data_string(String email_default_sender_name, String email_default_sender_email) { + String from_address; + + if (sender_address.size() > 0) { + if (sender_personal.size() > 0) { + from_address = sender_personal + " "; + } else { + from_address = sender_address.split("@")[0] + " "; + } + + from_address += "<" + sender_address + ">"; + } else { + if (email_default_sender_name.size() > 0) { + from_address = email_default_sender_name + " "; + } else { + from_address = email_default_sender_email.split("@")[0] + " "; + } + + from_address += "<" + email_default_sender_email + ">"; + } + + String data = "From: " + from_address + "\n"; + + String to_data_string = get_to_data_string(); + if (!to_data_string.empty()) { + data += "To: " + to_data_string + "\n"; + } + + String cc_data_string = get_cc_data_string(); + if (!cc_data_string.empty()) { + data += "Cc: " + cc_data_string + "\n"; + } + + data += "Subject: " + subject + "\n\n"; + data += body + "\n"; + + return data; +} +String EMail::_to_string() { + String from_address; + + if (sender_personal.size() > 0) { + from_address = sender_personal + " "; + } else { + from_address = sender_address.split("@")[0] + " "; + } + + from_address += "<" + sender_address + ">"; + + String data = "From: " + from_address + "\n"; + + String to_data_string = get_to_data_string(); + if (!to_data_string.empty()) { + data += "To: " + to_data_string + "\n"; + } + + String cc_data_string = get_cc_data_string(); + if (!cc_data_string.empty()) { + data += "Cc: " + cc_data_string + "\n"; + } + + data += "Subject: " + subject + "\n\n"; + data += body + "\n"; + + return data; +} + EMail::EMail() { } EMail::~EMail() { } void EMail::_bind_methods() { - //ClassDB::bind_method(D_METHOD("get_ignored_urls"), &EMail::get_ignored_urls); - //ClassDB::bind_method(D_METHOD("set_ignored_urls", "val"), &EMail::set_ignored_urls); - //ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "ignored_urls"), "set_ignored_urls", "get_ignored_urls"); + ClassDB::bind_method(D_METHOD("get_sender_address"), &EMail::get_sender_address); + ClassDB::bind_method(D_METHOD("set_sender_address", "val"), &EMail::set_sender_address); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "sender_address"), "set_sender_address", "get_ignored_urls"); + + ClassDB::bind_method(D_METHOD("get_sender_personal"), &EMail::get_sender_personal); + ClassDB::bind_method(D_METHOD("set_sender_personal", "val"), &EMail::set_sender_personal); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "sender_personal"), "set_sender_personal", "get_sender_personal"); + + ClassDB::bind_method(D_METHOD("set_sender", "address", "personal"), &EMail::set_sender); + + // Recipients + ClassDB::bind_method(D_METHOD("get_recipient_address", "index"), &EMail::get_recipient_address); + ClassDB::bind_method(D_METHOD("set_recipient_address", "index", "val"), &EMail::set_recipient_address); + + ClassDB::bind_method(D_METHOD("get_recipient_personal", "index"), &EMail::get_recipient_personal); + ClassDB::bind_method(D_METHOD("set_recipient_personal", "index", "val"), &EMail::set_recipient_personal); + + ClassDB::bind_method(D_METHOD("add_recipient", "address", "personal"), &EMail::add_recipient); + + ClassDB::bind_method(D_METHOD("get_recipient_count"), &EMail::get_recipient_count); + + ClassDB::bind_method(D_METHOD("remove_recipient", "index"), &EMail::remove_recipient); + + ClassDB::bind_method(D_METHOD("get_recipients"), &EMail::get_recipients); + ClassDB::bind_method(D_METHOD("set_recipients", "recipients"), &EMail::set_recipients); + + // CC + ClassDB::bind_method(D_METHOD("get_cc_address", "index"), &EMail::get_cc_address); + ClassDB::bind_method(D_METHOD("set_cc_address", "index", "val"), &EMail::set_cc_address); + + ClassDB::bind_method(D_METHOD("get_cc_personal", "index"), &EMail::get_cc_personal); + ClassDB::bind_method(D_METHOD("set_cc_personal", "index", "val"), &EMail::set_cc_personal); + + ClassDB::bind_method(D_METHOD("add_cc", "address", "personal"), &EMail::add_cc); + + ClassDB::bind_method(D_METHOD("get_cc_count"), &EMail::get_cc_count); + + ClassDB::bind_method(D_METHOD("remove_cc", "index"), &EMail::remove_cc); + + ClassDB::bind_method(D_METHOD("get_ccs"), &EMail::get_ccs); + ClassDB::bind_method(D_METHOD("set_ccs", "ccs"), &EMail::set_ccs); + + // Other + ClassDB::bind_method(D_METHOD("get_subject"), &EMail::get_subject); + ClassDB::bind_method(D_METHOD("set_subject", "val"), &EMail::set_subject); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "subject"), "set_subject", "get_subject"); + + ClassDB::bind_method(D_METHOD("get_body"), &EMail::get_body); + ClassDB::bind_method(D_METHOD("set_body", "val"), &EMail::set_body); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "body"), "set_body", "get_body"); + + // Helpers + ClassDB::bind_method(D_METHOD("get_to_data_string"), &EMail::get_to_data_string); + ClassDB::bind_method(D_METHOD("get_cc_data_string"), &EMail::get_cc_data_string); + + ClassDB::bind_method(D_METHOD("get_email_data_string", "email_default_sender_name", "email_default_sender_email"), &EMail::get_email_data_string); } diff --git a/modules/smtp/email.h b/modules/smtp/email.h index c45dbf349..e877c63aa 100644 --- a/modules/smtp/email.h +++ b/modules/smtp/email.h @@ -31,17 +31,106 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ +#include "core/containers/vector.h" +#include "core/string/ustring.h" + #include "core/object/reference.h" class EMail : public Reference { GDCLASS(EMail, Reference); public: + struct Address { + String address; + String personal; + + String get_address_data_string() { + if (personal.size() == 0) { + return address; + } + + return personal + " <" + address + ">"; + } + + String get_address_data_list_string() { + if (personal.size() > 0) { + return personal + ":" + address; + } + + return address; + } + }; + + // Sender + String get_sender_address() const; + void set_sender_address(const String &p_value); + + String get_sender_personal() const; + void set_sender_personal(const String &p_value); + + void set_sender(String p_address, String p_personal); + + // Recipients + String get_recipient_address(const int p_index) const; + void set_recipient_address(const int p_index, const String &p_value); + + String get_recipient_personal(const int p_index) const; + void set_recipient_personal(const int p_index, const String &p_value); + + void add_recipient(String p_address, String p_personal); + + int get_recipient_count() const; + + void remove_recipient(const int p_index); + + Array get_recipients(); + void set_recipients(const Array &p_recipients); + + // CC + String get_cc_address(const int p_index) const; + void set_cc_address(const int p_index, const String &p_value); + + String get_cc_personal(const int p_index) const; + void set_cc_personal(const int p_index, const String &p_value); + + void add_cc(String p_address, String p_personal); + + int get_cc_count() const; + + void remove_cc(const int p_index); + + Array get_ccs(); + void set_ccs(const Array &p_ccs); + + // Other + String get_subject() const; + void set_subject(String p_subject); + + String get_body() const; + void set_body(String p_body); + + // Helpers + String get_to_data_string(); + String get_cc_data_string(); + + String get_email_data_string(String email_default_sender_name, String email_default_sender_email); + + String _to_string(); + EMail(); ~EMail(); protected: static void _bind_methods(); + + String sender_address; + String sender_personal; + + Vector
to; + Vector cc; + + String subject; + String body; }; #endif