smtp_io.c
3.16 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2011 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_cset (smtp->rdbuf, "\r\n");
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 - 3);
smtp->replptr = smtp->flbuf;
rc = _mu_smtp_init_mlist (smtp);
MU_SMTP_CHECK_ERROR (smtp, rc);
do
{
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_cset (smtp->rdbuf, "\r\n");
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);
}
mu_list_append (smtp->mlrepl, strdup (smtp->rdbuf + 4));
}
while (smtp->rdbuf[3] == '-');
}
else
{
smtp->flags &= ~_MU_SMTP_MLREPL;
smtp->replptr = smtp->rdbuf + 4;
}
return 0;
}