From: Art Cancro Date: Tue, 8 Jan 2019 23:40:47 +0000 (-0500) Subject: Trim SMTP responses down to one line when putting them into bounce records. Also... X-Git-Tag: v939~313 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=ee8964e193dd616a539fd5216ac6850c480533a8 Trim SMTP responses down to one line when putting them into bounce records. Also put that in a separate function. --- diff --git a/citadel/modules/smtp/serv_smtpclient.c b/citadel/modules/smtp/serv_smtpclient.c index b8081fa2c..9e1226ac8 100644 --- a/citadel/modules/smtp/serv_smtpclient.c +++ b/citadel/modules/smtp/serv_smtpclient.c @@ -182,7 +182,7 @@ static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp) * by the remote server. This is an ugly way to extract it, by capturing debug data from * the library and filtering on the lines we want. */ -int ctdl_libcurl_smtp_debug_callback(CURL * handle, curl_infotype type, char *data, size_t size, void *userptr) +int ctdl_libcurl_smtp_debug_callback(CURL *handle, curl_infotype type, char *data, size_t size, void *userptr) { if (type != CURLINFO_HEADER_IN) return 0; @@ -200,6 +200,44 @@ int ctdl_libcurl_smtp_debug_callback(CURL * handle, curl_infotype type, char *da } +/* + * Go through the debug output of an SMTP transaction, and boil it down to just the final success or error response message. + */ +void trim_response(long response_code, char *response) +{ + if ((response_code < 100) || (response_code > 999) || (IsEmptyStr(response))) { + return; + } + + char *t = malloc(strlen(response)); + if (!t) { + return; + } + t[0] = 0; + + char *p; + for (p = response; *p != 0; ++p) { + if ( (*p != '\n') && (!isprint(*p)) ) { // expunge any nonprintables except for newlines + *p = ' '; + } + } + + char response_code_str[4]; + snprintf(response_code_str, sizeof response_code_str, "%ld", response_code); + char *respstart = strstr(response, response_code_str); + if (respstart == NULL) { + strcpy(response, smtpstatus(response_code)); + return; + } + strcpy(response, respstart); + + p = strstr(response, "\n"); + if (p != NULL) { + *p = 0; + } +} + + /* * Attempt a delivery to one recipient. * Returns a three-digit SMTP status code. @@ -297,23 +335,7 @@ int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from, char *res curl = NULL; // this gets reused; avoid double-free /* Trim the error message buffer down to just the actual message */ - char response_code_str[4]; - snprintf(response_code_str, sizeof response_code_str, "%ld", response_code); - char *respstart = strstr(response, response_code_str); - if (respstart == NULL) { - strcpy(response, smtpstatus(response_code)); - } else { - strcpy(response, respstart); - char *p; - for (p = response; *p != 0; ++p) { - if (*p == '\n') - *p = ' '; - if (*p == '\r') - *p = ' '; - if (!isprint(*p)) - *p = ' '; - } - } + trim_response(response_code, response); } }