pop3_readline.c
5.17 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) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program 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 Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <mailutils/sys/pop3.h>
#include <mailutils/error.h>
/* Read a complete line from the pop server. Transform CRLF to LF, remove
the stuff byte termination octet ".", put a null in the buffer
when done. */
static int
pop3_getline (pop3_t pop3)
{
size_t n = 0;
size_t total = pop3->io.ptr - pop3->io.buf;
int status = 0;
/* Must get a full line before bailing out. */
do
{
/* Timeout with select(), note that we have to reset select()
since on linux tv is modified when error. */
if (pop3->timeout)
{
/* Heavy hand but continue if select trip on a signal. */
for (;;)
{
int fd = -1;
struct timeval tv;
fd_set rfds;
status = stream_get_fd (pop3->stream, &fd);
if (status != 0)
return status;
if (fd == -1)
return MU_ERROR_IO;
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = pop3->timeout;
tv.tv_usec = 0;
status = select (fd + 1, &rfds, NULL, NULL, &tv);
if (status == 0)
return MU_ERROR_TIMEOUT;
else if (status == -1)
{
if (errno == EINTR)
continue;
return MU_ERROR_IO;
}
break;
}
}
status = stream_readline (pop3->stream, pop3->io.buf + total,
pop3->io.len - total, &n);
if (status != 0)
return status;
/* The server went away: It maybe a timeout and some pop server
does not send the -ERR. Consider this like an error. */
if (n == 0)
return MU_ERROR_IO;
total += n;
pop3->io.nl = memchr (pop3->io.buf, '\n', total);
if (pop3->io.nl == NULL) /* Do we have a full line. */
{
/* Allocate a bigger buffer ? */
if (total >= pop3->io.len -1)
{
pop3->io.len *= 2;
pop3->io.buf = realloc (pop3->io.buf, pop3->io.len + 1);
if (pop3->io.buf == NULL)
return MU_ERROR_NO_MEMORY;
}
}
pop3->io.ptr = pop3->io.buf + total;
}
while (pop3->io.nl == NULL); /* Bail only if we have a complete line. */
/* When examining a multi-line response, the client checks to see if the
line begins with the termination octet "."(DOT). If yes and if octets
other than CRLF follow, the first octet of the line (the termination
octet) is stripped away. */
if (total >= 3 && pop3->io.buf[0] == '.')
{
if (pop3->io.buf[1] != '\r' && pop3->io.buf[2] != '\n')
{
memmove (pop3->io.buf, pop3->io.buf + 1, total - 1);
pop3->io.ptr--;
pop3->io.nl--;
}
/* And if CRLF immediately follows the termination character, then
the response from the POP server is ended and the line containing
".CRLF" is not considered part of the multi-line response. */
else if (pop3->io.buf[1] == '\r' && pop3->io.buf[2] == '\n')
{
pop3->io.buf[0] = '\0';
pop3->io.ptr = pop3->io.buf;
pop3->io.nl = NULL;
}
}
/* \r\n --> \n\0, conversion. */
if (pop3->io.nl > pop3->io.buf)
{
*(pop3->io.nl - 1) = '\n';
*(pop3->io.nl) = '\0';
pop3->io.ptr = pop3->io.nl;
}
return status;
} /* if need to fill up. */
int
pop3_readline (pop3_t pop3, char *buffer, size_t buflen, size_t *pnread)
{
size_t nread = 0;
size_t n = 0;
int status;
/* Do we need to fill up? Yes if no NL or the buffer is empty. */
if (pop3->stream && (pop3->io.nl == NULL || pop3->io.ptr == pop3->io.buf))
{
status = pop3_getline (pop3);
if (status != 0)
return status;
}
/* How much we can copy ? */
n = pop3->io.ptr - pop3->io.buf;
/* Consume the line? */
if (buffer && buflen)
{
buflen--; /* For the null. */
if (buflen)
{
int nleft = buflen - n;
/* We got more then requested. */
if (nleft < 0)
{
size_t sentinel;
nread = buflen;
sentinel = pop3->io.ptr - (pop3->io.buf + nread);
memcpy (buffer, pop3->io.buf, nread);
memmove (pop3->io.buf, pop3->io.buf + nread, sentinel);
pop3->io.ptr = pop3->io.buf + sentinel;
}
else
{
/* Drain our buffer. */;
nread = n;
memcpy (buffer, pop3->io.buf, nread);
pop3->io.ptr = pop3->io.buf;
/* Clear of all residue. */
memset (pop3->io.buf, '\0', pop3->io.len);
}
}
buffer[nread] = '\0';
}
else
nread = n;
if (pnread)
*pnread = nread;
return status;
}