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