Commit 06e9cb84 06e9cb844ccf3aefea60b1eb04019ff0d86f63fb by Alain Magloire

mailbox.c mbx_mbox.c mbx_mbox.h mbx_mdir.h mbx_unix.c

 	mbx_unix.h
 	mbx_mdir.c mbx_mmdf.c mbx_mmdf.h

slowly continuing work.
1 parent e516428e
...@@ -20,6 +20,9 @@ ...@@ -20,6 +20,9 @@
20 #endif 20 #endif
21 21
22 #include <mbx_mbox.h> 22 #include <mbx_mbox.h>
23 #include <mbx_unix.h>
24 #include <mbx_mdir.h>
25 #include <mbx_mmdf.h>
23 #include <mbx_pop.h> 26 #include <mbx_pop.h>
24 #include <mbx_imap.h> 27 #include <mbx_imap.h>
25 28
...@@ -90,7 +93,10 @@ static struct mailbox_builtin ...@@ -90,7 +93,10 @@ static struct mailbox_builtin
90 } mailbox_builtin [] = { 93 } mailbox_builtin [] = {
91 { NULL, 0, &mailbox_builtin[1] }, /* sentinel, head list */ 94 { NULL, 0, &mailbox_builtin[1] }, /* sentinel, head list */
92 { &_mailbox_mbox_type, 0, &mailbox_builtin[2] }, 95 { &_mailbox_mbox_type, 0, &mailbox_builtin[2] },
93 { &_mailbox_pop_type, 0, &mailbox_builtin[3] }, 96 { &_mailbox_unix_type, 0, &mailbox_builtin[3] },
97 { &_mailbox_maildir_type, 0, &mailbox_builtin[4] },
98 { &_mailbox_mmdf_type, 0, &mailbox_builtin[5] },
99 { &_mailbox_pop_type, 0, &mailbox_builtin[6] },
94 { &_mailbox_imap_type, 0, &mailbox_builtin[0] }, 100 { &_mailbox_imap_type, 0, &mailbox_builtin[0] },
95 }; 101 };
96 102
...@@ -160,7 +166,8 @@ mailbox_list_mtype (struct mailbox_type **mlist, int *n) ...@@ -160,7 +166,8 @@ mailbox_list_mtype (struct mailbox_type **mlist, int *n)
160 struct mailbox_type *mtype; 166 struct mailbox_type *mtype;
161 int i; 167 int i;
162 168
163 if ((i = mailbox_list_type (NULL, 0)) <= 0 || (mtype = malloc (i)) == NULL) 169 if ((i = mailbox_list_type (NULL, 0)) <= 0
170 || (mtype = calloc (i, sizeof (*mtype))) == NULL)
164 { 171 {
165 return -1; 172 return -1;
166 } 173 }
...@@ -224,7 +231,7 @@ mailbox_init (mailbox_t *mbox, const char *name, int id) ...@@ -224,7 +231,7 @@ mailbox_init (mailbox_t *mbox, const char *name, int id)
224 } 231 }
225 } 232 }
226 233
227 /* 3nd run: nothing yet ?? try unixmbox/maildir directly as the last resort. 234 /* 3nd run: nothing yet ?? try mbox directly as the last resort.
228 this should take care of the case where the filename is use */ 235 this should take care of the case where the filename is use */
229 if (status != 0 ) 236 if (status != 0 )
230 { 237 {
...@@ -372,7 +379,7 @@ mbx_get_mpasswd (mailbox_t mbox, char **passwd, int *len) ...@@ -372,7 +379,7 @@ mbx_get_mpasswd (mailbox_t mbox, char **passwd, int *len)
372 int i; 379 int i;
373 char *p; 380 char *p;
374 if ((i = mbox->_get_passwd (mbox, NULL, 0, 0)) <= 0 381 if ((i = mbox->_get_passwd (mbox, NULL, 0, 0)) <= 0
375 || (p = malloc (i)) == NULL) 382 || (p = calloc (i, sizeof (*p))) == NULL)
376 { 383 {
377 return -1; 384 return -1;
378 } 385 }
...@@ -469,7 +476,7 @@ mbx_get_mbody (mailbox_t mbox, int id, char **body, int *len) ...@@ -469,7 +476,7 @@ mbx_get_mbody (mailbox_t mbox, int id, char **body, int *len)
469 int i; 476 int i;
470 char *b; 477 char *b;
471 if ((i = mbox->_get_body (mbox, id, NULL, 0, 0)) <= 0 478 if ((i = mbox->_get_body (mbox, id, NULL, 0, 0)) <= 0
472 || (b = malloc (i)) == NULL) 479 || (b = calloc (i, sizeof (*b))) == NULL)
473 { 480 {
474 return -1; 481 return -1;
475 } 482 }
...@@ -490,7 +497,7 @@ mbx_get_mheader (mailbox_t mbox, int id, char **header, int *len) ...@@ -490,7 +497,7 @@ mbx_get_mheader (mailbox_t mbox, int id, char **header, int *len)
490 int i; 497 int i;
491 char *h; 498 char *h;
492 if ((i = mbox->_get_header (mbox, id, NULL, 0, 0)) <= 0 499 if ((i = mbox->_get_header (mbox, id, NULL, 0, 0)) <= 0
493 || (h = malloc (i)) == NULL) 500 || (h = calloc (i, sizeof (*h))) == NULL)
494 { 501 {
495 return -1; 502 return -1;
496 } 503 }
......
...@@ -25,29 +25,101 @@ ...@@ -25,29 +25,101 @@
25 #include <mbx_unix.h> 25 #include <mbx_unix.h>
26 #include <mbx_mdir.h> 26 #include <mbx_mdir.h>
27 27
28 #include <errno.h>
29 #include <sys/stat.h>
30
28 struct mailbox_type _mailbox_mbox_type = 31 struct mailbox_type _mailbox_mbox_type =
29 { 32 {
30 "UNIX MBOX", 33 "UNIX_MBOX/Maildir/MMDF",
31 (int)&_url_mbox_type, &_url_mbox_type, 34 (int)&_url_mbox_type, &_url_mbox_type,
32 mailbox_mbox_init, mailbox_mbox_destroy 35 mailbox_mbox_init, mailbox_mbox_destroy
33 }; 36 };
34 37
35 /* 38 /*
36 There is no specific URL for file mailbox, until we 39 if there is no specific URL for file mailbox,
37 come up with a url for each like : 40 file://<path_name>
38 maildir:// 41
39 mmdf:// 42 It would be preferrable to use :
40 ubox:// 43 maildir://<path>
41 they all share the same url which is 44 unix://<path>
42 file://<path_name> */ 45 mmdf://<path>
46 This would eliminate heuristic discovery that would turn
47 out to be wrong. Caveat, there is no std URL for those
48 mailbox.
49 */
50
43 int 51 int
44 mailbox_mbox_init (mailbox_t *mbox, const char *name) 52 mailbox_mbox_init (mailbox_t *mbox, const char *name)
45 { 53 {
54 struct stat st;
55
56 /*
57 If they want to creat ?? should they know the type ???
58 What is the best course of action ??
59 */
60 if (stat (name, &st) == -1)
61 {
62 return -1; /* errno set by stat () */
63 }
64
65 if (S_ISREG (st.st_mode))
66 {
67 /*
68 FIXME: We should do an open() and try
69 to do a better reconnaissance of the type,
70 maybe MMDF. For now assume Unix MBox */
71 #if 0
72 char head[6];
73 ssize_t cout;
74 int fd;
75
76 fd = open (name, O_RDONLY);
77 if (fd == -1)
78 {
79 /* Oops !! wrong permission ? file deleted ? */
80 return -1; /* errno set by open () */
81 }
82
83 /* Read a small chunck */
84 count = read (fd, head, sizeof(head));
85
86 /* Try Unix Mbox */
87
88 /* FIXME:
89 What happen if the file is empty ???
90 Do we default to Unix ??
91 */
92 if (count == 0) /*empty file*/
93 {
94 return mailbox_unix_init (mbox, name);
95 }
96
97 if (count >= 5)
98 {
99 if (strncmp (head, "From ", 5) == 0)
100 {
101 /* This is Unix Mbox */
102 return mailbox_unix_init (mbox, name);
103 }
104 }
105
106 /* Try MMDF */
107 #endif
108 return mailbox_unix_init (mbox, name);
109 }
110 else if (S_ISDIR (st.st_mode))
111 {
112 /* Is that true ? Are all directories Maildir ?? */
113 return mailbox_maildir_init (mbox, name);
114 }
115
116 /* Why can't a mailbox be FIFO ? or a DOOR/Portal ? */
117 errno = EINVAL;
46 return -1; 118 return -1;
47 } 119 }
48 120
49 void 121 void
50 mailbox_mbox_destroy (mailbox_t *mbox) 122 mailbox_mbox_destroy (mailbox_t *mbox)
51 { 123 {
52 return ; 124 return (*mbox)->mtype->_destroy (mbox);
53 } 125 }
......
...@@ -15,16 +15,14 @@ ...@@ -15,16 +15,14 @@
15 along with this program; if not, write to the Free Software 15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #ifndef _MBX_UNIX_H 18 #ifndef _MBX_MBOX_H
19 #define _MBX_UNIX_H 1 19 #define _MBX_MBOX_H 1
20 20
21 #include <mailbox.h> 21 #include <mailbox.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 22
25 extern int mailbox_mbox_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_mbox_init __P ((mailbox_t *mbox, const char *name));
26 extern void mailbox_mbox_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_mbox_destroy __P ((mailbox_t *mbox));
27 25
28 extern struct mailbox_type _mailbox_mbox_type; 26 extern struct mailbox_type _mailbox_mbox_type;
29 27
30 #endif /* _MBX_UNIX_H */ 28 #endif /* _MBX_MBOX_H */
......
1 #include <url_mdir.h>
2 #include <mbx_mdir.h>
3
4 struct mailbox_type _mailbox_maildir_type =
5 {
6 "MAILDIR",
7 (int)&_url_maildir_type, &_url_maildir_type,
8 mailbox_maildir_init, mailbox_maildir_destroy
9 };
10
11 int
12 mailbox_maildir_init (mailbox_t *mbox, const char *name)
13 {
14 return -1;
15 }
16
17 void
18 mailbox_maildir_destroy (mailbox_t *mbox)
19 {
20 return;
21 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MBX_MDIR_H
19 #define _MBX_MDIR_H 1
20
21 #include <mailbox.h>
22
23 extern int mailbox_maildir_init __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_maildir_destroy __P ((mailbox_t *mbox));
25
26 extern struct mailbox_type _mailbox_maildir_type;
27
28 #endif /* _MBX_MDIR_H */
......
1 #include <url_mmdf.h>
2 #include <mbx_mmdf.h>
3
4 struct mailbox_type _mailbox_maildir_type =
5 {
6 "MMDF",
7 (int)&_url_mmdf_type, &_url_mmdf_type,
8 mailbox_mmdf_init, mailbox_mmdf_destroy
9 };
10
11 int
12 mailbox_mmdf_init (mailbox_t *mbox, const char *name)
13 {
14 return -1;
15 }
16
17 void
18 mailbox_mmdf_destroy (mailbox_t *mbox)
19 {
20 return;
21 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MBX_MMDF_H
19 #define _MBX_MMDF_H 1
20
21 #include <mailbox.h>
22
23 extern int mailbox_mmdf_init __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_mmdf_destroy __P ((mailbox_t *mbox));
25
26 extern struct mailbox_type _mailbox_mmdf_type;
27
28 #endif /* _MBX_MMDF_H */
1 #include <url_unix.h>
2 #include <mbx_unix.h>
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9
10 struct mailbox_type _mailbox_unix_type =
11 {
12 "UNIX MBOX",
13 (int)&_url_unix_type, &_url_unix_type,
14 mailbox_unix_init, mailbox_unix_destroy
15 };
16
17 typedef struct _mailbox_unix_msg
18 {
19 off_t header;
20 off_t body;
21 off_t end;
22 int deleted;
23 } mailbox_unix_msg_t;
24
25 typedef struct _mailbox_unix_data
26 {
27 mailbox_unix_msg_t *messages;
28 FILE *file;
29 mailbox_lock_t lockmode;
30 time_t last_mode_time;
31 } mailbox_unix_data_t;
32
33
34 /* forward prototypes */
35
36 static int mailbox_unix_open (mailbox_t *mbox, const char *name);
37 static int mailbox_unix_close (mailbox_t *mbox);
38
39 static int mailbox_unix_get_name (mailbox_t, int *id, char *name,
40 int offset, int len);
41 static int mailbox_unix_get_mname (mailbox_t, int *id, char **name, int *len);
42
43 /* passwd */
44 static int mailbox_unix_get_passwd (mailbox_t, char *passwd,
45 int offset, int len);
46 static int mailbox_unix_get_mpasswd (mailbox_t, char **passwd, int *len);
47 static int mailbox_unix_set_passwd (mailbox_t, const char *passwd,
48 int offset, int len);
49
50 /* deleting */
51 static int mailbox_unix_delete (mailbox_t, int);
52 static int mailbox_unix_undelete (mailbox_t, int);
53 static int mailbox_unix_expunge (mailbox_t);
54 static int mailbox_unix_is_deleted (mailbox_t, int);
55
56 /* appending */
57 static int mailbox_unix_new_msg (mailbox_t, int * id);
58 static int mailbox_unix_set_header (mailbox_t, int id, const char *h,
59 int offset, int n, int replace);
60 static int mailbox_unix_set_body (mailbox_t, int id, const char *b,
61 int offset, int n, int replace);
62 static int mailbox_unix_append (mailbox_t, int id);
63 static int mailbox_unix_destroy_msg (mailbox_t, int id);
64
65 /* reading */
66 static int mailbox_unix_get_body (mailbox_t, int id, char *b,
67 int offset, int n);
68 static int mailbox_unix_get_mbody (mailbox_t, int id, char **b,
69 int *n);
70 static int mailbox_unix_get_header (mailbox_t, int id, char *h,
71 int offset, int n);
72 static int mailbox_unix_get_mheader (mailbox_t, int id, char **h,
73 int *n);
74
75 /* locking */
76 static int mailbox_unix_lock (mailbox_t, int flag);
77 static int mailbox_unix_unlock (mailbox_t);
78
79 /* miscellany */
80 static int mailbox_unix_scan (mailbox_t, int *msgs);
81 static int mailbox_unix_is_updated (mailbox_t);
82 static int mailbox_unix_get_timeout (mailbox_t, int *timeout);
83 static int mailbox_unix_set_timeout (mailbox_t, int timeout);
84 static int mailbox_unix_get_refresh (mailbox_t, int *refresh);
85 static int mailbox_unix_set_refresh (mailbox_t, int refresh);
86 static int mailbox_unix_get_size (mailbox_t, int id, size_t *size);
87 static int mailbox_unix_set_notification (mailbox_t,
88 int (*func) (mailbox_t, void *arg));
89
90
91 int
92 mailbox_unix_init (mailbox_t *pmbox, const char *name)
93 {
94 mailbox_t mbox;
95
96 /*
97 Again should we do an open() and check if indeed this is a
98 correct mbox ? I think not, this should be delay to _open()
99 */
100 mbox = calloc (1, sizeof (*mbox));
101 if (mbox == NULL)
102 return -1; /* errno set by calloc() */
103
104 /* set the type */
105 mbox->mtype = &_mailbox_unix_type;
106
107 mbox->_open = mailbox_unix_open;
108 mbox->_close = mailbox_unix_close;
109
110 mbox->_get_name = mailbox_unix_get_name;
111 mbox->_get_mname = mailbox_unix_get_mname;
112
113 /* passwd */
114 mbox->_get_passwd = mailbox_unix_get_passwd;
115 mbox->_get_mpasswd = mailbox_unix_get_mpasswd;
116 mbox->_set_passwd = mailbox_unix_set_passwd;
117
118 /* deleting */
119 mbox->_delete = mailbox_unix_delete;
120 mbox->_undelete = mailbox_unix_undelete;
121 mbox->_expunge = mailbox_unix_expunge;
122 mbox->_is_deleted = mailbox_unix_is_deleted;
123
124 /* appending */
125 mbox->_new_msg = mailbox_unix_new_msg;
126 mbox->_set_header = mailbox_unix_set_header;
127 mbox->_set_body = mailbox_unix_set_body;
128 mbox->_append = mailbox_unix_append;
129 mbox->_destry_msg = mailbox_unix_destroy_msg;
130
131 /* reading */
132 mbox->_get_body = mailbox_unix_get_body;
133 mbox->_get_mbody = mailbox_unix_get_mbody;
134 mbox->_get_header = mailbox_unix_get_header;
135 mbox->_get_mheader = mailbox_unix_get_mheader;
136
137 /* locking */
138 mbox->_lock = mailbox_unix_lock;
139 mbox->_unlock = mailbox_unix_unlock;
140
141 /* miscellany */
142 mbox->_scan = mailbox_unix_scan;
143 mbox->_is_updated = mailbox_unix_is_updated;
144 mbox->_get_timeout = mailbox_unix_get_timeout;
145 mbox->_set_timeout = mailbox_unix_set_timeout;
146 mbox->_get_refresh = mailbox_unix_get_refresh;
147 mbox->_set_refresh = mailbox_unix_set_refresh;
148 mbox->_get_size = mailbox_unix_get_size;
149 mbox->_set_notification = mailbox_unix_set_notification;
150
151 return 0;
152 }
153
154 void
155 mailbox_unix_destroy (mailbox_t *mbox)
156 {
157 return;
158 }
159
160
161 /* start of Mbox Implementation */
162
163 static int mailbox_unix_open (mailbox_t mbox, int flags)
164 {
165 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MBX_UNIX_H
19 #define _MBX_UNIX_H 1
20
21 #include <mailbox.h>
22
23 extern int mailbox_unix_init __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_unix_destroy __P ((mailbox_t *mbox));
25
26 extern struct mailbox_type _mailbox_unix_type;
27
28 #endif /* _MBX_UNIX_H */
......