Commit 100fe99c 100fe99c3cc661d2a2915f0125e1496f2c944a7c by Jakob Kaivo

Added locking function and interface

Added mbox_header_line
Added mbox_body_lines
1 parent 3dea9405
/* copyright and license info go here */
/* 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mailbox.h"
#include "unixmbox.h"
......@@ -40,6 +55,7 @@ mbox_open (const char *name)
mbox->_expunge = _mbox_dummy1;
mbox->_add_message = _mbox_dummy3;
mbox->_is_deleted = _mbox_dummy2;
mbox->_lock = _mbox_dummy2;
mbox->_get_body = _mbox_dummy4;
mbox->_get_header = _mbox_dummy4;
......@@ -68,6 +84,97 @@ mbox_open (const char *name)
}
/*
* Gets the contents of a header field
*/
char *
mbox_header_line (mailbox *mbox, int num, const char *header)
{
char *full, *tmp, *line;
int i = 0, j=0, try = 1, len;
int lh = strlen (header);
full = mbox_get_header (mbox, num);
if (full == NULL)
return NULL;
len = strlen (full);
/* First get the appropriate line at the beginning */
for (i=0; i < len-(lh+2); i++)
{
if (try == 1)
{
if (!strncasecmp(&full[i], header, lh) && full[i+lh] == ':')
{
full[len-i] = '\0';
tmp = strdup (&full[i+lh+2]);
i = len;
}
else
try = 0;
}
else if (full[i] == '\n')
try = 1;
else
try = 0;
}
/* Now trim the fat */
len = strlen (tmp);
for (i = 0; i < len; i++)
{
if (tmp[i] == '\n' && i < (len - 1) && (tmp[i+1] == ' ' || tmp[i+1] == '\t'))
{
if (tmp[i+1] == '\t')
tmp[i + 1] = ' ';
for (j = i; j < len; j++)
tmp[j] = tmp[j+1];
}
else if (tmp[i] == '\n')
{
tmp[i] = '\0';
i = len;
}
}
line = strdup (tmp);
free (tmp);
free (full);
return line;
}
/*
* Gets first LINES lines from message body
*/
char *
mbox_body_lines (mailbox *mbox, int num, int lines)
{
char *full, *buf = NULL;
int i=0, line = 0, len;
if (lines < 1)
return strdup ("");
full = mbox_get_body (mbox, num);
if (full == NULL)
return NULL;
len = strlen (full);
for (i=0; i < len && line < lines; i++)
{
if (full[i] == '\n')
{
line++;
if (line >= lines)
{
full[i+1] = '\0';
buf = strdup (full);
i = len;
}
}
}
if (buf == NULL)
buf = strdup (full);
free (full);
return buf;
}
/*
* Bogus functions for unimplemented functions that return int
*/
int
......@@ -97,3 +204,4 @@ _mbox_dummy4 (mailbox * mbox, int num)
errno = ENOSYS;
return NULL;
}
......
/* copyright and license info go here */
/* 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _MAILBOX_H
#define _MAILBOX_H 1
......@@ -20,6 +35,12 @@
#define mbox_add_message(m,s) m->_add_message(m,s)
#define mbox_get_body(m,n) m->_get_body(m,n)
#define mbox_get_header(m,n) m->_get_header(m,n)
#define mbox_lock(m,n) m->_lock(m,n)
/* Lock settings */
#define MO_ULOCK 0
#define MO_RLOCK 1
#define MO_WLOCK 2
typedef struct _mailbox
{
......@@ -37,6 +58,7 @@ typedef struct _mailbox
int (*_expunge) __P ((struct _mailbox *));
int (*_add_message) __P ((struct _mailbox *, char *));
int (*_is_deleted) __P ((struct _mailbox *, int));
int (*_lock) __P((struct _mailbox *, int));
char *(*_get_body) __P ((struct _mailbox *, int));
char *(*_get_header) __P ((struct _mailbox *, int));
}
......@@ -44,4 +66,9 @@ mailbox;
mailbox *mbox_open __P ((const char *name));
char *mbox_header_line __P ((mailbox *mbox, int num, const char *header));
char *mbox_body_lines __P ((mailbox *mbox, int num, int lines));
#endif
......
/* copyright and license info go here */
/* 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "unixmbox.h"
/*
* Opens and locks a mailbox
* Opens a mailbox
*/
int
unixmbox_open (mailbox * mbox)
......@@ -22,7 +37,6 @@ unixmbox_open (mailbox * mbox)
mbox->_data = data;
data->file = fopen (mbox->name, "r+");
/* FIXME: do a good, NFS safe, file lock here, with error handling */
if (data->file == NULL)
{
/* errno is set by fopen() */
......@@ -39,6 +53,17 @@ unixmbox_open (mailbox * mbox)
return -1;
}
fgets (buf, 80, data->file);
if (strncmp (buf, "From ", 5))
{
/* This is NOT an mbox file */
unixmbox_close (mbox);
errno = 0;
return -1;
}
else
rewind (data->file);
while (fgets (buf, 80, data->file))
{
if (!strncmp (buf, "From ", 5))
......@@ -88,6 +113,7 @@ unixmbox_open (mailbox * mbox)
mbox->_expunge = unixmbox_expunge;
mbox->_add_message = unixmbox_add_message;
mbox->_is_deleted = unixmbox_is_deleted;
mbox->_lock = unixmbox_lock;
mbox->_get_body = unixmbox_get_body;
mbox->_get_header = unixmbox_get_header;
......@@ -101,7 +127,7 @@ int
unixmbox_close (mailbox * mbox)
{
unixmbox_data *data = mbox->_data;
/* FIXME: good file unlocking, to go along with the locking above */
unixmbox_lock (mbox, MO_ULOCK);
fclose (data->file);
free (data->messages);
free (mbox->sizes);
......@@ -266,3 +292,17 @@ unixmbox_get_header (mailbox * mbox, int num)
fread (buf, size, sizeof (char), data->file);
return buf;
}
/*
* Sets lock mode for a mailbox
* FIXME: this doesn't do anything, really
* Get locking code from Procmail and/or Exim
*/
int
unixmbox_lock (mailbox *mbox, int mode)
{
unixmbox_data *data = mbox->_data;
data->lockmode = mode;
return 0;
}
......
/* copyright and license info go here */
/* 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 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _UNIXMBOX_H
#define _UNIXMBOX_H 1
......@@ -40,6 +55,7 @@ typedef struct _unixmbox_data
{
unixmbox_message *messages;
FILE *file;
int lockmode;
}
unixmbox_data;
......@@ -49,6 +65,7 @@ int unixmbox_delete (mailbox *mbox, int num);
int unixmbox_undelete (mailbox *mbox, int num);
int unixmbox_expunge (mailbox *mbox);
int unixmbox_is_deleted (mailbox *mbox, int num);
int unixmbox_lock (mailbox *mbox, int mode);
int unixmbox_add_message (mailbox *mbox, char *message);
char *unixmbox_get_body (mailbox *mbox, int num);
char *unixmbox_get_header (mailbox *mbox, int num);
......