body.c header.c io.c mailbox.c mapfile_stream.c mbx_pop.c
mbx_unix.c message.c include/private/io0.h include/private/mailbox0.h include/private/message0.h include/public/header.h include/public/io.h include/public/mailbox.h include/public/message.h Try new mailutils_error.h
Showing
15 changed files
with
250 additions
and
45 deletions
... | @@ -308,6 +308,7 @@ lazy_create () | ... | @@ -308,6 +308,7 @@ lazy_create () |
308 | (void)remove(tmpbuf); | 308 | (void)remove(tmpbuf); |
309 | #else | 309 | #else |
310 | file = tmpfile (); | 310 | file = tmpfile (); |
311 | //file = fopen ("/tmp/mystuff", "w+"); | ||
311 | /* make sure the mode is right */ | 312 | /* make sure the mode is right */ |
312 | if (file) | 313 | if (file) |
313 | fchmod (fileno (file), 0600); | 314 | fchmod (fileno (file), 0600); | ... | ... |
... | @@ -51,6 +51,7 @@ struct _header | ... | @@ -51,6 +51,7 @@ struct _header |
51 | 51 | ||
52 | /* streams */ | 52 | /* streams */ |
53 | stream_t stream; | 53 | stream_t stream; |
54 | int (*_get_value) __P ((header_t, const char *, char *, size_t , size_t *)); | ||
54 | 55 | ||
55 | /* owner ? */ | 56 | /* owner ? */ |
56 | void *owner; | 57 | void *owner; |
... | @@ -202,7 +203,7 @@ header_parse (header_t header, char *blurb, int len) | ... | @@ -202,7 +203,7 @@ header_parse (header_t header, char *blurb, int len) |
202 | return 0; | 203 | return 0; |
203 | } | 204 | } |
204 | 205 | ||
205 | /* FIXME: grossly inneficient, to many copies and reallocating | 206 | /* FIXME: grossly inneficient, to many copies and reallocating. |
206 | * This all header business need a good rewrite. | 207 | * This all header business need a good rewrite. |
207 | */ | 208 | */ |
208 | int | 209 | int |
... | @@ -264,6 +265,20 @@ header_set_value (header_t header, const char *fn, const char *fv, int replace) | ... | @@ -264,6 +265,20 @@ header_set_value (header_t header, const char *fn, const char *fv, int replace) |
264 | } | 265 | } |
265 | 266 | ||
266 | int | 267 | int |
268 | header_set_get_value (header_t header, int (*_get_value) | ||
269 | (header_t, const char *, char *, size_t, size_t *), | ||
270 | void *owner) | ||
271 | { | ||
272 | if (header == NULL) | ||
273 | return EINVAL; | ||
274 | if (header->owner != owner) | ||
275 | return EACCES; | ||
276 | |||
277 | header->_get_value = _get_value; | ||
278 | return 0; | ||
279 | } | ||
280 | |||
281 | int | ||
267 | header_get_value (header_t header, const char *name, char *buffer, | 282 | header_get_value (header_t header, const char *name, char *buffer, |
268 | size_t buflen, size_t *pn) | 283 | size_t buflen, size_t *pn) |
269 | { | 284 | { |
... | @@ -319,7 +334,16 @@ header_get_value (header_t header, const char *name, char *buffer, | ... | @@ -319,7 +334,16 @@ header_get_value (header_t header, const char *name, char *buffer, |
319 | if (pn) | 334 | if (pn) |
320 | *pn = total; | 335 | *pn = total; |
321 | if (total == 0) | 336 | if (total == 0) |
322 | return ENOENT; | 337 | { |
338 | int err = ENOENT; | ||
339 | /* check if they provided a hook */ | ||
340 | if (header->_get_value != NULL) | ||
341 | err = header->_get_value (header, name, buffer, buflen, pn); | ||
342 | /* cache it locally */ | ||
343 | if (err == 0) | ||
344 | header_set_value (header, name, buffer, 0); | ||
345 | return err; | ||
346 | } | ||
323 | return 0; | 347 | return 0; |
324 | } | 348 | } |
325 | 349 | ... | ... |
... | @@ -36,6 +36,7 @@ struct _stream | ... | @@ -36,6 +36,7 @@ struct _stream |
36 | { | 36 | { |
37 | void *owner; | 37 | void *owner; |
38 | int flags; | 38 | int flags; |
39 | int state; | ||
39 | void (*_destroy) __P ((stream_t)); | 40 | void (*_destroy) __P ((stream_t)); |
40 | int (*_open) __P ((stream_t, const char *, int port, int flags)); | 41 | int (*_open) __P ((stream_t, const char *, int port, int flags)); |
41 | int (*_close) __P ((stream_t)); | 42 | int (*_close) __P ((stream_t)); | ... | ... |
... | @@ -52,7 +52,9 @@ struct _mailbox | ... | @@ -52,7 +52,9 @@ struct _mailbox |
52 | /* debug information */ | 52 | /* debug information */ |
53 | int debug_level; | 53 | int debug_level; |
54 | void *debug_arg; | 54 | void *debug_arg; |
55 | int (*debug_print) __P ((const char *, void *arg)); | 55 | char *debug_buffer; |
56 | size_t debug_bufsize; | ||
57 | int (*debug_print) __P ((void *arg, const char *, size_t)); | ||
56 | 58 | ||
57 | /* Back pointer to the specific mailbox */ | 59 | /* Back pointer to the specific mailbox */ |
58 | void *data; | 60 | void *data; |
... | @@ -85,6 +87,7 @@ extern int mailbox_num_deleted __P ((mailbox_t, size_t *)); | ... | @@ -85,6 +87,7 @@ extern int mailbox_num_deleted __P ((mailbox_t, size_t *)); |
85 | 87 | ||
86 | extern int mailbox_notification __P ((mailbox_t mbox, size_t type)); | 88 | extern int mailbox_notification __P ((mailbox_t mbox, size_t type)); |
87 | 89 | ||
90 | extern int mailbox_debug __P ((mailbox_t, int level, const char *fmt, ...)); | ||
88 | 91 | ||
89 | #ifdef __cplusplus | 92 | #ifdef __cplusplus |
90 | } | 93 | } | ... | ... |
... | @@ -60,6 +60,7 @@ struct _message | ... | @@ -60,6 +60,7 @@ struct _message |
60 | 60 | ||
61 | int (*_from) __P ((message_t msg, char *, size_t, size_t *)); | 61 | int (*_from) __P ((message_t msg, char *, size_t, size_t *)); |
62 | int (*_received) __P ((message_t msg, char *, size_t, size_t *)); | 62 | int (*_received) __P ((message_t msg, char *, size_t, size_t *)); |
63 | int (*_get_uidl) __P ((message_t msg, char *, size_t, size_t *)); | ||
63 | 64 | ||
64 | }; | 65 | }; |
65 | 66 | ... | ... |
... | @@ -73,6 +73,11 @@ extern void header_destroy __P ((header_t *, void *owner)); | ... | @@ -73,6 +73,11 @@ extern void header_destroy __P ((header_t *, void *owner)); |
73 | 73 | ||
74 | extern int header_set_value __P ((header_t, const char *fn, | 74 | extern int header_set_value __P ((header_t, const char *fn, |
75 | const char *fv, int replace)); | 75 | const char *fv, int replace)); |
76 | |||
77 | extern int header_set_get_value __P ((header_t, int (*_get_value) | ||
78 | __P ((header_t, const char *fn, char *buf, | ||
79 | size_t buflen, size_t *nwritten)), | ||
80 | void *owner)); | ||
76 | extern int header_get_value __P ((header_t, const char *fn, char *buf, | 81 | extern int header_get_value __P ((header_t, const char *fn, char *buf, |
77 | size_t buflen, size_t *nwritten)); | 82 | size_t buflen, size_t *nwritten)); |
78 | extern int header_entry_count __P ((header_t, size_t *num)); | 83 | extern int header_entry_count __P ((header_t, size_t *num)); | ... | ... |
... | @@ -35,13 +35,13 @@ extern "C" { /*}*/ | ... | @@ -35,13 +35,13 @@ extern "C" { /*}*/ |
35 | struct _stream; | 35 | struct _stream; |
36 | typedef struct _stream *stream_t; | 36 | typedef struct _stream *stream_t; |
37 | 37 | ||
38 | /* stream will be destroy on stream_destroy */ | ||
39 | #define MU_STREAM_READ 0x00000001 | 38 | #define MU_STREAM_READ 0x00000001 |
40 | #define MU_STREAM_WRITE 0x00000002 | 39 | #define MU_STREAM_WRITE 0x00000002 |
41 | #define MU_STREAM_RDWR 0x00000004 | 40 | #define MU_STREAM_RDWR 0x00000004 |
42 | #define MU_STREAM_APPEND 0x00000008 | 41 | #define MU_STREAM_APPEND 0x00000008 |
43 | #define MU_STREAM_CREAT 0x00000010 | 42 | #define MU_STREAM_CREAT 0x00000010 |
44 | #define MU_STREAM_NONBLOCK 0x00000020 | 43 | #define MU_STREAM_NONBLOCK 0x00000020 |
44 | /* stream will be destroy on stream_destroy */ | ||
45 | #define MU_STREAM_NO_CHECK 0x00000040 | 45 | #define MU_STREAM_NO_CHECK 0x00000040 |
46 | 46 | ||
47 | extern int stream_create __P ((stream_t *, int flags, void *owner)); | 47 | extern int stream_create __P ((stream_t *, int flags, void *owner)); |
... | @@ -99,6 +99,12 @@ extern int stream_set_flush __P ((stream_t, int (*_flush) | ... | @@ -99,6 +99,12 @@ extern int stream_set_flush __P ((stream_t, int (*_flush) |
99 | extern int stream_get_flags __P ((stream_t, int *pflags)); | 99 | extern int stream_get_flags __P ((stream_t, int *pflags)); |
100 | extern int stream_set_flags __P ((stream_t, int flags, void *owner)); | 100 | extern int stream_set_flags __P ((stream_t, int flags, void *owner)); |
101 | 101 | ||
102 | #define MU_STREAM_STATE_OPEN 1 | ||
103 | #define MU_STREAM_STATE_READ 2 | ||
104 | #define MU_STREAM_STATE_WRITE 4 | ||
105 | #define MU_STREAM_STATE_CLOSE 8 | ||
106 | extern int stream_get_state __P ((stream_t, int *pstate)); | ||
107 | |||
102 | /* misc */ | 108 | /* misc */ |
103 | extern int file_stream_create __P ((stream_t *stream)); | 109 | extern int file_stream_create __P ((stream_t *stream)); |
104 | extern int mapfile_stream_create __P ((stream_t *stream)); | 110 | extern int mapfile_stream_create __P ((stream_t *stream)); | ... | ... |
... | @@ -99,10 +99,13 @@ extern int mailbox_register __P ((mailbox_t mbox, size_t type, | ... | @@ -99,10 +99,13 @@ extern int mailbox_register __P ((mailbox_t mbox, size_t type, |
99 | extern int mailbox_deregister __P ((mailbox_t mbox, void *action)); | 99 | extern int mailbox_deregister __P ((mailbox_t mbox, void *action)); |
100 | 100 | ||
101 | /* trace */ | 101 | /* trace */ |
102 | #define MU_MAILBOX_DEBUG_TRACE 1 | ||
103 | #define MU_MAILBOX_DEBUG_PROT 2 | ||
102 | extern int mailbox_set_debug_level __P ((mailbox_t mbox, size_t level)); | 104 | extern int mailbox_set_debug_level __P ((mailbox_t mbox, size_t level)); |
103 | extern int mailbox_get_debug_level __P ((mailbox_t mbox, size_t *plevel)); | 105 | extern int mailbox_get_debug_level __P ((mailbox_t mbox, size_t *plevel)); |
104 | extern int mailbox_set_debug_print __P ((mailbox_t mbox, int (*debug_print) | 106 | extern int mailbox_set_debug_print __P ((mailbox_t mbox, int (*debug_print) |
105 | __P ((const char *, void *arg)), | 107 | __P ((void *arg, const char *, |
108 | size_t)), | ||
106 | void *arg)); | 109 | void *arg)); |
107 | 110 | ||
108 | 111 | ... | ... |
... | @@ -83,6 +83,11 @@ extern int message_set_get_part __P ((message_t, size_t part, message_t *msg)); | ... | @@ -83,6 +83,11 @@ extern int message_set_get_part __P ((message_t, size_t part, message_t *msg)); |
83 | extern int message_add_part __P ((message_t, message_t msg)); | 83 | extern int message_add_part __P ((message_t, message_t msg)); |
84 | extern int message_set_add_part __P ((message_t, message_t msg)); | 84 | extern int message_set_add_part __P ((message_t, message_t msg)); |
85 | 85 | ||
86 | extern int message_get_uidl __P ((message_t, char *buffer, size_t, size_t *)); | ||
87 | extern int message_set_uidl __P ((message_t, int (*_get_uidl) | ||
88 | __P ((message_t, char *, size_t, size_t *)), | ||
89 | void *owner)); | ||
90 | |||
86 | /* events */ | 91 | /* events */ |
87 | #define MU_EVT_MSG_DESTROY 32 | 92 | #define MU_EVT_MSG_DESTROY 32 |
88 | extern int message_register __P ((message_t msg, size_t type, int (*action) | 93 | extern int message_register __P ((message_t msg, size_t type, int (*action) | ... | ... |
... | @@ -70,6 +70,7 @@ stream_open (stream_t stream, const char *name, int port, int flags) | ... | @@ -70,6 +70,7 @@ stream_open (stream_t stream, const char *name, int port, int flags) |
70 | { | 70 | { |
71 | if (stream == NULL) | 71 | if (stream == NULL) |
72 | return EINVAL; | 72 | return EINVAL; |
73 | stream->state = MU_STREAM_STATE_OPEN; | ||
73 | if (stream->_open) | 74 | if (stream->_open) |
74 | return stream->_open (stream, name, port, flags); | 75 | return stream->_open (stream, name, port, flags); |
75 | return 0; | 76 | return 0; |
... | @@ -94,6 +95,7 @@ stream_close (stream_t stream) | ... | @@ -94,6 +95,7 @@ stream_close (stream_t stream) |
94 | { | 95 | { |
95 | if (stream == NULL) | 96 | if (stream == NULL) |
96 | return EINVAL; | 97 | return EINVAL; |
98 | stream->state = MU_STREAM_STATE_CLOSE; | ||
97 | if (stream->_close) | 99 | if (stream->_close) |
98 | return stream->_close (stream); | 100 | return stream->_close (stream); |
99 | return 0; | 101 | return 0; |
... | @@ -176,6 +178,7 @@ stream_read (stream_t is, char *buf, size_t count, | ... | @@ -176,6 +178,7 @@ stream_read (stream_t is, char *buf, size_t count, |
176 | { | 178 | { |
177 | if (is == NULL || is->_read == NULL) | 179 | if (is == NULL || is->_read == NULL) |
178 | return EINVAL; | 180 | return EINVAL; |
181 | is->state = MU_STREAM_STATE_READ; | ||
179 | return is->_read (is, buf, count, offset, pnread); | 182 | return is->_read (is, buf, count, offset, pnread); |
180 | } | 183 | } |
181 | 184 | ||
... | @@ -188,6 +191,9 @@ stream_readline (stream_t is, char *buf, size_t count, | ... | @@ -188,6 +191,9 @@ stream_readline (stream_t is, char *buf, size_t count, |
188 | int status; | 191 | int status; |
189 | if (is == NULL) | 192 | if (is == NULL) |
190 | return EINVAL; | 193 | return EINVAL; |
194 | |||
195 | is->state = MU_STREAM_STATE_READ; | ||
196 | |||
191 | if (is->_readline != NULL) | 197 | if (is->_readline != NULL) |
192 | return is->_readline (is, buf, count, offset, pnread); | 198 | return is->_readline (is, buf, count, offset, pnread); |
193 | 199 | ||
... | @@ -225,6 +231,7 @@ stream_write (stream_t os, const char *buf, size_t count, | ... | @@ -225,6 +231,7 @@ stream_write (stream_t os, const char *buf, size_t count, |
225 | { | 231 | { |
226 | if (os == NULL || os->_write == NULL) | 232 | if (os == NULL || os->_write == NULL) |
227 | return EINVAL; | 233 | return EINVAL; |
234 | os->state = MU_STREAM_STATE_WRITE; | ||
228 | return os->_write (os, buf, count, offset, pnwrite); | 235 | return os->_write (os, buf, count, offset, pnwrite); |
229 | } | 236 | } |
230 | 237 | ||
... | @@ -315,3 +322,12 @@ stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner) | ... | @@ -315,3 +322,12 @@ stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner) |
315 | stream->_flush = _flush; | 322 | stream->_flush = _flush; |
316 | return 0; | 323 | return 0; |
317 | } | 324 | } |
325 | |||
326 | int | ||
327 | stream_get_state (stream_t stream, int *pstate) | ||
328 | { | ||
329 | if (stream == NULL || pstate == NULL) | ||
330 | return EINVAL; | ||
331 | *pstate = stream->state; | ||
332 | return 0; | ||
333 | } | ... | ... |
... | @@ -23,9 +23,12 @@ | ... | @@ -23,9 +23,12 @@ |
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 <mailutils_errno.h> | ||
26 | 27 | ||
28 | #include <stdio.h> | ||
27 | #include <stdlib.h> | 29 | #include <stdlib.h> |
28 | #include <string.h> | 30 | #include <string.h> |
31 | #include <stdarg.h> | ||
29 | #include <errno.h> | 32 | #include <errno.h> |
30 | 33 | ||
31 | /* | 34 | /* |
... | @@ -37,7 +40,7 @@ | ... | @@ -37,7 +40,7 @@ |
37 | int | 40 | int |
38 | mailbox_create (mailbox_t *pmbox, const char *name, int id) | 41 | mailbox_create (mailbox_t *pmbox, const char *name, int id) |
39 | { | 42 | { |
40 | int status = EINVAL; | 43 | int status = MU_ERROR_INVALID_ARG; |
41 | struct mailbox_registrar *mreg; | 44 | struct mailbox_registrar *mreg; |
42 | url_t url = NULL; | 45 | url_t url = NULL; |
43 | 46 | ||
... | @@ -80,7 +83,7 @@ int | ... | @@ -80,7 +83,7 @@ int |
80 | mailbox_open (mailbox_t mbox, int flag) | 83 | mailbox_open (mailbox_t mbox, int flag) |
81 | { | 84 | { |
82 | if (mbox == NULL || mbox->_open == NULL) | 85 | if (mbox == NULL || mbox->_open == NULL) |
83 | return ENOSYS; | 86 | return MU_ERROR_NOT_IMPLEMENTED; |
84 | return mbox->_open (mbox, flag); | 87 | return mbox->_open (mbox, flag); |
85 | } | 88 | } |
86 | 89 | ||
... | @@ -88,7 +91,7 @@ int | ... | @@ -88,7 +91,7 @@ int |
88 | mailbox_close (mailbox_t mbox) | 91 | mailbox_close (mailbox_t mbox) |
89 | { | 92 | { |
90 | if (mbox == NULL || mbox->_close == NULL) | 93 | if (mbox == NULL || mbox->_close == NULL) |
91 | return ENOSYS; | 94 | return MU_ERROR_NOT_IMPLEMENTED; |
92 | return mbox->_close (mbox); | 95 | return mbox->_close (mbox); |
93 | } | 96 | } |
94 | 97 | ||
... | @@ -97,7 +100,7 @@ int | ... | @@ -97,7 +100,7 @@ int |
97 | mailbox_append_message (mailbox_t mbox, message_t msg) | 100 | mailbox_append_message (mailbox_t mbox, message_t msg) |
98 | { | 101 | { |
99 | if (mbox == NULL || mbox->_append_message == NULL) | 102 | if (mbox == NULL || mbox->_append_message == NULL) |
100 | return ENOSYS; | 103 | return MU_ERROR_NOT_IMPLEMENTED; |
101 | return mbox->_append_message (mbox, msg); | 104 | return mbox->_append_message (mbox, msg); |
102 | } | 105 | } |
103 | 106 | ||
... | @@ -105,7 +108,7 @@ int | ... | @@ -105,7 +108,7 @@ int |
105 | mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | 108 | mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
106 | { | 109 | { |
107 | if (mbox == NULL || mbox->_get_message == NULL) | 110 | if (mbox == NULL || mbox->_get_message == NULL) |
108 | return EINVAL; | 111 | return MU_ERROR_INVALID_ARG; |
109 | return mbox->_get_message (mbox, msgno, pmsg); | 112 | return mbox->_get_message (mbox, msgno, pmsg); |
110 | } | 113 | } |
111 | 114 | ||
... | @@ -113,7 +116,7 @@ int | ... | @@ -113,7 +116,7 @@ int |
113 | mailbox_messages_count (mailbox_t mbox, size_t *num) | 116 | mailbox_messages_count (mailbox_t mbox, size_t *num) |
114 | { | 117 | { |
115 | if (mbox == NULL || mbox->_messages_count == NULL) | 118 | if (mbox == NULL || mbox->_messages_count == NULL) |
116 | return ENOSYS; | 119 | return MU_ERROR_NOT_IMPLEMENTED; |
117 | return mbox->_messages_count (mbox, num); | 120 | return mbox->_messages_count (mbox, num); |
118 | } | 121 | } |
119 | 122 | ||
... | @@ -121,7 +124,7 @@ int | ... | @@ -121,7 +124,7 @@ int |
121 | mailbox_expunge (mailbox_t mbox) | 124 | mailbox_expunge (mailbox_t mbox) |
122 | { | 125 | { |
123 | if (mbox == NULL || mbox->_expunge == NULL) | 126 | if (mbox == NULL || mbox->_expunge == NULL) |
124 | return ENOSYS; | 127 | return MU_ERROR_NOT_IMPLEMENTED; |
125 | return mbox->_expunge (mbox); | 128 | return mbox->_expunge (mbox); |
126 | } | 129 | } |
127 | 130 | ||
... | @@ -129,7 +132,7 @@ int | ... | @@ -129,7 +132,7 @@ int |
129 | mailbox_num_deleted (mailbox_t mbox, size_t *num) | 132 | mailbox_num_deleted (mailbox_t mbox, size_t *num) |
130 | { | 133 | { |
131 | if (mbox == NULL || mbox->_num_deleted == NULL) | 134 | if (mbox == NULL || mbox->_num_deleted == NULL) |
132 | return EINVAL; | 135 | return MU_ERROR_INVALID_ARG; |
133 | return mbox->_num_deleted (mbox, num); | 136 | return mbox->_num_deleted (mbox, num); |
134 | } | 137 | } |
135 | 138 | ||
... | @@ -154,7 +157,7 @@ int | ... | @@ -154,7 +157,7 @@ int |
154 | mailbox_set_locker (mailbox_t mbox, locker_t locker) | 157 | mailbox_set_locker (mailbox_t mbox, locker_t locker) |
155 | { | 158 | { |
156 | if (mbox == NULL) | 159 | if (mbox == NULL) |
157 | return EINVAL; | 160 | return MU_ERROR_INVALID_ARG; |
158 | if (mbox->locker != NULL) | 161 | if (mbox->locker != NULL) |
159 | locker_destroy (&mbox->locker); | 162 | locker_destroy (&mbox->locker); |
160 | mbox->locker = locker; | 163 | mbox->locker = locker; |
... | @@ -165,7 +168,7 @@ int | ... | @@ -165,7 +168,7 @@ int |
165 | mailbox_get_locker (mailbox_t mbox, locker_t *plocker) | 168 | mailbox_get_locker (mailbox_t mbox, locker_t *plocker) |
166 | { | 169 | { |
167 | if (mbox == NULL || plocker == NULL) | 170 | if (mbox == NULL || plocker == NULL) |
168 | return EINVAL; | 171 | return MU_ERROR_INVALID_ARG; |
169 | if (plocker) | 172 | if (plocker) |
170 | *plocker = mbox->locker; | 173 | *plocker = mbox->locker; |
171 | return 0; | 174 | return 0; |
... | @@ -175,7 +178,7 @@ int | ... | @@ -175,7 +178,7 @@ int |
175 | mailbox_set_auth (mailbox_t mbox, auth_t auth) | 178 | mailbox_set_auth (mailbox_t mbox, auth_t auth) |
176 | { | 179 | { |
177 | if (mbox == NULL) | 180 | if (mbox == NULL) |
178 | return EINVAL; | 181 | return MU_ERROR_INVALID_ARG; |
179 | mbox->auth = auth; | 182 | mbox->auth = auth; |
180 | return 0; | 183 | return 0; |
181 | } | 184 | } |
... | @@ -184,7 +187,7 @@ int | ... | @@ -184,7 +187,7 @@ int |
184 | mailbox_get_auth (mailbox_t mbox, auth_t *pauth) | 187 | mailbox_get_auth (mailbox_t mbox, auth_t *pauth) |
185 | { | 188 | { |
186 | if (mbox == NULL || pauth == NULL) | 189 | if (mbox == NULL || pauth == NULL) |
187 | return EINVAL; | 190 | return MU_ERROR_INVALID_ARG; |
188 | if (pauth) | 191 | if (pauth) |
189 | *pauth = mbox->auth; | 192 | *pauth = mbox->auth; |
190 | return 0; | 193 | return 0; |
... | @@ -194,7 +197,7 @@ int | ... | @@ -194,7 +197,7 @@ int |
194 | mailbox_set_stream (mailbox_t mbox, stream_t stream) | 197 | mailbox_set_stream (mailbox_t mbox, stream_t stream) |
195 | { | 198 | { |
196 | if (mbox == NULL) | 199 | if (mbox == NULL) |
197 | return EINVAL; | 200 | return MU_ERROR_INVALID_ARG; |
198 | mbox->stream = stream; | 201 | mbox->stream = stream; |
199 | return 0; | 202 | return 0; |
200 | } | 203 | } |
... | @@ -203,7 +206,7 @@ int | ... | @@ -203,7 +206,7 @@ int |
203 | mailbox_get_stream (mailbox_t mbox, stream_t *pstream) | 206 | mailbox_get_stream (mailbox_t mbox, stream_t *pstream) |
204 | { | 207 | { |
205 | if (mbox == NULL || pstream == NULL) | 208 | if (mbox == NULL || pstream == NULL) |
206 | return EINVAL; | 209 | return MU_ERROR_INVALID_ARG; |
207 | if (pstream) | 210 | if (pstream) |
208 | *pstream = mbox->stream; | 211 | *pstream = mbox->stream; |
209 | return 0; | 212 | return 0; |
... | @@ -219,7 +222,7 @@ mailbox_register (mailbox_t mbox, size_t type, | ... | @@ -219,7 +222,7 @@ mailbox_register (mailbox_t mbox, size_t type, |
219 | 222 | ||
220 | /* FIXME: I should check for invalid types */ | 223 | /* FIXME: I should check for invalid types */ |
221 | if (mbox == NULL || action == NULL) | 224 | if (mbox == NULL || action == NULL) |
222 | return EINVAL; | 225 | return MU_ERROR_INVALID_ARG; |
223 | 226 | ||
224 | /* find a free spot */ | 227 | /* find a free spot */ |
225 | for (i = 0; i < mbox->event_num; i++) | 228 | for (i = 0; i < mbox->event_num; i++) |
... | @@ -237,7 +240,7 @@ mailbox_register (mailbox_t mbox, size_t type, | ... | @@ -237,7 +240,7 @@ mailbox_register (mailbox_t mbox, size_t type, |
237 | /* a new one */ | 240 | /* a new one */ |
238 | event = realloc (mbox->event, (mbox->event_num + 1) * sizeof (*event)); | 241 | event = realloc (mbox->event, (mbox->event_num + 1) * sizeof (*event)); |
239 | if (event == NULL) | 242 | if (event == NULL) |
240 | return ENOMEM; | 243 | return MU_ERROR_OUT_OF_MEMORY; |
241 | 244 | ||
242 | mbox->event = event; | 245 | mbox->event = event; |
243 | event[mbox->event_num]._action = action; | 246 | event[mbox->event_num]._action = action; |
... | @@ -264,7 +267,7 @@ mailbox_deregister (mailbox_t mbox, void *action) | ... | @@ -264,7 +267,7 @@ mailbox_deregister (mailbox_t mbox, void *action) |
264 | return 0; | 267 | return 0; |
265 | } | 268 | } |
266 | } | 269 | } |
267 | return ENOENT; | 270 | return MU_ERROR_NO_ENTRY; |
268 | } | 271 | } |
269 | 272 | ||
270 | int | 273 | int |
... | @@ -286,7 +289,7 @@ int | ... | @@ -286,7 +289,7 @@ int |
286 | mailbox_set_debug_level (mailbox_t mbox, size_t level) | 289 | mailbox_set_debug_level (mailbox_t mbox, size_t level) |
287 | { | 290 | { |
288 | if (mbox == NULL) | 291 | if (mbox == NULL) |
289 | return EINVAL; | 292 | return MU_ERROR_INVALID_ARG; |
290 | mbox->debug_level = level; | 293 | mbox->debug_level = level; |
291 | return 0; | 294 | return 0; |
292 | } | 295 | } |
... | @@ -295,18 +298,47 @@ int | ... | @@ -295,18 +298,47 @@ int |
295 | mailbox_get_debug_level (mailbox_t mbox, size_t *plevel) | 298 | mailbox_get_debug_level (mailbox_t mbox, size_t *plevel) |
296 | { | 299 | { |
297 | if (mbox == NULL || plevel == NULL) | 300 | if (mbox == NULL || plevel == NULL) |
298 | return EINVAL; | 301 | return MU_ERROR_INVALID_ARG; |
299 | *plevel = mbox->debug_level; | 302 | *plevel = mbox->debug_level; |
300 | return 0; | 303 | return 0; |
301 | } | 304 | } |
302 | 305 | ||
303 | int | 306 | int |
304 | mailbox_set_debug_print (mailbox_t mbox, int (*debug_print) | 307 | mailbox_set_debug_print (mailbox_t mbox, int (*debug_print) |
305 | (const char *, void *arg), void *arg) | 308 | (void *arg, const char *, size_t), void *arg) |
306 | { | 309 | { |
307 | if (mbox == NULL) | 310 | if (mbox == NULL) |
308 | return EINVAL; | 311 | return MU_ERROR_INVALID_ARG; |
309 | mbox->debug_print = debug_print; | 312 | mbox->debug_print = debug_print; |
310 | mbox->debug_arg = arg; | 313 | mbox->debug_arg = arg; |
311 | return 0; | 314 | return 0; |
312 | } | 315 | } |
316 | |||
317 | int | ||
318 | mailbox_debug (mailbox_t mbox, int level, const char *fmt, ...) | ||
319 | { | ||
320 | va_list ap; | ||
321 | if (mbox == NULL) | ||
322 | return MU_ERROR_INVALID_ARG; | ||
323 | |||
324 | if (!(mbox->debug_level & level)) | ||
325 | return 0; | ||
326 | |||
327 | va_start (ap, fmt); | ||
328 | if (mbox->debug_print) | ||
329 | { | ||
330 | int writen; | ||
331 | if (mbox->debug_buffer == NULL) | ||
332 | { | ||
333 | mbox->debug_bufsize = 255; | ||
334 | mbox->debug_buffer = malloc (mbox->debug_bufsize); | ||
335 | if (mbox->debug_buffer) | ||
336 | return MU_ERROR_OUT_OF_MEMORY; } | ||
337 | writen = vsnprintf (mbox->debug_buffer, mbox->debug_bufsize, fmt, ap); | ||
338 | mbox->debug_print (mbox->debug_arg, mbox->debug_buffer, writen); | ||
339 | } | ||
340 | else | ||
341 | vfprintf (stderr, fmt, ap); | ||
342 | va_end (ap); | ||
343 | return 0; | ||
344 | } | ... | ... |
... | @@ -60,6 +60,8 @@ _mapfile_read (stream_t stream, char *optr, size_t osize, | ... | @@ -60,6 +60,8 @@ _mapfile_read (stream_t stream, char *optr, size_t osize, |
60 | struct _mapfile_stream *mfs = stream->owner; | 60 | struct _mapfile_stream *mfs = stream->owner; |
61 | size_t n; | 61 | size_t n; |
62 | 62 | ||
63 | if (mfs->ptr == NULL) | ||
64 | return EINVAL; | ||
63 | if (offset >= (off_t)mfs->size) | 65 | if (offset >= (off_t)mfs->size) |
64 | { | 66 | { |
65 | if (nbytes) | 67 | if (nbytes) |
... | @@ -83,6 +85,8 @@ _mapfile_readline (stream_t stream, char *optr, size_t osize, | ... | @@ -83,6 +85,8 @@ _mapfile_readline (stream_t stream, char *optr, size_t osize, |
83 | char *nl; | 85 | char *nl; |
84 | size_t n = 0; | 86 | size_t n = 0; |
85 | 87 | ||
88 | if (mfs->ptr == NULL) | ||
89 | return EINVAL; | ||
86 | /* save space for the null byte */ | 90 | /* save space for the null byte */ |
87 | osize--; | 91 | osize--; |
88 | if (offset >= (off_t)mfs->size) | 92 | if (offset >= (off_t)mfs->size) |
... | @@ -108,6 +112,8 @@ _mapfile_write (stream_t stream, const char *iptr, size_t isize, | ... | @@ -108,6 +112,8 @@ _mapfile_write (stream_t stream, const char *iptr, size_t isize, |
108 | { | 112 | { |
109 | struct _mapfile_stream *mfs = stream->owner; | 113 | struct _mapfile_stream *mfs = stream->owner; |
110 | 114 | ||
115 | if (mfs->ptr == NULL) | ||
116 | return EINVAL; | ||
111 | if (! (mfs->flags & PROT_WRITE)) | 117 | if (! (mfs->flags & PROT_WRITE)) |
112 | return EACCES; | 118 | return EACCES; |
113 | 119 | ||
... | @@ -144,6 +150,8 @@ static int | ... | @@ -144,6 +150,8 @@ static int |
144 | _mapfile_truncate (stream_t stream, off_t len) | 150 | _mapfile_truncate (stream_t stream, off_t len) |
145 | { | 151 | { |
146 | struct _mapfile_stream *mfs = stream->owner; | 152 | struct _mapfile_stream *mfs = stream->owner; |
153 | if (mfs->ptr == NULL) | ||
154 | return EINVAL; | ||
147 | /* remap */ | 155 | /* remap */ |
148 | if (munmap (mfs->ptr, mfs->size) != 0) | 156 | if (munmap (mfs->ptr, mfs->size) != 0) |
149 | { | 157 | { |
... | @@ -170,6 +178,8 @@ _mapfile_size (stream_t stream, off_t *psize) | ... | @@ -170,6 +178,8 @@ _mapfile_size (stream_t stream, off_t *psize) |
170 | { | 178 | { |
171 | struct _mapfile_stream *mfs = stream->owner; | 179 | struct _mapfile_stream *mfs = stream->owner; |
172 | struct stat stbuf; | 180 | struct stat stbuf; |
181 | if (mfs->ptr == NULL) | ||
182 | return EINVAL; | ||
173 | msync (mfs->ptr, mfs->size, MS_SYNC); | 183 | msync (mfs->ptr, mfs->size, MS_SYNC); |
174 | if (fstat(mfs->fd, &stbuf) != 0) | 184 | if (fstat(mfs->fd, &stbuf) != 0) |
175 | return errno; | 185 | return errno; | ... | ... |
... | @@ -22,6 +22,7 @@ | ... | @@ -22,6 +22,7 @@ |
22 | #include <registrar0.h> | 22 | #include <registrar0.h> |
23 | #include <auth0.h> | 23 | #include <auth0.h> |
24 | #include <attribute.h> | 24 | #include <attribute.h> |
25 | #include <mailutils_errno.h> | ||
25 | 26 | ||
26 | #include <termios.h> | 27 | #include <termios.h> |
27 | #include <errno.h> | 28 | #include <errno.h> |
... | @@ -151,7 +152,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) | ... | @@ -151,7 +152,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) |
151 | 152 | ||
152 | /* sanity check */ | 153 | /* sanity check */ |
153 | if (pmbox == NULL || name == NULL || *name == '\0') | 154 | if (pmbox == NULL || name == NULL || *name == '\0') |
154 | return EINVAL; | 155 | return MU_ERROR_INVALID_ARG; |
155 | 156 | ||
156 | name_len = strlen (name); | 157 | name_len = strlen (name); |
157 | 158 | ||
... | @@ -173,14 +174,14 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) | ... | @@ -173,14 +174,14 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) |
173 | /* allocate memory for mbox */ | 174 | /* allocate memory for mbox */ |
174 | mbox = calloc (1, sizeof (*mbox)); | 175 | mbox = calloc (1, sizeof (*mbox)); |
175 | if (mbox == NULL) | 176 | if (mbox == NULL) |
176 | return ENOMEM; | 177 | return MU_ERROR_OUT_OF_MEMORY; |
177 | 178 | ||
178 | /* allocate specific pop box data */ | 179 | /* allocate specific pop box data */ |
179 | mpd = mbox->data = calloc (1, sizeof (*mpd)); | 180 | mpd = mbox->data = calloc (1, sizeof (*mpd)); |
180 | if (mbox->data == NULL) | 181 | if (mbox->data == NULL) |
181 | { | 182 | { |
182 | mailbox_pop_destroy (&mbox); | 183 | mailbox_pop_destroy (&mbox); |
183 | return ENOMEM; | 184 | return MU_ERROR_OUT_OF_MEMORY; |
184 | } | 185 | } |
185 | 186 | ||
186 | /* allocate the struct for buffered I/O */ | 187 | /* allocate the struct for buffered I/O */ |
... | @@ -196,7 +197,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) | ... | @@ -196,7 +197,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name) |
196 | if (mbox->name == NULL) | 197 | if (mbox->name == NULL) |
197 | { | 198 | { |
198 | mailbox_pop_destroy (&mbox); | 199 | mailbox_pop_destroy (&mbox); |
199 | return ENOMEM; | 200 | return MU_ERROR_OUT_OF_MEMORY; |
200 | } | 201 | } |
201 | memcpy (mbox->name, name, name_len); | 202 | memcpy (mbox->name, name, name_len); |
202 | 203 | ||
... | @@ -412,6 +413,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -412,6 +413,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
412 | //mpd->len = sprintf (pop->buffer, POP_BUFSIZ, "USER %s\r\n", user); | 413 | //mpd->len = sprintf (pop->buffer, POP_BUFSIZ, "USER %s\r\n", user); |
413 | bio->len = sprintf (bio->buffer, "USER %s\r\n", mpd->user); | 414 | bio->len = sprintf (bio->buffer, "USER %s\r\n", mpd->user); |
414 | bio->ptr = bio->buffer; | 415 | bio->ptr = bio->buffer; |
416 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
415 | free (mpd->user); mpd->user = NULL; | 417 | free (mpd->user); mpd->user = NULL; |
416 | mpd->state = 2; | 418 | mpd->state = 2; |
417 | /* send username */ | 419 | /* send username */ |
... | @@ -439,6 +441,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -439,6 +441,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
439 | } | 441 | } |
440 | return status; | 442 | return status; |
441 | } | 443 | } |
444 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
442 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | 445 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) |
443 | return EACCES; | 446 | return EACCES; |
444 | 447 | ||
... | @@ -446,6 +449,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -446,6 +449,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
446 | //mpd->len = snprintf (mpd->buffer, POP_BUFSIZ, "PASS %s\r\n", passwd); | 449 | //mpd->len = snprintf (mpd->buffer, POP_BUFSIZ, "PASS %s\r\n", passwd); |
447 | bio->len = sprintf (bio->buffer, "PASS %s\r\n", mpd->passwd); | 450 | bio->len = sprintf (bio->buffer, "PASS %s\r\n", mpd->passwd); |
448 | bio->ptr = bio->buffer; | 451 | bio->ptr = bio->buffer; |
452 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
449 | free (mpd->passwd); mpd->passwd = NULL; | 453 | free (mpd->passwd); mpd->passwd = NULL; |
450 | mpd->state = 4; | 454 | mpd->state = 4; |
451 | /* send Passwd */ | 455 | /* send Passwd */ |
... | @@ -473,6 +477,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) | ... | @@ -473,6 +477,7 @@ mailbox_pop_open (mailbox_t mbox, int flags) |
473 | } | 477 | } |
474 | return status; | 478 | return status; |
475 | } | 479 | } |
480 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
476 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | 481 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) |
477 | return EACCES; | 482 | return EACCES; |
478 | }/* swith state */ | 483 | }/* swith state */ |
... | @@ -513,6 +518,7 @@ mailbox_pop_close (mailbox_t mbox) | ... | @@ -513,6 +518,7 @@ mailbox_pop_close (mailbox_t mbox) |
513 | bio->len = sprintf (bio->buffer, "QUIT\r\n"); | 518 | bio->len = sprintf (bio->buffer, "QUIT\r\n"); |
514 | bio->ptr = bio->buffer; | 519 | bio->ptr = bio->buffer; |
515 | mpd->state = 1; | 520 | mpd->state = 1; |
521 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
516 | case 1: | 522 | case 1: |
517 | status = bio_write (mpd->bio); | 523 | status = bio_write (mpd->bio); |
518 | if (status != 0) | 524 | if (status != 0) |
... | @@ -524,6 +530,21 @@ mailbox_pop_close (mailbox_t mbox) | ... | @@ -524,6 +530,21 @@ mailbox_pop_close (mailbox_t mbox) |
524 | } | 530 | } |
525 | return status; | 531 | return status; |
526 | } | 532 | } |
533 | case 2: | ||
534 | status = bio_readline (bio); | ||
535 | if (status != 0) | ||
536 | { | ||
537 | if (status != EAGAIN && status != EINTR) | ||
538 | { | ||
539 | mpd->func = mpd->id = NULL; | ||
540 | mpd->state = 0; | ||
541 | } | ||
542 | return status; | ||
543 | } | ||
544 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
545 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | ||
546 | return EINVAL; | ||
547 | case 3: | ||
527 | close (mpd->fd); | 548 | close (mpd->fd); |
528 | mpd->fd = -1; | 549 | mpd->fd = -1; |
529 | } | 550 | } |
... | @@ -622,6 +643,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -622,6 +643,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
622 | /*bio->len = snprintf (bio->buffer, POP_BUFSIZ, "TOP %d 0\r\n", msgno);*/ | 643 | /*bio->len = snprintf (bio->buffer, POP_BUFSIZ, "TOP %d 0\r\n", msgno);*/ |
623 | bio->len = sprintf (bio->buffer, "TOP %d 0\r\n", msgno); | 644 | bio->len = sprintf (bio->buffer, "TOP %d 0\r\n", msgno); |
624 | bio->ptr = bio->buffer; | 645 | bio->ptr = bio->buffer; |
646 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
625 | mpd->state = 1; | 647 | mpd->state = 1; |
626 | } | 648 | } |
627 | /* send the TOP */ | 649 | /* send the TOP */ |
... | @@ -660,6 +682,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -660,6 +682,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
660 | } | 682 | } |
661 | return status; | 683 | return status; |
662 | } | 684 | } |
685 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
663 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | 686 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) |
664 | { | 687 | { |
665 | mpd->func = mpd->id = NULL; | 688 | mpd->func = mpd->id = NULL; |
... | @@ -850,6 +873,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount) | ... | @@ -850,6 +873,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount) |
850 | case 0: | 873 | case 0: |
851 | bio->len = sprintf (bio->buffer, "STAT\r\n"); | 874 | bio->len = sprintf (bio->buffer, "STAT\r\n"); |
852 | bio->ptr = bio->buffer; | 875 | bio->ptr = bio->buffer; |
876 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
853 | mpd->state = 1; | 877 | mpd->state = 1; |
854 | /* Send the STAT */ | 878 | /* Send the STAT */ |
855 | case 1: | 879 | case 1: |
... | @@ -862,6 +886,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount) | ... | @@ -862,6 +886,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount) |
862 | status = bio_readline (bio); | 886 | status = bio_readline (bio); |
863 | if (status != 0) | 887 | if (status != 0) |
864 | return status; | 888 | return status; |
889 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
865 | break; | 890 | break; |
866 | default: | 891 | default: |
867 | fprintf (stderr, "unknow state(messages_count)\n"); | 892 | fprintf (stderr, "unknow state(messages_count)\n"); |
... | @@ -963,6 +988,7 @@ mailbox_pop_expunge (mailbox_t mbox) | ... | @@ -963,6 +988,7 @@ mailbox_pop_expunge (mailbox_t mbox) |
963 | bio->len = sprintf (bio->buffer, "DELE %d\r\n", | 988 | bio->len = sprintf (bio->buffer, "DELE %d\r\n", |
964 | mpd->pmessages[i]->num); | 989 | mpd->pmessages[i]->num); |
965 | bio->ptr = bio->buffer; | 990 | bio->ptr = bio->buffer; |
991 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
966 | mpd->state = 1; | 992 | mpd->state = 1; |
967 | case 1: | 993 | case 1: |
968 | status = bio_write (bio); | 994 | status = bio_write (bio); |
... | @@ -989,6 +1015,7 @@ mailbox_pop_expunge (mailbox_t mbox) | ... | @@ -989,6 +1015,7 @@ mailbox_pop_expunge (mailbox_t mbox) |
989 | } | 1015 | } |
990 | return status; | 1016 | return status; |
991 | } | 1017 | } |
1018 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer); | ||
992 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) | 1019 | if (strncasecmp (bio->buffer, "+OK", 3) != 0) |
993 | { | 1020 | { |
994 | mpd->func = mpd->id = NULL; | 1021 | mpd->func = mpd->id = NULL; | ... | ... |
... | @@ -37,7 +37,7 @@ | ... | @@ -37,7 +37,7 @@ |
37 | #include <fcntl.h> | 37 | #include <fcntl.h> |
38 | #include <unistd.h> | 38 | #include <unistd.h> |
39 | #include <signal.h> | 39 | #include <signal.h> |
40 | #include <errno.h> | 40 | #include <mailutils_errno.h> |
41 | #include <time.h> | 41 | #include <time.h> |
42 | #ifdef HAVE_PTHREAD_H | 42 | #ifdef HAVE_PTHREAD_H |
43 | # include <pthread.h> | 43 | # include <pthread.h> |
... | @@ -164,7 +164,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -164,7 +164,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
164 | 164 | ||
165 | /* sanity check */ | 165 | /* sanity check */ |
166 | if (name == NULL || *name == '\0') | 166 | if (name == NULL || *name == '\0') |
167 | return EINVAL; | 167 | return MU_ERROR_INVALID_ARG; |
168 | 168 | ||
169 | name_len = strlen (name); | 169 | name_len = strlen (name); |
170 | 170 | ||
... | @@ -187,14 +187,14 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -187,14 +187,14 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
187 | /* allocate memory for mbox */ | 187 | /* allocate memory for mbox */ |
188 | mbox = calloc (1, sizeof (*mbox)); | 188 | mbox = calloc (1, sizeof (*mbox)); |
189 | if (mbox == NULL) | 189 | if (mbox == NULL) |
190 | return ENOMEM; | 190 | return MU_ERROR_OUT_OF_MEMORY; |
191 | 191 | ||
192 | /* allocate specific unix mbox data */ | 192 | /* allocate specific unix mbox data */ |
193 | mud = mbox->data = calloc (1, sizeof (*mud)); | 193 | mud = mbox->data = calloc (1, sizeof (*mud)); |
194 | if (mbox->data == NULL) | 194 | if (mbox->data == NULL) |
195 | { | 195 | { |
196 | mailbox_unix_destroy (&mbox); | 196 | mailbox_unix_destroy (&mbox); |
197 | return ENOMEM; | 197 | return MU_ERROR_OUT_OF_MEMORY; |
198 | } | 198 | } |
199 | 199 | ||
200 | /* copy the name */ | 200 | /* copy the name */ |
... | @@ -202,7 +202,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -202,7 +202,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
202 | if (mbox->name == NULL) | 202 | if (mbox->name == NULL) |
203 | { | 203 | { |
204 | mailbox_unix_destroy (&mbox); | 204 | mailbox_unix_destroy (&mbox); |
205 | return ENOMEM; | 205 | return MU_ERROR_OUT_OF_MEMORY; |
206 | } | 206 | } |
207 | memcpy (mbox->name, name, name_len); | 207 | memcpy (mbox->name, name, name_len); |
208 | 208 | ||
... | @@ -228,7 +228,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -228,7 +228,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
228 | if (mud->dirname == NULL) | 228 | if (mud->dirname == NULL) |
229 | { | 229 | { |
230 | mailbox_unix_destroy (&mbox); | 230 | mailbox_unix_destroy (&mbox); |
231 | return ENOMEM; | 231 | return MU_ERROR_OUT_OF_MEMORY; |
232 | } | 232 | } |
233 | memcpy (mud->dirname, name, sep - name); | 233 | memcpy (mud->dirname, name, sep - name); |
234 | 234 | ||
... | @@ -237,7 +237,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -237,7 +237,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
237 | if (mud->basename == NULL) | 237 | if (mud->basename == NULL) |
238 | { | 238 | { |
239 | mailbox_unix_destroy (&mbox); | 239 | mailbox_unix_destroy (&mbox); |
240 | return ENOMEM; | 240 | return MU_ERROR_OUT_OF_MEMORY; |
241 | } | 241 | } |
242 | memcpy (mud->basename, sep, name_len - (sep - name)); | 242 | memcpy (mud->basename, sep, name_len - (sep - name)); |
243 | } | 243 | } |
... | @@ -249,7 +249,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -249,7 +249,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
249 | if (mud->dirname == NULL) | 249 | if (mud->dirname == NULL) |
250 | { | 250 | { |
251 | mailbox_unix_destroy (&mbox); | 251 | mailbox_unix_destroy (&mbox); |
252 | return ENOMEM; | 252 | return MU_ERROR_OUT_OF_MEMORY; |
253 | } | 253 | } |
254 | mud->dirname[0] = '.'; | 254 | mud->dirname[0] = '.'; |
255 | 255 | ||
... | @@ -257,7 +257,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -257,7 +257,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
257 | if (mud->basename == NULL) | 257 | if (mud->basename == NULL) |
258 | { | 258 | { |
259 | mailbox_unix_destroy (&mbox); | 259 | mailbox_unix_destroy (&mbox); |
260 | return ENOMEM; | 260 | return MU_ERROR_OUT_OF_MEMORY; |
261 | } | 261 | } |
262 | memcpy (mud->basename, name, name_len); | 262 | memcpy (mud->basename, name, name_len); |
263 | } | 263 | } |
... | @@ -286,6 +286,8 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) | ... | @@ -286,6 +286,8 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name) |
286 | 286 | ||
287 | mbox->_size = mailbox_unix_size; | 287 | mbox->_size = mailbox_unix_size; |
288 | 288 | ||
289 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_create (%s/%s)\n", | ||
290 | mud->dirname, mud->basename); | ||
289 | (*pmbox) = mbox; | 291 | (*pmbox) = mbox; |
290 | 292 | ||
291 | return 0; /* okdoke */ | 293 | return 0; /* okdoke */ |
... | @@ -302,6 +304,9 @@ mailbox_unix_destroy (mailbox_t *pmbox) | ... | @@ -302,6 +304,9 @@ mailbox_unix_destroy (mailbox_t *pmbox) |
302 | { | 304 | { |
303 | size_t i; | 305 | size_t i; |
304 | mailbox_unix_data_t mud = mbox->data; | 306 | mailbox_unix_data_t mud = mbox->data; |
307 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, | ||
308 | "mailbox_unix_destroy (%s/%s)\n", | ||
309 | mud->dirname, mud->basename); | ||
305 | free (mud->dirname); | 310 | free (mud->dirname); |
306 | free (mud->basename); | 311 | free (mud->basename); |
307 | for (i = 0; i < mud->umessages_count; i++) | 312 | for (i = 0; i < mud->umessages_count; i++) |
... | @@ -403,6 +408,8 @@ mailbox_unix_open (mailbox_t mbox, int flags) | ... | @@ -403,6 +408,8 @@ mailbox_unix_open (mailbox_t mbox, int flags) |
403 | if (mbox->auth) | 408 | if (mbox->auth) |
404 | auth_epilogue (mbox->auth); | 409 | auth_epilogue (mbox->auth); |
405 | 410 | ||
411 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_open(%s, %d)\n", | ||
412 | mbox->name, flags); | ||
406 | /* give an appopriate way to lock */ | 413 | /* give an appopriate way to lock */ |
407 | if (mbox->locker == NULL) | 414 | if (mbox->locker == NULL) |
408 | locker_create (&(mbox->locker), mbox->name, | 415 | locker_create (&(mbox->locker), mbox->name, |
... | @@ -414,15 +421,38 @@ static int | ... | @@ -414,15 +421,38 @@ static int |
414 | mailbox_unix_close (mailbox_t mbox) | 421 | mailbox_unix_close (mailbox_t mbox) |
415 | { | 422 | { |
416 | mailbox_unix_data_t mud; | 423 | mailbox_unix_data_t mud; |
424 | size_t i; | ||
417 | 425 | ||
418 | if (mbox == NULL || | 426 | if (mbox == NULL || |
419 | (mud = (mailbox_unix_data_t)mbox->data) == NULL) | 427 | (mud = (mailbox_unix_data_t)mbox->data) == NULL) |
420 | return EINVAL; | 428 | return EINVAL; |
421 | 429 | ||
422 | stream_close (mbox->stream); | ||
423 | /* make sure we do not hold any lock for that file */ | 430 | /* make sure we do not hold any lock for that file */ |
424 | mailbox_unix_unlock (mbox); | 431 | mailbox_unix_unlock (mbox); |
425 | return 0; | 432 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_close(%s)\n", |
433 | mbox->name); | ||
434 | |||
435 | /* before closing we need to remove all the messages | ||
436 | * - to reclaim the memory | ||
437 | * - to prepare for another scan. | ||
438 | */ | ||
439 | for (i = 0; i < mud->umessages_count; i++) | ||
440 | { | ||
441 | mailbox_unix_message_t mum = mud->umessages[i]; | ||
442 | if (mum == NULL) | ||
443 | continue; | ||
444 | /* Destroy the attach messages */ | ||
445 | attribute_destroy (&(mum->old_attr)); | ||
446 | message_destroy (&(mum->message), mum); | ||
447 | /* new_attr free by message_destroy() */ | ||
448 | /* attribute_destroy (&(mum->new_attr)); */ | ||
449 | free (mum); | ||
450 | } | ||
451 | free (mud->umessages); | ||
452 | mud->umessages = NULL; | ||
453 | mud->messages_count = mud->umessages_count = 0; | ||
454 | mud->size = 0; | ||
455 | return stream_close (mbox->stream); | ||
426 | } | 456 | } |
427 | 457 | ||
428 | /* Mailbox Parsing */ | 458 | /* Mailbox Parsing */ |
... | @@ -431,6 +461,8 @@ mailbox_unix_close (mailbox_t mbox) | ... | @@ -431,6 +461,8 @@ mailbox_unix_close (mailbox_t mbox) |
431 | static int | 461 | static int |
432 | mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount) | 462 | mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount) |
433 | { | 463 | { |
464 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_scan(%s)\n", | ||
465 | mbox->name); | ||
434 | return mailbox_unix_scan0 (mbox, msgno, pcount, 1); | 466 | return mailbox_unix_scan0 (mbox, msgno, pcount, 1); |
435 | } | 467 | } |
436 | 468 | ||
... | @@ -568,6 +600,9 @@ mailbox_unix_expunge (mailbox_t mbox) | ... | @@ -568,6 +600,9 @@ mailbox_unix_expunge (mailbox_t mbox) |
568 | if (dirty == mud->messages_count) | 600 | if (dirty == mud->messages_count) |
569 | return 0; /* nothing change, bail out */ | 601 | return 0; /* nothing change, bail out */ |
570 | 602 | ||
603 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_expunge (%s)\n", | ||
604 | mbox->name); | ||
605 | |||
571 | /* Send notification to all the listeners | 606 | /* Send notification to all the listeners |
572 | * this is redundant, we go to the loop again | 607 | * this is redundant, we go to the loop again |
573 | * But it's more secure here since we don't | 608 | * But it's more secure here since we don't |
... | @@ -828,6 +863,9 @@ mailbox_unix_expunge (mailbox_t mbox) | ... | @@ -828,6 +863,9 @@ mailbox_unix_expunge (mailbox_t mbox) |
828 | mum->body = mum->body_end = 0; | 863 | mum->body = mum->body_end = 0; |
829 | mum->header_lines = mum->body_lines = 0; | 864 | mum->header_lines = mum->body_lines = 0; |
830 | } | 865 | } |
866 | /* this is should reset the messages_count, the last | ||
867 | * argument 0 means not to send event notification | ||
868 | */ | ||
831 | mailbox_unix_scan0 (mbox, dirty, NULL, 0); | 869 | mailbox_unix_scan0 (mbox, dirty, NULL, 0); |
832 | } | 870 | } |
833 | return status; | 871 | return status; |
... | @@ -1048,6 +1086,8 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) | ... | @@ -1048,6 +1086,8 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg) |
1048 | return 0; | 1086 | return 0; |
1049 | } | 1087 | } |
1050 | 1088 | ||
1089 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, | ||
1090 | "mailbox_unix_get_message(%s, %d)\n", mbox->name, msgno); | ||
1051 | /* get the headers */ | 1091 | /* get the headers */ |
1052 | do | 1092 | do |
1053 | { | 1093 | { |
... | @@ -1145,6 +1185,9 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) | ... | @@ -1145,6 +1185,9 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg) |
1145 | (mud = (mailbox_unix_data_t)mbox->data) == NULL) | 1185 | (mud = (mailbox_unix_data_t)mbox->data) == NULL) |
1146 | return EINVAL; | 1186 | return EINVAL; |
1147 | 1187 | ||
1188 | mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, | ||
1189 | "mailbox_unix_append_message (%s)\n", mbox->name); | ||
1190 | |||
1148 | mailbox_unix_lock (mbox, MU_LOCKER_WRLOCK); | 1191 | mailbox_unix_lock (mbox, MU_LOCKER_WRLOCK); |
1149 | { | 1192 | { |
1150 | off_t size; | 1193 | off_t size; | ... | ... |
... | @@ -210,7 +210,7 @@ message_size (message_t msg, size_t *psize) | ... | @@ -210,7 +210,7 @@ message_size (message_t msg, size_t *psize) |
210 | 210 | ||
211 | int | 211 | int |
212 | message_set_from (message_t msg, | 212 | message_set_from (message_t msg, |
213 | int (*_from)(message_t, char *, size_t, size_t*), | 213 | int (*_from) __P ((message_t, char *, size_t, size_t*)), |
214 | void *owner) | 214 | void *owner) |
215 | { | 215 | { |
216 | if (msg == NULL) | 216 | if (msg == NULL) |
... | @@ -273,8 +273,8 @@ message_from (message_t msg, char *buf, size_t len, size_t *pnwrite) | ... | @@ -273,8 +273,8 @@ message_from (message_t msg, char *buf, size_t len, size_t *pnwrite) |
273 | } | 273 | } |
274 | 274 | ||
275 | int | 275 | int |
276 | message_set_received (message_t msg, | 276 | message_set_received (message_t msg, int (*_received) |
277 | int (*_received) (message_t, char *, size_t , size_t *), | 277 | __P ((message_t, char *, size_t , size_t *)), |
278 | void *owner) | 278 | void *owner) |
279 | { | 279 | { |
280 | if (msg == NULL) | 280 | if (msg == NULL) |
... | @@ -541,7 +541,9 @@ message_write (stream_t os, const char *buf, size_t buflen, | ... | @@ -541,7 +541,9 @@ message_write (stream_t os, const char *buf, size_t buflen, |
541 | msg->hdr_buflen = msg->hdr_done = 0; | 541 | msg->hdr_buflen = msg->hdr_done = 0; |
542 | return status; | 542 | return status; |
543 | } | 543 | } |
544 | if (off > (off_t)msg->hdr_buflen) | 544 | if (off < (off_t)msg->hdr_buflen) |
545 | off = 0; | ||
546 | else | ||
545 | off -= msg->hdr_buflen; | 547 | off -= msg->hdr_buflen; |
546 | return stream_write (bs, buf, buflen, off, pnwrite); | 548 | return stream_write (bs, buf, buflen, off, pnwrite); |
547 | } | 549 | } |
... | @@ -573,6 +575,32 @@ message_get_fd (stream_t stream, int *pfd) | ... | @@ -573,6 +575,32 @@ message_get_fd (stream_t stream, int *pfd) |
573 | return stream_get_fd (is, pfd); | 575 | return stream_get_fd (is, pfd); |
574 | } | 576 | } |
575 | 577 | ||
578 | int | ||
579 | message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwritten) | ||
580 | { | ||
581 | header_t header = NULL; | ||
582 | if (msg == NULL || buffer == NULL || buflen == 0) | ||
583 | return EINVAL; | ||
584 | |||
585 | buffer[0] = '0'; | ||
586 | if (msg->_get_uidl) | ||
587 | return msg->_get_uidl (msg, buffer, buflen, pwritten); | ||
588 | |||
589 | message_get_header (msg, &header); | ||
590 | return header_get_value (header, "X-UIDL", buffer, buflen, pwritten); | ||
591 | } | ||
592 | |||
593 | int | ||
594 | message_set_uidl (message_t msg, int (* _get_uidl) | ||
595 | __P ((message_t msg, char *buffer, size_t buflen, size_t *pwritten)), void *owner) | ||
596 | { | ||
597 | if (msg == NULL) | ||
598 | return EINVAL; | ||
599 | if (msg->owner != owner) | ||
600 | return EACCES; | ||
601 | msg->_get_uidl = _get_uidl; | ||
602 | return 0; | ||
603 | } | ||
576 | static int | 604 | static int |
577 | extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite) | 605 | extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite) |
578 | { | 606 | { | ... | ... |
-
Please register or sign in to post a comment