-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
1 lines (1 loc) · 4.69 KB
/
index.js
1
"use strict";const querystring=require("querystring"),Core=require("./src/core.js");class EasyYopmail extends Core{async getMail(){try{const{location:a}=await this.getHttpSettings();let b=this.constants.URL_GENERATOR(a);const c=await this.request("GET",b);200!==c.statusCode&&this.handleError({customMessage:this.constants.ERROR_LOAD_PAGE});const d=this.loadHTMl(c.body),e=d(this.constants.S_INPUT_MAIL_GENERATE).text();return e.split(";")[1]||e}catch(a){this.handleError({customMessage:a,error:this.constants.ERROR_LOAD_PAGE})}}async getInbox(a){let b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:{LIMIT:5};const d=this.simplifyEmail(a),e=await this.getHttpSettings();return await this.detailInbox(d,b,c,e)}async readMessage(a,b){let c=2<arguments.length&&arguments[2]!==void 0?arguments[2]:{};try{let{opt:d,info:e}=this.validateReadMessageOpt(c)||[],{location:f,version:g,cookie:h,T:i,RT:j,ytime:k}=await this.getHttpSettings(),{format:l,selector:m,attribute:n,pathToSave:o,eq:p,saveAttachment:q}=d;const r=this.constants.URL_READ_MAIL(a,b,f),s=this.constants.HEADERS_READ_MESSAGE(h,a,f,i,j,k),t=await this.request("GET",r,s);(410===t.statusCode||400===t.statusCode)&&this.handleError({customMessage:this.constants.ERROR_READ_MESSAGE_STATUS_410,error:new Error}),200!==t.statusCode&&this.handleError({customMessage:this.constants.ERROR_READ_MESSAGE,error:new Error});const u=this.loadHTMl(t.body),v=u(this.constants.S_SUBMIT).text(),w=u(this.constants.S_FROM),x=u(this.constants.S_DATE),y=w.length?w.text():u(this.constants.S_FROM_ALT).text(),z=x.length?x.text().replace(y,""):u(this.constants.S_DATE_ALT).text(),A=u("#infofromdeliver span").text()||"Your account has been credited",B=u("#r_dialog"),C=u("div.fl.pjs.yscrollbar");let D="html"===l?this.getContentBySelectorForHTML(u,m,n,p,e):this.getContentBySelectorForTxt(u,m,n,p,e);const E=a=>{const b=a.lastIndexOf(".");if(-1===b)return{name:a,extension:""};else{const c=a.substring(0,b),d=a.substring(b+1);return{name:c,extension:d}}},F=[];return C.length&&C.find("a").length&&C.find("a").each(((a,b)=>{const c=u(b),d=c.attr("title"),e=c.attr("href"),f=this.constants.URL_BASE+e,{name:g,extension:h}=E(d);F.push({fullName:d,name:g,extension:h,file:f}),q&&this.downloadAttachments(f,d,s)})),B.length&&(D.content=B.text().replace(/\n/,"").trim()),o&&this.saveEmailHTML(o,t.body.replace('href="/ver/9.2/style.css"',`href="https://yopmail.com/ver/${g}/style.css"`),b),{id:b,submit:v,from:y,date:z,deliverability:A,attachments:F,saveAttachment:q,format:l,selector:m,eq:p?`eq(${p})`:null,attribute:n,pathToSave:o,content:D.content,info:D.info}}catch(a){this.handleError({customMessage:this.constants.ERROR_READ_MESSAGE,error:a})}}async deleteMessage(a,b){try{let{location:c,version:d,cookie:e,yp:f,yj:g,T:h,RT:i,ytime:j}=await this.getHttpSettings();const k=this.constants.HEADERS_INBOX(e,a,c,h,i,j),l=this.constants.URL_DELETE_MESSAGE(a,b,f,g,d,c),m=this.constants.URL_READ_MAIL(a,b,c),n=await this.request("GET",m,k);if(200!==n.statusCode)return this.constants.NOTIFY_EMAIL_NOT_FOUND(b);else{const a=await this.request("GET",l,k);return 200===a.statusCode?this.constants.NOTIFY_EMAIL_DELETED_SUCCESS(b):this.constants.NOTIFY_EMAIL_DELETED_FAILED(b)}}catch(a){this.handleError({customMessage:this.constants.ERROR_DELETE_MESSAGE,error:a})}}async deleteInbox(a){try{const{totalEmails:b,inbox:c}=await this.getInbox(a);if(0<b){let{location:d,version:e,cookie:f,yp:g,yj:h,T:i,RT:j,ytime:k}=await this.getHttpSettings();const l=c[0].id,m=this.constants.URL_DELETE_INBOX(a,l,g,h,e,d),n=this.constants.HEADERS_INBOX(f,a,d,i,j,k),o=await this.request("GET",m,n);return 200===o.statusCode?this.constants.NOTIFY_INBOX_DELETED_SUCCESS(a,b):this.constants.NOTIFY_INBOX_DELETED_FAILED(a)}return this.constants.NOTIFY_INBOX_EMPTY}catch(a){this.handleError({customMessage:this.constants.ERROR_DELETE_INBOX,error:a})}}async writeMessage(a,b,c,d){try{a&&b&&c&&d||console.error(this.constants.ERROR_MISSING_PARAMETERS);let{location:e,cookie:f,T:g,RT:h,ytime:i}=await this.getHttpSettings();const j=this.constants.URL_WRITE_MAIL(a,e),k=this.constants.HEADERS_INBOX(f,a,e,g,h,i),l=await this.request("GET",j,k);if(200===l.statusCode){const j=querystring.stringify({msgfrom:`${a}@yopmail.com`,msgto:b,msgsubject:c,msgbody:d});let k=this.constants.HEADERS_WRITE_MESSAGE(f,a,e,g,h,i);k.headers["Content-Type"]="application/x-www-form-urlencoded",k.headers["Content-Length"]=Buffer.byteLength(j);const l=this.constants.URL_SEND_MESSAGE(e),m=await this.request("POST",l,k,j);return 200!==m.statusCode&&console.error(this.constants.ERROR_WRITE_MESSAGE),(m.body+"").split("|")[2]}return l.data}catch(a){this.handleError({customMessage:this.constants.ERROR_WRITE_MESSAGE,error:a})}}}module.exports=new EasyYopmail;