]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/lib.js
make bosh proxy optional, add subscribe functionality and some more improvements...
[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() {
32         client_secret = getDB('jappix-mini', 'client_secret');
33         if (client_secret===null) {
34                 div = $('<div style="position:fixed;padding:1em;background-color:#F00;color:#fff;top:50px;left:50px;">Retype your Friendica password for chatting:</div>');
35                 div.append($("<br>"));
36                 input = $('<input type="password">')
37                 div.append(input);
38                 button = $('<input type="button" value="OK">');
39                 button.click(function(){
40                         password = input.val();
41                         jappixmini_addon_set_client_secret(password);
42                         div.remove();
43                 });
44                 div.append(button);
45                 $("body").append(div);
46         }
47
48         return client_secret;
49 }
50
51 function jappixmini_addon_encrypt_password(password) {
52         client_secret = jappixmini_addon_get_client_secret();
53
54         // add \0 to password until it has the same length as secret
55         if (password.length>client_secret.length-1) throw "password too long";
56         while (password.length<client_secret.length) {
57                 password += "\0";
58         }
59
60         // xor password with secret
61         encrypted_password = jappixmini_addon_xor(client_secret, password);
62
63         encrypted_password = encodeURI(encrypted_password)
64         return encrypted_password;
65 }
66
67 function jappixmini_addon_decrypt_password(encrypted_password) {
68         encrypted_password = decodeURI(encrypted_password);
69
70         client_secret = jappixmini_addon_get_client_secret();
71
72         // xor password with secret
73         password = jappixmini_addon_xor(client_secret, encrypted_password);
74
75         // remove \0
76         first_null = password.indexOf("\0")
77         password = password.substr(0, first_null);
78
79         return password;
80 }
81
82 function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
83         // listen for subscriptions
84         con.registerHandler('presence',function(presence){
85                 var type = presence.getType();
86                 if (type != "subscribe") return;
87
88                 var from = fullXID(getStanzaFrom(presence));
89                 var xid = bareXID(from);
90
91                 approve = true;
92                 if ((!autoapprove) || contacts[xid]===undefined)
93                         approve = confirm("Accept "+xid+" for chat?");
94
95                 if (approve) {
96                         acceptSubscribe(xid, contacts[xid]);
97                         console.log("Accepted "+xid+" for chat.");
98                 }
99         });
100
101         // autosubscribe
102         if (autosubscribe) {
103                 for (i=0; i<contacts.length; i++) {
104                         xid = contacts[i];
105                         sendSubscribe(xid, "subscribe");
106                         console.log("Subscribed to "+xid);
107                 }
108         }
109 }
110
111 function jappixmini_addon_subscribe() {
112         if (!window.con) {
113                 alert("Not connected.");
114                 return;
115         }
116
117         xid = prompt("Jabber address");
118         sendSubscribe(xid, "subscribe");
119 }
120
121 function jappixmini_addon_start(server, username, bosh, encrypted, password, nickname) {
122     // decrypt password
123     if (encrypted)
124         password = jappixmini_addon_decrypt_password(password);
125
126     // check if settings have changed, reinitialize jappix mini if this is the case
127     settings_identifier = str_sha1(server);
128     settings_identifier += str_sha1(username);
129     settings_identifier += str_sha1(bosh);
130     settings_identifier += str_sha1(password);
131     settings_identifier += str_sha1(nickname);
132
133     saved_identifier = getDB("jappix-mini", "settings_identifier");
134     if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
135     setDB("jappix-mini", "settings_identifier", settings_identifier);
136
137     // set bosh host
138     if (bosh)
139         HOST_BOSH = HOST_BOSH+"?host_bosh="+encodeURI(bosh);
140
141     // start jappix mini
142     MINI_NICKNAME = nickname;
143     launchMini(true, false, server, username, password);
144 }