mime-js

Create MIME message from browser, fork of https://github.com/ikr0m/mime-js
git clone git://git.defalsify.org/mime-js.git
Log | Files | Refs | LICENSE

mime-js.min.js (8876B)


      1 (function(){window.Mime=function(){var e,n,t,r,u;return u=function(e,n){var t,r,u,a,o,c,i,s,l,d,p,f,m,g,h,b;return m=function(e){var n,t,r,u;return n=/(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim,u=e.replace(n,'<a href="$1" target="_blank">$1</a>'),t=/(^|[^\/])(www\.[\S]+(\b|$))/gim,u=u.replace(t,'$1<a href="http://$2" target="_blank">$2</a>'),r=/(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim,u=u.replace(r,'<a href="mailto:$1">$1</a>')},p=function(){var e;return e=function(){return Math.random().toString(36).slice(2)},e()+e()},l=function(e){return null==e&&(e=""),"\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(e,!0).replace(/.{76}/g,"$&\n")},i=function(e){var n;return n=e.body||"",n=n.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/,"&gt;").replace(/\n/g,"\n<br/>"),n=m(n),n="<div>"+n+"</div>","\nContent-Type: text/html; charset=UTF-8\nContent-Transfer-Encoding: base64\n\n"+Base64.encode(n,!0).replace(/.{76}/g,"$&\n")},a=function(e,n){var t;return t=p(),"\nContent-Type: multipart/alternative; boundary="+t+"\n\n--"+t+e+"\n\n--"+t+n+"\n\n--"+t+"--"},c=function(e){var n,t,r,u,a,o,c,i;if(e){for(r=[],a=0,o=e.length;a<o;a++)t=e[a],i=t.type,c=t.name,n=t.base64,u=p(),r.push("\nContent-Type: "+i+'; name="'+c+'"\nContent-Transfer-Encoding: base64\nContent-ID: <'+u+">\nX-Attachment-Id: "+u+"\n\n"+n);return r}},d=function(e,n){var t,r,u,a,o;for(null==n&&(n=[]),t=p(),o="\nContent-Type: multipart/related; boundary="+t+"\n\n--"+t+e,u=0,a=n.length;u<a;u++)r=n[u],o+="\n--"+t+r;return o+"\n--"+t+"--"},o=function(e){var n,t,r,u,a,o,c,i,s;if(e){for(i=[],u=0,a=e.length;u<a;u++)n=e[u],s=n.type,o=n.name,r=p(),t="",c="\nContent-Type: "+s,o&&(c+='; name="'+o+'"\nContent-Disposition: attachment; filename="'+o+'"'),n.base64?(t=n.base64,c+="\nContent-Transfer-Encoding: base64"):t=n.raw,c+="\nX-Attachment-Id: "+r,c+="\n\n"+t,i.push(c);return i}},s=function(n,t){var r,u,a,o,c,i,s,l;for(u=p(),l="",e.subject&&(l="=?UTF-8?B?"+Base64.encode(e.subject,!0)+"?="),i="=?UTF-8?B?"+Base64.encode(e.fromName||"",!0)+"?=",a=(new Date).toGMTString().replace(/GMT|UTC/gi,"+0000"),s="MIME-Version: 1.0\nDate: "+a+"\nMessage-ID: <"+p()+"@mail.your-domain.com>\nSubject: "+l+"\nFrom: "+i+" <"+e.from+">"+(e.to?"\nTo: "+e.to:"")+(e.cc?"\nCc: "+e.cc:"")+"\nContent-Type: multipart/mixed; boundary="+u+"\n\n--"+u+n,o=0,c=t.length;o<c;o++)r=t[o],s+="\n--"+u+r;return(s+"\n--"+u+"--").replace(/\n/g,"\r\n")},g=l(e.body),n?h=g:(f=i(e),t=a(g,f),u=c(e.cids),h=d(t,u)),r=o(e.attaches),b=s(h,r)},e=function(e){var t,r,u,a,o,c,i,s;r=function(e){var t,u,a,o,c,i,s,l,d,p,f,m,g,h,b,C;if(l=e.indexOf("\r\n\r\n"),l===-1&&(e=e.replace(/\n/g,"\r\n"),l=e.indexOf("\r\n\r\n"),l===-1&&(l=e.length)),p=e.slice(0,l).replace(/\r\n\s+/g," ")+"\r\n",d=e.slice(l).replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),i="",h=p.match(/Content-Type: (.*)/i),h&&h.length>0?i=h[1]:console.log("Warning: MailParser: Content-type doesn't exist!"),s=i.split(";"),m=s[0].replace(/\s/g,""),g=m.split("/"),"multipart"===g[0].toLowerCase())for(o=[],f=s[1].match(/boundary="?([^"]*)"?/i),!f&&s[2]&&(f=s[2].match(/boundary="?([^"]*)"?/i)),c=n.trim(f[1]).replace(/"/g,""),t=c.replace(/\+/g,"\\+"),b=new RegExp("--"+t,"g"),o=d.replace(b,c).replace(b,c).split(c),o.shift(),o.pop(),u=0;u<o.length;)o[u]=o[u].replace(/(\r\n)+$/,"").replace(/^(\r\n)+/,""),o[u]=r(o[u]),u++;else if(a=d,"text"===g[0]&&(a=a.replace(RegExp("=\\r\\n","g"),""),C=a.match(RegExp("=[A-F0-9][A-F0-9]","g"))))for(u=0;u<C.length;)a=a.replace(C[u],String.fromCharCode(parseInt(C[u].replace(RegExp("="),""),16))),u++;return{rawHeaders:p,rawBody:d,body:a,contentType:i,contentTypeParts:s,boundary:c,bodyParts:o,mimeType:m,mimeTypeParts:g}},o="";try{o=r(e)}catch(l){}return c=o.rawHeaders,a=function(e){return null==e&&(e=[]),e[1]||""},i=a(/\r\nSubject: (.*)\r\n/g.exec(c)),s=a(/\r\nTo: (.*)\r\n/g.exec(c)),t=a(/\r\nCc: (.*)\r\n/g.exec(c)),u=a(/\r\nFrom: (.*)\r\n/g.exec(c)),{messageParts:o,subject:i,to:s,cc:t,from:u}},n=function(){var e,t,r,u,a,o,c,i;return c=function(e){return null==e&&(e=""),("function"==typeof e.trim?e.trim():void 0)||e.replace(/^\s+|\s+$/g,"")},u=function(n,t){var r;return null==n&&(n=""),null==t&&(t=""),t=t.toLowerCase(),r=function(){switch(!1){case t.indexOf("koi8-r")===-1:return e(n);case t.indexOf("utf-8")===-1:return Base64._utf8_decode(n);case t.indexOf("windows-1251")===-1:return i(n);default:return n}}()},t=function(e){return e.replace(/\=[\r\n]+/g,"").replace(/\=[0-9A-F]{2}/gi,function(e){return String.fromCharCode(parseInt(e.substr(1),16))})},e=function(e){var n,t,r,u,a,o,c;for(n=unescape("%u2500%u2502%u250C%u2510%u2514%u2518%u251C%u2524%u252C%u2534%u253C%u2580%u2584%u2588%u258C%u2590%u2591%u2592%u2593%u2320%u25A0%u2219%u221A%u2248%u2264%u2265%u00A0%u2321%u00B0%u00B2%u00B7%u00F7%u2550%u2551%u2552%u0451%u2553%u2554%u2555%u2556%u2557%u2558%u2559%u255A%u255B%u255C%u255D%u255E%u255F%u2560%u2561%u0401%u2562%u2563%u2564%u2565%u2566%u2567%u2568%u2569%u256A%u256B%u256C%u00A9%u044E%u0430%u0431%u0446%u0434%u0435%u0444%u0433%u0445%u0438%u0439%u043A%u043B%u043C%u043D%u043E%u043F%u044F%u0440%u0441%u0442%u0443%u0436%u0432%u044C%u044B%u0437%u0448%u044D%u0449%u0447%u044A%u042E%u0410%u0411%u0426%u0414%u0415%u0424%u0413%u0425%u0418%u0419%u041A%u041B%u041C%u041D%u041E%u041F%u042F%u0420%u0421%u0422%u0423%u0416%u0412%u042C%u042B%u0417%u0428%u042D%u0429%u0427%u042A"),t=function(e){return e>=128&&e<=255?n.charAt(e-128):String.fromCharCode(e)},o="",r=u=0,a=e.length;u<a;r=++u)c=e[r],o+=t(e.charCodeAt(r));return o},i=function(e){var n,t,r,u,a,o,c;for(null==e&&(e=""),o="",n=r=0,u=e.length;r<u;n=++r)c=e[n],t=e.charCodeAt(n),a=function(){switch(!1){case 168!==t:return 1025;case 184!==t:return 1105;case!(191<t&&t<256):return t+848;default:return t}}(),o+=String.fromCharCode(a);return o},r=function(e,r){var a,o,c;return e=n.trim(e),o=void 0,a=void 0,c=void 0,(c=e.match(/^\=\?([\w_\-]+)\?([QqBb])\?([^\?]*)\?\=$/i))?(o=c[1],a=(c[2]||"Q").toString().toUpperCase(),e=(c[3]||"").replace(/_/g," "),"B"===a?Base64.decode(e,r):"Q"===a?t(e):e):u(e,r)},a=function(e,n){return e=(e||"").toString().replace(/(=\?[^?]+\?[QqBb]\?[^?]+\?=)\s+(?==\?[^?]+\?[QqBb]\?[^?]*\?=)/g,"$1").replace(/\=\?([\w_\-]+)\?([QqBb])\?[^\?]*\?\=/g,function(e,n,t){return r(e)}.bind(this)),u(e,n)},o=function(e){return null==e&&(e=""),(e+"").replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")},{decode:u,KOIRDec:e,win1251Dec:i,decodeMimeWords:a,toHtmlEntity:o,trim:c}}(),t=function(t){var u,a,o,c,i,s,l,d,p,f,m,g;f={html:"",text:"",attaches:[],innerMsgs:[],to:n.decodeMimeWords(t.to),cc:n.decodeMimeWords(t.cc),from:n.decodeMimeWords(t.from),subject:n.decodeMimeWords(t.subject)},a=function(e,t){var r,u,a;return a=/Content-Transfer-Encoding: quoted-printable/i.test(t),u=/Content-Transfer-Encoding: base64/i.test(t),u?(e=e.replace(/\s/g,""),r="function"==typeof atob?atob(e):void 0,null==r&&(r=Base64.decode(e)),e=r):a&&(e=n.QPDec(e)),e},d=function(t){var u,o,c,i,s,l,p,m,g,h,b,C,y,x,T,v,w,B,A,E,M,D,F,$,O;if(t){for(h=0,C=t.length;h<C;h++)if(w=t[h],x=(null!=(A=w.mimeType)?A:"").toLowerCase(),x.indexOf("multipart")===-1)if(x.indexOf("message/rfc822")===-1)if(B=w.rawHeaders,i=B.indexOf("Content-Disposition: attachment")!==-1,o=w.rawBody,l=/text\/html/.test(x),m=/text\/plain/.test(x),p=/image/.test(x),s=/audio/.test(x),i||p||s){for(g=/Content-Transfer-Encoding: quoted-printable/i.test(B),g&&(o=n.QPDec(o),o=btoa?btoa(o):Base64.encode(o)),E=w.contentTypeParts,b=0,y=E.length;b<y;b++)if(O=E[b],/name=/i.test(O)){T=O.replace(/(.*)=/,"").replace(/"|'/g,"");break}T||(T=p?"image":s?"audio":"attachment",T+="_"+Math.floor(100*Math.random()),F=x.indexOf("/"),$=x.substring(F+1),$.length<4&&(T+="."+$)),D=/(.*)content-id:(.*)<(.*)>/i,u={type:x,base64:o,name:T,cid:null!=(M=D.exec(B))?M[3]:void 0,visible:/png|jpeg|jpg|gif/.test(x)},f.attaches.push(u)}else l||m?(o=a(o,B),o=n.decode(o,w.contentType),l&&(f.html+=o),m&&(f.text+=o)):console.log("Unknown mime type: "+x);else v=e(w.rawBody),c=r(v),f.innerMsgs.push(c);else d(w.bodyParts);return null}};try{if(p=t.messageParts,!p)return f;l=(p.mimeType||"").toLowerCase(),i=/text\/plain/.test(l),c=/text\/html/.test(l),l.indexOf("multipart")!==-1?d(p.bodyParts):i||c?(u=a(p.body,p.rawHeaders),u=n.decode(u,p.contentType),c&&(f.html=u),i&&(f.text=u)):console.log("Warning: mime type isn't supported! mime="+l)}catch(h){throw o=h,new Error(o)}return g=function(e){return"<pre>"+n.toHtmlEntity(e)+"</pre>"},s=function(e){var t,r,u,a,o,c,i,l;if(u=e.innerMsgs,null!=u?u.length:void 0)for(!n.trim(e.html)&&e.text&&(e.html+=g(e.text)),a=0,o=u.length;a<o;a++)r=u[a],c=s(r),l=c.text,t=c.html,t?e.html+=t:l&&(e.html+=wrapPerTag(l),e.text+=l),(null!=(i=c.attaches)?i.length:void 0)>0&&(e.attaches=e.attaches.concat(c.attaches));return e},m=s(f)},r=function(n){var r,u;return u=e(n),r=t(u)},{toMimeTxt:u,toMimeObj:r}}()}).call(this);