summaryrefslogtreecommitdiff
path: root/readmsg
diff options
context:
space:
mode:
authorAlain Magloire <alainm@gnu.org>2001-06-28 02:20:42 +0000
committerAlain Magloire <alainm@gnu.org>2001-06-28 02:20:42 +0000
commit86906fca462365bdb70af844e17384ca5db6063a (patch)
tree96f099be921da103232237a4dd8895dc5da12230 /readmsg
parenta273efe0a7716b83b3561ba69bb837c6a7f11b96 (diff)
downloadmailutils-86906fca462365bdb70af844e17384ca5db6063a.tar.gz
mailutils-86906fca462365bdb70af844e17384ca5db6063a.tar.bz2
* configure.in: We were not checking for thread support
at all because the test was faulty. * readmsg: New directory. * readmsg/readmsg.c: New file. * readmsg/readmsg.h: New file. * readmsg/msglist.c: New file. This is base on a old testing program Dave inglis wrote to test the mime parsing code. It is still work in progress but a good base for the readmsg(1) utility clone. Not enable yet in the TOP Makefile.
Diffstat (limited to 'readmsg')
-rw-r--r--readmsg/Makefile.am8
-rw-r--r--readmsg/msglist.c121
-rw-r--r--readmsg/readmsg.c303
-rw-r--r--readmsg/readmsg.h37
4 files changed, 469 insertions, 0 deletions
diff --git a/readmsg/Makefile.am b/readmsg/Makefile.am
new file mode 100644
index 000000000..3a2a661ca
--- /dev/null
+++ b/readmsg/Makefile.am
@@ -0,0 +1,8 @@
+AUTOMAKE_OPTIONS = ../lib/ansi2knr
+INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib
+
+bin_PROGRAMS = readmsg
+
+readmsg_SOURCES = readmsg.c msglist.c readmsg.h
+readmsg_DEPENDENCIES = ../mailbox/libmailbox.la
+readmsg_LDADD = ../mailbox/libmailbox.la ../lib/libmailutils.a
diff --git a/readmsg/msglist.c b/readmsg/msglist.c
new file mode 100644
index 000000000..cd6ab2518
--- /dev/null
+++ b/readmsg/msglist.c
@@ -0,0 +1,121 @@
+/* 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 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 <errno.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mailutils/mailbox.h>
+
+extern mailbox_t mbox;
+extern unsigned int total;
+
+static int
+addset (int **set, int *n, unsigned val)
+{
+ int *tmp;
+ tmp = realloc (*set, (*n + 1) * sizeof (**set));
+ if (tmp == NULL)
+ {
+ if (*set)
+ free (*set);
+ *n = 0;
+ *set = NULL;
+ return ENOMEM;
+ }
+ *set = tmp;
+ (*set)[*n] = val;
+ (*n)++;
+ return 0;
+}
+
+int
+msgset (const int argc, char **argv, int **set, int *n)
+{
+ int i = 0, lc = 0;
+ int undelete = 0;
+ int *ret = NULL;
+
+ for (i = 0; i < argc; i++)
+ {
+ /* Last message */
+ if (!strcmp (argv[i], "$") || !strcmp (argv[i], "0"))
+ {
+ addset (set, n, total);
+ }
+ else if (!strcmp (argv[i], "*"))
+ {
+ /* all messages */
+ for (i = 1; i <= total; i++)
+ addset (set, n, i);
+ i = argc + 1;
+ }
+ else if (argv[i][0] == '/')
+ {
+ /* FIXME: all messages with pattern following / in
+ the subject line, case insensitive */
+ /* This currently appears to be quit b0rked */
+ message_t msg;
+ header_t hdr;
+ char subj[128];
+ int j = 1, k = 0, len2 = 0;
+ int len = strlen (&argv[i][1]);
+ for (j = 1; j <= total; j++)
+ {
+ mailbox_get_message (mbox, j, &msg);
+ message_get_header (msg, &hdr);
+ header_get_value (hdr, MU_HEADER_SUBJECT, subj, 128, NULL);
+ len2 = strlen (subj);
+ for (k = 0; i < strlen (subj); k++)
+ {
+ if (len2 - k >= len
+ && !strncasecmp (&argv[i][1], &subj[k], len))
+ {
+ addset (set, n, j);
+ k = 128;
+ }
+ }
+ }
+ }
+ else if (isalpha(argv[i][0]))
+ {
+ /* FIXME: all messages from sender argv[i] */
+ }
+ else if (strchr (argv[i], '-') != NULL)
+ {
+ /* message range */
+ int j, x, y;
+ char *arg = strdup (argv[i]);
+ for (j = 0; j < strlen (arg); j++)
+ if (arg[j] == '-')
+ break;
+ arg[j] = '\0';
+ x = strtol (arg, NULL, 10);
+ y = strtol (&(arg[j + 1]), NULL, 10);
+ for (; x <= y; x++)
+ addset (set, n, x);
+ free (arg);
+ }
+ else
+ {
+ /* single message */
+ addset (set, n, strtol (argv[i], NULL, 10));
+ }
+ }
+
+ return 0;
+}
diff --git a/readmsg/readmsg.c b/readmsg/readmsg.c
new file mode 100644
index 000000000..2736c42ad
--- /dev/null
+++ b/readmsg/readmsg.c
@@ -0,0 +1,303 @@
+/* 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 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. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+
+#include "readmsg.h"
+
+mailbox_t mbox;
+size_t total;
+
+static char from[256];
+static char subject[256];
+
+static void display_parts (message_t, const char *indent);
+
+/* This is still work in progress */
+int
+main(int argc, char **argv)
+{
+ int status;
+ int *set = NULL;
+ int n = 0;
+ size_t i;
+ char *mailbox_name = NULL;
+
+ /* FIXME: Parse options: See readmsg(1) part of elm:
+ readmsg 1 3 0
+ extracts three messages from the folder: the first, the third, and
+ the last. */
+
+ /* Check this in the option --folder. */
+
+ /* Registration. */
+ {
+ list_t bookie;
+ registrar_get_list (&bookie);
+ list_append (bookie, mbox_record);
+ list_append (bookie, path_record);
+ list_append (bookie, pop_record);
+ list_append (bookie, imap_record);
+ }
+
+ status = mailbox_create_default (&mbox, mailbox_name);
+ if (status != 0)
+ {
+ fprintf (stderr, "could not create - %s\n", strerror(status));
+ exit (2);
+ }
+
+ /* Debuging Trace. */
+ if ( 0 )
+ {
+ debug_t debug;
+ mailbox_get_debug (mbox, &debug);
+ debug_set_level (debug, MU_DEBUG_TRACE|MU_DEBUG_PROT);
+ }
+
+ status = mailbox_open (mbox, MU_STREAM_READ);
+ if (status != 0)
+ {
+ fprintf (stderr, "could not open - %s\n", strerror(status));
+ exit (2);
+ }
+
+ mailbox_messages_count (mbox, &total);
+
+ /* Build an array containing the message number. */
+ if (argc > 1)
+ msgset (argc - 1, &argv[1], &set, &n);
+ else
+ {
+ char *av[] = { "*" };
+ msgset (1, av, &set, &n);
+ }
+
+ for (i = 0; i < n; ++i)
+ {
+ message_t msg;
+ header_t hdr;
+ size_t msize;
+
+ status = mailbox_get_message (mbox, set[i], &msg);
+ if (status != 0)
+ {
+ fprintf (stderr, "mailbox_get_message - %s\n", strerror (status));
+ exit (2);
+ }
+
+ status = message_get_header (msg, &hdr);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_get_header - %s\n", strerror(status));
+ exit(2);
+ }
+
+ header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
+ header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
+ NULL);
+ fprintf(stdout, "From: %s\tSubject: %s\n", from, subject);
+
+ status = message_size (msg, &msize);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_size - %s\n", strerror(status));
+ exit(2);
+ }
+ fprintf (stdout, "-- Total message size - %d\n", msize);
+
+ display_parts (msg, "\t");
+ }
+ mailbox_close (mbox);
+ mailbox_destroy (&mbox);
+ return 0;
+}
+
+static char buf[2048];
+
+static void
+display_parts (message_t message, const char *indent)
+{
+ int status, j;
+ size_t msize, nparts;
+ message_t msg;
+ header_t hdr;
+ char type[256];
+ char encoding[256];
+ int is_multi = 0;
+ char *nl;
+
+ status = message_get_num_parts (message, &nparts);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_get_num_parts - %s\n", strerror (status));
+ exit (2);
+ }
+ fprintf(stdout, "%s-- Number of parts in message - %d\n", indent, nparts);
+
+ for (j = 1; j <= nparts; j++)
+ {
+ status = message_get_part (message, j, &msg);
+ if (status != 0 )
+ {
+ fprintf (stderr, "message_get_part - %s\n", strerror (status));
+ exit (2);
+ }
+
+ status = message_get_header (msg, &hdr);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_get_header - %s\n", strerror (status));
+ exit (2);
+ }
+
+ type[0] = '\0';
+ header_get_value (hdr, MU_HEADER_CONTENT_TYPE, type, sizeof (type),
+ NULL);
+
+ nl = strchr (type, '\n');
+ while ((nl = strchr (type, '\n')) != NULL)
+ {
+ *nl = ' ';
+ }
+ fprintf(stdout, "%sType of part %d = %s\n", indent, j, type);
+
+ status = message_size (msg, &msize);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_size - %s\n", strerror (status));
+ exit (2);
+ }
+ fprintf(stdout, "%sMessage part size - %d\n",indent, msize);
+
+ encoding[0] = '\0';
+ header_get_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, encoding,
+ sizeof (encoding), NULL);
+
+ if (type[0] && strncasecmp (type, "message/rfc822", strlen (type)) == 0)
+ {
+ message_t submsg = NULL;
+ char tmp[10];
+ tmp[0] = '\0';
+
+ status = message_unencapsulate (msg, &submsg, NULL);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_unencapsulate - %s\n", strerror (status));
+ exit (2);
+ }
+
+ status = message_get_header (submsg, &hdr);
+ if (status != 0)
+ {
+ fprintf (stderr, "message_get_header - %s\n", strerror (status));
+ exit (2);
+ }
+
+ header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
+ header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
+ NULL);
+ fprintf (stdout, "%sEncapsulated message : %s\t%s\n", indent, from,
+ subject);
+ fprintf (stdout, "%s----------------------------------------------\
+---------------------\n", indent);
+
+ strcpy (tmp, indent);
+ strcat (tmp,"\t");
+ display_parts (submsg, tmp);
+ message_destroy (&submsg, NULL);
+ }
+ else if ((strncasecmp (type, "text/plain", strlen ("text/plain")) == 0)
+ || (strncasecmp (type, "text/html", strlen ("text/html")) == 0))
+ {
+ stream_t d_stream = NULL;
+ stream_t b_stream = NULL;
+ body_t body;
+ size_t nbytes = 0;
+ int offset = 0;
+ fprintf (stdout, "%sText Message\n",indent);
+ fprintf (stdout, "%s-------------------------------------------------------------------\n", indent);
+ message_get_body (msg, &body);
+ body_get_stream (body, &b_stream);
+ //d_stream = b_stream;
+ //status = decoder_stream_create(&d_stream, b_stream, encoding);
+ status = filter_create (&d_stream, b_stream, encoding, 0, 0);
+ stream_setbufsiz (d_stream, 128);
+ if (status != 0)
+ {
+ d_stream = b_stream;
+ }
+ while (stream_readline (d_stream, buf, sizeof (buf),
+ offset, &nbytes ) == 0 && nbytes )
+ {
+ fprintf (stdout, "%s%s", indent, buf);
+ offset += nbytes;
+ }
+ if (status == 0)
+ stream_destroy(&d_stream, NULL);
+ }
+ else
+ {
+ message_is_multipart (msg, &is_multi);
+ if (is_multi)
+ {
+ char tmp[24];
+ memset (tmp, '\0', sizeof (tmp));
+ strcpy(tmp, indent);
+ strcat(tmp,"\t");
+ display_parts (msg, tmp);
+ }
+ else
+ {
+ body_t body = NULL;
+ stream_t stream = NULL;
+ size_t nbytes = 0;
+ int offset = 0;
+ message_get_body (msg, &body);
+ body_get_stream (body, &stream);
+#if 0
+ while (stream_readline (stream, buf, sizeof (buf),
+ offset, &nbytes ) == 0 && nbytes )
+ {
+ fprintf (stdout, "%s%s", indent, buf);
+ offset += nbytes;
+ }
+#endif
+ }
+#if 0
+ {
+ char *fname;
+ message_attachment_filename ( msg, &fname);
+ if ( fname == NULL )
+ {
+ char buffer[PATH_MAX+1];
+ fname = tempnam (getcwd(buffer, PATH_MAX), "msg-" );
+ }
+ fprintf (stdout, "%sAttachment - saving [%s]\n",indent, fname);
+ fprintf (stdout, "%s-------------------------------------------------------------------\n", indent);
+ message_save_attachment (msg, fname, NULL);
+ free (fname);
+ }
+#endif
+
+ }
+ fprintf(stdout, "\n%s End -------------------------------------------------------------------\n", indent);
+ }
+}
diff --git a/readmsg/readmsg.h b/readmsg/readmsg.h
new file mode 100644
index 000000000..24c250c88
--- /dev/null
+++ b/readmsg/readmsg.h
@@ -0,0 +1,37 @@
+/* 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 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 _READMSG_H
+#define _REAMSG_H
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <limits.h>
+
+#include <mailutils/mailbox.h>
+#include <mailutils/header.h>
+#include <mailutils/mime.h>
+#include <mailutils/filter.h>
+#include <mailutils/registrar.h>
+
+extern mailbox_t mbox;
+extern size_t total;
+
+#endif

Return to:

Send suggestions and report system problems to the System administrator.