+ return (sendbytes); // return the number of bytes _actually_ copied
+}
+
+
+/*
+ * The libcurl API doesn't provide a way to capture the actual SMTP result message returned
+ * 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)
+{
+ if (type != CURLINFO_HEADER_IN)
+ return 0;
+ if (!userptr)
+ return 0;
+ char *debugbuf = (char *) userptr;
+
+ int len = strlen(debugbuf);
+ if (len + size > SIZ)
+ return 0;
+
+ memcpy(&debugbuf[len], data, size);
+ debugbuf[len + size] = 0;
+ return 0;
+}
+
+
+/*
+ * 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;
+ }