mh.c
6.46 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
/* 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 "mh.h"
/*
* Opens a mailbox
*/
int
mh_open (mailbox * mbox)
{
int i;
char old_dir[PATH_MAX];
struct dirent *entry;
mh_data *data;
if(mbox == NULL)
return EINVAL;
if(getcwd (old_dir, PATH_MAX) == NULL)
old_dir[0] = '\0';
chdir (mbox->name);
data = malloc (sizeof (mh_data));
data->dir = opendir (mbox->name);
if(data->dir == NULL)
return errno; /* set by opendir() */
/* process directory */
while (entry = readdir (data->dir))
{
unsigned long seq_num;
char *foo = NULL;
if (entry->d_name[0] == '.')
{
if (strcmp(entry->d_name, ".mh_sequences") == 0)
/* TODO: deal with mh sequence files */;
continue;
}
if (entry->d_name[0] == ',')
/* file marked for deletion */;
/* TODO: handle ERANGE */
seq_num = strtoul (entry->d_name, &foo, 10);
if (*foo != '\0') /* invalid sequence number */
continue; /* TODO: handle this better? */
printf ("message: %ld\r", seq_num);
mbox->messages++;
}
mbox->_data = data;
#if 0
mbox->_close = mh_close;
mbox->_delete = mh_delete;
mbox->_undelete = mh_undelete;
mbox->_expunge = mh_expunge;
mbox->_add_message = mh_add_message;
mbox->_is_deleted = mh_is_deleted;
mbox->_lock = mh_lock;
mbox->_get_body = mh_get_body;
mbox->_get_header = mh_get_header;
#endif
chdir(old_dir);
fprintf(stderr, "%d messages read\n", mbox->messages);
return 0;
}
#if 0
/*
* Closes and unlocks a mailbox, does not expunge
*/
int
mh_close (mailbox * mbox)
{
/* FIXME: hack from unixmbox */
return 0;
}
/*
* Marks a message for deletion
*/
int
mh_delete (mailbox * mbox, unsigned int num)
{
mh_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
mh_undelete (mailbox * mbox, unsigned int num)
{
mh_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
*/
int
mh_expunge (mailbox * mbox)
{
mh_data *data;
int i = 0;
char old_dir[PATH_MAX];
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
getcwd (old_dir, PATH_MAX);
chdir (mbox->name);
if (mbox->num_deleted)
{
data = mbox->_data;
for (i = 0; i < mbox->messages; i++)
{
if (data->messages[i].deleted == 0)
{
unlink (data->messages[i].file);
mbox->num_deleted--;
}
}
}
/* reorder messages? */
chdir (old_dir);
return 0;
}
/*
* Determines whether or a not a message is marked for deletion
*/
int
mh_is_deleted (mailbox * mbox, unsigned int num)
{
mh_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);
}
int
mh_is_updated (mailbox *mbox)
{
struct stat st;
mh_data *data;
if (mbox == NULL)
{
errno = EINVAL;
return -1;
}
if (stat (mbox->name, &st) == -1)
return -1;
data = mbox->_data;
return (st.st_mtime > data->last_mod_time);
}
/*
* Adds a message to the mailbox
*/
int
mh_add_message (mailbox * mbox, char *message)
{
/*
* FIXME: please write me
* http://www.qmail.org/man/man5/mh.html
*/
errno = ENOSYS;
return -1;
}
/*
* Returns a message body
*/
char *
mh_get_body (mailbox * mbox, unsigned int num)
{
mh_data *data;
char *buf;
char old_dir[PATH_MAX];
FILE *f;
struct stat st;
size_t size;
if (mbox == NULL)
{
errno = EINVAL;
return NULL;
}
if (num > mbox->messages || num < 0)
{
errno = ERANGE;
return NULL;
}
data = mbox->_data;
getcwd (old_dir, PATH_MAX);
chdir (mbox->name);
if (data->messages[num].location == cur)
chdir ("cur");
else if (data->messages[num].location == tmp)
chdir ("tmp");
else if (data->messages[num].location == new)
chdir ("new");
f = fopen (data->messages[num].file, "r");
if (f == NULL)
{
chdir (old_dir);
perror (NULL);
return NULL;
}
stat (data->messages[num].file, &st);
size = st.st_size - data->messages[num].body + 1;
buf = malloc (sizeof (char) * size);
if (buf == NULL)
{
fclose (f);
chdir (old_dir);
errno = ENOMEM;
return NULL;
}
memset (buf, 0, size);
if (fsetpos (f, &(data->messages[num].body)) == -1)
{
fclose (f);
chdir (old_dir);
free (buf);
return NULL;
}
if (fread (buf, sizeof (char), size-1, f) < size)
{
fclose (f);
chdir (old_dir);
free (buf);
return NULL;
}
fclose (f);
chdir (old_dir);
return buf;
}
/*
* Returns just the header of a message
*/
char *
mh_get_header (mailbox * mbox, unsigned int num)
{
mh_data *data;
char *buf;
if ( mbox == NULL )
{
errno = EINVAL;
return NULL;
}
if (num > mbox->messages)
{
errno = ERANGE;
return NULL;
}
data = mbox->_data;
buf = NULL; /* FIXME: read the file until data->message[num].body */
return buf;
}
/*
* Sets lock mode for a mailbox
* FIXME: What to do for Mh? no locking?
*/
int
mh_lock (mailbox *mbox, mailbox_lock_t mode)
{
return 0;
}
/* FIXME: not implemented */
int
mh_scan (mailbox *mbox)
{
errno = ENOSYS;
return -1;
}
#endif