message_save_to_mailbox(): convenience function, perhaps it should go
in mailbox/mutil.c, but nothing else in there is a mailutils API wrapper.
Showing
2 changed files
with
85 additions
and
4 deletions
... | @@ -24,13 +24,14 @@ | ... | @@ -24,13 +24,14 @@ |
24 | struct _message; | 24 | struct _message; |
25 | typedef struct _message *message_t; | 25 | typedef struct _message *message_t; |
26 | 26 | ||
27 | #include <mailutils/attribute.h> | ||
28 | #include <mailutils/auth.h> | ||
29 | #include <mailutils/body.h> | ||
27 | #include <mailutils/envelope.h> | 30 | #include <mailutils/envelope.h> |
28 | #include <mailutils/header.h> | 31 | #include <mailutils/header.h> |
29 | #include <mailutils/body.h> | ||
30 | #include <mailutils/stream.h> | ||
31 | #include <mailutils/observer.h> | ||
32 | #include <mailutils/attribute.h> | ||
33 | #include <mailutils/mailbox.h> | 32 | #include <mailutils/mailbox.h> |
33 | #include <mailutils/observer.h> | ||
34 | #include <mailutils/stream.h> | ||
34 | 35 | ||
35 | #ifndef __P | 36 | #ifndef __P |
36 | # ifdef __STDC__ | 37 | # ifdef __STDC__ |
... | @@ -124,6 +125,10 @@ extern int message_unencapsulate __P ((message_t msg, message_t *newmsg, | ... | @@ -124,6 +125,10 @@ extern int message_unencapsulate __P ((message_t msg, message_t *newmsg, |
124 | extern int message_get_attachment_name __P ((message_t, char *name, size_t bufsz, size_t* sz)); | 125 | extern int message_get_attachment_name __P ((message_t, char *name, size_t bufsz, size_t* sz)); |
125 | extern int message_aget_attachment_name __P ((message_t, char **name)); | 126 | extern int message_aget_attachment_name __P ((message_t, char **name)); |
126 | 127 | ||
128 | extern int message_save_to_mailbox __P ((message_t msg, ticket_t ticket, | ||
129 | mu_debug_t debug, const char *toname)); | ||
130 | |||
131 | |||
127 | #ifdef __cplusplus | 132 | #ifdef __cplusplus |
128 | } | 133 | } |
129 | #endif | 134 | #endif | ... | ... |
... | @@ -1004,3 +1004,79 @@ message_body_read (stream_t stream, char *buffer, size_t n, off_t off, | ... | @@ -1004,3 +1004,79 @@ message_body_read (stream_t stream, char *buffer, size_t n, off_t off, |
1004 | *pn = nread; | 1004 | *pn = nread; |
1005 | return status; | 1005 | return status; |
1006 | } | 1006 | } |
1007 | |||
1008 | int | ||
1009 | message_save_to_mailbox (message_t msg, ticket_t ticket, mu_debug_t debug, | ||
1010 | const char *toname) | ||
1011 | { | ||
1012 | int rc = 0; | ||
1013 | mailbox_t to = 0; | ||
1014 | |||
1015 | if ((rc = mailbox_create_default (&to, toname))) | ||
1016 | { | ||
1017 | mu_debug_print (debug, MU_DEBUG_TRACE, | ||
1018 | "mailbox_create_default (%s) failed: %s\n", toname, | ||
1019 | strerror (rc)); | ||
1020 | goto end; | ||
1021 | } | ||
1022 | |||
1023 | if (debug && (rc = mailbox_set_debug (to, debug))) | ||
1024 | goto end; | ||
1025 | |||
1026 | if (ticket) | ||
1027 | { | ||
1028 | folder_t folder = NULL; | ||
1029 | |||
1030 | if ((rc = mailbox_get_folder (to, &folder))) | ||
1031 | goto end; | ||
1032 | |||
1033 | /* FIXME: not all mailboxes have folders, thus this hack. */ | ||
1034 | if (folder) | ||
1035 | { | ||
1036 | authority_t auth = NULL; | ||
1037 | if ((rc = folder_get_authority (folder, &auth))) | ||
1038 | goto end; | ||
1039 | |||
1040 | /* FIXME: not all folders have authentication, thus this hack. */ | ||
1041 | if (auth && (rc = authority_set_ticket (auth, ticket))) | ||
1042 | goto end; | ||
1043 | } | ||
1044 | } | ||
1045 | |||
1046 | if ((rc = mailbox_open (to, MU_STREAM_WRITE | MU_STREAM_CREAT))) | ||
1047 | { | ||
1048 | mu_debug_print (debug, MU_DEBUG_TRACE, | ||
1049 | "mailbox_open (%s) failed: %s\n", toname, | ||
1050 | strerror (rc)); | ||
1051 | goto end; | ||
1052 | } | ||
1053 | |||
1054 | if ((rc = mailbox_append_message (to, msg))) | ||
1055 | { | ||
1056 | mu_debug_print (debug, MU_DEBUG_TRACE, | ||
1057 | "mailbox_append_message (%s) failed: %s\n", toname, | ||
1058 | strerror (rc)); | ||
1059 | goto end; | ||
1060 | } | ||
1061 | |||
1062 | end: | ||
1063 | |||
1064 | if (!rc) | ||
1065 | { | ||
1066 | if ((rc = mailbox_close (to))) | ||
1067 | { | ||
1068 | mu_debug_print (debug, MU_DEBUG_TRACE, | ||
1069 | "mailbox_close (%s) failed: %s\n", toname, | ||
1070 | strerror (rc)); | ||
1071 | } | ||
1072 | } | ||
1073 | else | ||
1074 | { | ||
1075 | mailbox_close (to); | ||
1076 | } | ||
1077 | |||
1078 | mailbox_destroy (&to); | ||
1079 | |||
1080 | return rc; | ||
1081 | } | ||
1082 | ... | ... |
-
Please register or sign in to post a comment