1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2  *@ MIME types: handlers, `mimetype', etc.
3  *
4  * Copyright (c) 2012 - 2020 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
5  * SPDX-License-Identifier: ISC
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 #ifndef mx_MIME_TYPE_H
20 #define mx_MIME_TYPE_H
21 
22 #include <mx/nail.h>
23 
24 #define mx_HEADER
25 #include <su/code-in.h>
26 
27 struct mx_mimetype_handler;
28 
29 enum mx_mimetype{
30    mx_MIMETYPE_UNKNOWN, /* unknown */
31    mx_MIMETYPE_SUBHDR, /* inside a multipart subheader */
32    mx_MIMETYPE_822, /* message/rfc822 */
33    mx_MIMETYPE_MESSAGE, /* other message/ */
34    mx_MIMETYPE_TEXT_PLAIN, /* text/plain */
35    mx_MIMETYPE_TEXT_HTML, /* text/html */
36    mx_MIMETYPE_TEXT, /* other text/ */
37    mx_MIMETYPE_ALTERNATIVE, /* multipart/alternative */
38    mx_MIMETYPE_RELATED, /* mime/related (RFC 2387) */
39    mx_MIMETYPE_DIGEST, /* multipart/digest */
40    mx_MIMETYPE_SIGNED, /* multipart/signed */
41    mx_MIMETYPE_ENCRYPTED, /* multipart/encrypted */
42    mx_MIMETYPE_MULTI, /* other multipart/ */
43    mx_MIMETYPE_PKCS7, /* PKCS7 */
44    mx_MIMETYPE_DISCARD /* is discarded */
45 };
46 
47 enum mx_mimetype_handler_flags{
48    mx_MIMETYPE_HDL_NIL, /* No pipe- mimetype handler, go away */
49    mx_MIMETYPE_HDL_CMD, /* Normal command */
50    mx_MIMETYPE_HDL_MSG, /* Display msg (returned as command string) */
51    mx_MIMETYPE_HDL_PTF, /* A special pointer-to-function handler */
52    mx_MIMETYPE_HDL_TEXT, /* ? special cmd to force treatment as text */
53    mx_MIMETYPE_HDL_TYPE_MASK = 7u,
54 
55    /* compose, composetyped, edit, print */
56 
57    mx_MIMETYPE_HDL_ASYNC = 1u<<8, /* Should run asynchronously */
58    mx_MIMETYPE_HDL_COPIOUSOUTPUT = 1u<<9, /* _CMD output directly usable */
59    mx_MIMETYPE_HDL_NEEDSTERM = 1u<<10, /* Takes over terminal */
60    mx_MIMETYPE_HDL_NOQUOTE = 1u<<11, /* No MIME for quoting */
61    mx_MIMETYPE_HDL_TMPF = 1u<<12, /* Create temporary file (zero-sized) */
62    mx_MIMETYPE_HDL_TMPF_FILL = 1u<<13, /* Fill in the msg body content */
63    mx_MIMETYPE_HDL_TMPF_UNLINK = 1u<<14, /* Delete it later again */
64    /* Handler with _HDL_TMPF has a .mth_tmpf_nametmpl.. */
65    mx_MIMETYPE_HDL_TMPF_NAMETMPL = 1u<<15,
66    mx_MIMETYPE_HDL_TMPF_NAMETMPL_SUFFIX = 1u<<16 /* ..to be used as suffix */
67    /* xxx textualnewlines */
68 };
69 enum {mx_MIMETYPE_HDL_MAX = mx_MIMETYPE_HDL_TMPF_NAMETMPL_SUFFIX};
70 
71 struct mx_mimetype_handler{
72    BITENUM_IS(u32,mx_mime_handler_flags) mth_flags;
73    su_64( u8 mth__dummy[4]; )
74    char const *mth_tmpf_nametmpl; /* Only with _HDL_TMPF_NAMETMPL */
75    /* XXX union{} the following? */
76    char const *mth_shell_cmd; /* For MIMETYPE_HDL_CMD */
77    int (*mth_ptf)(void); /* PTF main() for MIMETYPE_HDL_PTF */
78    struct str mth_msg; /* Message describing this command */
79 };
80 
81 /* `(un)?mimetype' commands */
82 EXPORT int c_mimetype(void *vp);
83 EXPORT int c_unmimetype(void *vp);
84 
85 /* Check whether name is correct according to RFC 4288, 4.2.
86  * With t_a_subt, check for valid TYPE/SUBTYPE.
87  * With subt_wildcard_ok, allow * as a SUBTYPE. */
88 EXPORT boole mx_mimetype_is_valid(char const *name, boole t_a_subt,
89       boole subt_wildcard_ok);
90 
91 /* Check whether the Content-Type name is internally known */
92 EXPORT boole mx_mimetype_is_known(char const *name);
93 
94 /* Return a Content-Type matching the name, or NIL if none could be found */
95 EXPORT char *mx_mimetype_classify_filename(char const *name);
96 
97 /* Classify content of *fp* as necessary and fill in arguments; **charset* is
98  * set to *charset-7bit* or charset_iter_or_fallback() if NIL.
99  * no_mboxo states whether 7BIT/8BIT is acceptable if only existence of
100  * a ^From_ would otherwise enforce QP/BASE64 */
101 /* TODO this should take a carrier and only fill that in with what has been
102  * TODO detected/classified, and suggest hints; rest up to caller!
103  * TODO This is not only more correct (no_mboxo crux++), it simplifies a lot */
104 EXPORT enum conversion mx_mimetype_classify_file(FILE *fp,
105       char const **content_type, char const **charset, boole *do_iconv,
106       boole no_mboxo);
107 
108 /* Dependent on *mime-counter-evidence* mpp->m_ct_type_usr_ovwr will be set,
109  * but otherwise mpp is const.  for_user_context rather maps 1:1 to
110  * MIME_PARSE_FOR_USER_CONTEXT */
111 EXPORT enum mx_mimetype mx_mimetype_classify_part(struct mimepart *mpp,
112       boole for_user_context);
113 
114 /* Query handler for a part, return the plain type (& MIMETYPE_HDL_TYPE_MASK).
115  * mthp is anyway initialized (.mth_flags, .mth_msg) */
116 EXPORT enum mx_mimetype_handler_flags mx_mimetype_handler(
117       struct mx_mimetype_handler *mthp, struct mimepart const *mpp,
118       enum sendaction action);
119 
120 #include <su/code-ou.h>
121 #endif /* mx_MIME_TYPE_H */
122 /* s-it-mode */
123