Skip to content

Commit

Permalink
Code optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Apr 16, 2023
1 parent e28dda2 commit cd0c5ea
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 96 deletions.
132 changes: 72 additions & 60 deletions src/ESP_Mail_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Mail Client Arduino Library for Espressif's ESP32 and ESP8266, Raspberry Pi RP2040 Pico, and SAMD21 with u-blox NINA-W102 WiFi/Bluetooth module
*
* Created April 15, 2023
* Created April 16, 2023
*
* This library allows Espressif's ESP32, ESP8266, SAMD and RP2040 Pico devices to send and read Email through the SMTP and IMAP servers.
*
Expand Down Expand Up @@ -1453,18 +1453,55 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg)

#if defined(ENABLE_ERROR_STRING)

// if there is server reply (IMAP), return the reply instead
// If there is server meanningful response (msg) is available, return it instead
if (strlen(msg) > 0)
return msg;

// The error code enums were defined in ESP_Mail_Error.h and MB_FS.h.
// Not all error enums in ESP_Mail_Error.h was used
// then errorCode will match all cases of currently implemented enums.
switch (errorCode)
{

case TCP_CLIENT_ERROR_CONNECTION_REFUSED:
ret = esp_mail_error_network_str_7; /* "connection refused" */
break;
case TCP_CLIENT_ERROR_SEND_DATA_FAILED:
ret = esp_mail_error_network_str_8; /* "data sending failed" */
break;
case TCP_CLIENT_ERROR_NOT_INITIALIZED:
ret = esp_mail_error_client_str_1; /* "client and/or necessary callback functions are not yet assigned" */
break;
case TCP_CLIENT_ERROR_NOT_CONNECTED:
ret = esp_mail_error_network_str_4; /* "not connected" */
break;

case MAIL_CLIENT_ERROR_CONNECTION_CLOSED:
ret = esp_mail_error_network_str_6; /* "connection closed" */
break;
case MAIL_CLIENT_ERROR_READ_TIMEOUT:
ret = esp_mail_error_network_str_3; /* "session timed out" */
break;
case MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP:
ret = esp_mail_error_ssl_str_1; /* "fail to set up the SSL/TLS structure" */
break;
case MAIL_CLIENT_ERROR_OUT_OF_MEMORY:
ret = esp_mail_error_mem_str_8; /* "out of memory" */
break;
case MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED:
ret = esp_mail_error_client_str_2; /* "custom Client is not yet enabled" */
break;
case MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT:
ret = esp_mail_error_network_str_1; /* "NTP server time synching timed out" */
break;
case MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED:
ret = esp_mail_error_session_str_1; /* "the Session_Config object was not assigned" */
break;
case MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET:
ret = esp_mail_error_time_str_1; /* "library or device time was not set" */
break;
case MAIL_CLIENT_ERROR_NOT_YET_LOGIN:
ret = esp_mail_error_auth_str_3; /* "not yet log in" */
break;

#if defined(ENABLE_SMTP)
case SMTP_STATUS_SERVER_CONNECT_FAILED:
ret = esp_mail_error_network_str_2; /* "unable to connect to server" */
Expand Down Expand Up @@ -1508,23 +1545,35 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg)
case SMTP_STATUS_NO_SUPPORTED_AUTH:
ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */
break;
case IMAP_STATUS_SMTP_SESSION_WAS_NOT_ASSIGNED:
ret = esp_mail_error_session_str_2; /* "the SMTPSession object was not assigned" */
case SMTP_STATUS_SEND_CUSTOM_COMMAND_FAILED:
ret = esp_mail_error_smtp_str_10; /* "send custom command failed" */
break;
case SMTP_STATUS_UNDEFINED:
ret = esp_mail_error_smtp_str_11; /* "undefined error" */
break;
#endif

#if defined(ENABLE_IMAP)
case IMAP_STATUS_SERVER_CONNECT_FAILED:
ret = esp_mail_error_network_str_2; /* "unable to connect to server" */
break;
case IMAP_STATUS_NO_MESSAGE:
ret = esp_mail_error_imap_str_5; /* "some of the requested messages no longer exist" */
case IMAP_STATUS_IMAP_RESPONSE_FAILED:
ret = esp_mail_error_imap_str_18; /* "server replied NO or BAD response" */
break;
case IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED:
ret = esp_mail_error_imap_str_14; /* "no message changed since (assigned) modsec" */
case IMAP_STATUS_AUTHENTICATE_FAILED:
ret = esp_mail_error_imap_str_19; /* "authenticate failed" */
break;
case IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED:
ret = esp_mail_error_imap_str_15; /* "CONDSTORE was not supported or modsec was not supported for selected mailbox" */
case IMAP_STATUS_BAD_COMMAND:
ret = esp_mail_error_imap_str_17; /* "could not parse command" */
break;
case IMAP_STATUS_STORE_FAILED:
ret = esp_mail_error_imap_str_20; /* "flags or keywords store failed" */
break;
case IMAP_STATUS_SERVER_OAUTH2_LOGIN_DISABLED:
ret = esp_mail_error_imap_str_21; /* "server is not support OAuth2 login" */
break;
case IMAP_STATUS_NO_MESSAGE:
ret = esp_mail_error_imap_str_5; /* "some of the requested messages no longer exist" */
break;
case IMAP_STATUS_ERROR_DOWNLAD_TIMEOUT:
ret = esp_mail_error_network_str_5; /* "connection timeout" */
Expand All @@ -1538,12 +1587,12 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg)
case IMAP_STATUS_LIST_MAILBOXS_FAILED:
ret = esp_mail_error_imap_str_1; /* "fail to list the mailboxes" */
break;
case IMAP_STATUS_NO_SUPPORTED_AUTH:
ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */
break;
case IMAP_STATUS_CHECK_CAPABILITIES_FAILED:
ret = esp_mail_error_imap_str_2; /* "fail to check the capabilities" */
break;
case IMAP_STATUS_NO_SUPPORTED_AUTH:
ret = esp_mail_error_auth_str_1; /* "the provided SASL authentication mechanism is not support" */
break;
case IMAP_STATUS_NO_MAILBOX_FOLDER_OPENED:
ret = esp_mail_error_imap_str_5; /* "no mailbox opened" */
break;
Expand All @@ -1556,56 +1605,23 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg)
case IMAP_STATUS_FIRMWARE_UPDATE_END_FAILED:
ret = esp_mail_error_imap_str_8; /* "firmware update finalize failed" */
break;
case IMAP_STATUS_IMAP_SESSION_WAS_NOT_ASSIGNED:
ret = esp_mail_error_session_str_3; /* "the IMAPSession object was not assigned" */
case IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED:
ret = esp_mail_error_imap_str_14; /* "no message changed since (assigned) modsec" */
break;
case IMAP_STATUS_BAD_COMMAND:
ret = esp_mail_error_imap_str_17; /* "could not parse command" */
case IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED:
ret = esp_mail_error_imap_str_15; /* "CONDSTORE was not supported or modsec was not supported for selected mailbox" */
break;
#endif

case MAIL_CLIENT_ERROR_CONNECTION_CLOSED:
ret = esp_mail_error_network_str_6; /* "connection closed" */
break;
case MAIL_CLIENT_ERROR_READ_TIMEOUT:
ret = esp_mail_error_network_str_3; /* "session timed out" */
break;
case MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP:
ret = esp_mail_error_ssl_str_1; /* "fail to set up the SSL/TLS structure" */
break;
case MAIL_CLIENT_ERROR_OUT_OF_MEMORY:
ret = esp_mail_error_mem_str_8; /* "out of memory" */
break;
case TCP_CLIENT_ERROR_SEND_DATA_FAILED:
ret = esp_mail_error_network_str_8; /* "data sending failed" */
break;
case TCP_CLIENT_ERROR_CONNECTION_REFUSED:
ret = esp_mail_error_network_str_7; /* "connection refused" */
break;
case TCP_CLIENT_ERROR_NOT_INITIALIZED:
ret = esp_mail_error_client_str_1; /* "client and/or necessary callback functions are not yet assigned" */
break;
case TCP_CLIENT_ERROR_NOT_CONNECTED:
ret = esp_mail_error_network_str_4; /* "not connected" */
break;
case MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT:
ret = esp_mail_error_network_str_1; /* "NTP server time synching timed out" */
break;
case MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET:
ret = esp_mail_error_time_str_1; /* "library or device time was not set" */
break;
case MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED:
ret = esp_mail_error_client_str_2; /* "custom Client is not yet enabled" */
break;
case MAIL_CLIENT_ERROR_NOT_YET_LOGIN:
ret = esp_mail_error_auth_str_3; /* "not yet log in" */
break;
#endif

#if defined(MBFS_FLASH_FS) || defined(MBFS_SD_FS)

case MB_FS_ERROR_FILE_IO_ERROR:
ret = esp_mail_error_mem_str_7; /* "file I/O error" */
break;
case MB_FS_ERROR_FILE_NOT_FOUND:
ret = esp_mail_error_mem_str_6; /* "file not found." */
break;
case MB_FS_ERROR_FLASH_STORAGE_IS_NOT_READY:
ret = esp_mail_error_mem_str_1; /* "flash Storage is not ready." */
break;
Expand All @@ -1615,13 +1631,9 @@ String ESP_Mail_Client::errorReason(bool isSMTP, int errorCode, const char *msg)
case MB_FS_ERROR_FILE_STILL_OPENED:
ret = esp_mail_error_mem_str_5; /* "file is still opened." */
break;
case MB_FS_ERROR_FILE_NOT_FOUND:
ret = esp_mail_error_mem_str_6; /* "file not found." */
break;

#endif
default:
// Not possible case
break;
}

Expand Down
11 changes: 7 additions & 4 deletions src/ESP_Mail_Const.h
Original file line number Diff line number Diff line change
Expand Up @@ -3126,8 +3126,6 @@ static const char esp_mail_error_auth_str_3[] PROGMEM = "not yet log in";

#if defined(ENABLE_ERROR_STRING)
static const char esp_mail_error_session_str_1[] PROGMEM = "the Session_Config object was not assigned";
static const char esp_mail_error_session_str_2[] PROGMEM = "the SMTPSession object was not assigned";
static const char esp_mail_error_session_str_3[] PROGMEM = "the IMAPSession object was not assigned";
#endif

/////////////////////////
Expand All @@ -3148,6 +3146,8 @@ static const char esp_mail_error_smtp_str_5[] PROGMEM = "send body failed";
static const char esp_mail_error_smtp_str_7[] PROGMEM = "sender Email address is not valid";
static const char esp_mail_error_smtp_str_8[] PROGMEM = "some of the recipient Email address is not valid";
static const char esp_mail_error_smtp_str_9[] PROGMEM = "set recipient failed";
static const char esp_mail_error_smtp_str_10[] PROGMEM = "send custom command failed";
static const char esp_mail_error_smtp_str_11[] PROGMEM = "undefined error";
#endif
#endif

Expand Down Expand Up @@ -3176,6 +3176,10 @@ static const char esp_mail_error_imap_str_13[] PROGMEM = "this feature was not s
static const char esp_mail_error_imap_str_14[] PROGMEM = "no message changed since (assigned) modsec";
static const char esp_mail_error_imap_str_15[] PROGMEM = "CONDSTORE was not supported or modsec was not supported for selected mailbox";
static const char esp_mail_error_imap_str_17[] PROGMEM = "could not parse command";
static const char esp_mail_error_imap_str_18[] PROGMEM = "server replied NO or BAD response";
static const char esp_mail_error_imap_str_19[] PROGMEM = "authenticate failed";
static const char esp_mail_error_imap_str_20[] PROGMEM = "flags or keywords store failed";
static const char esp_mail_error_imap_str_21[] PROGMEM = "server is not support OAuth2 login";
#endif
#endif

Expand All @@ -3201,7 +3205,7 @@ static const char esp_mail_str_17[] PROGMEM = "cid:";
static const char esp_mail_str_18[] PROGMEM = "\r\n";
static const char esp_mail_str_19[] PROGMEM = "<";
static const char esp_mail_str_20[] PROGMEM = ">";
// static const char esp_mail_str_21[] PROGMEM = "";
static const char esp_mail_str_21[] PROGMEM = "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")";
static const char esp_mail_str_22[] PROGMEM = "message/rfc822";
static const char esp_mail_str_23[] PROGMEM = "upload";
static const char esp_mail_str_24[] PROGMEM = "%";
Expand Down Expand Up @@ -3279,7 +3283,6 @@ static const char esp_mail_str_96[] PROGMEM = "Date/Time: %s";
static const char esp_mail_str_97[] PROGMEM = "Recipient: %s";
static const char esp_mail_str_98[] PROGMEM = "success";
static const char esp_mail_str_99[] PROGMEM = "failed";
static const char esp_mail_str_100[] PROGMEM = "(\"name\" \"ESP Mail Client\" \"version\" \"%s\")";

#if defined(ENABLE_SMTP)
static const char boundary_table[] PROGMEM = "=_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Expand Down
48 changes: 27 additions & 21 deletions src/ESP_Mail_Error.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Created April 15, 2023
* Created April 16, 2023
*/
#pragma once

Expand All @@ -11,11 +11,10 @@
#error "Mixed versions compilation."
#endif


#define TCP_CLIENT_ERROR_CONNECTION_REFUSED (-1)
#define TCP_CLIENT_ERROR_SEND_DATA_FAILED (-2)
#define TCP_CLIENT_ERROR_NOT_INITIALIZED (-3)
#define TCP_CLIENT_ERROR_NOT_CONNECTED (-4)
#define TCP_CLIENT_ERROR_CONNECTION_REFUSED -1
#define TCP_CLIENT_ERROR_SEND_DATA_FAILED -2
#define TCP_CLIENT_ERROR_NOT_INITIALIZED -3
#define TCP_CLIENT_ERROR_NOT_CONNECTED -4

#if defined(ENABLE_SMTP)

Expand All @@ -35,16 +34,15 @@
#define SMTP_STATUS_NO_SUPPORTED_AUTH -113
#define SMTP_STATUS_SEND_CUSTOM_COMMAND_FAILED -114
#define SMTP_STATUS_UNDEFINED -115
#define IMAP_STATUS_SMTP_SESSION_WAS_NOT_ASSIGNED -116
#endif

#if defined(ENABLE_IMAP)

#define IMAP_STATUS_SERVER_CONNECT_FAILED -200
#define IMAP_STATUS_IMAP_RESPONSE_FAILED -201
#define IMAP_STATUS_LOGIN_FAILED -202
#define IMAP_STATUS_AUTHENTICATE_FAILED -202
#define IMAP_STATUS_BAD_COMMAND -203
#define IMAP_STATUS_PARSE_FLAG_FAILED -204
#define IMAP_STATUS_STORE_FAILED -204
#define IMAP_STATUS_SERVER_OAUTH2_LOGIN_DISABLED -205
#define IMAP_STATUS_NO_MESSAGE -206
#define IMAP_STATUS_ERROR_DOWNLAD_TIMEOUT -207
Expand All @@ -57,22 +55,30 @@
#define IMAP_STATUS_FIRMWARE_UPDATE_INIT_FAILED -214
#define IMAP_STATUS_FIRMWARE_UPDATE_WRITE_FAILED -215
#define IMAP_STATUS_FIRMWARE_UPDATE_END_FAILED -216
#define IMAP_STATUS_IMAP_SESSION_WAS_NOT_ASSIGNED -217
#define IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED -218
#define IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED -219
#define IMAP_STATUS_CHANGEDSINC_MODSEQ_TEST_FAILED -217
#define IMAP_STATUS_MODSEQ_WAS_NOT_SUPPORTED -218
#endif

/**
* MB_FS.h
#define MB_FS_ERROR_FILE_IO_ERROR -300
#define MB_FS_ERROR_FILE_NOT_FOUND -301
#define MB_FS_ERROR_FLASH_STORAGE_IS_NOT_READY -302
#define MB_FS_ERROR_SD_STORAGE_IS_NOT_READY -303
#define MB_FS_ERROR_FILE_STILL_OPENED -304
*/

#if defined(ENABLE_SMTP) || defined(ENABLE_IMAP)

#define MAIL_CLIENT_ERROR_CONNECTION_CLOSED -28
#define MAIL_CLIENT_ERROR_READ_TIMEOUT -29
#define MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP -32
#define MAIL_CLIENT_ERROR_OUT_OF_MEMORY -33
#define MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED -34
#define MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT -35
#define MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED -36
#define MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET -37
#define MAIL_CLIENT_ERROR_NOT_YET_LOGIN -38
#define MAIL_CLIENT_ERROR_CONNECTION_CLOSED -400
#define MAIL_CLIENT_ERROR_READ_TIMEOUT -401
#define MAIL_CLIENT_ERROR_SSL_TLS_STRUCTURE_SETUP -402
#define MAIL_CLIENT_ERROR_OUT_OF_MEMORY -403
#define MAIL_CLIENT_ERROR_CUSTOM_CLIENT_DISABLED -404
#define MAIL_CLIENT_ERROR_NTP_TIME_SYNC_TIMED_OUT -405
#define MAIL_CLIENT_ERROR_SESSION_CONFIG_WAS_NOT_ASSIGNED -406
#define MAIL_CLIENT_ERROR_TIME_WAS_NOT_SET -407
#define MAIL_CLIENT_ERROR_NOT_YET_LOGIN -408

#endif

Expand Down
Loading

0 comments on commit cd0c5ea

Please sign in to comment.