summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/mimetest.c58
-rw-r--r--include/mailutils/message.h3
-rw-r--r--mailbox/attachment.c104
3 files changed, 133 insertions, 32 deletions
diff --git a/examples/mimetest.c b/examples/mimetest.c
index 757197826..724f9aeac 100644
--- a/examples/mimetest.c
+++ b/examples/mimetest.c
@@ -19,19 +19,21 @@
# include <config.h>
#endif
-#include <sys/types.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <unistd.h>
-#include <limits.h>
+
+#include <sys/types.h>
#include <mailutils/mailbox.h>
#include <mailutils/header.h>
#include <mailutils/filter.h>
#include <mailutils/registrar.h>
-void message_display_parts(message_t msg, char *indent);
+void message_display_parts(message_t msg, const char *indent);
char from[256];
char subject[256];
@@ -43,20 +45,24 @@ main (int argc, char **argv)
int ret;
size_t i;
size_t count = 0;
- char *mailbox_name = NULL;
+ char *mailbox_name = argv[1];
+ int debug = 0;
- /* have an argument */
- if (argc > 1)
- mailbox_name = argv[1];
+ if (strcmp("-d", mailbox_name) == 0)
+ {
+ debug = 1;
+ mailbox_name = argv[2];
+ }
/* Registration. */
{
list_t bookie;
registrar_get_list (&bookie);
+ list_append (bookie, file_record);
+ list_append (bookie, imap_record);
list_append (bookie, mbox_record);
list_append (bookie, path_record);
list_append (bookie, pop_record);
- list_append (bookie, imap_record);
}
if ((ret = mailbox_create_default (&mbox, mailbox_name)) != 0)
@@ -65,8 +71,8 @@ main (int argc, char **argv)
exit (2);
}
- /* Debuging Trace. */
- if (0)
+ /* Debugging trace. */
+ if (debug)
{
mu_debug_t debug;
mailbox_get_debug (mbox, &debug);
@@ -76,12 +82,13 @@ main (int argc, char **argv)
/* Open the mailbox for reading only. */
if ((ret = mailbox_open (mbox, MU_STREAM_RDWR)) != 0)
{
- fprintf (stderr, "could not open - %s\n", strerror (ret));
+ fprintf (stderr, "mailbox open - %s\n", strerror (ret));
exit (2);
}
- /* Iterator through the entire message set. */
+ /* Iterate through the entire message set. */
mailbox_messages_count (mbox, &count);
+
for (i = 1; i <= count; ++i)
{
message_t msg;
@@ -107,7 +114,10 @@ main (int argc, char **argv)
header_get_value (hdr, MU_HEADER_FROM, from, sizeof (from), NULL);
header_get_value (hdr, MU_HEADER_SUBJECT, subject, sizeof (subject),
NULL);
- printf ("From: %s\tSubject: %s\n", from, subject);
+ printf ("-- Message: %d\n", i);
+ printf ("-- From: %s\n", from);
+ printf ("-- Subject: %s\n", subject);
+
if ((ret = message_get_num_parts (msg, &nparts)) != 0)
{
fprintf (stderr, "message_get_num_parts - %s\n", strerror (ret));
@@ -115,7 +125,7 @@ main (int argc, char **argv)
}
printf ("-- Number of parts in message - %d\n", nparts);
printf ("-- Total message size - %d\n", msize);
- message_display_parts (msg, "\t");
+ message_display_parts (msg, " ");
}
mailbox_close (mbox);
mailbox_destroy (&mbox);
@@ -125,7 +135,7 @@ main (int argc, char **argv)
char buf[2048];
void
-message_display_parts (message_t msg, char *indent)
+message_display_parts (message_t msg, const char *indent)
{
int ret, j;
size_t msize, nparts, nsubparts;
@@ -138,7 +148,7 @@ message_display_parts (message_t msg, char *indent)
int offset, ismulti;
size_t nbytes;
- /* How many part those the message has? */
+ /* How many parts does the message has? */
if ((ret = message_get_num_parts (msg, &nparts)) != 0)
{
fprintf (stderr, "message_get_num_parts - %s\n", strerror (ret));
@@ -146,8 +156,8 @@ message_display_parts (message_t msg, char *indent)
}
/* Iterate through all the parts.
- Treat type "message/rfc822" differently, since it is a message of is own
- that can have other subparts(recursive). */
+ Treat type "message/rfc822" differently, since it is a message of
+ its own that can have other subparts(recursive). */
for (j = 1; j <= nparts; j++)
{
if ((ret = message_get_part (msg, j, &part)) != 0)
@@ -233,11 +243,9 @@ message_display_parts (message_t msg, char *indent)
}
else
{
-#if 1
-
/* Save the attachements. */
- char *fname;
- message_attachment_filename (part, &fname);
+ char *fname = NULL;
+ message_aget_attachment_name (part, &fname);
if (fname == NULL)
{
char buffer[PATH_MAX + 1];
@@ -247,11 +255,9 @@ message_display_parts (message_t msg, char *indent)
printf
("%s-------------------------------------------------------------------\n",
indent);
- /* FIXME: Somethings is not quite correct with this function.
- Please fix. */
- message_save_attachment (part, fname, NULL);
+ /*FIXME: What is the 'data' argument for? */
+ message_save_attachment (part, NULL, NULL);
free (fname);
-#endif
}
printf
("\n%s End -------------------------------------------------------------------\n",
diff --git a/include/mailutils/message.h b/include/mailutils/message.h
index cadafe2f8..149ecbee0 100644
--- a/include/mailutils/message.h
+++ b/include/mailutils/message.h
@@ -121,6 +121,9 @@ extern int message_encapsulate __P ((message_t msg, message_t *newmsg,
extern int message_unencapsulate __P ((message_t msg, message_t *newmsg,
void **data));
+extern int message_get_attachment_name __P ((message_t, char *name, size_t bufsz, size_t* sz));
+extern int message_aget_attachment_name __P ((message_t, char **name));
+
#ifdef __cplusplus
}
#endif
diff --git a/mailbox/attachment.c b/mailbox/attachment.c
index 9baed1aeb..d4c61058e 100644
--- a/mailbox/attachment.c
+++ b/mailbox/attachment.c
@@ -45,9 +45,6 @@
#define MAX_HDR_LEN 256
#define BUF_SIZE 2048
-/* FIXME: this should be in a public header. */
-extern int message_attachment_filename __P ((message_t, const char **filename));
-
struct _msg_info
{
char *buf;
@@ -227,8 +224,9 @@ _header_get_param (char *field_body, const char *param, size_t * len)
return NULL;
}
+#if 0
int
-message_attachment_filename (message_t msg, const char **filename)
+message_get_attachment_name (message_t msg, char *name, size_t bufsz, size_t *sz)
{
char *pTmp, *fname = NULL;
header_t hdr;
@@ -270,6 +268,90 @@ message_attachment_filename (message_t msg, const char **filename)
}
return ret;
}
+#endif
+
+int message_aget_attachment_name(message_t msg, char** name)
+{
+ size_t sz = 0;
+ int ret = 0;
+
+ if(name == NULL)
+ return EINVAL;
+
+ if((ret = message_get_attachment_name(msg, NULL, 0, &sz)) != 0)
+ return ret;
+
+ *name = malloc(sz + 1);
+
+ if((ret = message_get_attachment_name(msg, *name, sz + 1, NULL)) != 0)
+ {
+ free(*name);
+ *name = NULL;
+ }
+
+ return ret;
+}
+
+int
+message_get_attachment_name (message_t msg, char *buf, size_t bufsz, size_t *sz)
+{
+ int ret = EINVAL;
+ header_t hdr;
+ char *value = NULL;
+ char *name = NULL;
+ size_t namesz = 0;
+
+ if(!msg)
+ return ret;
+
+ if ((ret = message_get_header (msg, &hdr)) != 0)
+ return ret;
+
+ ret = header_aget_value (hdr, "Content-Disposition", &value);
+
+ /* If the header wasn't there, we'll fall back to Content-Type, but
+ other errors are fatal. */
+ if(ret != 0 && ret != ENOENT)
+ return ret;
+
+ if(ret == 0 && value != NULL)
+ {
+ /* FIXME: this is cheezy, it should check the value of the
+ Content-Disposition field, not strstr it. */
+
+ if (strstr (value, "attachment") != NULL)
+ name = _header_get_param (value, "filename", &namesz);
+ }
+
+ /* If we didn't get the name, we fall back on the Content-Type name
+ parameter. */
+
+ if (name == NULL)
+ {
+ if(value)
+ free(value);
+
+ ret = header_aget_value (hdr, "Content-Type", &value);
+ name = _header_get_param (value, "name", &namesz);
+ }
+
+ if (name)
+ {
+ ret = 0;
+
+ name[namesz] = '\0';
+
+ if(sz)
+ *sz = namesz;
+
+ if(buf)
+ strncpy(buf, name, bufsz);
+ }
+ else
+ ret = ENOENT;
+
+ return ret;
+}
int
message_save_attachment (message_t msg, const char *filename, void **data)
@@ -282,8 +364,9 @@ message_save_attachment (message_t msg, const char *filename, void **data)
header_t hdr;
char *content_encoding;
const char *fname = NULL;
+ char *partname = NULL;
- if (msg == NULL || filename == NULL)
+ if (msg == NULL)
return EINVAL;
if ((ret = _attachment_setup (&info, msg, &istream, data)) != 0)
@@ -292,7 +375,11 @@ message_save_attachment (message_t msg, const char *filename, void **data)
if (ret == 0 && (ret = message_get_header (msg, &hdr)) == 0)
{
if (filename == NULL)
- ret = message_attachment_filename (msg, &fname);
+ {
+ ret = message_aget_attachment_name (msg, &partname);
+ if(partname)
+ fname = partname;
+ }
else
fname = filename;
if (fname
@@ -349,6 +436,11 @@ message_save_attachment (message_t msg, const char *filename, void **data)
stream_destroy (&info->fstream, NULL);
_attachment_free (info, ret);
}
+
+ /* Free fname if we allocated it. */
+ if(partname)
+ free(partname);
+
return ret;
}

Return to:

Send suggestions and report system problems to the System administrator.