1 function jappixmini_addon_xor(str1, str2) {
2 if (str1.length != str2.length) throw "not same length";
6 for (i=0; i<str1.length;i++) {
7 var a = str1.charCodeAt(i);
8 var b = str2.charCodeAt(i);
11 encoded += String.fromCharCode(c);
17 function jappixmini_addon_set_client_secret(password) {
18 if (!password) return;
20 salt1 = "h8doCRekWto0njyQohKpdx6BN0UTyC6N";
21 salt2 = "jdX8OwFC1kWAq3s9uOyAcE8g3UNNO5t3";
23 client_secret1 = str_sha1(salt1+password);
24 client_secret2 = str_sha1(salt2+password);
25 client_secret = client_secret1 + client_secret2;
27 setPersistent('jappix-mini', 'client-secret', client_secret);
28 console.log("client secret set");
31 function jappixmini_addon_get_client_secret(callback) {
32 client_secret = getPersistent('jappix-mini', 'client-secret');
33 if (client_secret===null) {
34 div = document.getElementById("#jappixmini-password-query-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>');
39 input = $('<input type="password" id="jappixmini-password-query-input">')
42 button = $('<input type="button" value="OK" id="jappixmini-password-query-button">');
45 $("body").append(div);
48 button.click(function(){
49 password = $("#jappixmini-password-query-input").val();
50 jappixmini_addon_set_client_secret(password);
53 client_secret = getPersistent('jappix-mini', 'client-secret');
54 callback(client_secret);
58 callback(client_secret);
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) {
70 // xor password with secret
71 encrypted_password = jappixmini_addon_xor(client_secret, password);
73 encrypted_password = encodeURI(encrypted_password)
74 callback(encrypted_password);
78 function jappixmini_addon_decrypt_password(encrypted_password, callback) {
79 encrypted_password = decodeURI(encrypted_password);
81 jappixmini_addon_get_client_secret(function(client_secret){
82 // xor password with secret
83 password = jappixmini_addon_xor(client_secret, encrypted_password);
86 first_null = password.indexOf("\0")
87 if (first_null==-1) throw "Decrypted password does not contain \\0";
88 password = password.substr(0, first_null);
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;
100 var from = fullXID(getStanzaFrom(presence));
101 var xid = bareXID(from);
104 if ((!autoapprove) || contacts[xid]===undefined)
105 approve = confirm("Accept "+xid+" for chat?");
108 acceptSubscribe(xid, contacts[xid]);
109 console.log("Accepted "+xid+" for chat.");
114 if (!autosubscribe) return;
116 var get_roster = new JSJaCIQ();
117 get_roster.setType('get');
118 get_roster.setQuery(NS_ROSTER);
120 con.send(get_roster, function(iq){
121 var handleXML = iq.getQuery();
123 // filter out contacts that are already in the roster
124 $(handleXML).find('item').each(function() {
125 xid = $(this).attr("jid");
126 name = $(this).attr("name");
127 subscription = $(this).attr("subscription");
128 console.log(xid+" "+subscription);
130 // ignore accounts not in the list
131 if (contacts[xid]===undefined) return;
133 // TODO: add to Friendica group
135 // TODO: unblock and authorize if necessary
138 delete contacts[xid];
141 // go through remaining contacts
142 for (var xid in contacts) {if(!contacts.hasOwnProperty(xid)) continue;
144 var presence = new JSJaCPresence();
146 presence.setType("subscribe");
148 console.log("subscribed to "+xid);
151 var iq = new JSJaCIQ();
153 var iqQuery = iq.setQuery(NS_ROSTER);
154 var item = iqQuery.appendChild(iq.buildNode('item', {'xmlns': NS_ROSTER, 'jid': xid}));
155 item.setAttribute('name', contacts[xid]);
156 item.appendChild(iq.buildNode('group', {'xmlns': NS_ROSTER}, "Friendica"));
158 console.log("added to roster "+xid);
163 function jappixmini_addon_subscribe() {
165 alert("Not connected.");
169 xid = prompt("Jabber address");
170 sendSubscribe(xid, "subscribe");
173 function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) {
174 handler = function(password){
175 // check if settings have changed, reinitialize jappix mini if this is the case
176 settings_identifier = str_sha1(server);
177 settings_identifier += str_sha1(username);
178 settings_identifier += str_sha1(proxy);
179 settings_identifier += str_sha1(bosh);
180 settings_identifier += str_sha1(password);
181 settings_identifier += str_sha1(nickname);
183 saved_identifier = getDB("jappix-mini", "settings_identifier");
184 if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
185 setDB("jappix-mini", "settings_identifier", settings_identifier);
189 HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh);
194 MINI_NICKNAME = nickname;
196 console.log("launchMini");
197 launchMini(true, false, server, username, password);
199 // increase priority over other Jabber clients
201 sendPresence(null,null,priority);
203 jappixmini_manage_roster(contacts, autoapprove, autosubscribe)
206 // decrypt password if necessary
208 jappixmini_addon_decrypt_password(password, handler);