mailbox.c
14.3 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/* 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. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mbx_mbox.h>
#include <mbx_unix.h>
#include <mbx_mdir.h>
#include <mbx_mmdf.h>
#include <mbx_pop.h>
#include <mbx_imap.h>
#include <stdlib.h>
#include <errno.h>
/* forward prototypes */
static int mbx_open (mailbox_t, int flag);
static int mbx_close (mailbox_t);
/* name */
static int mbx_get_name (mailbox_t, int *id, char *name,
size_t len, size_t *n);
static int mbx_get_mname (mailbox_t mbox, int *id, char **name, size_t *n);
/* passwd */
static int mbx_get_passwd (mailbox_t, char *passwd, size_t, size_t *);
static int mbx_get_mpasswd (mailbox_t, char **passwd, size_t *len);
static int mbx_set_passwd (mailbox_t, const char *passwd, size_t len);
/* deleting */
static int mbx_delete (mailbox_t, size_t msgno);
static int mbx_undelete (mailbox_t, size_t msgno);
static int mbx_expunge (mailbox_t);
static int mbx_is_deleted (mailbox_t, size_t msgno);
/* appending */
static int mbx_new_msg (mailbox_t, size_t *msgno);
static int mbx_set_header (mailbox_t, size_t msgno, const char *h,
size_t n, int replace);
static int mbx_set_body (mailbox_t, size_t msgno, const char *b,
size_t n, int replace);
static int mbx_append (mailbox_t, size_t msgno);
static int mbx_destroy_msg (mailbox_t, size_t msgno);
/* reading */
static int mbx_get_body (mailbox_t, size_t msgno, off_t off,
char *b, size_t len, size_t *n);
static int mbx_get_mbody (mailbox_t, size_t msgno, off_t off,
char **b, size_t *n);
static int mbx_get_header (mailbox_t, size_t msgno, off_t off,
char *h, size_t len, size_t *n);
static int mbx_get_mheader (mailbox_t, size_t msgno, off_t off,
char **h, size_t *n);
/* locking */
static int mbx_lock (mailbox_t, int flag);
static int mbx_unlock (mailbox_t);
//static int mbx_ilock (mailbox_t, int flag);
//static int mbx_iunlock (mailbox_t);
/* owner and group */
static int mbx_set_owner (mailbox_t, uid_t uid);
static int mbx_get_owner (mailbox_t, uid_t *uid);
static int mbx_set_group (mailbox_t, uid_t gid);
static int mbx_get_group (mailbox_t, uid_t *gid);
/* misc */
static int mbx_scan (mailbox_t, size_t *msgs);
static int mbx_is_updated (mailbox_t);
static int mbx_get_timeout (mailbox_t, size_t *timeout);
static int mbx_set_timeout (mailbox_t, size_t timeout);
static int mbx_get_refresh (mailbox_t, size_t *refresh);
static int mbx_set_refresh (mailbox_t, size_t refresh);
static int mbx_get_size (mailbox_t, size_t msgno, size_t *sh, size_t *sb);
static int mbx_set_notification (mailbox_t,
int (*func) (mailbox_t, void *arg));
/* init all the functions to a default value */
static void mbx_check_struct (mailbox_t);
/*
Builtin mailbox types.
A circular list is use for the builtin.
Proper locking is not done when accessing the list.
FIXME: not thread-safe. */
static struct mailbox_builtin
{
struct mailbox_type *mtype;
int is_malloc;
struct mailbox_builtin * next;
} mailbox_builtin [] = {
{ NULL, 0, &mailbox_builtin[1] }, /* sentinel, head list */
{ &_mailbox_mbox_type, 0, &mailbox_builtin[2] },
{ &_mailbox_unix_type, 0, &mailbox_builtin[3] },
{ &_mailbox_maildir_type, 0, &mailbox_builtin[4] },
{ &_mailbox_mmdf_type, 0, &mailbox_builtin[5] },
{ &_mailbox_pop_type, 0, &mailbox_builtin[6] },
{ &_mailbox_imap_type, 0, &mailbox_builtin[0] },
};
int
mailbox_add_type (struct mailbox_type *mtype)
{
struct mailbox_builtin *current = malloc (sizeof (*current));
if (current == NULL)
return ENOMEM;
if (mtype->utype)
{
int status = url_add_type (mtype->utype);
if (status != 0)
{
free (current);
return status;
}
mtype->id = mtype->utype->id; /* same ID as the url_type */
}
else
{
mtype->id = (int)mtype; /* just need to be uniq */
}
current->mtype = mtype;
current->is_malloc = 1;
current->next = mailbox_builtin->next;
mailbox_builtin->next = current;
return 0;
}
int
mailbox_remove_type (struct mailbox_type *mtype)
{
struct mailbox_builtin *current, *previous;
for (previous = mailbox_builtin, current = mailbox_builtin->next;
current != mailbox_builtin;
previous = current, current = current->next)
{
if (current->mtype == mtype)
{
previous->next = current->next;
if (current->is_malloc)
free (current);
return 0;;
}
}
return EINVAL;
}
int
mailbox_list_type (struct mailbox_type *list, size_t len, size_t *n)
{
struct mailbox_builtin *current;
size_t i;
for (i = 0, current = mailbox_builtin->next; current != mailbox_builtin;
current = current->next, i++)
{
if (list)
if (i < len)
list[i] = *(current->mtype);
}
if (n)
*n = i;
return 0;
}
int
mailbox_list_mtype (struct mailbox_type **mlist, size_t *n)
{
struct mailbox_type *mtype;
size_t i = 0;
mailbox_list_type (NULL, 0, &i);
if (i == 0)
{
if (n)
{
*n = i;
}
return 0;
}
mtype = calloc (i, sizeof (*mtype));
if (mtype == NULL)
{
return ENOMEM;
}
*mlist = mtype;
return *n = mailbox_list_type (mtype, i, n);
}
int
mailbox_get_type (struct mailbox_type **mtype, int id)
{
struct mailbox_builtin *current;
for (current = mailbox_builtin->next; current != mailbox_builtin;
current = current->next)
{
if (current->mtype->id == id)
{
*mtype = current->mtype;
return 0;;
}
}
return EINVAL;
}
int
mailbox_init (mailbox_t *mbox, const char *name, int id)
{
int status = EINVAL;
/* 1st run: if they know what they want, shortcut */
if (id)
{
struct mailbox_type *mtype;
status = mailbox_get_type (&mtype, id);
if (status == 0)
{
status = mtype->_init (mbox, name);
if (status == 0)
mbx_check_struct (*mbox);
}
}
/* 2nd run: try heuristic URL discovery */
if (status != 0)
{
url_t url;
status = url_init (&url, name);
if (status == 0)
{
int id;
struct mailbox_type *mtype;
/* We've found a match get it */
if (url_get_id (url, &id) == 0
&& (status = mailbox_get_type (&mtype, id)) == 0)
{
status = mtype->_init (mbox, name);
if (status == 0)
mbx_check_struct (*mbox);
}
url_destroy (&url);
}
}
/* 3nd run: nothing yet ?? try mbox directly as the last resort.
this should take care of the case where the filename is use */
if (status != 0 )
{
status = mailbox_mbox_init (mbox, name);
if (status == 0)
mbx_check_struct (*mbox);
}
return status;
}
void
mailbox_destroy (mailbox_t *mbox)
{
struct mailbox_type *mtype = (*mbox)->mtype;
return mtype->_destroy (mbox);
}
/* -------------- stub functions ------------------- */
static void
mbx_check_struct (mailbox_t mbox)
{
if (mbox->_open == NULL)
mbox->_open = mbx_open;
if (mbox->_close == NULL)
mbox->_close = mbx_close;
if (mbox->_get_name == NULL)
mbox->_get_name = mbx_get_name;
if (mbox->_get_mname == NULL)
mbox->_get_mname = mbx_get_mname;
if (mbox->_get_passwd == NULL)
mbox->_get_passwd = mbx_get_passwd;
if (mbox->_get_mpasswd == NULL)
mbox->_get_mpasswd = mbx_get_mpasswd;
if (mbox->_set_passwd == NULL)
mbox->_set_passwd = mbx_set_passwd;
if (mbox->_delete == NULL)
mbox->_delete = mbx_delete;
if (mbox->_undelete == NULL)
mbox->_undelete = mbx_undelete;
if (mbox->_expunge == NULL)
mbox->_expunge = mbx_expunge;
if (mbox->_is_deleted == NULL)
mbox->_is_deleted = mbx_is_deleted;
if (mbox->_new_msg == NULL)
mbox->_new_msg = mbx_new_msg;
if (mbox->_set_header == NULL)
mbox->_set_header = mbx_set_header;
if (mbox->_set_body == NULL)
mbox->_set_body = mbx_set_body;
if (mbox->_append == NULL)
mbox->_append = mbx_append;
if (mbox->_destroy_msg == NULL)
mbox->_destroy_msg = mbx_destroy_msg;
if (mbox->_get_body == NULL)
mbox->_get_body = mbx_get_body;
if (mbox->_get_mbody == NULL)
mbox->_get_mbody = mbx_get_mbody;
if (mbox->_get_header == NULL)
mbox->_get_header = mbx_get_header;
if (mbox->_get_mheader == NULL)
mbox->_get_mheader = mbx_get_mheader;
if (mbox->_lock == NULL)
mbox->_lock = mbx_lock;
if (mbox->_unlock == NULL)
mbox->_unlock = mbx_unlock;
if (mbox->_ilock == NULL)
mbox->_ilock = mbx_lock;
if (mbox->_iunlock == NULL)
mbox->_iunlock = mbx_unlock;
if (mbox->_scan == NULL)
mbox->_scan = mbx_scan;
if (mbox->_is_updated == NULL)
mbox->_is_updated = mbx_is_updated;
if (mbox->_set_owner == NULL)
mbox->_set_owner = mbx_set_owner;
if (mbox->_get_owner == NULL)
mbox->_get_owner = mbx_get_owner;
if (mbox->_set_group == NULL)
mbox->_set_group = mbx_set_group;
if (mbox->_get_group == NULL)
mbox->_get_group = mbx_get_group;
if (mbox->_get_timeout == NULL)
mbox->_get_timeout = mbx_get_timeout;
if (mbox->_set_timeout == NULL)
mbox->_set_timeout = mbx_set_timeout;
if (mbox->_get_refresh == NULL)
mbox->_get_refresh = mbx_get_refresh;
if (mbox->_set_refresh == NULL)
mbox->_set_refresh = mbx_set_refresh;
if (mbox->_get_size == NULL)
mbox->_get_size = mbx_get_size;
if (mbox->_set_notification == NULL)
mbox->_set_notification = mbx_set_notification;
}
static int
mbx_open (mailbox_t mbox, int flag)
{
return ENOSYS;
}
static int
mbx_close (mailbox_t mbox)
{
return ENOSYS;
}
/* name */
static int
mbx_get_name (mailbox_t mbox, int *id, char *name, size_t len, size_t *n)
{
char *s;
size_t i;
if (mbox == NULL || mbox->mtype == NULL)
{
return EINVAL;
}
s = mbox->mtype->name;
i = strlen (s);
if (id)
{
*id = mbox->mtype->id;
}
if (name && len > 0)
{
i = (len < i) ? len : i;
strncpy (name, s, i - 1);
name [i - 1] = 0;
}
if (n)
{
*n = i;
}
return 0;
}
static int
mbx_get_mname (mailbox_t mbox, int *id, char **name, size_t *n)
{
size_t i = 0;
mbox->_get_name (mbox, id, 0, 0, &i);
i++;
*name = calloc (i, sizeof (char));
if (*name == NULL)
{
return ENOMEM;
}
mbox->_get_name (mbox, id, *name, i, n);
return 0;
}
/* passwd */
static int
mbx_get_passwd (mailbox_t mbox, char *passwd, size_t len, size_t *n)
{
return ENOSYS;
}
static int
mbx_get_mpasswd (mailbox_t mbox, char **passwd, size_t *n)
{
size_t i = 0;
mbox->_get_passwd (mbox, NULL, 0, &i);
i++;
*passwd = calloc (i, sizeof (char));
if (*passwd == NULL)
{
return ENOMEM;
}
mbox->_get_passwd (mbox, *passwd, i, n);
return 0;
}
static int
mbx_set_passwd (mailbox_t mbox, const char *passwd, size_t len)
{
return ENOSYS;
}
/* deleting */
static int
mbx_delete (mailbox_t mbox, size_t msgno)
{
return ENOSYS;
}
static int
mbx_undelete (mailbox_t mbox, size_t msgno)
{
return ENOSYS;
}
static int
mbx_expunge (mailbox_t mbox)
{
return ENOSYS;
}
static int
mbx_is_deleted (mailbox_t mbox, size_t msgno)
{
return ENOSYS;
}
/* appending */
static int
mbx_new_msg (mailbox_t mbox, size_t *msgno)
{
return ENOSYS;
}
static int
mbx_set_header (mailbox_t mbox, size_t msgno, const char *h,
size_t len, int replace)
{
return ENOSYS;
}
static int
mbx_set_body (mailbox_t mbox, size_t msgno, const char *b,
size_t len, int replace)
{
return ENOSYS;
}
static int
mbx_append (mailbox_t mbox, size_t msgno)
{
return ENOSYS;
}
static int
mbx_destroy_msg (mailbox_t mbox, size_t msgno)
{
return ENOSYS;
}
/* reading */
static int
mbx_get_body (mailbox_t mbox, size_t msgno, off_t off,
char *b, size_t len, size_t *n)
{
return ENOSYS;
}
static int
mbx_get_mbody (mailbox_t mbox, size_t msgno, off_t off,
char **body, size_t *n)
{
size_t i = 0;
mbox->_get_body (mbox, msgno, off, NULL, 0, &i);
i++;
*body = calloc (i, sizeof (char));
if (*body == NULL)
{
return ENOMEM;
}
mbox->_get_body (mbox, msgno, off, *body, i, n);
return 0;
}
static int
mbx_get_header (mailbox_t mbox, size_t msgno, off_t off,
char *h, size_t len, size_t *n)
{
return ENOSYS;
}
static int
mbx_get_mheader (mailbox_t mbox, size_t msgno, off_t off,
char **header, size_t *n)
{
size_t i;
i = mbox->_get_header (mbox, msgno, off, NULL, 0, &i);
i++;
*header = calloc (i, sizeof (char));
if (*header == NULL)
{
return ENOMEM;
}
mbox->_get_header (mbox, msgno, off, *header, i, n);
return 0;
}
/* locking */
static int
mbx_lock (mailbox_t mbox, int flag)
{
return ENOSYS;
}
static int
mbx_unlock (mailbox_t mbox)
{
return ENOSYS;
}
/* owner and group */
static int
mbx_set_owner (mailbox_t mbox, uid_t uid)
{
mbox->owner = uid;
return 0;
}
static int
mbx_get_owner (mailbox_t mbox, uid_t *uid)
{
*uid = mbox->owner;
return 0;
}
static int
mbx_set_group (mailbox_t mbox, uid_t gid)
{
mbox->group = gid;
return 0;
}
static int
mbx_get_group (mailbox_t mbox, uid_t *gid)
{
*gid = mbox->group;
return 0;
}
/* misc */
static int
mbx_scan (mailbox_t mbox, size_t *msgs)
{
return ENOSYS;
}
static int
mbx_is_updated (mailbox_t mbox)
{
return ENOSYS;
}
static int
mbx_get_timeout (mailbox_t mbox, size_t *timeout)
{
*timeout = mbox->timeout;
return 0;
}
static int
mbx_set_timeout (mailbox_t mbox, size_t timeout)
{
mbox->timeout = timeout;
return 0;
}
static int
mbx_get_refresh (mailbox_t mbox, size_t *refresh)
{
*refresh = mbox->refresh;
return 0;
}
static int
mbx_set_refresh (mailbox_t mbox, size_t refresh)
{
mbox->refresh = refresh;
return 0;
}
static int
mbx_get_size (mailbox_t mbox, size_t msgno, size_t *sh, size_t *sb)
{
return ENOSYS;
}
static int
mbx_set_notification (mailbox_t mbox, int (*func) (mailbox_t, void *arg))
{
return ENOSYS;
}