Commit 06e9cb84 06e9cb844ccf3aefea60b1eb04019ff0d86f63fb by Alain Magloire

mailbox.c mbx_mbox.c mbx_mbox.h mbx_mdir.h mbx_unix.c

 	mbx_unix.h
 	mbx_mdir.c mbx_mmdf.c mbx_mmdf.h

slowly continuing work.
1 parent e516428e
......@@ -20,6 +20,9 @@
#endif
#include <mbx_mbox.h>
#include <mbx_unix.h>
#include <mbx_mdir.h>
#include <mbx_mmdf.h>
#include <mbx_pop.h>
#include <mbx_imap.h>
......@@ -88,10 +91,13 @@ static struct mailbox_builtin
int is_malloc;
struct mailbox_builtin * next;
} mailbox_builtin [] = {
{ NULL, 0, &mailbox_builtin[1] }, /* sentinel, head list */
{ &_mailbox_mbox_type, 0, &mailbox_builtin[2] },
{ &_mailbox_pop_type, 0, &mailbox_builtin[3] },
{ &_mailbox_imap_type, 0, &mailbox_builtin[0] },
{ NULL, 0, &mailbox_builtin[1] }, /* sentinel, head list */
{ &_mailbox_mbox_type, 0, &mailbox_builtin[2] },
{ &_mailbox_unix_type, 0, &mailbox_builtin[3] },
{ &_mailbox_maildir_type, 0, &mailbox_builtin[4] },
{ &_mailbox_mmdf_type, 0, &mailbox_builtin[5] },
{ &_mailbox_pop_type, 0, &mailbox_builtin[6] },
{ &_mailbox_imap_type, 0, &mailbox_builtin[0] },
};
int
......@@ -160,7 +166,8 @@ mailbox_list_mtype (struct mailbox_type **mlist, int *n)
struct mailbox_type *mtype;
int i;
if ((i = mailbox_list_type (NULL, 0)) <= 0 || (mtype = malloc (i)) == NULL)
if ((i = mailbox_list_type (NULL, 0)) <= 0
|| (mtype = calloc (i, sizeof (*mtype))) == NULL)
{
return -1;
}
......@@ -224,7 +231,7 @@ mailbox_init (mailbox_t *mbox, const char *name, int id)
}
}
/* 3nd run: nothing yet ?? try unixmbox/maildir directly as the last resort.
/* 3nd run: nothing yet ?? try mbox directly as the last resort.
this should take care of the case where the filename is use */
if (status != 0 )
{
......@@ -372,7 +379,7 @@ mbx_get_mpasswd (mailbox_t mbox, char **passwd, int *len)
int i;
char *p;
if ((i = mbox->_get_passwd (mbox, NULL, 0, 0)) <= 0
|| (p = malloc (i)) == NULL)
|| (p = calloc (i, sizeof (*p))) == NULL)
{
return -1;
}
......@@ -469,7 +476,7 @@ mbx_get_mbody (mailbox_t mbox, int id, char **body, int *len)
int i;
char *b;
if ((i = mbox->_get_body (mbox, id, NULL, 0, 0)) <= 0
|| (b = malloc (i)) == NULL)
|| (b = calloc (i, sizeof (*b))) == NULL)
{
return -1;
}
......@@ -490,7 +497,7 @@ mbx_get_mheader (mailbox_t mbox, int id, char **header, int *len)
int i;
char *h;
if ((i = mbox->_get_header (mbox, id, NULL, 0, 0)) <= 0
|| (h = malloc (i)) == NULL)
|| (h = calloc (i, sizeof (*h))) == NULL)
{
return -1;
}
......
......@@ -25,29 +25,101 @@
#include <mbx_unix.h>
#include <mbx_mdir.h>
#include <errno.h>
#include <sys/stat.h>
struct mailbox_type _mailbox_mbox_type =
{
"UNIX MBOX",
"UNIX_MBOX/Maildir/MMDF",
(int)&_url_mbox_type, &_url_mbox_type,
mailbox_mbox_init, mailbox_mbox_destroy
};
/*
There is no specific URL for file mailbox, until we
come up with a url for each like :
maildir://
mmdf://
ubox://
they all share the same url which is
file://<path_name> */
if there is no specific URL for file mailbox,
file://<path_name>
It would be preferrable to use :
maildir://<path>
unix://<path>
mmdf://<path>
This would eliminate heuristic discovery that would turn
out to be wrong. Caveat, there is no std URL for those
mailbox.
*/
int
mailbox_mbox_init (mailbox_t *mbox, const char *name)
{
struct stat st;
/*
If they want to creat ?? should they know the type ???
What is the best course of action ??
*/
if (stat (name, &st) == -1)
{
return -1; /* errno set by stat () */
}
if (S_ISREG (st.st_mode))
{
/*
FIXME: We should do an open() and try
to do a better reconnaissance of the type,
maybe MMDF. For now assume Unix MBox */
#if 0
char head[6];
ssize_t cout;
int fd;
fd = open (name, O_RDONLY);
if (fd == -1)
{
/* Oops !! wrong permission ? file deleted ? */
return -1; /* errno set by open () */
}
/* Read a small chunck */
count = read (fd, head, sizeof(head));
/* Try Unix Mbox */
/* FIXME:
What happen if the file is empty ???
Do we default to Unix ??
*/
if (count == 0) /*empty file*/
{
return mailbox_unix_init (mbox, name);
}
if (count >= 5)
{
if (strncmp (head, "From ", 5) == 0)
{
/* This is Unix Mbox */
return mailbox_unix_init (mbox, name);
}
}
/* Try MMDF */
#endif
return mailbox_unix_init (mbox, name);
}
else if (S_ISDIR (st.st_mode))
{
/* Is that true ? Are all directories Maildir ?? */
return mailbox_maildir_init (mbox, name);
}
/* Why can't a mailbox be FIFO ? or a DOOR/Portal ? */
errno = EINVAL;
return -1;
}
void
mailbox_mbox_destroy (mailbox_t *mbox)
{
return ;
return (*mbox)->mtype->_destroy (mbox);
}
......
......@@ -15,16 +15,14 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _MBX_UNIX_H
#define _MBX_UNIX_H 1
#ifndef _MBX_MBOX_H
#define _MBX_MBOX_H 1
#include <mailbox.h>
#include <stdlib.h>
#include <stdio.h>
extern int mailbox_mbox_init __P ((mailbox_t *mbox, const char *name));
extern void mailbox_mbox_destroy __P ((mailbox_t *mbox));
extern struct mailbox_type _mailbox_mbox_type;
#endif /* _MBX_UNIX_H */
#endif /* _MBX_MBOX_H */
......
#include <url_mdir.h>
#include <mbx_mdir.h>
struct mailbox_type _mailbox_maildir_type =
{
"MAILDIR",
(int)&_url_maildir_type, &_url_maildir_type,
mailbox_maildir_init, mailbox_maildir_destroy
};
int
mailbox_maildir_init (mailbox_t *mbox, const char *name)
{
return -1;
}
void
mailbox_maildir_destroy (mailbox_t *mbox)
{
return;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General 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. */
#ifndef _MBX_MDIR_H
#define _MBX_MDIR_H 1
#include <mailbox.h>
extern int mailbox_maildir_init __P ((mailbox_t *mbox, const char *name));
extern void mailbox_maildir_destroy __P ((mailbox_t *mbox));
extern struct mailbox_type _mailbox_maildir_type;
#endif /* _MBX_MDIR_H */
......
#include <url_mmdf.h>
#include <mbx_mmdf.h>
struct mailbox_type _mailbox_maildir_type =
{
"MMDF",
(int)&_url_mmdf_type, &_url_mmdf_type,
mailbox_mmdf_init, mailbox_mmdf_destroy
};
int
mailbox_mmdf_init (mailbox_t *mbox, const char *name)
{
return -1;
}
void
mailbox_mmdf_destroy (mailbox_t *mbox)
{
return;
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General 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. */
#ifndef _MBX_MMDF_H
#define _MBX_MMDF_H 1
#include <mailbox.h>
extern int mailbox_mmdf_init __P ((mailbox_t *mbox, const char *name));
extern void mailbox_mmdf_destroy __P ((mailbox_t *mbox));
extern struct mailbox_type _mailbox_mmdf_type;
#endif /* _MBX_MMDF_H */
#include <url_unix.h>
#include <mbx_unix.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
struct mailbox_type _mailbox_unix_type =
{
"UNIX MBOX",
(int)&_url_unix_type, &_url_unix_type,
mailbox_unix_init, mailbox_unix_destroy
};
typedef struct _mailbox_unix_msg
{
off_t header;
off_t body;
off_t end;
int deleted;
} mailbox_unix_msg_t;
typedef struct _mailbox_unix_data
{
mailbox_unix_msg_t *messages;
FILE *file;
mailbox_lock_t lockmode;
time_t last_mode_time;
} mailbox_unix_data_t;
/* forward prototypes */
static int mailbox_unix_open (mailbox_t *mbox, const char *name);
static int mailbox_unix_close (mailbox_t *mbox);
static int mailbox_unix_get_name (mailbox_t, int *id, char *name,
int offset, int len);
static int mailbox_unix_get_mname (mailbox_t, int *id, char **name, int *len);
/* passwd */
static int mailbox_unix_get_passwd (mailbox_t, char *passwd,
int offset, int len);
static int mailbox_unix_get_mpasswd (mailbox_t, char **passwd, int *len);
static int mailbox_unix_set_passwd (mailbox_t, const char *passwd,
int offset, int len);
/* deleting */
static int mailbox_unix_delete (mailbox_t, int);
static int mailbox_unix_undelete (mailbox_t, int);
static int mailbox_unix_expunge (mailbox_t);
static int mailbox_unix_is_deleted (mailbox_t, int);
/* appending */
static int mailbox_unix_new_msg (mailbox_t, int * id);
static int mailbox_unix_set_header (mailbox_t, int id, const char *h,
int offset, int n, int replace);
static int mailbox_unix_set_body (mailbox_t, int id, const char *b,
int offset, int n, int replace);
static int mailbox_unix_append (mailbox_t, int id);
static int mailbox_unix_destroy_msg (mailbox_t, int id);
/* reading */
static int mailbox_unix_get_body (mailbox_t, int id, char *b,
int offset, int n);
static int mailbox_unix_get_mbody (mailbox_t, int id, char **b,
int *n);
static int mailbox_unix_get_header (mailbox_t, int id, char *h,
int offset, int n);
static int mailbox_unix_get_mheader (mailbox_t, int id, char **h,
int *n);
/* locking */
static int mailbox_unix_lock (mailbox_t, int flag);
static int mailbox_unix_unlock (mailbox_t);
/* miscellany */
static int mailbox_unix_scan (mailbox_t, int *msgs);
static int mailbox_unix_is_updated (mailbox_t);
static int mailbox_unix_get_timeout (mailbox_t, int *timeout);
static int mailbox_unix_set_timeout (mailbox_t, int timeout);
static int mailbox_unix_get_refresh (mailbox_t, int *refresh);
static int mailbox_unix_set_refresh (mailbox_t, int refresh);
static int mailbox_unix_get_size (mailbox_t, int id, size_t *size);
static int mailbox_unix_set_notification (mailbox_t,
int (*func) (mailbox_t, void *arg));
int
mailbox_unix_init (mailbox_t *pmbox, const char *name)
{
mailbox_t mbox;
/*
Again should we do an open() and check if indeed this is a
correct mbox ? I think not, this should be delay to _open()
*/
mbox = calloc (1, sizeof (*mbox));
if (mbox == NULL)
return -1; /* errno set by calloc() */
/* set the type */
mbox->mtype = &_mailbox_unix_type;
mbox->_open = mailbox_unix_open;
mbox->_close = mailbox_unix_close;
mbox->_get_name = mailbox_unix_get_name;
mbox->_get_mname = mailbox_unix_get_mname;
/* passwd */
mbox->_get_passwd = mailbox_unix_get_passwd;
mbox->_get_mpasswd = mailbox_unix_get_mpasswd;
mbox->_set_passwd = mailbox_unix_set_passwd;
/* deleting */
mbox->_delete = mailbox_unix_delete;
mbox->_undelete = mailbox_unix_undelete;
mbox->_expunge = mailbox_unix_expunge;
mbox->_is_deleted = mailbox_unix_is_deleted;
/* appending */
mbox->_new_msg = mailbox_unix_new_msg;
mbox->_set_header = mailbox_unix_set_header;
mbox->_set_body = mailbox_unix_set_body;
mbox->_append = mailbox_unix_append;
mbox->_destry_msg = mailbox_unix_destroy_msg;
/* reading */
mbox->_get_body = mailbox_unix_get_body;
mbox->_get_mbody = mailbox_unix_get_mbody;
mbox->_get_header = mailbox_unix_get_header;
mbox->_get_mheader = mailbox_unix_get_mheader;
/* locking */
mbox->_lock = mailbox_unix_lock;
mbox->_unlock = mailbox_unix_unlock;
/* miscellany */
mbox->_scan = mailbox_unix_scan;
mbox->_is_updated = mailbox_unix_is_updated;
mbox->_get_timeout = mailbox_unix_get_timeout;
mbox->_set_timeout = mailbox_unix_set_timeout;
mbox->_get_refresh = mailbox_unix_get_refresh;
mbox->_set_refresh = mailbox_unix_set_refresh;
mbox->_get_size = mailbox_unix_get_size;
mbox->_set_notification = mailbox_unix_set_notification;
return 0;
}
void
mailbox_unix_destroy (mailbox_t *mbox)
{
return;
}
/* start of Mbox Implementation */
static int mailbox_unix_open (mailbox_t mbox, int flags)
{
}
......
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General 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. */
#ifndef _MBX_UNIX_H
#define _MBX_UNIX_H 1
#include <mailbox.h>
extern int mailbox_unix_init __P ((mailbox_t *mbox, const char *name));
extern void mailbox_unix_destroy __P ((mailbox_t *mbox));
extern struct mailbox_type _mailbox_unix_type;
#endif /* _MBX_UNIX_H */
......