message.c
7.83 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000 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. */
#include <header0.h>
#include <attribute0.h>
#include <message0.h>
#include <mailbox0.h>
#include <io0.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
static int message_read (istream_t is, char *buf, size_t buflen,
off_t off, ssize_t *pnread );
static int message_write (ostream_t os, const char *buf, size_t buflen,
off_t off, ssize_t *pnwrite);
int message_clone (message_t omsg, message_t *pmsg)
{
int status;
FILE *file;
message_t msg;
istream_t is;
ostream_t os;
header_t header;
char buffer[BUFSIZ];
char *pbuf = NULL;
char *tbuf = NULL;
off_t offset = 0;
size_t nread = 0;
/* retreive the header */
{
status = message_get_header (omsg, &header);
if (status != 0)
return status;
status = header_get_istream (header, &is);
if (status != 0)
return status;
do {
status = istream_read (is, buffer, sizeof (buffer), offset, &nread);
if (status != 0)
{
free (pbuf);
return status;
}
tbuf = realloc (pbuf, offset + nread);
if (tbuf == NULL)
{
free (pbuf);
return ENOMEM;
}
else
pbuf = tbuf;
memcpy (pbuf + offset, buffer, nread);
offset += nread;
} while (nread > 0);
status = header_init (&header, pbuf, offset, MU_HEADER_RFC822, NULL);
if (status != 0)
{
free (pbuf);
return status;
}
free (pbuf);
}
/* retrieve the body */
{
file = tmpfile ();
if (file == NULL)
{
header_destroy (&header, NULL);
return errno;
}
offset = 0;
message_get_istream (omsg, &is);
do {
istream_read (is, buffer, sizeof (buffer), offset, &nread);
fwrite (buffer, sizeof (*buffer), nread, file);
offset += nread;
} while (nread > 0);
rewind (file);
}
/* create the message */
status = message_init (&msg, NULL);
if (status != 0)
return status;
/* set the header */
message_set_header (msg, header, NULL);
/* set the body with the streams */
msg->body = calloc (1, sizeof (*(msg->body)));
if (msg->body == NULL)
{
fclose (file);
message_destroy (&msg, NULL);
return ENOMEM;
}
status = istream_init (&is, message_read, NULL);
if (status != 0)
{
message_destroy (&msg, NULL);
return status;
}
status = ostream_init (&os, message_write, NULL);
if (status != 0)
{
message_destroy (&msg, NULL);
return status;
}
*pmsg = msg;
return 0;
}
int
message_init (message_t *pmsg, void *owner)
{
message_t msg;
if (pmsg == NULL)
return EINVAL;
msg = calloc (1, sizeof (*msg));
if (msg == NULL)
return ENOMEM;
msg->owner = owner;
*pmsg = msg;
return 0;
}
void
message_destroy (message_t *pmsg, void *owner)
{
if (pmsg && *pmsg)
{
message_t msg = *pmsg;
msg->ref_count--;
if ((msg->owner && msg->owner != owner) ||
(msg->owner == NULL && msg->ref_count <= 0))
{
/* header */
header_destroy (&(msg->header), owner);
/* attribute */
attribute_destroy (&(msg->attribute), owner);
/* istream */
istream_destroy (&(msg->is), owner);
/* ostream */
ostream_destroy (&(msg->os), owner);
/* if sometype of floating/temporary message */
if (msg->body)
{
body_t body = msg->body;
if (body->file)
fclose (body->file);
free (body->content);
free (msg->body);
}
free (msg);
}
/* loose the link */
*pmsg = NULL;
}
}
int
message_get_header (message_t msg, header_t *phdr)
{
if (phdr == NULL || msg == NULL)
return EINVAL;
*phdr = msg->header;
return 0;
}
int
message_set_header (message_t msg, header_t hdr, void *owner)
{
if (msg == NULL )
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->header = hdr;
return 0;
}
int
message_get_istream (message_t msg, istream_t *pis)
{
if (msg == NULL || pis == NULL)
return EINVAL;
*pis = msg->is;
return 0;
}
int
message_set_istream (message_t msg, istream_t is, void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->is = is;
return 0;
}
int
message_get_ostream (message_t msg, ostream_t *pos)
{
if (msg == NULL || pos == NULL)
return EINVAL;
*pos = msg->os;
return 0;
}
int
message_set_ostream (message_t msg, ostream_t os, void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->os = os;
return 0;
}
int
message_size (message_t msg, size_t *size)
{
if (msg == NULL)
return EINVAL;
if (msg->_size)
return msg->_size (msg, size);
return ENOSYS;
}
int
message_get_attribute (message_t msg, attribute_t *pattribute)
{
if (msg == NULL || pattribute == NULL)
return EINVAL;
*pattribute = msg->attribute;
return 0;
}
int
message_set_attribute (message_t msg, attribute_t attribute, void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->attribute = attribute;
return 0;
}
static int
message_read (istream_t is, char *buf, size_t buflen,
off_t off, ssize_t *pnread )
{
message_t msg = NULL;
ssize_t nread = 0;
if (is == NULL)
return EINVAL;
if (msg->body)
{
body_t body = msg->body;
if (body->file)
{
/* we're not checking the error of fseek for some handlers
* like socket in those not make sense.
* FIXME: Alternative is to check fseeck and errno == EBADF
* if not a seekable stream.
*/
fseek (body->file, off, SEEK_SET);
nread = fread (buf, sizeof (char), buflen, body->file);
if (nread == 0)
{
if (ferror (body->file))
return errno;
/* clear the error for feof() */
clearerr (body->file);
}
/* errno set by fread()/fseek() ? */
}
else if (body->content)
{
off_t ln = body->content_len - off;
if (ln > 0)
{
nread = ((size_t)ln < buflen) ? ln : buflen;
memcpy (buf, body->content + off, nread);
}
else
nread = 0;
errno = 0;
}
else
{
return EINVAL;
}
}
if (pnread)
*pnread = nread;
return 0;
}
static int
message_write (ostream_t os, const char *buf, size_t buflen,
off_t off, ssize_t *pnwrite)
{
message_t msg = NULL;
ssize_t nwrite = 0;
if (os == NULL)
return EINVAL;
if (msg->body)
{
body_t body = msg->body;
if (body->file)
{
/* we're not checking the error of fseek for some handlers
* like socket in those not make sense.
* FIXME: Alternative is to check fseeck and errno == EBADF
* if not a seekable stream.
*/
fseek (body->file, off, SEEK_SET);
nwrite = fwrite (buf, sizeof (char), buflen, body->file);
if (nwrite == 0)
{
if (ferror (body->file))
return errno;
/* clear the error for feof() */
clearerr (body->file);
}
/* errno set by fread()/fseek() ? */
}
else if (body->content)
{
off_t ln = body->content_len - off;
if (ln > 0)
{
nwrite = ((size_t)ln < buflen) ? ln : buflen;
memcpy (body->content + off, buf, nwrite);
}
else
nwrite = 0;
}
else
{
return EINVAL;
}
}
if (pnwrite)
*pnwrite = nwrite;
return 0;
}