Commit d9a9b601 d9a9b601ddbfa36443aa6482c892c5587986dbff by Alain Magloire

attribute.c auth.c header.c io.c locker.c mailbox.c mbx_imap.c

 	mbx_mbox.c mbx_mdir.c mbx_mh.c mbx_mmdf.c mbx_pop.c mbx_unix.c
 	message.c mime.c registrar.c url.c url_file.c url_imap.c
 	url_mail.c url_mbox.c url_mdir.c url_mh.c url_mmdf.c url_pop.c
 	url_unix.c include/private/mailbox0.h
 	include/private/mbx_imap.h include/private/mbx_mbox.h
 	include/private/mbx_mdir.h include/private/mbx_mmdf.h
 	include/private/mbx_pop.h include/private/mbx_unix.h
 	include/private/message0.h include/private/mime0.h
 	include/private/url0.h include/public/attribute.h
 	include/public/auth.h include/public/event.h
 	include/public/header.h include/public/io.h
 	include/public/locker.h include/public/mailbox.h
 	include/public/message.h include/public/mime.h
 	include/public/registrar.h include/public/transcode.h
 	include/public/url.h

changed all the *_init to *_create().
added *_lines() functions
optimisation of the parsing.
1 parent edadcf90
...@@ -15,29 +15,15 @@ ...@@ -15,29 +15,15 @@
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 #include <attribute.h> 18 #include <attribute0.h>
19 19
20 #include <sys/types.h> 20 #include <sys/types.h>
21 #include <stdlib.h> 21 #include <stdlib.h>
22 #include <string.h> 22 #include <string.h>
23 #include <errno.h> 23 #include <errno.h>
24 24
25 #define MU_ATTRIBUTE_SEEN ((int)1)
26 #define MU_ATTRIBUTE_ANSWERED (MU_ATTRIBUTE_SEEN << 1)
27 #define MU_ATTRIBUTE_FLAGGED (MU_ATTRIBUTE_ANSWERED << 1)
28 #define MU_ATTRIBUTE_DELETED (MU_ATTRIBUTE_FLAGGED << 1)
29 #define MU_ATTRIBUTE_DRAFT (MU_ATTRIBUTE_DELETED << 1)
30 #define MU_ATTRIBUTE_RECENT (MU_ATTRIBUTE_DRAFT << 1)
31 #define MU_ATTRIBUTE_READ (MU_ATTRIBUTE_RECENT << 1)
32
33 struct _attribute
34 {
35 size_t flag;
36 void *owner;
37 };
38
39 int 25 int
40 attribute_init (attribute_t *pattr, void *owner) 26 attribute_create (attribute_t *pattr, void *owner)
41 { 27 {
42 attribute_t attr; 28 attribute_t attr;
43 if (pattr == NULL || owner == NULL) 29 if (pattr == NULL || owner == NULL)
...@@ -274,7 +260,7 @@ string_to_attribute (const char *buffer, size_t len, ...@@ -274,7 +260,7 @@ string_to_attribute (const char *buffer, size_t len,
274 char *sep; 260 char *sep;
275 int status; 261 int status;
276 262
277 status = attribute_init (pattr, owner); 263 status = attribute_create (pattr, owner);
278 if (status != 0) 264 if (status != 0)
279 return status; 265 return status;
280 266
......
...@@ -32,7 +32,7 @@ struct _auth ...@@ -32,7 +32,7 @@ struct _auth
32 }; 32 };
33 33
34 int 34 int
35 auth_init (auth_t *pauth, void *owner) 35 auth_create (auth_t *pauth, void *owner)
36 { 36 {
37 auth_t auth; 37 auth_t auth;
38 if (pauth == NULL) 38 if (pauth == NULL)
......
...@@ -57,7 +57,7 @@ struct _header ...@@ -57,7 +57,7 @@ struct _header
57 }; 57 };
58 58
59 int 59 int
60 header_init (header_t *ph, const char *blurb, size_t len, void *owner) 60 header_create (header_t *ph, const char *blurb, size_t len, void *owner)
61 { 61 {
62 header_t h; 62 header_t h;
63 int status; 63 int status;
...@@ -66,14 +66,9 @@ header_init (header_t *ph, const char *blurb, size_t len, void *owner) ...@@ -66,14 +66,9 @@ header_init (header_t *ph, const char *blurb, size_t len, void *owner)
66 return ENOMEM; 66 return ENOMEM;
67 h->owner = owner; 67 h->owner = owner;
68 68
69 status = header_parse (h, (char *)blurb, len); 69 header_parse (h, (char *)blurb, len);
70 if (status != 0)
71 {
72 free (h);
73 return status;
74 }
75 70
76 status = stream_init (&(h->stream), h); 71 status = stream_create (&(h->stream), h);
77 if (status != 0) 72 if (status != 0)
78 return status; 73 return status;
79 74
...@@ -132,7 +127,7 @@ header_parse (header_t header, char *blurb, int len) ...@@ -132,7 +127,7 @@ header_parse (header_t header, char *blurb, int len)
132 return 0; 127 return 0;
133 128
134 header->blurb_len = len; 129 header->blurb_len = len;
135 header->blurb = calloc (1, header->blurb_len); 130 header->blurb = calloc (1, header->blurb_len + 1);
136 if (header->blurb == NULL) 131 if (header->blurb == NULL)
137 return ENOMEM; 132 return ENOMEM;
138 memcpy (header->blurb, blurb, header->blurb_len); 133 memcpy (header->blurb, blurb, header->blurb_len);
...@@ -214,6 +209,7 @@ header_parse (header_t header, char *blurb, int len) ...@@ -214,6 +209,7 @@ header_parse (header_t header, char *blurb, int len)
214 header->hdr_count++; 209 header->hdr_count++;
215 } /* for (header_start ...) */ 210 } /* for (header_start ...) */
216 211
212 #if 0
217 header->blurb_len -= len; 213 header->blurb_len -= len;
218 if (header->blurb_len <= 0) 214 if (header->blurb_len <= 0)
219 { 215 {
...@@ -221,6 +217,10 @@ header_parse (header_t header, char *blurb, int len) ...@@ -221,6 +217,10 @@ header_parse (header_t header, char *blurb, int len)
221 free (header->hdr); 217 free (header->hdr);
222 return EINVAL; 218 return EINVAL;
223 } 219 }
220 /* always add the separtor LF */
221 header->blurb [header->blurb_len] = '\n';
222 header->blurb_len++;
223 #endif
224 return 0; 224 return 0;
225 } 225 }
226 226
...@@ -305,7 +305,24 @@ header_entry_count (header_t header, size_t *pnum) ...@@ -305,7 +305,24 @@ header_entry_count (header_t header, size_t *pnum)
305 } 305 }
306 306
307 int 307 int
308 header_get_size (header_t header, size_t *pnum) 308 header_lines (header_t header, size_t *plines)
309 {
310 int n;
311 size_t t = 0;
312 if (header == NULL)
313 return EINVAL;
314 for (n = header->blurb_len - 1; n >= 0; n--)
315 {
316 if (header->blurb[n] == '\n')
317 t++;
318 }
319 if (plines)
320 *plines = t;
321 return 0;
322 }
323
324 int
325 header_size (header_t header, size_t *pnum)
309 { 326 {
310 if (header == NULL) 327 if (header == NULL)
311 return EINVAL; 328 return EINVAL;
......
...@@ -42,6 +42,7 @@ struct _mailbox ...@@ -42,6 +42,7 @@ struct _mailbox
42 char *name; 42 char *name;
43 auth_t auth; 43 auth_t auth;
44 locker_t locker; 44 locker_t locker;
45 netinstance_t netinstance;
45 url_t url; 46 url_t url;
46 47
47 /* register events */ 48 /* register events */
...@@ -53,7 +54,7 @@ struct _mailbox ...@@ -53,7 +54,7 @@ struct _mailbox
53 54
54 /* Public methods */ 55 /* Public methods */
55 56
56 int (*_init) __P ((mailbox_t *, const char *)); 57 int (*_create) __P ((mailbox_t *, const char *));
57 void (*_destroy) __P ((mailbox_t *)); 58 void (*_destroy) __P ((mailbox_t *));
58 59
59 int (*_open) __P ((mailbox_t, int flag)); 60 int (*_open) __P ((mailbox_t, int flag));
...@@ -75,19 +76,8 @@ struct _mailbox ...@@ -75,19 +76,8 @@ struct _mailbox
75 }; 76 };
76 77
77 /* private */ 78 /* private */
78 extern int mailbox_delete __P ((mailbox_t, size_t msgno));
79 extern int mailbox_undelete __P ((mailbox_t, size_t msgno));
80 extern int mailbox_is_deleted __P ((mailbox_t, size_t msgno));
81 extern int mailbox_num_deleted __P ((mailbox_t, size_t *)); 79 extern int mailbox_num_deleted __P ((mailbox_t, size_t *));
82 80
83 extern int mailbox_get_auth __P ((mailbox_t mbox, auth_t *auth));
84 extern int mailbox_set_auth __P ((mailbox_t mbox, auth_t auth));
85 extern int mailbox_get_locker __P ((mailbox_t mbox, locker_t *locker));
86 extern int mailbox_set_locker __P ((mailbox_t mbox, locker_t locker));
87 extern int mailbox_get_attribute __P ((mailbox_t mbox, size_t msgno,
88 attribute_t *attr));
89 extern int mailbox_set_attribute __P ((mailbox_t mbox, size_t msgno,
90 attribute_t attr));
91 extern int mailbox_notification __P ((mailbox_t mbox, size_t type)); 81 extern int mailbox_notification __P ((mailbox_t mbox, size_t type));
92 82
93 83
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 20
21 #include <mailbox0.h> 21 #include <mailbox0.h>
22 22
23 extern int mailbox_imap_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_imap_create __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_imap_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_imap_destroy __P ((mailbox_t *mbox));
25 25
26 extern struct mailbox_type _mailbox_imap_type; 26 extern struct mailbox_type _mailbox_imap_type;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 20
21 #include <mailbox.h> 21 #include <mailbox.h>
22 22
23 extern int mailbox_mbox_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_mbox_create __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_mbox_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_mbox_destroy __P ((mailbox_t *mbox));
25 25
26 extern struct mailbox_type _mailbox_mbox_type; 26 extern struct mailbox_type _mailbox_mbox_type;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 20
21 #include <mailbox0.h> 21 #include <mailbox0.h>
22 22
23 extern int mailbox_maildir_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_maildir_create __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_maildir_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_maildir_destroy __P ((mailbox_t *mbox));
25 25
26 extern struct mailbox_type _mailbox_maildir_type; 26 extern struct mailbox_type _mailbox_maildir_type;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 20
21 #include <mailbox0.h> 21 #include <mailbox0.h>
22 22
23 extern int mailbox_mmdf_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_mmdf_create __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_mmdf_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_mmdf_destroy __P ((mailbox_t *mbox));
25 25
26 extern struct mailbox_type _mailbox_mmdf_type; 26 extern struct mailbox_type _mailbox_mmdf_type;
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 20
21 #include <mailbox0.h> 21 #include <mailbox0.h>
22 22
23 extern int mailbox_pop_init __P ((mailbox_t *mbox, const char *name)); 23 extern int mailbox_pop_create __P ((mailbox_t *mbox, const char *name));
24 extern void mailbox_pop_destroy __P ((mailbox_t *mbox)); 24 extern void mailbox_pop_destroy __P ((mailbox_t *mbox));
25 25
26 extern struct mailbox_type _mailbox_pop_type; 26 extern struct mailbox_type _mailbox_pop_type;
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
24 extern "C" { 24 extern "C" {
25 #endif 25 #endif
26 26
27 extern int mailbox_unix_init __P ((mailbox_t *mbox, const char *name)); 27 extern int mailbox_unix_create __P ((mailbox_t *mbox, const char *name));
28 extern void mailbox_unix_destroy __P ((mailbox_t *mbox)); 28 extern void mailbox_unix_destroy __P ((mailbox_t *mbox));
29 29
30 extern struct mailbox_type _mailbox_unix_type; 30 extern struct mailbox_type _mailbox_unix_type;
......
...@@ -39,20 +39,6 @@ extern "C" { ...@@ -39,20 +39,6 @@ extern "C" {
39 # endif 39 # endif
40 #endif /*__P */ 40 #endif /*__P */
41 41
42 /* The notion of body_t is not exported outside,
43 * there was no need for yet another object.
44 * since only floating messages need them. The functions
45 * that manipulate those objects are static to message.c
46 *
47 */
48 struct _body
49 {
50 FILE *file;
51 void *owner;
52 };
53
54 typedef struct _body * body_t;
55
56 /* forward declaration */ 42 /* forward declaration */
57 struct _message 43 struct _message
58 { 44 {
...@@ -79,9 +65,9 @@ struct _message ...@@ -79,9 +65,9 @@ struct _message
79 int (*_get_stream) __P ((message_t msg, stream_t *)); 65 int (*_get_stream) __P ((message_t msg, stream_t *));
80 int (*_set_stream) __P ((message_t msg, stream_t, void *owner)); 66 int (*_set_stream) __P ((message_t msg, stream_t, void *owner));
81 67
82 int (*_size) __P ((message_t msg, size_t *size)); 68 int (*_from) __P ((message_t msg, char *, size_t, size_t *));
69 int (*_received) __P ((message_t msg, char *, size_t, size_t *));
83 70
84 int (*_clone) __P ((message_t msg, message_t *cmsg));
85 }; 71 };
86 72
87 #ifdef _cplusplus 73 #ifdef _cplusplus
......
...@@ -81,6 +81,7 @@ struct _mime ...@@ -81,6 +81,7 @@ struct _mime
81 81
82 struct _mime_part 82 struct _mime_part
83 { 83 {
84 char sig[4];
84 mime_t mime; 85 mime_t mime;
85 header_t hdr; 86 header_t hdr;
86 message_t msg; 87 message_t msg;
......
...@@ -47,7 +47,7 @@ struct _url ...@@ -47,7 +47,7 @@ struct _url
47 47
48 void *data; 48 void *data;
49 49
50 int (*_init) __P ((url_t *url, const char *name)); 50 int (*_create) __P ((url_t *url, const char *name));
51 void (*_destroy) __P ((url_t *url)); 51 void (*_destroy) __P ((url_t *url));
52 52
53 /* Methods */ 53 /* Methods */
......
...@@ -35,7 +35,7 @@ extern "C" { ...@@ -35,7 +35,7 @@ extern "C" {
35 struct _attribute; 35 struct _attribute;
36 typedef struct _attribute * attribute_t; 36 typedef struct _attribute * attribute_t;
37 37
38 extern int attribute_init __P ((attribute_t *, void *owner)); 38 extern int attribute_create __P ((attribute_t *, void *owner));
39 extern void attribute_destroy __P ((attribute_t *, void *owner)); 39 extern void attribute_destroy __P ((attribute_t *, void *owner));
40 40
41 extern int attribute_is_seen __P ((attribute_t)); 41 extern int attribute_is_seen __P ((attribute_t));
......
...@@ -36,7 +36,7 @@ extern "C" { ...@@ -36,7 +36,7 @@ extern "C" {
36 struct _auth; 36 struct _auth;
37 typedef struct _auth *auth_t; 37 typedef struct _auth *auth_t;
38 38
39 extern int auth_init __P ((auth_t *, void *owner)); 39 extern int auth_create __P ((auth_t *, void *owner));
40 extern void auth_destroy __P ((auth_t *, void *owner)); 40 extern void auth_destroy __P ((auth_t *, void *owner));
41 41
42 extern int auth_prologue __P ((auth_t)); 42 extern int auth_prologue __P ((auth_t));
......
...@@ -67,7 +67,7 @@ extern "C" { ...@@ -67,7 +67,7 @@ extern "C" {
67 struct _header; 67 struct _header;
68 typedef struct _header * header_t; 68 typedef struct _header * header_t;
69 69
70 extern int header_init __P ((header_t *, const char *blurb, 70 extern int header_create __P ((header_t *, const char *blurb,
71 size_t ln, void *owner)); 71 size_t ln, void *owner));
72 extern void header_destroy __P ((header_t *, void *owner)); 72 extern void header_destroy __P ((header_t *, void *owner));
73 73
...@@ -81,7 +81,8 @@ extern int header_entry_name __P ((header_t, size_t num, char *buf, ...@@ -81,7 +81,8 @@ extern int header_entry_name __P ((header_t, size_t num, char *buf,
81 extern int header_entry_value __P ((header_t, size_t num, char *buf, 81 extern int header_entry_value __P ((header_t, size_t num, char *buf,
82 size_t buflen, size_t *total)); 82 size_t buflen, size_t *total));
83 extern int header_get_stream __P ((header_t, stream_t *stream)); 83 extern int header_get_stream __P ((header_t, stream_t *stream));
84 extern int header_get_size __P ((header_t, size_t *size)); 84 extern int header_size __P ((header_t, size_t *size));
85 extern int header_lines __P ((header_t, size_t *lines));
85 86
86 #ifdef _cplusplus 87 #ifdef _cplusplus
87 } 88 }
......
...@@ -35,7 +35,7 @@ extern "C" { /*}*/ ...@@ -35,7 +35,7 @@ extern "C" { /*}*/
35 struct _stream; 35 struct _stream;
36 typedef struct _stream *stream_t; 36 typedef struct _stream *stream_t;
37 37
38 extern int stream_init __P ((stream_t *, void *owner)); 38 extern int stream_create __P ((stream_t *, void *owner));
39 extern void stream_destroy __P ((stream_t *, void *owner)); 39 extern void stream_destroy __P ((stream_t *, void *owner));
40 40
41 extern int stream_set_fd __P ((stream_t, 41 extern int stream_set_fd __P ((stream_t,
......
...@@ -35,7 +35,7 @@ extern "C" { ...@@ -35,7 +35,7 @@ extern "C" {
35 struct _locker; 35 struct _locker;
36 typedef struct _locker *locker_t; 36 typedef struct _locker *locker_t;
37 37
38 extern int locker_init __P ((locker_t *, char *filename, 38 extern int locker_create __P ((locker_t *, char *filename,
39 size_t len, int flags)); 39 size_t len, int flags));
40 extern void locker_destroy __P ((locker_t *)); 40 extern void locker_destroy __P ((locker_t *));
41 41
......
...@@ -18,13 +18,14 @@ ...@@ -18,13 +18,14 @@
18 #ifndef _MAILBOX_H 18 #ifndef _MAILBOX_H
19 # define _MAILBOX_H 19 # define _MAILBOX_H
20 20
21 #include <sys/types.h>
22
23 #include <url.h> 21 #include <url.h>
24 #include <message.h> 22 #include <message.h>
25 #include <attribute.h> 23 #include <attribute.h>
26 #include <auth.h> 24 #include <auth.h>
27 #include <locker.h> 25 #include <locker.h>
26 #include <net.h>
27
28 #include <sys/types.h>
28 29
29 #ifdef __cplusplus 30 #ifdef __cplusplus
30 extern "C" { 31 extern "C" {
...@@ -43,7 +44,7 @@ struct _mailbox; ...@@ -43,7 +44,7 @@ struct _mailbox;
43 typedef struct _mailbox *mailbox_t; 44 typedef struct _mailbox *mailbox_t;
44 45
45 /* constructor/destructor and possible types */ 46 /* constructor/destructor and possible types */
46 extern int mailbox_init __P ((mailbox_t *, const char *, int id)); 47 extern int mailbox_create __P ((mailbox_t *, const char *, int id));
47 extern void mailbox_destroy __P ((mailbox_t *)); 48 extern void mailbox_destroy __P ((mailbox_t *));
48 49
49 /* flags for mailbox_open () */ 50 /* flags for mailbox_open () */
...@@ -63,6 +64,10 @@ extern int mailbox_append_message __P ((mailbox_t, message_t msg)); ...@@ -63,6 +64,10 @@ extern int mailbox_append_message __P ((mailbox_t, message_t msg));
63 extern int mailbox_messages_count __P ((mailbox_t, size_t *num)); 64 extern int mailbox_messages_count __P ((mailbox_t, size_t *num));
64 extern int mailbox_expunge __P ((mailbox_t)); 65 extern int mailbox_expunge __P ((mailbox_t));
65 66
67 /* netinstance settings */
68 extern int mailbox_get_netinstance __P ((mailbox_t, netinstance_t *net));
69 extern int mailbox_set_netinstance __P ((mailbox_t, netinstance_t net));
70
66 /* Lock settings */ 71 /* Lock settings */
67 extern int mailbox_get_locker __P ((mailbox_t, locker_t *locker)); 72 extern int mailbox_get_locker __P ((mailbox_t, locker_t *locker));
68 extern int mailbox_set_locker __P ((mailbox_t, locker_t locker)); 73 extern int mailbox_set_locker __P ((mailbox_t, locker_t locker));
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
19 #define _MESSAGE_H 19 #define _MESSAGE_H
20 20
21 #include <header.h> 21 #include <header.h>
22 #include <body.h>
22 #include <attribute.h> 23 #include <attribute.h>
23 #include <io.h> 24 #include <io.h>
24 25
...@@ -48,24 +49,38 @@ typedef struct _message *message_t; ...@@ -48,24 +49,38 @@ typedef struct _message *message_t;
48 * was enough. 49 * was enough.
49 */ 50 */
50 51
51 extern int message_init __P ((message_t *, void *owner)); 52 extern int message_create __P ((message_t *, void *owner));
52 extern void message_destroy __P ((message_t *, void *owner)); 53 extern void message_destroy __P ((message_t *, void *owner));
53 54
54 extern int message_get_header __P ((message_t, header_t *)); 55 extern int message_get_header __P ((message_t, header_t *));
55 extern int message_set_header __P ((message_t, header_t, void *owner)); 56 extern int message_set_header __P ((message_t, header_t, void *owner));
56 57
58 extern int message_get_body __P ((message_t, body_t *));
59 extern int message_set_body __P ((message_t, body_t, void *owner));
60
57 extern int message_get_stream __P ((message_t, stream_t *)); 61 extern int message_get_stream __P ((message_t, stream_t *));
58 extern int message_set_stream __P ((message_t, stream_t, void *owner));
59 62
60 extern int message_is_multipart __P ((message_t)); 63 extern int message_is_multipart __P ((message_t));
61 64
62 extern int message_get_size __P ((message_t, size_t *)); 65 extern int message_size __P ((message_t, size_t *));
63 extern int message_set_size __P ((message_t, size_t, void *owner)); 66 extern int message_lines __P ((message_t, size_t *));
67
68 extern int message_from __P ((message_t, char *, size_t, size_t *));
69 extern int message_set_from __P ((message_t,
70 int (*_from) __P ((message_t, char *,
71 size_t, size_t *)),
72 void *owner));
73 extern int message_received __P ((message_t, char *, size_t, size_t *));
74 extern int message_set_received __P ((message_t,
75 int (*_received) __P ((message_t,
76 char *, size_t,
77 size_t *)),
78 void *owner));
64 79
65 extern int message_get_attribute __P ((message_t, attribute_t *)); 80 extern int message_get_attribute __P ((message_t, attribute_t *));
66 extern int message_set_attribute __P ((message_t, attribute_t, void *owner)); 81 extern int message_set_attribute __P ((message_t, attribute_t, void *owner));
67 82
68 extern int message_clone __P ((message_t)); 83 //extern int message_clone __P ((message_t));
69 84
70 /* events */ 85 /* events */
71 #define MU_EVT_MSG_DESTROY 32 86 #define MU_EVT_MSG_DESTROY 32
......
...@@ -41,7 +41,7 @@ extern "C" { ...@@ -41,7 +41,7 @@ extern "C" {
41 struct _mime; 41 struct _mime;
42 typedef struct _mime *mime_t; 42 typedef struct _mime *mime_t;
43 43
44 int mime_init __P ((mime_t *pmime, message_t msg, int flags)); 44 int mime_create __P ((mime_t *pmime, message_t msg, int flags));
45 void mime_destroy __P ((mime_t *pmime)); 45 void mime_destroy __P ((mime_t *pmime));
46 int mime_is_multi_part __P ((mime_t mime)); 46 int mime_is_multi_part __P ((mime_t mime));
47 int mime_get_part __P ((mime_t mime, int part, message_t *msg)); 47 int mime_get_part __P ((mime_t mime, int part, message_t *msg));
......
...@@ -38,14 +38,14 @@ extern "C" { ...@@ -38,14 +38,14 @@ extern "C" {
38 struct url_registrar 38 struct url_registrar
39 { 39 {
40 const char *scheme; 40 const char *scheme;
41 int (*_init) __P ((url_t *, const char * name)); 41 int (*_create) __P ((url_t *, const char * name));
42 void (*_destroy) __P ((url_t *)); 42 void (*_destroy) __P ((url_t *));
43 }; 43 };
44 44
45 struct mailbox_registrar 45 struct mailbox_registrar
46 { 46 {
47 const char *name; 47 const char *name;
48 int (*_init) __P ((mailbox_t *, const char *name)); 48 int (*_create) __P ((mailbox_t *, const char *name));
49 void (*_destroy) __P ((mailbox_t *)); 49 void (*_destroy) __P ((mailbox_t *));
50 }; 50 };
51 51
......
...@@ -44,7 +44,7 @@ struct _transcoder ...@@ -44,7 +44,7 @@ struct _transcoder
44 void *tcdata; 44 void *tcdata;
45 }; 45 };
46 46
47 extern int transcode_init __P ((transcoder_t *, char *encoding)); 47 extern int transcode_create __P ((transcoder_t *, char *encoding));
48 extern void transcode_destroy __P ((transcoder_t *)); 48 extern void transcode_destroy __P ((transcoder_t *));
49 extern int transcode_get_stream __P ((transcoder_t tc, stream_t *pis)); 49 extern int transcode_get_stream __P ((transcoder_t tc, stream_t *pis));
50 extern int transcode_set_stream __P ((transcoder_t tc, stream_t is)); 50 extern int transcode_set_stream __P ((transcoder_t tc, stream_t is));
......
...@@ -36,7 +36,7 @@ extern "C" { ...@@ -36,7 +36,7 @@ extern "C" {
36 struct _url; 36 struct _url;
37 typedef struct _url * url_t; 37 typedef struct _url * url_t;
38 38
39 extern int url_init __P ((url_t *, const char *name)); 39 extern int url_create __P ((url_t *, const char *name));
40 extern void url_destroy __P ((url_t *)); 40 extern void url_destroy __P ((url_t *));
41 41
42 extern int url_get_id __P ((const url_t, int *id)); 42 extern int url_get_id __P ((const url_t, int *id));
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 #include <stdio.h> 22 #include <stdio.h>
23 23
24 int 24 int
25 stream_init (stream_t *pstream, void *owner) 25 stream_create (stream_t *pstream, void *owner)
26 { 26 {
27 stream_t stream; 27 stream_t stream;
28 if (pstream == NULL || owner == NULL) 28 if (pstream == NULL || owner == NULL)
......
...@@ -45,7 +45,7 @@ struct _locker ...@@ -45,7 +45,7 @@ struct _locker
45 }; 45 };
46 46
47 int 47 int
48 locker_init (locker_t *plocker, char *filename, size_t len, int flags) 48 locker_create (locker_t *plocker, char *filename, size_t len, int flags)
49 { 49 {
50 locker_t l; 50 locker_t l;
51 51
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
23 #include <message0.h> 23 #include <message0.h>
24 #include <registrar.h> 24 #include <registrar.h>
25 #include <locker.h> 25 #include <locker.h>
26 #include <net.h>
26 27
27 #include <stdlib.h> 28 #include <stdlib.h>
28 #include <string.h> 29 #include <string.h>
...@@ -32,23 +33,23 @@ ...@@ -32,23 +33,23 @@
32 * Point of entry. 33 * Point of entry.
33 * Simple, first check if they ask for something specific; with the ID. 34 * Simple, first check if they ask for something specific; with the ID.
34 * Then try to discover the type of mailbox with the url(name). 35 * Then try to discover the type of mailbox with the url(name).
35 * Then we call the appropriate mailbox_*type*_init() function. 36 * Then we call the appropriate mailbox_*type*_create() function.
36 */ 37 */
37 int 38 int
38 mailbox_init (mailbox_t *pmbox, const char *name, int id) 39 mailbox_create (mailbox_t *pmbox, const char *name, int id)
39 { 40 {
40 int status = EINVAL; 41 int status = EINVAL;
41 struct mailbox_registrar *mreg; 42 struct mailbox_registrar *mreg;
42 url_t url = NULL; 43 url_t url = NULL;
43 44
44 url_init (&url, name); 45 url_create (&url, name);
45 46
46 /* 1st guest: if an ID is specify, shortcut */ 47 /* 1st guest: if an ID is specify, shortcut */
47 if (id) 48 if (id)
48 { 49 {
49 status = registrar_get (id, NULL, &mreg); 50 status = registrar_get (id, NULL, &mreg);
50 if (status == 0) 51 if (status == 0)
51 status = mreg->_init (pmbox, name); 52 status = mreg->_create (pmbox, name);
52 } 53 }
53 /* 2nd fallback: Use the URL */ 54 /* 2nd fallback: Use the URL */
54 else if (url != NULL) 55 else if (url != NULL)
...@@ -56,7 +57,7 @@ mailbox_init (mailbox_t *pmbox, const char *name, int id) ...@@ -56,7 +57,7 @@ mailbox_init (mailbox_t *pmbox, const char *name, int id)
56 url_get_id (url, &id); 57 url_get_id (url, &id);
57 status = registrar_get (id, NULL, &mreg); 58 status = registrar_get (id, NULL, &mreg);
58 if (status == 0) 59 if (status == 0)
59 status = mreg->_init (pmbox, name); 60 status = mreg->_create (pmbox, name);
60 } 61 }
61 62
62 /* set the URL */ 63 /* set the URL */
...@@ -189,6 +190,24 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth) ...@@ -189,6 +190,24 @@ mailbox_get_auth (mailbox_t mbox, auth_t *pauth)
189 } 190 }
190 191
191 int 192 int
193 mailbox_set_netinstance (mailbox_t mbox, netinstance_t netinstance)
194 {
195 if (mbox == NULL)
196 return EINVAL;
197 mbox->netinstance = netinstance;
198 return 0;
199 }
200
201 int
202 mailbox_get_netinstance (mailbox_t mbox, netinstance_t *pnetinstance)
203 {
204 if (mbox == NULL || pnetinstance == NULL)
205 return EINVAL;
206 *pnetinstance = mbox->netinstance;
207 return 0;
208 }
209
210 int
192 mailbox_register (mailbox_t mbox, size_t type, 211 mailbox_register (mailbox_t mbox, size_t type,
193 int (*action) (size_t type, void *arg), 212 int (*action) (size_t type, void *arg),
194 void *arg) 213 void *arg)
......
...@@ -20,13 +20,13 @@ ...@@ -20,13 +20,13 @@
20 #include <errno.h> 20 #include <errno.h>
21 21
22 22
23 static int mailbox_imap_init (mailbox_t *mbox, const char *name); 23 static int mailbox_imap_create (mailbox_t *mbox, const char *name);
24 static void mailbox_imap_destroy (mailbox_t *mbox); 24 static void mailbox_imap_destroy (mailbox_t *mbox);
25 25
26 struct mailbox_registrar _mailbox_imap_registrar = 26 struct mailbox_registrar _mailbox_imap_registrar =
27 { 27 {
28 "IMAP4", 28 "IMAP4",
29 mailbox_imap_init, mailbox_imap_destroy 29 mailbox_imap_create, mailbox_imap_destroy
30 }; 30 };
31 31
32 void 32 void
...@@ -37,7 +37,7 @@ mailbox_imap_destroy (mailbox_t *mbox) ...@@ -37,7 +37,7 @@ mailbox_imap_destroy (mailbox_t *mbox)
37 } 37 }
38 38
39 int 39 int
40 mailbox_imap_init (mailbox_t *mbox, const char *name) 40 mailbox_imap_create (mailbox_t *mbox, const char *name)
41 { 41 {
42 (void)mbox; (void)name; 42 (void)mbox; (void)name;
43 return ENOSYS; 43 return ENOSYS;
......
...@@ -22,13 +22,13 @@ ...@@ -22,13 +22,13 @@
22 #include <errno.h> 22 #include <errno.h>
23 #include <sys/stat.h> 23 #include <sys/stat.h>
24 24
25 static int mailbox_mbox_init (mailbox_t *mbox, const char *name); 25 static int mailbox_mbox_create (mailbox_t *mbox, const char *name);
26 static void mailbox_mbox_destroy (mailbox_t *mbox); 26 static void mailbox_mbox_destroy (mailbox_t *mbox);
27 27
28 struct mailbox_registrar _mailbox_mbox_registrar = 28 struct mailbox_registrar _mailbox_mbox_registrar =
29 { 29 {
30 "UNIX_MBOX/Maildir/MMDF", 30 "UNIX_MBOX/Maildir/MMDF",
31 mailbox_mbox_init, mailbox_mbox_destroy 31 mailbox_mbox_create, mailbox_mbox_destroy
32 }; 32 };
33 33
34 /* 34 /*
...@@ -45,7 +45,7 @@ struct mailbox_registrar _mailbox_mbox_registrar = ...@@ -45,7 +45,7 @@ struct mailbox_registrar _mailbox_mbox_registrar =
45 */ 45 */
46 46
47 static int 47 static int
48 mailbox_mbox_init (mailbox_t *mbox, const char *name) 48 mailbox_mbox_create (mailbox_t *mbox, const char *name)
49 { 49 {
50 struct stat st; 50 struct stat st;
51 size_t len; 51 size_t len;
...@@ -69,7 +69,7 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name) ...@@ -69,7 +69,7 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name)
69 * For the default is unix if the file does not exist. 69 * For the default is unix if the file does not exist.
70 */ 70 */
71 if (stat (name, &st) < 0) 71 if (stat (name, &st) < 0)
72 return _mailbox_unix_registrar._init (mbox, name); 72 return _mailbox_unix_registrar._create (mbox, name);
73 73
74 if (S_ISREG (st.st_mode)) 74 if (S_ISREG (st.st_mode))
75 { 75 {
...@@ -101,7 +101,7 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name) ...@@ -101,7 +101,7 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name)
101 if (count == 0) /*empty file*/ 101 if (count == 0) /*empty file*/
102 { 102 {
103 close (fd); 103 close (fd);
104 return _mailbox_unix_registrar._init (mbox, name); 104 return _mailbox_unix_registrar._create (mbox, name);
105 } 105 }
106 106
107 if (count >= 5) 107 if (count >= 5)
...@@ -110,18 +110,18 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name) ...@@ -110,18 +110,18 @@ mailbox_mbox_init (mailbox_t *mbox, const char *name)
110 { 110 {
111 /* This is a Unix Mbox */ 111 /* This is a Unix Mbox */
112 close (fd); 112 close (fd);
113 return _mailbox_unix_registrar._init (mbox, name); 113 return _mailbox_unix_registrar._create (mbox, name);
114 } 114 }
115 } 115 }
116 116
117 /* Try MMDF */ 117 /* Try MMDF */
118 close (fd); 118 close (fd);
119 #endif 119 #endif
120 return _mailbox_unix_registrar._init (mbox, name); 120 return _mailbox_unix_registrar._create (mbox, name);
121 } 121 }
122 /* Is that true ? Are all directories Maildir ?? */ 122 /* Is that true ? Are all directories Maildir ?? */
123 else if (S_ISDIR (st.st_mode)) 123 else if (S_ISDIR (st.st_mode))
124 return _mailbox_maildir_registrar._init (mbox, name); 124 return _mailbox_maildir_registrar._create (mbox, name);
125 125
126 /* Why can't a mailbox be FIFO ? or a DOOR/Portal ??? */ 126 /* Why can't a mailbox be FIFO ? or a DOOR/Portal ??? */
127 return EINVAL; 127 return EINVAL;
......
...@@ -19,17 +19,17 @@ ...@@ -19,17 +19,17 @@
19 #include <registrar0.h> 19 #include <registrar0.h>
20 #include <errno.h> 20 #include <errno.h>
21 21
22 static int mailbox_maildir_init (mailbox_t *mbox, const char *name); 22 static int mailbox_maildir_create (mailbox_t *mbox, const char *name);
23 static void mailbox_maildir_destroy (mailbox_t *mbox); 23 static void mailbox_maildir_destroy (mailbox_t *mbox);
24 24
25 struct mailbox_registrar _mailbox_maildir_registrar = 25 struct mailbox_registrar _mailbox_maildir_registrar =
26 { 26 {
27 "MAILDIR", 27 "MAILDIR",
28 mailbox_maildir_init, mailbox_maildir_destroy 28 mailbox_maildir_create, mailbox_maildir_destroy
29 }; 29 };
30 30
31 int 31 int
32 mailbox_maildir_init (mailbox_t *mbox, const char *name) 32 mailbox_maildir_create (mailbox_t *mbox, const char *name)
33 { 33 {
34 (void)mbox; (void)name; 34 (void)mbox; (void)name;
35 return ENOSYS; 35 return ENOSYS;
......
...@@ -27,13 +27,13 @@ ...@@ -27,13 +27,13 @@
27 #include <stdlib.h> 27 #include <stdlib.h>
28 #include <ctype.h> 28 #include <ctype.h>
29 29
30 static int mailbox_mh_init (mailbox_t *pmbox, const char *name); 30 static int mailbox_mh_create (mailbox_t *pmbox, const char *name);
31 static void mailbox_mh_destroy (mailbox_t *pmbox); 31 static void mailbox_mh_destroy (mailbox_t *pmbox);
32 32
33 struct mailbox_registrar _mailbox_mh_registrar = 33 struct mailbox_registrar _mailbox_mh_registrar =
34 { 34 {
35 "MH", 35 "MH",
36 mailbox_mh_init, mailbox_mh_destroy 36 mailbox_mh_create, mailbox_mh_destroy
37 }; 37 };
38 38
39 typedef struct _mh_data 39 typedef struct _mh_data
...@@ -43,11 +43,11 @@ typedef struct _mh_data ...@@ -43,11 +43,11 @@ typedef struct _mh_data
43 43
44 static int mh_open (mailbox_t mbox, int flags); 44 static int mh_open (mailbox_t mbox, int flags);
45 static int mh_close (mailbox_t mbox); 45 static int mh_close (mailbox_t mbox);
46 static int mh_scan (mailbox_t mbox, size_t *msgs); 46 static int mh_scan (mailbox_t mbox, size_t msgno, size_t *msgs);
47 static int mh_sequence(const char *name); 47 static int mh_sequence(const char *name);
48 48
49 static int 49 static int
50 mailbox_mh_init (mailbox_t *pmbox, const char *name) 50 mailbox_mh_create (mailbox_t *pmbox, const char *name)
51 { 51 {
52 mailbox_t mbox; 52 mailbox_t mbox;
53 mh_data *data; 53 mh_data *data;
...@@ -57,10 +57,11 @@ mailbox_mh_init (mailbox_t *pmbox, const char *name) ...@@ -57,10 +57,11 @@ mailbox_mh_init (mailbox_t *pmbox, const char *name)
57 mbox->name = malloc(strlen(name) + 1); 57 mbox->name = malloc(strlen(name) + 1);
58 strcpy(mbox->name, name); 58 strcpy(mbox->name, name);
59 mbox->data = data; 59 mbox->data = data;
60 mbox->_init = mailbox_mh_init; 60 mbox->_create = mailbox_mh_create;
61 mbox->_destroy = mailbox_mh_destroy; 61 mbox->_destroy = mailbox_mh_destroy;
62 mbox->_open = mh_open; 62 mbox->_open = mh_open;
63 mbox->_close = mh_close; 63 mbox->_close = mh_close;
64 mbox->_scan = mh_scan;
64 *pmbox = mbox; 65 *pmbox = mbox;
65 66
66 return 0; 67 return 0;
...@@ -110,7 +111,7 @@ mh_close (mailbox_t mbox) ...@@ -110,7 +111,7 @@ mh_close (mailbox_t mbox)
110 } 111 }
111 112
112 static int 113 static int
113 mh_scan (mailbox_t mbox, size_t *msgs) 114 mh_scan (mailbox_t mbox, size_t msgno, size_t *msgs)
114 { 115 {
115 struct stat st; 116 struct stat st;
116 DIR *maildir; 117 DIR *maildir;
...@@ -119,6 +120,8 @@ mh_scan (mailbox_t mbox, size_t *msgs) ...@@ -119,6 +120,8 @@ mh_scan (mailbox_t mbox, size_t *msgs)
119 unsigned int count = 0; 120 unsigned int count = 0;
120 int parse_sequence_file = 0; 121 int parse_sequence_file = 0;
121 122
123 (void)msgno;
124
122 data = mbox->data; 125 data = mbox->data;
123 126
124 maildir = opendir(mbox->name); 127 maildir = opendir(mbox->name);
......
...@@ -20,17 +20,17 @@ ...@@ -20,17 +20,17 @@
20 20
21 #include <errno.h> 21 #include <errno.h>
22 22
23 static int mailbox_mmdf_init (mailbox_t *mbox, const char *name); 23 static int mailbox_mmdf_create (mailbox_t *mbox, const char *name);
24 static void mailbox_mmdf_destroy (mailbox_t *mbox); 24 static void mailbox_mmdf_destroy (mailbox_t *mbox);
25 25
26 struct mailbox_registrar _mailbox_mmdf_registrar = 26 struct mailbox_registrar _mailbox_mmdf_registrar =
27 { 27 {
28 "MMDF", 28 "MMDF",
29 mailbox_mmdf_init, mailbox_mmdf_destroy 29 mailbox_mmdf_create, mailbox_mmdf_destroy
30 }; 30 };
31 31
32 static int 32 static int
33 mailbox_mmdf_init (mailbox_t *mbox, const char *name) 33 mailbox_mmdf_create (mailbox_t *mbox, const char *name)
34 { 34 {
35 (void)mbox; (void)name; 35 (void)mbox; (void)name;
36 return ENOSYS; 36 return ENOSYS;
......
...@@ -20,13 +20,13 @@ ...@@ -20,13 +20,13 @@
20 20
21 #include <errno.h> 21 #include <errno.h>
22 22
23 static int mailbox_pop_init (mailbox_t *mbox, const char *name); 23 static int mailbox_pop_create (mailbox_t *mbox, const char *name);
24 static void mailbox_pop_destroy (mailbox_t *mbox); 24 static void mailbox_pop_destroy (mailbox_t *mbox);
25 25
26 struct mailbox_registrar _mailbox_pop_registrar = 26 struct mailbox_registrar _mailbox_pop_registrar =
27 { 27 {
28 "POP3", 28 "POP3",
29 mailbox_pop_init, mailbox_pop_destroy 29 mailbox_pop_create, mailbox_pop_destroy
30 }; 30 };
31 31
32 static void 32 static void
...@@ -37,7 +37,7 @@ mailbox_pop_destroy (mailbox_t *mbox) ...@@ -37,7 +37,7 @@ mailbox_pop_destroy (mailbox_t *mbox)
37 } 37 }
38 38
39 static int 39 static int
40 mailbox_pop_init (mailbox_t *mbox, const char *name) 40 mailbox_pop_create (mailbox_t *mbox, const char *name)
41 { 41 {
42 (void)mbox; (void)name; 42 (void)mbox; (void)name;
43 return ENOSYS; 43 return ENOSYS;
......
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
22 #include <message0.h> 22 #include <message0.h>
23 #include <url0.h> 23 #include <url0.h>
24 #include <io0.h> 24 #include <io0.h>
25 #include <attribute.h> 25 #include <body0.h>
26 #include <attribute0.h>
26 #include <header.h> 27 #include <header.h>
27 #include <auth.h> 28 #include <auth.h>
28 #include <locker.h> 29 #include <locker.h>
...@@ -48,13 +49,13 @@ ...@@ -48,13 +49,13 @@
48 #include <ctype.h> 49 #include <ctype.h>
49 #include <limits.h> 50 #include <limits.h>
50 51
51 static int mailbox_unix_init (mailbox_t *pmbox, const char *name); 52 static int mailbox_unix_create (mailbox_t *pmbox, const char *name);
52 static void mailbox_unix_destroy (mailbox_t *pmbox); 53 static void mailbox_unix_destroy (mailbox_t *pmbox);
53 54
54 struct mailbox_registrar _mailbox_unix_registrar = 55 struct mailbox_registrar _mailbox_unix_registrar =
55 { 56 {
56 "UNIX MBOX", 57 "UNIX MBOX",
57 mailbox_unix_init, mailbox_unix_destroy 58 mailbox_unix_create, mailbox_unix_destroy
58 }; 59 };
59 60
60 /* 61 /*
...@@ -64,13 +65,13 @@ struct mailbox_registrar _mailbox_unix_registrar = ...@@ -64,13 +65,13 @@ struct mailbox_registrar _mailbox_unix_registrar =
64 typedef struct _mailbox_unix_message 65 typedef struct _mailbox_unix_message
65 { 66 {
66 /* offset of the parts of the messages in the mailbox*/ 67 /* offset of the parts of the messages in the mailbox*/
67 off_t header; 68 off_t header_from;
68 off_t header_end; 69 off_t header_from_end;
69 /* little hack to make things easier 70 /* little hack to make things easier
70 * when updating the attribute 71 * when updating the attribute
71 */ 72 */
72 off_t hdr_status; 73 off_t header_status;
73 off_t hdr_status_end; 74 off_t header_status_end;
74 off_t body; 75 off_t body;
75 off_t body_end; 76 off_t body_end;
76 77
...@@ -79,11 +80,8 @@ typedef struct _mailbox_unix_message ...@@ -79,11 +80,8 @@ typedef struct _mailbox_unix_message
79 attribute_t old_attr; 80 attribute_t old_attr;
80 attribute_t new_attr; 81 attribute_t new_attr;
81 82
82 /* some sort of uniq id, maybe we should use the 83 size_t header_lines;
83 * Message-ID of the message, but we'll have to free 84 size_t body_lines;
84 * when done.
85 */
86 size_t num;
87 FILE *file; 85 FILE *file;
88 86
89 /* if we have a message attach to it */ 87 /* if we have a message attach to it */
...@@ -130,19 +128,20 @@ static int mailbox_unix_is_updated (mailbox_t); ...@@ -130,19 +128,20 @@ static int mailbox_unix_is_updated (mailbox_t);
130 128
131 static int mailbox_unix_size (mailbox_t, off_t *size); 129 static int mailbox_unix_size (mailbox_t, off_t *size);
132 130
133 static ssize_t mailbox_unix_get_header (mailbox_t, size_t msgno, char *h,
134 size_t len, off_t off, int *err);
135
136 131
137 /* private stuff */ 132 /* private stuff */
138 static int mailbox_unix_is_deleted (mailbox_t mbox, size_t msgno); 133
139 static int mailbox_unix_validity (mailbox_t mbox, size_t msgno); 134 static int mailbox_unix_get_header (mailbox_unix_message_t mum, char *buffer,
135 size_t len, off_t off, ssize_t *pnread);
140 static int mailbox_unix_getfd (stream_t is, int *pfd); 136 static int mailbox_unix_getfd (stream_t is, int *pfd);
141 static int mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, 137 static int mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
142 off_t off, size_t *pnread); 138 off_t off, size_t *pnread);
143 static int mailbox_unix_is_from (const char *); 139 static int mailbox_unix_body_size (body_t body, size_t *psize);
144 static int mailbox_unix_readhdr (mailbox_t mbox, char *buf, size_t len, 140 static int mailbox_unix_body_lines (body_t body, size_t *plines);
145 off_t *content_length, size_t msgno); 141 static int mailbox_unix_msg_from (message_t msg, char *buf, size_t len,
142 size_t *pnwrite);
143 static int mailbox_unix_msg_received (message_t msg, char *buf, size_t len,
144 size_t *pnwrite);
146 static int mailbox_unix_lock (mailbox_t mbox, int flag); 145 static int mailbox_unix_lock (mailbox_t mbox, int flag);
147 static int mailbox_unix_touchlock (mailbox_t mbox); 146 static int mailbox_unix_touchlock (mailbox_t mbox);
148 static int mailbox_unix_unlock (mailbox_t mbox); 147 static int mailbox_unix_unlock (mailbox_t mbox);
...@@ -156,7 +155,7 @@ static int mailbox_unix_iunlock (mailbox_t mbox); ...@@ -156,7 +155,7 @@ static int mailbox_unix_iunlock (mailbox_t mbox);
156 * Hopefully there will not be a mailbox name "unix:" 155 * Hopefully there will not be a mailbox name "unix:"
157 */ 156 */
158 static int 157 static int
159 mailbox_unix_init (mailbox_t *pmbox, const char *name) 158 mailbox_unix_create (mailbox_t *pmbox, const char *name)
160 { 159 {
161 mailbox_t mbox; 160 mailbox_t mbox;
162 mailbox_unix_data_t mud; 161 mailbox_unix_data_t mud;
...@@ -271,7 +270,7 @@ mailbox_unix_init (mailbox_t *pmbox, const char *name) ...@@ -271,7 +270,7 @@ mailbox_unix_init (mailbox_t *pmbox, const char *name)
271 pthread_mutex_init (&(mud->mutex), NULL); 270 pthread_mutex_init (&(mud->mutex), NULL);
272 #endif 271 #endif
273 272
274 mbox->_init = mailbox_unix_init; 273 mbox->_create = mailbox_unix_create;
275 mbox->_destroy = mailbox_unix_destroy; 274 mbox->_destroy = mailbox_unix_destroy;
276 275
277 mbox->_open = mailbox_unix_open; 276 mbox->_open = mailbox_unix_open;
...@@ -437,10 +436,11 @@ mailbox_unix_open (mailbox_t mbox, int flags) ...@@ -437,10 +436,11 @@ mailbox_unix_open (mailbox_t mbox, int flags)
437 if (mud->file == NULL) 436 if (mud->file == NULL)
438 { 437 {
439 /* what's up ?? */ 438 /* what's up ?? */
439 int err = errno;
440 mailbox_unix_iunlock (mbox); 440 mailbox_unix_iunlock (mbox);
441 return ENOMEM; 441 return err;
442 } 442 }
443 /* Is this necessary ?? */ 443 /* Is this check necessary ?? */
444 /* Check to make sure this is indeed a Unix Mail format */ 444 /* Check to make sure this is indeed a Unix Mail format */
445 flockfile (mud->file); 445 flockfile (mud->file);
446 { 446 {
...@@ -496,410 +496,8 @@ mailbox_unix_close (mailbox_t mbox) ...@@ -496,410 +496,8 @@ mailbox_unix_close (mailbox_t mbox)
496 return 0; 496 return 0;
497 } 497 }
498 498
499 499 /* Mailbox Parsing */
500 /* FIXME: a little weak, we should do full reconnaissance of the 500 #include "mbx_unixscan.c"
501 * the "From " header :
502 * From <user> <weekday> <month> <day> <hr:min:sec>
503 * [T1 [T2]] <year> [remote-list]
504 */
505 static int
506 mailbox_unix_is_from (const char *from)
507 {
508 const char *sep;
509
510 /* From_ */
511 if (strncmp (from, "From ", 5) != 0)
512 return 0;
513 from += 5;
514
515 /* <user> */
516 sep = strchr (from, ' ');
517 if (sep == NULL)
518 return 0;
519 from = ++sep;
520
521 /* weekday */
522 sep = strchr (from, ' ');
523 if (sep == NULL)
524 return 0;
525 from = ++sep;
526
527 /* month */
528 sep = strchr (from, ' ');
529 if (sep == NULL)
530 return 0;
531 from = ++sep;
532
533 /* day */
534 sep = strchr (from, ' ');
535 if (sep == NULL)
536 return 0;
537 from = ++sep;
538
539 /* hr:min:sec */
540 /* hr */
541 sep = strchr (from, ':');
542 if (sep == NULL)
543 return 0;
544 from = ++sep;
545 /* min */
546 sep = strchr (from, ':');
547 if (sep == NULL)
548 return 0;
549 from = ++sep ;
550 /* sec */
551 sep = strchr (from, ' ');
552 if (sep == NULL)
553 return 0;
554
555 /* FIXME PLEASE: the rest is getting more complex, finish it later */
556 return 1;
557 }
558
559 /*
560 * We skip over the rest of the header. Scan for
561 * Status: to set the attribute. Hopefully the Content-Length
562 * in there too.
563 */
564 static int
565 mailbox_unix_readhdr (mailbox_t mbox, char *buf, size_t len,
566 off_t *content_length, size_t msgno)
567 {
568 mailbox_unix_data_t mud = (mailbox_unix_data_t)mbox->data;
569 mailbox_unix_message_t mum = mud->umessages[msgno];
570 char *sep;
571
572 /* skip over the remaining header */
573 errno = 0;
574 while (fgets (buf, len, mud->file))
575 {
576 /* FIXME: The heuristic is still weak
577 we will break and consider things not to be a header if:
578 1: an empty line
579 2: can not be a header
580 */
581 if (strcmp (buf, "\n") == 0 || strcmp (buf, "\r\n") == 0
582 || (isalpha (buf[0]) && (sep = strchr (buf, ':')) == NULL))
583 break;
584 /* get the the Content lenght of the body if possible */
585 else if (strncmp (buf, "Content-Length:", 15) == 0)
586 {
587 sep = strchr(buf, ':'); /* pass the ':' */
588 sep[strlen (sep) - 1] = '\0'; /* chop the newline */
589 /* FIXME: use xstrtol() if strtol() is not on the platform */
590 /* FIXME: what an awkward way of handling error
591 Some damage control can be done here.
592 for example just set content_length = -1;
593 and rely above to discover the body part */
594 errno = 0;
595 *content_length = strtol (sep + 1, NULL, 10);
596 if (*content_length == 0 ||
597 *content_length == LONG_MIN ||
598 *content_length == LONG_MAX)
599 {
600 if (errno != 0)
601 return errno;
602 }
603 }
604 /* Set the attribute */
605 else if (strncasecmp (buf, "Status:", 7) == 0)
606 {
607 mum->hdr_status_end = ftell (mud->file);
608 mum->hdr_status = mum->hdr_status_end - strlen (buf);
609 sep = strchr(buf, ':'); /* pass the ':' */
610 if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
611 attribute_set_read (mum->old_attr);
612 if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
613 attribute_set_seen (mum->old_attr);
614 if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL)
615 attribute_set_answered (mum->old_attr);
616 if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL)
617 attribute_set_flagged (mum->old_attr);
618 attribute_copy (mum->new_attr, mum->old_attr);
619 }
620 }
621 /* check for any dubious conditions */
622 if (feof (mud->file) || ferror (mud->file))
623 return errno;
624 return 0;
625 }
626
627 /* Parsing.
628 * This a bit fragile, I need to secure this.
629 * The approach is to detect the "From " as start of a
630 * new message, give the position of the header and scan
631 * until "\n" then set header_end, set body position, if we have
632 * a Content-Length field jump to the point if not
633 * scan until we it another "From " and set body_end.
634 */
635 static int
636 mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
637 {
638 char buf[BUFSIZ];
639 int header = 0;
640 int body = 0;
641 off_t content_length = -1;
642 mailbox_unix_data_t mud;
643 mailbox_unix_message_t mum = NULL;
644 struct stat st;
645 int status;
646 size_t progress_counter = 0;
647
648 /* sanity */
649 if (mbox == NULL ||
650 (mud = (mailbox_unix_data_t)mbox->data) == NULL)
651 return EINVAL;
652
653 /* FIXME: I should also block signals and check cancelstate(Pthread)
654 since We can not afford to be intr */
655 mailbox_unix_ilock (mbox, MU_LOCKER_WRLOCK);
656 mailbox_unix_lock (mbox, MU_LOCKER_RDLOCK);
657 flockfile (mud->file);
658
659 if (fstat (fileno (mud->file), &st) != 0)
660 {
661 status = errno;
662 funlockfile (mud->file);
663 mailbox_unix_iunlock (mbox);
664 mailbox_unix_unlock (mbox);
665 return status;
666 }
667 mud->mtime = st.st_mtime;
668 mud->size = st.st_size;
669
670 rewind (mud->file);
671
672 /* seek to the starting point */
673 if (mud->umessages)
674 {
675 if (mud->messages_count > 0 &&
676 msgno != 0 &&
677 msgno <= mud->messages_count)
678 {
679 mum = mud->umessages[msgno - 1];
680 if (mum)
681 fseek (mud->file, mum->body_end, SEEK_SET);
682 mud->messages_count = msgno;
683 }
684 }
685
686 errno = 0;
687 while (fgets (buf, sizeof (buf), mud->file))
688 {
689 if (mailbox_unix_is_from (buf))
690 {
691 if (body && mum )
692 {
693 int over = strlen (buf);
694 mum->body_end = ftell (mud->file);
695 mum->body_end -= (over);
696 body = 0;
697 mud->messages_count++;
698 /* reset the progress_counter */
699 progress_counter = 0;
700 /* notifications ADD_MESG */
701 {
702 off_t where = ftell (mud->file);
703 funlockfile (mud->file);
704 mailbox_unix_iunlock (mbox);
705 mailbox_unix_unlock (mbox);
706 /*
707 * My! I do not like this way of bailing out.
708 */
709 if (mailbox_notification (mbox, MU_EVT_MBX_MSG_ADD) != 0)
710 {
711 if (pcount)
712 *pcount = mud->messages_count;
713 return EINTR;
714 }
715 flockfile (mud->file);
716 mailbox_unix_ilock (mbox, MU_LOCKER_WRLOCK);
717 mailbox_unix_lock (mbox, MU_LOCKER_RDLOCK);
718 fseek (mud->file, where, SEEK_SET);
719 }
720 }
721 header = 1;
722 }
723
724 /* header */
725 if (header)
726 {
727 /* FIXME: What happen if some mailer violates the rfc822 and the
728 "From " field contains a NULL byte */
729 int over = strlen (buf);
730
731 /* allocate a slot for the new message */
732 if (mud->messages_count >= mud->umessages_count)
733 {
734 mailbox_unix_message_t *m;
735 m = realloc (mud->umessages,
736 (mud->messages_count + 1) * sizeof (*m));
737 if (m == NULL)
738 {
739 funlockfile (mud->file);
740 mailbox_unix_iunlock (mbox);
741 mailbox_unix_unlock (mbox);
742 return ENOMEM;
743 }
744 mud->umessages = m;
745 mud->umessages[mud->umessages_count] = NULL;
746 mud->umessages_count++;
747 }
748 /* allocate space for the new message */
749 if (mud->umessages[mud->messages_count] == NULL)
750 {
751 mum = calloc (1, sizeof (*mum));
752 if (mum == NULL)
753 {
754 funlockfile (mud->file);
755 mailbox_unix_iunlock (mbox);
756 mailbox_unix_unlock (mbox);
757 return ENOMEM;
758 }
759 mud->umessages[mud->messages_count] = mum;
760 }
761 else
762 {
763 /* reuse the message */
764 mum = mud->umessages[mud->messages_count];
765 }
766 status = attribute_init (&(mum->old_attr), mbox);
767 if (status != 0)
768 {
769 funlockfile (mud->file);
770 mailbox_unix_iunlock (mbox);
771 mailbox_unix_unlock (mbox);
772 return ENOMEM;
773 }
774 status = attribute_init (&(mum->new_attr), mbox);
775 if (status != 0)
776 {
777 attribute_destroy (&(mum->old_attr), mbox);
778 funlockfile (mud->file);
779 mailbox_unix_iunlock (mbox);
780 mailbox_unix_unlock (mbox);
781 return ENOMEM;
782 }
783 mum->file = mud->file;
784 mum->num = mud->messages_count + 1;
785 mum->header = ftell (mud->file);
786 /* substract the overrun */
787 mum->header -= over;
788
789 /* skip the remaining header and set the attributes */
790 if ((status = mailbox_unix_readhdr (mbox, buf,
791 sizeof (buf), &content_length,
792 mud->messages_count)) != 0)
793 {
794 funlockfile (mud->file);
795 mailbox_unix_iunlock (mbox);
796 mailbox_unix_unlock (mbox);
797 return status;
798 }
799 mum->header_end = ftell (mud->file) - strlen(buf);
800 header = 0;
801 body = !header;
802 } /* header */
803
804 /* body */
805 if (body)
806 {
807 /* set the body position */
808 if (mum->body == 0)
809 mum->body = ftell (mud->file);
810
811 if (content_length >= 0)
812 {
813 /* ouf ! we got the lenght, jump */
814 mum->body_end = mum->body + content_length;
815 fseek (mud->file, content_length, SEEK_CUR);
816 content_length = -1;
817 body = 0;
818 mud->messages_count++;
819 /* reset the progress_counter */
820 progress_counter = 0;
821 /* notification ADD MESG*/
822 {
823 off_t where = ftell (mud->file);
824 funlockfile (mud->file);
825 mailbox_unix_iunlock (mbox);
826 mailbox_unix_unlock (mbox);
827 /*
828 * Brian E. are sure this is a good idea ??
829 */
830 if (mailbox_notification (mbox, MU_EVT_MBX_MSG_ADD) != 0)
831 {
832 if (pcount)
833 *pcount = mud->messages_count;
834 return EINTR;
835 }
836 flockfile (mud->file);
837 mailbox_unix_ilock (mbox, MU_LOCKER_WRLOCK);
838 mailbox_unix_lock (mbox, MU_LOCKER_RDLOCK);
839 fseek (mud->file, where, SEEK_SET);
840 }
841 }
842 }
843 /* notification MBX_PROGRESS
844 * We do not want to fire up the progress notification
845 * every line, it will be too expensive, so we do it
846 * arbitrarely every 10 000 Lines.
847 * FIXME: maybe this should be configurable.
848 */
849 if ((++progress_counter % 10000) == 0)
850 {
851 off_t where = ftell (mud->file);
852 funlockfile (mud->file);
853 mailbox_unix_iunlock (mbox);
854 mailbox_unix_unlock (mbox);
855 /*
856 * This is more tricky we can not leave the mum
857 * struct incomplete. If they want to bailout
858 * they probably did not care about the last message
859 * we should free it;
860 */
861 if (mailbox_notification (mbox, MU_EVT_MBX_PROGRESS) != 0)
862 {
863 if (mum)
864 {
865 attribute_destroy (&(mum->old_attr), mbox);
866 attribute_destroy (&(mum->new_attr), mbox);
867 message_destroy (&(mum->message), mbox);
868 free (mum);
869 mud->umessages[mud->messages_count - 1] = NULL;
870 mud->messages_count--;
871 }
872 if (pcount)
873 *pcount = mud->messages_count;
874 return EINTR;
875 }
876 flockfile (mud->file);
877 mailbox_unix_ilock (mbox, MU_LOCKER_WRLOCK);
878 mailbox_unix_lock (mbox, MU_LOCKER_RDLOCK);
879 fseek (mud->file, where, SEEK_SET);
880 }
881 mailbox_unix_touchlock (mbox);
882 } /* while */
883 status = errno;
884
885 if (feof (mud->file))
886 status = 0;
887
888 clearerr (mud->file);
889 if (mum)
890 {
891 mum->body_end = ftell (mud->file);
892 mud->messages_count++;
893 }
894 rewind (mud->file);
895 funlockfile (mud->file);
896 mailbox_unix_iunlock (mbox);
897 mailbox_unix_unlock (mbox);
898 if (pcount)
899 *pcount = mud->messages_count;
900 mailbox_notification (mbox, MU_EVT_MBX_MSG_ADD);
901 return status;
902 }
903 501
904 /* FIXME: How to handle a shrink ? meaning, the &^$^@%#@^& user 502 /* FIXME: How to handle a shrink ? meaning, the &^$^@%#@^& user
905 * start two browsers and delete files in one. My views 503 * start two browsers and delete files in one. My views
...@@ -911,38 +509,13 @@ mailbox_unix_is_updated (mailbox_t mbox) ...@@ -911,38 +509,13 @@ mailbox_unix_is_updated (mailbox_t mbox)
911 { 509 {
912 mailbox_unix_data_t mud; 510 mailbox_unix_data_t mud;
913 struct stat st; 511 struct stat st;
914 if (mbox == NULL || 512 if ((mud = (mailbox_unix_data_t)mbox->data) == NULL ||
915 (mud = (mailbox_unix_data_t)mbox->data) == NULL || 513 fstat (fileno (mud->file), &st) < 0)
916 fstat (fileno (mud->file), &st) < 0)
917 return 0; 514 return 0;
918 return (mud->mtime == st.st_mtime); 515 return (mud->mtime == st.st_mtime);
919 } 516 }
920 517
921 static int 518 static int
922 mailbox_unix_validity (mailbox_t mbox, size_t msgno)
923 {
924 mailbox_unix_data_t mud;
925 if (mbox == NULL ||
926 (mud = (mailbox_unix_data_t) mbox->data) == NULL)
927 return EINVAL;
928 /* valid ? */
929 return !(mud->messages_count > 0 && msgno > 0 &&
930 msgno <= mud->messages_count);
931 }
932
933 static int
934 mailbox_unix_is_deleted (mailbox_t mbox, size_t msgno)
935 {
936 mailbox_unix_data_t mud;
937 int status = mailbox_unix_validity (mbox, msgno);
938 if (status != 0)
939 return status;
940 msgno--;
941 mud = (mailbox_unix_data_t) mbox->data;
942 return attribute_is_deleted (mud->umessages[msgno]->new_attr);
943 }
944
945 static int
946 mailbox_unix_num_deleted (mailbox_t mbox, size_t *num) 519 mailbox_unix_num_deleted (mailbox_t mbox, size_t *num)
947 { 520 {
948 mailbox_unix_data_t mud; 521 mailbox_unix_data_t mud;
...@@ -1065,6 +638,17 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1065,6 +638,17 @@ mailbox_unix_expunge (mailbox_t mbox)
1065 mailbox_unix_ilock (mbox, MU_LOCKER_RDLOCK); 638 mailbox_unix_ilock (mbox, MU_LOCKER_RDLOCK);
1066 flockfile (mud->file); 639 flockfile (mud->file);
1067 640
641 /*
642 * We can not be NONBLOCKING here.
643 * It would irresponsable.
644 */
645 if ((oflags = fcntl (fileno (mud->file), F_GETFL, 0)) < 0)
646 {
647 status = errno;
648 goto bailout;
649 }
650 fcntl (fileno (mud->file), F_SETFL, oflags & ~O_NONBLOCK);
651
1068 /* Do we have a consistent view of the mailbox */ 652 /* Do we have a consistent view of the mailbox */
1069 /* FIXME: this is not enough we can do better 653 /* FIXME: this is not enough we can do better
1070 * - by checking the file size and scream bloody murder 654 * - by checking the file size and scream bloody murder
...@@ -1078,17 +662,6 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1078,17 +662,6 @@ mailbox_unix_expunge (mailbox_t mbox)
1078 goto bailout; 662 goto bailout;
1079 } 663 }
1080 664
1081 /*
1082 * We can not be NONBLOCKING here.
1083 * It would irresponsable.
1084 */
1085 if ((oflags = fcntl (fileno (mud->file), F_GETFL, 0)) < 0)
1086 {
1087 status = errno;
1088 goto bailout;
1089 }
1090 fcntl (fileno (mud->file), F_SETFL, oflags & ~O_NONBLOCK);
1091
1092 rewind (mud->file); 665 rewind (mud->file);
1093 666
1094 /* Go to the first mail with an attribute change */ 667 /* Go to the first mail with an attribute change */
...@@ -1108,7 +681,7 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1108,7 +681,7 @@ mailbox_unix_expunge (mailbox_t mbox)
1108 } 681 }
1109 682
1110 /* set the marker position */ 683 /* set the marker position */
1111 total = marker = mud->umessages[j]->header; 684 total = marker = mud->umessages[j]->header_from;
1112 685
1113 /* copy to tempfile emails not mark changed */ 686 /* copy to tempfile emails not mark changed */
1114 for (first = 1, i = j; i < mud->messages_count; i++) 687 for (first = 1, i = j; i < mud->messages_count; i++)
...@@ -1130,17 +703,17 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1130,17 +703,17 @@ mailbox_unix_expunge (mailbox_t mbox)
1130 total++; 703 total++;
1131 } 704 }
1132 /* copy the header */ 705 /* copy the header */
1133 if (fseek (mud->file, mum->header, SEEK_SET) == -1) 706 if (fseek (mud->file, mum->header_from, SEEK_SET) == -1)
1134 { 707 {
1135 status = errno; 708 status = errno;
1136 goto bailout; 709 goto bailout;
1137 } 710 }
1138 /* attribute change ? */ 711 /* attribute change ? */
1139 if (! attribute_is_equal (mum->old_attr, mum->new_attr) && 712 if (! attribute_is_equal (mum->old_attr, mum->new_attr) &&
1140 mum->hdr_status > mum->header) 713 mum->header_status > mum->header_from)
1141 { 714 {
1142 len = mum->hdr_status - mum->header; 715 len = mum->header_status - mum->header_from;
1143 current = mum->hdr_status_end; 716 current = mum->header_status_end;
1144 while (len > 0) 717 while (len > 0)
1145 { 718 {
1146 nread = (len < sizeof (buffer)) ? len : sizeof (buffer); 719 nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
...@@ -1190,9 +763,9 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1190,9 +763,9 @@ mailbox_unix_expunge (mailbox_t mbox)
1190 } 763 }
1191 else /* attribute did not change */ 764 else /* attribute did not change */
1192 { 765 {
1193 current = mum->header; 766 current = mum->header_from;
1194 } 767 }
1195 len = mum->header_end - current; 768 len = mum->body - current;
1196 while (len > 0) 769 while (len > 0)
1197 { 770 {
1198 nread = (len < sizeof (buffer)) ? len : sizeof (buffer); 771 nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
...@@ -1207,8 +780,8 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1207,8 +780,8 @@ mailbox_unix_expunge (mailbox_t mbox)
1207 } 780 }
1208 781
1209 /* Separate the header from body */ 782 /* Separate the header from body */
1210 fputc ('\n', tempfile); 783 /*fputc ('\n', tempfile); */
1211 total++; 784 /*total++;*/
1212 785
1213 /* copy the body */ 786 /* copy the body */
1214 if (fseek (mud->file, mum->body, SEEK_SET) < 0) 787 if (fseek (mud->file, mum->body, SEEK_SET) < 0)
...@@ -1281,8 +854,8 @@ mailbox_unix_expunge (mailbox_t mbox) ...@@ -1281,8 +854,8 @@ mailbox_unix_expunge (mailbox_t mbox)
1281 { 854 {
1282 if (errno == EAGAIN || errno == EINTR) 855 if (errno == EAGAIN || errno == EINTR)
1283 { 856 {
1284 continue;
1285 errno = 0; 857 errno = 0;
858 continue;
1286 } 859 }
1287 status = errno; 860 status = errno;
1288 goto bailout; 861 goto bailout;
...@@ -1352,19 +925,13 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, ...@@ -1352,19 +925,13 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
1352 { 925 {
1353 nread = ((size_t)ln < buflen) ? ln : buflen; 926 nread = ((size_t)ln < buflen) ? ln : buflen;
1354 /* position the file pointer and the buffer */ 927 /* position the file pointer and the buffer */
1355 if (fseek (mum->file, mum->body + off, SEEK_SET) < 0) 928 if (fseek (mum->file, mum->body + off, SEEK_SET) < 0 ||
1356 { 929 fread (buffer, sizeof (*buffer), nread, mum->file) != nread)
1357 funlockfile (mum->file);
1358 return errno;
1359 }
1360 if (fread (buffer, sizeof (*buffer), nread, mum->file) != nread)
1361 { 930 {
1362 funlockfile (mum->file); 931 funlockfile (mum->file);
1363 return errno; 932 return errno;
1364 } 933 }
1365 } 934 }
1366 else
1367 nread = 0;
1368 } 935 }
1369 funlockfile (mum->file); 936 funlockfile (mum->file);
1370 937
...@@ -1374,56 +941,146 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen, ...@@ -1374,56 +941,146 @@ mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
1374 } 941 }
1375 942
1376 static int 943 static int
1377 mailbox_unix_get_header (mailbox_t mbox, size_t msgno, char *buffer, 944 mailbox_unix_get_header (mailbox_unix_message_t mum, char *buffer,
1378 size_t len, off_t off, ssize_t *pnread) 945 size_t len, off_t off, ssize_t *pnread)
1379 { 946 {
1380 mailbox_unix_data_t mud;
1381 size_t nread = 0; 947 size_t nread = 0;
1382 /* check if valid */ 948 FILE *file = mum->file;
1383 int status = mailbox_unix_validity (mbox, msgno);
1384 if (status != 0)
1385 return status;
1386 949
1387 mud = (mailbox_unix_data_t) mbox->data; 950 flockfile (file);
1388 msgno--;
1389
1390 if (buffer == NULL || len == 0)
1391 {
1392 if (pnread)
1393 *pnread = nread;
1394 return 0;
1395 }
1396
1397 mailbox_unix_ilock (mbox, MU_LOCKER_RDLOCK);
1398 flockfile (mud->file);
1399 { 951 {
1400 mailbox_unix_message_t mum = mud->umessages[msgno]; 952 off_t ln = mum->body - (mum->header_from_end + off);
1401 off_t ln = mum->header_end - (mum->header + off);
1402 if (ln > 0) 953 if (ln > 0)
1403 { 954 {
1404 nread = ((size_t)ln < len) ? ln : len; 955 nread = ((size_t)ln < len) ? ln : len;
1405 /* position the file pointer and the buffer */ 956 /* position the file pointer and the buffer */
1406 if (fseek (mud->file, mum->header + off, SEEK_SET) < 0) 957 if (fseek (file, mum->header_from_end + off, SEEK_SET) < 0 ||
1407 { 958 fread (buffer, sizeof (*buffer), nread, file) != nread)
1408 funlockfile (mud->file);
1409 mailbox_unix_iunlock (mbox);
1410 return errno;
1411 }
1412 if (fread (buffer, sizeof (*buffer), nread, mud->file) != nread)
1413 { 959 {
1414 funlockfile (mud->file); 960 funlockfile (file);
1415 mailbox_unix_iunlock (mbox);
1416 return errno; 961 return errno;
1417 } 962 }
1418 } 963 }
1419 else
1420 nread = 0;
1421 } 964 }
1422 funlockfile (mud->file); 965 funlockfile (file);
1423 mailbox_unix_iunlock (mbox);
1424 966
1425 if (pnread) 967 *pnread = nread;
1426 *pnread = nread; 968 return 0;
969 }
970
971 static int
972 mailbox_unix_body_size (body_t body, size_t *psize)
973 {
974 mailbox_unix_message_t mum = body->owner;
975 if (mum == NULL)
976 return EINVAL;
977 if (psize)
978 *psize = mum->body_end - mum->body;
979 return 0;
980 }
981
982 static int
983 mailbox_unix_body_lines (body_t body, size_t *plines)
984 {
985 mailbox_unix_message_t mum = body->owner;
986 if (mum == NULL)
987 return EINVAL;
988 if (plines)
989 *plines = mum->body_lines;
990 return 0;
991 }
992
993 static int
994 mailbox_unix_msg_received (message_t msg, char *buf, size_t len,
995 size_t *pnwrite)
996 {
997 mailbox_unix_message_t mum = msg->owner;
998 char buffer[512];
999
1000 if (mum == NULL)
1001 return EINVAL;
1002
1003 flockfile (mum->file);
1004 {
1005 if (fseek (mum->file, mum->header_from, SEEK_SET) < 0 ||
1006 fgets (buffer, sizeof (buffer), mum->file) == NULL)
1007 {
1008 if (pnwrite)
1009 *pnwrite = 0;
1010 if (buf)
1011 *buf = '\0';
1012 return errno;
1013 }
1014 }
1015 funlockfile (mum->file);
1016
1017 if (strlen (buffer) > 5)
1018 {
1019 char *s;
1020 s = strchr (buffer + 5, ' ');
1021 if (s)
1022 {
1023 if (buf && len > 0)
1024 {
1025 strncpy (buf, s + 1, len);
1026 buffer [len - 1] = '\0';
1027 }
1028 if (pnwrite)
1029 *pnwrite = strlen (s);
1030 return 0;
1031 }
1032 }
1033 if (pnwrite)
1034 *pnwrite = 0;
1035 if (buf)
1036 *buf = '\0';
1037 return 0;
1038 }
1039
1040 static int
1041 mailbox_unix_msg_from (message_t msg, char *buf, size_t len, size_t *pnwrite)
1042 {
1043 mailbox_unix_message_t mum = msg->owner;
1044 char buffer[512];
1045
1046 if (mum == NULL)
1047 return EINVAL;
1048
1049 flockfile (mum->file);
1050 {
1051 if (fseek (mum->file, mum->header_from, SEEK_SET) < 0 ||
1052 fgets (buffer, sizeof (buffer), mum->file) == NULL)
1053 {
1054 if (pnwrite)
1055 *pnwrite = 0;
1056 if (buf)
1057 *buf = '\0';
1058 return errno;
1059 }
1060 }
1061 funlockfile (mum->file);
1062
1063 if (strlen (buffer) > 5)
1064 {
1065 char *s;
1066 s = strchr (buffer + 5, ' ');
1067 if (s)
1068 {
1069 *s = '\0';
1070 if (buf && len > 0)
1071 {
1072 strncpy (buf, buffer + 5, len);
1073 buffer [len - 1] = '\0';
1074 }
1075 if (pnwrite)
1076 *pnwrite = strlen (buffer + 5);
1077 return 0;
1078 }
1079 }
1080 if (pnwrite)
1081 *pnwrite = 0;
1082 if (buf)
1083 *buf = '\0';
1427 return 0; 1084 return 0;
1428 } 1085 }
1429 1086
...@@ -1441,9 +1098,12 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) ...@@ -1441,9 +1098,12 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
1441 message_t msg = NULL; 1098 message_t msg = NULL;
1442 stream_t stream = NULL; 1099 stream_t stream = NULL;
1443 header_t header = NULL; 1100 header_t header = NULL;
1101 body_t body = NULL;
1444 1102
1445 if (mbox == NULL || pmsg == NULL || 1103 if (mbox == NULL || pmsg == NULL ||
1446 (mud = (mailbox_unix_data_t)mbox->data) == NULL) 1104 (mud = (mailbox_unix_data_t)mbox->data) == NULL ||
1105 (!(mud->messages_count > 0 && msgno > 0 &&
1106 msgno <= mud->messages_count)))
1447 return EINVAL; 1107 return EINVAL;
1448 1108
1449 mum = mud->umessages[msgno - 1]; 1109 mum = mud->umessages[msgno - 1];
...@@ -1457,32 +1117,32 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) ...@@ -1457,32 +1117,32 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
1457 } 1117 }
1458 1118
1459 /* get the headers */ 1119 /* get the headers */
1460 do { 1120 do
1461 status = mailbox_unix_get_header (mbox, msgno, buf, sizeof(buf), 1121 {
1462 offset, &nread); 1122 status = mailbox_unix_get_header (mum, buf, sizeof(buf), offset, &nread);
1463 if (status != 0) 1123 if (status != 0)
1464 { 1124 {
1465 free (pbuf); 1125 free (pbuf);
1466 return status; 1126 return status;
1467 } 1127 }
1468 1128
1469 if (nread == 0) 1129 if (nread == 0)
1470 break; 1130 break;
1471 1131
1472 tbuf = realloc (pbuf, offset + nread); 1132 tbuf = realloc (pbuf, offset + nread);
1473 if (tbuf == NULL) 1133 if (tbuf == NULL)
1474 { 1134 {
1475 free (pbuf); 1135 free (pbuf);
1476 return ENOMEM; 1136 return ENOMEM;
1477 } 1137 }
1478 else 1138 else
1479 pbuf = tbuf; 1139 pbuf = tbuf;
1480 memcpy (pbuf + offset, buf, nread); 1140 memcpy (pbuf + offset, buf, nread);
1481 offset += nread; 1141 offset += nread;
1482 } while (nread > 0); 1142 } while (nread > 0);
1483 1143
1484 /* get an empty message struct */ 1144 /* get an empty message struct */
1485 status = message_init (&msg, mum); 1145 status = message_create (&msg, mum);
1486 if (status != 0) 1146 if (status != 0)
1487 { 1147 {
1488 free (pbuf); 1148 free (pbuf);
...@@ -1490,8 +1150,8 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) ...@@ -1490,8 +1150,8 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
1490 } 1150 }
1491 1151
1492 /* set the header */ 1152 /* set the header */
1493 if ((status = header_init (&header, pbuf, offset, mum)) != 0 || 1153 status = header_create (&header, pbuf, offset, mum);
1494 (status = message_set_header (msg, header, mum)) != 0) 1154 if (status != 0)
1495 { 1155 {
1496 free (pbuf); 1156 free (pbuf);
1497 message_destroy (&msg, mum); 1157 message_destroy (&msg, mum);
...@@ -1500,33 +1160,39 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) ...@@ -1500,33 +1160,39 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
1500 free (pbuf); 1160 free (pbuf);
1501 message_set_header (msg, header, mum); 1161 message_set_header (msg, header, mum);
1502 1162
1503 /* prepare the stream */ 1163 /* prepare the body */
1504 status = stream_init (&stream, mum); 1164 status = body_create (&body, mum);
1505 if (status != 0) 1165 if (status != 0)
1506 { 1166 {
1507 message_destroy (&msg, mum); 1167 message_destroy (&msg, mum);
1508 return status; 1168 return status;
1509 } 1169 }
1510 stream_set_read (stream, mailbox_unix_readstream, mum); 1170 status = stream_create (&stream, mum);
1511 stream_set_fd (stream, mailbox_unix_getfd, mum);
1512 message_set_stream (msg, stream, mum);
1513
1514 /* set the attribute */
1515 status = message_set_attribute (msg, mum->new_attr, mum);
1516 if (status != 0) 1171 if (status != 0)
1517 { 1172 {
1518 message_destroy (&msg, mum); 1173 message_destroy (&msg, mum);
1519 return status; 1174 return status;
1520 } 1175 }
1176 stream_set_read (stream, mailbox_unix_readstream, mum);
1177 stream_set_fd (stream, mailbox_unix_getfd, mum);
1178 body_set_stream (body, stream, mum);
1179 body_set_size (body, mailbox_unix_body_size, mum);
1180 /* set the line */
1181 body_set_lines (body, mailbox_unix_body_lines, mum);
1182 message_set_body (msg, body, mum);
1521 1183
1522 /* set the size */ 1184 /* set the attribute */
1523 status = message_set_size (msg, mum->body_end - mum->header, mum); 1185 status = message_set_attribute (msg, mum->new_attr, mum);
1524 if (status != 0) 1186 if (status != 0)
1525 { 1187 {
1526 message_destroy (&msg, mum); 1188 message_destroy (&msg, mum);
1527 return status; 1189 return status;
1528 } 1190 }
1529 1191
1192 /* set the envelope */
1193 message_set_from (msg, mailbox_unix_msg_from, mum);
1194 message_set_received (msg, mailbox_unix_msg_received, mum);
1195
1530 /* attach the message to the mailbox unix data */ 1196 /* attach the message to the mailbox unix data */
1531 mum->message = msg; 1197 mum->message = msg;
1532 1198
...@@ -1555,12 +1221,13 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) ...@@ -1555,12 +1221,13 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
1555 struct stat st; 1221 struct stat st;
1556 int fd; 1222 int fd;
1557 char buffer[BUFSIZ]; 1223 char buffer[BUFSIZ];
1558 size_t nread; 1224 size_t nread = 0;
1559 off_t off = 0; 1225 off_t off = 0;
1560 stream_t is; 1226 stream_t is;
1561 header_t hdr; 1227 header_t hdr;
1562 int status; 1228 int status;
1563 1229
1230 /* move to the end of the file, not necesary if _APPEND mode */
1564 fd = fileno (mud->file); 1231 fd = fileno (mud->file);
1565 if (fstat (fd, &st) != 0) 1232 if (fstat (fd, &st) != 0)
1566 { 1233 {
...@@ -1576,24 +1243,42 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) ...@@ -1576,24 +1243,42 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
1576 } 1243 }
1577 1244
1578 /* header */ 1245 /* header */
1579 message_get_header (msg, &hdr);
1580 header_get_stream (hdr, &is);
1581 if (st.st_size != 0) 1246 if (st.st_size != 0)
1582 fputc ('\n', mud->file); 1247 fputc ('\n', mud->file);
1248
1249 message_get_header (msg, &hdr);
1250 /* generate a "From " separator */
1251 {
1252 char from[128];
1253 char date[128];
1254 char *s;
1255 size_t n = 0;
1256 *date = *from = '\0';
1257 message_from (msg, from, sizeof (from), &n);
1258 s = memchr (from, '\n', n);
1259 if (s) *s = '\0'; n = 0;
1260 message_received (msg, date, sizeof (date), &n);
1261 s = memchr (date, '\n', n);
1262 if (s) *s = '\0';
1263 fprintf (mud->file, "From %s %s\n", from, date);
1264 }
1265
1266 header_get_stream (hdr, &is);
1583 do { 1267 do {
1584 status = stream_read (is, buffer, sizeof (buffer), off, &nread); 1268 status = stream_read (is, buffer, sizeof (buffer), off, &nread);
1585 if (status != 0) 1269 if (status != 0)
1586 return status; 1270 return status;
1271 if (nread == 0)
1272 break;
1587 fwrite (buffer, sizeof (*buffer), nread, mud->file); 1273 fwrite (buffer, sizeof (*buffer), nread, mud->file);
1588 off += nread; 1274 off += nread;
1589 } while (nread > 0); 1275 } while (nread > 0);
1590 1276
1591 *buffer = '\0'; 1277 *buffer = '\0';
1592 /* separator */ 1278 /* separator */
1593 fputc ('\n', mud->file); 1279 /*fputc ('\n', mud->file);*/
1594 1280
1595 /* body */ 1281 /* body */
1596 off = 0;
1597 message_get_stream (msg, &is); 1282 message_get_stream (msg, &is);
1598 do { 1283 do {
1599 stream_read (is, buffer, sizeof (buffer), off, &nread); 1284 stream_read (is, buffer, sizeof (buffer), off, &nread);
...@@ -1630,17 +1315,17 @@ mailbox_unix_size (mailbox_t mbox, off_t *size) ...@@ -1630,17 +1315,17 @@ mailbox_unix_size (mailbox_t mbox, off_t *size)
1630 } 1315 }
1631 1316
1632 static int 1317 static int
1633 mailbox_unix_messages_count (mailbox_t mbox, size_t *count) 1318 mailbox_unix_messages_count (mailbox_t mbox, size_t *pcount)
1634 { 1319 {
1635 mailbox_unix_data_t mud; 1320 mailbox_unix_data_t mud;
1636 if (mbox == NULL || (mud = (mailbox_unix_data_t) mbox->data) == NULL) 1321 if (mbox == NULL || (mud = (mailbox_unix_data_t) mbox->data) == NULL)
1637 return EINVAL; 1322 return EINVAL;
1638 1323
1639 if (! mailbox_unix_is_updated (mbox)) 1324 if (! mailbox_unix_is_updated (mbox))
1640 return mailbox_unix_scan (mbox, 1, count); 1325 return mailbox_unix_scan (mbox, 1, pcount);
1641 1326
1642 if (count) 1327 if (pcount)
1643 *count = mud->messages_count; 1328 *pcount = mud->messages_count;
1644 1329
1645 return 0; 1330 return 0;
1646 } 1331 }
......
...@@ -16,20 +16,19 @@ ...@@ -16,20 +16,19 @@
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 18
19 #include <header.h>
20 #include <attribute.h>
21 #include <message0.h>
22 #include <mailbox0.h>
23 #include <io0.h> 19 #include <io0.h>
20 #include <message0.h>
24 21
25 #include <errno.h> 22 #include <errno.h>
26 #include <stdio.h> 23 #include <stdio.h>
27 #include <stdlib.h> 24 #include <stdlib.h>
28 #include <sys/types.h> 25 #include <sys/types.h>
29 #include <sys/stat.h> 26 #include <sys/stat.h>
27 #include <time.h>
28 #include <string.h>
30 29
31 static int body_init (body_t *pbody, void *owner); 30 static int extract_addr(const char *s, size_t n, char **presult,
32 static void body_destroy (body_t *pbody, void *owner); 31 size_t *pnwrite);
33 static int message_read (stream_t is, char *buf, size_t buflen, 32 static int message_read (stream_t is, char *buf, size_t buflen,
34 off_t off, size_t *pnread ); 33 off_t off, size_t *pnread );
35 static int message_write (stream_t os, const char *buf, size_t buflen, 34 static int message_write (stream_t os, const char *buf, size_t buflen,
...@@ -37,142 +36,25 @@ static int message_write (stream_t os, const char *buf, size_t buflen, ...@@ -37,142 +36,25 @@ static int message_write (stream_t os, const char *buf, size_t buflen,
37 static int message_get_fd (stream_t stream, int *pfd); 36 static int message_get_fd (stream_t stream, int *pfd);
38 37
39 int 38 int
40 message_clone (message_t msg) 39 message_create (message_t *pmsg, void *owner)
41 { 40 {
42 int status; 41 message_t msg;
43 stream_t stream; 42 stream_t stream;
44 header_t header; 43 int status;
45 attribute_t attribute;
46 body_t body;
47 char buffer[BUFSIZ];
48 char *pbuf = NULL;
49 char *tbuf = NULL;
50 off_t offset = 0;
51 size_t nread = 0;
52 44
53 if (msg == NULL) 45 if (pmsg == NULL)
54 return EINVAL; 46 return EINVAL;
55 47 msg = calloc (1, sizeof (*msg));
56 /* If is not own, then it's a floating message 48 if (msg == NULL)
57 * just bump the reference count. 49 return ENOMEM;
58 */ 50 status = stream_create (&stream, msg);
59 if (msg->owner == NULL)
60 {
61 if (msg->ref_count <= 0)
62 msg->ref_count = 1;
63 else
64 msg->ref_count++;
65 return 0;
66 }
67
68 /* retreive the header */
69 {
70 header = msg->header;
71 status = header_get_stream (header, &stream);
72 if (status != 0)
73 return status;
74
75 do {
76 status = stream_read (stream, buffer, sizeof (buffer), offset, &nread);
77 if (status != 0)
78 {
79 free (pbuf);
80 return status;
81 }
82 if (nread == 0)
83 break;
84 tbuf = realloc (pbuf, offset + nread);
85 if (tbuf == NULL)
86 {
87 free (pbuf);
88 return ENOMEM;
89 }
90 else
91 pbuf = tbuf;
92 memcpy (pbuf + offset, buffer, nread);
93 offset += nread;
94 } while (nread > 0);
95
96 }
97 /* set the new header */
98 status = header_init (&header, pbuf, offset, msg);
99 if (status != 0) 51 if (status != 0)
100 { 52 {
101 free (pbuf); 53 free (msg);
102 return status;
103 }
104 free (pbuf);
105
106 /* retrieve the body */
107 {
108 status = body_init (&body, msg);
109 if (status != 0)
110 {
111 header_destroy (&header, msg);
112 return status;
113 }
114
115 stream = msg->stream;
116 offset = 0;
117 do {
118 do
119 {
120 status = stream_read (stream, buffer, sizeof (buffer), offset, &nread);
121 } while (status == EAGAIN);
122 if (status != 0)
123 {
124 header_destroy (&header, msg);
125 body_destroy (&body, msg);
126 return status;
127 }
128 fwrite (buffer, sizeof (*buffer), nread, body->file);
129 offset += nread;
130 } while (nread > 0);
131 rewind (body->file);
132 }
133
134 /* set the body with the streams */
135 status = stream_init (&stream, msg);
136 if (status != 0 )
137 {
138 header_destroy (&header, msg);
139 body_destroy (&body, msg);
140 return status; 54 return status;
141 } 55 }
142 stream_set_read (stream, message_read, msg); 56 stream_set_read (stream, message_read, msg);
143 stream_set_write (stream, message_write, msg); 57 stream_set_write (stream, message_write, msg);
144
145 /* attribute */
146 status = attribute_init (&attribute, msg);
147 if (status != 0)
148 {
149 header_destroy (&header, msg);
150 body_destroy (&body, msg);
151 stream_destroy (&stream, msg);
152 }
153 attribute_copy (attribute, msg->attribute);
154
155 /* every thing went ok */
156 msg->header = header;
157 msg->attribute = attribute;
158 msg->stream = stream;
159 msg->body = body;
160 msg->size = offset;
161 msg->ref_count++;
162 msg->owner = NULL; /* orphan */
163 return 0;
164 }
165
166 int
167 message_init (message_t *pmsg, void *owner)
168 {
169 message_t msg;
170
171 if (pmsg == NULL)
172 return EINVAL;
173 msg = calloc (1, sizeof (*msg));
174 if (msg == NULL)
175 return ENOMEM;
176 msg->owner = owner; 58 msg->owner = owner;
177 *pmsg = msg; 59 *pmsg = msg;
178 return 0; 60 return 0;
...@@ -200,9 +82,9 @@ message_destroy (message_t *pmsg, void *owner) ...@@ -200,9 +82,9 @@ message_destroy (message_t *pmsg, void *owner)
200 82
201 if (destroy) 83 if (destroy)
202 { 84 {
203 header_t header = msg->header;
204 attribute_t attribute = msg->attribute; 85 attribute_t attribute = msg->attribute;
205 stream_t stream = msg->stream; 86 stream_t stream = msg->stream;
87 header_t header = msg->header;
206 body_t body = msg->body; 88 body_t body = msg->body;
207 89
208 /* notify the listeners */ 90 /* notify the listeners */
...@@ -215,12 +97,7 @@ message_destroy (message_t *pmsg, void *owner) ...@@ -215,12 +97,7 @@ message_destroy (message_t *pmsg, void *owner)
215 stream_destroy (&stream, owner); 97 stream_destroy (&stream, owner);
216 98
217 /* if sometype of floating/temporary message */ 99 /* if sometype of floating/temporary message */
218 if (body) 100 body_destroy (&body, owner);
219 {
220 if (body->file)
221 fclose (body->file);
222 free (body);
223 }
224 /* notifications are done */ 101 /* notifications are done */
225 free (msg->event); 102 free (msg->event);
226 103
...@@ -240,14 +117,14 @@ message_destroy (message_t *pmsg, void *owner) ...@@ -240,14 +117,14 @@ message_destroy (message_t *pmsg, void *owner)
240 int 117 int
241 message_get_header (message_t msg, header_t *phdr) 118 message_get_header (message_t msg, header_t *phdr)
242 { 119 {
243 if (phdr == NULL || msg == NULL) 120 if (msg == NULL || phdr == NULL)
244 return EINVAL; 121 return EINVAL;
245 122
246 /* is it a floating mesg */ 123 /* is it a floating mesg */
247 if (msg->header == NULL && msg->owner == NULL) 124 if (msg->header == NULL && msg->owner == NULL)
248 { 125 {
249 header_t header; 126 header_t header;
250 int status = header_init (&header, NULL, 0, msg); 127 int status = header_create (&header, NULL, 0, msg);
251 if (status != 0) 128 if (status != 0)
252 return status; 129 return status;
253 msg->header = header; 130 msg->header = header;
...@@ -270,26 +147,53 @@ message_set_header (message_t msg, header_t hdr, void *owner) ...@@ -270,26 +147,53 @@ message_set_header (message_t msg, header_t hdr, void *owner)
270 } 147 }
271 148
272 int 149 int
150 message_get_body (message_t msg, body_t *pbody)
151 {
152 if (msg == NULL || pbody == NULL)
153 return EINVAL;
154
155 /* is it a floating mesg */
156 if (msg->body == NULL)
157 {
158 body_t body;
159 int status = body_create (&body, msg);
160 if (status != 0)
161 return status;
162 msg->body = body;
163 }
164 *pbody = msg->body;
165 return 0;
166 }
167
168 int
169 message_set_body (message_t msg, body_t body, void *owner)
170 {
171 if (msg == NULL )
172 return EINVAL;
173 if (msg->owner != owner)
174 return EACCES;
175 /* make sure we destoy the old if it was own by the mesg */
176 body_destroy (&(msg->body), msg);
177 msg->body = body;
178 return 0;
179 }
180
181 int
273 message_get_stream (message_t msg, stream_t *pstream) 182 message_get_stream (message_t msg, stream_t *pstream)
274 { 183 {
275 if (msg == NULL || pstream == NULL) 184 if (msg == NULL || pstream == NULL)
276 return EINVAL; 185 return EINVAL;
277 186
278 if (msg->stream == NULL && msg->owner == NULL) 187 if (msg->stream == NULL)
279 { 188 {
280 stream_t stream; 189 stream_t stream;
281 int status; 190 int status;
282 /* lazy floating message the body is created when 191 status = stream_create (&stream, msg);
283 * doing the first message_write creation
284 */
285 status = stream_init (&stream, msg);
286 if (status != 0) 192 if (status != 0)
287 return status; 193 return status;
288 stream_set_read (stream, message_read, msg); 194 stream_set_read (stream, message_read, msg);
289 stream_set_write (stream, message_write, msg); 195 stream_set_write (stream, message_write, msg);
290 stream_set_fd (stream, message_get_fd, msg); 196 stream_set_fd (stream, message_get_fd, msg);
291 /* make sure we've clean */
292 stream_destroy (&(msg->stream), msg);
293 msg->stream = stream; 197 msg->stream = stream;
294 } 198 }
295 199
...@@ -298,36 +202,143 @@ message_get_stream (message_t msg, stream_t *pstream) ...@@ -298,36 +202,143 @@ message_get_stream (message_t msg, stream_t *pstream)
298 } 202 }
299 203
300 int 204 int
301 message_set_stream (message_t msg, stream_t stream, void *owner) 205 message_lines (message_t msg, size_t *plines)
302 { 206 {
207 size_t hlines, blines;
303 if (msg == NULL) 208 if (msg == NULL)
304 return EINVAL; 209 return EINVAL;
210 if (plines)
211 {
212 hlines = blines = 0;
213 header_lines (msg->header, &hlines);
214 body_lines (msg->body, &blines);
215 *plines = hlines + blines;
216 }
217 return 0;
218 }
219
220 int
221 message_size (message_t msg, size_t *psize)
222 {
223 size_t hsize, bsize;
224 if (msg == NULL)
225 return EINVAL;
226 if (psize)
227 {
228 hsize = bsize = 0;
229 header_size (msg->header, &hsize);
230 body_size (msg->body, &bsize);
231 *psize = hsize + bsize;
232 }
233 return 0;
234 }
235
236 int
237 message_set_from (message_t msg,
238 int (*_from)(message_t, char *, size_t, size_t*),
239 void *owner)
240 {
241 if (msg == NULL)
242 return EINVAL;
305 if (msg->owner != owner) 243 if (msg->owner != owner)
306 return EACCES; 244 return EACCES;
307 /* make sure we destroy the old one if it is own by the message */ 245 msg->_from = _from;
308 stream_destroy (&(msg->stream), msg);
309 msg->stream = stream;
310 return 0; 246 return 0;
311 } 247 }
312 248
313 int 249 int
314 message_get_size (message_t msg, size_t *psize) 250 message_from (message_t msg, char *buf, size_t len, size_t *pnwrite)
315 { 251 {
252 header_t header = NULL;
253 size_t n = 0;
254 int status;
255
316 if (msg == NULL) 256 if (msg == NULL)
317 return EINVAL; 257 return EINVAL;
318 if (psize) 258
319 *psize = msg->size; 259 /* did they provide a way to get it */
260 if (msg->_from)
261 return msg->_from (msg, buf, len, pnwrite);
262
263 /* can it be extracted from the FROM: */
264 message_get_header (msg, &header);
265 status = header_get_value (header, "FROM", NULL, 0, &n);
266 if (status == 0 && n != 0)
267 {
268 char *from = calloc (1, n + 1);
269 char *addr;
270 header_get_value (header, "FROM", from, n + 1, NULL);
271 if (extract_addr (from, n, &addr, &n) == 0)
272 {
273 n = (n > len) ? len : n;
274 if (buf && len > 0)
275 {
276 memcpy (buf, addr, n);
277 buf[n - 1] = '\0';
278 }
279 free (addr);
280 free (from);
281 if (pnwrite)
282 *pnwrite = n;
283 return 0;
284 }
285 }
286
287 /* oops */
288 n = (7 > len) ? len: 7;
289 if (buf && len > 0)
290 {
291 memcpy (buf, "unknown", n);
292 buf [n - 1] = '\0';
293 }
294
295 if (pnwrite)
296 *pnwrite = n;
320 return 0; 297 return 0;
321 } 298 }
322 299
323 int 300 int
324 message_set_size (message_t msg, size_t size, void *owner) 301 message_set_received (message_t msg,
302 int (*_received) (message_t, char *, size_t , size_t *),
303 void *owner)
325 { 304 {
326 if (msg == NULL) 305 if (msg == NULL)
327 return EINVAL; 306 return EINVAL;
328 if (msg->owner != owner) 307 if (msg->owner != owner)
329 return EACCES; 308 return EACCES;
330 msg->size = size; 309 msg->_received = _received;
310 return 0;
311 }
312
313 int
314 message_received (message_t msg, char *buf, size_t len, size_t *pnwrite)
315 {
316 time_t t;
317 size_t n;
318 if (msg == NULL)
319 return EINVAL;
320 /* is it provided */
321 if (msg->_received)
322 return msg->_received (msg, buf, len, pnwrite);
323
324 /* FIXME: extract the time from "Date:" */
325
326 /* catch all */
327 /* FIXME: ctime() is not thread safe use strftime() */
328 t = time (NULL);
329 n = strlen (ctime (&t));
330
331 if (buf == NULL || len == 0)
332 {
333 if (pnwrite)
334 *pnwrite = n;
335 return 0;
336 }
337 n = (n > len) ? len : n;
338 strncpy (buf, ctime (&t), n);
339 buf [n - 1] = '\0';
340 if (pnwrite)
341 *pnwrite = n;
331 return 0; 342 return 0;
332 } 343 }
333 344
...@@ -339,7 +350,7 @@ message_get_attribute (message_t msg, attribute_t *pattribute) ...@@ -339,7 +350,7 @@ message_get_attribute (message_t msg, attribute_t *pattribute)
339 if (msg->attribute == NULL && msg->owner == NULL) 350 if (msg->attribute == NULL && msg->owner == NULL)
340 { 351 {
341 attribute_t attribute; 352 attribute_t attribute;
342 int status = attribute_init (&attribute, msg); 353 int status = attribute_create (&attribute, msg);
343 if (status != 0) 354 if (status != 0)
344 return status; 355 return status;
345 msg->attribute = attribute; 356 msg->attribute = attribute;
...@@ -427,100 +438,43 @@ message_notification (message_t msg, size_t type) ...@@ -427,100 +438,43 @@ message_notification (message_t msg, size_t type)
427 } 438 }
428 } 439 }
429 440
430 /* We use the notion of body_t here */
431 static int
432 body_init (body_t *pbody, void *owner)
433 {
434 body_t body;
435 FILE *file;
436
437 if (pbody == NULL)
438 return EINVAL;
439
440 #ifdef HAVE_MKSTEMP
441 {
442 char tmpbuf[L_tmpnam + 1];
443 int fd;
444
445 if (tmpnam (tmpbuf) == NULL ||
446 (fd = mkstemp (tmpbuf)) == -1 ||
447 (file = fdopen(fd, "w+")) == NULL)
448 return errno;
449 (void)remove(tmpbuf);
450 }
451 #else
452 file = tmpfile ();
453 if (file == NULL)
454 return errno;
455 /* make sure the mode is right */
456 fchmod (fileno (file), 0600);
457 #endif
458
459 /* set the body with the streams */
460 body = calloc (1, sizeof (*body));
461 if (body == NULL)
462 {
463 fclose (file);
464 return ENOMEM;
465 }
466 body->file = file;
467 body->owner = owner;
468 *pbody = body;
469 return 0;
470 }
471
472 static void
473 body_destroy (body_t *pbody, void *owner)
474 {
475 if (pbody && *pbody)
476 {
477 body_t body = *pbody;
478 if (body->owner == owner)
479 {
480 if (body->file)
481 fclose (body->file);
482 }
483 *pbody = NULL;
484 }
485 }
486
487 static int 441 static int
488 message_read (stream_t is, char *buf, size_t buflen, 442 message_read (stream_t is, char *buf, size_t buflen,
489 off_t off, size_t *pnread ) 443 off_t off, size_t *pnread )
490 { 444 {
491 message_t msg; 445 message_t msg;
492 size_t nread = 0; 446 stream_t his, bis;
447 size_t hread, hsize, bread, bsize;
448
493 449
494 if (is == NULL || (msg = is->owner) == NULL) 450 if (is == NULL || (msg = is->owner) == NULL)
495 return EINVAL; 451 return EINVAL;
496 452
497 if (msg->body) 453 bsize = hsize = bread = hread = 0;
454 his = bis = NULL;
455
456 header_size (msg->header, &hsize);
457 body_size (msg->body, &bsize);
458
459 if ((size_t)off <= hsize)
498 { 460 {
499 body_t body = msg->body; 461 header_get_stream (msg->header, &his);
500 if (body->file) 462 stream_read (his, buf, buflen, off, &hread);
463 /* still room left for some body, .. a pun ;-) */
464 if ((buflen - hread) > 0)
501 { 465 {
502 /* we're not checking the error of fseek for some handlers 466 body_get_stream (msg->body, &bis);
503 * like socket in those not make sense. 467 stream_read (bis, buf + hread, buflen - hread, 0, &bread);
504 * FIXME: Alternative is to check fseeck and errno == EBADF
505 * if not a seekable stream.
506 */
507 fseek (body->file, off, SEEK_SET);
508 nread = fread (buf, sizeof (char), buflen, body->file);
509 if (nread == 0)
510 {
511 if (ferror (body->file))
512 return errno;
513 /* clear the error for feof() */
514 clearerr (body->file);
515 }
516 /* errno set by fread()/fseek() ? */
517 } 468 }
518 else 469 }
519 return EINVAL; 470 else
471 {
472 body_get_stream (msg->body, &bis);
473 stream_read (bis, buf, buflen, off - hsize, &bread);
520 } 474 }
521 475
522 if (pnread) 476 if (pnread)
523 *pnread = nread; 477 *pnread = hread + bread;
524 return 0; 478 return 0;
525 } 479 }
526 480
...@@ -529,47 +483,11 @@ message_write (stream_t os, const char *buf, size_t buflen, ...@@ -529,47 +483,11 @@ message_write (stream_t os, const char *buf, size_t buflen,
529 off_t off, size_t *pnwrite) 483 off_t off, size_t *pnwrite)
530 { 484 {
531 message_t msg; 485 message_t msg;
532 size_t nwrite = 0; 486 (void)buf; (void)buflen; (void)off; (void)pnwrite;
533 body_t body;
534 487
535 if (os == NULL || (msg = os->owner) == NULL) 488 if (os == NULL || (msg = os->owner) == NULL)
536 return EINVAL; 489 return EINVAL;
537 490 return ENOSYS;
538 /* Probably being lazy, then create a body for the stream */
539 if (msg->body == NULL)
540 {
541 int status = body_init (&body, msg);
542 if (status != 0 )
543 return status;
544 msg->body = body;
545 }
546 else
547 body = msg->body;
548
549 if (body->file)
550 {
551 /* we're not checking the error of fseek for some handlers
552 * like socket in those not make sense.
553 * FIXME: Alternative is to check fseeck and errno == EBADF
554 * if not a seekable stream.
555 */
556 fseek (body->file, off, SEEK_SET);
557 nwrite = fwrite (buf, sizeof (char), buflen, body->file);
558 if (nwrite == 0)
559 {
560 if (ferror (body->file))
561 return errno;
562 /* clear the error for feof() */
563 clearerr (body->file);
564 }
565 /* errno set by fread()/fseek() ? */
566 }
567 else
568 return EINVAL;
569
570 if (pnwrite)
571 *pnwrite = nwrite;
572 return 0;
573 } 491 }
574 492
575 static int 493 static int
...@@ -577,6 +495,7 @@ message_get_fd (stream_t stream, int *pfd) ...@@ -577,6 +495,7 @@ message_get_fd (stream_t stream, int *pfd)
577 { 495 {
578 message_t msg; 496 message_t msg;
579 body_t body; 497 body_t body;
498 stream_t is;
580 499
581 if (stream == NULL || (msg = stream->owner) == NULL) 500 if (stream == NULL || (msg = stream->owner) == NULL)
582 return EINVAL; 501 return EINVAL;
...@@ -584,7 +503,7 @@ message_get_fd (stream_t stream, int *pfd) ...@@ -584,7 +503,7 @@ message_get_fd (stream_t stream, int *pfd)
584 /* Probably being lazy, then create a body for the stream */ 503 /* Probably being lazy, then create a body for the stream */
585 if (msg->body == NULL) 504 if (msg->body == NULL)
586 { 505 {
587 int status = body_init (&body, msg); 506 int status = body_create (&body, msg);
588 if (status != 0 ) 507 if (status != 0 )
589 return status; 508 return status;
590 msg->body = body; 509 msg->body = body;
...@@ -592,8 +511,75 @@ message_get_fd (stream_t stream, int *pfd) ...@@ -592,8 +511,75 @@ message_get_fd (stream_t stream, int *pfd)
592 else 511 else
593 body = msg->body; 512 body = msg->body;
594 513
595 if (pfd) 514 body_get_stream (body, &is);
596 *pfd = fileno (body->file); 515 return stream_get_fd (is, pfd);
516 }
517
518 static int
519 extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite)
520 {
521 char *p, *p1, *p2;
522
523 if (s == NULL || n == 0 || presult == NULL)
524 return EINVAL;
525
526 /* skip the double quotes */
527 p = memchr (s, '\"', n);
528 if (p != NULL)
529 {
530 p1 = memchr (s, '<', p - s);
531 p2 = memchr (s, '@', p - s);
532 if (p1 == NULL && p2 == NULL)
533 {
534 p1 = memchr (p + 1, '\"', n - ((p + 1) - s));
535 if (p1 != NULL)
536 {
537 n -= (p1 + 1) - s;
538 s = p1 + 1;
539 }
540 }
541 }
542
543 /* <name@hostname> ?? */
544 p = memchr (s, '<', n);
545 if (p != NULL)
546 {
547 p1 = memchr (p, '>', n - (p - s));
548 if (p1 != NULL && (p1 - p) > 1)
549 {
550 p2 = memchr (p, ' ', p1 - p);
551 if (p2 == NULL)
552 {
553 /* the NULL is already accounted for */
554 *presult = calloc (1, p1 - p);
555 if (*presult == NULL)
556 return ENOMEM;
557 memcpy (*presult, p + 1, (p1 - p) - 1);
558 if (pnwrite)
559 *pnwrite = (p1 - p) - 1;
560 return 0;
561 }
562 }
563 }
564 /* name@domain */
565 p = memchr (s, '@', n);
566 if (p != NULL)
567 {
568 p1 = p;
569 while (*p != ' ' && p != s)
570 p--;
571 while (*p1 != ' ' && p1 < (s + n))
572 p1++;
573 *presult = calloc (1, (p1 - p) + 1);
574 if (*presult == NULL)
575 return ENOMEM;
576 memcpy (*presult, p, p1 - p);
577 if (pnwrite)
578 *pnwrite = p1 - p;
579 return 0;
580 }
581
582 *presult = NULL;
583 return EINVAL;
597 584
598 return 0;
599 } 585 }
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
19 #include <message0.h> 19 #include <message0.h>
20 #include <mime0.h> 20 #include <mime0.h>
21 #include <io0.h> 21 #include <io0.h>
22 #include <body0.h>
22 #include <mime.h> 23 #include <mime.h>
23 24
24 #include <errno.h> 25 #include <errno.h>
...@@ -37,17 +38,15 @@ ...@@ -37,17 +38,15 @@
37 * Define mbx i/f for protocols that support mime parsing (IMAP). 38 * Define mbx i/f for protocols that support mime parsing (IMAP).
38 */ 39 */
39 40
40 static int _mime_append_part( mime_t mime, int body_offset, int body_len, int encap_msg ) 41 static int _mime_append_part(mime_t mime, message_t msg, int body_offset, int body_len, int encap_msg)
41 { 42 {
42 struct _mime_part *mime_part, **part_arr; 43 struct _mime_part *mime_part, **part_arr;
43 int ret; 44 int ret;
44 45
45 if ( ( mime_part = calloc(1, sizeof(*mime_part)) ) == NULL ) 46 if ( ( mime_part = calloc(1, sizeof(*mime_part)) ) == NULL )
46 return ENOMEM; 47 return ENOMEM;
47 if ( ( ret = header_init(&mime_part->hdr, mime->header_buf, mime->header_length, mime_part) ) != 0 ) { 48
48 free(mime_part); 49 memcpy(mime_part->sig,"MIME", 4);
49 return ret;
50 }
51 if ( encap_msg ) { 50 if ( encap_msg ) {
52 if ( mime->ncap_msgs >= mime->tmsgs ) { 51 if ( mime->ncap_msgs >= mime->tmsgs ) {
53 if ( ( part_arr = realloc(mime->cap_msgs, ( mime->tmsgs + 2 ) * sizeof(mime_part)) ) == NULL ) { 52 if ( ( part_arr = realloc(mime->cap_msgs, ( mime->tmsgs + 2 ) * sizeof(mime_part)) ) == NULL ) {
...@@ -70,24 +69,31 @@ static int _mime_append_part( mime_t mime, int body_offset, int body_len, int en ...@@ -70,24 +69,31 @@ static int _mime_append_part( mime_t mime, int body_offset, int body_len, int en
70 } 69 }
71 mime->mtp_parts[mime->nmtp_parts++] = mime_part; 70 mime->mtp_parts[mime->nmtp_parts++] = mime_part;
72 } 71 }
72 if ( msg == NULL ) {
73 if ( ( ret = header_create(&mime_part->hdr, mime->header_buf, mime->header_length, mime_part) ) != 0 ) {
74 free(mime_part);
75 return ret;
76 }
77 mime->header_length = 0;
78 }
73 mime_part->body_len = body_len; 79 mime_part->body_len = body_len;
74 mime_part->body_offset = body_offset; 80 mime_part->body_offset = body_offset;
81 mime_part->msg = msg;
75 mime_part->mime = mime; 82 mime_part->mime = mime;
76 mime->header_length = 0;
77 return 0; 83 return 0;
78 } 84 }
79 85
80 static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg) 86 static struct _mime_part *_mime_get_owner(mime_t mime, message_t msg)
81 { 87 {
82 int i; 88 int i;
83 89
84 for ( i = 0; i < mime->nmtp_parts; i++ ) { 90 for ( i = 0; i < mime->nmtp_parts; i++ ) {
85 if ( mime->mtp_parts[i] == msg->owner ) 91 if ( mime->mtp_parts[i] == msg->owner )
86 return mime->mtp_parts[i]; 92 return mime->mtp_parts[i];
87 } 93 }
88 return NULL; 94 return NULL;
89 } 95 }
90 96
91 static char *_strltrim(char *str) 97 static char *_strltrim(char *str)
92 { 98 {
93 char *p; 99 char *p;
...@@ -100,7 +106,7 @@ static char *_strltrim(char *str) ...@@ -100,7 +106,7 @@ static char *_strltrim(char *str)
100 static char *_strttrim(char *str) 106 static char *_strttrim(char *str)
101 { 107 {
102 char *p; 108 char *p;
103 109
104 for (p = str + strlen(str) - 1; isspace(*p) && p >= str; --p) 110 for (p = str + strlen(str) - 1; isspace(*p) && p >= str; --p)
105 ; 111 ;
106 *++p = '\0'; 112 *++p = '\0';
...@@ -115,28 +121,28 @@ char *_strtrim(char *str); ...@@ -115,28 +121,28 @@ char *_strtrim(char *str);
115 || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \ 121 || ((c) == '@') || ((c) == ',') || ((c) == ';') || ((c) == ':') \
116 || ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \ 122 || ((c) == '\\') || ((c) == '"') || ((c) == '.') || ((c) == '[') \
117 || ((c) == ']') ) 123 || ((c) == ']') )
118 124
119 static void _mime_munge_content_header(char *field_body ) 125 static void _mime_munge_content_header(char *field_body )
120 { 126 {
121 char *p, *e, *str = field_body; 127 char *p, *e, *str = field_body;
122 int quoted = 0; 128 int quoted = 0;
123 129
124 _strtrim(field_body); 130 _strtrim(field_body);
125 131
126 if ( ( e = p = strchr(str, ';') ) == NULL ) 132 if ( ( e = p = strchr(str, ';') ) == NULL )
127 return; 133 return;
128 e++; 134 e++;
129 while ( *e && isspace(*e) ) /* remove space upto param */ 135 while ( *e && isspace(*e) ) /* remove space upto param */
130 e++; 136 e++;
131 memmove(p+1, e, strlen(e)+1); 137 memmove(p+1, e, strlen(e)+1);
132 e = p+1; 138 e = p+1;
133 139
134 while ( *e && *e != '=' ) /* find end of value */ 140 while ( *e && *e != '=' ) /* find end of value */
135 e++; 141 e++;
136 e = p = e+1; 142 e = p = e+1;
137 while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) { 143 while ( *e && (quoted || !_ISSPECIAL(*e) || !isspace(*e) ) ) {
138 if ( *e == '\\' ) { /* escaped */ 144 if ( *e == '\\' ) { /* escaped */
139 memmove(e, e+1, strlen(e)+2); 145 memmove(e, e+1, strlen(e)+2);
140 } else if ( *e == '\"' ) 146 } else if ( *e == '\"' )
141 quoted = ~quoted; 147 quoted = ~quoted;
142 e++; 148 e++;
...@@ -147,7 +153,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) ...@@ -147,7 +153,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
147 { 153 {
148 char *str, *p, *v, *e; 154 char *str, *p, *v, *e;
149 int quoted = 0, was_quoted = 0; 155 int quoted = 0, was_quoted = 0;
150 156
151 if ( len == NULL || ( str = field_body ) == NULL ) 157 if ( len == NULL || ( str = field_body ) == NULL )
152 return NULL; 158 return NULL;
153 159
...@@ -162,7 +168,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) ...@@ -162,7 +168,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
162 if ( *e == '\"' ) 168 if ( *e == '\"' )
163 quoted = ~quoted, was_quoted = 1; 169 quoted = ~quoted, was_quoted = 1;
164 else 170 else
165 (*len)++; 171 (*len)++;
166 e++; 172 e++;
167 } 173 }
168 if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */ 174 if ( strncasecmp(p, param, strlen(param)) ) { /* no match jump to next */
...@@ -171,7 +177,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len) ...@@ -171,7 +177,7 @@ static char *_mime_get_param(char *field_body, const char *param, int *len)
171 } 177 }
172 else 178 else
173 return was_quoted ? v + 1 : v; /* return unquted value */ 179 return was_quoted ? v + 1 : v; /* return unquted value */
174 } 180 }
175 return NULL; 181 return NULL;
176 } 182 }
177 183
...@@ -205,11 +211,11 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -205,11 +211,11 @@ static int _mime_parse_mpart_message(mime_t mime)
205 char *cp, *cp2; 211 char *cp, *cp2;
206 int blength, body_length, body_offset, ret; 212 int blength, body_length, body_offset, ret;
207 size_t nbytes; 213 size_t nbytes;
208 214
209 if ( !(mime->flags & MIME_PARSER_ACTIVE) ) { 215 if ( !(mime->flags & MIME_PARSER_ACTIVE) ) {
210 char *boundary; 216 char *boundary;
211 int len; 217 int len;
212 218
213 if ( ( ret = _mime_setup_buffers(mime) ) != 0 ) 219 if ( ( ret = _mime_setup_buffers(mime) ) != 0 )
214 return ret; 220 return ret;
215 if ( ( boundary = _mime_get_param(mime->content_type, "boundary", &len) ) == NULL ) 221 if ( ( boundary = _mime_get_param(mime->content_type, "boundary", &len) ) == NULL )
...@@ -217,7 +223,7 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -217,7 +223,7 @@ static int _mime_parse_mpart_message(mime_t mime)
217 if ( ( mime->boundary = calloc(1, len + 1) ) == NULL ) 223 if ( ( mime->boundary = calloc(1, len + 1) ) == NULL )
218 return ENOMEM; 224 return ENOMEM;
219 strncpy(mime->boundary, boundary, len ); 225 strncpy(mime->boundary, boundary, len );
220 226
221 mime->cur_offset = 0; 227 mime->cur_offset = 0;
222 mime->line_ndx = 0; 228 mime->line_ndx = 0;
223 mime->parser_state = MIME_STATE_SCAN_BOUNDARY; 229 mime->parser_state = MIME_STATE_SCAN_BOUNDARY;
...@@ -225,7 +231,7 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -225,7 +231,7 @@ static int _mime_parse_mpart_message(mime_t mime)
225 } 231 }
226 body_length = mime->body_length; 232 body_length = mime->body_length;
227 body_offset = mime->body_offset; 233 body_offset = mime->body_offset;
228 234
229 while ( ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) { 235 while ( ( ret = stream_read(mime->stream, mime->cur_buf, mime->buf_size, mime->cur_offset, &nbytes) ) == 0 && nbytes ) {
230 cp = mime->cur_buf; 236 cp = mime->cur_buf;
231 while ( nbytes ) { 237 while ( nbytes ) {
...@@ -241,14 +247,14 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -241,14 +247,14 @@ static int _mime_parse_mpart_message(mime_t mime)
241 cp2 = mime->cur_line[0] == '\n' ? mime->cur_line + 1 : mime->cur_line; 247 cp2 = mime->cur_line[0] == '\n' ? mime->cur_line + 1 : mime->cur_line;
242 blength = strlen(mime->boundary); 248 blength = strlen(mime->boundary);
243 if ( mime->line_ndx >= blength ) { 249 if ( mime->line_ndx >= blength ) {
244 if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) ) 250 if ( ( !strncasecmp(cp2,"--", 2) && !strncasecmp(cp2+2, mime->boundary, blength) )
245 || !strncasecmp(cp2, mime->boundary, blength) ) { 251 || !strncasecmp(cp2, mime->boundary, blength) ) {
246 mime->parser_state = MIME_STATE_HEADERS; 252 mime->parser_state = MIME_STATE_HEADERS;
247 mime->flags &= ~MIME_PARSER_HAVE_CR; 253 mime->flags &= ~MIME_PARSER_HAVE_CR;
248 body_length = mime->cur_offset - body_offset - mime->line_ndx + 1; 254 body_length = mime->cur_offset - body_offset - mime->line_ndx + 1;
249 if ( mime->header_length ) /* this skips the preamble */ 255 if ( mime->header_length ) /* this skips the preamble */
250 _mime_append_part(mime, body_offset, body_length, FALSE ); 256 _mime_append_part(mime, NULL, body_offset, body_length, FALSE );
251 if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) || 257 if ( ( cp2 + blength + 2 < cp && !strncasecmp(cp2+2+blength, "--",2) ) ||
252 !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */ 258 !strncasecmp(cp2+blength, "--",2) ) { /* very last boundary */
253 break; 259 break;
254 } 260 }
...@@ -256,7 +262,7 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -256,7 +262,7 @@ static int _mime_parse_mpart_message(mime_t mime)
256 break; 262 break;
257 } 263 }
258 } 264 }
259 mime->line_ndx = 0; 265 mime->line_ndx = 0;
260 mime->cur_line[0] = *cp; /* stay in this state but leave '\n' at begining */ 266 mime->cur_line[0] = *cp; /* stay in this state but leave '\n' at begining */
261 break; 267 break;
262 case MIME_STATE_HEADERS: 268 case MIME_STATE_HEADERS:
...@@ -265,7 +271,7 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -265,7 +271,7 @@ static int _mime_parse_mpart_message(mime_t mime)
265 if ( mime->line_ndx == 1 || mime->cur_line[0] == '\r' ) { 271 if ( mime->line_ndx == 1 || mime->cur_line[0] == '\r' ) {
266 mime->parser_state = MIME_STATE_BEGIN_LINE; 272 mime->parser_state = MIME_STATE_BEGIN_LINE;
267 body_offset = mime->cur_offset + 1; 273 body_offset = mime->cur_offset + 1;
268 } 274 }
269 mime->line_ndx = -1; 275 mime->line_ndx = -1;
270 break; 276 break;
271 } 277 }
...@@ -278,12 +284,12 @@ static int _mime_parse_mpart_message(mime_t mime) ...@@ -278,12 +284,12 @@ static int _mime_parse_mpart_message(mime_t mime)
278 mime->cur_offset++; 284 mime->cur_offset++;
279 nbytes--; 285 nbytes--;
280 cp++; 286 cp++;
281 } 287 }
282 if ( mime->flags & MIME_INCREAMENTAL_PARSER ) { 288 if ( mime->flags & MIME_INCREAMENTAL_PARSER ) {
283 ret = EAGAIN; 289 ret = EAGAIN;
284 break; 290 break;
285 } 291 }
286 } 292 }
287 mime->body_length = body_length; 293 mime->body_length = body_length;
288 mime->body_offset = body_offset; 294 mime->body_offset = body_offset;
289 if ( ret != EAGAIN ) { /* finished cleanup */ 295 if ( ret != EAGAIN ) { /* finished cleanup */
...@@ -297,7 +303,7 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o ...@@ -297,7 +303,7 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o
297 { 303 {
298 struct _mime_part *mime_part = stream->owner; 304 struct _mime_part *mime_part = stream->owner;
299 size_t read_len; 305 size_t read_len;
300 306
301 if ( nbytes == NULL ) 307 if ( nbytes == NULL )
302 return(EINVAL); 308 return(EINVAL);
303 309
...@@ -310,7 +316,26 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o ...@@ -310,7 +316,26 @@ static int _mime_message_read(stream_t stream, char *buf, size_t buflen, off_t o
310 return stream_read(mime_part->mime->stream, buf, read_len, mime_part->body_offset + off, nbytes ); 316 return stream_read(mime_part->mime->stream, buf, read_len, mime_part->body_offset + off, nbytes );
311 } 317 }
312 318
313 int mime_init(mime_t *pmime, message_t msg, int flags) 319 static int _mime_new_message_read(stream_t stream, char *buf, size_t buflen, off_t off, size_t *nbytes)
320 {
321 (void)stream; (void)buf; (void)buflen; (void)off;
322 if ( nbytes == NULL )
323 return(EINVAL);
324
325 return 0;
326 }
327
328 static int _mime_body_size (body_t body, size_t *psize)
329 {
330 struct _mime_part *mime_part = body->owner;
331 if (mime_part == NULL)
332 return EINVAL;
333 if (psize)
334 *psize = mime_part->body_len;
335 return 0;
336 }
337
338 int mime_create(mime_t *pmime, message_t msg, int flags)
314 { 339 {
315 mime_t mime = NULL; 340 mime_t mime = NULL;
316 int ret = 0; 341 int ret = 0;
...@@ -336,15 +361,12 @@ int mime_init(mime_t *pmime, message_t msg, int flags) ...@@ -336,15 +361,12 @@ int mime_init(mime_t *pmime, message_t msg, int flags)
336 mime->msg = msg; 361 mime->msg = msg;
337 mime->buf_size = MIME_DFLT_BUF_SIZE; 362 mime->buf_size = MIME_DFLT_BUF_SIZE;
338 message_get_stream(msg, &(mime->stream)); 363 message_get_stream(msg, &(mime->stream));
339 } 364 }
340 } 365 }
341 } 366 }
342 else { /* create a floating message to contain this new mime message */ 367 else {
343 if ( ( ret = message_init( &msg, NULL ) ) == 0 ) { 368 if ( ( ret = message_create( &msg, mime ) ) == 0 ) {
344 if ( ( ret = message_get_stream(msg, &(mime->stream)) ) == 0 ) { 369 mime->flags |= MIME_NEW_MESSAGE;
345 msg->owner = mime;
346 mime->flags |= MIME_NEW_MESSAGE;
347 }
348 } 370 }
349 } 371 }
350 if ( ret != 0 ) { 372 if ( ret != 0 ) {
...@@ -364,22 +386,22 @@ void mime_destroy(mime_t *pmime) ...@@ -364,22 +386,22 @@ void mime_destroy(mime_t *pmime)
364 mime_t mime; 386 mime_t mime;
365 struct _mime_part *mime_part; 387 struct _mime_part *mime_part;
366 int i; 388 int i;
367 389
368 if (pmime && *pmime) { 390 if (pmime && *pmime) {
369 mime = *pmime; 391 mime = *pmime;
370 if ( mime->mtp_parts != NULL ) { 392 if ( mime->mtp_parts != NULL ) {
371 for ( i = 0; i < mime->nmtp_parts; i++ ) { 393 for ( i = 0; i < mime->nmtp_parts; i++ ) {
372 mime_part = mime->mtp_parts[i]; 394 mime_part = mime->mtp_parts[i];
373 if ( mime_part->msg ) 395 if ( mime_part->msg )
374 message_destroy(&mime_part->msg, mime_part); 396 message_destroy(&mime_part->msg, mime_part);
375 else 397 else
376 header_destroy(&mime_part->hdr, mime_part); 398 header_destroy(&mime_part->hdr, mime_part);
377 } 399 }
378 } 400 }
379 if ( mime->cap_msgs != NULL ) { 401 if ( mime->cap_msgs != NULL ) {
380 for ( i = 0; i < mime->ncap_msgs; i++ ) { 402 for ( i = 0; i < mime->ncap_msgs; i++ ) {
381 mime_part = mime->cap_msgs[i]; 403 mime_part = mime->cap_msgs[i];
382 if ( mime_part->msg ) 404 if ( mime_part->msg )
383 message_destroy(&mime_part->msg, mime_part); 405 message_destroy(&mime_part->msg, mime_part);
384 else 406 else
385 header_destroy(&mime_part->hdr, mime_part); 407 header_destroy(&mime_part->hdr, mime_part);
...@@ -391,7 +413,7 @@ void mime_destroy(mime_t *pmime) ...@@ -391,7 +413,7 @@ void mime_destroy(mime_t *pmime)
391 free(mime->cur_buf); 413 free(mime->cur_buf);
392 if ( mime->cur_line ) 414 if ( mime->cur_line )
393 free(mime->cur_line); 415 free(mime->cur_line);
394 if ( mime->boundary ) 416 if ( mime->boundary )
395 free(mime->boundary); 417 free(mime->boundary);
396 if ( mime->header_buf ) 418 if ( mime->header_buf )
397 free(mime->header_buf); 419 free(mime->header_buf);
...@@ -413,8 +435,9 @@ int mime_get_part(mime_t mime, int part, message_t *msg) ...@@ -413,8 +435,9 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
413 int nmtp_parts, ret = 0; 435 int nmtp_parts, ret = 0;
414 size_t hsize = 0; 436 size_t hsize = 0;
415 stream_t stream; 437 stream_t stream;
438 body_t body;
416 struct _mime_part *mime_part; 439 struct _mime_part *mime_part;
417 440
418 if ( ( ret = mime_get_num_parts(mime, &nmtp_parts ) ) == 0 ) { 441 if ( ( ret = mime_get_num_parts(mime, &nmtp_parts ) ) == 0 ) {
419 if ( part < 1 || part > nmtp_parts ) 442 if ( part < 1 || part > nmtp_parts )
420 return EINVAL; 443 return EINVAL;
...@@ -422,18 +445,17 @@ int mime_get_part(mime_t mime, int part, message_t *msg) ...@@ -422,18 +445,17 @@ int mime_get_part(mime_t mime, int part, message_t *msg)
422 *msg = mime->msg; 445 *msg = mime->msg;
423 else { 446 else {
424 mime_part = mime->mtp_parts[part-1]; 447 mime_part = mime->mtp_parts[part-1];
425 if ( ( ret = message_init(&(mime_part->msg), mime_part) ) == 0 ) { 448 if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0 ) {
426 message_set_header(mime_part->msg, mime_part->hdr, mime_part); 449 message_set_header(mime_part->msg, mime_part->hdr, mime_part);
427 header_get_size(mime_part->hdr, &hsize); 450 header_size(mime_part->hdr, &hsize);
428 message_set_size (mime_part->msg, mime_part->body_len + hsize, mime_part); 451 if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
429 if ( ( ret = stream_init(&stream, mime_part) ) == 0 ) { 452 if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
430 if ( ( ret = stream_set_read(stream, _mime_message_read, mime_part) ) == 0 ) { 453 body_set_size (body, _mime_body_size, mime_part);
431 if ( ( ret = message_set_stream( mime_part->msg, stream, mime_part) ) == 0 ) { 454 stream_set_read(stream, _mime_message_read, mime_part);
432 *msg = mime_part->msg; 455 body_set_stream(body, stream, mime_part);
433 return 0; 456 *msg = mime_part->msg;
434 } 457 return 0;
435 } 458 }
436 stream_destroy(&stream, mime_part);
437 } 459 }
438 message_destroy(&mime_part->msg, mime_part); 460 message_destroy(&mime_part->msg, mime_part);
439 } 461 }
...@@ -449,11 +471,12 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) ...@@ -449,11 +471,12 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
449 char *content_type, *cp; 471 char *content_type, *cp;
450 header_t hdr; 472 header_t hdr;
451 stream_t stream; 473 stream_t stream;
474 body_t body;
452 struct _mime_part *mime_part; 475 struct _mime_part *mime_part;
453 476
454 if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE ) 477 if ( mime == NULL || msg == NULL || newmsg == NULL || mime->flags & MIME_NEW_MESSAGE )
455 return EINVAL; 478 return EINVAL;
456 479
457 if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */ 480 if ( mime->msg != msg && ( mime_part = _mime_get_owner( mime, msg ) ) == NULL ) /* I don't know about or own this message */
458 return EPERM; 481 return EPERM;
459 482
...@@ -493,21 +516,20 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) ...@@ -493,21 +516,20 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
493 } 516 }
494 body_length -= mime->cur_offset - body_offset; 517 body_length -= mime->cur_offset - body_offset;
495 body_offset = mime->cur_offset + 1; 518 body_offset = mime->cur_offset + 1;
496 if ( ( ret = _mime_append_part( mime, body_offset, body_length, TRUE ) ) == 0 ) { 519 if ( ( ret = _mime_append_part( mime, NULL, body_offset, body_length, TRUE ) ) == 0 ) {
497 mime_part = mime->cap_msgs[mime->ncap_msgs - 1]; 520 mime_part = mime->cap_msgs[mime->ncap_msgs - 1];
498 if ( ( ret = message_init(&(mime_part->msg), mime_part) ) == 0) { 521 if ( ( ret = message_create(&(mime_part->msg), mime_part) ) == 0) {
499 message_set_header(mime_part->msg, mime_part->hdr, mime_part); 522 message_set_header(mime_part->msg, mime_part->hdr, mime_part);
500 message_set_size (mime_part->msg, mime_part->body_len, mime_part); 523 if ( ( ret = body_create(&body, mime_part) ) == 0 ) {
501 if ( ( ret = stream_init(&stream, mime_part) ) == 0 ) { 524 if ( ( ret = stream_create(&stream, mime_part) ) == 0 ) {
502 if ( ( ret = stream_set_read(stream, _mime_message_read, mime_part) ) == 0 ) { 525 stream_set_read(stream, _mime_message_read, mime_part);
503 if ( ( ret = message_set_stream( mime_part->msg, stream, mime_part) ) == 0 ) { 526 body_set_size (body, _mime_body_size, mime_part);
504 *newmsg = mime_part->msg; 527 body_set_stream( body, stream, mime_part);
505 return 0; 528 *newmsg = mime_part->msg;
506 } 529 return 0;
507 } 530 }
508 stream_destroy(&stream, mime_part); 531 message_destroy(&mime_part->msg, mime_part);
509 } 532 }
510 message_destroy(&mime_part->msg, mime_part);
511 } 533 }
512 } 534 }
513 } 535 }
...@@ -515,13 +537,13 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg) ...@@ -515,13 +537,13 @@ int mime_unencapsulate(mime_t mime, message_t msg, message_t *newmsg)
515 } 537 }
516 } 538 }
517 } 539 }
518 return ret; 540 return ret;
519 } 541 }
520 542
521 int mime_get_num_parts(mime_t mime, int *nmtp_parts) 543 int mime_get_num_parts(mime_t mime, int *nmtp_parts)
522 { 544 {
523 int ret = 0; 545 int ret = 0;
524 546
525 if ( mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE ) { 547 if ( mime->nmtp_parts == 0 || mime->flags & MIME_PARSER_ACTIVE ) {
526 if ( mime_is_multi_part(mime) ) { 548 if ( mime_is_multi_part(mime) ) {
527 if ( ( ret = _mime_parse_mpart_message(mime) ) != 0 ) 549 if ( ( ret = _mime_parse_mpart_message(mime) ) != 0 )
...@@ -531,14 +553,14 @@ int mime_get_num_parts(mime_t mime, int *nmtp_parts) ...@@ -531,14 +553,14 @@ int mime_get_num_parts(mime_t mime, int *nmtp_parts)
531 } 553 }
532 *nmtp_parts = mime->nmtp_parts; 554 *nmtp_parts = mime->nmtp_parts;
533 return(ret); 555 return(ret);
534 556
535 } 557 }
536 558
537 int mime_add_part(mime_t mime, message_t msg) 559 int mime_add_part(mime_t mime, message_t msg)
538 { 560 {
539 if ( mime == NULL || msg == NULL || ( mime->flags & MIME_NEW_MESSAGE ) == 0 ) 561 if ( mime == NULL || msg == NULL || ( mime->flags & MIME_NEW_MESSAGE ) == 0 )
540 return EINVAL; 562 return EINVAL;
541 return ENOTSUP; 563 return _mime_append_part(mime, msg, 0, 0, FALSE);
542 } 564 }
543 565
544 int mime_get_message(mime_t mime, message_t *msg) 566 int mime_get_message(mime_t mime, message_t *msg)
...@@ -548,3 +570,4 @@ int mime_get_message(mime_t mime, message_t *msg) ...@@ -548,3 +570,4 @@ int mime_get_message(mime_t mime, message_t *msg)
548 *msg = mime->msg; 570 *msg = mime->msg;
549 return 0; 571 return 0;
550 } 572 }
573
......
...@@ -88,7 +88,7 @@ registrar_add (struct url_registrar *new_ureg, ...@@ -88,7 +88,7 @@ registrar_add (struct url_registrar *new_ureg,
88 return ENOMEM; 88 return ENOMEM;
89 } 89 }
90 } 90 }
91 mreg->_init = new_mreg->_init; 91 mreg->_create = new_mreg->_create;
92 mreg->_destroy = new_mreg->_destroy; 92 mreg->_destroy = new_mreg->_destroy;
93 93
94 /* URL */ 94 /* URL */
...@@ -110,7 +110,7 @@ registrar_add (struct url_registrar *new_ureg, ...@@ -110,7 +110,7 @@ registrar_add (struct url_registrar *new_ureg,
110 return ENOMEM; 110 return ENOMEM;
111 } 111 }
112 } 112 }
113 ureg->_init = new_ureg->_init; 113 ureg->_create = new_ureg->_create;
114 ureg->_destroy = new_ureg->_destroy; 114 ureg->_destroy = new_ureg->_destroy;
115 } 115 }
116 116
......
...@@ -39,7 +39,7 @@ static int get_query (const url_t, char *, size_t, size_t *); ...@@ -39,7 +39,7 @@ static int get_query (const url_t, char *, size_t, size_t *);
39 static int get_id (const url_t, int *); 39 static int get_id (const url_t, int *);
40 40
41 int 41 int
42 url_init (url_t * purl, const char *name) 42 url_create (url_t * purl, const char *name)
43 { 43 {
44 int status = EINVAL; 44 int status = EINVAL;
45 struct url_registrar *ureg; 45 struct url_registrar *ureg;
...@@ -87,7 +87,7 @@ url_init (url_t * purl, const char *name) ...@@ -87,7 +87,7 @@ url_init (url_t * purl, const char *name)
87 /* Found one initialize it */ 87 /* Found one initialize it */
88 if (status == 0) 88 if (status == 0)
89 { 89 {
90 status = ureg->_init (purl, name); 90 status = ureg->_create (purl, name);
91 if (status == 0) 91 if (status == 0)
92 { 92 {
93 url_t url = *purl; 93 url_t url = *purl;
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_file_destroy (url_t *purl); 25 static void url_file_destroy (url_t *purl);
26 static int url_file_init (url_t *purl, const char *name); 26 static int url_file_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_file_registrar = 28 struct url_registrar _url_file_registrar =
29 { 29 {
30 "file:", 30 "file:",
31 url_file_init, url_file_destroy 31 url_file_create, url_file_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -48,7 +48,7 @@ url_file_destroy (url_t *purl) ...@@ -48,7 +48,7 @@ url_file_destroy (url_t *purl)
48 UNIX box 48 UNIX box
49 */ 49 */
50 static int 50 static int
51 url_file_init (url_t *purl, const char *name) 51 url_file_create (url_t *purl, const char *name)
52 { 52 {
53 url_t url; 53 url_t url;
54 struct url_registrar *ureg = &_url_mbox_registrar; 54 struct url_registrar *ureg = &_url_mbox_registrar;
...@@ -64,7 +64,7 @@ url_file_init (url_t *purl, const char *name) ...@@ -64,7 +64,7 @@ url_file_init (url_t *purl, const char *name)
64 return ENOMEM; 64 return ENOMEM;
65 65
66 /* TYPE */ 66 /* TYPE */
67 url->_init = ureg->_init; 67 url->_create = ureg->_create;
68 url->_destroy = ureg->_destroy; 68 url->_destroy = ureg->_destroy;
69 69
70 /* SCHEME */ 70 /* SCHEME */
......
...@@ -19,13 +19,13 @@ ...@@ -19,13 +19,13 @@
19 #include <registrar.h> 19 #include <registrar.h>
20 #include <errno.h> 20 #include <errno.h>
21 21
22 static int url_imap_init (url_t *purl, const char *name); 22 static int url_imap_create (url_t *purl, const char *name);
23 static void url_imap_destroy (url_t *purl); 23 static void url_imap_destroy (url_t *purl);
24 24
25 struct url_registrar _url_imap_registrar = 25 struct url_registrar _url_imap_registrar =
26 { 26 {
27 "imap://", 27 "imap://",
28 url_imap_init, url_imap_destroy 28 url_imap_create, url_imap_destroy
29 }; 29 };
30 30
31 static void 31 static void
...@@ -36,7 +36,7 @@ url_imap_destroy (url_t *purl) ...@@ -36,7 +36,7 @@ url_imap_destroy (url_t *purl)
36 } 36 }
37 37
38 static int 38 static int
39 url_imap_init (url_t *purl, const char *name) 39 url_imap_create (url_t *purl, const char *name)
40 { 40 {
41 (void)purl; (void)name; 41 (void)purl; (void)name;
42 return ENOSYS; 42 return ENOSYS;
......
...@@ -19,13 +19,13 @@ ...@@ -19,13 +19,13 @@
19 #include <registrar.h> 19 #include <registrar.h>
20 #include <errno.h> 20 #include <errno.h>
21 21
22 static int url_mailto_init (url_t *purl, const char *name); 22 static int url_mailto_create (url_t *purl, const char *name);
23 static void url_mailto_destroy (url_t *purl); 23 static void url_mailto_destroy (url_t *purl);
24 24
25 struct url_registrar _url_mailto_registrar = 25 struct url_registrar _url_mailto_registrar =
26 { 26 {
27 "mailto:", 27 "mailto:",
28 url_mailto_init, url_mailto_destroy 28 url_mailto_create, url_mailto_destroy
29 }; 29 };
30 30
31 static void 31 static void
...@@ -36,7 +36,7 @@ url_mailto_destroy (url_t *purl) ...@@ -36,7 +36,7 @@ url_mailto_destroy (url_t *purl)
36 } 36 }
37 37
38 static int 38 static int
39 url_mailto_init (url_t *purl, const char *name) 39 url_mailto_create (url_t *purl, const char *name)
40 { 40 {
41 (void)purl; (void)name; 41 (void)purl; (void)name;
42 return ENOSYS; 42 return ENOSYS;
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_mbox_destroy (url_t *purl); 25 static void url_mbox_destroy (url_t *purl);
26 static int url_mbox_init (url_t *purl, const char *name); 26 static int url_mbox_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_mbox_registrar = 28 struct url_registrar _url_mbox_registrar =
29 { 29 {
30 "/", 30 "/",
31 url_mbox_init, url_mbox_destroy 31 url_mbox_create, url_mbox_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -48,7 +48,7 @@ url_mbox_destroy (url_t *purl) ...@@ -48,7 +48,7 @@ url_mbox_destroy (url_t *purl)
48 UNIX box 48 UNIX box
49 */ 49 */
50 static int 50 static int
51 url_mbox_init (url_t *purl, const char *name) 51 url_mbox_create (url_t *purl, const char *name)
52 { 52 {
53 url_t url; 53 url_t url;
54 struct url_registrar *ureg = &_url_mbox_registrar; 54 struct url_registrar *ureg = &_url_mbox_registrar;
...@@ -64,7 +64,7 @@ url_mbox_init (url_t *purl, const char *name) ...@@ -64,7 +64,7 @@ url_mbox_init (url_t *purl, const char *name)
64 return ENOMEM; 64 return ENOMEM;
65 65
66 /* TYPE */ 66 /* TYPE */
67 url->_init = ureg->_init; 67 url->_create = ureg->_create;
68 url->_destroy = ureg->_destroy; 68 url->_destroy = ureg->_destroy;
69 69
70 /* SCHEME */ 70 /* SCHEME */
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_maildir_destroy (url_t *purl); 25 static void url_maildir_destroy (url_t *purl);
26 static int url_maildir_init (url_t *purl, const char *name); 26 static int url_maildir_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_maildir_registrar = 28 struct url_registrar _url_maildir_registrar =
29 { 29 {
30 "maildir:", 30 "maildir:",
31 url_maildir_init, url_maildir_destroy 31 url_maildir_create, url_maildir_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -49,7 +49,7 @@ url_maildir_destroy (url_t *purl) ...@@ -49,7 +49,7 @@ url_maildir_destroy (url_t *purl)
49 maildir: 49 maildir:
50 */ 50 */
51 static int 51 static int
52 url_maildir_init (url_t *purl, const char *name) 52 url_maildir_create (url_t *purl, const char *name)
53 { 53 {
54 url_t url; 54 url_t url;
55 struct url_registrar *ureg = &_url_maildir_registrar; 55 struct url_registrar *ureg = &_url_maildir_registrar;
...@@ -67,7 +67,7 @@ url_maildir_init (url_t *purl, const char *name) ...@@ -67,7 +67,7 @@ url_maildir_init (url_t *purl, const char *name)
67 return EINVAL; 67 return EINVAL;
68 68
69 /* TYPE */ 69 /* TYPE */
70 url->_init = ureg->_init; 70 url->_create = ureg->_create;
71 url->_destroy = ureg->_destroy; 71 url->_destroy = ureg->_destroy;
72 72
73 /* SCHEME */ 73 /* SCHEME */
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_mh_destroy (url_t *purl); 25 static void url_mh_destroy (url_t *purl);
26 static int url_mh_init (url_t *purl, const char *name); 26 static int url_mh_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_mh_registrar = 28 struct url_registrar _url_mh_registrar =
29 { 29 {
30 "mh:", 30 "mh:",
31 url_mh_init, url_mh_destroy 31 url_mh_create, url_mh_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -49,7 +49,7 @@ url_mh_destroy (url_t *purl) ...@@ -49,7 +49,7 @@ url_mh_destroy (url_t *purl)
49 mh: 49 mh:
50 */ 50 */
51 static int 51 static int
52 url_mh_init (url_t *purl, const char *name) 52 url_mh_create (url_t *purl, const char *name)
53 { 53 {
54 url_t url; 54 url_t url;
55 struct url_registrar *ureg = &_url_mh_registrar; 55 struct url_registrar *ureg = &_url_mh_registrar;
...@@ -67,7 +67,7 @@ url_mh_init (url_t *purl, const char *name) ...@@ -67,7 +67,7 @@ url_mh_init (url_t *purl, const char *name)
67 return ENOMEM; 67 return ENOMEM;
68 68
69 /* TYPE */ 69 /* TYPE */
70 url->_init = ureg->_init; 70 url->_create = ureg->_create;
71 url->_destroy = ureg->_destroy; 71 url->_destroy = ureg->_destroy;
72 72
73 /* SCHEME */ 73 /* SCHEME */
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_mmdf_destroy (url_t *purl); 25 static void url_mmdf_destroy (url_t *purl);
26 static int url_mmdf_init (url_t *purl, const char *name); 26 static int url_mmdf_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_mmdf_registrar = 28 struct url_registrar _url_mmdf_registrar =
29 { 29 {
30 "mmdf:", 30 "mmdf:",
31 url_mmdf_init, url_mmdf_destroy 31 url_mmdf_create, url_mmdf_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -49,7 +49,7 @@ url_mmdf_destroy (url_t *purl) ...@@ -49,7 +49,7 @@ url_mmdf_destroy (url_t *purl)
49 mmdf: 49 mmdf:
50 */ 50 */
51 static int 51 static int
52 url_mmdf_init (url_t *purl, const char *name) 52 url_mmdf_create (url_t *purl, const char *name)
53 { 53 {
54 url_t url; 54 url_t url;
55 struct url_registrar *ureg = &_url_mmdf_registrar; 55 struct url_registrar *ureg = &_url_mmdf_registrar;
...@@ -67,7 +67,7 @@ url_mmdf_init (url_t *purl, const char *name) ...@@ -67,7 +67,7 @@ url_mmdf_init (url_t *purl, const char *name)
67 return ENOMEM; 67 return ENOMEM;
68 68
69 /* TYPE */ 69 /* TYPE */
70 url->_init = ureg->_init; 70 url->_create = ureg->_create;
71 url->_destroy = ureg->_destroy; 71 url->_destroy = ureg->_destroy;
72 72
73 /* SCHEME */ 73 /* SCHEME */
......
...@@ -25,12 +25,12 @@ ...@@ -25,12 +25,12 @@
25 #include <errno.h> 25 #include <errno.h>
26 26
27 static void url_pop_destroy (url_t *purl); 27 static void url_pop_destroy (url_t *purl);
28 static int url_pop_init (url_t *purl, const char *name); 28 static int url_pop_create (url_t *purl, const char *name);
29 29
30 struct url_registrar _url_pop_registrar = 30 struct url_registrar _url_pop_registrar =
31 { 31 {
32 "pop://", 32 "pop://",
33 url_pop_init, url_pop_destroy 33 url_pop_create, url_pop_destroy
34 }; 34 };
35 35
36 static int get_auth (const url_pop_t up, char *s, size_t len, size_t *); 36 static int get_auth (const url_pop_t up, char *s, size_t len, size_t *);
...@@ -80,7 +80,7 @@ url_pop_destroy (url_t *purl) ...@@ -80,7 +80,7 @@ url_pop_destroy (url_t *purl)
80 pop://<user>;AUTH=<auth>@<host>:<port> 80 pop://<user>;AUTH=<auth>@<host>:<port>
81 */ 81 */
82 static int 82 static int
83 url_pop_init (url_t *purl, const char *name) 83 url_pop_create (url_t *purl, const char *name)
84 { 84 {
85 const char *host_port, *indexe; 85 const char *host_port, *indexe;
86 struct url_registrar *ureg = &_url_pop_registrar; 86 struct url_registrar *ureg = &_url_pop_registrar;
...@@ -101,7 +101,7 @@ url_pop_init (url_t *purl, const char *name) ...@@ -101,7 +101,7 @@ url_pop_init (url_t *purl, const char *name)
101 up->_get_auth = get_auth; 101 up->_get_auth = get_auth;
102 102
103 /* TYPE */ 103 /* TYPE */
104 url->_init = _url_pop_registrar._init; 104 url->_create = _url_pop_registrar._create;
105 url->_destroy = _url_pop_registrar._destroy; 105 url->_destroy = _url_pop_registrar._destroy;
106 106
107 /* SCHEME */ 107 /* SCHEME */
......
...@@ -23,12 +23,12 @@ ...@@ -23,12 +23,12 @@
23 #include <string.h> 23 #include <string.h>
24 24
25 static void url_unix_destroy (url_t *purl); 25 static void url_unix_destroy (url_t *purl);
26 static int url_unix_init (url_t *purl, const char *name); 26 static int url_unix_create (url_t *purl, const char *name);
27 27
28 struct url_registrar _url_unix_registrar = 28 struct url_registrar _url_unix_registrar =
29 { 29 {
30 "unix:", 30 "unix:",
31 url_unix_init, url_unix_destroy 31 url_unix_create, url_unix_destroy
32 }; 32 };
33 33
34 static void 34 static void
...@@ -49,7 +49,7 @@ url_unix_destroy (url_t *purl) ...@@ -49,7 +49,7 @@ url_unix_destroy (url_t *purl)
49 unix:/path 49 unix:/path
50 */ 50 */
51 static int 51 static int
52 url_unix_init (url_t *purl, const char *name) 52 url_unix_create (url_t *purl, const char *name)
53 { 53 {
54 url_t url; 54 url_t url;
55 struct url_registrar *ureg = &_url_unix_registrar; 55 struct url_registrar *ureg = &_url_unix_registrar;
...@@ -67,7 +67,7 @@ url_unix_init (url_t *purl, const char *name) ...@@ -67,7 +67,7 @@ url_unix_init (url_t *purl, const char *name)
67 return ENOMEM; 67 return ENOMEM;
68 68
69 /* TYPE */ 69 /* TYPE */
70 url->_init = ureg->_init; 70 url->_create = ureg->_create;
71 url->_destroy = ureg->_destroy; 71 url->_destroy = ureg->_destroy;
72 72
73 /* SCHEME */ 73 /* SCHEME */
......