smtp_data.c
3.12 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/errno.h>
#include <mailutils/diag.h>
#include <mailutils/filter.h>
#include <mailutils/list.h>
#include <mailutils/smtp.h>
#include <mailutils/stream.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/smtp.h>
int
_mu_smtp_data_begin (mu_smtp_t smtp)
{
int status;
status = mu_smtp_write (smtp, "DATA\r\n");
MU_SMTP_CHECK_ERROR (smtp, status);
status = mu_smtp_response (smtp);
MU_SMTP_CHECK_ERROR (smtp, status);
if (smtp->replcode[0] != '3')
return MU_ERR_REPLY;
if (mu_smtp_trace_mask (smtp, MU_SMTP_TRACE_QRY, MU_XSCRIPT_PAYLOAD))
_mu_smtp_xscript_level (smtp, MU_XSCRIPT_PAYLOAD);
if (mu_stream_ioctl (smtp->carrier, MU_IOCTL_GET_TRANSPORT_BUFFER,
&smtp->savebuf) == 0)
{
struct mu_buffer_query newbuf;
newbuf.type = MU_TRANSPORT_OUTPUT;
newbuf.buftype = mu_buffer_full;
newbuf.bufsize = 64*1024;
if (mu_stream_ioctl (smtp->carrier, MU_IOCTL_SET_TRANSPORT_BUFFER,
&newbuf) == 0)
MU_SMTP_FSET (smtp, _MU_SMTP_SAVEBUF);
}
return 0;
}
int
_mu_smtp_data_end (mu_smtp_t smtp)
{
int status = 0;
/* code is always _MU_STR_EVENT_CLOSE */
if (MU_SMTP_FISSET (smtp, _MU_SMTP_SAVEBUF))
{
status = mu_stream_ioctl (smtp->carrier, MU_IOCTL_SET_TRANSPORT_BUFFER,
&smtp->savebuf);
if (status)
mu_diag_output (MU_DIAG_NOTICE,
"failed to restore buffer state on SMTP carrier: %s",
mu_strerror (status));
}
_mu_smtp_xscript_level (smtp, MU_XSCRIPT_NORMAL);
return status;
}
static void
_smtp_event_cb (struct _mu_stream *str, int code,
unsigned long lval, void *pval)
{
mu_smtp_t smtp = str->event_cb_data;
_mu_smtp_data_end (smtp);
}
int
mu_smtp_data (mu_smtp_t smtp, mu_stream_t *pstream)
{
int status;
mu_stream_t input;
if (!smtp)
return EINVAL;
if (MU_SMTP_FISSET (smtp, _MU_SMTP_ERR))
return MU_ERR_FAILURE;
if (smtp->state != MU_SMTP_MORE)
return MU_ERR_SEQ;
status = _mu_smtp_data_begin (smtp);
if (status)
return status;
status = mu_filter_create (&input, smtp->carrier, "CRLFDOT",
MU_FILTER_ENCODE, MU_STREAM_WRITE);
if (status)
return status;
input->event_cb = _smtp_event_cb;
input->event_cb_data = smtp;
input->event_mask = _MU_STR_EVMASK (_MU_STR_EVENT_CLOSE);
*pstream = input;
return 0;
}