pop3_stream.c
4.72 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2003, 2007, 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 of the License, 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 this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <mailutils/filter.h>
#include <mailutils/stream.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/pop3.h>
/* Implementation of the stream for TOP and RETR. */
#define _POP3F_DONE 0x01
#define _POP3F_CHBUF 0x02
struct mu_pop3_stream
{
struct _mu_stream stream;
mu_pop3_t pop3;
int flags;
struct mu_buffer_query oldbuf;
};
static void
_pop3_event_cb (mu_stream_t str, int ev, int flags)
{
if (ev == _MU_STR_EVENT_SET)
{
mu_transport_t trans[2];
if (mu_stream_ioctl (str, MU_IOCTL_GET_TRANSPORT, trans) == 0)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *) trans[0];
_mu_pop3_xscript_level (sp->pop3, MU_XSCRIPT_NORMAL);
sp->pop3->state = MU_POP3_NO_STATE;
if (sp->flags & _POP3F_CHBUF)
{
mu_stream_ioctl (sp->pop3->carrier,
MU_IOCTL_SET_TRANSPORT_BUFFER,
&sp->oldbuf);
sp->flags = _POP3F_DONE;
}
}
}
}
static int
mu_pop3_filter_create (mu_stream_t *pstream, mu_stream_t stream)
{
int rc;
rc = mu_filter_create (pstream, stream, "CRLFDOT", MU_FILTER_DECODE,
MU_STREAM_READ | MU_STREAM_AUTOCLOSE);
if (rc == 0)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *) stream;
mu_stream_t str = *pstream;
str->event_cb = _pop3_event_cb;
str->event_mask = _MU_STR_EOF;
sp->oldbuf.type = MU_TRANSPORT_OUTPUT;
if (mu_stream_ioctl (sp->pop3->carrier, MU_IOCTL_GET_TRANSPORT_BUFFER,
&sp->oldbuf) == 0)
{
struct mu_buffer_query newbuf;
sp->flags |= _POP3F_CHBUF;
newbuf.type = MU_TRANSPORT_OUTPUT;
newbuf.buftype = mu_buffer_full;
newbuf.bufsize = 64*1024;
mu_stream_ioctl (sp->pop3->carrier, MU_IOCTL_SET_TRANSPORT_BUFFER,
&newbuf);
}
}
return rc;
}
static int
_mu_pop3_read (struct _mu_stream *str, char *buf, size_t bufsize,
size_t *pnread)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
mu_pop3_t pop3 = sp->pop3;
size_t nread;
int status = 0;
if (sp->flags & _POP3F_DONE)
nread = 0;
else
{
status = mu_stream_read (pop3->carrier, buf, bufsize, &nread);
if (status == 0 && nread == 0)
{
pop3->state = MU_POP3_NO_STATE;
sp->flags |= _POP3F_DONE;
}
}
*pnread = nread;
return status;
}
static int
_mu_pop3_readdelim (struct _mu_stream *str, char *buf, size_t bufsize,
int delim, size_t *pnread)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
mu_pop3_t pop3 = sp->pop3;
size_t nread;
int status = 0;
if (sp->flags & _POP3F_DONE)
nread = 0;
else
{
status = mu_stream_readdelim (pop3->carrier, buf, bufsize, delim,
&nread);
if (status == 0 && nread == 0)
{
pop3->state = MU_POP3_NO_STATE;
sp->flags |= _POP3F_DONE;
}
}
*pnread = nread;
return status;
}
static int
_mu_pop3_flush (struct _mu_stream *str)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
mu_pop3_t pop3 = sp->pop3;
return mu_stream_flush (pop3->carrier);
}
static int
_mu_pop3_wait (struct _mu_stream *str, int *pflags, struct timeval *tvp)
{
struct mu_pop3_stream *sp = (struct mu_pop3_stream *)str;
mu_pop3_t pop3 = sp->pop3;
return mu_stream_wait (pop3->carrier, pflags, tvp);
}
int
mu_pop3_stream_create (mu_pop3_t pop3, mu_stream_t *pstream)
{
struct mu_pop3_stream *sp;
mu_stream_t str;
int status;
sp = (struct mu_pop3_stream *) _mu_stream_create (sizeof (*sp),
MU_STREAM_READ);
if (!sp)
return ENOMEM;
sp->stream.read = _mu_pop3_read;
sp->stream.readdelim = _mu_pop3_readdelim;
sp->stream.flush = _mu_pop3_flush;
sp->stream.wait = _mu_pop3_wait;
sp->pop3 = pop3;
sp->flags = 0;
str = (mu_stream_t) sp;
mu_stream_set_buffer (str, mu_buffer_line, 1024);
status = mu_pop3_filter_create (pstream, str);
mu_stream_unref (str);
return status;
}