1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2025 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <mailutils/cstr.h>
#include <mailutils/cctype.h>
#include <mailutils/diag.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/smtp.h>
#include <mailutils/stream.h>
#include <mailutils/sys/smtp.h>
int
mu_smtp_write (mu_smtp_t smtp, const char *fmt, ...)
{
va_list ap;
int rc;
va_start (ap, fmt);
rc = mu_stream_vprintf (smtp->carrier, fmt, ap);
va_end (ap);
return rc;
}
static int
_mu_smtp_init_mlist (mu_smtp_t smtp)
{
if (!smtp->mlrepl)
{
int rc = mu_list_create (&smtp->mlrepl);
if (rc == 0)
mu_list_set_destroy_item (smtp->mlrepl, mu_list_free_item);
return rc;
}
else
mu_list_clear (smtp->mlrepl);
return 0;
}
int
mu_smtp_response (mu_smtp_t smtp)
{
int rc;
size_t n;
rc = mu_stream_getline (smtp->carrier, &smtp->rdbuf, &smtp->rdsize, &n);
MU_SMTP_CHECK_ERROR (smtp, rc);
if (n == 0)
MU_SMTP_CHECK_ERROR (smtp, EIO);
n = mu_rtrim_class (smtp->rdbuf, MU_CTYPE_ENDLN);
if (n < 3 || !mu_isdigit (smtp->rdbuf[0]))
{
mu_diag_output (MU_DIAG_NOTICE,
"received invalid reply from SMTP server");
MU_SMTP_CHECK_ERROR (smtp, MU_ERR_BADREPLY);
}
memcpy (smtp->replcode, smtp->rdbuf, 3);
smtp->replcode[3] = 0;
if (smtp->rdbuf[3] == '-')
{
smtp->flags |= _MU_SMTP_MLREPL;
n -= 3;
if (smtp->flsize < n)
{
char *p = realloc (smtp->flbuf, n);
if (!p)
MU_SMTP_CHECK_ERROR (smtp, ENOMEM);
smtp->flbuf = p;
smtp->flsize = n;
}
memcpy (smtp->flbuf, smtp->rdbuf + 4, n - 1);
smtp->flbuf[n - 1] = 0;
smtp->replptr = smtp->flbuf;
rc = _mu_smtp_init_mlist (smtp);
MU_SMTP_CHECK_ERROR (smtp, rc);
do
{
char *p;
rc = mu_stream_getline (smtp->carrier, &smtp->rdbuf, &smtp->rdsize,
&n);
MU_SMTP_CHECK_ERROR (smtp, rc);
if (n == 0)
MU_SMTP_CHECK_ERROR (smtp, EIO);
n = mu_rtrim_class (smtp->rdbuf, MU_CTYPE_ENDLN);
if (n < 3 || memcmp (smtp->rdbuf, smtp->replcode, 3))
{
mu_diag_output (MU_DIAG_NOTICE,
"received invalid reply from SMTP server");
MU_SMTP_CHECK_ERROR (smtp, MU_ERR_BADREPLY);
}
p = strdup (smtp->rdbuf + 4);
if (!p)
MU_SMTP_CHECK_ERROR (smtp, ENOMEM);
mu_list_append (smtp->mlrepl, p);
}
while (smtp->rdbuf[3] == '-');
}
else
{
smtp->flags &= ~_MU_SMTP_MLREPL;
smtp->replptr = smtp->rdbuf + 4;
}
return 0;
}
int
mu_smtp_replcode (mu_smtp_t smtp, char *buf)
{
if (!smtp || !buf)
return EINVAL;
strcpy (buf, smtp->replcode);
return 0;
}
int
mu_smtp_sget_reply (mu_smtp_t smtp, const char **pbuf)
{
if (!smtp || !pbuf)
return EINVAL;
*pbuf = smtp->replptr;
return 0;
}
int
mu_smtp_get_reply_iterator (mu_smtp_t smtp, mu_iterator_t *pitr)
{
if (!smtp || !pitr)
return EINVAL;
return mu_list_get_iterator (smtp->mlrepl, pitr);
}
|