NeoMutt  2020-08-07-1-gab41a1
Teaching an old dog new tricks
DOXYGEN
multipart.c
Go to the documentation of this file.
1 
30 #include "config.h"
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include "mutt/lib.h"
34 #include "email/lib.h"
35 #include "sendlib.h"
36 
42 static int get_toplevel_encoding(struct Body *a)
43 {
44  int e = ENC_7BIT;
45 
46  for (; a; a = a->next)
47  {
48  if (a->encoding == ENC_BINARY)
49  return ENC_BINARY;
50  if (a->encoding == ENC_8BIT)
51  e = ENC_8BIT;
52  }
53 
54  return e;
55 }
56 
63 static bool check_boundary(const char *boundary, struct Body *b)
64 {
65  char *p = NULL;
66 
67  if (b->parts && check_boundary(boundary, b->parts))
68  return true;
69 
70  if (b->next && check_boundary(boundary, b->next))
71  return true;
72 
73  p = mutt_param_get(&b->parameter, "boundary");
74  if (p && mutt_str_equal(p, boundary))
75  {
76  return true;
77  }
78  return false;
79 }
80 
85 void mutt_generate_boundary(struct ParameterList *pl)
86 {
87  char rs[MUTT_RANDTAG_LEN + 1];
88 
89  mutt_rand_base32(rs, sizeof(rs) - 1);
90  rs[MUTT_RANDTAG_LEN] = 0;
91  mutt_param_set(pl, "boundary", rs);
92 }
93 
99 struct Body *mutt_make_multipart(struct Body *b)
100 {
101  struct Body *new_body = mutt_body_new();
102  new_body->type = TYPE_MULTIPART;
103  new_body->subtype = mutt_str_dup("mixed");
104  new_body->encoding = get_toplevel_encoding(b);
105  do
106  {
107  mutt_generate_boundary(&new_body->parameter);
108  if (check_boundary(mutt_param_get(&new_body->parameter, "boundary"), b))
109  mutt_param_delete(&new_body->parameter, "boundary");
110  } while (!mutt_param_get(&new_body->parameter, "boundary"));
111  new_body->use_disp = false;
112  new_body->disposition = DISP_INLINE;
113  new_body->parts = b;
114 
115  return new_body;
116 }
117 
125 struct Body *mutt_remove_multipart(struct Body *b)
126 {
127  struct Body *t = NULL;
128 
129  if (b->parts)
130  {
131  t = b;
132  b = b->parts;
133  t->parts = NULL;
134  mutt_body_free(&t);
135  }
136  return b;
137 }
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:876
Structs that make up an email.
7-bit text
Definition: mime.h:49
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:375
struct Body * next
next attachment in the list
Definition: body.h:53
8-bit text
Definition: mime.h:50
The body of an email.
Definition: body.h:34
unsigned int disposition
content-disposition, ContentDisposition
Definition: body.h:67
#define MUTT_RANDTAG_LEN
Definition: sendlib.h:35
struct Body * mutt_body_new(void)
Create a new Body.
Definition: body.c:43
static int get_toplevel_encoding(struct Body *a)
Find the most restrictive encoding type.
Definition: multipart.c:42
void mutt_generate_boundary(struct ParameterList *pl)
Create a unique boundary id for a MIME part.
Definition: multipart.c:85
unsigned int encoding
content-transfer-encoding, ContentEncoding
Definition: body.h:66
struct Body * mutt_make_multipart(struct Body *b)
Create a multipart email.
Definition: multipart.c:99
char * subtype
content-type subtype
Definition: body.h:37
void mutt_param_delete(struct ParameterList *pl, const char *attribute)
Delete a matching Parameter.
Definition: parameter.c:142
static bool check_boundary(const char *boundary, struct Body *b)
check for duplicate boundary
Definition: multipart.c:63
struct Body * mutt_remove_multipart(struct Body *b)
Extract the multipart body if it exists.
Definition: multipart.c:125
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:54
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
unsigned int type
content-type primary type, ContentType
Definition: body.h:65
Type: &#39;multipart/*&#39;.
Definition: mime.h:37
bool use_disp
Content-Disposition uses filename= ?
Definition: body.h:68
void mutt_rand_base32(char *buf, size_t buflen)
Fill a buffer with a base32-encoded random string.
Definition: random.c:101
Binary.
Definition: mime.h:53
char * mutt_param_get(const struct ParameterList *pl, const char *s)
Find a matching Parameter.
Definition: parameter.c:84
Miscellaneous functions for sending an email.
Convenience wrapper for the library headers.
Content is inline.
Definition: mime.h:62
struct ParameterList parameter
parameters of the content-type
Definition: body.h:39
void mutt_param_set(struct ParameterList *pl, const char *attribute, const char *value)
Set a Parameter.
Definition: parameter.c:110