pop3_stream.c
7.73 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 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 2 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/pop3.h>
/* Implementation of the stream for TOP and RETR. */
static int p_ref __P ((stream_t));
static void p_destroy __P ((stream_t *));
static int p_open __P ((stream_t, const char *, int, int));
static int p_close __P ((stream_t));
static int p_read __P ((stream_t, void *, size_t, off_t, size_t *));
static int p_readline __P ((stream_t, char *, size_t, off_t, size_t *));
static int p_write __P ((stream_t, const void *, size_t, off_t, size_t *));
static int p_tell __P ((stream_t, off_t *));
static int p_get_size __P ((stream_t, off_t *));
static int p_truncate __P ((stream_t, off_t));
static int p_flush __P ((stream_t));
static int p_get_fd __P ((stream_t, int *));
static int p_get_flags __P ((stream_t, int *));
static int p_get_state __P ((stream_t, enum stream_state *));
static int p_is_seekable __P ((stream_t));
static int p_is_readready __P ((stream_t, int timeout));
static int p_is_writeready __P ((stream_t, int timeout));
static int p_is_exceptionpending __P ((stream_t, int timeout));
static int p_is_open __P ((stream_t));
static struct _stream_vtable p_s_vtable =
{
p_ref,
p_destroy,
p_open,
p_close,
p_read,
p_readline,
p_write,
p_tell,
p_get_size,
p_truncate,
p_flush,
p_get_fd,
p_get_flags,
p_get_state,
p_is_seekable,
p_is_readready,
p_is_writeready,
p_is_exceptionpending,
p_is_open
};
int
pop3_stream_create (pop3_t pop3, stream_t *pstream)
{
struct p_stream *p_stream;
p_stream = malloc (sizeof *p_stream);
if (p_stream == NULL)
return MU_ERROR_NO_MEMORY;
mu_refcount_create (&p_stream->refcount);
if (p_stream->refcount == NULL)
{
free (p_stream);
return MU_ERROR_NO_MEMORY;
}
p_stream->done = 0;
p_stream->pop3 = pop3;
p_stream->base.vtable = &p_s_vtable;
*pstream = &p_stream->base;
return 0;
}
static int
p_ref (stream_t stream)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return mu_refcount_inc (p_stream->refcount);
}
static void
p_destroy (stream_t *pstream)
{
struct p_stream *p_stream = (struct p_stream *)*pstream;
if (mu_refcount_dec (p_stream->refcount) == 0)
{
if (!p_stream->done)
{
char buf[128];
size_t n = 0;
while (pop3_readline (p_stream->pop3, buf, sizeof buf, &n) > 0
&& n > 0)
n = 0;
}
p_stream->pop3->state = POP3_NO_STATE;
mu_refcount_destroy (&p_stream->refcount);
free (p_stream);
}
}
static int
p_open (stream_t stream, const char *h, int p, int f)
{
struct p_stream *p_stream = (struct p_stream *)stream;
(void)f;
return pop3_connect (p_stream->pop3, h, p);
}
static int
p_close (stream_t stream)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return pop3_disconnect (p_stream->pop3);
}
static int
p_read (stream_t stream, void *buf, size_t buflen, off_t offset, size_t *pn)
{
struct p_stream *p_stream = (struct p_stream *)stream;
size_t n = 0;
int status = 0;
char *p = buf;
(void)offset;
if (p_stream)
{
mu_refcount_lock (p_stream->refcount);
if (!p_stream->done)
{
do
{
size_t nread = 0;
/* The pop3_readline () function will always read one less to
be able to null terminate the buffer, this will cause
serious grief for stream_read() where it is legitimate to
have a buffer of 1 char. So we must catch it here. */
if (buflen == 1)
{
char buffer[2];
*buffer = '\0';
status = pop3_readline (p_stream->pop3, buffer, 2, &nread);
*p = *buffer;
}
else
status = pop3_readline (p_stream->pop3, p, buflen, &nread);
if (status != 0)
break;
if (nread == 0)
{
p_stream->pop3->state = POP3_NO_STATE;
p_stream->done = 1;
break;
}
n += nread;
buflen -= nread;
p += nread;
}
while (buflen > 0);
}
mu_refcount_unlock (p_stream->refcount);
}
if (pn)
*pn = n;
return status;
}
static int
p_readline (stream_t stream, char *buf, size_t buflen, off_t offset,
size_t *pn)
{
struct p_stream *p_stream = (struct p_stream *)stream;
size_t n = 0;
int status = 0;
(void)offset;
if (p_stream)
{
mu_refcount_lock (p_stream->refcount);
if (!p_stream->done)
{
status = pop3_readline (p_stream->pop3, buf, buflen, &n);
if (n == 0)
{
p_stream->pop3->state = POP3_NO_STATE;
p_stream->done = 1;
}
}
mu_refcount_unlock (p_stream->refcount);
}
if (pn)
*pn = n;
return status;
}
static int
p_write (stream_t stream, const void *buf, size_t buflen, off_t offset,
size_t *pn)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_write (p_stream->pop3->carrier, buf, buflen, offset, pn);
}
static int
p_is_seekable (stream_t stream)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_is_seekable (p_stream->pop3->carrier);
}
static int
p_tell (stream_t stream, off_t *offset)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_tell (p_stream->pop3->carrier, offset);
}
static int
p_get_size (stream_t stream, off_t *size)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_get_size (p_stream->pop3->carrier, size);
}
static int
p_truncate (stream_t stream, off_t size)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_truncate (p_stream->pop3->carrier, size);
}
static int
p_flush (stream_t stream)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_flush (p_stream->pop3->carrier);
}
static int
p_get_fd (stream_t stream, int *fd)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_get_fd (p_stream->pop3->carrier, fd);
}
static int
p_get_flags (stream_t stream, int *flags)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_get_flags (p_stream->pop3->carrier, flags);
}
static int
p_get_state (stream_t stream, enum stream_state *state)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_get_state (p_stream->pop3->carrier, state);
}
static int
p_is_readready (stream_t stream, int timeout)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_is_readready (p_stream->pop3->carrier, timeout);
}
static int
p_is_writeready (stream_t stream, int timeout)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_is_writeready (p_stream->pop3->carrier, timeout);
}
static int
p_is_exceptionpending (stream_t stream, int timeout)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_is_exceptionpending (p_stream->pop3->carrier, timeout);
}
static int
p_is_open (stream_t stream)
{
struct p_stream *p_stream = (struct p_stream *)stream;
return stream_is_open (p_stream->pop3->carrier);
}