]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/lib.js
add jappixmini addon
[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         client_secret = str_sha1("client_secret:"+password);
19         setDB('jappix-mini', 'client_secret', client_secret);
20 }
21
22 function jappixmini_addon_get_client_secret() {
23         client_secret = getDB('jappix-mini', 'client_secret');
24         if (client_secret===null) {
25                 div = $('<div style="position:fixed;padding:1em;background-color:#F00;color:#fff;top:50px;left:50px;" id="x123">Reintroduce your Friendica password for chatting:</div>');
26                 div.append($("<br>"));
27                 input = $('<input type="password">')
28                 div.append(input);
29                 button = $('<input type="button" value="OK">');
30                 button.click(function(){
31                         password = input.val();
32                         jappixmini_addon_set_client_secret(password);
33                         div.remove();
34                 });
35                 div.append(button);
36                 $("body").append(div);
37         }
38
39         return client_secret;
40 }
41
42 function jappixmini_addon_encrypt_password(password) {
43         client_secret = jappixmini_addon_get_client_secret();
44
45         // add \0 to password until it has the same length as secret
46         if (client_secret.length<password.length) throw "password too long";
47         while (password.length<client_secret.length) {
48                 password += "\0";
49         }
50
51         // xor password with secret
52         encrypted_password = jappixmini_addon_xor(client_secret, password);
53
54         encrypted_password = encodeURI(encrypted_password)
55         return encrypted_password;
56 }
57
58 function jappixmini_addon_decrypt_password(encrypted_password) {
59         encrypted_password = decodeURI(encrypted_password);
60
61         client_secret = jappixmini_addon_get_client_secret();
62
63         // xor encrypted password with secret
64         password = jappixmini_addon_xor(client_secret, encrypted_password);
65
66         // remove \0
67         first_null = password.indexOf("\0")
68         password = password.substr(0, first_null);
69
70         return password;
71 }
72
73 function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
74         // listen for subscriptions
75         con.registerHandler('presence',function(presence){
76                 var type = presence.getType();
77                 if (type != "subscribe") return;
78
79                 var from = fullXID(getStanzaFrom(presence));
80                 var xid = bareXID(from);
81
82                 approve = true;
83                 if ((!autoapprove) || ($.inArray(xid, contacts) == -1))
84                         approve = confirm("Accept "+xid+" for chat?");
85
86                 if (approve) {
87                         acceptSubscribe(xid);
88                         //alert("Accepted "+xid+" for chat.");
89                 }
90         });
91
92         // autosubscribe
93         if (autosubscribe) {
94                 for (i=0; i<contacts.length; i++) {
95                         xid = contacts[i];
96                         sendSubscribe(xid, "subscribe");
97                 }
98         }
99 }
100
101 function jappixmini_addon_start(server, username, bosh, encrypted_password) {
102     // check if settings have changed, reinitialize jappix mini if this is the case
103     settings_identifier = str_sha1(server);
104     settings_identifier += str_sha1(username);
105     settings_identifier += str_sha1(bosh);
106     settings_identifier += str_sha1(encrypted_password);
107
108     saved_identifier = getDB("jappix-mini", "settings_identifier");
109     if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
110     setDB("jappix-mini", "settings_identifier", settings_identifier);
111
112     // set bosh host
113     HOST_BOSH = HOST_BOSH+"?host_bosh="+encodeURI(bosh);
114
115     // decrypt password
116     password = jappixmini_addon_decrypt_password(encrypted_password);
117
118     // start jappix mini
119     launchMini(true, false, server, username, password);
120 }