1 import { URISchemeHandler, URIComponents, URIOptions } from "../uri";
2 import { pctEncChar, pctDecChars, unescapeComponent } from "../uri";
3 import punycode from "punycode";
4 import { merge, subexp, toUpperCase, toArray } from "../util";
6 export interface MailtoHeaders {
10 export interface MailtoComponents extends URIComponents {
12 headers?:MailtoHeaders,
17 const O:MailtoHeaders = {};
21 const UNRESERVED$$ = "[A-Za-z0-9\\-\\.\\_\\~" + (isIRI ? "\\xA0-\\u200D\\u2010-\\u2029\\u202F-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFEF" : "") + "]";
22 const HEXDIG$$ = "[0-9A-Fa-f]"; //case-insensitive
23 const PCT_ENCODED$ = subexp(subexp("%[EFef]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%[89A-Fa-f]" + HEXDIG$$ + "%" + HEXDIG$$ + HEXDIG$$) + "|" + subexp("%" + HEXDIG$$ + HEXDIG$$)); //expanded
25 //RFC 5322, except these symbols as per RFC 6068: @ : / ? # [ ] & ; =
26 //const ATEXT$$ = "[A-Za-z0-9\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\_\\`\\{\\|\\}\\~]";
27 //const WSP$$ = "[\\x20\\x09]";
28 //const OBS_QTEXT$$ = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x7F]"; //(%d1-8 / %d11-12 / %d14-31 / %d127)
29 //const QTEXT$$ = merge("[\\x21\\x23-\\x5B\\x5D-\\x7E]", OBS_QTEXT$$); //%d33 / %d35-91 / %d93-126 / obs-qtext
30 //const VCHAR$$ = "[\\x21-\\x7E]";
31 //const WSP$$ = "[\\x20\\x09]";
32 //const OBS_QP$ = subexp("\\\\" + merge("[\\x00\\x0D\\x0A]", OBS_QTEXT$$)); //%d0 / CR / LF / obs-qtext
33 //const FWS$ = subexp(subexp(WSP$$ + "*" + "\\x0D\\x0A") + "?" + WSP$$ + "+");
34 //const QUOTED_PAIR$ = subexp(subexp("\\\\" + subexp(VCHAR$$ + "|" + WSP$$)) + "|" + OBS_QP$);
35 //const QUOTED_STRING$ = subexp('\\"' + subexp(FWS$ + "?" + QCONTENT$) + "*" + FWS$ + "?" + '\\"');
36 const ATEXT$$ = "[A-Za-z0-9\\!\\$\\%\\'\\*\\+\\-\\^\\_\\`\\{\\|\\}\\~]";
37 const QTEXT$$ = "[\\!\\$\\%\\'\\(\\)\\*\\+\\,\\-\\.0-9\\<\\>A-Z\\x5E-\\x7E]";
38 const VCHAR$$ = merge(QTEXT$$, "[\\\"\\\\]");
39 const DOT_ATOM_TEXT$ = subexp(ATEXT$$ + "+" + subexp("\\." + ATEXT$$ + "+") + "*");
40 const QUOTED_PAIR$ = subexp("\\\\" + VCHAR$$);
41 const QCONTENT$ = subexp(QTEXT$$ + "|" + QUOTED_PAIR$);
42 const QUOTED_STRING$ = subexp('\\"' + QCONTENT$ + "*" + '\\"');
45 const DTEXT_NO_OBS$$ = "[\\x21-\\x5A\\x5E-\\x7E]"; //%d33-90 / %d94-126
46 const SOME_DELIMS$$ = "[\\!\\$\\'\\(\\)\\*\\+\\,\\;\\:\\@]";
47 const QCHAR$ = subexp(UNRESERVED$$ + "|" + PCT_ENCODED$ + "|" + SOME_DELIMS$$);
48 const DOMAIN$ = subexp(DOT_ATOM_TEXT$ + "|" + "\\[" + DTEXT_NO_OBS$$ + "*" + "\\]");
49 const LOCAL_PART$ = subexp(DOT_ATOM_TEXT$ + "|" + QUOTED_STRING$);
50 const ADDR_SPEC$ = subexp(LOCAL_PART$ + "\\@" + DOMAIN$);
51 const TO$ = subexp(ADDR_SPEC$ + subexp("\\," + ADDR_SPEC$) + "*");
52 const HFNAME$ = subexp(QCHAR$ + "*");
53 const HFVALUE$ = HFNAME$;
54 const HFIELD$ = subexp(HFNAME$ + "\\=" + HFVALUE$);
55 const HFIELDS2$ = subexp(HFIELD$ + subexp("\\&" + HFIELD$) + "*");
56 const HFIELDS$ = subexp("\\?" + HFIELDS2$);
57 const MAILTO_URI = new RegExp("^mailto\\:" + TO$ + "?" + HFIELDS$ + "?$");
59 const UNRESERVED = new RegExp(UNRESERVED$$, "g");
60 const PCT_ENCODED = new RegExp(PCT_ENCODED$, "g");
61 const NOT_LOCAL_PART = new RegExp(merge("[^]", ATEXT$$, "[\\.]", '[\\"]', VCHAR$$), "g");
62 const NOT_DOMAIN = new RegExp(merge("[^]", ATEXT$$, "[\\.]", "[\\[]", DTEXT_NO_OBS$$, "[\\]]"), "g");
63 const NOT_HFNAME = new RegExp(merge("[^]", UNRESERVED$$, SOME_DELIMS$$), "g");
64 const NOT_HFVALUE = NOT_HFNAME;
65 const TO = new RegExp("^" + TO$ + "$");
66 const HFIELDS = new RegExp("^" + HFIELDS2$ + "$");
68 function decodeUnreserved(str:string):string {
69 const decStr = pctDecChars(str);
70 return (!decStr.match(UNRESERVED) ? str : decStr);
73 const handler:URISchemeHandler<MailtoComponents> = {
76 parse : function (components:URIComponents, options:URIOptions):MailtoComponents {
77 const mailtoComponents = components as MailtoComponents;
78 const to = mailtoComponents.to = (mailtoComponents.path ? mailtoComponents.path.split(",") : []);
79 mailtoComponents.path = undefined;
81 if (mailtoComponents.query) {
82 let unknownHeaders = false
83 const headers:MailtoHeaders = {};
84 const hfields = mailtoComponents.query.split("&");
86 for (let x = 0, xl = hfields.length; x < xl; ++x) {
87 const hfield = hfields[x].split("=");
91 const toAddrs = hfield[1].split(",");
92 for (let x = 0, xl = toAddrs.length; x < xl; ++x) {
97 mailtoComponents.subject = unescapeComponent(hfield[1], options);
100 mailtoComponents.body = unescapeComponent(hfield[1], options);
103 unknownHeaders = true;
104 headers[unescapeComponent(hfield[0], options)] = unescapeComponent(hfield[1], options);
109 if (unknownHeaders) mailtoComponents.headers = headers;
112 mailtoComponents.query = undefined;
114 for (let x = 0, xl = to.length; x < xl; ++x) {
115 const addr = to[x].split("@");
117 addr[0] = unescapeComponent(addr[0]);
119 if (!options.unicodeSupport) {
120 //convert Unicode IDN -> ASCII IDN
122 addr[1] = punycode.toASCII(unescapeComponent(addr[1], options).toLowerCase());
124 mailtoComponents.error = mailtoComponents.error || "Email address's domain name can not be converted to ASCII via punycode: " + e;
127 addr[1] = unescapeComponent(addr[1], options).toLowerCase();
130 to[x] = addr.join("@");
133 return mailtoComponents;
136 serialize : function (mailtoComponents:MailtoComponents, options:URIOptions):URIComponents {
137 const components = mailtoComponents as URIComponents;
138 const to = toArray(mailtoComponents.to);
140 for (let x = 0, xl = to.length; x < xl; ++x) {
141 const toAddr = String(to[x]);
142 const atIdx = toAddr.lastIndexOf("@");
143 const localPart = (toAddr.slice(0, atIdx)).replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_LOCAL_PART, pctEncChar);
144 let domain = toAddr.slice(atIdx + 1);
146 //convert IDN via punycode
148 domain = (!options.iri ? punycode.toASCII(unescapeComponent(domain, options).toLowerCase()) : punycode.toUnicode(domain));
150 components.error = components.error || "Email address's domain name can not be converted to " + (!options.iri ? "ASCII" : "Unicode") + " via punycode: " + e;
153 to[x] = localPart + "@" + domain;
156 components.path = to.join(",");
159 const headers = mailtoComponents.headers = mailtoComponents.headers || {};
161 if (mailtoComponents.subject) headers["subject"] = mailtoComponents.subject;
162 if (mailtoComponents.body) headers["body"] = mailtoComponents.body;
165 for (const name in headers) {
166 if (headers[name] !== O[name]) {
168 name.replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_HFNAME, pctEncChar) +
170 headers[name].replace(PCT_ENCODED, decodeUnreserved).replace(PCT_ENCODED, toUpperCase).replace(NOT_HFVALUE, pctEncChar)
175 components.query = fields.join("&");
182 export default handler;