Commit 5887db14 5887db144a7794cac67bc9338427421a3fb008b7 by Sam Roberts

message_save_to_mailbox(): convenience function, perhaps it should go

in mailbox/mutil.c, but nothing else in there is a mailutils API wrapper.
1 parent 5706f6e6
......@@ -24,13 +24,14 @@
struct _message;
typedef struct _message *message_t;
#include <mailutils/attribute.h>
#include <mailutils/auth.h>
#include <mailutils/body.h>
#include <mailutils/envelope.h>
#include <mailutils/header.h>
#include <mailutils/body.h>
#include <mailutils/stream.h>
#include <mailutils/observer.h>
#include <mailutils/attribute.h>
#include <mailutils/mailbox.h>
#include <mailutils/observer.h>
#include <mailutils/stream.h>
#ifndef __P
# ifdef __STDC__
......@@ -124,6 +125,10 @@ extern int message_unencapsulate __P ((message_t msg, message_t *newmsg,
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));
extern int message_save_to_mailbox __P ((message_t msg, ticket_t ticket,
mu_debug_t debug, const char *toname));
#ifdef __cplusplus
}
#endif
......
......@@ -1004,3 +1004,79 @@ message_body_read (stream_t stream, char *buffer, size_t n, off_t off,
*pn = nread;
return status;
}
int
message_save_to_mailbox (message_t msg, ticket_t ticket, mu_debug_t debug,
const char *toname)
{
int rc = 0;
mailbox_t to = 0;
if ((rc = mailbox_create_default (&to, toname)))
{
mu_debug_print (debug, MU_DEBUG_TRACE,
"mailbox_create_default (%s) failed: %s\n", toname,
strerror (rc));
goto end;
}
if (debug && (rc = mailbox_set_debug (to, debug)))
goto end;
if (ticket)
{
folder_t folder = NULL;
if ((rc = mailbox_get_folder (to, &folder)))
goto end;
/* FIXME: not all mailboxes have folders, thus this hack. */
if (folder)
{
authority_t auth = NULL;
if ((rc = folder_get_authority (folder, &auth)))
goto end;
/* FIXME: not all folders have authentication, thus this hack. */
if (auth && (rc = authority_set_ticket (auth, ticket)))
goto end;
}
}
if ((rc = mailbox_open (to, MU_STREAM_WRITE | MU_STREAM_CREAT)))
{
mu_debug_print (debug, MU_DEBUG_TRACE,
"mailbox_open (%s) failed: %s\n", toname,
strerror (rc));
goto end;
}
if ((rc = mailbox_append_message (to, msg)))
{
mu_debug_print (debug, MU_DEBUG_TRACE,
"mailbox_append_message (%s) failed: %s\n", toname,
strerror (rc));
goto end;
}
end:
if (!rc)
{
if ((rc = mailbox_close (to)))
{
mu_debug_print (debug, MU_DEBUG_TRACE,
"mailbox_close (%s) failed: %s\n", toname,
strerror (rc));
}
}
else
{
mailbox_close (to);
}
mailbox_destroy (&to);
return rc;
}
......