Commit 663ecdf5 663ecdf5d565988e9ac28d43a31ba94f4943e084 by Alain Magloire

First draft of the mailbox/mbox2. Not functional.

	Changes are too numerous to enumarate.

	* mailbox2/mbox/*.[ch]: First mbox implementation.
	* mailbox2/mbox/Makefile.am: New file.
1 parent e2d51dc8
2001-09-23 Alain Magloire
First draft of the mailbox/mbox2. Not functional.
Changes are too numerous to enumarate.
* mailbox2/mbox/*.[ch]: First mbox implementation.
* mailbox2/mbox/Makefile.am: New file.
2001-09-22 Alain Magloire
In order to provide inheritance, it is necessary to make
......
......@@ -33,17 +33,17 @@ extern "C" {
struct _attribute;
typedef struct _attribute * attribute_t;
#define MU_ATTRIBUTE_ANSWERED 0x0001
#define MU_ATTRIBUTE_FLAGGED 0x0002
#define MU_ATTRIBUTE_DELETED 0x0004
#define MU_ATTRIBUTE_DRAFT 0x0008
#define MU_ATTRIBUTE_SEEN 0x0010
#define MU_ATTRIBUTE_READ 0x0020
#define MU_ATTRIBUTE_MODIFIED 0x1000
#define MU_ATTRIBUTE_RECENT 0x0000
#define MU_ATTRIBUTE_ANSWERED 0x00001
#define MU_ATTRIBUTE_FLAGGED 0x00002
#define MU_ATTRIBUTE_DELETED 0x00004
#define MU_ATTRIBUTE_DRAFT 0x00008
#define MU_ATTRIBUTE_SEEN 0x00010
#define MU_ATTRIBUTE_READ 0x00020
#define MU_ATTRIBUTE_MODIFIED 0x10000
#define MU_ATTRIBUTE_RECENT 0x00000
extern int attribute_ref __P ((attribute_t));
extern void attribute_destroy __P ((attribute_t *));
extern void attribute_destroy __P ((attribute_t *));
extern int attribute_is_seen __P ((attribute_t));
extern int attribute_is_answered __P ((attribute_t));
......
......@@ -19,7 +19,6 @@
#define _MAILUTILS_ERROR_H
#include <errno.h>
#include <stdarg.h>
#ifndef __P
#ifdef __STDC__
......@@ -33,7 +32,7 @@
extern "C" {
#endif
/* When Possible use the platform error for our needs. */
/* When Possible use the platform error for our needs. */
#ifndef MU_ERROR_RANGE
# define MU_ERROR_RANGE 600
#endif
......@@ -119,10 +118,30 @@ extern "C" {
# define MU_ERROR_OPERATION_CANCELED (MU_ERROR_RANGE + 13)
#endif
typedef int (*error_pfn_t) __P ((const char *fmt, va_list ap));
#if defined (EBADF)
# define MU_ERROR_FD_INVALID EBADF
#else
# define MU_ERROR_FD_INVALID (MU_ERROR_RANGE + 14)
#endif
#if defined (ENOENT)
# define MU_ERROR_ENTRY_NOT_EXIST ENOENT
#else
# define MU_ERROR_ENTRY_NOT_EXIST (MU_ERROR_RANGE + 15)
#endif
#if defined (EEXIST)
# define MU_ERROR_ENTRY_EXIST EEXIST
#else
# define MU_ERROR_ENTRY_EXIST (MU_ERROR_RANGE + 16)
#endif
#if defined (ENOTDIR)
# define MU_ERROR_NO_DIRECTORY ENOTDIR
#else
# define MU_ERROR_NO_DIRECTORY (MU_ERROR_RANGE + 17)
#endif
extern int mu_error __P ((const char *fmt, ...));
extern void mu_error_set_print __P ((error_pfn_t));
#ifdef __cplusplus
}
......
......@@ -37,8 +37,8 @@ struct _mbox;
typedef struct _mbox *mbox_t;
extern int mbox_create __P ((mbox_t *));
extern void mbox_destroy __P ((mbox_t *));
extern int mbox_create __P ((mbox_t *));
extern void mbox_destroy __P ((mbox_t *));
extern int mbox_get_uidvalidity __P ((mbox_t, unsigned long *));
extern int mbox_get_uidnext __P ((mbox_t, unsigned long *));
......@@ -62,31 +62,39 @@ extern int mbox_set_bstream __P ((mbox_t, unsigned int, stream_t));
extern int mbox_get_bsize __P ((mbox_t, unsigned int, unsigned int *));
extern int mbox_get_blines __P ((mbox_t, unsigned int, unsigned int *));
extern int mbox_get_size __P ((mbox_t, unsigned long *));
extern int mbox_get_size __P ((mbox_t, off_t *));
extern int mbox_save_attributes __P ((mbox_t));
extern int mbox_mark_deleted __P ((mbox_t, unsigned int));
extern int mbox_unmark_deleted __P ((mbox_t, unsigned int));
extern int mbox_expunge __P ((mbox_t));
extern int mbox_expunge __P ((mbox_t, int));
extern int mbox_changed_on_disk __P ((mbox_t));
extern int mbox_set_progress_cb __P ((mbox_t, int (*) __P ((int, void *)), void *));
extern int mbox_set_newmsg_cb __P ((mbox_t, int (*) __P ((int, void *)), void *));
extern int mbox_newmsg_cb __P ((mbox_t, int));
extern int mbox_progress_cb __P ((mbox_t, int));
extern int mbox_set_newmsg_cb __P ((mbox_t, int (*) __P ((int, void *)), void *));
extern int mbox_newmsg_cb __P ((mbox_t, int));
extern int mbox_progress_cb __P ((mbox_t, int));
extern int mbox_scan __P ((mbox_t, unsigned int, unsigned int *, int));
extern int mbox_messages_count __P ((mbox_t, unsigned int *));
extern int mbox_scan __P ((mbox_t, unsigned int, unsigned int *, int));
extern int mbox_messages_count __P ((mbox_t, unsigned int *));
extern int mbox_append __P ((mbox_t, const char *, stream_t));
extern int mbox_append_hb __P ((mbox_t, const char *, stream_t, stream_t));
extern int mbox_append __P ((mbox_t, const char *, stream_t));
extern int mbox_append_hb __P ((mbox_t, const char *, stream_t, stream_t));
extern int mbox_get_carrier __P ((mbox_t, stream_t *));
extern int mbox_set_carrier __P ((mbox_t, stream_t));
extern int mbox_get_carrier __P ((mbox_t, stream_t *));
extern int mbox_set_carrier __P ((mbox_t, stream_t));
extern int mbox_set_hcache __P ((mbox_t, char **, unsigned int));
extern int mbox_hcache_get_value __P ((mbox_t, const char *, char *, size_t,
size_t *));
extern int mbox_set_hcache __P ((mbox_t, const char **, size_t));
extern int mbox_set_hcache_default __P ((mbox_t));
extern void mbox_hcache_free __P ((mbox_t, unsigned int));
extern int mbox_hcache_append __P ((mbox_t, unsigned int, const char *,
const char *));
extern int mbox_header_get_value __P ((mbox_t, unsigned int, const char *,
char *, size_t, size_t *));
extern int stream_mbox_create __P ((stream_t *, mbox_t, unsigned int, int));
extern int attribute_mbox_create __P ((attribute_t *, mbox_t, unsigned int));
#ifdef __cplusplus
}
......
......@@ -23,7 +23,6 @@
#endif
#include <mailutils/attribute.h>
#include <mailutils/refcount.h>
#ifdef __cplusplus
extern "C" {
......@@ -53,14 +52,6 @@ struct _attribute
struct _attribute_vtable *vtable;
};
/* A simple default attribute implementation. */
struct _da
{
struct _attribute base;
mu_refcount_t refcount;
int flags;
};
#ifdef __cplusplus
}
#endif
......
......@@ -28,6 +28,12 @@ extern "C" {
typedef struct _mbox_message* mbox_message_t;
struct _hcache
{
char **values;
size_t size;
};
/* The umessages is an array of pointers that contains umessages_count of
mbox_message_t*; umessages[umessages_count]. We do it this way because
realloc() can move everything to a new memory region and invalidate all
......@@ -48,6 +54,8 @@ struct _mbox
size_t uidnext;
char *filename;
struct _hcache hcache;
/* The variables below are use to hold the state when appending messages. */
enum mbox_state
{
......@@ -70,11 +78,12 @@ struct _mbox_message
{
/* Offset of the messages in the mailbox. */
off_t from_;
char *separator;
/* Fast header retrieve, we save here the most common headers. This will
speed the header search. The entire headers are copied, when modified,
by the header_t object, we do not have to worry about updating them. */
char **fhdr;
struct _hcache hcache;
struct
{
......@@ -89,8 +98,6 @@ struct _mbox_message
unsigned long uid;
unsigned int attr_flags;
attribute_t attribute; /* The attr_flags contains the "Status:" attribute */
mbox_t mbox; /* Back pointer. */
};
#ifdef __cplusplus
......
# Use automake to process this file -*-Makefile-*-
AUTOMAKE_OPTIONS = ../lib/ansi2knr
#INCLUDES = -I${top_srcdir}/include
INCLUDES = -I${top_srcdir}/mailbox2/include
EXTRA_DIST = valid.h
lib_LTLIBRARIES = libmbox.la
libmbox_la_SOURCES = \
mbox_attribute.c \
mbox_blines.c \
mbox_bsize.c \
mbox_bstream.c \
mbox_carrier.c \
mbox_cb.c \
mbox_changed.c \
mbox_close.c \
mbox_create.c \
mbox_destroy.c \
mbox_expunge.c \
mbox_hcache.c \
mbox_hlines.c \
mbox_hsize.c \
mbox_hstream.c \
mbox_open.c \
mbox_scan.c \
mbox_separator.c \
mbox_size.c \
mbox_stream.c \
mbox_uid.c \
mbox_uidnext.c \
mbox_uidvalidity.c
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
/* Save the uidvalidity
- if it is an empty mbox in the first message append
- if for the first message the uidvalidity is not the same
from the mbox->uidvalidity.
- strip X-IMAPBASE, X-UID
- add X-UID base on mbox->uidnext.
- mangle any leading "From " in the body to ">From "
*/
int
mbox_append (mbox_t mbox, const char *sep, stream_t stream)
{
if (mbox == NULL || stream == NULL)
return MU_ERROR_INVALID_PARAMETER;
(void)sep;
return 0;
}
int
mbox_append_hb (mbox_t mbox, const char *sep, stream_t hstream,
stream_t bstream)
{
if (mbox == NULL || hstream == NULL || bstream == NULL)
return MU_ERROR_INVALID_PARAMETER;
(void)sep;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/refcount.h>
#include <mailutils/sys/attribute.h>
#include <mailutils/sys/mbox.h>
struct _attribute_mbox
{
struct _attribute base;
mu_refcount_t refcount;
mbox_t mbox;
unsigned int msgno;
};
static int
_attribute_mbox_ref (attribute_t attribute)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)attribute;
return mu_refcount_inc (ma->refcount);
}
static void
_attribute_mbox_destroy (attribute_t *pattribute)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)*pattribute;
if (mu_refcount_dec (ma->refcount) == 0)
{
if (ma->msgno <= ma->mbox->messages_count)
{
/* If it is the attribute save in the mailbox structure. */
if (ma == (struct _attribute_mbox *)(ma->mbox->umessages[ma->msgno - 1]->attribute))
ma->mbox->umessages[ma->msgno - 1]->attribute = NULL;
mu_refcount_destroy (&ma->refcount);
free (ma);
}
}
}
static int
_attribute_mbox_get_flags (attribute_t attribute, int *pflags)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)attribute;
if (pflags)
{
if (ma->msgno <= ma->mbox->messages_count)
{
*pflags = ma->mbox->umessages[ma->msgno - 1]->attr_flags;
}
}
return 0;
}
static int
_attribute_mbox_set_flags (attribute_t attribute, int flags)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)attribute;
if (ma->msgno <= ma->mbox->messages_count)
{
ma->mbox->umessages[ma->msgno - 1]->attr_flags |= (flags | MU_ATTRIBUTE_MODIFIED);
}
return 0;
}
static int
_attribute_mbox_unset_flags (attribute_t attribute, int flags)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)attribute;
if (ma->msgno <= ma->mbox->messages_count)
{
ma->mbox->umessages[ma->msgno - 1]->attr_flags &= ~flags;
/* If Modified was being unset do not reset it. */
if (!(flags & MU_ATTRIBUTE_MODIFIED))
ma->mbox->umessages[ma->msgno - 1]->attr_flags |= MU_ATTRIBUTE_MODIFIED;
}
return 0;
}
static int
_attribute_mbox_clear_flags (attribute_t attribute)
{
struct _attribute_mbox *ma = (struct _attribute_mbox *)attribute;
if (ma->msgno <= ma->mbox->messages_count)
{
ma->mbox->umessages[ma->msgno - 1]->attr_flags = 0;
}
return 0;
}
static struct _attribute_vtable _attribute_mbox_vtable =
{
_attribute_mbox_ref,
_attribute_mbox_destroy,
_attribute_mbox_get_flags,
_attribute_mbox_set_flags,
_attribute_mbox_unset_flags,
_attribute_mbox_clear_flags
};
static int
_attribute_mbox_ctor (struct _attribute_mbox *ma, mbox_t mbox,
unsigned int msgno)
{
mu_refcount_create (&(ma->refcount));
if (ma->refcount == NULL)
return MU_ERROR_NO_MEMORY;
ma->mbox = mbox;
ma->msgno = msgno;
ma->base.vtable = &_attribute_mbox_vtable;
return 0;
}
/*
static int
_attribute_mbox_dtor (struct _attribute_mbox *ma)
{
}
*/
int
attribute_mbox_create (attribute_t *pattribute, mbox_t mbox,
unsigned int msgno)
{
struct _attribute_mbox *ma;
int status;
attribute_t attribute;
char buf[128];
/* Get the attribute from the status field. */
*buf = '\0';
status = mbox_header_get_value (mbox, msgno, "Status", buf, sizeof buf, 0);
if (status != 0)
return status;
status = attribute_status_create (&attribute, buf);
if (status != 0)
return status;
attribute_get_flags (attribute, &mbox->umessages[msgno - 1]->attr_flags);
attribute_destroy (&attribute);
ma = calloc (1, sizeof *ma);
if (ma == NULL)
return MU_ERROR_NO_MEMORY;
status = _attribute_mbox_ctor (ma, mbox, msgno);
if (status != 0)
{
free (ma);
return status;
}
*pattribute = &ma->base;
return 0;
}
int
mbox_get_attribute (mbox_t mbox, unsigned int msgno, attribute_t *pattribute)
{
int status = 0;
if (mbox == NULL || msgno == 0 || pattribute == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->attribute)
{
attribute_ref (mbox->umessages[msgno]->attribute);
*pattribute = mbox->umessages[msgno]->attribute;
}
else
{
status = attribute_mbox_create (pattribute, mbox, msgno + 1);
if (status == 0)
mbox->umessages[msgno]->attribute = *pattribute;
}
return status;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_blines (mbox_t mbox, unsigned int msgno, unsigned int *plines)
{
unsigned int lines = 0;
if (mbox && msgno)
{
msgno--;
if (msgno >= mbox->messages_count)
lines = mbox->umessages[msgno]->body.lines;
}
if (plines)
*plines = lines;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_bsize (mbox_t mbox, unsigned int msgno, unsigned int *psize)
{
unsigned int size = 0;
if (mbox && msgno)
{
msgno--;
if (msgno >= mbox->messages_count)
size = mbox->umessages[msgno]->body.size;
}
if (psize)
*psize = size;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_bstream (mbox_t mbox, unsigned int msgno, stream_t *pstream)
{
int status = 0;
if (mbox == NULL || msgno == 0 || pstream == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->body.stream)
{
stream_ref (mbox->umessages[msgno]->body.stream);
*pstream = mbox->umessages[msgno]->body.stream;
}
else
{
status = stream_mbox_create (pstream, mbox, msgno + 1, 0);
if (status == 0)
mbox->umessages[msgno]->body.stream = *pstream;
}
return status;
}
int
mbox_set_bstream (mbox_t mbox, unsigned int msgno, stream_t stream)
{
if (mbox == NULL || msgno == 0)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->umessages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->body.stream)
stream_destroy (&mbox->umessages[msgno]->body.stream);
mbox->umessages[msgno]->body.stream = stream;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_set_carrier (mbox_t mbox, stream_t carrier)
{
if (mbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->carrier)
{
stream_close (mbox->carrier);
stream_destroy (&mbox->carrier);
}
mbox->carrier = carrier;
return 0;
}
int
mbox_get_carrier (mbox_t mbox, stream_t *pcarrier)
{
if (mbox == NULL || pcarrier == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->carrier == NULL)
{
int status = stream_file_create (&mbox->carrier);
if (status != 0)
return status;
}
*pcarrier = mbox->carrier;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_set_progress_cb (mbox_t mbox, int (*cb) __P ((int, void *)), void *arg)
{
if (mbox)
return MU_ERROR_INVALID_PARAMETER;
mbox->progress.cb = cb;
mbox->progress.arg = arg;
return 0;
}
int
mbox_set_newmsg_cb (mbox_t mbox, int (*cb) __P ((int, void *)), void *arg)
{
if (mbox)
return MU_ERROR_INVALID_PARAMETER;
mbox->newmsg.cb = cb;
mbox->newmsg.arg = arg;
return 0;
}
int
mbox_newmsg_cb (mbox_t mbox, int info)
{
if (mbox)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->newmsg.cb)
return mbox->newmsg.cb (info, mbox->newmsg.arg);
return 0;
}
int
mbox_progress_cb (mbox_t mbox, int info)
{
if (mbox)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->progress.cb)
return mbox->progress.cb (info, mbox->progress.arg);
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <sys/stat.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_changed_on_disk (mbox_t mbox)
{
int changed = 0;
/* Check if the mtime stamp changed, random modifications can give
us back the same size. */
if (mbox->carrier)
{
int fd = -1;
if (stream_get_fd (mbox->carrier, &fd) == 0)
{
struct stat statbuf;
if (fstat (fd, &statbuf) == 0)
{
if (difftime (mbox->mtime, statbuf.st_mtime) != 0)
changed = 1;
}
}
}
return changed;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_close (mbox_t mbox)
{
size_t i;
if (mbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
/* Make sure that we do not hold any file locking. */
if (mbox->lockfile)
{
lockfile_unlock (mbox->lockfile);
lockfile_destroy (&mbox->lockfile);
}
/* Before closing we need to remove all the umesages
- to reclaim the memory
- to prepare for another scan. */
for (i = 0; i < mbox->umessages_count; i++)
{
mbox_message_t mum = mbox->umessages[i];
/* Destroy the attach messages. */
if (mum)
{
mbox_hcache_free (mbox, i + 1);
if (mum->header.stream)
stream_destroy (&(mum->header.stream));
if (mum->body.stream)
stream_destroy (&(mum->body.stream));
if (mum->separator)
free (mum->separator);
if (mum->attribute)
attribute_destroy (&mum->attribute);
free (mum);
}
}
if (mbox->umessages)
free (mbox->umessages);
mbox->umessages = NULL;
mbox->umessages_count = 0;
mbox->size = 0;
mbox->mtime = 0;
mbox->uidvalidity = 0;
mbox->uidnext = 0;
if (mbox->filename)
free (mbox->filename);
mbox->filename = NULL;
return stream_close (mbox->carrier);
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_create (mbox_t *pmbox)
{
mbox_t mbox;
if (pmbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
/* Allocate specific mbox data. */
mbox = calloc (1, sizeof (*mbox));
if (mbox == NULL)
return MU_ERROR_NO_MEMORY;
*pmbox = mbox;
return 0; /* okdoke */
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/mbox.h>
void
mbox_destroy (mbox_t *pmbox)
{
if (pmbox && *pmbox)
{
mbox_t mbox = *pmbox;
mbox_close (mbox);
free (mbox);
}
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
static const char *hcache_default[] =
{
"Bcc",
"Cc",
"Content-Language",
"Content-Transfer-Encoding",
"Content-Type",
"Date",
"From",
"In-Reply-To",
"Message-ID",
"Reply-To",
"Reply-To",
"Sender",
"Subject",
"To",
"X-UIDL"
};
void
mbox_hcache_free (mbox_t mbox, unsigned int msgno)
{
struct _hcache *hc;
if (mbox == NULL)
return;
if (msgno)
hc = &mbox->umessages[msgno - 1]->hcache;
else
hc = &mbox->hcache;
if (hc->size)
{
unsigned int i;
for (i = 0; i < hc->size; i++)
{
if (hc->values[i])
free (hc->values[i]);
}
}
hc->size = 0;
if (hc->values)
free (hc->values);
hc->values = NULL;
}
int
mbox_set_hcache (mbox_t mbox, const char **array, size_t len)
{
if (mbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
mbox_hcache_free (mbox, 0);
if (array && len)
{
unsigned int i;
mbox->hcache.values = calloc (len, sizeof (*(mbox->hcache.values)));
if (mbox->hcache.values == NULL)
return MU_ERROR_NO_MEMORY;
for (i = 0; i < len; i++)
{
mbox->hcache.values[i] = strdup (array[i]);
if (mbox->hcache.values[i] == NULL)
{
mbox_set_hcache (mbox, NULL, 0);
return MU_ERROR_NO_MEMORY;
}
mbox->hcache.size++;
}
}
return 0;
}
int
mbox_hcache_append (mbox_t mbox, unsigned int msgno, const char *name,
const char *value)
{
struct _hcache *hc;
size_t i;
int status = MU_ERROR_ENTRY_NOT_EXIST;
if (mbox == NULL || msgno == 0 || name == NULL || *name == '\0')
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->hcache.size == 0)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
hc = &(mbox->umessages[msgno]->hcache);
if (hc->values == NULL)
{
hc->values = calloc (mbox->hcache.size, sizeof (*(mbox->hcache.values)));
if (hc->values == NULL)
return MU_ERROR_NO_MEMORY;
}
for (i = 0; i < mbox->hcache.size; i++)
{
if (strcasecmp (mbox->hcache.values[i], name) == 0)
{
if (value == NULL)
{
if (hc->values[i])
free (hc->values[i]);
hc->values[i] = NULL;
}
else
{
if (hc->values[i] == NULL)
hc->values[i] = strdup (value);
else
{
char *tmp = realloc (hc->values[i], strlen (value) +
strlen (hc->values[i]) + 1);
if (tmp)
{
strcat (hc->values[i], value);
hc->values[i] = tmp;
}
}
}
status = 0;
break;
}
}
return status;
}
int
mbox_set_hcache_default (mbox_t mbox)
{
return mbox_set_hcache (mbox, hcache_default,
sizeof (hcache_default) / sizeof (*hcache_default));
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_hlines (mbox_t mbox, unsigned int msgno, unsigned int *plines)
{
unsigned int lines = 0;
if (mbox && msgno)
{
msgno--;
if (msgno >= mbox->messages_count)
lines = mbox->umessages[msgno]->header.lines;
}
if (plines)
*plines = lines;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_hsize (mbox_t mbox, unsigned int msgno, unsigned int *psize)
{
unsigned int size = 0;
if (mbox && msgno)
{
msgno--;
if (msgno >= mbox->messages_count)
size = mbox->umessages[msgno]->header.size;
}
if (psize)
*psize = size;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_hstream (mbox_t mbox, unsigned int msgno, stream_t *pstream)
{
int status = 0;
if (mbox == NULL || msgno == 0 || pstream == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->header.stream)
{
stream_ref (mbox->umessages[msgno]->header.stream);
*pstream = mbox->umessages[msgno]->header.stream;
}
else
{
status = stream_mbox_create (pstream, mbox, msgno + 1, 1);
if (status == 0)
mbox->umessages[msgno]->header.stream = *pstream;
}
return status;
}
int
mbox_set_hstream (mbox_t mbox, unsigned int msgno, stream_t stream)
{
if (mbox == NULL || msgno == 0)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->umessages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->header.stream)
stream_destroy (&mbox->umessages[msgno]->header.stream);
mbox->umessages[msgno]->header.stream = stream;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_open (mbox_t mbox, const char *filename, int flags)
{
int status = 0;
char from[12];
size_t n = 0;
if (mbox == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (mbox->carrier == NULL)
{
stream_t carrier;
status = mbox_get_carrier (mbox, &carrier);
if (status != 0)
return status;
}
else
mbox_close (mbox);
status = stream_open (mbox->carrier, filename, 0, flags);
if (status != 0)
return status;
/* We need to be able to seek on the stream. */
status = stream_seek (mbox->carrier, 0, MU_STREAM_WHENCE_SET);
if (status != 0)
return status;
/* Check if it is indeed a mbox format. */
stream_readline (mbox->carrier, from, sizeof from, &n);
if (status != 0)
return status;
/* Empty file is Ok. */
if (n)
{
status = strncmp (from, "From ", 5);
if (status != 0)
return MU_ERROR_NOT_SUPPORTED;
}
/* Give an appropriate way to file lock. */
/* FIXME: use dotlock external program: we may not be setgid. */
status = lockfile_dotlock_create (&(mbox->lockfile), filename);
mbox->filename = strdup (filename);
return status;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_separator (mbox_t mbox, unsigned int msgno, char **psep)
{
if (mbox == NULL || msgno == 0 || psep == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->separator)
{
*psep = strdup (mbox->umessages[msgno]->separator);
}
else
{
char *p = NULL;
int len = 0 ;
size_t n = 0;
len = 2;
p = malloc (len);
if (p == NULL)
return MU_ERROR_NO_MEMORY;
*p = '\0';
do
{
char *s;
stream_seek (mbox->carrier, mbox->umessages[msgno]->from_,
MU_STREAM_WHENCE_SET);
len += 128;
s = realloc (p, len);
if (s == NULL)
{
free (p);
return MU_ERROR_NO_MEMORY;
}
p = s ;
stream_readline (mbox->carrier, p + strlen (p), len, &n);
} while (n && p[n - 1] == '\n');
*psep = strdup (p);
free (p);
}
return 0;
}
int
mbox_set_separator (mbox_t mbox, unsigned int msgno, const char *sep)
{
if (mbox == NULL || msgno == 0)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->umessages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
if (mbox->umessages[msgno]->separator)
free (mbox->umessages[msgno]->separator);
mbox->umessages[msgno]->separator = (sep) ? strdup (sep) : NULL;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_size (mbox_t mbox, off_t *size)
{
if (mbox && size)
*size = mbox->size;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <mailutils/error.h>
#include <mailutils/refcount.h>
#include <mailutils/sys/stream.h>
#include <mailutils/sys/mbox.h>
#undef min
#define min(a,b) ((a) < (b) ? (a) : (b))
struct _stream_mbox
{
struct _stream base;
mu_refcount_t refcount;
mbox_t mbox;
unsigned int msgno;
int is_header;
off_t offset;
};
static int
_stream_mbox_ref (stream_t stream)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
return mu_refcount_inc (ms->refcount);
}
static void
_stream_mbox_destroy (stream_t *pstream)
{
struct _stream_mbox *ms = (struct _stream_mbox *)*pstream;
if (mu_refcount_dec (ms->refcount) == 0)
{
if (ms->msgno <= ms->mbox->messages_count)
{
mu_refcount_destroy (&ms->refcount);
if (ms->is_header)
{
if (ms == (struct _stream_mbox *)ms->mbox->umessages[ms->msgno - 1]->header.stream)
ms->mbox->umessages[ms->msgno - 1]->header.stream = NULL;
}
else
{
if (ms == (struct _stream_mbox *)ms->mbox->umessages[ms->msgno - 1]->body.stream)
ms->mbox->umessages[ms->msgno - 1]->body.stream = NULL;
}
free (ms);
}
}
}
static int
_stream_mbox_open (stream_t stream, const char *name, int port, int flags)
{
(void)stream; (void)name; (void)port; (void)flags;
return 0;
}
static int
_stream_mbox_close (stream_t stream)
{
(void)stream;
return 0;
}
static int
_stream_mbox_read (stream_t stream, void *buf, size_t buflen, size_t *pnread)
{
int status = 0;
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
size_t nread = 0;
if (buf && buflen)
{
if (ms->msgno <= ms->mbox->messages_count)
{
off_t ln;
mbox_message_t umessage = ms->mbox->umessages[ms->msgno - 1];
if (ms->is_header)
ln = umessage->header.end - (umessage->header.start + ms->offset);
else
ln = umessage->body.end - (umessage->body.start + ms->offset);
if (ln > 0)
{
size_t n = min ((size_t)ln, buflen);
/* Position the file pointer. */
status = stream_seek (ms->mbox->carrier, ms->offset,
MU_STREAM_WHENCE_SET);
if (status == 0)
{
status = stream_read (ms->mbox->carrier, buf, n, &nread);
ms->offset += nread;
}
}
}
}
if (pnread)
*pnread = nread;
return status;
}
/*
* Read at most n-1 characters.
* Stop when a newline has been read, or the count runs out.
*/
static int
_stream_mbox_readline (stream_t stream, char *buf, size_t buflen,
size_t *pnread)
{
int status = 0;
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
size_t nread = 0;
if (buf && buflen)
{
if (ms->msgno <= ms->mbox->messages_count)
{
off_t ln;
mbox_message_t umessage = ms->mbox->umessages[ms->msgno - 1];
if (ms->is_header)
ln = umessage->header.end - (umessage->header.start + ms->offset);
else
ln = umessage->body.end - (umessage->body.start + ms->offset);
if (ln > 0)
{
size_t n = min ((size_t)ln, buflen);
/* Position the stream. */
status = stream_seek (ms->mbox->carrier, ms->offset,
MU_STREAM_WHENCE_SET);
if (status == 0)
{
status = stream_readline (ms->mbox->carrier, buf, n, &nread);
ms->offset += nread;
}
}
}
}
if (pnread)
*pnread = nread;
return status;
}
static int
_stream_mbox_write (stream_t stream, const void *buf, size_t count,
size_t *pnwrite)
{
(void)stream; (void)buf; (void)count; (void)pnwrite;
return MU_ERROR_IO;
}
static int
_stream_mbox_get_fd (stream_t stream, int *pfd)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
return stream_get_fd (ms->mbox->carrier, pfd);
}
static int
_stream_mbox_get_flags (stream_t stream, int *pfl)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
return stream_get_flags (ms->mbox->carrier, pfl);
}
static int
_stream_mbox_get_size (stream_t stream, off_t *psize)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
if (psize)
{
if (ms->msgno <= ms->mbox->messages_count)
{
if (ms->is_header)
*psize = ms->mbox->umessages[ms->msgno - 1]->header.end
- ms->mbox->umessages[ms->msgno - 1]->header.start;
else
*psize = ms->mbox->umessages[ms->msgno - 1]->body.end
- ms->mbox->umessages[ms->msgno - 1]->body.start;
}
}
return 0;
}
static int
_stream_mbox_truncate (stream_t stream, off_t len)
{
(void)stream; (void)len;
return MU_ERROR_IO;
}
static int
_stream_mbox_flush (stream_t stream)
{
(void)stream;
return 0;
}
static int
_stream_mbox_get_state (stream_t stream, enum stream_state *pstate)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
return stream_get_state (ms->mbox->carrier, pstate);
}
static int
_stream_mbox_seek (stream_t stream, off_t off, enum stream_whence whence)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
off_t noff = ms->offset;
int err = 0;
if (whence == MU_STREAM_WHENCE_SET)
noff = off;
else if (whence == MU_STREAM_WHENCE_CUR)
noff += off;
else if (whence == MU_STREAM_WHENCE_END)
{
off_t size = 0;
_stream_mbox_get_size (stream, &size);
noff = size + off;
}
else
noff = -1; /* error. */
if (noff >= 0)
ms->offset = noff;
else
err = MU_ERROR_INVALID_PARAMETER;
return err;
}
static int
_stream_mbox_tell (stream_t stream, off_t *off)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
if (off == NULL)
return MU_ERROR_INVALID_PARAMETER;
*off = ms->offset;
return 0;
}
static int
_stream_mbox_is_readready (stream_t stream, int timeout)
{
(void)timeout;
return stream_is_open (stream);
}
static int
_stream_mbox_is_writeready (stream_t stream, int timeout)
{
(void)stream; (void)timeout;
return 0;
}
static int
_stream_mbox_is_exceptionpending (stream_t stream, int timeout)
{
(void)stream; (void)timeout;
return 0;
}
static int
_stream_mbox_is_open (stream_t stream)
{
struct _stream_mbox *ms = (struct _stream_mbox *)stream;
return stream_is_open (ms->mbox->carrier);
}
static struct _stream_vtable _stream_mbox_vtable =
{
_stream_mbox_ref,
_stream_mbox_destroy,
_stream_mbox_open,
_stream_mbox_close,
_stream_mbox_read,
_stream_mbox_readline,
_stream_mbox_write,
_stream_mbox_seek,
_stream_mbox_tell,
_stream_mbox_get_size,
_stream_mbox_truncate,
_stream_mbox_flush,
_stream_mbox_get_fd,
_stream_mbox_get_flags,
_stream_mbox_get_state,
_stream_mbox_is_readready,
_stream_mbox_is_writeready,
_stream_mbox_is_exceptionpending,
_stream_mbox_is_open
};
static int
_stream_mbox_ctor (struct _stream_mbox *ms, mbox_t mbox, unsigned int msgno,
int is_header)
{
mu_refcount_create (&(ms->refcount));
if (ms->refcount == NULL)
return MU_ERROR_NO_MEMORY;
ms->mbox = mbox;
ms->msgno = msgno;
ms->offset = 0;
ms->is_header = is_header;
ms->base.vtable = &_stream_mbox_vtable;
return 0;
}
/*
static void
_stream_mbox_dtor (struct _stream_mbox *ms)
{
}
*/
int
stream_mbox_create (stream_t *pstream, mbox_t mbox, unsigned int msgno,
int is_header)
{
struct _stream_mbox *ms;
int status;
if (pstream == NULL || mbox == NULL || msgno == 0)
return MU_ERROR_INVALID_PARAMETER;
ms = calloc (1, sizeof *ms);
if (ms == NULL)
return MU_ERROR_NO_MEMORY;
status = _stream_mbox_ctor (ms, mbox, msgno, is_header);
if (status != 0)
return status;
*pstream = &ms->base;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_uid (mbox_t mbox, unsigned int msgno, unsigned long *puid)
{
if (mbox == NULL || msgno == 0 || puid == NULL)
return MU_ERROR_INVALID_PARAMETER;
if (msgno > mbox->messages_count)
return MU_ERROR_INVALID_PARAMETER;
msgno--;
*puid = mbox->umessages[msgno]->uid;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_uidnext (mbox_t mbox, unsigned long *puidnext)
{
if (mbox == NULL || puidnext == NULL)
return MU_ERROR_INVALID_PARAMETER;
/* If we did not start a scanning yet do it now or
the mailbox as change on disk. */
if (mbox->messages_count == 0)
{
int status = mbox_scan (mbox, 0, NULL, 0);
if (status != 0)
return status;
}
*puidnext = mbox->uidnext;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <mailutils/error.h>
#include <mailutils/sys/mbox.h>
int
mbox_get_uidvalidity (mbox_t mbox, unsigned long *puidvalidity)
{
if (mbox == NULL || puidvalidity == NULL)
return MU_ERROR_INVALID_PARAMETER;
/* If we did not start a scanning yet do it now. */
if (mbox->messages_count == 0)
{
int status = mbox_scan (mbox, 0, NULL, 0);
if (status != 0)
return status;
}
*puidvalidity = mbox->uidvalidity;
return 0;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Library Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* Credits to the c-client and its Authors
* The notorius c-client VALID() macro, was written by Mark Crispin.
*/
/* From the C-Client, part of pine */
/* You are not expected to understand this macro, but read the next page if
* you are not faint of heart.
*
* Known formats to the VALID macro are:
* From user Wed Dec 2 05:53 1992
* BSD From user Wed Dec 2 05:53:22 1992
* SysV From user Wed Dec 2 05:53 PST 1992
* rn From user Wed Dec 2 05:53:22 PST 1992
* From user Wed Dec 2 05:53 -0700 1992
* From user Wed Dec 2 05:53:22 -0700 1992
* From user Wed Dec 2 05:53 1992 PST
* From user Wed Dec 2 05:53:22 1992 PST
* From user Wed Dec 2 05:53 1992 -0700
* Solaris From user Wed Dec 2 05:53:22 1992 -0700
*
* Plus all of the above with `` remote from xxx'' after it. Thank you very
* much, smail and Solaris, for making my life considerably more complicated.
*/
/*
* What? You want to understand the VALID macro anyway? Alright, since you
* insist. Actually, it isn't really all that difficult, provided that you
* take it step by step.
*
* Line 1 Initializes the return ti value to failure (0);
* Lines 2-3 Validates that the 1st-5th characters are ``From ''.
* Lines 4-6 Validates that there is an end of line and points x at it.
* Lines 7-14 First checks to see if the line is at least 41 characters long
.
* If so, it scans backwards to find the rightmost space. From
* that point, it scans backwards to see if the string matches
* `` remote from''. If so, it sets x to point to the space at
* the start of the string.
* Line 15 Makes sure that there are at least 27 characters in the line.
* Lines 16-21 Checks if the date/time ends with the year (there is a space
* five characters back). If there is a colon three characters
* further back, there is no timezone field, so zn is set to 0
* and ti is set in front of the year. Otherwise, there must
* either to be a space four characters back for a three-letter
* timezone, or a space six characters back followed by a + or -
* for a numeric timezone; in either case, zn and ti become the
* offset of the space immediately before it.
* Lines 22-24 Are the failure case for line 14. If there is a space four
* characters back, it is a three-letter timezone; there must be
a
* space for the year nine characters back. zn is the zone
* offset; ti is the offset of the space.
* Lines 25-28 Are the failure case for line 20. If there is a space six
* characters back, it is a numeric timezone; there must be a
* space eleven characters back and a + or - five characters back
.
* zn is the zone offset; ti is the offset of the space.
* Line 29-32 If ti is valid, make sure that the string before ti is of the
* form www mmm dd hh:mm or www mmm dd hh:mm:ss, otherwise
* invalidate ti. There must be a colon three characters back
* and a space six or nine characters back (depending upon
* whether or not the character six characters back is a colon).
* There must be a space three characters further back (in front
* of the day), one seven characters back (in front of the month)
,
* and one eleven characters back (in front of the day of week).
* ti is set to be the offset of the space before the time.
*
* Why a macro? It gets invoked a *lot* in a tight loop. On some of the
* newer pipelined machines it is faster being open-coded than it would be if
* subroutines are called.
*
* Why does it scan backwards from the end of the line, instead of doing the
* much easier forward scan? There is no deterministic way to parse the
* ``user'' field, because it may contain unquoted spaces! Yes, I tested it t
o
* see if unquoted spaces were possible. They are, and I've encountered enoug
h
* evil mail to be totally unwilling to trust that ``it will never happen''.
*/
#define VALID(s,x,ti,zn) { \
ti = 0; \
if ((*s == 'F') && (s[1] == 'r') && (s[2] == 'o') && (s[3] == 'm') && \
(s[4] == ' ')) { \
for (x = s + 5; *x && *x != '\n'; x++); \
if (x) { \
if (x - s >= 41) { \
for (zn = -1; x[zn] != ' '; zn--); \
if ((x[zn-1] == 'm') && (x[zn-2] == 'o') && (x[zn-3] == 'r') && \
(x[zn-4] == 'f') && (x[zn-5] == ' ') && (x[zn-6] == 'e') && \
(x[zn-7] == 't') && (x[zn-8] == 'o') && (x[zn-9] == 'm') && \
(x[zn-10] == 'e') && (x[zn-11] == 'r') && (x[zn-12] == ' '))\
x += zn - 12; \
} \
if (x - s >= 27) { \
if (x[-5] == ' ') { \
if (x[-8] == ':') zn = 0,ti = -5; \
else if (x[-9] == ' ') ti = zn = -9; \
else if ((x[-11] == ' ') && ((x[-10]=='+') || (x[-10]=='-'))) \
ti = zn = -11; \
} \
else if (x[-4] == ' ') { \
if (x[-9] == ' ') zn = -4,ti = -9; \
} \
else if (x[-6] == ' ') { \
if ((x[-11] == ' ') && ((x[-5] == '+') || (x[-5] == '-'))) \
zn = -6,ti = -11; \
} \
if (ti && !((x[ti - 3] == ':') && \
(x[ti -= ((x[ti - 6] == ':') ? 9 : 6)] == ' ') && \
(x[ti - 3] == ' ') && (x[ti - 7] == ' ') && \
(x[ti - 11] == ' '))) ti = 0; \
} \
} \
} \
}