NeoMutt  2019-11-11
Teaching an old dog new tricks
DOXYGEN
mailcap.h File Reference

RFC1524 Mailcap routines. More...

#include <stdbool.h>
+ Include dependency graph for mailcap.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  MailcapEntry
 A mailcap entry. More...
 

Enumerations

enum  MailcapLookup {
  MUTT_MC_NO_FLAGS = 0, MUTT_MC_EDIT, MUTT_MC_COMPOSE, MUTT_MC_PRINT,
  MUTT_MC_AUTOVIEW
}
 Mailcap actions. More...
 

Functions

void mailcap_entry_free (struct MailcapEntry **ptr)
 Deallocate an struct MailcapEntry. More...
 
struct MailcapEntrymailcap_entry_new (void)
 Allocate memory for a new rfc1524 entry. More...
 
int mailcap_expand_command (struct Body *a, const char *filename, const char *type, struct Buffer *command)
 Expand expandos in a command. More...
 
void mailcap_expand_filename (const char *nametemplate, const char *oldfile, struct Buffer *newfile)
 Expand a new filename from a template or existing filename. More...
 
bool mailcap_lookup (struct Body *a, char *type, size_t typelen, struct MailcapEntry *entry, enum MailcapLookup opt)
 Find given type in the list of mailcap files. More...
 

Variables

bool C_MailcapSanitize
 Config: Restrict the possible characters in mailcap expandos. More...
 

Detailed Description

RFC1524 Mailcap 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 mailcap.h.

Enumeration Type Documentation

◆ MailcapLookup

Mailcap actions.

Enumerator
MUTT_MC_NO_FLAGS 

No flags set.

MUTT_MC_EDIT 

Mailcap edit field.

MUTT_MC_COMPOSE 

Mailcap compose field.

MUTT_MC_PRINT 

Mailcap print field.

MUTT_MC_AUTOVIEW 

Mailcap autoview field.

Definition at line 55 of file mailcap.h.

56 {
57  MUTT_MC_NO_FLAGS = 0,
58  MUTT_MC_EDIT,
62 };
Mailcap autoview field.
Definition: mailcap.h:61
No flags set.
Definition: mailcap.h:57
Mailcap compose field.
Definition: mailcap.h:59
Mailcap print field.
Definition: mailcap.h:60
Mailcap edit field.
Definition: mailcap.h:58

Function Documentation

◆ mailcap_entry_free()

void mailcap_entry_free ( struct MailcapEntry **  ptr)

Deallocate an struct MailcapEntry.

Parameters
[out]ptrMailcapEntry to deallocate

Definition at line 438 of file mailcap.c.

439 {
440  if (!ptr || !*ptr)
441  return;
442 
443  struct MailcapEntry *me = *ptr;
444 
445  FREE(&me->command);
446  FREE(&me->testcommand);
447  FREE(&me->composecommand);
448  FREE(&me->composetypecommand);
449  FREE(&me->editcommand);
450  FREE(&me->printcommand);
451  FREE(&me->nametemplate);
452  FREE(ptr);
453 }
A mailcap entry.
Definition: mailcap.h:37
char * composetypecommand
Definition: mailcap.h:42
char * editcommand
Definition: mailcap.h:43
char * command
Definition: mailcap.h:39
char * printcommand
Definition: mailcap.h:44
char * nametemplate
Definition: mailcap.h:45
char * composecommand
Definition: mailcap.h:41
#define FREE(x)
Definition: memory.h:40
char * testcommand
Definition: mailcap.h:40
+ Here is the caller graph for this function:

◆ mailcap_entry_new()

struct MailcapEntry* mailcap_entry_new ( void  )

Allocate memory for a new rfc1524 entry.

Return values
ptrAn un-initialized struct MailcapEntry

Definition at line 429 of file mailcap.c.

430 {
431  return mutt_mem_calloc(1, sizeof(struct MailcapEntry));
432 }
A mailcap entry.
Definition: mailcap.h:37
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailcap_expand_command()

int mailcap_expand_command ( struct Body a,
const char *  filename,
const char *  type,
struct Buffer command 
)

Expand expandos in a command.

Parameters
aEmail Body
filenameFile containing the email text
typeType, e.g. "text/plain"
commandBuffer containing command
Return values
0Command works on a file
1Command works on a pipe

The command semantics include the following: s is the filename that contains the mail body data t is the content type, like text/plain %{parameter} is replaced by the parameter value from the content-type field % is % Unsupported rfc1524 parameters: these would probably require some doing by neomutt, and can probably just be done by piping the message to metamail n is the integer number of sub-parts in the multipart F is "content-type filename" repeated for each sub-part

Definition at line 70 of file mailcap.c.

72 {
73  int needspipe = true;
74  struct Buffer *buf = mutt_buffer_pool_get();
75  struct Buffer *quoted = mutt_buffer_pool_get();
76  struct Buffer *param = NULL;
77  struct Buffer *type2 = NULL;
78 
79  const char *cptr = mutt_b2s(command);
80  while (*cptr)
81  {
82  if (*cptr == '\\')
83  {
84  cptr++;
85  if (*cptr)
86  mutt_buffer_addch(buf, *cptr++);
87  }
88  else if (*cptr == '%')
89  {
90  cptr++;
91  if (*cptr == '{')
92  {
93  const char *pvalue2 = NULL;
94 
95  if (param)
96  mutt_buffer_reset(param);
97  else
98  param = mutt_buffer_pool_get();
99 
100  /* Copy parameter name into param buffer */
101  cptr++;
102  while (*cptr && (*cptr != '}'))
103  mutt_buffer_addch(param, *cptr++);
104 
105  /* In send mode, use the current charset, since the message hasn't
106  * been converted yet. If noconv is set, then we assume the
107  * charset parameter has the correct value instead. */
108  if ((mutt_str_strcasecmp(mutt_b2s(param), "charset") == 0) && a->charset && !a->noconv)
109  pvalue2 = a->charset;
110  else
111  pvalue2 = mutt_param_get(&a->parameter, mutt_b2s(param));
112 
113  /* Now copy the parameter value into param buffer */
114  if (C_MailcapSanitize)
115  mutt_buffer_sanitize_filename(param, NONULL(pvalue2), false);
116  else
117  mutt_buffer_strcpy(param, NONULL(pvalue2));
118 
119  mutt_buffer_quote_filename(quoted, mutt_b2s(param), true);
120  mutt_buffer_addstr(buf, mutt_b2s(quoted));
121  }
122  else if ((*cptr == 's') && filename)
123  {
124  mutt_buffer_quote_filename(quoted, filename, true);
125  mutt_buffer_addstr(buf, mutt_b2s(quoted));
126  needspipe = false;
127  }
128  else if (*cptr == 't')
129  {
130  if (!type2)
131  {
132  type2 = mutt_buffer_pool_get();
133  if (C_MailcapSanitize)
134  mutt_buffer_sanitize_filename(type2, type, false);
135  else
136  mutt_buffer_strcpy(type2, type);
137  }
138  mutt_buffer_quote_filename(quoted, mutt_b2s(type2), true);
139  mutt_buffer_addstr(buf, mutt_b2s(quoted));
140  }
141 
142  if (*cptr)
143  cptr++;
144  }
145  else
146  mutt_buffer_addch(buf, *cptr++);
147  }
148  mutt_buffer_copy(command, buf);
149 
151  mutt_buffer_pool_release(&quoted);
152  mutt_buffer_pool_release(&param);
153  mutt_buffer_pool_release(&type2);
154 
155  return needspipe;
156 }
void mutt_buffer_quote_filename(struct Buffer *buf, const char *filename, bool add_outer)
Quote a filename to survive the shell&#39;s quoting rules.
Definition: file.c:834
#define NONULL(x)
Definition: string2.h:37
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:100
size_t mutt_buffer_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer&#39;s contents to another Buffer.
Definition: buffer.c:445
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
bool noconv
Don&#39;t do character set conversion.
Definition: body.h:73
String manipulation buffer.
Definition: buffer.h:33
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
#define mutt_b2s(buf)
Definition: buffer.h:41
char * charset
Send mode: charset of attached file as stored on disk.
Definition: body.h:49
bool C_MailcapSanitize
Config: Restrict the possible characters in mailcap expandos.
Definition: mailcap.c:49
size_t mutt_buffer_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:240
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
void mutt_buffer_sanitize_filename(struct Buffer *buf, const char *path, short slash)
Replace unsafe characters in a filename.
Definition: muttlib.c:1757
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:628
char * mutt_param_get(const struct ParameterList *pl, const char *s)
Find a matching Parameter.
Definition: parameter.c:84
struct ParameterList parameter
parameters of the content-type
Definition: body.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailcap_expand_filename()

void mailcap_expand_filename ( const char *  nametemplate,
const char *  oldfile,
struct Buffer newfile 
)

Expand a new filename from a template or existing filename.

Parameters
nametemplateTemplate
oldfileOriginal filename
newfileBuffer for new filename

If there is no nametemplate, the stripped oldfile name is used as the template for newfile.

If there is no oldfile, the stripped nametemplate name is used as the template for newfile.

If both a nametemplate and oldfile are specified, the template is checked for a "%s". If none is found, the nametemplate is used as the template for newfile. The first path component of the nametemplate and oldfile are ignored.

Definition at line 522 of file mailcap.c.

524 {
525  int i, j, k;
526  char *s = NULL;
527  bool lmatch = false, rmatch = false;
528 
529  mutt_buffer_reset(newfile);
530 
531  /* first, ignore leading path components */
532 
533  if (nametemplate && (s = strrchr(nametemplate, '/')))
534  nametemplate = s + 1;
535 
536  if (oldfile && (s = strrchr(oldfile, '/')))
537  oldfile = s + 1;
538 
539  if (!nametemplate)
540  {
541  if (oldfile)
542  mutt_buffer_strcpy(newfile, oldfile);
543  }
544  else if (!oldfile)
545  {
546  mutt_file_expand_fmt(newfile, nametemplate, "neomutt");
547  }
548  else /* oldfile && nametemplate */
549  {
550  /* first, compare everything left from the "%s"
551  * (if there is one). */
552 
553  lmatch = true;
554  bool ps = false;
555  for (i = 0; nametemplate[i]; i++)
556  {
557  if ((nametemplate[i] == '%') && (nametemplate[i + 1] == 's'))
558  {
559  ps = true;
560  break;
561  }
562 
563  /* note that the following will _not_ read beyond oldfile's end. */
564 
565  if (lmatch && (nametemplate[i] != oldfile[i]))
566  lmatch = false;
567  }
568 
569  if (ps)
570  {
571  /* If we had a "%s", check the rest. */
572 
573  /* now, for the right part: compare everything right from
574  * the "%s" to the final part of oldfile.
575  *
576  * The logic here is as follows:
577  *
578  * - We start reading from the end.
579  * - There must be a match _right_ from the "%s",
580  * thus the i + 2.
581  * - If there was a left hand match, this stuff
582  * must not be counted again. That's done by the
583  * condition (j >= (lmatch ? i : 0)). */
584 
585  rmatch = true;
586 
587  for (j = mutt_str_strlen(oldfile) - 1, k = mutt_str_strlen(nametemplate) - 1;
588  (j >= (lmatch ? i : 0)) && (k >= (i + 2)); j--, k--)
589  {
590  if (nametemplate[k] != oldfile[j])
591  {
592  rmatch = false;
593  break;
594  }
595  }
596 
597  /* Now, check if we had a full match. */
598 
599  if (k >= i + 2)
600  rmatch = false;
601 
602  struct Buffer *left = mutt_buffer_pool_get();
603  struct Buffer *right = mutt_buffer_pool_get();
604 
605  if (!lmatch)
606  mutt_buffer_strcpy_n(left, nametemplate, i);
607  if (!rmatch)
608  mutt_buffer_strcpy(right, nametemplate + i + 2);
609  mutt_buffer_printf(newfile, "%s%s%s", mutt_b2s(left), oldfile, mutt_b2s(right));
610 
612  mutt_buffer_pool_release(&right);
613  }
614  else
615  {
616  /* no "%s" in the name template. */
617  mutt_buffer_strcpy(newfile, nametemplate);
618  }
619  }
620 
621  mutt_adv_mktemp(newfile);
622 }
void mutt_buffer_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:79
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:100
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
String manipulation buffer.
Definition: buffer.h:33
size_t mutt_str_strlen(const char *a)
Calculate the length of a string, safely.
Definition: string.c:666
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:160
#define mutt_b2s(buf)
Definition: buffer.h:41
size_t mutt_buffer_strcpy_n(struct Buffer *buf, const char *s, size_t len)
Copy a string into a Buffer.
Definition: buffer.c:327
void mutt_file_expand_fmt(struct Buffer *dest, const char *fmt, const char *src)
Replace s in a string with a filename.
Definition: file.c:1434
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
void mutt_adv_mktemp(struct Buffer *buf)
Create a temporary file.
Definition: muttlib.c:96
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailcap_lookup()

bool mailcap_lookup ( struct Body a,
char *  type,
size_t  typelen,
struct MailcapEntry entry,
enum MailcapLookup  opt 
)

Find given type in the list of mailcap files.

Parameters
aMessage body
typeText type in "type/subtype" format
typelenLength of the type
entrystruct MailcapEntry to populate with results
optType of mailcap entry to lookup, see MailcapLookup
Return values
trueIf *entry is not NULL it populates it with the mailcap entry
falseNo matching entry is found

Find the given type in the list of mailcap files.

Definition at line 467 of file mailcap.c.

469 {
470  /* rfc1524 specifies that a path of mailcap files should be searched.
471  * joy. They say
472  * $HOME/.mailcap:/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap, etc
473  * and overridden by the MAILCAPS environment variable, and, just to be nice,
474  * we'll make it specifiable in .neomuttrc */
475  if (!C_MailcapPath || (C_MailcapPath->count == 0))
476  {
477  mutt_error(_("No mailcap path specified"));
478  return false;
479  }
480 
481  mutt_check_lookup_list(a, type, typelen);
482 
483  struct Buffer *path = mutt_buffer_pool_get();
484  bool found = false;
485 
486  struct ListNode *np = NULL;
487  STAILQ_FOREACH(np, &C_MailcapPath->head, entries)
488  {
489  mutt_buffer_strcpy(path, np->data);
491 
492  mutt_debug(LL_DEBUG2, "Checking mailcap file: %s\n", mutt_b2s(path));
493  found = rfc1524_mailcap_parse(a, mutt_b2s(path), type, entry, opt);
494  if (found)
495  break;
496  }
497 
499 
500  if (entry && !found)
501  mutt_error(_("mailcap entry for type %s not found"), type);
502 
503  return found;
504 }
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:100
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:111
String manipulation buffer.
Definition: buffer.h:33
#define _(a)
Definition: message.h:28
Log at debug level 2.
Definition: logging.h:57
struct ListHead head
Definition: slist.h:45
size_t count
Definition: slist.h:46
#define mutt_b2s(buf)
Definition: buffer.h:41
WHERE struct Slist * C_MailcapPath
Config: Colon-separated list of mailcap files.
Definition: globals.h:120
static bool rfc1524_mailcap_parse(struct Body *a, const char *filename, const char *type, struct MailcapEntry *entry, enum MailcapLookup opt)
Parse a mailcap entry.
Definition: mailcap.c:232
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:312
char * data
String.
Definition: list.h:35
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:343
void mutt_check_lookup_list(struct Body *b, char *type, size_t len)
Update the mime type.
Definition: mutt_attach.c:330
#define mutt_error(...)
Definition: logging.h:84
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
A List node for strings.
Definition: list.h:33
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ C_MailcapSanitize

bool C_MailcapSanitize

Config: Restrict the possible characters in mailcap expandos.

Definition at line 49 of file mailcap.c.