unixmbox.c
9.09 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General 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 General Public License for more details.
You should have received a copy of the GNU 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 "unixmbox.h"
/*
* Opens a mailbox
*/
int
unixmbox_open (mailbox * mbox)
{
char buf[80];
unsigned int max_count = 10;
int mess = 0;
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = malloc (sizeof (unixmbox_data));
if (data == NULL)
{
errno = ENOMEM;
free (data);
return -1;
}
mbox->_data = data;
data->file = fopen (mbox->name, "r+");
if (data->file == NULL)
{
/* errno is set by fopen() */
free (data);
return -1;
}
data->messages = malloc (sizeof (unixmbox_message) * max_count);
mbox->sizes = malloc (sizeof (int) * max_count);
if (data->messages == NULL || mbox->sizes == NULL)
{
unixmbox_close (mbox);
errno = ENOMEM;
return -1;
}
if (fgets (buf, 80, data->file) == NULL)
{
if (feof(data->file))
goto END; /* empty file, no messages */
unixmbox_close (mbox);
return -1;
}
if (strncmp (buf, "From ", 5))
{
/* This is NOT an mbox file */
unixmbox_close (mbox);
errno = EBADMSG; /* use this to signify wrong mbox type */
return -1;
}
do
{
if (!strncmp (buf, "From ", 5))
{
/* Beginning of a header */
while (strchr (buf, '\n') == NULL)
if (fgets (buf, 80, data->file) == NULL) /* eat the From line */
{
if (feof (data->file))
errno = EIO; /* corrupted mailbox? */
unixmbox_close (mbox);
return -1;
}
mbox->messages++;
if (mbox->messages > max_count)
{
max_count = mbox->messages * 2;
data->messages = realloc (data->messages,
max_count * sizeof (unixmbox_message));
mbox->sizes = realloc (mbox->sizes, max_count * sizeof (int));
if (data->messages == NULL || mbox->sizes == NULL)
{
unixmbox_close (mbox);
errno = ENOMEM;
return -1;
}
}
fgetpos (data->file, &(data->messages[mbox->messages - 1].header));
mbox->sizes[mbox->messages - 1] = 0;
data->messages[mbox->messages - 1].deleted = 0;
mess = 0;
}
else if (((!strncmp (buf, "\r\n", 2) || strlen (buf) <= 1)) && mess == 0)
{
/* Beginning of a body */
fgetpos (data->file, &(data->messages[mbox->messages - 1].body));
mbox->sizes[mbox->messages - 1] += strlen (buf) + 1;
mess = 1;
}
else
{
mbox->sizes[mbox->messages - 1] += strlen (buf) + 1;
fgetpos (data->file, &(data->messages[mbox->messages - 1].end));
}
}
while (fgets (buf, 80, data->file));
if (!feof (data->file)) /* stopped due to error, not EOF */
{
unixmbox_close (mbox);
return -1; /* errno is set */
}
END:
mbox->_close = unixmbox_close;
mbox->_delete = unixmbox_delete;
mbox->_undelete = unixmbox_undelete;
mbox->_expunge = unixmbox_expunge;
mbox->_add_message = unixmbox_add_message;
mbox->_is_deleted = unixmbox_is_deleted;
mbox->_lock = unixmbox_lock;
mbox->_get_body = unixmbox_get_body;
mbox->_get_header = unixmbox_get_header;
#ifdef TESTING
mbox->_tester = unixmbox_tester;
#endif
return 0;
}
/*
* Closes and unlocks a mailbox, does not expunge
*/
int
unixmbox_close (mailbox * mbox)
{
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
data = mbox->_data;
unixmbox_lock (mbox, MO_ULOCK);
if (data->file)
fclose (data->file);
free (data->messages);
free (mbox->sizes);
free (data);
return 0;
}
/*
* Marks a message for deletion
*/
int
unixmbox_delete (mailbox * mbox, unsigned int num)
{
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
if (num > mbox->messages || mbox_is_deleted (mbox, num))
{
errno = ERANGE;
return -1;
}
data = mbox->_data;
data->messages[num].deleted = 1;
mbox->num_deleted++;
return 0;
}
/*
* Unmark a message for deletion
*/
int
unixmbox_undelete (mailbox * mbox, unsigned int num)
{
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
if (num > mbox->messages || !mbox_is_deleted (mbox, num))
{
errno = ERANGE;
return -1;
}
data = mbox->_data;
data->messages[num].deleted = 0;
mbox->num_deleted--;
return 0;
}
/*
* Updates a mailbox to remove message marked for deletion
* FIXME: BROKEN! DOES NOT WORK! BLOWS UP REAL GOOD!
*/
int
unixmbox_expunge (mailbox * mbox)
{
unixmbox_data *data;
int i = 0;
size_t size = 0, size_read = 0;
char *buf = NULL;
int file;
int deletion_needed = 0; /* true when a deleted message has been found */
size_t buff_size = 0;
size_t tmp = 0;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
if (mbox->num_deleted)
{
data = mbox->_data;
fclose(data->file);
/* error handling */
data->file = NULL;
file = open(mbox->name, O_RDWR);
/* error handling */
for (i = 0; i < mbox->messages; i++)
{
if (data->messages[i].deleted == 0)
{
if (deletion_needed)
{
tmp = mbox->sizes[i];
if (tmp > buff_size)
{
buff_size = tmp;
buf = realloc (buf, tmp);
/* error checking */
}
lseek (file, data->messages[i].header, SEEK_SET);
size_read = read (file, buf, tmp);
/* error checking */
lseek (file, size, SEEK_SET);
write (file, buf, size_read);
/* error checking */
size += size_read;
}
else
{
size += mbox->sizes[i];
}
}
else
{
deletion_needed = 1;
}
}
close (file);
truncate (mbox->name, size);
free (buf);
}
return 0;
}
/*
* Determines whether or a not a message is marked for deletion
*/
int
unixmbox_is_deleted (mailbox * mbox, unsigned int num)
{
unixmbox_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
if (mbox->num_deleted == 0)
return 0;
data = mbox->_data;
return (data->messages[num].deleted == 1);
}
/*
* Adds a message to the mailbox
*/
int
unixmbox_add_message (mailbox * mbox, char *message)
{
/*
* FIXME: please write me
* move to end of file and write text, don't forget to update
* mbox->messages and mbox->_data->messages
*/
errno = ENOSYS;
return -1;
}
/*
* Returns a message body
*/
char *
unixmbox_get_body (mailbox * mbox, unsigned int num)
{
unixmbox_data *data;
size_t size;
char *buf;
if (mbox == NULL)
{
errno = EINVAL;
return NULL;
}
if (num > mbox->messages || num < 0)
{
errno = ERANGE;
return NULL;
}
data = mbox->_data;
size = data->messages[num].end - data->messages[num].body;
buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
{
errno = ENOMEM;
return NULL;
}
memset (buf, 0, size + 1);
if (fsetpos (data->file, &(data->messages[num].body)) == -1)
{
free (buf);
return NULL;
}
if (fread (buf, sizeof (char), size, data->file) < size)
{
free (buf);
return NULL;
}
return buf;
}
/*
* Returns just the header of a message
*/
char *
unixmbox_get_header (mailbox * mbox, unsigned int num)
{
unixmbox_data *data;
size_t size;
char *buf;
if ( mbox == NULL )
{
errno = EINVAL;
return NULL;
}
if (num > mbox->messages)
{
errno = ERANGE;
return NULL;
}
data = mbox->_data;
size = (data->messages[num].body - 1) - data->messages[num].header;
buf = malloc ((1 + size) * sizeof (char));
if (buf == NULL)
{
errno = ENOMEM;
return NULL;
}
memset (buf, 0, size + 1);
if (fsetpos (data->file, &(data->messages[num].header)) == -1)
{
free (buf);
return NULL;
}
if (fread (buf, sizeof (char), size, data->file) < size)
{
free (buf);
return NULL;
}
return buf;
}
/*
* Sets lock mode for a mailbox
* FIXME: this doesn't do anything, really
* Get locking code from Procmail and/or Exim
*/
int
unixmbox_lock (mailbox *mbox, mailbox_lock_t mode)
{
unixmbox_data *data = mbox->_data;
data->lockmode = mode;
return 0;
}
#ifdef TESTING
void unixmbox_tester (mailbox *mbox, unsigned int num)
{
unixmbox_data *data = mbox->_data;
if (!data || num > mbox->messages || num < 0)
return;
printf ("Message size: %u\n", mbox->sizes[num]);
printf ("Message length: %lu\n",
data->messages[num].end - data->messages[num].header);
}
#endif