summaryrefslogtreecommitdiffabout
path: root/libmailutils
authorSergey Poznyakoff <gray@gnu.org.ua>2010-10-07 10:23:45 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2010-10-07 10:23:45 (GMT)
commitdd8d091ba3cae58fd437c664478de8bcbdf25ac6 (patch) (side-by-side diff)
tree8ffdc4ca5a4a05808d247a69ca87635a9f92a71a /libmailutils
parentf24df125b4a5f7e29eee39e89127871a38856281 (diff)
downloadmailutils-dd8d091ba3cae58fd437c664478de8bcbdf25ac6.tar.gz
mailutils-dd8d091ba3cae58fd437c664478de8bcbdf25ac6.tar.bz2
Implement a filter for quoting ^From. Use it for appending to UNIX mailboxes.
* libmailutils/fromflt.c: New file. * libmailutils/Makefile.am (libmailutils_la_SOURCES): Add fromflt.c * libmailutils/tests/testsuite.at: Include fromflt.at. * include/mailutils/filter.h (mu_from_filter): New filter type. * libmailutils/filter.c (mu_filter_get_list): Register mu_from_filter. * libmailutils/fromflt.at: New file. * libmailutils/tests/Makefile.am (TESTSUITE_AT): Add fromflt.at. * libmailutils/tests/base64d.at: Mention `filter' in the keywords. * libmailutils/tests/base64e.at: Likewise. * libmailutils/tests/fltst.c (main): Fix argc check. * libproto/mbox/mbox.c (append_message_to_stream): Use "FROM" filter.
Diffstat (limited to 'libmailutils') (more/less context) (ignore whitespace changes)
-rw-r--r--libmailutils/Makefile.am1
-rw-r--r--libmailutils/filter.c1
-rw-r--r--libmailutils/fromflt.c283
-rw-r--r--libmailutils/tests/Makefile.am1
-rw-r--r--libmailutils/tests/base64d.at4
-rw-r--r--libmailutils/tests/base64e.at4
-rw-r--r--libmailutils/tests/fltst.c2
-rw-r--r--libmailutils/tests/fromflt.at96
-rw-r--r--libmailutils/tests/testsuite.at1
9 files changed, 388 insertions, 5 deletions
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index 3f083e0..07a0a4e 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -83,6 +83,7 @@ libmailutils_la_SOURCES = \
fltstream.c\
folder.c\
freeitem.c\
+ fromflt.c\
gdebug.c\
getpass.c\
gocs.c\
diff --git a/libmailutils/filter.c b/libmailutils/filter.c
index 8fdc25f..0008ba0 100644
--- a/libmailutils/filter.c
+++ b/libmailutils/filter.c
@@ -78,6 +78,7 @@ mu_filter_get_list (mu_list_t *plist)
mu_list_append (filter_list, mu_dot_filter);
mu_list_append (filter_list, mu_rfc_2047_Q_filter);
mu_list_append (filter_list, mu_rfc_2047_B_filter);
+ mu_list_append (filter_list, mu_from_filter);
/* FIXME: add the default encodings? */
}
*plist = filter_list;
diff --git a/libmailutils/fromflt.c b/libmailutils/fromflt.c
new file mode 100644
index 0000000..d96d46d
--- a/dev/null
+++ b/libmailutils/fromflt.c
@@ -0,0 +1,283 @@
+/* GNU Mailutils -- a suite of utilities for electronic mail
+ Copyright (C) 2003, 2007, 2010 Free Software Foundation, Inc.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301 USA */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/errno.h>
+#include <mailutils/filter.h>
+#include <mailutils/stream.h>
+
+enum from_decode_state
+ {
+ from_decode_init,
+ from_decode_nl,
+ from_decode_char
+ };
+
+#define GT_FROM_MARK_STR ">From "
+#define GT_FROM_MARK_LEN (sizeof (GT_FROM_MARK_STR) - 1)
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each '>From '
+ at the beginning of line with 'From '. */
+static enum mu_filter_result
+_from_decoder (void *xd,
+ enum mu_filter_command cmd,
+ struct mu_filter_io *iobuf)
+{
+ int *pstate = xd;
+ const unsigned char *iptr;
+ size_t isize;
+ char *optr;
+ size_t osize;
+ enum from_decode_state state;
+ size_t i, j;
+
+ switch (cmd)
+ {
+ case mu_filter_init:
+ *pstate = from_decode_init;
+ return mu_filter_ok;
+
+ case mu_filter_done:
+ return mu_filter_ok;
+
+ default:
+ state = *pstate;
+ break;
+ }
+
+ iptr = (const unsigned char *) iobuf->input;
+ isize = iobuf->isize;
+ optr = iobuf->output;
+ osize = iobuf->osize;
+
+ for (i = j = 0; i < isize && j < osize; i++)
+ {
+ unsigned char c = *iptr++;
+
+ if (c == '\n')
+ state = from_decode_nl;
+ else if (state == from_decode_init || state == from_decode_nl)
+ {
+ size_t len = isize - i;
+
+ if (len < GT_FROM_MARK_LEN)
+ {
+ if (memcmp (iptr - 1, GT_FROM_MARK_STR, len) == 0)
+ {
+ if (i == 0)
+ {
+ iobuf->isize = GT_FROM_MARK_LEN - len;
+ return mu_filter_moreinput;
+ }
+ break;
+ }
+ else
+ state = from_decode_char;
+ }
+ else if (memcmp (iptr - 1, GT_FROM_MARK_STR, GT_FROM_MARK_LEN) == 0)
+ {
+ /* Skip > */
+ state = from_decode_char;
+ continue;
+ }
+ }
+ optr[j++] = c;
+ }
+
+ *pstate = state;
+ iobuf->isize = i;
+ iobuf->osize = j;
+ return mu_filter_ok;
+}
+
+#define FROM_MARK_STR "From "
+#define FROM_MARK_LEN (sizeof (FROM_MARK_STR) - 1)
+
+enum from_encode_state
+ {
+ from_encode_init,
+ from_encode_nl,
+ from_encode_char,
+ from_encode_gt,
+ from_encode_f,
+ from_encode_r,
+ from_encode_o,
+ from_encode_m,
+ from_encode_sp
+ };
+
+static int length_to_state_tab[] = {
+ from_encode_gt,
+ from_encode_f,
+ from_encode_r,
+ from_encode_o,
+ from_encode_m,
+ from_encode_sp
+};
+
+static int state_to_length_tab[] = {
+ 0, 0, 0,
+ GT_FROM_MARK_LEN,
+ GT_FROM_MARK_LEN-1,
+ GT_FROM_MARK_LEN-2,
+ GT_FROM_MARK_LEN-3,
+ GT_FROM_MARK_LEN-4,
+ GT_FROM_MARK_LEN-5
+};
+
+/* Move min(isize,osize) bytes from iptr to optr, replacing each 'From '
+ at the beginning of line with '>From '. */
+
+static enum mu_filter_result
+_from_encoder (void *xd,
+ enum mu_filter_command cmd,
+ struct mu_filter_io *iobuf)
+{
+ int *pstate = xd;
+ const unsigned char *iptr;
+ size_t isize;
+ char *optr;
+ size_t osize;
+ enum from_encode_state state;
+ size_t i, j;
+
+ switch (cmd)
+ {
+ case mu_filter_init:
+ *pstate = from_encode_init;
+ return mu_filter_ok;
+
+ case mu_filter_done:
+ return mu_filter_ok;
+
+ default:
+ state = *pstate;
+ switch (state)
+ {
+ case from_encode_init:
+ case from_encode_nl:
+ case from_encode_char:
+ break;
+
+ default:
+ osize = state_to_length_tab[state];
+ if (iobuf->osize < osize)
+ {
+ iobuf->osize = osize;
+ return mu_filter_moreoutput;
+ }
+ memcpy (iobuf->output, GT_FROM_MARK_STR + GT_FROM_MARK_LEN - osize,
+ osize);
+ iobuf->osize = osize;
+ iobuf->isize = osize;
+ *pstate = from_encode_init;
+ return mu_filter_ok;
+ }
+ break;
+ }
+
+ iptr = (const unsigned char *) iobuf->input;
+ isize = iobuf->isize;
+ optr = iobuf->output;
+ osize = iobuf->osize;
+
+ for (i = j = 0; i < isize && j < osize; i++)
+ {
+ unsigned char c = *iptr++;
+
+ if (c == '\n')
+ state = from_encode_nl;
+ else if (state == from_encode_init || state == from_encode_nl)
+ {
+ size_t len = isize - i;
+
+ if (len < FROM_MARK_LEN)
+ {
+ if (memcmp (iptr - 1, FROM_MARK_STR, len) == 0)
+ {
+ if (i == 0)
+ {
+ iobuf->isize = FROM_MARK_LEN;
+ return mu_filter_moreinput;
+ }
+ break;
+ }
+ else
+ state = from_encode_char;
+ }
+ else if (memcmp (iptr - 1, FROM_MARK_STR, FROM_MARK_LEN) == 0)
+ {
+ size_t rest = osize - j;
+
+ if (rest > GT_FROM_MARK_LEN)
+ rest = GT_FROM_MARK_LEN;
+ else if (rest < 2)
+ {
+ if (i == 0)
+ {
+ iobuf->osize = GT_FROM_MARK_LEN;
+ return mu_filter_moreoutput;
+ }
+ break;
+ }
+
+ memcpy (optr + j, GT_FROM_MARK_STR, rest);
+ i += rest - 2;
+ iptr += rest - 2;
+ j += rest;
+ if (rest < GT_FROM_MARK_LEN)
+ state = length_to_state_tab[rest];
+ else
+ state = from_encode_char;
+ continue;
+ }
+ else
+ state = from_encode_char;
+ }
+ optr[j++] = c;
+ }
+ *pstate = state;
+ iobuf->isize = i;
+ iobuf->osize = j;
+ return mu_filter_ok;
+}
+
+static int
+_from_alloc_state (void **pret, int mode, void *data MU_ARG_UNUSED)
+{
+ *pret = malloc (sizeof (int));
+ if (!*pret)
+ return ENOMEM;
+ return 0;
+}
+
+static struct _mu_filter_record _from_filter = {
+ "FROM",
+ 0,
+ _from_alloc_state,
+ _from_encoder,
+ _from_decoder
+};
+
+mu_filter_record_t mu_from_filter = &_from_filter;
+
diff --git a/libmailutils/tests/Makefile.am b/libmailutils/tests/Makefile.am
index 7324e88..a8215fb 100644
--- a/libmailutils/tests/Makefile.am
+++ b/libmailutils/tests/Makefile.am
@@ -64,6 +64,7 @@ TESTSUITE_AT = \
base64e.at\
decode2047.at\
encode2047.at\
+ fromflt.at\
list.at\
mailcap.at\
testsuite.at\
diff --git a/libmailutils/tests/base64d.at b/libmailutils/tests/base64d.at
index b26c415..df1efd3 100644
--- a/libmailutils/tests/base64d.at
+++ b/libmailutils/tests/base64d.at
@@ -16,7 +16,7 @@
# This file is part of Mailfromd testsuite.
AT_SETUP([base64 decoding (read)])
-AT_KEYWORDS([base64 base64d base64dr decode])
+AT_KEYWORDS([base64 base64d base64dr decode filter])
AT_CHECK([
cp $abs_top_srcdir/libmailutils/tests/Encode expout
@@ -28,7 +28,7 @@ AT_CLEANUP
AT_SETUP([base64 decoding (write)])
-AT_KEYWORDS([base64 base64d base64dw decode])
+AT_KEYWORDS([base64 base64d base64dw decode filter])
AT_CHECK([
cp $abs_top_srcdir/libmailutils/tests/Encode expout
diff --git a/libmailutils/tests/base64e.at b/libmailutils/tests/base64e.at
index c753471..b94522e 100644
--- a/libmailutils/tests/base64e.at
+++ b/libmailutils/tests/base64e.at
@@ -16,7 +16,7 @@
# This file is part of Mailfromd testsuite.
AT_SETUP([base64 encoding (read)])
-AT_KEYWORDS([base64 base64e base64er encode])
+AT_KEYWORDS([base64 base64e base64er encode filter])
AT_CHECK([
cp $abs_top_srcdir/libmailutils/tests/Decode expout
@@ -28,7 +28,7 @@ AT_CLEANUP
AT_SETUP([base64 encoding (write)])
-AT_KEYWORDS([base64 base64e base64ew encode])
+AT_KEYWORDS([base64 base64e base64ew encode filter])
AT_CHECK([
cp $abs_top_srcdir/libmailutils/tests/Decode expout
diff --git a/libmailutils/tests/fltst.c b/libmailutils/tests/fltst.c
index 8597dd5..d62a02b 100644
--- a/libmailutils/tests/fltst.c
+++ b/libmailutils/tests/fltst.c
@@ -114,7 +114,7 @@ main (int argc, char * argv [])
if (argc == 1)
usage (NULL);
- if (argc < 3)
+ if (argc < 4)
usage ("not enough arguments");
fltname = argv[1];
diff --git a/libmailutils/tests/fromflt.at b/libmailutils/tests/fromflt.at
new file mode 100644
index 0000000..a5b3313
--- a/dev/null
+++ b/libmailutils/tests/fromflt.at
@@ -0,0 +1,96 @@
+# This file is part of GNU Mailutils. -*- Autotest -*-
+# Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
+#
+# GNU Mailutils 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 3, 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 GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
+# This file is part of Mailfromd testsuite.
+
+# -------------------------------------------
+# Data for 'From' filter tests.
+# -------------------------------------------
+
+m4_define([from_plain_text],[From this time on
+from that source
+ From stdin
+and
+From them
+])
+
+m4_define([from_encoded_text],[>From this time on
+from that source
+ From stdin
+and
+>From them
+])
+
+# -------------------------------------------
+# Test 'From' encoding
+# -------------------------------------------
+
+# Test read mode
+
+AT_SETUP([from filter encoding (read)])
+AT_KEYWORDS([from frome fromer encode])
+
+AT_CHECK([
+AT_DATA([input],from_plain_text)
+fltst from encode read < input
+],
+[0],
+[from_encoded_text])
+
+AT_CLEANUP
+
+# The same, in write mode
+
+AT_SETUP([from filter encoding (write)])
+AT_KEYWORDS([from frome fromew encode])
+
+AT_CHECK([
+AT_DATA([input],from_plain_text)
+fltst from encode write < input
+],
+[0],
+[from_encoded_text])
+
+AT_CLEANUP
+
+# -------------------------------------------
+# Test '>From' decoding
+# -------------------------------------------
+
+AT_SETUP([from filter decoding (read)])
+AT_KEYWORDS([from fromd fromdr decode])
+
+AT_CHECK([
+AT_DATA([input],from_encoded_text)
+fltst from decode read < input
+],
+[0],
+[from_plain_text])
+
+AT_CLEANUP
+
+# The same, in write mode
+
+AT_SETUP([from filter decoding (write)])
+AT_KEYWORDS([from fromd fromdw decode])
+
+AT_CHECK([
+AT_DATA([input],from_encoded_text)
+fltst from decode write < input
+],
+[0],
+[from_plain_text])
+
+AT_CLEANUP
diff --git a/libmailutils/tests/testsuite.at b/libmailutils/tests/testsuite.at
index 1de64b6..fbdc7c9 100644
--- a/libmailutils/tests/testsuite.at
+++ b/libmailutils/tests/testsuite.at
@@ -63,4 +63,5 @@ m4_include([base64e.at])
m4_include([base64d.at])
m4_include([decode2047.at])
m4_include([encode2047.at])
+m4_include([fromflt.at])

Return to:

Send suggestions and report system problems to the System administrator.