Commit 651775c9 651775c9cd519d2fd1a87eab2e5125ec55721de7 by Alain Magloire

attribute.c attribute.h auth.h event.h header.c header.h io.c

 	io.h io0.h locker.c mbx_unix.c message.c message.h message0.h
 	registrar.c registrar.h url_pop.c

Typos and after a long debate merge istream_t/ostream_t to stream_t
better fit to the net_t object comming.
1 parent 26adb3ba
......@@ -19,6 +19,7 @@
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define MU_ATTRIBUTE_SEEN ((int)1)
......@@ -33,14 +34,13 @@ struct _attribute
{
size_t flag;
void *owner;
int ref_count;
};
int
attribute_init (attribute_t *pattr, void *owner)
{
attribute_t attr;
if (pattr == NULL)
if (pattr == NULL || owner == NULL)
return EINVAL;
attr = calloc (1, sizeof(*attr));
if (attr == NULL)
......@@ -57,12 +57,8 @@ attribute_destroy (attribute_t *pattr, void *owner)
{
attribute_t attr = *pattr;
attr->ref_count--;
if ((attr->owner && attr->owner == owner) ||
(attr->owner == NULL && attr->ref_count <= 0))
{
if (attr->owner == owner)
free (attr);
}
/* loose the link */
*pattr = NULL;
}
......@@ -70,66 +66,94 @@ attribute_destroy (attribute_t *pattr, void *owner)
}
int
attribute_set_seen (attribute_t attr)
attribute_set_seen (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_SEEN;
return 0;
}
return EACCES;
}
int
attribute_set_answered (attribute_t attr)
attribute_set_answered (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_ANSWERED;
return 0;
}
return EACCES;
}
int
attribute_set_flagged (attribute_t attr)
attribute_set_flagged (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_FLAGGED;
return 0;
}
return EACCES;
}
int
attribute_set_read (attribute_t attr)
attribute_set_read (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_READ;
return 0;
}
return EACCES;
}
int
attribute_set_deleted (attribute_t attr)
attribute_set_deleted (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_DELETED;
return 0;
}
return EACCES;
}
int
attribute_set_draft (attribute_t attr)
attribute_set_draft (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (owner == attr->owner)
{
attr->flag |= MU_ATTRIBUTE_DRAFT;
return 0;
}
return EACCES;
}
int
attribute_set_recent (attribute_t attr)
attribute_set_recent (attribute_t attr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (attr == NULL)
{
attr->flag |= MU_ATTRIBUTE_RECENT;
return 0;
}
return EACCES;
}
int
......@@ -189,66 +213,94 @@ attribute_is_recent (attribute_t attr)
}
int
attribute_unset_seen (attribute_t attr)
attribute_unset_seen (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_SEEN;
return 0;
}
return EACCES;
}
int
attribute_unset_answered (attribute_t attr)
attribute_unset_answered (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_ANSWERED;
return 0;
}
return EACCES;
}
int
attribute_unset_flagged (attribute_t attr)
attribute_unset_flagged (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_FLAGGED;
return 0;
}
return EACCES;
}
int
attribute_unset_read (attribute_t attr)
attribute_unset_read (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_READ;
return 0;
}
return EACCES;
}
int
attribute_unset_deleted (attribute_t attr)
attribute_unset_deleted (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_DELETED;
return 0;
}
return EACCES;
}
int
attribute_unset_draft (attribute_t attr)
attribute_unset_draft (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_DRAFT;
return 0;
}
return EACCES;
}
int
attribute_unset_recent (attribute_t attr)
attribute_unset_recent (attribute_t attr, void *owner)
{
if (attr == NULL)
return 0;
if (owner == attr->owner)
{
attr->flag &= ~MU_ATTRIBUTE_RECENT;
return 0;
}
return EACCES;
}
int
......@@ -260,20 +312,42 @@ attribute_is_equal (attribute_t attr, attribute_t attr2)
}
int
attribute_copy (attribute_t dest, attribute_t src)
attribute_copy (attribute_t dest, attribute_t src, void *dest_owner)
{
if (dest == NULL || src == NULL)
return EINVAL;
if (dest->owner == dest_owner)
{
memcpy (dest, src, sizeof (*dest));
return 0;
}
return EACCES;
}
int
attribute_get_owner (attribute_t attr, void **powner)
string_to_attribute (const char *buffer, size_t len,
attribute_t *pattr, void *owner)
{
if (attr == NULL)
return EINVAL;
if (powner)
*powner = attr->owner;
char *sep;
int status;
status = attribute_init (pattr, owner);
if (status != 0)
return status;
/* Set the attribute */
if (len > 7 && strncasecmp (buffer, "Status:", 7) == 0)
{
sep = strchr(buffer, ':'); /* pass the ':' */
if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
attribute_set_read (*pattr, owner);
if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
attribute_set_seen (*pattr, owner);
if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL)
attribute_set_answered (*pattr, owner);
if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL)
attribute_set_flagged (*pattr, owner);
}
return 0;
}
......
......@@ -18,6 +18,8 @@
#ifndef _ATTRIBUTE_H
#define _ATTRIBUTE_H
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
......@@ -44,25 +46,29 @@ extern int attribute_is_draft __P ((attribute_t));
extern int attribute_is_recent __P ((attribute_t));
extern int attribute_is_read __P ((attribute_t));
extern int attribute_set_seen __P ((attribute_t));
extern int attribute_set_answered __P ((attribute_t));
extern int attribute_set_flagged __P ((attribute_t));
extern int attribute_set_deleted __P ((attribute_t));
extern int attribute_set_draft __P ((attribute_t));
extern int attribute_set_recent __P ((attribute_t));
extern int attribute_set_read __P ((attribute_t));
extern int attribute_unset_seen __P ((attribute_t));
extern int attribute_unset_answered __P ((attribute_t));
extern int attribute_unset_flagged __P ((attribute_t));
extern int attribute_unset_deleted __P ((attribute_t));
extern int attribute_unset_draft __P ((attribute_t));
extern int attribute_unset_recent __P ((attribute_t));
extern int attribute_unset_read __P ((attribute_t));
extern int attribute_set_seen __P ((attribute_t, void *owner));
extern int attribute_set_answered __P ((attribute_t, void *owner));
extern int attribute_set_flagged __P ((attribute_t, void *owner));
extern int attribute_set_deleted __P ((attribute_t, void *owner));
extern int attribute_set_draft __P ((attribute_t, void *owner));
extern int attribute_set_recent __P ((attribute_t, void *owner));
extern int attribute_set_read __P ((attribute_t, void *owner));
extern int attribute_unset_seen __P ((attribute_t, void *owner));
extern int attribute_unset_answered __P ((attribute_t, void *owner));
extern int attribute_unset_flagged __P ((attribute_t, void *owner));
extern int attribute_unset_deleted __P ((attribute_t, void *owner));
extern int attribute_unset_draft __P ((attribute_t, void *owner));
extern int attribute_unset_recent __P ((attribute_t, void *owner));
extern int attribute_unset_read __P ((attribute_t, void *owner));
extern int attribute_is_equal __P ((attribute_t att1, attribute_t att2));
extern int attribute_copy __P ((attribute_t dst, attribute_t src));
extern int attribute_copy __P ((attribute_t dst,
attribute_t src, void *dest_owner));
extern int string_to_attribute __P ((const char *buf, size_t len,
attribute_t *pattr, void *owner));
extern int attribute_get_owner __P ((attribute_t attr, void **owner));
#ifdef __cplusplus
}
......
......@@ -28,7 +28,7 @@
#endif
#endif /*__P */
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
......@@ -54,7 +54,7 @@ extern int auth_set_epilogue __P ((auth_t auth,
int (*_epilogue) __P ((auth_t)),
void *owner));
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -26,7 +26,7 @@
# endif
#endif /* __P */
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
......@@ -38,7 +38,7 @@ struct _event
};
typedef struct _event *event_t;
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -27,9 +27,9 @@
#include <errno.h>
static int header_parse (header_t h, char *blurb, int len);
static int header_read (istream_t is, char *buf, size_t buflen,
static int header_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread);
static int header_write (ostream_t os, const char *buf, size_t buflen,
static int header_write (stream_t os, const char *buf, size_t buflen,
off_t off, size_t *pnwrite);
struct _hdr
......@@ -49,8 +49,7 @@ struct _header
struct _hdr *hdr;
/* streams */
istream_t is;
ostream_t os;
stream_t stream;
/* owner ? */
void *owner;
......@@ -74,13 +73,12 @@ header_init (header_t *ph, const char *blurb, size_t len, void *owner)
return status;
}
status = istream_init (&(h->is), header_read, h);
status = stream_init (&(h->stream), h);
if (status != 0)
return status;
status = ostream_init (&(h->os), header_write, h);
if (status != 0)
return status;
stream_set_read (h->stream, header_read, h);
stream_set_write (h->stream, header_write, h);
*ph = h;
return status;
......@@ -101,8 +99,7 @@ header_destroy (header_t *ph, void *owner)
(h->owner == NULL && h->ref_count <= 0))
{
/* io */
istream_destroy (&(h->is), h);
ostream_destroy (&(h->os), h);
stream_destroy (&(h->stream), h);
free (h->hdr);
free (h->blurb);
......@@ -308,7 +305,7 @@ header_entry_count (header_t header, size_t *pnum)
}
int
header_size (header_t header, size_t *pnum)
header_get_size (header_t header, size_t *pnum)
{
if (header == NULL)
return EINVAL;
......@@ -364,7 +361,7 @@ header_entry_value (header_t header, size_t num, char *buf,
}
static int
header_write (ostream_t os, const char *buf, size_t buflen,
header_write (stream_t os, const char *buf, size_t buflen,
off_t off, size_t *pnwrite)
{
header_t header;
......@@ -382,7 +379,7 @@ header_write (ostream_t os, const char *buf, size_t buflen,
}
static int
header_read (istream_t is, char *buf, size_t buflen,
header_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread)
{
header_t header;
......@@ -409,19 +406,10 @@ header_read (istream_t is, char *buf, size_t buflen,
}
int
header_get_istream (header_t header, istream_t *pis)
header_get_stream (header_t header, stream_t *pstream)
{
if (header == NULL || pis == NULL)
if (header == NULL || pstream == NULL)
return EINVAL;
*pis = header->is;
*pstream = header->stream;
return 0;
}
int
rfc822_get_ostream (header_t header, ostream_t *pos)
{
if (header == NULL || pos == NULL)
return EINVAL;
*pos = header->os;
return ENOSYS;
}
......
......@@ -29,7 +29,7 @@
#endif
#endif /*__P */
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
......@@ -79,10 +79,10 @@ extern int header_entry_name __P ((header_t, size_t num, char *buf,
size_t buflen, size_t *total));
extern int header_entry_value __P ((header_t, size_t num, char *buf,
size_t buflen, size_t *total));
extern int header_get_istream __P ((header_t, istream_t *pis));
extern int header_get_ostream __P ((header_t, ostream_t *pos));
extern int header_get_stream __P ((header_t, stream_t *stream));
extern int header_get_size __P ((header_t, size_t *size));
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -22,69 +22,76 @@
#include <stdio.h>
int
istream_init (istream_t *pis, int (*_read)
__P ((istream_t, char *, size_t, off_t, size_t *)),
void *owner)
stream_init (stream_t *pstream, void *owner)
{
istream_t is;
if (pis == NULL)
stream_t stream;
if (pstream == NULL || owner == NULL)
return EINVAL;
is = calloc (1, sizeof (*is));
if (is == NULL)
stream = calloc (1, sizeof (*stream));
if (stream == NULL)
return ENOMEM;
is->owner = owner;
is->_read = _read;
*pis = is;
stream->owner = owner;
*pstream = stream;
return 0;
}
void
stream_destroy (stream_t *pstream, void *owner)
{
if (pstream && *pstream)
{
stream_t stream = *pstream;
if (stream->owner == owner)
free (stream);
*pstream = NULL;
}
}
int
ostream_init (ostream_t *pos, int (*_write)
__P ((ostream_t, const char *, size_t, off_t, size_t *)),
void *owner)
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
ostream_t os;
if (pos == NULL)
if (stream == NULL)
return EINVAL;
os = calloc (1, sizeof (*os));
if (os == NULL)
return ENOMEM;
os->owner = owner;
os->_write = _write;
*pos = os;
if (owner == stream->owner)
{
stream->_get_fd = _get_fd;
return 0;
}
return EACCES;
}
void
istream_destroy (istream_t *pis, void *owner)
int
stream_set_read (stream_t stream, int (*_read)
(stream_t, char *, size_t, off_t, size_t *),
void *owner)
{
if (pis && *pis)
if (stream == NULL)
return EINVAL;
if (owner == stream->owner)
{
istream_t is = *pis;
is->ref_count--;
if ((is->owner && is->owner == owner) ||
(is->owner == NULL && is->ref_count <= 0))
free (is);
*pis = NULL;
stream->_read = _read;
return 0;
}
return EACCES;
}
void
ostream_destroy (ostream_t *pos, void *owner)
int
stream_set_write (stream_t stream, int (*_write)
__P ((stream_t, const char *, size_t, off_t, size_t *)),
void *owner)
{
if (pos && (*pos))
if (stream == NULL)
return EINVAL;
if (stream->owner == owner)
{
ostream_t os = *pos;
os->ref_count--;
if ((os->owner && os->owner == owner) ||
(os->owner == NULL && os->ref_count <= 0))
free (os);
*pos = NULL;
stream->_write = _write;
return 0;
}
return EACCES;
}
int
istream_read (istream_t is, char *buf, size_t count,
stream_read (stream_t is, char *buf, size_t count,
off_t offset, size_t *pnread)
{
if (is == NULL || is->_read == NULL)
......@@ -93,10 +100,18 @@ istream_read (istream_t is, char *buf, size_t count,
}
int
ostream_write (ostream_t os, const char *buf, size_t count,
stream_write (stream_t os, const char *buf, size_t count,
off_t offset, size_t *pnwrite)
{
if (os == NULL || os->_write == NULL)
return EINVAL;
return os->_write (os, buf, count, offset, pnwrite);
}
int
stream_get_fd (stream_t stream, int *pfd)
{
if (stream == NULL || stream->_get_fd == NULL)
return EINVAL;
return stream->_get_fd (stream, pfd);
}
......
......@@ -21,7 +21,7 @@
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
extern "C" { /*}*/
#endif
#ifndef __P
......@@ -32,16 +32,30 @@ extern "C" {
# endif
#endif /*__P */
struct _istream;
typedef struct _istream *istream_t;
struct _ostream;
typedef struct _ostream *ostream_t;
extern int istream_read __P ((istream_t, char *, size_t, off_t, size_t *));
extern int ostream_write __P ((ostream_t, const char *, size_t,
struct _stream;
typedef struct _stream *stream_t;
extern int stream_init __P ((stream_t *, void *owner));
extern void stream_destroy __P ((stream_t *, void *owner));
extern int stream_set_fd __P ((stream_t,
int (*_get_fd)(stream_t, int *),
void *owner));
extern int stream_set_read __P ((stream_t,
int (*_read) __P ((stream_t, char *,
size_t, off_t, size_t *)),
void *owner));
extern int stream_set_write __P ((stream_t,
int (*_write) __P ((stream_t, const char *,
size_t, off_t,
size_t *)),
void *owner));
extern int stream_get_fd __P ((stream_t , int *));
extern int stream_read __P ((stream_t, char *, size_t, off_t, size_t *));
extern int stream_write __P ((stream_t, const char *, size_t,
off_t, size_t *));
#ifdef __cplusplus
}
#endif
......
......@@ -32,33 +32,14 @@ extern "C" {
# endif
#endif /*__P */
struct _istream
struct _stream
{
/* owner of the stream can not be a specific type */
void *owner;
int ref_count;
int (*_read) __P ((istream_t, char *, size_t, off_t, size_t *));
int (*_get_fd) __P ((stream_t, int *));
int (*_read) __P ((stream_t, char *, size_t, off_t, size_t *));
int (*_write) __P ((stream_t, const char *, size_t, off_t, size_t *));
};
struct _ostream
{
/* owner of the stream can not be a specific type */
void *owner;
int ref_count;
int (*_write) __P ((ostream_t, const char *, size_t, off_t, size_t *));
};
extern int istream_init __P ((istream_t *,
int (*_read) __P ((istream_t, char *,
size_t, off_t, size_t *)),
void *owner));
extern void istream_destroy __P ((istream_t *, void *owner));
extern int ostream_init __P ((ostream_t *,
int (*_write) __P ((ostream_t, const char *,
size_t, off_t, size_t *)),
void *owner));
extern void ostream_destroy __P ((ostream_t *, void *owner));
#ifdef __cplusplus
}
#endif
......
......@@ -95,7 +95,7 @@ locker_lock (locker_t lock, int flags)
int fd = -1;
char buf[16];
pid_t pid;
int remove = 0;
int removed = 0;
(void)flags;
if (lock == NULL)
......@@ -115,22 +115,22 @@ locker_lock (locker_t lock, int flags)
{
/* process is gone so we try to remove the lock */
if (kill(pid, 0) == -1)
remove = 1;
removed = 1;
}
}
}
if (lock->flags & MU_LOCKER_TIME)
{
struct stat buf;
struct stat stbuf;
fstat(fd, &buf);
fstat(fd, &stbuf);
/* the lock has expired */
if ((time(NULL) - buf.st_mtime) > LOCK_EXPIRE_TIME)
remove = 1;
if ((time(NULL) - stbuf.st_mtime) > LOCK_EXPIRE_TIME)
removed = 1;
}
close(fd);
if (remove)
if (removed)
unlink(lock->fname);
}
......
......@@ -137,7 +137,8 @@ static ssize_t mailbox_unix_get_header (mailbox_t, size_t msgno, char *h,
/* private stuff */
static int mailbox_unix_is_deleted (mailbox_t mbox, size_t msgno);
static int mailbox_unix_validity (mailbox_t mbox, size_t msgno);
static int mailbox_unix_readstream (istream_t is, char *buffer, size_t buflen,
static int mailbox_unix_getfd (stream_t is, int *pfd);
static int mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
off_t off, size_t *pnread);
static int mailbox_unix_is_from (const char *);
static int mailbox_unix_readhdr (mailbox_t mbox, char *buf, size_t len,
......@@ -338,7 +339,7 @@ mailbox_unix_open (mailbox_t mbox, int flags)
mailbox_unix_data_t mud;
int fd = -1;
int flg = 0;
char *mode;
const char *mode;
if (mbox == NULL ||
(mud = (mailbox_unix_data_t)mbox->data) == NULL)
......@@ -601,20 +602,20 @@ mailbox_unix_readhdr (mailbox_t mbox, char *buf, size_t len,
}
}
/* Set the attribute */
else if (strncmp (buf, "Status:", 7) == 0)
else if (strncasecmp (buf, "Status:", 7) == 0)
{
mum->hdr_status_end = ftell (mud->file);
mum->hdr_status = mum->hdr_status_end - strlen (buf);
sep = strchr(buf, ':'); /* pass the ':' */
if (strchr (sep, 'R') != NULL)
attribute_set_read (mum->old_attr);
if (strchr (sep, 'O') != NULL)
attribute_set_seen (mum->old_attr);
if (strchr (sep, 'A') != NULL)
attribute_set_answered (mum->old_attr);
if (strchr (sep, 'F') != NULL)
attribute_set_flagged (mum->old_attr);
attribute_copy (mum->new_attr, mum->old_attr);
if (strchr (sep, 'R') != NULL || strchr (sep, 'r') != NULL)
attribute_set_read (mum->old_attr, mbox);
if (strchr (sep, 'O') != NULL || strchr (sep, 'o') != NULL)
attribute_set_seen (mum->old_attr, mbox);
if (strchr (sep, 'A') != NULL || strchr (sep, 'a') != NULL)
attribute_set_answered (mum->old_attr, mbox);
if (strchr (sep, 'F') != NULL || strchr (sep, 'f') != NULL)
attribute_set_flagged (mum->old_attr, mbox);
attribute_copy (mum->new_attr, mum->old_attr, mbox);
}
}
/* check for any dubious conditions */
......@@ -1018,7 +1019,7 @@ mailbox_unix_expunge (mailbox_t mbox)
int status = 0;
int oflags;
sigset_t sigset;
FILE *tmpfile;
FILE *tempfile;
size_t nread;
size_t i, j, first;
off_t marker = 0;
......@@ -1034,14 +1035,14 @@ mailbox_unix_expunge (mailbox_t mbox)
if (mud->messages_count == 0)
return 0;
tmpfile = mailbox_unix_tmpfile (mbox, tmpmbox);
if (tmpfile == NULL)
tempfile = mailbox_unix_tmpfile (mbox, tmpmbox);
if (tempfile == NULL)
return errno;
/* Get the lock */
if (mailbox_unix_lock (mbox, MU_LOCKER_WRLOCK) < 0)
{
fclose (tmpfile);
fclose (tempfile);
remove (tmpmbox);
free (tmpmbox);
return ENOLCK;
......@@ -1125,7 +1126,7 @@ mailbox_unix_expunge (mailbox_t mbox)
first = 0;
else
{
fputc ('\n', tmpfile);
fputc ('\n', tempfile);
total++;
}
/* copy the header */
......@@ -1144,7 +1145,7 @@ mailbox_unix_expunge (mailbox_t mbox)
{
nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
if (fread (buffer, sizeof (*buffer), nread, mud->file) != nread
|| fwrite(buffer, sizeof(*buffer), nread, tmpfile) != nread)
|| fwrite(buffer, sizeof(*buffer), nread, tempfile) != nread)
{
status = errno;
goto bailout;
......@@ -1155,29 +1156,29 @@ mailbox_unix_expunge (mailbox_t mbox)
/* put the new attributes */
{
attribute_t attr = mum->new_attr;
fputs ("Status: ", tmpfile);
fputs ("Status: ", tempfile);
total += 8;
if (attribute_is_seen (attr))
{
fputc ('R', tmpfile);
fputc ('R', tempfile);
total++;
}
if (attribute_is_answered (attr))
{
fputc ('A', tmpfile);
fputc ('A', tempfile);
total++;
}
if (attribute_is_flagged (attr))
{
fputc ('F', tmpfile);
fputc ('F', tempfile);
total++;
}
if (attribute_is_read (attr))
{
fputc ('O', tmpfile);
fputc ('O', tempfile);
total++;
}
fputc ('\n', tmpfile);
fputc ('\n', tempfile);
total++;
}
/* skip the status field */
......@@ -1196,7 +1197,7 @@ mailbox_unix_expunge (mailbox_t mbox)
{
nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
if (fread (buffer, sizeof (*buffer), nread, mud->file) != nread
|| fwrite(buffer, sizeof(*buffer), nread, tmpfile) != nread)
|| fwrite(buffer, sizeof(*buffer), nread, tempfile) != nread)
{
status = errno;
goto bailout;
......@@ -1206,7 +1207,7 @@ mailbox_unix_expunge (mailbox_t mbox)
}
/* Separate the header from body */
fputc ('\n', tmpfile);
fputc ('\n', tempfile);
total++;
/* copy the body */
......@@ -1220,7 +1221,7 @@ mailbox_unix_expunge (mailbox_t mbox)
{
nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
if (fread (buffer, sizeof (*buffer), nread, mud->file) != nread
|| fwrite(buffer, sizeof(*buffer), nread, tmpfile) != nread)
|| fwrite(buffer, sizeof(*buffer), nread, tempfile) != nread)
{
status = errno;
goto bailout;
......@@ -1254,7 +1255,7 @@ mailbox_unix_expunge (mailbox_t mbox)
{
nread = (len < sizeof (buffer)) ? len : sizeof (buffer);
if (fread (buffer, sizeof (*buffer), nread, mud->file) != nread
|| fwrite(buffer, sizeof(*buffer), nread, tmpfile) != nread)
|| fwrite(buffer, sizeof(*buffer), nread, tempfile) != nread)
{
status = errno;
goto bailout;
......@@ -1270,11 +1271,11 @@ mailbox_unix_expunge (mailbox_t mbox)
status = errno;
goto bailout;
}
rewind (tmpfile);
rewind (tempfile);
errno = 0;
while ((nread = fread (buffer, sizeof (*buffer),
sizeof (buffer), tmpfile)) != 0)
sizeof (buffer), tempfile)) != 0)
{
if (fwrite (buffer, sizeof (*buffer), nread, mud->file) != nread)
{
......@@ -1289,7 +1290,7 @@ mailbox_unix_expunge (mailbox_t mbox)
}
/* how can I handle error here ?? */
clearerr (tmpfile);
clearerr (tempfile);
clearerr (mud->file);
fflush (mud->file);
......@@ -1310,13 +1311,25 @@ bailout:
mailbox_unix_unlock (mbox);
funlockfile (mud->file);
mailbox_unix_iunlock (mbox);
fclose (tmpfile);
fclose (tempfile);
sigprocmask (SIG_UNBLOCK, &sigset, 0);
return status;
}
static int
mailbox_unix_readstream (istream_t is, char *buffer, size_t buflen,
mailbox_unix_getfd (stream_t is, int *pfd)
{
mailbox_unix_message_t mum;
if (is == NULL || (mum = (mailbox_unix_message_t)is->owner) == NULL)
return EINVAL;
if (pfd)
*pfd = fileno (mum->file);
return 0;
}
static int
mailbox_unix_readstream (stream_t is, char *buffer, size_t buflen,
off_t off, size_t *pnread)
{
mailbox_unix_message_t mum;
......@@ -1426,7 +1439,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
mailbox_unix_data_t mud;
mailbox_unix_message_t mum;
message_t msg = NULL;
istream_t is = NULL;
stream_t stream = NULL;
header_t header = NULL;
if (mbox == NULL || pmsg == NULL ||
......@@ -1488,13 +1501,16 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
free (pbuf);
message_set_header (msg, header, mum);
/* prepare the istream */
if ((status = istream_init (&is, mailbox_unix_readstream, mum)) != 0 ||
(status = message_set_istream (msg, is, mum)) != 0)
/* prepare the stream */
status = stream_init (&stream, mum);
if (status != 0)
{
message_destroy (&msg, mum);
return status;
}
stream_set_read (stream, mailbox_unix_readstream, mum);
stream_set_fd (stream, mailbox_unix_getfd, mum);
message_set_stream (msg, stream, mum);
/* set the attribute */
status = message_set_attribute (msg, mum->new_attr, mum);
......@@ -1505,7 +1521,7 @@ mailbox_unix_get_message (mailbox_t mbox, size_t msgno, message_t *pmsg)
}
/* set the size */
status = message_set_size (msg, mum->body_end - mum->body, mum);
status = message_set_size (msg, mum->body_end - mum->header, mum);
if (status != 0)
{
message_destroy (&msg, mum);
......@@ -1542,7 +1558,7 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
char buffer[BUFSIZ];
size_t nread;
off_t off = 0;
istream_t is;
stream_t is;
header_t hdr;
int status;
......@@ -1562,11 +1578,11 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
/* header */
message_get_header (msg, &hdr);
header_get_istream (hdr, &is);
header_get_stream (hdr, &is);
if (st.st_size != 0)
fputc ('\n', mud->file);
do {
status = istream_read (is, buffer, sizeof (buffer), off, &nread);
status = stream_read (is, buffer, sizeof (buffer), off, &nread);
if (status != 0)
return status;
fwrite (buffer, sizeof (*buffer), nread, mud->file);
......@@ -1579,9 +1595,9 @@ mailbox_unix_append_message (mailbox_t mbox, message_t msg)
/* body */
off = 0;
message_get_istream (msg, &is);
message_get_stream (msg, &is);
do {
istream_read (is, buffer, sizeof (buffer), off, &nread);
stream_read (is, buffer, sizeof (buffer), off, &nread);
fwrite (buffer, sizeof (*buffer), nread, mud->file);
off += nread;
} while (nread > 0);
......
......@@ -30,17 +30,17 @@
static int body_init (body_t *pbody, void *owner);
static void body_destroy (body_t *pbody, void *owner);
static int message_read (istream_t is, char *buf, size_t buflen,
static int message_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread );
static int message_write (ostream_t os, const char *buf, size_t buflen,
static int message_write (stream_t os, const char *buf, size_t buflen,
off_t off, size_t *pnwrite);
static int message_get_fd (stream_t stream, int *pfd);
int
message_clone (message_t msg)
{
int status;
istream_t is;
ostream_t os;
stream_t stream;
header_t header;
attribute_t attribute;
body_t body;
......@@ -68,12 +68,12 @@ message_clone (message_t msg)
/* retreive the header */
{
header = msg->header;
status = header_get_istream (header, &is);
status = header_get_stream (header, &stream);
if (status != 0)
return status;
do {
status = istream_read (is, buffer, sizeof (buffer), offset, &nread);
status = stream_read (stream, buffer, sizeof (buffer), offset, &nread);
if (status != 0)
{
free (pbuf);
......@@ -112,12 +112,12 @@ message_clone (message_t msg)
return status;
}
is = msg->is;
stream = msg->stream;
offset = 0;
do {
do
{
status = istream_read (is, buffer, sizeof (buffer), offset, &nread);
status = stream_read (stream, buffer, sizeof (buffer), offset, &nread);
} while (status == EAGAIN);
if (status != 0)
{
......@@ -132,21 +132,15 @@ message_clone (message_t msg)
}
/* set the body with the streams */
status = istream_init (&is, message_read, msg);
if (status != 0)
{
header_destroy (&header, msg);
body_destroy (&body, msg);
return status;
}
status = ostream_init (&os, message_write, msg);
if (status != 0)
status = stream_init (&stream, msg);
if (status != 0 )
{
header_destroy (&header, msg);
body_destroy (&body, msg);
istream_destroy (&is, msg);
return status;
}
stream_set_read (stream, message_read, msg);
stream_set_write (stream, message_write, msg);
/* attribute */
status = attribute_init (&attribute, msg);
......@@ -154,16 +148,14 @@ message_clone (message_t msg)
{
header_destroy (&header, msg);
body_destroy (&body, msg);
istream_destroy (&is, msg);
ostream_destroy (&os, msg);
stream_destroy (&stream, msg);
}
attribute_copy (attribute, msg->attribute);
attribute_copy (attribute, msg->attribute, msg);
/* every thing went ok */
msg->header = header;
msg->attribute = attribute;
msg->is = is;
msg->os = os;
msg->stream = stream;
msg->body = body;
msg->size = offset;
msg->ref_count++;
......@@ -210,8 +202,7 @@ message_destroy (message_t *pmsg, void *owner)
{
header_t header = msg->header;
attribute_t attribute = msg->attribute;
istream_t is = msg->is;
ostream_t os = msg->os;
stream_t stream = msg->stream;
body_t body = msg->body;
/* notify the listeners */
......@@ -220,10 +211,8 @@ message_destroy (message_t *pmsg, void *owner)
header_destroy (&header, owner);
/* attribute */
attribute_destroy (&attribute, owner);
/* istream */
istream_destroy (&is, owner);
/* ostream */
ostream_destroy (&os, owner);
/* stream */
stream_destroy (&stream, owner);
/* if sometype of floating/temporary message */
if (body)
......@@ -281,75 +270,43 @@ message_set_header (message_t msg, header_t hdr, void *owner)
}
int
message_get_istream (message_t msg, istream_t *pis)
message_get_stream (message_t msg, stream_t *pstream)
{
if (msg == NULL || pis == NULL)
if (msg == NULL || pstream == NULL)
return EINVAL;
if (msg->is == NULL && msg->owner == NULL)
if (msg->stream == NULL && msg->owner == NULL)
{
istream_t is;
stream_t stream;
int status;
status = istream_init (&is, message_read, msg);
if (status != 0)
return status;
/* make sure we've clean */
istream_destroy (&(msg->is), msg);
msg->is = is;
}
*pis = msg->is;
return 0;
}
int
message_set_istream (message_t msg, istream_t is, void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
/* make sure we destroy the old one if it is own by the message */
istream_destroy (&(msg->is), msg);
msg->is = is;
return 0;
}
int
message_get_ostream (message_t msg, ostream_t *pos)
{
if (msg == NULL || pos == NULL)
return EINVAL;
/* lazy floating message the body is created when
* doing the first message_write creation
*/
if (msg->os == NULL && msg->owner == NULL)
{
ostream_t os;
int status;
status = ostream_init (&os, message_write, msg);
status = stream_init (&stream, msg);
if (status != 0)
return status;
stream_set_read (stream, message_read, msg);
stream_set_write (stream, message_write, msg);
stream_set_fd (stream, message_get_fd, msg);
/* make sure we've clean */
ostream_destroy (&(msg->os), msg);
msg->os = os;
stream_destroy (&(msg->stream), msg);
msg->stream = stream;
}
*pos = msg->os;
*pstream = msg->stream;
return 0;
}
int
message_set_ostream (message_t msg, ostream_t os, void *owner)
message_set_stream (message_t msg, stream_t stream, void *owner)
{
if (msg == NULL)
return EINVAL;
if (msg->owner != owner)
return EACCES;
/* make sure we destroy the old one if it is own by the message */
ostream_destroy (&(msg->os), msg);
msg->os = os;
stream_destroy (&(msg->stream), msg);
msg->stream = stream;
return 0;
}
......@@ -528,7 +485,7 @@ body_destroy (body_t *pbody, void *owner)
}
static int
message_read (istream_t is, char *buf, size_t buflen,
message_read (stream_t is, char *buf, size_t buflen,
off_t off, size_t *pnread )
{
message_t msg;
......@@ -568,7 +525,7 @@ message_read (istream_t is, char *buf, size_t buflen,
}
static int
message_write (ostream_t os, const char *buf, size_t buflen,
message_write (stream_t os, const char *buf, size_t buflen,
off_t off, size_t *pnwrite)
{
message_t msg;
......@@ -614,3 +571,29 @@ message_write (ostream_t os, const char *buf, size_t buflen,
*pnwrite = nwrite;
return 0;
}
static int
message_get_fd (stream_t stream, int *pfd)
{
message_t msg;
body_t body;
if (stream == NULL || (msg = stream->owner) == NULL)
return EINVAL;
/* Probably being lazy, then create a body for the stream */
if (msg->body == NULL)
{
int status = body_init (&body, msg);
if (status != 0 )
return status;
msg->body = body;
}
else
body = msg->body;
if (pfd)
*pfd = fileno (body->file);
return 0;
}
......
......@@ -32,7 +32,7 @@
# endif
#endif /* __P */
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
......@@ -54,10 +54,8 @@ extern void message_destroy __P ((message_t *, void *owner));
extern int message_get_header __P ((message_t, header_t *));
extern int message_set_header __P ((message_t, header_t, void *owner));
extern int message_get_istream __P ((message_t, istream_t *));
extern int message_set_istream __P ((message_t, istream_t, void *owner));
extern int message_get_ostream __P ((message_t, ostream_t *));
extern int message_set_ostream __P ((message_t, ostream_t, void *owner));
extern int message_get_stream __P ((message_t, stream_t *));
extern int message_set_stream __P ((message_t, stream_t, void *owner));
extern int message_is_multipart __P ((message_t));
......@@ -75,7 +73,7 @@ extern int message_register __P ((message_t msg, size_t type,
int (*action) (size_t typ, void *arg),
void *arg));
extern int message_deregister __P ((message_t msg, void *action));
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -27,7 +27,7 @@
#include <sys/types.h>
#include <stdio.h>
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
......@@ -57,8 +57,7 @@ typedef struct _body * body_t;
struct _message
{
header_t header;
istream_t is;
ostream_t os;
stream_t stream;
body_t body;
attribute_t attribute;
size_t num;
......@@ -77,17 +76,15 @@ struct _message
int (*_get_attribute) __P ((message_t msg, attribute_t *attr));
int (*_set_attribute) __P ((message_t msg, attribute_t attr, void *owner));
int (*_get_istream) __P ((message_t msg, istream_t *));
int (*_set_istream) __P ((message_t msg, istream_t, void *owner));
int (*_get_ostream) __P ((message_t msg, ostream_t *));
int (*_set_ostream) __P ((message_t msg, ostream_t, void *owner));
int (*_get_stream) __P ((message_t msg, stream_t *));
int (*_set_stream) __P ((message_t msg, stream_t, void *owner));
int (*_size) __P ((message_t msg, size_t *size));
int (*_clone) __P ((message_t msg, message_t *cmsg));
};
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -47,7 +47,7 @@ free_ureg (struct url_registrar *ureg)
{
if (ureg)
{
free (ureg->scheme);
free ((char *)ureg->scheme);
free (ureg);
}
}
......@@ -57,7 +57,7 @@ free_mreg (struct mailbox_registrar *mreg)
{
if (mreg)
{
free (mreg->name);
free ((char *)mreg->name);
free (mreg);
}
}
......
......@@ -31,20 +31,20 @@
# endif
#endif /*__P */
#ifdef _cpluscplus
#ifdef _cplusplus
extern "C" {
#endif
struct url_registrar
{
char *scheme;
const char *scheme;
int (*_init) __P ((url_t *, const char * name));
void (*_destroy) __P ((url_t *));
};
struct mailbox_registrar
{
char *name;
const char *name;
int (*_init) __P ((mailbox_t *, const char *name));
void (*_destroy) __P ((mailbox_t *));
};
......@@ -66,7 +66,7 @@ extern int registrar_get_entry __P ((size_t num, struct url_registrar **ureg,
extern int registrar_list __P ((struct url_registrar **ureg,
struct mailbox_registrar **mreg,
int *id, registrar_t *reg));
#ifdef _cpluscplus
#ifdef _cplusplus
}
#endif
......
......@@ -82,7 +82,7 @@ url_pop_destroy (url_t *purl)
static int
url_pop_init (url_t *purl, const char *name)
{
const char *host_port, *index;
const char *host_port, *indexe;
struct url_registrar *ureg = &_url_pop_registrar;
size_t len, scheme_len = strlen (ureg->scheme);
url_t url;
......@@ -115,34 +115,34 @@ url_pop_init (url_t *purl, const char *name)
name += scheme_len; /* pass the scheme */
/* looking for "user;auth=auth-enc" */
for (index = name; index != host_port; index++)
for (indexe = name; indexe != host_port; indexe++)
{
/* Auth ? */
if (*index == ';')
if (*indexe == ';')
{
/* make sure it the token */
if (strncasecmp(index + 1, "auth=", 5) == 0)
if (strncasecmp(indexe + 1, "auth=", 5) == 0)
break;
}
}
if (index == name)
if (indexe == name)
{
url_pop_destroy (&url);
return -1;
}
/* USER */
url->user = malloc(index - name + 1);
url->user = malloc(indexe - name + 1);
if (url->user == NULL)
{
url_pop_destroy (&url);
return -1;
}
((char *)memcpy(url->user, name, index - name))[index - name] = '\0';
((char *)memcpy(url->user, name, indexe - name))[indexe - name] = '\0';
/* AUTH */
if ((host_port - index) <= 6 /*strlen(";AUTH=")*/)
if ((host_port - indexe) <= 6 /*strlen(";AUTH=")*/)
{
/* Use default AUTH '*' */
up->auth = malloc (1 + 1);
......@@ -155,12 +155,12 @@ url_pop_init (url_t *purl, const char *name)
else
{
/* move pass AUTH= */
index += 6;
up->auth = malloc (host_port - index + 1);
indexe += 6;
up->auth = malloc (host_port - indexe + 1);
if (up->auth)
{
((char *)memcpy (up->auth, index, host_port - index))
[host_port - index] = '\0';
((char *)memcpy (up->auth, indexe, host_port - indexe))
[host_port - indexe] = '\0';
}
}
......@@ -171,20 +171,20 @@ url_pop_init (url_t *purl, const char *name)
}
/* HOST:PORT */
index = strchr (++host_port, ':');
if (index == NULL)
indexe = strchr (++host_port, ':');
if (indexe == NULL)
{
url->host = strdup (host_port);
url->port = MU_POP_PORT;
}
else
{
long p = strtol(index + 1, NULL, 10);
url->host = malloc (index - host_port + 1);
long p = strtol(indexe + 1, NULL, 10);
url->host = malloc (indexe - host_port + 1);
if (url->host)
{
((char *)memcpy (url->host, host_port, index - host_port))
[index - host_port]='\0';
((char *)memcpy (url->host, host_port, indexe - host_port))
[indexe - host_port]='\0';
url->port = (p == 0) ? MU_POP_PORT : p;
}
}
......