barebones version
[ICQJS.git] / main.js.bak
blob8fde11682778b8e980bba70c2f4fea34e7aa0dfa
1 /**
2  * ICQJS - áèáëèîòåêà äëÿ èñïîëüçîâàíèÿ Âåá ÀÏÈ àñüêè â äæàâàñêðèïòå
3  * (c) InceptionTime, 2024.
4  * Äîêóìåíòàöèÿ íà OSDN è â docs/index.MD
5  */
6 class User{
7     constructor(login, password, fetchCallback, devId, screenNameAPIHost = "https://api.screenname.nina.bz", mainAPIHost = "https://api.oscar.nina.bz"){
8         /**
9                  * Êîíñòðóêòîð þçåðà
10                  * Docs: COMING SOON...
11                  */
12                 
13                 /**
14          * Èíèöèàëèçàöèÿ ïåðåìåííûõ êëàññà
15          */
16         this.login = login;
17         this.password = password;
18         this.screenNameAPIHost = screenNameAPIHost;
19         this.mainAPIHost = mainAPIHost;
20         this.fetchCallback = fetchCallback;
21         this.devId = devId;
22         this.loggedIn = false;
23         /**
24          * Àóòåíòèôèêàöèÿ ïîëüçîâàòåëÿ. Èñïîëüçóåì clientLogin è ñèíõðîííûé XHR
25          */
26         let xhr = new XMLHttpRequest();
27         let data = `f=JSON&k=${this.devId}&s=${this.login}&pwd=${this.password}&clientVersion=0.1&ClientName=OscarWave`;
28             xhr.open('POST', `${this.screenNameAPIHost}/auth/clientLogin?f=JSON`), false);
29             xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
30             xhr.send(data);
31             if (xhr.status === 200) {
32                    let respObj = JSON.parse(xhr.responseText);
33                    this.aToken = respObj.response.data.token.a;
34                    this.sessionSecret = respObj.response.data.sessionSecret;
35                    return 1;
36             } else {
37                    throw new Error('Request failed: ' + xhr.statusText);
38                    return 0;
39         }
40                 // Ãåíåðèðóåì êëþ÷. Çäåñü ïîíàäîáèòñÿ çàâèñèìîñòü lib/CryptHMAC.js
41                 this.sessionKey = btoa(_HMAC(this.password,this.sessionSecret));
43         // Ñòàâèì ñåññèîííóþ ïåðåìåííóþ âõîäà
44         this.loggedIn = true;
45         
46         // Óíè÷òîæàåì ïàðîëü äëÿ áåçîïàñíîñòè
47         this.password = null;
49     get login(){
50         /*
51          * Ïîëó÷åíèå ëîãèíà
52          */
53         return this.login;
54     }
56         startSession(){
57                 /**
58                  * Çàïóñê ñåññèè
59                  * Docs: COMING SOON...
60                  */
61                 let xhr = new XMLHttpRequest();
62                 let data = `f=JSON&k=${this.devId}&a=${this.aToken}&events=im`;
63                 xhr.open('GET', `${this.screenNameAPIHost}/aim/startSession?${data}`;
64                 xhr.send(null);
65             if (xhr.status === 200) {
66                    let respObj = JSON.parse(xhr.responseText);
67                    this.aimsid = respObj.response.data.aimsid;
68                    this.status = respObj.response.data.myInfo.state;
69                    this.displayName = respObj.response.data.myInfo.displayId;
70                    this.fetchURL = respObj.response.data.fetchBaseURL;
71                    return 1;
72             } else {
73                    throw new Error('Request failed: ' + xhr.statusText);
74                    return 0;
75         }
76         }
78         fetch(){
79                 /**
80                  * Áåñêîíå÷íûé ôåò÷ èâåíòîâ.
81                  * Docs: COMING SOON...
82                  */
83                 
84         }