]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/lib.js
jappixmini: wait for client secret to be set
[friendica-addons.git] / jappixmini / lib.js
1 function jappixmini_addon_xor(str1, str2) {
2     if (str1.length != str2.length) throw "not same length";
3
4     encoded = "";
5
6     for (i=0; i<str1.length;i++) {
7         var a = str1.charCodeAt(i);
8         var b = str2.charCodeAt(i);
9         var c = a ^ b;
10
11         encoded += String.fromCharCode(c);
12     }
13
14     return encoded;
15 }
16
17 function jappixmini_addon_set_client_secret(password) {
18         if (!password) return;
19
20         salt1 = "h8doCRekWto0njyQohKpdx6BN0UTyC6N";
21         salt2 = "jdX8OwFC1kWAq3s9uOyAcE8g3UNNO5t3";
22
23         client_secret1 = str_sha1(salt1+password);
24         client_secret2 = str_sha1(salt2+password);
25         client_secret = client_secret1 + client_secret2;
26
27         setDB('jappix-mini', 'client-secret', client_secret);
28         console.log("client secret set");
29 }
30
31 function jappixmini_addon_get_client_secret(callback) {
32         client_secret = getDB('jappix-mini', 'client-secret');
33         if (client_secret===null) {
34                 div = document.getElementById("#jappixmini-password-query-div");
35
36                 if (!div) {
37                         div = $('<div id="jappixmini-password-query-div" style="position:fixed;padding:1em;background-color:#F00;color:#fff;top:50px;left:50px;">Retype your Friendica password for chatting:<br></div>');
38
39                         input = $('<input type="password" id="jappixmini-password-query-input">')
40                         div.append(input);
41
42                         button = $('<input type="button" value="OK" id="jappixmini-password-query-button">');
43                         div.append(button);
44
45                         $("body").append(div);
46                 }
47
48                 button.click(function(){
49                         password = $("#jappixmini-password-query-input").val();
50                         jappixmini_addon_set_client_secret(password);
51                         div.remove();
52
53                         client_secret = getDB('jappix-mini', 'client-secret');
54                         callback(client_secret);
55                 });
56         }
57         else {
58                 callback(client_secret);
59         }
60 }
61
62 function jappixmini_addon_encrypt_password(password, callback) {
63         jappixmini_addon_get_client_secret(function(client_secret){
64                 // add \0 to password until it has the same length as secret
65                 if (password.length>client_secret.length-1) throw "password too long";
66                 while (password.length<client_secret.length) {
67                         password += "\0";
68                 }
69
70                 // xor password with secret
71                 encrypted_password = jappixmini_addon_xor(client_secret, password);
72
73                 encrypted_password = encodeURI(encrypted_password)
74                 callback(encrypted_password);
75         });
76 }
77
78 function jappixmini_addon_decrypt_password(encrypted_password, callback) {
79         encrypted_password = decodeURI(encrypted_password);
80
81         jappixmini_addon_get_client_secret(function(client_secret){
82                 // xor password with secret
83                 password = jappixmini_addon_xor(client_secret, encrypted_password);
84
85                 // remove \0
86                 first_null = password.indexOf("\0")
87                 // TODO: check first_null==null
88                 password = password.substr(0, first_null);
89
90                 callback(password);
91         });
92 }
93
94 function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
95         // listen for subscriptions
96         con.registerHandler('presence',function(presence){
97                 var type = presence.getType();
98                 if (type != "subscribe") return;
99
100                 var from = fullXID(getStanzaFrom(presence));
101                 var xid = bareXID(from);
102
103                 approve = true;
104                 if ((!autoapprove) || contacts[xid]===undefined)
105                         approve = confirm("Accept "+xid+" for chat?");
106
107                 if (approve) {
108                         acceptSubscribe(xid, contacts[xid]);
109                         console.log("Accepted "+xid+" for chat.");
110                 }
111         });
112
113         // autosubscribe
114         if (autosubscribe) {
115                 for (i=0; i<contacts.length; i++) {
116                         xid = contacts[i];
117                         sendSubscribe(xid, "subscribe");
118                         console.log("Subscribed to "+xid);
119                 }
120         }
121 }
122
123 function jappixmini_addon_subscribe() {
124         if (!window.con) {
125                 alert("Not connected.");
126                 return;
127         }
128
129         xid = prompt("Jabber address");
130         sendSubscribe(xid, "subscribe");
131 }
132
133 function jappixmini_addon_start(server, username, bosh, encrypted, password, nickname) {
134     handler = function(password){
135         // check if settings have changed, reinitialize jappix mini if this is the case
136         settings_identifier = str_sha1(server);
137         settings_identifier += str_sha1(username);
138         settings_identifier += str_sha1(bosh);
139         settings_identifier += str_sha1(password);
140         settings_identifier += str_sha1(nickname);
141
142         saved_identifier = getDB("jappix-mini", "settings_identifier");
143         if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
144         setDB("jappix-mini", "settings_identifier", settings_identifier);
145
146         // set bosh host
147         if (bosh)
148             HOST_BOSH = HOST_BOSH+"?host_bosh="+encodeURI(bosh);
149
150         // start jappix mini
151         MINI_NICKNAME = nickname;
152         launchMini(true, false, server, username, password);
153     }
154
155     // decrypt password if necessary
156     if (encrypted)
157         jappixmini_addon_decrypt_password(password, handler);
158     else
159         handler(password);
160 }