Commit f79d9cd4 f79d9cd488640506744f0b2b5128aaaaafc41141 by Alain Magloire

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
1 parent 13350f03
......@@ -308,6 +308,7 @@ lazy_create ()
(void)remove(tmpbuf);
#else
file = tmpfile ();
//file = fopen ("/tmp/mystuff", "w+");
/* make sure the mode is right */
if (file)
fchmod (fileno (file), 0600);
......
......@@ -51,6 +51,7 @@ struct _header
/* streams */
stream_t stream;
int (*_get_value) __P ((header_t, const char *, char *, size_t , size_t *));
/* owner ? */
void *owner;
......@@ -202,7 +203,7 @@ header_parse (header_t header, char *blurb, int len)
return 0;
}
/* FIXME: grossly inneficient, to many copies and reallocating
/* FIXME: grossly inneficient, to many copies and reallocating.
* This all header business need a good rewrite.
*/
int
......@@ -264,6 +265,20 @@ header_set_value (header_t header, const char *fn, const char *fv, int replace)
}
int
header_set_get_value (header_t header, int (*_get_value)
(header_t, const char *, char *, size_t, size_t *),
void *owner)
{
if (header == NULL)
return EINVAL;
if (header->owner != owner)
return EACCES;
header->_get_value = _get_value;
return 0;
}
int
header_get_value (header_t header, const char *name, char *buffer,
size_t buflen, size_t *pn)
{
......@@ -319,7 +334,16 @@ header_get_value (header_t header, const char *name, char *buffer,
if (pn)
*pn = total;
if (total == 0)
return ENOENT;
{
int err = ENOENT;
/* check if they provided a hook */
if (header->_get_value != NULL)
err = header->_get_value (header, name, buffer, buflen, pn);
/* cache it locally */
if (err == 0)
header_set_value (header, name, buffer, 0);
return err;
}
return 0;
}
......
......@@ -36,6 +36,7 @@ struct _stream
{
void *owner;
int flags;
int state;
void (*_destroy) __P ((stream_t));
int (*_open) __P ((stream_t, const char *, int port, int flags));
int (*_close) __P ((stream_t));
......
......@@ -52,7 +52,9 @@ struct _mailbox
/* debug information */
int debug_level;
void *debug_arg;
int (*debug_print) __P ((const char *, void *arg));
char *debug_buffer;
size_t debug_bufsize;
int (*debug_print) __P ((void *arg, const char *, size_t));
/* Back pointer to the specific mailbox */
void *data;
......@@ -85,6 +87,7 @@ extern int mailbox_num_deleted __P ((mailbox_t, size_t *));
extern int mailbox_notification __P ((mailbox_t mbox, size_t type));
extern int mailbox_debug __P ((mailbox_t, int level, const char *fmt, ...));
#ifdef __cplusplus
}
......
......@@ -60,6 +60,7 @@ struct _message
int (*_from) __P ((message_t msg, char *, size_t, size_t *));
int (*_received) __P ((message_t msg, char *, size_t, size_t *));
int (*_get_uidl) __P ((message_t msg, char *, size_t, size_t *));
};
......
......@@ -73,6 +73,11 @@ extern void header_destroy __P ((header_t *, void *owner));
extern int header_set_value __P ((header_t, const char *fn,
const char *fv, int replace));
extern int header_set_get_value __P ((header_t, int (*_get_value)
__P ((header_t, const char *fn, char *buf,
size_t buflen, size_t *nwritten)),
void *owner));
extern int header_get_value __P ((header_t, const char *fn, char *buf,
size_t buflen, size_t *nwritten));
extern int header_entry_count __P ((header_t, size_t *num));
......
......@@ -35,13 +35,13 @@ extern "C" { /*}*/
struct _stream;
typedef struct _stream *stream_t;
/* stream will be destroy on stream_destroy */
#define MU_STREAM_READ 0x00000001
#define MU_STREAM_WRITE 0x00000002
#define MU_STREAM_RDWR 0x00000004
#define MU_STREAM_APPEND 0x00000008
#define MU_STREAM_CREAT 0x00000010
#define MU_STREAM_NONBLOCK 0x00000020
/* stream will be destroy on stream_destroy */
#define MU_STREAM_NO_CHECK 0x00000040
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)
extern int stream_get_flags __P ((stream_t, int *pflags));
extern int stream_set_flags __P ((stream_t, int flags, void *owner));
#define MU_STREAM_STATE_OPEN 1
#define MU_STREAM_STATE_READ 2
#define MU_STREAM_STATE_WRITE 4
#define MU_STREAM_STATE_CLOSE 8
extern int stream_get_state __P ((stream_t, int *pstate));
/* misc */
extern int file_stream_create __P ((stream_t *stream));
extern int mapfile_stream_create __P ((stream_t *stream));
......
......@@ -99,10 +99,13 @@ extern int mailbox_register __P ((mailbox_t mbox, size_t type,
extern int mailbox_deregister __P ((mailbox_t mbox, void *action));
/* trace */
#define MU_MAILBOX_DEBUG_TRACE 1
#define MU_MAILBOX_DEBUG_PROT 2
extern int mailbox_set_debug_level __P ((mailbox_t mbox, size_t level));
extern int mailbox_get_debug_level __P ((mailbox_t mbox, size_t *plevel));
extern int mailbox_set_debug_print __P ((mailbox_t mbox, int (*debug_print)
__P ((const char *, void *arg)),
__P ((void *arg, const char *,
size_t)),
void *arg));
......
......@@ -83,6 +83,11 @@ extern int message_set_get_part __P ((message_t, size_t part, message_t *msg));
extern int message_add_part __P ((message_t, message_t msg));
extern int message_set_add_part __P ((message_t, message_t msg));
extern int message_get_uidl __P ((message_t, char *buffer, size_t, size_t *));
extern int message_set_uidl __P ((message_t, int (*_get_uidl)
__P ((message_t, char *, size_t, size_t *)),
void *owner));
/* events */
#define MU_EVT_MSG_DESTROY 32
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)
{
if (stream == NULL)
return EINVAL;
stream->state = MU_STREAM_STATE_OPEN;
if (stream->_open)
return stream->_open (stream, name, port, flags);
return 0;
......@@ -94,6 +95,7 @@ stream_close (stream_t stream)
{
if (stream == NULL)
return EINVAL;
stream->state = MU_STREAM_STATE_CLOSE;
if (stream->_close)
return stream->_close (stream);
return 0;
......@@ -176,6 +178,7 @@ stream_read (stream_t is, char *buf, size_t count,
{
if (is == NULL || is->_read == NULL)
return EINVAL;
is->state = MU_STREAM_STATE_READ;
return is->_read (is, buf, count, offset, pnread);
}
......@@ -188,6 +191,9 @@ stream_readline (stream_t is, char *buf, size_t count,
int status;
if (is == NULL)
return EINVAL;
is->state = MU_STREAM_STATE_READ;
if (is->_readline != NULL)
return is->_readline (is, buf, count, offset, pnread);
......@@ -225,6 +231,7 @@ stream_write (stream_t os, const char *buf, size_t count,
{
if (os == NULL || os->_write == NULL)
return EINVAL;
os->state = MU_STREAM_STATE_WRITE;
return os->_write (os, buf, count, offset, pnwrite);
}
......@@ -315,3 +322,12 @@ stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner)
stream->_flush = _flush;
return 0;
}
int
stream_get_state (stream_t stream, int *pstate)
{
if (stream == NULL || pstate == NULL)
return EINVAL;
*pstate = stream->state;
return 0;
}
......
......@@ -23,9 +23,12 @@
#include <message0.h>
#include <registrar.h>
#include <locker.h>
#include <mailutils_errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
/*
......@@ -37,7 +40,7 @@
int
mailbox_create (mailbox_t *pmbox, const char *name, int id)
{
int status = EINVAL;
int status = MU_ERROR_INVALID_ARG;
struct mailbox_registrar *mreg;
url_t url = NULL;
......@@ -80,7 +83,7 @@ int
mailbox_open (mailbox_t mbox, int flag)
{
if (mbox == NULL || mbox->_open == NULL)
return ENOSYS;
return MU_ERROR_NOT_IMPLEMENTED;
return mbox->_open (mbox, flag);
}
......@@ -88,7 +91,7 @@ int
mailbox_close (mailbox_t mbox)
{
if (mbox == NULL || mbox->_close == NULL)
return ENOSYS;
return MU_ERROR_NOT_IMPLEMENTED;
return mbox->_close (mbox);
}
......@@ -97,7 +100,7 @@ int
mailbox_append_message (mailbox_t mbox, message_t msg)
{
if (mbox == NULL || mbox->_append_message == NULL)
return ENOSYS;
return MU_ERROR_NOT_IMPLEMENTED;
return mbox->_append_message (mbox, msg);
}
......@@ -105,7 +108,7 @@ int
mailbox_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
{
if (mbox == NULL || mbox->_get_message == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
return mbox->_get_message (mbox, msgno, pmsg);
}
......@@ -113,7 +116,7 @@ int
mailbox_messages_count (mailbox_t mbox, size_t *num)
{
if (mbox == NULL || mbox->_messages_count == NULL)
return ENOSYS;
return MU_ERROR_NOT_IMPLEMENTED;
return mbox->_messages_count (mbox, num);
}
......@@ -121,7 +124,7 @@ int
mailbox_expunge (mailbox_t mbox)
{
if (mbox == NULL || mbox->_expunge == NULL)
return ENOSYS;
return MU_ERROR_NOT_IMPLEMENTED;
return mbox->_expunge (mbox);
}
......@@ -129,7 +132,7 @@ int
mailbox_num_deleted (mailbox_t mbox, size_t *num)
{
if (mbox == NULL || mbox->_num_deleted == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
return mbox->_num_deleted (mbox, num);
}
......@@ -154,7 +157,7 @@ int
mailbox_set_locker (mailbox_t mbox, locker_t locker)
{
if (mbox == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
if (mbox->locker != NULL)
locker_destroy (&mbox->locker);
mbox->locker = locker;
......@@ -165,7 +168,7 @@ int
mailbox_get_locker (mailbox_t mbox, locker_t *plocker)
{
if (mbox == NULL || plocker == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
if (plocker)
*plocker = mbox->locker;
return 0;
......@@ -175,7 +178,7 @@ int
mailbox_set_auth (mailbox_t mbox, auth_t auth)
{
if (mbox == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
mbox->auth = auth;
return 0;
}
......@@ -184,7 +187,7 @@ int
mailbox_get_auth (mailbox_t mbox, auth_t *pauth)
{
if (mbox == NULL || pauth == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
if (pauth)
*pauth = mbox->auth;
return 0;
......@@ -194,7 +197,7 @@ int
mailbox_set_stream (mailbox_t mbox, stream_t stream)
{
if (mbox == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
mbox->stream = stream;
return 0;
}
......@@ -203,7 +206,7 @@ int
mailbox_get_stream (mailbox_t mbox, stream_t *pstream)
{
if (mbox == NULL || pstream == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
if (pstream)
*pstream = mbox->stream;
return 0;
......@@ -219,7 +222,7 @@ mailbox_register (mailbox_t mbox, size_t type,
/* FIXME: I should check for invalid types */
if (mbox == NULL || action == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
/* find a free spot */
for (i = 0; i < mbox->event_num; i++)
......@@ -237,7 +240,7 @@ mailbox_register (mailbox_t mbox, size_t type,
/* a new one */
event = realloc (mbox->event, (mbox->event_num + 1) * sizeof (*event));
if (event == NULL)
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
mbox->event = event;
event[mbox->event_num]._action = action;
......@@ -264,7 +267,7 @@ mailbox_deregister (mailbox_t mbox, void *action)
return 0;
}
}
return ENOENT;
return MU_ERROR_NO_ENTRY;
}
int
......@@ -286,7 +289,7 @@ int
mailbox_set_debug_level (mailbox_t mbox, size_t level)
{
if (mbox == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
mbox->debug_level = level;
return 0;
}
......@@ -295,18 +298,47 @@ int
mailbox_get_debug_level (mailbox_t mbox, size_t *plevel)
{
if (mbox == NULL || plevel == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
*plevel = mbox->debug_level;
return 0;
}
int
mailbox_set_debug_print (mailbox_t mbox, int (*debug_print)
(const char *, void *arg), void *arg)
(void *arg, const char *, size_t), void *arg)
{
if (mbox == NULL)
return EINVAL;
return MU_ERROR_INVALID_ARG;
mbox->debug_print = debug_print;
mbox->debug_arg = arg;
return 0;
}
int
mailbox_debug (mailbox_t mbox, int level, const char *fmt, ...)
{
va_list ap;
if (mbox == NULL)
return MU_ERROR_INVALID_ARG;
if (!(mbox->debug_level & level))
return 0;
va_start (ap, fmt);
if (mbox->debug_print)
{
int writen;
if (mbox->debug_buffer == NULL)
{
mbox->debug_bufsize = 255;
mbox->debug_buffer = malloc (mbox->debug_bufsize);
if (mbox->debug_buffer)
return MU_ERROR_OUT_OF_MEMORY; }
writen = vsnprintf (mbox->debug_buffer, mbox->debug_bufsize, fmt, ap);
mbox->debug_print (mbox->debug_arg, mbox->debug_buffer, writen);
}
else
vfprintf (stderr, fmt, ap);
va_end (ap);
return 0;
}
......
......@@ -60,6 +60,8 @@ _mapfile_read (stream_t stream, char *optr, size_t osize,
struct _mapfile_stream *mfs = stream->owner;
size_t n;
if (mfs->ptr == NULL)
return EINVAL;
if (offset >= (off_t)mfs->size)
{
if (nbytes)
......@@ -83,6 +85,8 @@ _mapfile_readline (stream_t stream, char *optr, size_t osize,
char *nl;
size_t n = 0;
if (mfs->ptr == NULL)
return EINVAL;
/* save space for the null byte */
osize--;
if (offset >= (off_t)mfs->size)
......@@ -108,6 +112,8 @@ _mapfile_write (stream_t stream, const char *iptr, size_t isize,
{
struct _mapfile_stream *mfs = stream->owner;
if (mfs->ptr == NULL)
return EINVAL;
if (! (mfs->flags & PROT_WRITE))
return EACCES;
......@@ -144,6 +150,8 @@ static int
_mapfile_truncate (stream_t stream, off_t len)
{
struct _mapfile_stream *mfs = stream->owner;
if (mfs->ptr == NULL)
return EINVAL;
/* remap */
if (munmap (mfs->ptr, mfs->size) != 0)
{
......@@ -170,6 +178,8 @@ _mapfile_size (stream_t stream, off_t *psize)
{
struct _mapfile_stream *mfs = stream->owner;
struct stat stbuf;
if (mfs->ptr == NULL)
return EINVAL;
msync (mfs->ptr, mfs->size, MS_SYNC);
if (fstat(mfs->fd, &stbuf) != 0)
return errno;
......
......@@ -22,6 +22,7 @@
#include <registrar0.h>
#include <auth0.h>
#include <attribute.h>
#include <mailutils_errno.h>
#include <termios.h>
#include <errno.h>
......@@ -151,7 +152,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name)
/* sanity check */
if (pmbox == NULL || name == NULL || *name == '\0')
return EINVAL;
return MU_ERROR_INVALID_ARG;
name_len = strlen (name);
......@@ -173,14 +174,14 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name)
/* allocate memory for mbox */
mbox = calloc (1, sizeof (*mbox));
if (mbox == NULL)
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
/* allocate specific pop box data */
mpd = mbox->data = calloc (1, sizeof (*mpd));
if (mbox->data == NULL)
{
mailbox_pop_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
/* allocate the struct for buffered I/O */
......@@ -196,7 +197,7 @@ mailbox_pop_create (mailbox_t *pmbox, const char *name)
if (mbox->name == NULL)
{
mailbox_pop_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
memcpy (mbox->name, name, name_len);
......@@ -412,6 +413,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
//mpd->len = sprintf (pop->buffer, POP_BUFSIZ, "USER %s\r\n", user);
bio->len = sprintf (bio->buffer, "USER %s\r\n", mpd->user);
bio->ptr = bio->buffer;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
free (mpd->user); mpd->user = NULL;
mpd->state = 2;
/* send username */
......@@ -439,6 +441,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
}
return status;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
if (strncasecmp (bio->buffer, "+OK", 3) != 0)
return EACCES;
......@@ -446,6 +449,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
//mpd->len = snprintf (mpd->buffer, POP_BUFSIZ, "PASS %s\r\n", passwd);
bio->len = sprintf (bio->buffer, "PASS %s\r\n", mpd->passwd);
bio->ptr = bio->buffer;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
free (mpd->passwd); mpd->passwd = NULL;
mpd->state = 4;
/* send Passwd */
......@@ -473,6 +477,7 @@ mailbox_pop_open (mailbox_t mbox, int flags)
}
return status;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
if (strncasecmp (bio->buffer, "+OK", 3) != 0)
return EACCES;
}/* swith state */
......@@ -513,6 +518,7 @@ mailbox_pop_close (mailbox_t mbox)
bio->len = sprintf (bio->buffer, "QUIT\r\n");
bio->ptr = bio->buffer;
mpd->state = 1;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
case 1:
status = bio_write (mpd->bio);
if (status != 0)
......@@ -524,6 +530,21 @@ mailbox_pop_close (mailbox_t mbox)
}
return status;
}
case 2:
status = bio_readline (bio);
if (status != 0)
{
if (status != EAGAIN && status != EINTR)
{
mpd->func = mpd->id = NULL;
mpd->state = 0;
}
return status;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
if (strncasecmp (bio->buffer, "+OK", 3) != 0)
return EINVAL;
case 3:
close (mpd->fd);
mpd->fd = -1;
}
......@@ -622,6 +643,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
/*bio->len = snprintf (bio->buffer, POP_BUFSIZ, "TOP %d 0\r\n", msgno);*/
bio->len = sprintf (bio->buffer, "TOP %d 0\r\n", msgno);
bio->ptr = bio->buffer;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
mpd->state = 1;
}
/* send the TOP */
......@@ -660,6 +682,7 @@ mailbox_pop_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
}
return status;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
if (strncasecmp (bio->buffer, "+OK", 3) != 0)
{
mpd->func = mpd->id = NULL;
......@@ -850,6 +873,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount)
case 0:
bio->len = sprintf (bio->buffer, "STAT\r\n");
bio->ptr = bio->buffer;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
mpd->state = 1;
/* Send the STAT */
case 1:
......@@ -862,6 +886,7 @@ mailbox_pop_messages_count (mailbox_t mbox, size_t *pcount)
status = bio_readline (bio);
if (status != 0)
return status;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
break;
default:
fprintf (stderr, "unknow state(messages_count)\n");
......@@ -963,6 +988,7 @@ mailbox_pop_expunge (mailbox_t mbox)
bio->len = sprintf (bio->buffer, "DELE %d\r\n",
mpd->pmessages[i]->num);
bio->ptr = bio->buffer;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
mpd->state = 1;
case 1:
status = bio_write (bio);
......@@ -989,6 +1015,7 @@ mailbox_pop_expunge (mailbox_t mbox)
}
return status;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_PROT, bio->buffer);
if (strncasecmp (bio->buffer, "+OK", 3) != 0)
{
mpd->func = mpd->id = NULL;
......
......@@ -37,7 +37,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <mailutils_errno.h>
#include <time.h>
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
......@@ -164,7 +164,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
/* sanity check */
if (name == NULL || *name == '\0')
return EINVAL;
return MU_ERROR_INVALID_ARG;
name_len = strlen (name);
......@@ -187,14 +187,14 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
/* allocate memory for mbox */
mbox = calloc (1, sizeof (*mbox));
if (mbox == NULL)
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
/* allocate specific unix mbox data */
mud = mbox->data = calloc (1, sizeof (*mud));
if (mbox->data == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
/* copy the name */
......@@ -202,7 +202,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
if (mbox->name == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
memcpy (mbox->name, name, name_len);
......@@ -228,7 +228,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
if (mud->dirname == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
memcpy (mud->dirname, name, sep - name);
......@@ -237,7 +237,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
if (mud->basename == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
memcpy (mud->basename, sep, name_len - (sep - name));
}
......@@ -249,7 +249,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
if (mud->dirname == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
mud->dirname[0] = '.';
......@@ -257,7 +257,7 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
if (mud->basename == NULL)
{
mailbox_unix_destroy (&mbox);
return ENOMEM;
return MU_ERROR_OUT_OF_MEMORY;
}
memcpy (mud->basename, name, name_len);
}
......@@ -286,6 +286,8 @@ mailbox_unix_create (mailbox_t *pmbox, const char *name)
mbox->_size = mailbox_unix_size;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_create (%s/%s)\n",
mud->dirname, mud->basename);
(*pmbox) = mbox;
return 0; /* okdoke */
......@@ -302,6 +304,9 @@ mailbox_unix_destroy (mailbox_t *pmbox)
{
size_t i;
mailbox_unix_data_t mud = mbox->data;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE,
"mailbox_unix_destroy (%s/%s)\n",
mud->dirname, mud->basename);
free (mud->dirname);
free (mud->basename);
for (i = 0; i < mud->umessages_count; i++)
......@@ -403,6 +408,8 @@ mailbox_unix_open (mailbox_t mbox, int flags)
if (mbox->auth)
auth_epilogue (mbox->auth);
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_open(%s, %d)\n",
mbox->name, flags);
/* give an appopriate way to lock */
if (mbox->locker == NULL)
locker_create (&(mbox->locker), mbox->name,
......@@ -414,15 +421,38 @@ static int
mailbox_unix_close (mailbox_t mbox)
{
mailbox_unix_data_t mud;
size_t i;
if (mbox == NULL ||
(mud = (mailbox_unix_data_t)mbox->data) == NULL)
return EINVAL;
stream_close (mbox->stream);
/* make sure we do not hold any lock for that file */
mailbox_unix_unlock (mbox);
return 0;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_close(%s)\n",
mbox->name);
/* before closing we need to remove all the messages
* - to reclaim the memory
* - to prepare for another scan.
*/
for (i = 0; i < mud->umessages_count; i++)
{
mailbox_unix_message_t mum = mud->umessages[i];
if (mum == NULL)
continue;
/* Destroy the attach messages */
attribute_destroy (&(mum->old_attr));
message_destroy (&(mum->message), mum);
/* new_attr free by message_destroy() */
/* attribute_destroy (&(mum->new_attr)); */
free (mum);
}
free (mud->umessages);
mud->umessages = NULL;
mud->messages_count = mud->umessages_count = 0;
mud->size = 0;
return stream_close (mbox->stream);
}
/* Mailbox Parsing */
......@@ -431,6 +461,8 @@ mailbox_unix_close (mailbox_t mbox)
static int
mailbox_unix_scan (mailbox_t mbox, size_t msgno, size_t *pcount)
{
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_scan(%s)\n",
mbox->name);
return mailbox_unix_scan0 (mbox, msgno, pcount, 1);
}
......@@ -568,6 +600,9 @@ mailbox_unix_expunge (mailbox_t mbox)
if (dirty == mud->messages_count)
return 0; /* nothing change, bail out */
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE, "mailbox_unix_expunge (%s)\n",
mbox->name);
/* Send notification to all the listeners
* this is redundant, we go to the loop again
* But it's more secure here since we don't
......@@ -828,6 +863,9 @@ mailbox_unix_expunge (mailbox_t mbox)
mum->body = mum->body_end = 0;
mum->header_lines = mum->body_lines = 0;
}
/* this is should reset the messages_count, the last
* argument 0 means not to send event notification
*/
mailbox_unix_scan0 (mbox, dirty, NULL, 0);
}
return status;
......@@ -1048,6 +1086,8 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
return 0;
}
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE,
"mailbox_unix_get_message(%s, %d)\n", mbox->name, msgno);
/* get the headers */
do
{
......@@ -1145,6 +1185,9 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
(mud = (mailbox_unix_data_t)mbox->data) == NULL)
return EINVAL;
mailbox_debug (mbox, MU_MAILBOX_DEBUG_TRACE,
"mailbox_unix_append_message (%s)\n", mbox->name);
mailbox_unix_lock (mbox, MU_LOCKER_WRLOCK);
{
off_t size;
......
......@@ -210,7 +210,7 @@ message_size (message_t msg, size_t *psize)
int
message_set_from (message_t msg,
int (*_from)(message_t, char *, size_t, size_t*),
int (*_from) __P ((message_t, char *, size_t, size_t*)),
void *owner)
{
if (msg == NULL)
......@@ -273,8 +273,8 @@ message_from (message_t msg, char *buf, size_t len, size_t *pnwrite)
}
int
message_set_received (message_t msg,
int (*_received) (message_t, char *, size_t , size_t *),
message_set_received (message_t msg, int (*_received)
__P ((message_t, char *, size_t , size_t *)),
void *owner)
{
if (msg == NULL)
......@@ -541,7 +541,9 @@ message_write (stream_t os, const char *buf, size_t buflen,
msg->hdr_buflen = msg->hdr_done = 0;
return status;
}
if (off > (off_t)msg->hdr_buflen)
if (off < (off_t)msg->hdr_buflen)
off = 0;
else
off -= msg->hdr_buflen;
return stream_write (bs, buf, buflen, off, pnwrite);
}
......@@ -573,6 +575,32 @@ message_get_fd (stream_t stream, int *pfd)
return stream_get_fd (is, pfd);
}
int
message_get_uidl (message_t msg, char *buffer, size_t buflen, size_t *pwritten)
{
header_t header = NULL;
if (msg == NULL || buffer == NULL || buflen == 0)
return EINVAL;
buffer[0] = '0';
if (msg->_get_uidl)
return msg->_get_uidl (msg, buffer, buflen, pwritten);
message_get_header (msg, &header);
return header_get_value (header, "X-UIDL", buffer, buflen, pwritten);
}
int
message_set_uidl (message_t msg, int (* _get_uidl)
__P ((message_t msg, char *buffer, size_t buflen, size_t *pwritten)), void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
msg->_get_uidl = _get_uidl;
return 0;
}
static int
extract_addr (const char *s, size_t n, char **presult, size_t *pnwrite)
{
......