NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
mutt_parse.c File Reference

Miscellaneous email parsing routines. More...

#include "config.h"
#include <regex.h>
#include <stdbool.h>
#include <stdio.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "mutt.h"
#include "mutt_parse.h"
#include "globals.h"
#include "mx.h"
#include "ncrypt/ncrypt.h"
+ Include dependency graph for mutt_parse.c:

Go to the source code of this file.

Macros

#define MUTT_PARTS_TOPLEVEL   (1 << 0) /* is the top-level part */
 

Functions

void mutt_parse_mime_message (struct Mailbox *m, struct Email *cur)
 Parse a MIME email. More...
 
static bool count_body_parts_check (struct ListHead *checklist, struct Body *b, bool dflt)
 Compares mime types to the ok and except lists. More...
 
static int count_body_parts (struct Body *body, int flags)
 Count the MIME Body parts. More...
 
int mutt_count_body_parts (struct Mailbox *m, struct Email *e)
 Count the MIME Body parts. More...
 

Detailed Description

Miscellaneous email parsing routines.

Authors
  • Michael R. Elkins

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file mutt_parse.c.

Macro Definition Documentation

#define MUTT_PARTS_TOPLEVEL   (1 << 0) /* is the top-level part */

Definition at line 43 of file mutt_parse.c.

Function Documentation

void mutt_parse_mime_message ( struct Mailbox m,
struct Email cur 
)

Parse a MIME email.

Parameters
mMailbox
curEmail

Definition at line 50 of file mutt_parse.c.

51 {
52  struct Message *msg = NULL;
53 
54  do
55  {
56  if (cur->content->type != TYPE_MESSAGE && cur->content->type != TYPE_MULTIPART)
57  break; /* nothing to do */
58 
59  if (cur->content->parts)
60  break; /* The message was parsed earlier. */
61 
62  msg = mx_msg_open(m, cur->msgno);
63  if (msg)
64  {
65  mutt_parse_part(msg->fp, cur->content);
66 
67  if (WithCrypto)
68  cur->security = crypt_query(cur->content);
69 
70  mx_msg_close(m, &msg);
71  }
72  } while (false);
73 
74  cur->attach_valid = false;
75 }
unsigned int security
bit 0-8: flags, bit 9,10: application.
Definition: email.h:37
struct Body * content
list of MIME parts
Definition: email.h:92
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1187
A local copy of an email.
Definition: mx.h:81
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:58
bool attach_valid
Definition: email.h:72
unsigned int type
content-type primary type
Definition: body.h:67
Type: &#39;message/*&#39;.
Definition: mime.h:35
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition: parse.c:1229
Type: &#39;multipart/*&#39;.
Definition: mime.h:37
int crypt_query(struct Body *m)
Check out the type of encryption used.
Definition: crypt.c:647
FILE * fp
pointer to the message data
Definition: mx.h:83
#define WithCrypto
Definition: ncrypt.h:154
struct Message * mx_msg_open(struct Mailbox *m, int msgno)
return a stream pointer for a message
Definition: mx.c:1139
int msgno
number displayed to the user
Definition: email.h:88

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool count_body_parts_check ( struct ListHead *  checklist,
struct Body b,
bool  dflt 
)
static

Compares mime types to the ok and except lists.

Parameters
checklistList of AttachMatch
bEmail Body
dfltLog whether the matches are OK, or Excluded
Return values
trueAttachment should be counted

Definition at line 84 of file mutt_parse.c.

85 {
86  struct AttachMatch *a = NULL;
87 
88  /* If list is null, use default behavior. */
89  if (!checklist || STAILQ_EMPTY(checklist))
90  {
91  return false;
92  }
93 
94  struct ListNode *np = NULL;
95  STAILQ_FOREACH(np, checklist, entries)
96  {
97  a = (struct AttachMatch *) np->data;
98  mutt_debug(5, "%s %d/%s ?? %s/%s [%d]... ", dflt ? "[OK] " : "[EXCL] ", b->type,
99  b->subtype ? b->subtype : "*", a->major, a->minor, a->major_int);
100  if ((a->major_int == TYPE_ANY || a->major_int == b->type) &&
101  (!b->subtype || !regexec(&a->minor_regex, b->subtype, 0, NULL, 0)))
102  {
103  mutt_debug(5, "yes\n");
104  return true;
105  }
106  else
107  {
108  mutt_debug(5, "no\n");
109  }
110  }
111 
112  return false;
113 }
int major_int
Definition: mutt.h:200
Type: &#39;*&#39; or &#39;.*&#39;.
Definition: mime.h:40
const char * major
Definition: mutt.h:199
char * subtype
content-type subtype
Definition: body.h:36
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
unsigned int type
content-type primary type
Definition: body.h:67
char * data
Definition: list.h:35
regex_t minor_regex
Definition: mutt.h:202
#define STAILQ_EMPTY(head)
Definition: queue.h:346
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
An attachment matching a regex.
Definition: mutt.h:197
A List node for strings.
Definition: list.h:33
const char * minor
Definition: mutt.h:201

+ Here is the caller graph for this function:

static int count_body_parts ( struct Body body,
int  flags 
)
static

Count the MIME Body parts.

Parameters
bodyBody of email
flagsFlags, e.g. MUTT_PARTS_TOPLEVEL
Return values
numNumber of MIME Body parts

Definition at line 121 of file mutt_parse.c.

122 {
123  int count = 0;
124 
125  if (!body)
126  return 0;
127 
128  for (struct Body *bp = body; bp; bp = bp->next)
129  {
130  /* Initial disposition is to count and not to recurse this part. */
131  bool shallcount = true; /* default */
132  bool shallrecurse = false;
133 
134  mutt_debug(5, "desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
135  bp->description ? bp->description : ("none"),
136  bp->filename ? bp->filename : bp->d_filename ? bp->d_filename : "(none)",
137  bp->type, bp->subtype ? bp->subtype : "*");
138 
139  if (bp->type == TYPE_MESSAGE)
140  {
141  shallrecurse = true;
142 
143  /* If it's an external body pointer, don't recurse it. */
144  if (mutt_str_strcasecmp(bp->subtype, "external-body") == 0)
145  shallrecurse = false;
146 
147  /* Don't count containers if they're top-level. */
148  if (flags & MUTT_PARTS_TOPLEVEL)
149  shallcount = false; // top-level message/*
150  }
151  else if (bp->type == TYPE_MULTIPART)
152  {
153  /* Always recurse multiparts, except multipart/alternative. */
154  shallrecurse = true;
155  if (mutt_str_strcasecmp(bp->subtype, "alternative") == 0)
156  shallrecurse = false;
157 
158  /* Don't count containers if they're top-level. */
159  if (flags & MUTT_PARTS_TOPLEVEL)
160  shallcount = false; /* top-level multipart */
161  }
162 
163  if (bp->disposition == DISP_INLINE && bp->type != TYPE_MULTIPART &&
164  bp->type != TYPE_MESSAGE && bp == body)
165  {
166  shallcount = false; /* ignore fundamental inlines */
167  }
168 
169  /* If this body isn't scheduled for enumeration already, don't bother
170  * profiling it further.
171  */
172  if (shallcount)
173  {
174  /* Turn off shallcount if message type is not in ok list,
175  * or if it is in except list. Check is done separately for
176  * inlines vs. attachments.
177  */
178 
179  if (bp->disposition == DISP_ATTACH)
180  {
181  if (!count_body_parts_check(&AttachAllow, bp, true))
182  shallcount = false; /* attach not allowed */
183  if (count_body_parts_check(&AttachExclude, bp, false))
184  shallcount = false; /* attach excluded */
185  }
186  else
187  {
188  if (!count_body_parts_check(&InlineAllow, bp, true))
189  shallcount = false; /* inline not allowed */
190  if (count_body_parts_check(&InlineExclude, bp, false))
191  shallcount = false; /* excluded */
192  }
193  }
194 
195  if (shallcount)
196  count++;
197  bp->attach_qualifies = shallcount ? true : false;
198 
199  mutt_debug(5, "%p shallcount = %d\n", (void *) bp, shallcount);
200 
201  if (shallrecurse)
202  {
203  mutt_debug(5, "%p pre count = %d\n", (void *) bp, count);
204  bp->attach_count = count_body_parts(bp->parts, flags & ~MUTT_PARTS_TOPLEVEL);
205  count += bp->attach_count;
206  mutt_debug(5, "%p post count = %d\n", (void *) bp, count);
207  }
208  }
209 
210  mutt_debug(5, "return %d\n", count < 0 ? 0 : count);
211  return (count < 0) ? 0 : count;
212 }
static bool count_body_parts_check(struct ListHead *checklist, struct Body *b, bool dflt)
Compares mime types to the ok and except lists.
Definition: mutt_parse.c:84
#define MUTT_PARTS_TOPLEVEL
Definition: mutt_parse.c:43
struct Body * next
next attachment in the list
Definition: body.h:57
The body of an email.
Definition: body.h:33
Content is attached.
Definition: mime.h:63
static int count_body_parts(struct Body *body, int flags)
Count the MIME Body parts.
Definition: mutt_parse.c:121
Type: &#39;message/*&#39;.
Definition: mime.h:35
Type: &#39;multipart/*&#39;.
Definition: mime.h:37
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:625
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
Content is inline.
Definition: mime.h:62

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_count_body_parts ( struct Mailbox m,
struct Email e 
)

Count the MIME Body parts.

Parameters
mMailbox
eEmail
Return values
numNumber of MIME Body parts

Definition at line 220 of file mutt_parse.c.

221 {
222  bool keep_parts = false;
223 
224  if (e->attach_valid)
225  return e->attach_total;
226 
227  if (e->content->parts)
228  keep_parts = true;
229  else
231 
232  if (!STAILQ_EMPTY(&AttachAllow) || !STAILQ_EMPTY(&AttachExclude) ||
233  !STAILQ_EMPTY(&InlineAllow) || !STAILQ_EMPTY(&InlineExclude))
234  {
236  }
237  else
238  e->attach_total = 0;
239 
240  e->attach_valid = true;
241 
242  if (!keep_parts)
244 
245  return e->attach_total;
246 }
struct Body * content
list of MIME parts
Definition: email.h:92
#define MUTT_PARTS_TOPLEVEL
Definition: mutt_parse.c:43
void mutt_parse_mime_message(struct Mailbox *m, struct Email *cur)
Parse a MIME email.
Definition: mutt_parse.c:50
short attach_total
Definition: email.h:99
static int count_body_parts(struct Body *body, int flags)
Count the MIME Body parts.
Definition: mutt_parse.c:121
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:58
bool attach_valid
Definition: email.h:72
void mutt_body_free(struct Body **p)
Free a Body.
Definition: body.c:56
#define STAILQ_EMPTY(head)
Definition: queue.h:346

+ Here is the call graph for this function:

+ Here is the caller graph for this function: