mailbox.c
11.9 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
/* 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 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_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, int flag);
/* name */
static int mbx_get_name (mailbox_t, int *id, char *name, int offset, int n);
/* passwd */
static int mbx_get_passwd (mailbox_t, char *passwd, int offset, int len);
static int mbx_get_mpasswd (mailbox_t, char **passwd, int *len);
static int mbx_set_passwd (mailbox_t, const char *passwd, int offset, int n);
/* deleting */
static int mbx_delete (mailbox_t, int id);
static int mbx_undelete (mailbox_t, int id);
static int mbx_expunge (mailbox_t);
static int mbx_is_deleted (mailbox_t, int id);
/* appending */
static int mbx_new_msg (mailbox_t, int *id);
static int mbx_set_header (mailbox_t, int id, const char *h,
int offset, int n, int replace);
static int mbx_set_body (mailbox_t, int id, const char *b,
int offset, int n, int replace);
static int mbx_append (mailbox_t, int id);
static int mbx_destroy_msg (mailbox_t, int id);
/* reading */
static int mbx_get_body (mailbox_t, int id, char *b, int offset, int n);
static int mbx_get_mbody (mailbox_t, int id, char **b, int *n);
static int mbx_get_header (mailbox_t, int id, char *h, int offset, int n);
static int mbx_get_mheader (mailbox_t, int id, char **h, int *n);
/* locking */
static int mbx_lock (mailbox_t, int flag);
static int mbx_unlock (mailbox_t);
/* misc */
static int mbx_scan (mailbox_t, int *msgs);
static int mbx_is_updated (mailbox_t);
static int mbx_get_timeout (mailbox_t, int *timeout);
static int mbx_set_timeout (mailbox_t, int timeout);
static int mbx_get_refresh (mailbox_t, int *refresh);
static int mbx_set_refresh (mailbox_t, int refresh);
static int mbx_get_size (mailbox_t, int id, size_t *size);
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_pop_type, 0, &mailbox_builtin[3] },
{ &_mailbox_imap_type, 0, &mailbox_builtin[0] },
};
int
malibox_add_type (struct mailbox_type *mtype)
{
struct mailbox_builtin *current = malloc (sizeof (*current));
if (current == NULL)
return -1;
if (mtype->utype)
{
if (url_add_type (mtype->utype) < 0)
{
free (current);
return -1;
}
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 -1;
}
int
mailbox_list_type (struct mailbox_type *list, int n)
{
struct mailbox_builtin *current;
int i;
for (i = 0, current = mailbox_builtin->next; current != mailbox_builtin;
current = current->next, i++)
{
if (list)
if (i < n)
list[i] = *(current->mtype);
}
return i;
}
int
mailbox_list_mtype (struct mailbox_type **mlist, int *n)
{
struct mailbox_type *mtype;
int i;
if ((i = mailbox_list_type (NULL, 0)) <= 0 || (mtype = malloc (i)) == NULL)
{
return -1;
}
*mlist = mtype;
return *n = mailbox_list_type (mtype, i);
}
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 -1;
}
int
mailbox_init (mailbox_t *mbox, const char *name, int id)
{
int status = -1; /* -1 means error */
/* 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 unixmbox/maildir 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_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->_scan == NULL)
mbox->_scan = mbx_scan;
if (mbox->_is_updated == NULL)
mbox->_is_updated = mbx_is_updated;
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)
{
errno = ENOSYS;
return -1;
}
static int
mbx_close (mailbox_t mbox, int flag)
{
errno = ENOSYS;
return -1;
}
/* name */
static int
mbx_get_name (mailbox_t mbox, int *id, char *name, int offset, int n)
{
errno = ENOSYS;
return -1;
}
/* passwd */
static int
mbx_get_passwd (mailbox_t mbox, char *passwd, int offset, int len)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_mpasswd (mailbox_t mbox, char **passwd, int *len)
{
int i;
char *p;
if ((i = mbox->_get_passwd (mbox, NULL, 0, 0)) <= 0
|| (p = malloc (i)) == NULL)
{
return -1;
}
*passwd = p;
return *len = i;
}
static int
mbx_set_passwd (mailbox_t mbox, const char *passwd, int offset, int n)
{
errno = ENOSYS;
return -1;
}
/* deleting */
static int
mbx_delete (mailbox_t mbox, int id)
{
errno = ENOSYS;
return -1;
}
static int
mbx_undelete (mailbox_t mbox, int id)
{
errno = ENOSYS;
return -1;
}
static int
mbx_expunge (mailbox_t mbox)
{
errno = ENOSYS;
return -1;
}
static int
mbx_is_deleted (mailbox_t mbox, int id)
{
errno = ENOSYS;
return -1;
}
/* appending */
static int
mbx_new_msg (mailbox_t mbox, int *id)
{
errno = ENOSYS;
return -1;
}
static int
mbx_set_header (mailbox_t mbox, int id, const char *h,
int offset, int n, int replace)
{
errno = ENOSYS;
return -1;
}
static int
mbx_set_body (mailbox_t mbox, int id, const char *b,
int offset, int n, int replace)
{
errno = ENOSYS;
return -1;
}
static int
mbx_append (mailbox_t mbox, int id)
{
errno = ENOSYS;
return -1;
}
static int
mbx_destroy_msg (mailbox_t mbox, int id)
{
errno = ENOSYS;
return -1;
}
/* reading */
static int
mbx_get_body (mailbox_t mbox, int id, char *b, int offset, int n)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_mbody (mailbox_t mbox, int id, char **body, int *len)
{
int i;
char *b;
if ((i = mbox->_get_body (mbox, id, NULL, 0, 0)) <= 0
|| (b = malloc (i)) == NULL)
{
return -1;
}
*body = b;
return *len = i;
}
static int
mbx_get_header (mailbox_t mbox, int id, char *h, int offset, int n)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_mheader (mailbox_t mbox, int id, char **header, int *len)
{
int i;
char *h;
if ((i = mbox->_get_header (mbox, id, NULL, 0, 0)) <= 0
|| (h = malloc (i)) == NULL)
{
return -1;
}
*header = h;
return *len = i;
}
/* locking */
static int
mbx_lock (mailbox_t mbox, int flag)
{
errno = ENOSYS;
return -1;
}
static int
mbx_unlock (mailbox_t mbox)
{
errno = ENOSYS;
return -1;
}
/* misc */
static int
mbx_scan (mailbox_t mbox, int *msgs)
{
errno = ENOSYS;
return -1;
}
static int
mbx_is_updated (mailbox_t mbox)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_timeout (mailbox_t mbox, int *timeout)
{
errno = ENOSYS;
return -1;
}
static int
mbx_set_timeout (mailbox_t mbox, int timeout)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_refresh (mailbox_t mbox, int *refresh)
{
errno = ENOSYS;
return -1;
}
static int
mbx_set_refresh (mailbox_t mbox, int refresh)
{
errno = ENOSYS;
return -1;
}
static int
mbx_get_size (mailbox_t mbox, int id, size_t *size)
{
errno = ENOSYS;
return -1;
}
static int
mbx_set_notification (mailbox_t mbox, int (*func) (mailbox_t, void *arg))
{
errno = ENOSYS;
return -1;
}