NeoMutt  2018-07-16 +2388-bcedc8
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 *e)
 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 41 of file mutt_parse.c.

Function Documentation

void mutt_parse_mime_message ( struct Mailbox m,
struct Email e 
)

Parse a MIME email.

Parameters
mMailbox
eEmail

Definition at line 48 of file mutt_parse.c.

49 {
50  do
51  {
52  if ((e->content->type != TYPE_MESSAGE) && (e->content->type != TYPE_MULTIPART))
53  break; /* nothing to do */
54 
55  if (e->content->parts)
56  break; /* The message was parsed earlier. */
57 
58  struct Message *msg = mx_msg_open(m, e->msgno);
59  if (msg)
60  {
61  mutt_parse_part(msg->fp, e->content);
62 
63  if (WithCrypto)
64  e->security = crypt_query(e->content);
65 
66  mx_msg_close(m, &msg);
67  }
68  } while (false);
69 
70  e->attach_valid = false;
71 }
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:1089
A local copy of an email.
Definition: mx.h:81
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:54
bool attach_valid
true when the attachment count is valid
Definition: email.h:72
SecurityFlags security
bit 0-10: flags, bit 11,12: application, bit 13: traditional pgp See: ncrypt/ncrypt.h pgplib.h, smime.h
Definition: email.h:41
unsigned int type
content-type primary type
Definition: body.h:65
Type: &#39;message/*&#39;.
Definition: mime.h:35
void mutt_parse_part(FILE *fp, struct Body *b)
Parse a MIME part.
Definition: parse.c:1400
Type: &#39;multipart/*&#39;.
Definition: mime.h:37
FILE * fp
pointer to the message data
Definition: mx.h:83
#define WithCrypto
Definition: ncrypt.h:160
SecurityFlags crypt_query(struct Body *m)
Check out the type of encryption used.
Definition: crypt.c:696
struct Message * mx_msg_open(struct Mailbox *m, int msgno)
return a stream pointer for a message
Definition: mx.c:1041
int msgno
Number displayed to the user.
Definition: email.h:88

+ 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 80 of file mutt_parse.c.

81 {
82  struct AttachMatch *a = NULL;
83 
84  /* If list is null, use default behavior. */
85  if (!checklist || STAILQ_EMPTY(checklist))
86  {
87  return false;
88  }
89 
90  struct ListNode *np = NULL;
91  STAILQ_FOREACH(np, checklist, entries)
92  {
93  a = (struct AttachMatch *) np->data;
94  mutt_debug(LL_DEBUG3, "%s %d/%s ?? %s/%s [%d]... ", dflt ? "[OK] " : "[EXCL] ",
95  b->type, b->subtype ? b->subtype : "*", a->major, a->minor, a->major_int);
96  if (((a->major_int == TYPE_ANY) || (a->major_int == b->type)) &&
97  (!b->subtype || !regexec(&a->minor_regex, b->subtype, 0, NULL, 0)))
98  {
99  mutt_debug(LL_DEBUG3, "yes\n");
100  return true;
101  }
102  else
103  {
104  mutt_debug(LL_DEBUG3, "no\n");
105  }
106  }
107 
108  return false;
109 }
enum ContentType major_int
Definition: mutt.h:133
Type: &#39;*&#39; or &#39;.*&#39;.
Definition: mime.h:40
const char * major
Definition: mutt.h:132
char * subtype
content-type subtype
Definition: body.h:37
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
unsigned int type
content-type primary type
Definition: body.h:65
char * data
Definition: list.h:35
regex_t minor_regex
Definition: mutt.h:135
#define STAILQ_EMPTY(head)
Definition: queue.h:346
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
An attachment matching a regex.
Definition: mutt.h:130
A List node for strings.
Definition: list.h:33
const char * minor
Definition: mutt.h:134
Log at debug level 3.
Definition: logging.h:58

+ 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 117 of file mutt_parse.c.

118 {
119  if (!body)
120  return 0;
121 
122  int count = 0;
123 
124  for (struct Body *bp = body; bp; bp = bp->next)
125  {
126  /* Initial disposition is to count and not to recurse this part. */
127  bool shallcount = true; /* default */
128  bool shallrecurse = false;
129 
130  mutt_debug(LL_DEBUG5, "desc=\"%s\"; fn=\"%s\", type=\"%d/%s\"\n",
131  bp->description ? bp->description : ("none"),
132  bp->filename ? bp->filename : bp->d_filename ? bp->d_filename : "(none)",
133  bp->type, bp->subtype ? bp->subtype : "*");
134 
135  if (bp->type == TYPE_MESSAGE)
136  {
137  shallrecurse = true;
138 
139  /* If it's an external body pointer, don't recurse it. */
140  if (mutt_str_strcasecmp(bp->subtype, "external-body") == 0)
141  shallrecurse = false;
142 
143  /* Don't count containers if they're top-level. */
144  if (flags & MUTT_PARTS_TOPLEVEL)
145  shallcount = false; // top-level message/*
146  }
147  else if (bp->type == TYPE_MULTIPART)
148  {
149  /* Always recurse multiparts, except multipart/alternative. */
150  shallrecurse = true;
151  if (mutt_str_strcasecmp(bp->subtype, "alternative") == 0)
152  shallrecurse = false;
153 
154  /* Don't count containers if they're top-level. */
155  if (flags & MUTT_PARTS_TOPLEVEL)
156  shallcount = false; /* top-level multipart */
157  }
158 
159  if ((bp->disposition == DISP_INLINE) && (bp->type != TYPE_MULTIPART) &&
160  (bp->type != TYPE_MESSAGE) && (bp == body))
161  {
162  shallcount = false; /* ignore fundamental inlines */
163  }
164 
165  /* If this body isn't scheduled for enumeration already, don't bother
166  * profiling it further. */
167  if (shallcount)
168  {
169  /* Turn off shallcount if message type is not in ok list,
170  * or if it is in except list. Check is done separately for
171  * inlines vs. attachments. */
172 
173  if (bp->disposition == DISP_ATTACH)
174  {
175  if (!count_body_parts_check(&AttachAllow, bp, true))
176  shallcount = false; /* attach not allowed */
177  if (count_body_parts_check(&AttachExclude, bp, false))
178  shallcount = false; /* attach excluded */
179  }
180  else
181  {
182  if (!count_body_parts_check(&InlineAllow, bp, true))
183  shallcount = false; /* inline not allowed */
184  if (count_body_parts_check(&InlineExclude, bp, false))
185  shallcount = false; /* excluded */
186  }
187  }
188 
189  if (shallcount)
190  count++;
191  bp->attach_qualifies = shallcount;
192 
193  mutt_debug(LL_DEBUG3, "%p shallcount = %d\n", (void *) bp, shallcount);
194 
195  if (shallrecurse)
196  {
197  mutt_debug(LL_DEBUG3, "%p pre count = %d\n", (void *) bp, count);
198  bp->attach_count = count_body_parts(bp->parts, flags & ~MUTT_PARTS_TOPLEVEL);
199  count += bp->attach_count;
200  mutt_debug(LL_DEBUG3, "%p post count = %d\n", (void *) bp, count);
201  }
202  }
203 
204  mutt_debug(LL_DEBUG3, "return %d\n", (count < 0) ? 0 : count);
205  return (count < 0) ? 0 : count;
206 }
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:80
#define MUTT_PARTS_TOPLEVEL
Definition: mutt_parse.c:41
struct Body * next
next attachment in the list
Definition: body.h:53
The body of an email.
Definition: body.h:34
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:117
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:628
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
Log at debug level 5.
Definition: logging.h:60
Content is inline.
Definition: mime.h:62
Log at debug level 3.
Definition: logging.h:58

+ 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 214 of file mutt_parse.c.

215 {
216  bool keep_parts = false;
217 
218  if (e->attach_valid)
219  return e->attach_total;
220 
221  if (e->content->parts)
222  keep_parts = true;
223  else
225 
226  if (!STAILQ_EMPTY(&AttachAllow) || !STAILQ_EMPTY(&AttachExclude) ||
227  !STAILQ_EMPTY(&InlineAllow) || !STAILQ_EMPTY(&InlineExclude))
228  {
230  }
231  else
232  e->attach_total = 0;
233 
234  e->attach_valid = true;
235 
236  if (!keep_parts)
238 
239  return e->attach_total;
240 }
struct Body * content
List of MIME parts.
Definition: email.h:92
void mutt_parse_mime_message(struct Mailbox *m, struct Email *e)
Parse a MIME email.
Definition: mutt_parse.c:48
#define MUTT_PARTS_TOPLEVEL
Definition: mutt_parse.c:41
short attach_total
Number of qualifying attachments in message, if attach_valid.
Definition: email.h:98
static int count_body_parts(struct Body *body, int flags)
Count the MIME Body parts.
Definition: mutt_parse.c:117
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:54
bool attach_valid
true when the attachment count is valid
Definition: email.h:72
void mutt_body_free(struct Body **p)
Free a Body.
Definition: body.c:57
#define STAILQ_EMPTY(head)
Definition: queue.h:346

+ Here is the caller graph for this function: