| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/net/smtpclients.h" | ||
| 30 | #include "gate/results.h" | ||
| 31 | #include "gate/encode/base64.h" | ||
| 32 | #include "gate/debugging.h" | ||
| 33 | #include "gate/net/sockettools.h" | ||
| 34 | #include "gate/net/sslsessions.h" | ||
| 35 | #include "gate/guids.h" | ||
| 36 | |||
| 37 | #if defined(GATE_SYS_WIN) | ||
| 38 | # define GATE_NET_SMTPCLIENTS_WINCDO 1 | ||
| 39 | #elif defined(GATE_SYS_POSIX) | ||
| 40 | # define GATE_NET_SMTPCLIENTS_CURL 1 | ||
| 41 | #endif | ||
| 42 | |||
| 43 | ✗ | static gate_result_t read_smtp_server_response_line(gate_stream_t* stream, int* out_response_code, gate_string_t* out_message) | |
| 44 | { | ||
| 45 | char buffer[GATE_DEFAULT_LINE_LENGTH]; | ||
| 46 | gate_result_t ret; | ||
| 47 | do | ||
| 48 | { | ||
| 49 | ✗ | gate_size_t received = 0; | |
| 50 | ✗ | gate_int64_t num = 0; | |
| 51 | gate_size_t num_parsed; | ||
| 52 | |||
| 53 | ✗ | ret = gate_stream_read_line(stream, buffer, sizeof(buffer), &received); | |
| 54 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 55 | |||
| 56 | ✗ | if (received == 0) | |
| 57 | { | ||
| 58 | ✗ | ret = GATE_RESULT_ENDOFSTREAM; | |
| 59 | ✗ | break; | |
| 60 | } | ||
| 61 | ✗ | num_parsed = gate_str_parse_int64(buffer, received, &num); | |
| 62 | ✗ | if (num_parsed == 0) | |
| 63 | { | ||
| 64 | ✗ | ret = GATE_RESULT_INVALIDDATA; | |
| 65 | ✗ | break; | |
| 66 | } | ||
| 67 | |||
| 68 | /* success case reached -> return values */ | ||
| 69 | ✗ | if (out_response_code != NULL) | |
| 70 | { | ||
| 71 | ✗ | *out_response_code = (int)num; | |
| 72 | } | ||
| 73 | ✗ | if (out_message != NULL) | |
| 74 | { | ||
| 75 | ✗ | if (NULL == gate_string_create(out_message, &buffer[num_parsed], received - num_parsed)) | |
| 76 | { | ||
| 77 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 78 | ✗ | break; | |
| 79 | } | ||
| 80 | ✗ | gate_string_trim(out_message, out_message); | |
| 81 | } | ||
| 82 | } while (0); | ||
| 83 | ✗ | return ret; | |
| 84 | } | ||
| 85 | |||
| 86 | ✗ | static gate_result_t read_smpt_server_response_result(gate_stream_t* stream, int required_response_code, gate_string_t* out_required_message) | |
| 87 | { | ||
| 88 | ✗ | int response_code = 0; | |
| 89 | ✗ | gate_result_t const result = read_smtp_server_response_line(stream, &response_code, out_required_message); | |
| 90 | ✗ | if (GATE_SUCCEEDED(result)) | |
| 91 | { | ||
| 92 | ✗ | if (response_code != required_response_code) | |
| 93 | { | ||
| 94 | ✗ | if (out_required_message != NULL) | |
| 95 | { | ||
| 96 | ✗ | gate_string_release(out_required_message); | |
| 97 | } | ||
| 98 | ✗ | return GATE_RESULT_INVALIDSTATE; | |
| 99 | } | ||
| 100 | } | ||
| 101 | ✗ | return result; | |
| 102 | } | ||
| 103 | |||
| 104 | static gate_string_t const str_smtp_msg_ok = GATE_STRING_INIT_STATIC("OK"); | ||
| 105 | |||
| 106 | ✗ | static gate_result_t read_smtp_server_response_until(gate_stream_t* stream, int required_response_code, gate_string_t const* required_message) | |
| 107 | { | ||
| 108 | gate_result_t result; | ||
| 109 | ✗ | gate_bool_t read_next_line = true; | |
| 110 | ✗ | int response_code = 0; | |
| 111 | ✗ | gate_string_t message = GATE_STRING_INIT_EMPTY; | |
| 112 | |||
| 113 | ✗ | while (read_next_line) | |
| 114 | { | ||
| 115 | ✗ | result = read_smtp_server_response_line(stream, &response_code, &message); | |
| 116 | ✗ | GATE_BREAK_IF_FAILED(result); | |
| 117 | |||
| 118 | ✗ | if (response_code == required_response_code && gate_string_equals_ic(&message, required_message)) | |
| 119 | { | ||
| 120 | /* required response found */ | ||
| 121 | ✗ | read_next_line = false; | |
| 122 | } | ||
| 123 | ✗ | gate_string_release(&message); | |
| 124 | } | ||
| 125 | ✗ | return result; | |
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | ✗ | gate_result_t gate_smtpclientstream_init(gate_stream_t* stream) | |
| 130 | { | ||
| 131 | ✗ | int response_code = 0; | |
| 132 | ✗ | gate_result_t result = read_smtp_server_response_line(stream, &response_code, NULL); | |
| 133 | ✗ | if (response_code < 200 || response_code >= 300) | |
| 134 | { | ||
| 135 | ✗ | return GATE_RESULT_INVALIDSTATE; | |
| 136 | } | ||
| 137 | ✗ | return result; | |
| 138 | } | ||
| 139 | |||
| 140 | ✗ | static gate_result_t send_smtp_msg(gate_stream_t* stream, char const* ptr_data, gate_size_t data_len) | |
| 141 | { | ||
| 142 | ✗ | gate_size_t len_written = 0; | |
| 143 | ✗ | gate_result_t const result = gate_stream_write_block(stream, ptr_data, data_len, &len_written); | |
| 144 | ✗ | if (GATE_SUCCEEDED(result)) | |
| 145 | { | ||
| 146 | ✗ | if (len_written < data_len) | |
| 147 | { | ||
| 148 | ✗ | return GATE_RESULT_ENDOFSTREAM; | |
| 149 | } | ||
| 150 | } | ||
| 151 | ✗ | return result; | |
| 152 | } | ||
| 153 | |||
| 154 | ✗ | static gate_result_t send_smtp_msg_strbuilder(gate_stream_t* stream, gate_strbuilder_t* sb) | |
| 155 | { | ||
| 156 | ✗ | char const* const ptr_data = gate_strbuilder_ptr(sb, 0); | |
| 157 | ✗ | gate_size_t const data_len = gate_strbuilder_length(sb); | |
| 158 | ✗ | return send_smtp_msg(stream, ptr_data, data_len); | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | static gate_result_t send_smtp_msg_str(gate_stream_t* stream, char const* str) | |
| 162 | { | ||
| 163 | ✗ | gate_size_t const len = gate_str_length(str); | |
| 164 | ✗ | return send_smtp_msg(stream, str, len); | |
| 165 | } | ||
| 166 | |||
| 167 | ✗ | static gate_result_t send_smtp_msg_cmd(gate_stream_t* stream, char const* cmd, gate_string_t const* param) | |
| 168 | { | ||
| 169 | char buffer[GATE_DEFAULT_LINE_LENGTH]; | ||
| 170 | ✗ | gate_strbuilder_t sb = GATE_INIT_EMPTY; | |
| 171 | ✗ | gate_strbuilder_create_static(&sb, buffer, sizeof(buffer), 0); | |
| 172 | ✗ | if (!gate_str_is_empty(cmd)) | |
| 173 | { | ||
| 174 | ✗ | gate_strbuilder_append_cstr(&sb, cmd); | |
| 175 | } | ||
| 176 | ✗ | if (!gate_string_is_empty(param)) | |
| 177 | { | ||
| 178 | ✗ | gate_strbuilder_append_string(&sb, param); | |
| 179 | } | ||
| 180 | ✗ | gate_strbuilder_append_cstr(&sb, GATE_STR_CRLF); | |
| 181 | ✗ | return send_smtp_msg_strbuilder(stream, &sb); | |
| 182 | } | ||
| 183 | |||
| 184 | ✗ | gate_result_t gate_smtpclientstream_login(gate_stream_t* stream, gate_string_t const* host, gate_string_t const* user, gate_string_t const* password) | |
| 185 | { | ||
| 186 | ✗ | gate_result_t ret = GATE_RESULT_FAILED; | |
| 187 | char buffer[GATE_DEFAULT_LINE_LENGTH]; | ||
| 188 | ✗ | gate_strbuilder_t sb = GATE_INIT_EMPTY; | |
| 189 | ✗ | gate_string_t str_auth = GATE_STRING_INIT_EMPTY; | |
| 190 | do | ||
| 191 | { | ||
| 192 | ✗ | gate_strbuilder_create_static(&sb, buffer, sizeof(buffer), 0); | |
| 193 | |||
| 194 | ✗ | ret = send_smtp_msg_cmd(stream, "EHLO ", host); | |
| 195 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 196 | ✗ | gate_strbuilder_discard(&sb, GATE_STR_NPOS); | |
| 197 | |||
| 198 | ✗ | ret = read_smtp_server_response_until(stream, 250, &str_smtp_msg_ok); | |
| 199 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 200 | |||
| 201 | ✗ | if (!gate_string_is_empty(user) && !gate_string_is_empty(password)) | |
| 202 | { | ||
| 203 | int response_code; | ||
| 204 | ✗ | ret = send_smtp_msg_cmd(stream, "AUTH LOGIN", NULL); | |
| 205 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 206 | |||
| 207 | ✗ | ret = read_smpt_server_response_result(stream, 334, &str_auth); | |
| 208 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 209 | |||
| 210 | /* username requested in base64 */ | ||
| 211 | ✗ | if (!gate_string_equals_str(&str_auth, "VXNlcm5hbWU6")) | |
| 212 | { | ||
| 213 | ✗ | ret = GATE_RESULT_INVALIDSTATE; | |
| 214 | ✗ | break; | |
| 215 | } | ||
| 216 | ✗ | gate_string_release(&str_auth); | |
| 217 | |||
| 218 | /* send username */ | ||
| 219 | ✗ | ret = gate_base64_encode(user, &sb); | |
| 220 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 221 | ✗ | gate_strbuilder_append_cstr(&sb, GATE_STR_CRLF); | |
| 222 | ✗ | ret = send_smtp_msg_strbuilder(stream, &sb); | |
| 223 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 224 | ✗ | gate_strbuilder_discard(&sb, GATE_STR_NPOS); | |
| 225 | |||
| 226 | /* await response */ | ||
| 227 | ✗ | ret = read_smpt_server_response_result(stream, 334, &str_auth); | |
| 228 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 229 | |||
| 230 | /* password requested in base64 */ | ||
| 231 | ✗ | if (!gate_string_equals_str(&str_auth, "UGFzc3dvcmQ6")) | |
| 232 | { | ||
| 233 | ✗ | ret = GATE_RESULT_INVALIDSTATE; | |
| 234 | ✗ | break; | |
| 235 | } | ||
| 236 | ✗ | gate_string_release(&str_auth); | |
| 237 | |||
| 238 | /* send password */ | ||
| 239 | ✗ | ret = gate_base64_encode(password, &sb); | |
| 240 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 241 | ✗ | gate_strbuilder_append_cstr(&sb, GATE_STR_CRLF); | |
| 242 | ✗ | ret = send_smtp_msg_strbuilder(stream, &sb); | |
| 243 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 244 | ✗ | gate_strbuilder_discard(&sb, GATE_STR_NPOS); | |
| 245 | |||
| 246 | /* await response */ | ||
| 247 | ✗ | ret = read_smpt_server_response_result(stream, 235, NULL); | |
| 248 | } | ||
| 249 | } while (0); | ||
| 250 | |||
| 251 | ✗ | gate_string_release(&str_auth); | |
| 252 | ✗ | gate_strbuilder_release(&sb); | |
| 253 | ✗ | return ret; | |
| 254 | } | ||
| 255 | |||
| 256 | ✗ | static gate_result_t send_text_body_line_by_line(gate_stream_t* stream, gate_string_t const* text) | |
| 257 | { | ||
| 258 | gate_result_t ret; | ||
| 259 | gate_string_t src; | ||
| 260 | ✗ | gate_string_t line = GATE_STRING_INIT_EMPTY; | |
| 261 | ✗ | gate_string_duplicate(&src, text); | |
| 262 | |||
| 263 | ✗ | while (NULL != gate_string_read_line(&line, &src, &src)) | |
| 264 | { | ||
| 265 | ✗ | gate_string_rtrim(&line, &line); | |
| 266 | ✗ | if (gate_string_starts_with_char(&line, '.')) | |
| 267 | { | ||
| 268 | ✗ | ret = gate_stream_print_cstr(stream, "."); | |
| 269 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 270 | } | ||
| 271 | ✗ | ret = gate_stream_print_string(stream, &line); | |
| 272 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 273 | ✗ | ret = gate_stream_print_cstr(stream, "\r\n"); | |
| 274 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 275 | |||
| 276 | ✗ | gate_string_release(&line); | |
| 277 | } | ||
| 278 | ✗ | gate_string_release(&line); | |
| 279 | ✗ | gate_string_release(&src); | |
| 280 | ✗ | return ret; | |
| 281 | } | ||
| 282 | |||
| 283 | ✗ | gate_result_t send_base64_content(gate_stream_t* stream, gate_stream_t* data) | |
| 284 | { | ||
| 285 | ✗ | gate_result_t ret = GATE_RESULT_OK; | |
| 286 | char plain[60]; | ||
| 287 | char encoded[80]; | ||
| 288 | ✗ | gate_size_t plain_len = 0; | |
| 289 | ✗ | gate_size_t written = 0; | |
| 290 | |||
| 291 | ✗ | while (GATE_SUCCEEDED(ret)) | |
| 292 | { | ||
| 293 | ✗ | gate_size_t encoded_len = sizeof(encoded); | |
| 294 | ✗ | ret = gate_stream_read_block(data, plain, sizeof(plain), &plain_len); | |
| 295 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 296 | |||
| 297 | ✗ | if (plain_len == 0) | |
| 298 | { | ||
| 299 | ✗ | break; | |
| 300 | } | ||
| 301 | |||
| 302 | ✗ | ret = gate_base64_encode_buffer(plain, plain_len, encoded, &encoded_len); | |
| 303 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 304 | |||
| 305 | ✗ | ret = gate_stream_write_block(stream, encoded, encoded_len, &written); | |
| 306 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 307 | |||
| 308 | ✗ | if (written != encoded_len) | |
| 309 | { | ||
| 310 | ✗ | ret = GATE_RESULT_FAILED; | |
| 311 | } | ||
| 312 | |||
| 313 | ✗ | ret = gate_stream_print_cstr(stream, GATE_STR_CRLF); | |
| 314 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 315 | } | ||
| 316 | ✗ | return ret; | |
| 317 | } | ||
| 318 | |||
| 319 | ✗ | gate_result_t gate_smtpclientstream_send(gate_stream_t* stream, gate_mail_message_t const* mail) | |
| 320 | { | ||
| 321 | gate_result_t ret; | ||
| 322 | |||
| 323 | do | ||
| 324 | { | ||
| 325 | gate_string_t const* ptr_entry; | ||
| 326 | gate_enumerator_t e; | ||
| 327 | |||
| 328 | /* add mail-from */ | ||
| 329 | ✗ | ret = send_smtp_msg_cmd(stream, "MAIL FROM:", &mail->sender); | |
| 330 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 331 | |||
| 332 | ✗ | ret = read_smpt_server_response_result(stream, 250, NULL); | |
| 333 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 334 | |||
| 335 | /* add mail-to recipients */ | ||
| 336 | ✗ | gate_arraylist_enumerate(mail->recipients, &e); | |
| 337 | ✗ | GATE_FOR_ENUMERATOR(ptr_entry, &e) | |
| 338 | { | ||
| 339 | ✗ | ret = send_smtp_msg_cmd(stream, "RCPT TO:", ptr_entry); | |
| 340 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 341 | |||
| 342 | ✗ | ret = read_smpt_server_response_result(stream, 250, NULL); | |
| 343 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 344 | } | ||
| 345 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 346 | |||
| 347 | /* add CC mail-to recipients */ | ||
| 348 | ✗ | gate_arraylist_enumerate(mail->recipients_cc, &e); | |
| 349 | ✗ | GATE_FOR_ENUMERATOR(ptr_entry, &e) | |
| 350 | { | ||
| 351 | ✗ | ret = send_smtp_msg_cmd(stream, "RCPT TO:", ptr_entry); | |
| 352 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 353 | |||
| 354 | ✗ | ret = read_smpt_server_response_result(stream, 250, NULL); | |
| 355 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 356 | } | ||
| 357 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 358 | |||
| 359 | /* add BCC mail-to recipients */ | ||
| 360 | ✗ | gate_arraylist_enumerate(mail->recipients_bcc, &e); | |
| 361 | ✗ | GATE_FOR_ENUMERATOR(ptr_entry, &e) | |
| 362 | { | ||
| 363 | ✗ | ret = send_smtp_msg_cmd(stream, "RCPT TO:", ptr_entry); | |
| 364 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 365 | |||
| 366 | ✗ | ret = read_smpt_server_response_result(stream, 250, NULL); | |
| 367 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 368 | } | ||
| 369 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 370 | |||
| 371 | /* start data transmission */ | ||
| 372 | ✗ | ret = send_smtp_msg_cmd(stream, "DATA", NULL); | |
| 373 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 374 | |||
| 375 | ✗ | ret = read_smpt_server_response_result(stream, 354, NULL); | |
| 376 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 377 | |||
| 378 | ✗ | ret = send_smtp_msg_cmd(stream, "From: ", &mail->sender); | |
| 379 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 380 | |||
| 381 | ✗ | if (gate_arraylist_length(mail->recipients) != 0) | |
| 382 | { | ||
| 383 | /* add TO header entries */ | ||
| 384 | ✗ | gate_bool_t is_first = true; | |
| 385 | ✗ | ret = gate_stream_print_cstr(stream, "To: "); | |
| 386 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 387 | |||
| 388 | ✗ | gate_arraylist_enumerate(mail->recipients, &e); | |
| 389 | ✗ | GATE_FOR_ENUMERATOR(ptr_entry, &e) | |
| 390 | { | ||
| 391 | ✗ | if (is_first) | |
| 392 | { | ||
| 393 | ✗ | is_first = false; | |
| 394 | } | ||
| 395 | else | ||
| 396 | { | ||
| 397 | ✗ | ret = gate_stream_print_cstr(stream, ", "); | |
| 398 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 399 | } | ||
| 400 | ✗ | ret = gate_stream_print_string(stream, ptr_entry); | |
| 401 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 402 | } | ||
| 403 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 404 | |||
| 405 | ✗ | ret = gate_stream_print_cstr(stream, GATE_STR_CRLF); | |
| 406 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 407 | } | ||
| 408 | |||
| 409 | ✗ | if (gate_arraylist_length(mail->recipients_cc) != 0) | |
| 410 | { | ||
| 411 | /* add CC header entries */ | ||
| 412 | ✗ | gate_bool_t is_first = true; | |
| 413 | ✗ | ret = gate_stream_print_cstr(stream, "Cc: "); | |
| 414 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 415 | |||
| 416 | ✗ | gate_arraylist_enumerate(mail->recipients_cc, &e); | |
| 417 | ✗ | GATE_FOR_ENUMERATOR(ptr_entry, &e) | |
| 418 | { | ||
| 419 | ✗ | if (is_first) | |
| 420 | { | ||
| 421 | ✗ | is_first = false; | |
| 422 | } | ||
| 423 | else | ||
| 424 | { | ||
| 425 | ✗ | ret = gate_stream_print_cstr(stream, ", "); | |
| 426 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 427 | |||
| 428 | } | ||
| 429 | ✗ | ret = gate_stream_print_string(stream, ptr_entry); | |
| 430 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 431 | } | ||
| 432 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 433 | |||
| 434 | ✗ | ret = gate_stream_print_cstr(stream, GATE_STR_CRLF); | |
| 435 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 436 | } | ||
| 437 | |||
| 438 | /* send subject header */ | ||
| 439 | ✗ | ret = send_smtp_msg_cmd(stream, "Subject: ", &mail->subject); | |
| 440 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 441 | |||
| 442 | |||
| 443 | ✗ | if (gate_map_count(&mail->attachments) == 0 && gate_string_is_empty(&mail->html_body)) | |
| 444 | { | ||
| 445 | /* simple text message */ | ||
| 446 | |||
| 447 | /* send header-end empty line */ | ||
| 448 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 449 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 450 | |||
| 451 | /* send text-body line by line */ | ||
| 452 | ✗ | ret = send_text_body_line_by_line(stream, &mail->text_body); | |
| 453 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 454 | } | ||
| 455 | else | ||
| 456 | { | ||
| 457 | /* multipart message */ | ||
| 458 | ✗ | char boundary[40] = GATE_INIT_EMPTY; | |
| 459 | gate_guid_t g; | ||
| 460 | gate_string_t str_boundary; | ||
| 461 | gate_map_iterator_t iter; | ||
| 462 | |||
| 463 | ✗ | gate_guid_generate(&g); | |
| 464 | ✗ | gate_guid_to_string(&g, boundary); | |
| 465 | ✗ | gate_string_create_static(&str_boundary, boundary); | |
| 466 | |||
| 467 | ✗ | ret = send_smtp_msg_str(stream, | |
| 468 | "MIME-Version: 1.0" GATE_STR_CRLF | ||
| 469 | "Content-Type: multipart/mixed;" GATE_STR_CRLF | ||
| 470 | " boundary=\"----"); | ||
| 471 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 472 | ✗ | ret = send_smtp_msg_str(stream, boundary); | |
| 473 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 474 | ✗ | ret = send_smtp_msg_str(stream, "\"" GATE_STR_CRLF | |
| 475 | GATE_STR_CRLF); | ||
| 476 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 477 | |||
| 478 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 479 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 480 | ✗ | ret = send_smtp_msg_cmd(stream, "------", &str_boundary); | |
| 481 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 482 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Type: text/plain; charset=UTF-8", NULL); | |
| 483 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 484 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Transfer-Encoding : 8bit", NULL); | |
| 485 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 486 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 487 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 488 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, &mail->text_body); | |
| 489 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 490 | |||
| 491 | ✗ | if (!gate_string_is_empty(&mail->html_body)) | |
| 492 | { | ||
| 493 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 494 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 495 | ✗ | ret = send_smtp_msg_cmd(stream, "------", &str_boundary); | |
| 496 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 497 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Type: text/html; charset=UTF-8", NULL); | |
| 498 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 499 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Transfer-Encoding : 8bit", NULL); | |
| 500 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 501 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 502 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 503 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, &mail->html_body); | |
| 504 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 505 | } | ||
| 506 | |||
| 507 | ✗ | GATE_MAP_FOREACH(iter, &mail->attachments) | |
| 508 | { | ||
| 509 | ✗ | gate_string_t const* filename = gate_map_iterator_key(iter); | |
| 510 | ✗ | gate_stream_t* const* data = gate_map_iterator_value(iter); | |
| 511 | |||
| 512 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 513 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 514 | ✗ | ret = send_smtp_msg_cmd(stream, "------", &str_boundary); | |
| 515 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 516 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Type: application/octet-stream", NULL); | |
| 517 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 518 | ✗ | ret = send_smtp_msg_str(stream, "Content-Disposition: attachment; filename=\""); | |
| 519 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 520 | ✗ | ret = send_smtp_msg(stream, filename->str, filename->length); | |
| 521 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 522 | ✗ | ret = send_smtp_msg_str(stream, "\"" GATE_STR_CRLF); | |
| 523 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 524 | ✗ | ret = send_smtp_msg_cmd(stream, "Content-Transfer-Encoding: base64", NULL); | |
| 525 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 526 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 527 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 528 | |||
| 529 | ✗ | ret = send_base64_content(stream, *data); | |
| 530 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 531 | } | ||
| 532 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 533 | |||
| 534 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 535 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 536 | ✗ | ret = send_smtp_msg_str(stream, "------"); | |
| 537 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 538 | ✗ | ret = send_smtp_msg_str(stream, boundary); | |
| 539 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 540 | ✗ | ret = send_smtp_msg_str(stream, "--" GATE_STR_CRLF); | |
| 541 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 542 | } | ||
| 543 | |||
| 544 | /* add empty line to indicate end-of-data */ | ||
| 545 | ✗ | ret = send_smtp_msg_cmd(stream, NULL, NULL); | |
| 546 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 547 | |||
| 548 | /* send ending-dot line */ | ||
| 549 | ✗ | ret = send_smtp_msg_cmd(stream, ".", NULL); | |
| 550 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 551 | |||
| 552 | /* read server response */ | ||
| 553 | ✗ | ret = read_smpt_server_response_result(stream, 250, NULL); | |
| 554 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 555 | |||
| 556 | } while (0); | ||
| 557 | |||
| 558 | ✗ | return ret; | |
| 559 | } | ||
| 560 | |||
| 561 | ✗ | gate_result_t gate_smtpclientstream_quit(gate_stream_t* stream) | |
| 562 | { | ||
| 563 | gate_result_t ret; | ||
| 564 | do | ||
| 565 | { | ||
| 566 | ✗ | ret = send_smtp_msg_cmd(stream, "QUIT", NULL); | |
| 567 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 568 | |||
| 569 | ✗ | ret = read_smpt_server_response_result(stream, 221, NULL); | |
| 570 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 571 | } while (0); | ||
| 572 | ✗ | return ret; | |
| 573 | } | ||
| 574 | |||
| 575 | |||
| 576 | |||
| 577 | |||
| 578 | #if defined(GATE_NET_SMTPCLIENTS_WINCDO) | ||
| 579 | |||
| 580 | #include "gate/net/platform/cdosys_api.h" | ||
| 581 | #include "gate/uris.h" | ||
| 582 | |||
| 583 | static gate_result_t cdosys_smtpclient_create(gate_smtpclient_t* client) | ||
| 584 | { | ||
| 585 | GATE_UNUSED_ARG(client); | ||
| 586 | return GATE_RESULT_OK; | ||
| 587 | } | ||
| 588 | |||
| 589 | static gate_result_t cdosys_smtpclient_release(gate_smtpclient_t* client) | ||
| 590 | { | ||
| 591 | GATE_UNUSED_ARG(client); | ||
| 592 | return GATE_RESULT_OK; | ||
| 593 | } | ||
| 594 | |||
| 595 | static gate_result_t stringlist_to_senderstring(gate_string_t* target, gate_arraylist_t lst) | ||
| 596 | { | ||
| 597 | const gate_size_t length = gate_arraylist_is_null(lst) ? 0 : gate_arraylist_length(lst); | ||
| 598 | if (length == 0) | ||
| 599 | { | ||
| 600 | gate_string_create_empty(target); | ||
| 601 | return GATE_RESULT_OK; | ||
| 602 | } | ||
| 603 | else | ||
| 604 | { | ||
| 605 | gate_result_t ret; | ||
| 606 | gate_strbuilder_t sb; | ||
| 607 | gate_size_t ndx; | ||
| 608 | |||
| 609 | gate_strbuilder_create(&sb, 48); | ||
| 610 | for (ndx = 0; ndx != length; ++ndx) | ||
| 611 | { | ||
| 612 | gate_string_t const* entry = (gate_string_t const*)gate_arraylist_get(lst, ndx); | ||
| 613 | if (gate_string_is_empty(entry)) | ||
| 614 | { | ||
| 615 | continue; | ||
| 616 | } | ||
| 617 | if (gate_strbuilder_length(&sb) != 0) | ||
| 618 | { | ||
| 619 | gate_strbuilder_append_cstr(&sb, ", "); | ||
| 620 | } | ||
| 621 | gate_strbuilder_append_string(&sb, entry); | ||
| 622 | } | ||
| 623 | ret = (NULL == gate_strbuilder_to_string(&sb, target)) | ||
| 624 | ? GATE_RESULT_OUTOFMEMORY | ||
| 625 | : GATE_RESULT_OK; | ||
| 626 | gate_strbuilder_release(&sb); | ||
| 627 | return ret; | ||
| 628 | } | ||
| 629 | } | ||
| 630 | |||
| 631 | static gate_result_t cdosys_smtpclient_send(gate_smtpclient_t* client, gate_mail_message_t const* message) | ||
| 632 | { | ||
| 633 | gate_result_t result; | ||
| 634 | gate_bool_t deinit_com = false; | ||
| 635 | gate_string_t receiver_to = GATE_INIT_EMPTY; | ||
| 636 | gate_string_t receiver_cc = GATE_INIT_EMPTY; | ||
| 637 | gate_string_t receiver_bcc = GATE_INIT_EMPTY; | ||
| 638 | |||
| 639 | GATE_DEBUG_ASSERT(client != NULL); | ||
| 640 | GATE_DEBUG_ASSERT(message != NULL); | ||
| 641 | |||
| 642 | do | ||
| 643 | { | ||
| 644 | gate_string_t smtp_server; | ||
| 645 | gate_string_t smtp_user; | ||
| 646 | gate_string_t smtp_pass; | ||
| 647 | |||
| 648 | result = gate_win32_com_init(); | ||
| 649 | GATE_BREAK_IF_FAILED(result); | ||
| 650 | deinit_com = result == GATE_RESULT_OK; | ||
| 651 | |||
| 652 | result = stringlist_to_senderstring(&receiver_to, message->recipients); | ||
| 653 | GATE_BREAK_IF_FAILED(result); | ||
| 654 | result = stringlist_to_senderstring(&receiver_cc, message->recipients_cc); | ||
| 655 | GATE_BREAK_IF_FAILED(result); | ||
| 656 | result = stringlist_to_senderstring(&receiver_bcc, message->recipients_bcc); | ||
| 657 | GATE_BREAK_IF_FAILED(result); | ||
| 658 | |||
| 659 | gate_string_create_static(&smtp_server, client->server); | ||
| 660 | gate_string_create_static(&smtp_user, &client->config[0]); | ||
| 661 | gate_string_create_static(&smtp_pass, &client->config[smtp_user.length + 1]); | ||
| 662 | |||
| 663 | result = cdosys_send_mail( | ||
| 664 | &smtp_server, client->port, | ||
| 665 | GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_SECURE), | ||
| 666 | &smtp_user, &smtp_pass, | ||
| 667 | &message->sender, | ||
| 668 | &receiver_to, &receiver_cc, &receiver_bcc, | ||
| 669 | &message->subject, | ||
| 670 | &message->text_body, | ||
| 671 | &message->html_body); | ||
| 672 | |||
| 673 | } while (0); | ||
| 674 | |||
| 675 | gate_string_release(&receiver_bcc); | ||
| 676 | gate_string_release(&receiver_cc); | ||
| 677 | gate_string_release(&receiver_to); | ||
| 678 | |||
| 679 | gate_win32_com_pump_messages(0); | ||
| 680 | |||
| 681 | if (deinit_com) | ||
| 682 | { | ||
| 683 | gate_win32_com_uninit(); | ||
| 684 | } | ||
| 685 | |||
| 686 | return result; | ||
| 687 | } | ||
| 688 | |||
| 689 | #endif /* GATE_NETSMTPCLIENTS_WINCDO */ | ||
| 690 | |||
| 691 | |||
| 692 | |||
| 693 | #if defined(GATE_NET_SMTPCLIENTS_CURL) | ||
| 694 | |||
| 695 | #include "gate/net/platform/libcurl_api.h" | ||
| 696 | #include "gate/utilities.h" | ||
| 697 | |||
| 698 | 1 | static gate_result_t curl_smtpclient_create(gate_smtpclient_t* client) | |
| 699 | { | ||
| 700 | gate_result_t ret; | ||
| 701 | |||
| 702 | do | ||
| 703 | { | ||
| 704 | 1 | CURL* handle = NULL; | |
| 705 | gate_strbuilder_t sb; | ||
| 706 | gate_size_t cfg_len; | ||
| 707 | 1 | gate_bool_t const use_ssl = GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_SECURE); | |
| 708 | |||
| 709 | 1 | ret = load_libcurl_functions(); | |
| 710 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_BREAK_IF_FAILED(ret); |
| 711 | |||
| 712 | 1 | handle = curl.easy_init(); | |
| 713 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (NULL == handle) |
| 714 | { | ||
| 715 | ✗ | ret = GATE_RESULT_FAILED; | |
| 716 | ✗ | break; | |
| 717 | } | ||
| 718 | |||
| 719 | { | ||
| 720 | char buffer[256]; | ||
| 721 | 1 | gate_size_t len = gate_str_length(client->server); | |
| 722 | 1 | gate_str_print_text(buffer, sizeof(buffer), client->server, len); | |
| 723 | 1 | gate_strbuilder_create_static(&sb, client->server, sizeof(client->server), 0); | |
| 724 | 1 | gate_strbuilder_append(&sb, | |
| 725 | GATE_PRINT_CSTR, "smtp://", | ||
| 726 | GATE_PRINT_CSTR, buffer, | ||
| 727 | GATE_PRINT_CSTR, ":", | ||
| 728 |
1/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | GATE_PRINT_UI16, (client->port != 0 ? client->port : (gate_uint16_t)(use_ssl ? 465 : 25)), |
| 729 | GATE_PRINT_END); | ||
| 730 | 1 | curl.easy_setopt(handle, CURLOPT_URL, client->server); | |
| 731 | } | ||
| 732 | |||
| 733 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (use_ssl) |
| 734 | { | ||
| 735 | 1 | curl.easy_setopt(handle, CURLOPT_USE_SSL, CURLUSESSL_ALL); | |
| 736 | } | ||
| 737 | |||
| 738 | 1 | cfg_len = gate_str_length(client->config); | |
| 739 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (cfg_len != 0) |
| 740 | { | ||
| 741 | 1 | char const* user = client->config; | |
| 742 | 1 | char const* pass = &client->config[cfg_len + 1]; | |
| 743 | 1 | curl.easy_setopt(handle, CURLOPT_USERNAME, user); | |
| 744 | 1 | curl.easy_setopt(handle, CURLOPT_PASSWORD, pass); | |
| 745 | } | ||
| 746 | |||
| 747 | 1 | client->handles[0] = (void*)handle; | |
| 748 | |||
| 749 | 1 | ret = GATE_RESULT_OK; | |
| 750 | } while (0); | ||
| 751 | 1 | return ret; | |
| 752 | } | ||
| 753 | |||
| 754 | 1 | static gate_result_t curl_smtpclient_release(gate_smtpclient_t* client) | |
| 755 | { | ||
| 756 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(client != NULL); |
| 757 | |||
| 758 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (client->handles[0] != NULL) |
| 759 | { | ||
| 760 | 1 | CURL* handle = (CURL*)client->handles[0]; | |
| 761 | 1 | curl.easy_cleanup(handle); | |
| 762 | 1 | client->handles[0] = NULL; | |
| 763 | } | ||
| 764 | 1 | return GATE_RESULT_OK; | |
| 765 | } | ||
| 766 | |||
| 767 | /* gate_string_t instances are not required to be NUL-terminated | ||
| 768 | -> ensure a nul-terminated string is available in cache */ | ||
| 769 | 5 | static char const* get_terminated_string_ptr(gate_string_t const* text, gate_arraylist_t cache) | |
| 770 | { | ||
| 771 |
1/2✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
|
5 | if (gate_string_is_terminated(text)) |
| 772 | { | ||
| 773 | 5 | return gate_string_ptr(text, 0); | |
| 774 | } | ||
| 775 | else | ||
| 776 | { | ||
| 777 | ✗ | gate_string_t const* const str = gate_util_stringarray_add(cache, text); | |
| 778 | ✗ | return (str == NULL) ? NULL : gate_string_ptr(str, 0); | |
| 779 | } | ||
| 780 | } | ||
| 781 | |||
| 782 | 3 | static void add_to_header_list( | |
| 783 | struct curl_slist** ptr_slist_headers, | ||
| 784 | gate_arraylist_t cache, | ||
| 785 | char const* header_prefix, | ||
| 786 | gate_string_t const* value) | ||
| 787 | { | ||
| 788 | gate_strbuilder_t sb; | ||
| 789 | gate_string_t entry; | ||
| 790 | 3 | gate_strbuilder_create(&sb, 16); | |
| 791 | 3 | gate_strbuilder_append_cstr(&sb, header_prefix); | |
| 792 | 3 | gate_strbuilder_append_string(&sb, value); | |
| 793 | 3 | gate_strbuilder_to_string(&sb, &entry); | |
| 794 | 3 | gate_util_stringarray_add(cache, &entry); | |
| 795 | 3 | *ptr_slist_headers = curl.slist_append(*ptr_slist_headers, gate_string_ptr(&entry, 0)); | |
| 796 | 3 | gate_string_release(&entry); | |
| 797 | 3 | gate_strbuilder_release(&sb); | |
| 798 | 3 | } | |
| 799 | |||
| 800 | 3 | static void create_recipient_list( | |
| 801 | struct curl_slist** slist_recipients, struct curl_slist** slist_headers, | ||
| 802 | gate_arraylist_t str_list_recipients, gate_arraylist_t cache, | ||
| 803 | char const* recipient_header_prefix) | ||
| 804 | { | ||
| 805 | 3 | struct curl_slist* recipients = *slist_recipients; | |
| 806 | 3 | struct curl_slist* headers = *slist_headers; | |
| 807 | gate_enumerator_t e; | ||
| 808 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (NULL != gate_arraylist_enumerate(str_list_recipients, &e)) |
| 809 | { | ||
| 810 | gate_string_t const* entry; | ||
| 811 |
2/2✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
|
4 | GATE_FOR_ENUMERATOR(entry, &e) |
| 812 | { | ||
| 813 | 1 | recipients = curl.slist_append(recipients, get_terminated_string_ptr(entry, cache)); | |
| 814 | 1 | add_to_header_list(&headers, cache, recipient_header_prefix, entry); | |
| 815 | } | ||
| 816 | } | ||
| 817 | 3 | *slist_recipients = recipients; | |
| 818 | 3 | *slist_headers = headers; | |
| 819 | 3 | } | |
| 820 | |||
| 821 | 1 | gate_result_t curl_smtpclient_send(gate_smtpclient_t* client, gate_mail_message_t const* message) | |
| 822 | { | ||
| 823 | gate_result_t ret; | ||
| 824 | 1 | struct curl_slist* slist_recipients = NULL; | |
| 825 | 1 | struct curl_slist* slist_headers = NULL; | |
| 826 | 1 | curl_mime* mime = NULL; | |
| 827 | 1 | gate_arraylist_t cache = GATE_INIT_EMPTY; | |
| 828 | |||
| 829 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(client != NULL); |
| 830 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(message != NULL); |
| 831 | |||
| 832 | do | ||
| 833 | { | ||
| 834 | 1 | char curl_errbuf[CURL_ERROR_SIZE] = GATE_INIT_EMPTY; | |
| 835 | CURLcode result; | ||
| 836 | 1 | CURL* handle = (CURL*)client->handles[0]; | |
| 837 | 1 | curl_mimepart* mime_part = NULL; | |
| 838 | gate_enumerator_t enumerator; | ||
| 839 | |||
| 840 | 1 | cache = gate_arraylist_create(sizeof(gate_string_t), NULL, 0, &gate_string_create_copy_constructor, &gate_string_destructor); | |
| 841 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (gate_arraylist_is_null(cache)) |
| 842 | { | ||
| 843 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 844 | ✗ | break; | |
| 845 | } | ||
| 846 | |||
| 847 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (handle == NULL) |
| 848 | { | ||
| 849 | ✗ | ret = GATE_RESULT_INVALIDSTATE; | |
| 850 | ✗ | break; | |
| 851 | } | ||
| 852 | |||
| 853 | 1 | add_to_header_list(&slist_headers, cache, "From: ", &message->sender); | |
| 854 | 1 | curl.easy_setopt(handle, CURLOPT_MAIL_FROM, get_terminated_string_ptr(&message->sender, cache)); | |
| 855 | |||
| 856 | 1 | create_recipient_list(&slist_recipients, &slist_headers, message->recipients, cache, "To: "); | |
| 857 | 1 | create_recipient_list(&slist_recipients, &slist_headers, message->recipients_cc, cache, "Cc: "); | |
| 858 | 1 | create_recipient_list(&slist_recipients, &slist_headers, message->recipients_bcc, cache, "Bcc: "); | |
| 859 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!gate_string_is_empty(&message->subject)) |
| 860 | { | ||
| 861 | 1 | add_to_header_list(&slist_headers, cache, "Subject: ", &message->subject); | |
| 862 | } | ||
| 863 | 1 | curl.easy_setopt(handle, CURLOPT_MAIL_RCPT, slist_recipients); | |
| 864 | 1 | curl.easy_setopt(handle, CURLOPT_HTTPHEADER, slist_headers); | |
| 865 | |||
| 866 | 1 | mime = curl.mime_init(handle); | |
| 867 | |||
| 868 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!gate_string_is_empty(&message->text_body)) |
| 869 | { | ||
| 870 | 1 | mime_part = curl.mime_addpart(mime); | |
| 871 | 1 | curl.mime_data(mime_part, | |
| 872 | get_terminated_string_ptr(&message->text_body, cache), | ||
| 873 | CURL_ZERO_TERMINATED); | ||
| 874 | 1 | curl.mime_type(mime_part, "text/plain"); | |
| 875 | } | ||
| 876 | |||
| 877 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!gate_string_is_empty(&message->html_body)) |
| 878 | { | ||
| 879 | 1 | mime_part = curl.mime_addpart(mime); | |
| 880 | 1 | curl.mime_data(mime_part, | |
| 881 | get_terminated_string_ptr(&message->html_body, cache), | ||
| 882 | CURL_ZERO_TERMINATED); | ||
| 883 | 1 | curl.mime_type(mime_part, "text/html"); | |
| 884 | } | ||
| 885 | |||
| 886 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (NULL != gate_map_enumerate(&message->attachments, &enumerator)) |
| 887 | { | ||
| 888 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | for (; gate_enumerator_valid(&enumerator); gate_enumerator_next(&enumerator)) |
| 889 | { | ||
| 890 | 1 | gate_mapping_t const* ptr_mapping = gate_enumerator_get(&enumerator); | |
| 891 | 1 | gate_string_t const* attachment_filename = (gate_string_t const*)ptr_mapping->key; | |
| 892 | 1 | gate_stream_t* attachment_stream = *((gate_stream_t**)ptr_mapping->value); | |
| 893 | 1 | gate_strbuilder_t sb = GATE_INIT_EMPTY; | |
| 894 | 1 | gate_stringstream_t* ss = NULL; | |
| 895 | 1 | gate_string_t attachment_data = GATE_STRING_INIT_EMPTY; | |
| 896 | 1 | gate_strbuilder_create(&sb, 0); | |
| 897 | 1 | ss = gate_stringstream_create_builder(&sb); | |
| 898 | 1 | gate_stream_transfer(attachment_stream, (gate_stream_t*)ss); | |
| 899 | 1 | gate_object_release(ss); | |
| 900 | 1 | gate_strbuilder_to_string(&sb, &attachment_data); | |
| 901 | 1 | gate_strbuilder_release(&sb); | |
| 902 | 1 | gate_util_stringarray_add(cache, &attachment_data); | |
| 903 | |||
| 904 | 1 | mime_part = curl.mime_addpart(mime); | |
| 905 | 2 | curl.mime_data(mime_part, | |
| 906 | 1 | gate_string_ptr(&attachment_data, 0), | |
| 907 | gate_string_length(&attachment_data)); | ||
| 908 | 1 | curl.mime_filename(mime_part, get_terminated_string_ptr(attachment_filename, cache)); | |
| 909 | 1 | curl.mime_type(mime_part, "application/octet-stream"); | |
| 910 | 1 | gate_string_release(&attachment_data); | |
| 911 | } | ||
| 912 | } | ||
| 913 | 1 | curl.easy_setopt(handle, CURLOPT_MIMEPOST, mime); | |
| 914 | |||
| 915 | 1 | curl.easy_setopt(handle, CURLOPT_ERRORBUFFER, curl_errbuf); | |
| 916 | |||
| 917 | 1 | result = curl.easy_perform(handle); | |
| 918 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | ret = result == CURLE_OK ? GATE_RESULT_OK : GATE_RESULT_FAILED; |
| 919 | |||
| 920 | } while (0); | ||
| 921 | |||
| 922 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (mime != NULL) |
| 923 | { | ||
| 924 | 1 | curl.mime_free(mime); | |
| 925 | } | ||
| 926 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (slist_recipients != NULL) |
| 927 | { | ||
| 928 | 1 | curl.slist_free_all(slist_recipients); | |
| 929 | } | ||
| 930 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (slist_headers != NULL) |
| 931 | { | ||
| 932 | 1 | curl.slist_free_all(slist_headers); | |
| 933 | } | ||
| 934 | 1 | gate_arraylist_release(&cache); | |
| 935 | |||
| 936 | 1 | return ret; | |
| 937 | } | ||
| 938 | |||
| 939 | #endif /* GATE_NET_SMTPCLIENTS_CURL */ | ||
| 940 | |||
| 941 | |||
| 942 | 1 | static gate_result_t native_smtpclient_create(gate_smtpclient_t* client) | |
| 943 | { | ||
| 944 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 945 | 1 | gate_bool_t const is_secure = GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_SECURE); | |
| 946 |
1/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1 | gate_uint16_t const port = client->port != 0 ? client->port : (is_secure ? 465 : 25); |
| 947 | char buffer[256]; | ||
| 948 | gate_strbuilder_t sb; | ||
| 949 | 1 | gate_stream_t* ptr_stream = NULL; | |
| 950 | 1 | gate_controlstream_t* ptr_ctrl_stream = NULL; | |
| 951 | 1 | gate_string_t server_address = GATE_STRING_INIT_EMPTY; | |
| 952 | do | ||
| 953 | { | ||
| 954 | gate_string_t host, user, pass; | ||
| 955 | gate_size_t len; | ||
| 956 | 1 | gate_strbuilder_create_static(&sb, buffer, sizeof(buffer), 0); | |
| 957 | 1 | gate_strbuilder_append_cstr(&sb, client->server); | |
| 958 | 1 | gate_strbuilder_append_cstr(&sb, ":"); | |
| 959 | 1 | gate_strbuilder_append_uint16(&sb, port); | |
| 960 | 1 | gate_strbuilder_to_string(&sb, &server_address); | |
| 961 | |||
| 962 | 1 | ret = gate_socketstream_create_address(&server_address, &ptr_ctrl_stream, false); | |
| 963 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
2 | GATE_BREAK_IF_FAILED(ret); |
| 964 | |||
| 965 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (is_secure) |
| 966 | { | ||
| 967 | gate_ssl_session_params_t params; | ||
| 968 | 1 | gate_mem_clear(¶ms, sizeof(params)); | |
| 969 | 1 | params.server_side = false; | |
| 970 | 1 | params.session_version = GATE_SSL_SESSION_TYPE_AUTO; | |
| 971 | 1 | gate_string_create_static(¶ms.host_name, client->server); | |
| 972 | 1 | ret = gate_ssl_stream_create(¶ms, (gate_stream_t*)ptr_ctrl_stream, &ptr_stream); | |
| 973 | 1 | gate_string_release(¶ms.host_name); | |
| 974 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
2 | GATE_BREAK_IF_FAILED(ret); |
| 975 | 1 | ret = gate_stream_flush(ptr_stream); | |
| 976 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | GATE_BREAK_IF_FAILED(ret); |
| 977 | } | ||
| 978 | else | ||
| 979 | { | ||
| 980 | ✗ | ptr_stream = (gate_stream_t*)ptr_ctrl_stream; | |
| 981 | ✗ | ptr_ctrl_stream = NULL; | |
| 982 | } | ||
| 983 | |||
| 984 | /* read SMTP server-connect response */ | ||
| 985 | ✗ | ret = gate_smtpclientstream_init(ptr_stream); | |
| 986 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 987 | |||
| 988 | /* send SMTP login */ | ||
| 989 | ✗ | gate_string_create_static(&host, client->server); | |
| 990 | ✗ | len = gate_str_length(client->config); | |
| 991 | ✗ | gate_string_create_static(&user, client->config); | |
| 992 | ✗ | gate_string_create_static(&pass, &client->config[len + 1]); | |
| 993 | |||
| 994 | ✗ | ret = gate_smtpclientstream_login(ptr_stream, &host, &user, &pass); | |
| 995 | ✗ | GATE_BREAK_IF_FAILED(ret); | |
| 996 | |||
| 997 | /* success case: */ | ||
| 998 | ✗ | client->handles[0] = (void*)ptr_stream; | |
| 999 | ✗ | ptr_stream = NULL; | |
| 1000 | } while (0); | ||
| 1001 | |||
| 1002 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptr_ctrl_stream != NULL) |
| 1003 | { | ||
| 1004 | 1 | gate_object_release(ptr_ctrl_stream); | |
| 1005 | } | ||
| 1006 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ptr_stream != NULL) |
| 1007 | { | ||
| 1008 | 1 | gate_object_release(ptr_stream); | |
| 1009 | } | ||
| 1010 | |||
| 1011 | 1 | gate_string_release(&server_address); | |
| 1012 | 1 | gate_strbuilder_release(&sb); | |
| 1013 | |||
| 1014 | 1 | return ret; | |
| 1015 | } | ||
| 1016 | |||
| 1017 | ✗ | static gate_result_t native_smtpclient_release(gate_smtpclient_t* client) | |
| 1018 | { | ||
| 1019 | ✗ | gate_stream_t* const ptr_stream = (gate_stream_t*)client->handles[0]; | |
| 1020 | ✗ | if (ptr_stream != NULL) | |
| 1021 | { | ||
| 1022 | ✗ | gate_smtpclientstream_quit(ptr_stream); | |
| 1023 | |||
| 1024 | ✗ | gate_object_release(ptr_stream); | |
| 1025 | ✗ | client->handles[0] = NULL; | |
| 1026 | } | ||
| 1027 | ✗ | return GATE_RESULT_OK; | |
| 1028 | } | ||
| 1029 | |||
| 1030 | ✗ | static gate_result_t native_smtpclient_send(gate_smtpclient_t* client, gate_mail_message_t const* message) | |
| 1031 | { | ||
| 1032 | ✗ | gate_stream_t* const ptr_stream = (gate_stream_t*)client->handles[0]; | |
| 1033 | ✗ | GATE_DEBUG_ASSERT(ptr_stream != NULL); | |
| 1034 | ✗ | return gate_smtpclientstream_send(ptr_stream, message); | |
| 1035 | } | ||
| 1036 | |||
| 1037 | 2 | gate_result_t gate_smtpclient_create(gate_smtpclient_t* client, | |
| 1038 | gate_string_t const* server, gate_uint16_t port, | ||
| 1039 | gate_string_t const* user, gate_string_t const* password, | ||
| 1040 | gate_enumint_t flags) | ||
| 1041 | { | ||
| 1042 | gate_size_t len; | ||
| 1043 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_DEBUG_ASSERT(client != NULL); |
| 1044 | 2 | gate_mem_clear(client, sizeof(gate_smtpclient_t)); | |
| 1045 | 2 | gate_string_to_buffer(server, client->server, sizeof(client->server)); | |
| 1046 | 2 | len = gate_string_to_buffer(user, &client->config[0], sizeof(client->config) - 2); | |
| 1047 | 2 | gate_string_to_buffer(password, &client->config[len + 1], sizeof(client->config) - len - 1); | |
| 1048 | 2 | client->port = port; | |
| 1049 | 2 | client->flags = flags; | |
| 1050 | |||
| 1051 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_NATIVE)) |
| 1052 | { | ||
| 1053 | #if defined(GATE_NET_SMTPCLIENTS_WINCDO) | ||
| 1054 | return cdosys_smtpclient_create(client); | ||
| 1055 | #endif | ||
| 1056 | #if defined(GATE_NET_SMTPCLIENTS_CURL) | ||
| 1057 | 1 | return curl_smtpclient_create(client); | |
| 1058 | #endif | ||
| 1059 | /* if there is no system-client, we switch to the native one */ | ||
| 1060 | GATE_FLAG_SET(client->flags, GATE_SMTPCLIENT_FLAG_NATIVE, true); | ||
| 1061 | } | ||
| 1062 | |||
| 1063 | 1 | return native_smtpclient_create(client); | |
| 1064 | } | ||
| 1065 | |||
| 1066 | 1 | gate_result_t gate_smtpclient_release(gate_smtpclient_t* client) | |
| 1067 | { | ||
| 1068 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(client != NULL); |
| 1069 | |||
| 1070 | do | ||
| 1071 | { | ||
| 1072 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_NATIVE)) |
| 1073 | { | ||
| 1074 | #if defined(GATE_NET_SMTPCLIENTS_WINCDO) | ||
| 1075 | cdosys_smtpclient_release(client); | ||
| 1076 | break; | ||
| 1077 | #endif | ||
| 1078 | #if defined(GATE_NET_SMTPCLIENTS_CURL) | ||
| 1079 | 1 | curl_smtpclient_release(client); | |
| 1080 | 1 | break; | |
| 1081 | #endif | ||
| 1082 | } | ||
| 1083 | |||
| 1084 | ✗ | native_smtpclient_release(client); | |
| 1085 | } while (0); | ||
| 1086 | |||
| 1087 | 1 | gate_mem_clear(client, sizeof(gate_smtpclient_t)); | |
| 1088 | 1 | return GATE_RESULT_OK; | |
| 1089 | } | ||
| 1090 | |||
| 1091 | 1 | gate_result_t gate_smtpclient_send(gate_smtpclient_t* client, gate_mail_message_t const* message) | |
| 1092 | { | ||
| 1093 | 1 | gate_result_t ret = GATE_RESULT_NOTIMPLEMENTED; | |
| 1094 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(client != NULL); |
| 1095 | |||
| 1096 | do | ||
| 1097 | { | ||
| 1098 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!GATE_FLAG_ENABLED(client->flags, GATE_SMTPCLIENT_FLAG_NATIVE)) |
| 1099 | { | ||
| 1100 | #if defined(GATE_NET_SMTPCLIENTS_WINCDO) | ||
| 1101 | ret = cdosys_smtpclient_send(client, message); | ||
| 1102 | break; | ||
| 1103 | #endif | ||
| 1104 | #if defined(GATE_NET_SMTPCLIENTS_CURL) | ||
| 1105 | 1 | ret = curl_smtpclient_send(client, message); | |
| 1106 | 1 | break; | |
| 1107 | #endif | ||
| 1108 | } | ||
| 1109 | |||
| 1110 | ✗ | ret = native_smtpclient_send(client, message); | |
| 1111 | } while (0); | ||
| 1112 | 1 | return ret; | |
| 1113 | } | ||
| 1114 |