-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsquadbot.min.mjs
More file actions
1 lines (1 loc) · 12.5 KB
/
jsquadbot.min.mjs
File metadata and controls
1 lines (1 loc) · 12.5 KB
1
class MessagesViewModel extends Array{constructor(...e){super();this.lastChecked=0;e.forEach(e=>this.push(e))}push(e){if(e.author&&e.text){if(e.date>this.lastChecked)this.lastChecked=e.date;return super.push(e)}return false}has(e){return this.some(s=>s.equals(e))}remove(e){let s=[...e];return this.filter(e=>{const t=s.findIndex(s=>e.equals(s));if(t>-1)s.splice(t,1);return-1==t})}}class MessageViewModel{constructor(e,s,t){if(!e||!s||!(t instanceof Date))throw`Could not create a MessageViewModel with author "${e}", text "${s}" and date "${t}"`;this._author=e;this._text=s;this._date=t}equals(e){return this.author==e.author&&this.text==e.text&&this.date.valueOf()==e.date.valueOf()}get date(){return this._date}get author(){return this._author}get text(){return this._text}set date(e){return this._date=e}set author(e){return this._author=e}set text(e){return this._text=e}}class ChatView{constructor(e){this._id=e}get id(){return this._id}querySelector(e,s,t=((e,s,t)=>s||e>=3e3),i=document){return new Promise(a=>{let h=[],r,n=null,o=0;void function l(){r=i.querySelector(e);if(h=t(o+=+s,r,n||r))a(true===h?null:h);else{n=r;setTimeout(l,s)}}()})}querySelectorAll(e,s,t=((e,s,t)=>s.length||e>=3e3?s:false),i=document){return new Promise(a=>{let h=[],r,n=[],o=0;void function l(){r=i.querySelectorAll(e);if(h=t(o+=+s,r,n||r))a(h);else{n=r;setTimeout(l,s)}}()})}hasNewMessage(){void 0;return new Promise(async e=>{e(!!(await this.getMessages()).remove(this._messagesViewModel).length)})}popNewMessages(){void 0;return new Promise(async e=>{const s=(await this.getMessages()).remove(this._messagesViewModel);void 0;s.forEach(e=>this._messagesViewModel.push(e));e(s)})}async postMessage(e){throw"async postMessage() must be implemented."}async getMessages(){throw"async getMessages() must be implemented."}}class ChatViewWhatsApp extends ChatView{constructor(e){super(e);this._messagesViewModel=new MessagesViewModel}async switch(){void 0;document.querySelector(`span._3ko75._5h6Y_._3Whw5[title*='${this.id}']`).dispatchEvent(new MouseEvent("mousedown",{bubbles:true,cancelable:true,view:window}));void 0;await this.querySelector(`header span[title*='${this.id}']`,1e3);await this.querySelector("._2J60S",1e3,(e,s)=>e>=5e3||["load earlier messages…","Carregar mensagens recentes"].includes(s.title));void 0}parseMessage(e){void 0;let s=e.dataset.prePlainText;if(!s)throw"Tried to create a MessageViewModel with a wrong DIV";if(e.querySelector("img"))throw"Tried to parse a message with an emoji";if(e.querySelector("._3AFCK"))throw"Tried to parse a reply message";const t=s.substr(0,s.length-2).split("] ")[1];const i=[...e.firstChild.firstChild.firstChild.childNodes].reduce((e,s)=>{let t=s.nodeValue;if(!t&&""!==t&&s.dataset.appTextTemplate){const e=s.dataset.appTextTemplate;const i=e.substr(0,e.indexOf("$"));t=i+s.textContent+i}return t?e.concat(t):e},"");s=[...s.matchAll(/\d+/g)];const a=new Date(s[4],s[2]-1,s[3],s[0],s[1]);const h=new MessageViewModel(t,i,a);return h}async postMessage(e){await this.switch();void 0;let s=await this.querySelector("footer div[contenteditable]",1e3,(e,s)=>{if(s)if(""===s.textContent||e>=5e3)return s;return false});s.textContent=e;s.dispatchEvent(new Event("input",{bubbles:true,cancelable:true,view:window}));void 0;(await this.querySelector("button._1U1xa",1e3)).click()}async getMessages(){await this.switch();return new MessagesViewModel(...[...await this.querySelectorAll("div[data-pre-plain-text]",2e3,(e,s,t)=>{void 0;return e>=6e3||t.length&&t.length==s.length?(void 0,s):false})].reduce((e,s)=>!s.querySelector("._3AFCK")&&!s.querySelector("img")?e.concat(this.parseMessage(s)):e,[]))}static getAllChatsTitles(){return[...document.querySelectorAll("._3ko75._5h6Y_._3Whw5[title]")].map(e=>e.title)}}const e="0123456789abcdef".split("");class Cryptography{static _f(s){let t="",i=0;for(;i<4;i++)t+=e[s>>8*i+4&15]+e[s>>8*i&15];return t}static _d(e,s){return e+s&4294967295}static _j(e,s,t,i,a,h){s=this._d(this._d(s,e),this._d(i,h));return this._d(s<<a|s>>>32-a,t)}static _e(e,s,t,i,a,h,r){return this._j(s^t^i,e,s,a,h,r)}static _k(e,s,t,i,a,h,r){return this._j(t^(s|~i),e,s,a,h,r)}static _h(e,s,t,i,a,h,r){return this._j(s&i|t&~i,e,s,a,h,r)}static _a(e,s,t,i,a,h,r){return this._j(s&t|~s&i,e,s,a,h,r)}static _c(e,s){let t=e[0],i=e[1],a=e[2],h=e[3];t=this._a(t,i,a,h,s[0],7,-680876936);h=this._a(h,t,i,a,s[1],12,-389564586);a=this._a(a,h,t,i,s[2],17,606105819);i=this._a(i,a,h,t,s[3],22,-1044525330);t=this._a(t,i,a,h,s[4],7,-176418897);h=this._a(h,t,i,a,s[5],12,1200080426);a=this._a(a,h,t,i,s[6],17,-1473231341);i=this._a(i,a,h,t,s[7],22,-45705983);t=this._a(t,i,a,h,s[8],7,1770035416);h=this._a(h,t,i,a,s[9],12,-1958414417);a=this._a(a,h,t,i,s[10],17,-42063);i=this._a(i,a,h,t,s[11],22,-1990404162);t=this._a(t,i,a,h,s[12],7,1804603682);h=this._a(h,t,i,a,s[13],12,-40341101);a=this._a(a,h,t,i,s[14],17,-1502002290);i=this._a(i,a,h,t,s[15],22,1236535329);t=this._h(t,i,a,h,s[1],5,-165796510);h=this._h(h,t,i,a,s[6],9,-1069501632);a=this._h(a,h,t,i,s[11],14,643717713);i=this._h(i,a,h,t,s[0],20,-373897302);t=this._h(t,i,a,h,s[5],5,-701558691);h=this._h(h,t,i,a,s[10],9,38016083);a=this._h(a,h,t,i,s[15],14,-660478335);i=this._h(i,a,h,t,s[4],20,-405537848);t=this._h(t,i,a,h,s[9],5,568446438);h=this._h(h,t,i,a,s[14],9,-1019803690);a=this._h(a,h,t,i,s[3],14,-187363961);i=this._h(i,a,h,t,s[8],20,1163531501);t=this._h(t,i,a,h,s[13],5,-1444681467);h=this._h(h,t,i,a,s[2],9,-51403784);a=this._h(a,h,t,i,s[7],14,1735328473);i=this._h(i,a,h,t,s[12],20,-1926607734);t=this._e(t,i,a,h,s[5],4,-378558);h=this._e(h,t,i,a,s[8],11,-2022574463);a=this._e(a,h,t,i,s[11],16,1839030562);i=this._e(i,a,h,t,s[14],23,-35309556);t=this._e(t,i,a,h,s[1],4,-1530992060);h=this._e(h,t,i,a,s[4],11,1272893353);a=this._e(a,h,t,i,s[7],16,-155497632);i=this._e(i,a,h,t,s[10],23,-1094730640);t=this._e(t,i,a,h,s[13],4,681279174);h=this._e(h,t,i,a,s[0],11,-358537222);a=this._e(a,h,t,i,s[3],16,-722521979);i=this._e(i,a,h,t,s[6],23,76029189);t=this._e(t,i,a,h,s[9],4,-640364487);h=this._e(h,t,i,a,s[12],11,-421815835);a=this._e(a,h,t,i,s[15],16,530742520);i=this._e(i,a,h,t,s[2],23,-995338651);t=this._k(t,i,a,h,s[0],6,-198630844);h=this._k(h,t,i,a,s[7],10,1126891415);a=this._k(a,h,t,i,s[14],15,-1416354905);i=this._k(i,a,h,t,s[5],21,-57434055);t=this._k(t,i,a,h,s[12],6,1700485571);h=this._k(h,t,i,a,s[3],10,-1894986606);a=this._k(a,h,t,i,s[10],15,-1051523);i=this._k(i,a,h,t,s[1],21,-2054922799);t=this._k(t,i,a,h,s[8],6,1873313359);h=this._k(h,t,i,a,s[15],10,-30611744);a=this._k(a,h,t,i,s[6],15,-1560198380);i=this._k(i,a,h,t,s[13],21,1309151649);t=this._k(t,i,a,h,s[4],6,-145523070);h=this._k(h,t,i,a,s[11],10,-1120210379);a=this._k(a,h,t,i,s[2],15,718787259);i=this._k(i,a,h,t,s[9],21,-343485551);e[0]=this._d(t,e[0]);e[1]=this._d(i,e[1]);e[2]=this._d(a,e[2]);e[3]=this._d(h,e[3])}static _i(e){let s=e.length,t=[1732584193,-271733879,-1732584194,271733878],i;e=e.substring(i-64);let a=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];for(i=0;i<e.length;i++)a[i>>2]|=e.charCodeAt(i)<<(i%4<<3);a[i>>2]|=128<<(i%4<<3);if(i>55){this._c(t,a);for(i=0;i<16;i++)a[i]=0}a[14]=8*s;this._c(t,a);return t}static _b(e){for(let s=0;s<e.length;s++)e[s]=this._f(e[s]);return e.join("")}static md5(e){return this._b(this._i(e))}static messageEncode(e){let s="",t="";for(let i=0;i<=e.length;i++)if(e.charCodeAt(i)>255){t=escape(e.charAt(i));if("%u"==t.substring(0,2))s+="|"+t.substring(2,t.length);else s+=t}else s+=e.charAt(i);s=s.replace("|201C","'").replace("|201D","'").replace("|2018","'").replace("|2019","'").replace("`","'").replace("%B4","'").replace("|FF20","").replace("|FE6B","");s=escape(s);return s}}class BotAPI{constructor(){this._sessionID;this._XAI;this._ns;this.language;this._messages;this.init()}init(){this._sessionID="";this._XAI="";this._ns=1;this.language="en";this._messages=[]}postMessage(e){return new Promise(s=>{e=e[0].toUpperCase()+e.slice(1);this._messages.push(e);const t=new XMLHttpRequest;t.withCredentials=true;let i="https://www."+"clever"+"bot.com/"+"webservice"+"min?uc=UseOfficial"+"Clever"+"bot"+"API&";if(this._sessionID)i=i.concat(`out=${!!this._messages[this._messages.length-2]?Cryptography.messageEncode(this._messages[this._messages.length-2]):""}&in=${Cryptography.messageEncode(this._messages[this._messages.length-1])}&bot=c&cbsid=${this._sessionID}&xai=${this._sessionID.substr(0,3)},${this._XAI}&ns=${this._ns++}&al=&dl=${this.language}&flag=&user=&mode=1&alt=0&reac=&emo=&sou=website&xed=&`);t.open("POST",i);t.onload=e=>{void 0;const t=e.target.responseText.split("\r");this._messages.push(t[0]);this._sessionID=t[1];this._XAI=t[2];s(t[0])};const a=`stimulus=${[...this._messages].reverse().map((e,s)=>`${s?`vText${s+1}=`:""}${Cryptography.messageEncode(e)}`).join("&")}&cb_settings_language=${this.language}&cb_settings_scripting=no&sessionid=${this._sessionID}&islearning=1&icognoid=wsf`;const h=Cryptography.md5(a.substring(7,33));void 0;t.send(a+"&icognocheck="+h)})}}class Bot{constructor(e){this._name=e;this._messagesToBeRead=[],this._messagesToBeSent=[];this._api=new BotAPI}get name(){return this._name}set name(e){return this._name=e}addMessageToBeRead(e){return this._messagesToBeRead.push(e)}addMessageToBeSent(e){this._sent=false;return this._messagesToBeSent.push(`[${this.name}] ${e}`)}clearMessagesToBeSent(){this._messagesToBeSent.length=0}clearMessagesToBeRead(){this._messagesToBeRead.length=0}reply(){return new Promise(e=>{if(!this._messagesToBeRead.length){e();return}this._api.postMessage(this._messagesToBeRead[this._messagesToBeRead.length-1]).then(s=>{this.addMessageToBeSent(s);this._messagesToBeRead.length=0;e()})})}getAnswer(){let e=[...this._messagesToBeSent];this._messagesToBeSent.length=0;return e.length?e[e.length-1]:""}}class ChatModel{constructor(e){this._id=e;this._messages=[],this._messagesToBeRead=[],this._messagesToBeSent=[];this._bots=new Map}bots(){return this._bots}addBot(e){this._bots.set(e.name,e)}get id(){return this._id}set id(e){this._id=e}get messages(){return[...this._messages]}addMessageToBeRead(e){if(e){this._messagesToBeRead.push(e);return this._messages.push(e)}}addMessageToBeSent(e){if(e){this._messagesToBeSent.push(e);return this._messages.push(e)}}clearMessagesToBeRead(){this._messagesToBeRead.length=0}popMessagesToBeSent(){const e=[...this._messagesToBeSent];this._messagesToBeSent.length=0;return e}reply(){return new Promise(async e=>{for(let e of this._bots.values())this._messagesToBeRead.forEach(s=>{if(new RegExp(`\\b${e.name}\\b`,"i").test(s)){if(s.includes(`[${e.name}:reset]`))return e.clearMessagesToBeRead();else if(s.includes(`[${e.name}`))return;s=s.replace(new RegExp(`[^a-z|\\d|\\u00E0-\\u00FC]*\\b${e.name}\\b`,"i"),"").replace(/^\W+/,"");if(/[a-z|\d]\s*$/i.test(s))s+=".";e.addMessageToBeRead(s)}});this.clearMessagesToBeRead();await Promise.all([...this._bots.values()].map(e=>e.reply()));this._bots.forEach(e=>{e.clearMessagesToBeRead();this.addMessageToBeSent(e.getAnswer())});e()})}}class ChatController{constructor(e){if(!(e instanceof ChatView))throw new TypeError(`Invalid ChatView object: ${e}`);this._id=e.id;this._chatModel=new ChatModel(e.id);this._chatView=e}get id(){return this._id}getMessages(){return this._chatView.getMessages()}hasNewMessage(){return this._chatView.hasNewMessage()}async update(){if(await this.hasNewMessage()){(await this.popNewMessages()).forEach(e=>this.addMessageToBeRead(e.text));await this.reply();this.popMessagesToBeSent().forEach(e=>this.postMessage(e))}}postMessage(e){return this._chatView.postMessage(e)}popNewMessages(){return this._chatView.popNewMessages()}addMessageToBeRead(e){return this._chatModel.addMessageToBeRead(e)}reply(){return this._chatModel.reply()}popMessagesToBeSent(){return this._chatModel.popMessagesToBeSent()}async addBot(e){this._chatModel.addBot(new Bot(e));await this._chatView.postMessage(`[${e}:reset]`);await this._chatView.postMessage(`[${e}:listening]`)}}class ChatControllerMap extends Map{constructor(...e){super(e.map(e=>[e.id,e]));this._timeoutID=null;this._refreshInterval=6e3}set(...e){if(1===e.length)super.set(e[0].id,e[0]);else super.set(e[0],e[1])}async listen(){this._timeoutID=true;void async function e(){void 0;await this.update();if(this._timeoutID)this._timeoutID=setTimeout(e.bind(this),this._refreshInterval)}.call(this)}stop(){if(this._timeoutID)clearInterval(this._timeoutID);this._timeoutID=null}async getNextUnansweredChatController(){for(let e of this.values())if(await e.hasNewMessage())return e;return null}async update(){let e=await this.getNextUnansweredChatController();if(e)await e.update()}set refreshInterval(e){this._refreshInterval=e;if(this._timeoutID){clearInterval(this._timeoutID);this.listen()}}get refreshInterval(){return this._refreshInterval}}const chatControllerMap=new ChatControllerMap;